lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.18.0 vs lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.19.0

- old
+ new

@@ -34,11 +34,12 @@ @style.width = default(:width) || nil @style.height = default(:height) || nil @style.background_canvas = Background.new - @style.border_canvas = BorderCanvas.new(element: self) + @style.background_nine_slice_canvas = BackgroundNineSlice.new + @style.border_canvas = BorderCanvas.new(element: self) @style_event = :default stylize @@ -52,10 +53,11 @@ set_padding set_margin set_background + set_background_nine_slice set_border_thickness set_border_color end @@ -72,10 +74,25 @@ @style.background = safe_style_fetch(:background) @style.background_canvas.background = @style.background end + def set_background_nine_slice + @style.background_nine_slice = safe_style_fetch(:background_nine_slice) + + @style.background_nine_slice_mode = safe_style_fetch(:background_nine_slice_mode) || :stretch + @style.background_nine_slice_color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE + @style.background_nine_slice_canvas.color = @style.background_nine_slice_color + + @style.background_nine_slice_from_edge = safe_style_fetch(:background_nine_slice_from_edge) + + @style.background_nine_slice_left = safe_style_fetch(:background_nine_slice_left) || @style.background_nine_slice_from_edge + @style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top) || @style.background_nine_slice_from_edge + @style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right) || @style.background_nine_slice_from_edge + @style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge + end + def set_border_thickness @style.border_thickness = safe_style_fetch(:border_thickness) @style.border_thickness_left = safe_style_fetch(:border_thickness_left) || @style.border_thickness @style.border_thickness_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness @@ -116,19 +133,28 @@ @style.margin_top = safe_style_fetch(:margin_top) || @style.margin @style.margin_bottom = safe_style_fetch(:margin_bottom) || @style.margin end def update_styles(event = :default) + old_width = width + old_height = height + _style = @style.send(event) @style_event = event if @text.is_a?(CyberarmEngine::Text) @text.color = _style&.dig(:color) || @style.default[:color] @text.swap_font(_style&.dig(:text_size) || @style.default[:text_size], _style&.dig(:font) || @style.default[:font]) end - (root&.gui_state || @gui_state).request_recalculate + return if self.is_a?(ToolTip) + + if old_width != width || old_height != height + (root&.gui_state || @gui_state).request_recalculate + else + stylize + end end def default_events %i[left middle right].each do |button| event(:"#{button}_mouse_button") @@ -239,10 +265,11 @@ def draw return unless visible? @style.background_canvas.draw + @style.background_nine_slice_canvas.draw @style.border_canvas.draw Gosu.clip_to(@x, @y, width, height) do render end @@ -341,15 +368,15 @@ def inner_height (@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom) end def scroll_width - @children.sum { |c| c.width } + noncontent_width + @children.sum(&:width) + noncontent_width end def scroll_height - @children.sum { |c| c.height } + noncontent_height + @children.sum(&:height) + noncontent_height end def max_scroll_width scroll_width - width end @@ -359,16 +386,14 @@ end def dimensional_size(size, dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) - if size && size.is_a?(Numeric) - if size.between?(0.0, 1.0) - ((@parent.send(:"content_#{dimension}") - send(:"noncontent_#{dimension}")) * size).round - else - size - end + if size.is_a?(Numeric) && size.between?(0.0, 1.0) + (@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round + else + size end end def background=(_background) @style.background_canvas.background = _background @@ -381,11 +406,35 @@ @style.background_canvas.z = @z @style.background_canvas.width = width @style.background_canvas.height = height @style.background_canvas.update - + update_background_nine_slice @style.border_canvas.update + end + + def background_nine_slice=(_image_path) + @style.background_nine_slice_canvas.image = _image_path + update_background_nine_slice + end + + def update_background_nine_slice + @style.background_nine_slice_canvas.x = @x + @style.background_nine_slice_canvas.y = @y + @style.background_nine_slice_canvas.z = @z + @style.background_nine_slice_canvas.width = width + @style.background_nine_slice_canvas.height = height + + @style.background_nine_slice_canvas.mode = @style.background_nine_slice_mode + + @style.background_nine_slice_canvas.color = @style.background_nine_slice_color + + @style.background_nine_slice_canvas.left = @style.background_nine_slice_left + @style.background_nine_slice_canvas.top = @style.background_nine_slice_top + @style.background_nine_slice_canvas.right = @style.background_nine_slice_right + @style.background_nine_slice_canvas.bottom = @style.background_nine_slice_bottom + + @style.background_nine_slice_canvas.image = @style.background_nine_slice end def root return self if is_root?