Added GuiState#request_recalculation to enable requesting a gui recalc on next update, GUI state will request a recalculation if the window size changes

This commit is contained in:
2019-06-23 10:43:31 -05:00
parent 4642056576
commit e95b4c05e2
4 changed files with 26 additions and 7 deletions

View File

@@ -3,10 +3,11 @@ module CyberarmEngine
include Common include Common
attr_accessor :stroke_color, :fill_color attr_accessor :stroke_color, :fill_color
attr_reader :children attr_reader :children, :gui_state
attr_reader :scroll_x, :scroll_y attr_reader :scroll_x, :scroll_y
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
@gui_state = options.delete(:gui_state)
super super
@scroll_x, @scroll_y = 0, 0 @scroll_x, @scroll_y = 0, 0

View File

@@ -119,17 +119,17 @@ module CyberarmEngine
def toggle def toggle
@visible = !@visible @visible = !@visible
root.recalculate root.gui_state.request_recalculate
end end
def show def show
@visible = true @visible = true
root.recalculate root.gui_state.request_recalculate
end end
def hide def hide
@visible = false @visible = false
root.recalculate root.gui_state.request_recalculate
end end
def draw def draw

View File

@@ -10,15 +10,18 @@ module CyberarmEngine
@down_keys = {} @down_keys = {}
@root_container = Stack.new @root_container = Stack.new(gui_state: self)
@game_objects << @root_container @game_objects << @root_container
@containers = [@root_container] @containers = [@root_container]
@active_width = window.width
@active_height = window.height
@focus = nil @focus = nil
@mouse_over = nil @mouse_over = nil
@mouse_down_on = {} @mouse_down_on = {}
@mouse_down_position = {} @mouse_down_position = {}
@pending_recalculate_request = false
setup setup
end end
@@ -35,6 +38,11 @@ module CyberarmEngine
end end
def update def update
if @pending_recalculate_request
@root_container.recalculate
@pending_recalculate_request = false
end
super super
new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y) 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(: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(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight) 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 end
def button_down(id) def button_down(id)
@@ -115,5 +128,10 @@ module CyberarmEngine
def redirect_mouse_wheel(button) def redirect_mouse_wheel(button)
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over @mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over
end end
# Schedule a full GUI recalculation on next update
def request_recalculate
@pending_recalculate_request = true
end
end end
end end

View File

@@ -37,7 +37,7 @@ module CyberarmEngine
old_width, old_height = width, height old_width, old_height = width, height
recalculate 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 end
end end