diff --git a/lib/cyberarm_engine/ui/container.rb b/lib/cyberarm_engine/ui/container.rb index bf1f5cd..7854647 100644 --- a/lib/cyberarm_engine/ui/container.rb +++ b/lib/cyberarm_engine/ui/container.rb @@ -3,10 +3,11 @@ module CyberarmEngine include Common attr_accessor :stroke_color, :fill_color - attr_reader :children + attr_reader :children, :gui_state attr_reader :scroll_x, :scroll_y def initialize(options = {}, block = nil) + @gui_state = options.delete(:gui_state) super @scroll_x, @scroll_y = 0, 0 diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index 32448f2..4fe7f0f 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -119,17 +119,17 @@ module CyberarmEngine def toggle @visible = !@visible - root.recalculate + root.gui_state.request_recalculate end def show @visible = true - root.recalculate + root.gui_state.request_recalculate end def hide @visible = false - root.recalculate + root.gui_state.request_recalculate end def draw diff --git a/lib/cyberarm_engine/ui/gui_state.rb b/lib/cyberarm_engine/ui/gui_state.rb index 0cacd5f..4bc16ba 100644 --- a/lib/cyberarm_engine/ui/gui_state.rb +++ b/lib/cyberarm_engine/ui/gui_state.rb @@ -10,15 +10,18 @@ module CyberarmEngine @down_keys = {} - @root_container = Stack.new + @root_container = Stack.new(gui_state: self) @game_objects << @root_container @containers = [@root_container] + @active_width = window.width + @active_height = window.height + @focus = nil @mouse_over = nil @mouse_down_on = {} @mouse_down_position = {} - + @pending_recalculate_request = false setup end @@ -35,6 +38,11 @@ module CyberarmEngine end def update + if @pending_recalculate_request + @root_container.recalculate + @pending_recalculate_request = false + end + super new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y) @@ -49,6 +57,11 @@ module CyberarmEngine redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft) redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle) redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight) + + request_recalculate if @active_width != window.width || @active_height != window.height + + @active_width = window.width + @active_height = window.height end def button_down(id) @@ -115,5 +128,10 @@ module CyberarmEngine def redirect_mouse_wheel(button) @mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over end + + # Schedule a full GUI recalculation on next update + def request_recalculate + @pending_recalculate_request = true + end end end \ No newline at end of file diff --git a/lib/cyberarm_engine/ui/label.rb b/lib/cyberarm_engine/ui/label.rb index 7777d4a..c713113 100644 --- a/lib/cyberarm_engine/ui/label.rb +++ b/lib/cyberarm_engine/ui/label.rb @@ -37,7 +37,7 @@ module CyberarmEngine old_width, old_height = width, height recalculate - root.recalculate if old_width != width || old_height != height + root.gui_state.request_recalculate if old_width != width || old_height != height end end end \ No newline at end of file