From af24fc86908838278c74d959f95ba89e9e942f51 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sun, 31 Jan 2021 09:44:46 -0600 Subject: [PATCH] Added vertical scrolling support for containers (no scrollbar yet) --- lib/cyberarm_engine/ui/element.rb | 16 ++++++++ lib/cyberarm_engine/ui/elements/container.rb | 39 ++++++++++++++------ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index 6395048..4ad7421 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -226,6 +226,22 @@ module CyberarmEngine (@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 + end + + def scroll_height + @children.sum { |c| c.height } + noncontent_height + end + + def max_scroll_width + scroll_width - width + end + + def max_scroll_height + scroll_height - height + end + def dimensional_size(size, dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) diff --git a/lib/cyberarm_engine/ui/elements/container.rb b/lib/cyberarm_engine/ui/elements/container.rb index 4c29d1f..67903af 100644 --- a/lib/cyberarm_engine/ui/elements/container.rb +++ b/lib/cyberarm_engine/ui/elements/container.rb @@ -4,14 +4,13 @@ module CyberarmEngine include Common attr_accessor :stroke_color, :fill_color - attr_reader :children, :gui_state, :scroll_x, :scroll_y + attr_reader :children, :gui_state, :scroll_position def initialize(options = {}, block = nil) @gui_state = options.delete(:gui_state) super - @scroll_x = 0 - @scroll_y = 0 + @scroll_position = Vector.new(0, 0) @scroll_speed = 10 @text_color = options[:color] @@ -98,6 +97,8 @@ module CyberarmEngine def recalculate @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top) + @current_position += @scroll_position + return unless visible? Stats.increment(:gui_recalculations_last_frame, 1) @@ -189,15 +190,31 @@ module CyberarmEngine @current_position.y += element.outer_height end - # def mouse_wheel_up(sender, x, y) - # @children.each {|c| c.y -= @scroll_speed} - # @children.each {|c| c.recalculate} - # end + def mouse_wheel_up(sender, x, y) + return unless @style.scroll + return if height < max_scroll_height - # def mouse_wheel_down(sender, x, y) - # @children.each {|c| c.y += @scroll_speed} - # @children.each {|c| c.recalculate} - # end + if @scroll_position.y < 0 + @scroll_position.y += @scroll_speed + @scroll_position.y = 0 if @scroll_position.y > 0 + recalculate + + return :handled + end + end + + def mouse_wheel_down(sender, x, y) + return unless @style.scroll + return if height < max_scroll_height + + if @scroll_position.y.abs < max_scroll_height + @scroll_position.y -= @scroll_speed + @scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height + recalculate + + return :handled + end + end def value @children.map { |c| c.class }.join(", ")