diff options
| author | realtradam <[email protected]> | 2021-05-08 05:21:24 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-05-08 05:21:24 -0400 |
| commit | b1ac1abcdbe11c0bb465fe042eaba2ab9e012a5a (patch) | |
| tree | e10790fc1858649874f20ca36efd09cc3a07cfa0 | |
| parent | 625042cef2f11211ab4cae145ea5cf309994312e (diff) | |
| download | tileset-map-editor-master.tar.gz tileset-map-editor-master.zip | |
| -rw-r--r-- | math_plus.rb | 5 | ||||
| -rw-r--r-- | run.rb | 56 | ||||
| -rw-r--r-- | tileset.rb | 5 | ||||
| -rw-r--r-- | ui-tools.rb | 19 | ||||
| -rw-r--r-- | ui.rb | 72 |
5 files changed, 132 insertions, 25 deletions
diff --git a/math_plus.rb b/math_plus.rb new file mode 100644 index 0000000..eac8d21 --- /dev/null +++ b/math_plus.rb @@ -0,0 +1,5 @@ +module MathPlus + def self.divisors_of(num) + (1..num).select { |n| (num % n).zero? } + end +end @@ -7,8 +7,9 @@ require_relative 'ui' require_relative 'input' set width: 640, height: 480 -#set width: 1280, height: 720 -#set width: 1920, height: 1080 +set width: 1280, height: 720 +set width: 1920, height: 1080 + set background: 'navy' UI.load @@ -42,7 +43,7 @@ end ([email protected]).each do |row| ([email protected]).each do |column| - Camera << @yep.create_image(column: column, row: row, x: (column * (128 + 12)), y: (row * (128 + 12))) + #Camera << @yep.create_image(column: column, row: row, x: (column * (128 + 12)), y: (row * (128 + 12))) end end @@ -54,6 +55,9 @@ Camera.zoom = 0.25 UI::Main.mouse_follow.objects.each do |object| Camera << object end + +UI::Popup.menubox.base.color = ['orange', 'yellow', 'red', 'maroon'] + @timer = 0 update do Camera.x += 10 if Input.held('d') @@ -68,17 +72,17 @@ update do #@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) + # 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) + UI::Main.history_slots.each do |item| if item[0].contains(Window.mouse_x, Window.mouse_y) item[0].base_color = 'lime' else @@ -86,14 +90,32 @@ update do end end end + if UI::Main.popup_button.contains(Window.mouse_x, Window.mouse_y) + if Input.down('left') + UI::Main.popup_button.swap_colors = true + if UI::Popup.showing_menu = !UI::Popup.showing_menu + UI::Popup.tiles.each do |tile| + tile.add + end + UI::Main.mouse_follow.remove + else + UI::Popup.tiles.each do |tile| + tile.remove + end + UI::Main.mouse_follow.add + end + elsif Input.up('left') + UI::Main.popup_button.swap_colors = false + end + else + UI::Main.popup_button.swap_colors = false + 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 @@ -28,12 +28,13 @@ class Tileset end end - def create_image(column:, row:, x: 0, y: 0) + def create_image(column:, row:, x: 0, y: 0, z: 0, width: self.width, height: self.height) Image.new(tileset[column][row], width: width, height: height, x: x, - y: y) + y: y, + z: z) end private diff --git a/ui-tools.rb b/ui-tools.rb index 611566b..6edb1a4 100644 --- a/ui-tools.rb +++ b/ui-tools.rb @@ -70,6 +70,18 @@ module UIToolkit self.swap_colors = swap_colors end + def remove + @objects.each do |object| + object.remove + end + end + + def add + @objects.each do |object| + object.add + 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]) @@ -82,12 +94,12 @@ module UIToolkit end def swap_colors=(swap_colors) - unless swap_colors == @swap_colors if swap_colors + border_base= Color.new([[@base_color.r - 0.1, 0].max, [@base_color.g - 0.1, 0].max, [@base_color.b - 0.1, 0].max, @base_color.a]) 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 + @left_border.color = border_base + @bottom_border.color = border_base @top_border.color = border_dark @right_border.color = border_dark @base.color = border_light @@ -100,7 +112,6 @@ module UIToolkit @right_border.color = border_light @base.color = @base_color end - end end def x @@ -1,5 +1,6 @@ require 'ruby2d' require_relative 'ui-tools' +require_relative 'math_plus' module UI def self.load @@ -9,7 +10,7 @@ module UI module Main class <<self - attr_reader :objects, :mouse_follow, :history_slots + attr_reader :objects, :mouse_follow, :history_slots, :popup_button end def self.height @@ -55,7 +56,7 @@ module UI end module Popup class <<self - attr_reader :objects, :menubox + attr_reader :objects, :menubox, :tileset, :tiles end def self.load @@ -67,7 +68,74 @@ module UI border_width: 12, base_color: [1.0, 0.52, 0.1, 0.8], z:99) + @menubox.remove @objects.push @menubox + maxinnerwidth = @menubox.width - 24 + maxinnerheight = @menubox.height - 24 + menuaspect = maxinnerwidth / maxinnerheight.to_f + @tileset = Tileset.new('./assets/kenny/PNG/128', 128, 128) + tilesetaspect = (@tileset.width * @tileset.columns) / (@tileset.height * @tileset.rows).to_f + use_divisor = nil + if menuaspect > tilesetaspect + # make height match or less + totalheight = @tileset.height * @tileset.rows + if @tileset.width <= @tileset.height + MathPlus.divisors_of(@tileset.width).sort.each do |divisor| + if divisor == @tileset.width + # exausted search of even divisor and failed, just use exact uneven match instead + use_divisor = (@tileset.width * @tileset.columns) / maxinnerwidth.to_f + break + end + puts "(#{totalheight} / #{divisor}) + #{@tileset.rows} < #{maxinnerheight}" + if (totalheight / divisor) + @tileset.rows < maxinnerheight + # use divisor + use_divisor = divisor.to_f + break + end + end + else + MathPlus.divisors_of(@tileset.height).sort.each do |divisor| + if (totalheight / divisor) + @tileset.rows < maxinnerheight + # use divisor + use_divisor = divisor.to_f + break + end + end + end + else + # make width match or less + end + @tiles = [] + ([email protected]).each do |row| + ([email protected]).each do |column| + @tiles.push @tileset.create_image(column: column, + row: row, + x: Window.width - (@menubox.x + 48 + (@tileset.width * @tileset.columns) / use_divisor) + (column * ((@tileset.width / use_divisor) + 1)), + y: Window.height - (@menubox.y + 36 + 120 + (@tileset.height * @tileset.rows) / use_divisor) + (row * ((@tileset.height / use_divisor) + 1)), + width: @tileset.width / use_divisor, + height: @tileset.height / use_divisor, + z: 101) + @objects.push @tiles.last + @tiles.last.remove + end + end + @menubox.x = @tiles[0].x - 24 + @menubox.y = @tiles[0].y - 24 + @menubox.width = 48 + ((@tileset.width * @tileset.columns) / use_divisor) + @tileset.columns - 1 + @menubox.height = 48 + ((@tileset.height * @tileset.rows) / use_divisor) + @tileset.rows - 1 + end + + def self.showing_menu + @showing_menu ||= false + end + + def self.showing_menu=(show) + if show + @menubox.add + else + @menubox.remove + end + @showing_menu = show end end end |
