Added vertical scrolling support for containers (no scrollbar yet)

This commit is contained in:
2021-01-31 09:44:46 -06:00
parent f63b893c70
commit af24fc8690
2 changed files with 44 additions and 11 deletions

View File

@@ -226,6 +226,22 @@ module CyberarmEngine
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom) (@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
end 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) def dimensional_size(size, dimension)
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)

View File

@@ -4,14 +4,13 @@ module CyberarmEngine
include Common include Common
attr_accessor :stroke_color, :fill_color 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) def initialize(options = {}, block = nil)
@gui_state = options.delete(:gui_state) @gui_state = options.delete(:gui_state)
super super
@scroll_x = 0 @scroll_position = Vector.new(0, 0)
@scroll_y = 0
@scroll_speed = 10 @scroll_speed = 10
@text_color = options[:color] @text_color = options[:color]
@@ -98,6 +97,8 @@ module CyberarmEngine
def recalculate def recalculate
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top) @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
@current_position += @scroll_position
return unless visible? return unless visible?
Stats.increment(:gui_recalculations_last_frame, 1) Stats.increment(:gui_recalculations_last_frame, 1)
@@ -189,15 +190,31 @@ module CyberarmEngine
@current_position.y += element.outer_height @current_position.y += element.outer_height
end end
# def mouse_wheel_up(sender, x, y) def mouse_wheel_up(sender, x, y)
# @children.each {|c| c.y -= @scroll_speed} return unless @style.scroll
# @children.each {|c| c.recalculate} return if height < max_scroll_height
# end
# def mouse_wheel_down(sender, x, y) if @scroll_position.y < 0
# @children.each {|c| c.y += @scroll_speed} @scroll_position.y += @scroll_speed
# @children.each {|c| c.recalculate} @scroll_position.y = 0 if @scroll_position.y > 0
# end 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 def value
@children.map { |c| c.class }.join(", ") @children.map { |c| c.class }.join(", ")