summaryrefslogtreecommitdiffhomepage
path: root/ui.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ui.rb')
-rw-r--r--ui.rb72
1 files changed, 70 insertions, 2 deletions
diff --git a/ui.rb b/ui.rb
index 2ddac87..4a8d114 100644
--- a/ui.rb
+++ b/ui.rb
@@ -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