Implemented support for dynamic repainting of gui states with GuiState#needs_repaint?

This commit is contained in:
2023-01-03 22:16:07 -06:00
parent a2d44ea2dc
commit f82c0953b2
4 changed files with 48 additions and 4 deletions

View File

@@ -65,6 +65,8 @@ module CyberarmEngine
set_border_thickness set_border_thickness
set_border_color set_border_color
root.gui_state.request_repaint
end end
def safe_style_fetch(*args) def safe_style_fetch(*args)
@@ -166,10 +168,10 @@ module CyberarmEngine
return if self.is_a?(ToolTip) return if self.is_a?(ToolTip)
if old_width != width || old_height != height if old_width != width || old_height != height
(root&.gui_state || @gui_state).request_recalculate root.gui_state.request_recalculate
else
stylize
end end
stylize
end end
def default_events def default_events
@@ -267,6 +269,10 @@ module CyberarmEngine
@enabled @enabled
end end
def focused?
@focus
end
def visible? def visible?
@visible @visible
end end
@@ -278,18 +284,21 @@ module CyberarmEngine
def toggle def toggle
@visible = !@visible @visible = !@visible
root.gui_state.request_recalculate root.gui_state.request_recalculate
root.gui_state.request_repaint
end end
def show def show
bool = visible? bool = visible?
@visible = true @visible = true
root.gui_state.request_recalculate unless bool root.gui_state.request_recalculate unless bool
root.gui_state.request_repaint unless bool
end end
def hide def hide
bool = visible? bool = visible?
@visible = false @visible = false
root.gui_state.request_recalculate if bool root.gui_state.request_recalculate if bool
root.gui_state.request_repaint if bool
end end
def draw def draw
@@ -479,6 +488,8 @@ module CyberarmEngine
end end
def background=(_background) def background=(_background)
root.gui_state.request_repaint
@style.background_canvas.background = _background @style.background_canvas.background = _background
update_background update_background
end end
@@ -497,6 +508,8 @@ module CyberarmEngine
end end
def background_nine_slice=(_image_path) def background_nine_slice=(_image_path)
root.gui_state.request_repaint
@style.background_nine_slice_canvas.image = _image_path @style.background_nine_slice_canvas.image = _image_path
update_background_nine_slice update_background_nine_slice
end end
@@ -521,6 +534,8 @@ module CyberarmEngine
end end
def background_image=(image_path) def background_image=(image_path)
root.gui_state.request_repaint
@style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path) @style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path)
update_background_image update_background_image
end end

View File

@@ -122,6 +122,9 @@ module CyberarmEngine
layout layout
old_width = @width
old_height = @height
if is_root? if is_root?
@width = @style.width = window.width @width = @style.width = window.width
@height = @style.height = window.height @height = @style.height = window.height
@@ -178,6 +181,8 @@ module CyberarmEngine
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}" # puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
update_background update_background
root.gui_state.request_repaint if @width != old_width || @height != old_height
end end
def layout def layout
@@ -242,6 +247,7 @@ module CyberarmEngine
@scroll_position.y = 0 if @scroll_position.y > 0 @scroll_position.y = 0 if @scroll_position.y > 0
root.gui_state.request_recalculate_for(self) root.gui_state.request_recalculate_for(self)
root.gui_state.request_repaint
return :handled return :handled
end end
@@ -257,6 +263,7 @@ module CyberarmEngine
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height @scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
root.gui_state.request_recalculate_for(self) root.gui_state.request_recalculate_for(self)
root.gui_state.request_repaint
return :handled return :handled
end end
@@ -278,7 +285,7 @@ module CyberarmEngine
end end
def value def value
@children.map { |c| c.class }.join(", ") @children.map(&:class).join(", ")
end end
def to_s def to_s

View File

@@ -36,6 +36,9 @@ module CyberarmEngine
@text.color = @style.color @text.color = @style.color
end end
old_width = @width
old_height = @height
@width = 0 @width = 0
@height = 0 @height = 0
@@ -79,6 +82,8 @@ module CyberarmEngine
end end
update_background update_background
root.gui_state.request_repaint if @width != old_width || @height != old_height
end end
def handle_text_wrapping(max_width) def handle_text_wrapping(max_width)
@@ -159,6 +164,7 @@ module CyberarmEngine
end end
def value=(value) def value=(value)
old_value = @raw_text
@raw_text = value.to_s.chomp @raw_text = value.to_s.chomp
old_width = width old_width = width
@@ -170,6 +176,8 @@ module CyberarmEngine
recalculate recalculate
end end
root.gui_state.request_repaint if old_value != @raw_text
publish(:changed, self.value) publish(:changed, self.value)
end end
end end

View File

@@ -27,6 +27,8 @@ module CyberarmEngine
@pending_recalculate_request = false @pending_recalculate_request = false
@pending_element_recalculate_requests = [] @pending_element_recalculate_requests = []
@needs_repaint = false
@menu = nil @menu = nil
@min_drag_distance = 0 @min_drag_distance = 0
@mouse_pos = Vector.new @mouse_pos = Vector.new
@@ -66,6 +68,12 @@ module CyberarmEngine
@root_container.debug_draw @root_container.debug_draw
end end
@needs_repaint = false
end
def needs_repaint?
@needs_repaint
end end
def update def update
@@ -253,6 +261,12 @@ module CyberarmEngine
@pending_focus_element = element @pending_focus_element = element
end end
def request_repaint
# puts caller[0..4]
# puts
@needs_repaint = true
end
def show_menu(list_box) def show_menu(list_box)
@menu = list_box @menu = list_box
end end