lib/cyberarm_engine/ui/elements/container.rb in cyberarm_engine-0.16.0 vs lib/cyberarm_engine/ui/elements/container.rb in cyberarm_engine-0.17.0
- old
+ new
@@ -2,23 +2,24 @@
class Element
class Container < Element
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_speed = 10
+ @scroll_position = Vector.new(0, 0)
+ @scroll_speed = 40
@text_color = options[:color]
@children = []
+
+ event(:window_size_changed)
end
def build
@block.call(self) if @block
@@ -42,10 +43,21 @@
$__current_container__ = old_container
root.gui_state.request_recalculate
end
+ def apend(&block)
+ old_container = $__current_container__
+
+ $__current_container__ = self
+ block.call(self) if block
+
+ $__current_container__ = old_container
+
+ root.gui_state.request_recalculate
+ end
+
def render
Gosu.clip_to(@x, @y, width, height) do
@children.each(&:draw)
end
@@ -96,10 +108,12 @@
self if hit?(x, y)
end
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)
stylize
@@ -187,18 +201,34 @@
element.y = element.style.margin_top + @current_position.y
@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(", ")
end