lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.19.1 vs lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.20.0

- old
+ new

@@ -2,30 +2,31 @@ class Element include Theme include Event include Common - attr_accessor :x, :y, :z, :enabled, :tip + attr_accessor :x, :y, :z, :tip, :element_visible attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas def initialize(options = {}, block = nil) @parent = options.delete(:parent) # parent Container (i.e. flow/stack) options = theme_defaults(options) @options = options @block = block - @focus = @options[:focus].nil? ? false : @options[:focus] - @enabled = @options[:enabled].nil? ? true : @options[:enabled] - @visible = @options[:visible].nil? ? true : @options[:visible] + @focus = !@options.key?(:focus) ? false : @options[:focus] + @enabled = !@options.key?(:enabled) ? true : @options[:enabled] + @visible = !@options.key?(:visible) ? true : @options[:visible] @tip = @options[:tip] || "" @debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color] @style = Style.new(options) @root ||= nil @gui_state ||= nil + @element_visible = true @x = @style.x @y = @style.y @z = @style.z @@ -49,10 +50,13 @@ end def stylize set_static_position + set_color + set_font + set_padding set_margin set_background set_background_nine_slice @@ -68,10 +72,19 @@ def set_static_position @x = @style.x if @style.x != 0 @y = @style.y if @style.y != 0 end + def set_color + @style.color = safe_style_fetch(:color) + @text&.color = @style.color + end + + def set_font + @text&.swap_font(safe_style_fetch(:text_size), safe_style_fetch(:font)) + end + def set_background @style.background = safe_style_fetch(:background) @style.background_canvas.background = @style.background end @@ -136,18 +149,12 @@ 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 - return if self.is_a?(ToolTip) if old_width != width || old_height != height (root&.gui_state || @gui_state).request_recalculate else @@ -236,18 +243,30 @@ end :handled end + def enabled=(boolean) + @enabled = boolean + + recalculate + + @enabled + end + def enabled? @enabled end def visible? @visible end + def element_visible? + @element_visible + end + def toggle @visible = !@visible root.gui_state.request_recalculate end @@ -263,18 +282,17 @@ root.gui_state.request_recalculate if bool end def draw return unless visible? + return unless element_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 + render end def debug_draw return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT @@ -368,22 +386,45 @@ def inner_height (@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom) end def scroll_width - @children.sum(&:width) + noncontent_width + @children.sum(&:outer_width) end def scroll_height - @children.sum(&:height) + noncontent_height + if is_a?(CyberarmEngine::Element::Flow) + return 0 if @children.size.zero? + + pairs_ = [] + sorted_children_ = @children.sort_by(&:y) + a_ = [] + y_position_ = sorted_children_.first.y + + sorted_children_.each do |child| + unless child.y == y_position_ + y_position_ = child.y + pairs_ << a_ + a_ = [] + end + + a_ << child + end + + pairs_ << a_ unless pairs_.last == a_ + + pairs_.sum { |pair| pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom + else + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom + end end def max_scroll_width - scroll_width - width + scroll_width - outer_width end def max_scroll_height - scroll_height - height + scroll_height - outer_height end def dimensional_size(size, dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)