diff options
| -rw-r--r-- | input.rb | 55 | ||||
| -rw-r--r-- | run.rb | 99 | ||||
| -rw-r--r-- | tileset.rb | 7 | ||||
| -rw-r--r-- | ui-tools.rb | 243 | ||||
| -rw-r--r-- | ui.rb | 73 |
5 files changed, 342 insertions, 135 deletions
diff --git a/input.rb b/input.rb new file mode 100644 index 0000000..22d1bee --- /dev/null +++ b/input.rb @@ -0,0 +1,55 @@ +require 'ruby2d' + +class Input + @down = {} + @held = {} + @up = {} + + def self.down(char, toggle_on: false) + #puts 'work' if char == 'left' if toggle_on + if toggle_on + @down[char] = true + else + !@down[char].nil? + end + end + + def self.held(char, toggle_on: false) + if toggle_on + @held[char] = true + else + !@held[char].nil? + end + end + + def self.up(char, toggle_on: false) + if toggle_on + @up[char] = true + else + !@up[char].nil? + end + end + + class <<self + alias pressed down + alias released up + end + + def self.any(char) + !@pressed[char].nil? || !@held[char].nil? || !@released[char].nil? + end + + def self.reset + @down = {} + @held = {} + @up = {} + end +end + +on :mouse do |event| + Input.public_send(event.type, event.button.to_s, toggle_on: true) unless event.button.nil? +end + +on :key do |event| + Input.public_send(event.type, event.key, toggle_on: true) +end @@ -3,39 +3,26 @@ require 'ruby2d' require_relative 'lib/camera/camera' require_relative 'tileset' -require_relative 'ui-tools' - -set width: 1024, height: 720 +require_relative 'ui' +require_relative 'input' +set width: 640, height: 480 +#set width: 1280, height: 720 +#set width: 1920, height: 1080 set background: 'navy' +UI.load + @eks = 0 @why = 0 -on :key_held do |event| - if event.key == 'w' - Camera.y += -10 - end - if event.key == 's' - Camera.y += 10 - end - if event.key == 'a' - Camera.x += -10 - end - if event.key == 'd' - Camera.x += 10 - end -end -on :mouse_up do |event| - case event.button - when :left - @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 +# Create on click + #@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 + on :mouse_scroll do |event| @selected_item[0] += event.delta_y if @selected_item[0] >= @yep.columns @@ -59,33 +46,55 @@ end end end -@selected_item = [0,0] -@selected_image = @yep.create_image(column: @selected_item[0], row: @selected_item[1]) +#@selected_item = [0,0] +#@selected_image = @yep.create_image(column: @selected_item[0], row: @selected_item[1]) 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') - +UI::Main.mouse_follow.objects.each do |object| + Camera << object +end @timer = 0 update do + Camera.x += 10 if Input.held('d') + Camera.x -= 10 if Input.held('a') + Camera.y -= 10 if Input.held('w') + Camera.y += 10 if Input.held('s') @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: (Camera.mouse[0] - (Camera.mouse[0] % (128 + 12))), - y: (Camera.mouse[1] - (Camera.mouse[1] % (128 + 12)))) - Camera << @selected_image + #Camera.remove @selected_image + #@selected_image.remove + UI::Main.mouse_follow.x = (Camera.mouse[0] - (Camera.mouse[0] % (128 + 12) + 12)) + UI::Main.mouse_follow.y = (Camera.mouse[1] - (Camera.mouse[1] % (128 + 12) + 12)) + UI::Main.history_slots.each do |item| + # Ignore if to the left of UI boxes + if (Window.mouse_x - 30).positive? && \ + # Ignore if to the right of UI boxes + (Window.mouse_x < (UI::Main.history_slots.length * 75) + 15) && \ + # If within a box, and not between 2 boxes + (((Window.mouse_x - 30) % 75) <= 60) && \ + # Ignore below boxes + (Window.mouse_y >= Window.height - 90) && \ + # Ignore above boxes + (Window.mouse_y <= Window.height - 30) + if item[0].contains(Window.mouse_x, Window.mouse_y) + item[0].base_color = 'lime' + else + item[0].base_color = 'green' + end + end + end + #@selected_image = @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 << @selected_image + puts "yep" if Input.down('left') + puts "yup" if Input.down('middle') + puts "yorp" if Input.down('right') + Input.reset Camera.redraw end @@ -7,7 +7,6 @@ class Tileset :width, :height def initialize(directory, width, height) - puts directory @width = width @height = height if directory[-1] == '/' @@ -16,20 +15,16 @@ class Tileset directory = "#{directory}/*" end - puts directory images = Dir[directory].sort factors = divisors_of(images.length) - puts factors @columns = factors[factors.length / 2] @rows = factors[(factors.length / 2) - 1] - @tileset = Array.new(@columns, nil) { Array.new(@rows, nil) } + @tileset = Array.new(@columns) { Array.new(@rows, nil) } images.each_with_index do |image, index| x = index % @columns - puts "Image #{index} is #{image}" #@tileset[x] = [] if @tileset[x].nil? @tileset[x][((index - x) / @columns)] = image - #puts @tileset[x][((index - x) / 23)] end end diff --git a/ui-tools.rb b/ui-tools.rb index 3e19ef4..611566b 100644 --- a/ui-tools.rb +++ b/ui-tools.rb @@ -2,20 +2,25 @@ require 'ruby2d' module UIToolkit # a nice looking box for placing text inside + # only StandardTextbox works as intended module Textbox class StandardTextbox - attr_reader :objects + attr_reader :objects, :base, + :top_border, :right_border, + :left_border, :bottom_border, + :base_color, :swap_colors - def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a') + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a', swap_colors: false, z: 99) @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) + @base ||= Rectangle.new(x: border_width + x, + y: border_width + y, + width: width - (border_width * 2), + height: height - (border_width * 2), + color: @base_color, + z: z) @objects.push @base @top_border = Quad.new(x1: x, y1: y, @@ -25,7 +30,8 @@ module UIToolkit y3: y + border_width, x4: x + border_width, y4: y + border_width, - color: border_light) + color: border_light, + z: z) @objects.push @top_border @right_border = Quad.new(x1: x + width, y1: y, @@ -35,7 +41,8 @@ module UIToolkit y3: y + height - border_width, x4: x + width - border_width, y4: y + border_width, - color: border_light) + color: border_light, + z: z) @objects.push @right_border @bottom_border = Quad.new(x1: x, y1: y + height, @@ -45,7 +52,8 @@ module UIToolkit y3: y + height - border_width, x4: x + border_width, y4: y + height - border_width, - color: border_dark) + color: border_dark, + z: z) @objects.push @bottom_border @left_border = Quad.new(x1: x, y1: y, @@ -55,8 +63,11 @@ module UIToolkit y3: y + height - border_width, x4: x + border_width, y4: y + border_width, - color: border_dark) + color: border_dark, + z: z) @objects.push @left_border + @swap_colors = false + self.swap_colors = swap_colors end def base_color=(base_color) @@ -70,6 +81,28 @@ module UIToolkit @left_border.color = border_dark end + def swap_colors=(swap_colors) + unless swap_colors == @swap_colors + if swap_colors + 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]) + @left_border.color = @base_color + @bottom_border.color = @base_color + @top_border.color = border_dark + @right_border.color = border_dark + @base.color = border_light + else + 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]) + @left_border.color = border_dark + @bottom_border.color = border_dark + @top_border.color = border_light + @right_border.color = border_light + @base.color = @base_color + end + end + end + def x @top_border.x1 end @@ -85,7 +118,7 @@ module UIToolkit end def y - @top_border.x1 + @top_border.y1 end def y=(y) @@ -133,11 +166,56 @@ module UIToolkit @left_border.y2 += height @left_border.y3 += height end + + def border_width + @top_border.y4 - @top_border.y1 + end + + def border_width=(border_width) + border_width -= self.border_width + @base.width += border_width * 2 + @base.height += border_width * 2 + @top_border.x3 += border_width + @top_border.y3 -= border_width + @top_border.x4 -= border_width + @top_border.y4 -= border_width + @right_border.x3 += border_width + @right_border.y3 += border_width + @right_border.x4 += border_width + @right_border.y4 -= border_width + @bottom_border.x3 += border_width + @bottom_border.y3 += border_width + @bottom_border.x4 -= border_width + @bottom_border.y4 += border_width + @left_border.x3 -= border_width + @left_border.y3 += border_width + @left_border.x4 -= border_width + @left_border.y4 -= border_width + @base.x = @top_border.x4 + @base.y = @top_border.y4 + end + + def z + @object[0].z + end + + def z=(z) + + @objects.each do |object| + object.z = z + end + end + + def contains(x, y) + (x >= self.x) && (x <= self.x + width) && (y >= self.y) && (y <= self.y + 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) + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a', z: 99) + super(x: x + width - border_width, y: y, width: -(width - (border_width * 2)), height: height, border_width: border_width, base_color: base_color, z: z) + @base.x += border_width + @base.width -= border_width * 2 end def x @@ -145,7 +223,7 @@ module UIToolkit end def width - @top_border.x3 - @top_border.x4 + @top_border.x4 - @top_border.x3 end def width=(width) @@ -163,8 +241,12 @@ module UIToolkit self.x += width end end + class RoundedTextbox < TaperedTextbox - def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a') + attr_reader :top_left, :top_right, + :bottom_right, :bottom_left + + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a', z: 99) @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]) @@ -175,21 +257,21 @@ module UIToolkit 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) + 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) + 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) + y: y + height - border_width, + radius: border_width, + color: border_dark) + @objects << @bottom_left + super(x: x, y: y, width: width, height: height, border_width: border_width, base_color: base_color, z: z) end def x=(x) @@ -219,71 +301,64 @@ module UIToolkit end end end + def width=(width) - #@top_left.x -= width - #@top_right.x -= width + tempwidth = width - self.width + @top_left.x -= tempwidth + @bottom_left.x -= tempwidth super(width) 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_left.color = border_light + @top_right.color = @bottom_left.color = @bottom_right.color = border_dark + super(base_color) + end + + def border_width=(border_width) + super(border_width) + @top_left.x = x + border_width + @top_left.y = y + border_width + @top_left.radius = border_width + @top_right.x = x + width - border_width + @top_right.y = y + border_width + @top_right.radius = border_width + @bottom_right.x = x + width - border_width + @bottom_right.y = y + height - border_width + @bottom_right.radius = border_width + @bottom_left.x = x + border_width + @bottom_left.y = y + height - border_width + @bottom_left.radius = border_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 + # Only StandardBorderbox works as intended + module Borderbox + class StandardBorderbox < UIToolkit::Textbox::StandardTextbox + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a', z: 99) + super(x: x, y: y, width: width, height: height, border_width: border_width, base_color: base_color, z: z) + @base.remove + @objects.delete @base + end 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 + class TaperedBorderbox < UIToolkit::Textbox::TaperedTextbox + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a', z: 99) + super(x: x, y: y, width: width, height: height, border_width: border_width, base_color: base_color, z: z) + @base.remove + @objects.delete @base + end + end + class RoundedBorderbox < UIToolkit::Textbox::RoundedTextbox + def initialize(x: 0, y: 0, width: 90, height: 90, border_width: 10, base_color: '#ff7f2a', z: 99) + super(x: x, y: y, width: width, height: height, border_width: border_width, base_color: base_color, z: z) + @base.remove + @objects.delete @base + end end end end @@ -0,0 +1,73 @@ +require 'ruby2d' +require_relative 'ui-tools' + +module UI + def self.load + UI::Main.load + UI::Popup.load + end + + module Main + class <<self + attr_reader :objects, :mouse_follow, :history_slots + end + + def self.height + 120 + end + + def self.load + @objects = [] + @objects.push UIToolkit::Textbox::StandardTextbox.new(y: Window.height - 120, + height: height, + width: Window.width, + border_width: 12, + base_color: 'orange', + z: 99) + @history_slots = Array.new((((Window.width - 90) / 75) - 1), nil) + puts @history_slots.length + (0...@history_slots.length).each do |iterate| + @history_slots[iterate] = [UIToolkit::Borderbox::StandardBorderbox.new(y: Window.height - 90, + x: (75 * (iterate + 1)) - 45, + width: 60, + height: 60, + border_width: 5, + base_color: 'green', + z: 99), nil] + @objects.push @history_slots[iterate][0] + end + @history_slots[0][0].base_color = 'lime' + @popup_button = UIToolkit::Textbox::StandardTextbox.new(y: Window.height - 90, + x: @objects.last.x + 75, + width: (Window.width - 30) - (@objects.last.x + 75), + height: 60, + border_width: 5, + base_color: 'green', + z: 99) + @objects.push @popup_button + @mouse_follow = UIToolkit::Borderbox::TaperedBorderbox.new(width: 128 + 24, + height: 128 + 24, + border_width: 24, + base_color: 'fuchsia', + z: 98) + @objects.push @mouse_follow + end + end + module Popup + class <<self + attr_reader :objects, :menubox + end + + def self.load + @objects = [] + @menubox = UIToolkit::Textbox::StandardTextbox.new(x: Window.width * 0.01, + y: Window.width * 0.01, + width: Window.width * 0.98, + height: Window.height - (Window.width * 0.02) - 120, + border_width: 12, + base_color: [1.0, 0.52, 0.1, 0.8], + z:99) + @objects.push @menubox + end + end +end |
