From 200d6ec73caf9c070375a972677c4c0af7c444ba Mon Sep 17 00:00:00 2001 From: realtradam Date: Wed, 5 May 2021 06:45:59 -0400 Subject: . --- assets/kenny/PNG/128/towerDefense_tile000.png | Bin 0 -> 2154 bytes lib/camera/camera.rb | 25 ++- run.rb | 41 +++- ui-tools.rb | 289 ++++++++++++++++++++++++++ 4 files changed, 344 insertions(+), 11 deletions(-) create mode 100644 assets/kenny/PNG/128/towerDefense_tile000.png create mode 100644 ui-tools.rb diff --git a/assets/kenny/PNG/128/towerDefense_tile000.png b/assets/kenny/PNG/128/towerDefense_tile000.png new file mode 100644 index 0000000..0e8550c Binary files /dev/null and b/assets/kenny/PNG/128/towerDefense_tile000.png differ diff --git a/lib/camera/camera.rb b/lib/camera/camera.rb index 64e2095..e34d2d8 100644 --- a/lib/camera/camera.rb +++ b/lib/camera/camera.rb @@ -69,8 +69,8 @@ module Camera if auto_purge objects.each do |item| if item.nil? - puts "Warning: Nil Object detected in Camera" - puts " Nil Object removed" + puts 'Warning: Nil Object detected in Camera' + puts ' Nil Object removed' objects.delete(obj) else item.redraw @@ -81,6 +81,27 @@ module Camera end end + # Convert screenspace coordinates into worldspace camera ones + def self.coordinate_to_worldspace(x, y) + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + + [(((x - half_width) / zoom) * Math.cos(-angle)) - (((y - half_height) / zoom) * Math.sin(-angle)) + self.x, + (((x - half_width) / zoom) * Math.sin(-angle)) + (((y - half_height) / zoom) * Math.cos(-angle)) + self.y] + end + + # Convert worldspace camera coordinates into screenspace ones + def self.coordinate_to_screenspace(x, y) + angle = Camera.angle * (Math::PI / 180) + [(((x - Camera.x) * Math.cos(angle)) - ((y - Camera.y) * Math.sin(angle))) * Camera.zoom + (Window.width * 0.5), + (((x - Camera.x) * Math.sin(angle)) + ((y - Camera.y) * Math.cos(angle))) * Camera.zoom + (Window.height * 0.5)] + end + + def self.mouse + coordinate_to_worldspace(Window.mouse_x, Window.mouse_y) + end + # Variables changing Camera properties def self._x(x) @x += x diff --git a/run.rb b/run.rb index 46a9d16..b441931 100644 --- a/run.rb +++ b/run.rb @@ -3,9 +3,12 @@ require 'ruby2d' require_relative 'lib/camera/camera' require_relative 'tileset' +require_relative 'ui-tools' set width: 1024, height: 720 +set background: 'navy' + @eks = 0 @why = 0 @@ -26,17 +29,21 @@ end on :mouse_up do |event| case event.button when :left - @new = @yep.create_image(column: @selected_item[0], - row: @selected_item[1], - x: Window.mouse_x - (Window.mouse_x % 128), - y: Window.mouse_y - (Window.mouse_y % 128)) + @new = @yep.create_image(column: @selected_item[0], + row: @selected_item[1], + x: (Camera.mouse[0] - (Camera.mouse[0] % (128 + 12))), + y: (Camera.mouse[1] - (Camera.mouse[1] % (128 + 12)))) + Camera << @new end end on :mouse_scroll do |event| @selected_item[0] += event.delta_y - @selected_item[1] += @selected_item[0] / @yep.columns + if @selected_item[0] >= @yep.columns + @selected_item[1] = (@selected_item[1] + 1) % @yep.rows + elsif @selected_item[0].negative? + @selected_item[1] = (@selected_item[1] - 1) % @yep.rows + end @selected_item[0] %= @yep.columns - @selected_item[1] %= @yep.rows end @yep = Tileset.new('./assets/kenny/PNG/128', 128, 128) @@ -48,7 +55,7 @@ end (0...@yep.rows).each do |row| (0...@yep.columns).each do |column| - Camera << @yep.create_image(column: column, row: row, x: (column * 128) + (12 * column), y: (row * 128) + (row * 12)) + Camera << @yep.create_image(column: column, row: row, x: (column * (128 + 12)), y: (row * (128 + 12))) end end @@ -57,12 +64,28 @@ end Camera.zoom = 0.25 +@textbox = UIToolkit::Textbox::RoundedTextbox.new(y: Window.height - 100, height: 100, width: Window.width, border_width: 35, base_color: 'fuchsia') +@borderbox = UIToolkit::Borderbox.new(height: 100, width: Window.width - 30, border_width: 15, base_color: 'fuchsia') + +@timer = 0 update do + @timer += 1 + @timer %= 60 + if (@timer % 30).zero? + @textbox.base_color = Color.new 'random' + @textbox.x += 1 + @textbox.y -= 1 + @textbox.width -= 15 + @borderbox.base_color = Color.new 'random' + puts @textbox.x + end + Camera.remove @selected_image @selected_image.remove @selected_image = @yep.create_image(column: @selected_item[0], row: @selected_item[1], - x: (Window.mouse_x - (Window.mouse_x % 128)), - y: (Window.mouse_y - (Window.mouse_y % 128))) + x: (Camera.mouse[0] - (Camera.mouse[0] % (128 + 12))), + y: (Camera.mouse[1] - (Camera.mouse[1] % (128 + 12)))) + Camera << @selected_image Camera.redraw end diff --git a/ui-tools.rb b/ui-tools.rb new file mode 100644 index 0000000..3e19ef4 --- /dev/null +++ b/ui-tools.rb @@ -0,0 +1,289 @@ +require 'ruby2d' + +module UIToolkit + # a nice looking box for placing text inside + module Textbox + class StandardTextbox + attr_reader :objects + + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a') + @base_color = Color.new(base_color) + border_light = Color.new([[@base_color.r - 0.2, 0].max, [@base_color.g - 0.2, 0].max, [@base_color.b - 0.2, 0].max, @base_color.a]) + border_dark = Color.new([[@base_color.r - 0.4, 0].max, [@base_color.g - 0.4, 0].max, [@base_color.b - 0.4, 0].max, @base_color.a]) + @objects ||= [] + @base = Rectangle.new(x: border_width + x, + y: border_width + y, + width: width - (border_width * 2), + height: height - (border_width * 2), + color: @base_color) + @objects.push @base + @top_border = Quad.new(x1: x, + y1: y, + x2: x + width, + y2: y, + x3: x + width - border_width, + y3: y + border_width, + x4: x + border_width, + y4: y + border_width, + color: border_light) + @objects.push @top_border + @right_border = Quad.new(x1: x + width, + y1: y, + x2: x + width, + y2: y + height, + x3: x + width - border_width, + y3: y + height - border_width, + x4: x + width - border_width, + y4: y + border_width, + color: border_light) + @objects.push @right_border + @bottom_border = Quad.new(x1: x, + y1: y + height, + x2: x + width, + y2: y + height, + x3: x + width - border_width, + y3: y + height - border_width, + x4: x + border_width, + y4: y + height - border_width, + color: border_dark) + @objects.push @bottom_border + @left_border = Quad.new(x1: x, + y1: y, + x2: x, + y2: y + height, + x3: x + border_width, + y3: y + height - border_width, + x4: x + border_width, + y4: y + border_width, + color: border_dark) + @objects.push @left_border + end + + def base_color=(base_color) + @base_color = Color.new(base_color) + border_light = Color.new([[@base_color.r - 0.2, 0].max, [@base_color.g - 0.2, 0].max, [@base_color.b - 0.2, 0].max, @base_color.a]) + border_dark = Color.new([[@base_color.r - 0.4, 0].max, [@base_color.g - 0.4, 0].max, [@base_color.b - 0.4, 0].max, @base_color.a]) + @base.color = @base_color + @top_border.color = border_light + @right_border.color = border_light + @bottom_border.color = border_dark + @left_border.color = border_dark + end + + def x + @top_border.x1 + end + + def x=(x) + x -= self.x + @objects.each do |object| + object.x1 += x + object.x2 += x + object.x3 += x + object.x4 += x + end + end + + def y + @top_border.x1 + end + + def y=(y) + y -= self.y + @objects.each do |object| + object.y1 += y + object.y2 += y + object.y3 += y + object.y4 += y + end + end + + def width + @top_border.x2 - @top_border.x1 + end + + def width=(width) + width -= self.width + @base.x2 += width + @base.x3 += width + @top_border.x2 += width + @top_border.x3 += width + @right_border.x1 += width + @right_border.x2 += width + @right_border.x3 += width + @right_border.x4 += width + @bottom_border.x2 += width + @bottom_border.x3 += width + end + + def height + @right_border.y2 - @right_border.y1 + end + + def height=(height) + height -= self.height + @base.y3 += height + @base.y4 += height + @right_border.y2 += height + @right_border.y3 += height + @bottom_border.y1 += height + @bottom_border.y2 += height + @bottom_border.y3 += height + @bottom_border.y4 += height + @left_border.y2 += height + @left_border.y3 += height + end + end + + class TaperedTextbox < StandardTextbox + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a') + super(x: x + width - border_width, y: y, width: -(width - (border_width * 2)), height: height, border_width: border_width, base_color: base_color) + end + + def x + @top_border.x3 + end + + def width + @top_border.x3 - @top_border.x4 + end + + def width=(width) + width -= self.width + @base.x2 -= width + @base.x3 -= width + @top_border.x2 -= width + @top_border.x3 -= width + @right_border.x1 -= width + @right_border.x2 -= width + @right_border.x3 -= width + @right_border.x4 -= width + @bottom_border.x2 -= width + @bottom_border.x3 -= width + self.x += width + end + end + class RoundedTextbox < TaperedTextbox + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a') + @objects ||= [] + @base_color = Color.new(base_color) + border_light = Color.new([[@base_color.r - 0.2, 0].max, [@base_color.g - 0.2, 0].max, [@base_color.b - 0.2, 0].max, @base_color.a]) + border_dark = Color.new([[@base_color.r - 0.4, 0].max, [@base_color.g - 0.4, 0].max, [@base_color.b - 0.4, 0].max, @base_color.a]) + @top_left = Circle.new(x: x + border_width, + y: y + border_width, + radius: border_width, + color: border_light) + @objects << @top_left + @top_right = Circle.new(x: x + width - border_width, + y: y + border_width, + radius: border_width, + color: border_dark) + @objects << @top_right + @bottom_right = Circle.new(x: x + width - border_width, + y: y + height - border_width, + radius: border_width, + color: border_dark) + @objects << @bottom_right + @bottom_left = Circle.new(x: x + border_width, + y: y + height - border_width, + radius: border_width, + color: border_dark) + @objects << @top_right + super(x: x, y: y, width: width, height: height, border_width: border_width, base_color: base_color) + end + + def x=(x) + x -= self.x + @objects.each do |object| + if !object.is_a? Circle + object.x1 += x + object.x2 += x + object.x3 += x + object.x4 += x + else + object.x += x + end + end + end + + def y=(y) + y -= self.y + @objects.each do |object| + if !object.is_a? Circle + object.y1 += y + object.y2 += y + object.y3 += y + object.y4 += y + else + object.y += y + end + end + end + def width=(width) + #@top_left.x -= width + #@top_right.x -= width + super(width) + end + end + end + + # A Hollow Textbox + class Borderbox + attr_reader :objects + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a') + @base_color = Color.new(base_color) + border_light = Color.new([[@base_color.r - 0.2, 0].max, [@base_color.g - 0.2, 0].max, [@base_color.b - 0.2, 0].max, @base_color.a]) + border_dark = Color.new([[@base_color.r - 0.4, 0].max, [@base_color.g - 0.4, 0].max, [@base_color.b - 0.4, 0].max, @base_color.a]) + @objects = [] + @top_border = Quad.new(x1: x, + y1: y, + x2: x + (border_width * 2) + width, + y2: y, + x3: x + (border_width * 1) + width, + y3: y + border_width, + x4: x + border_width, + y4: y + border_width, + color: border_light) + @objects.push @top_border + @right_border = Quad.new(x1: x + (border_width * 2) + width, + y1: y, + x2: x + (border_width * 2) + width, + y2: y + (border_width * 2) + height, + x3: x + border_width + width, + y3: y + border_width + height, + x4: x + border_width + width, + y4: y + border_width, + color: border_light) + @objects.push @right_border + @bottom_border = Quad.new(x1: x, + y1: y + (border_width * 2)+ height, + x2: x + (border_width * 2) + width, + y2: y + (border_width * 2) + height, + x3: x + (border_width * 1) + width, + y3: y + border_width + height, + x4: x + border_width, + y4: y + border_width + height, + color: border_dark) + @objects.push @right_border + @left_border = Quad.new(x1: x, + y1: y, + x2: x, + y2: y + (border_width * 2) + height, + x3: x + border_width, + y3: y + border_width + height, + x4: x + border_width, + y4: y + border_width, + color: border_dark) + @objects.push @left_border + end + def base_color=(base_color) + @base_color = Color.new(base_color) + border_light = Color.new([[@base_color.r - 0.2, 0].max, [@base_color.g - 0.2, 0].max, [@base_color.b - 0.2, 0].max, @base_color.a]) + border_dark = Color.new([[@base_color.r - 0.4, 0].max, [@base_color.g - 0.4, 0].max, [@base_color.b - 0.4, 0].max, @base_color.a]) + @top_border.color = border_light + @right_border.color = border_light + @bottom_border.color = border_dark + @left_border.color = border_dark + end + end +end -- cgit v1.2.3