This commit is contained in:
2019-02-04 16:45:39 -06:00
parent 31687598e5
commit 998f27b742
3 changed files with 73 additions and 22 deletions

View File

@@ -29,7 +29,7 @@ module CyberarmEngine
raise "#{self.class} 'height' must be a number" unless height.is_a?(Numeric) raise "#{self.class} 'height' must be a number" unless height.is_a?(Numeric)
raise "#{self.class} 'options' must be a Hash" unless options.is_a?(Hash) raise "#{self.class} 'options' must be a Hash" unless options.is_a?(Hash)
@x, @y, @z, @width, @height, @internal_width, @internal_height = x, y, z, width-x, height-y, width-x, height-y @x, @y, @z, @width, @height, @internal_width, @internal_height = x, y, z, width, height, width, height
@origin_x, @origin_x = @x, @x @origin_x, @origin_x = @x, @x
@origin_width, @origin_height = @width, @height @origin_width, @origin_height = @width, @height
@scroll_x, @scroll_y = 0, 0 @scroll_x, @scroll_y = 0, 0
@@ -50,6 +50,7 @@ module CyberarmEngine
end end
def build def build
@theme.merge(@parent.theme) if @parent
@block.call(self) if @block @block.call(self) if @block
recalculate recalculate
@@ -83,24 +84,43 @@ module CyberarmEngine
end end
def button_up(id) def button_up(id)
if $window.mouse_x.between?(@x, @x + @width) unless active_container
if $window.mouse_y.between?(@y, @y + @height) @children.each {|child| child.button_up(id)}
case id return
when Gosu::MsWheelUp end
@scroll_y += @scroll_speed
@scroll_y = 0 if @scroll_y > 0 case id
when Gosu::MsWheelDown when Gosu::MsWheelUp
@scroll_y -= @scroll_speed scroll_down
if $window.height - @internal_height - @y > 0 when Gosu::MsWheelDown
@scroll_y = 0 scroll_up
else end
@scroll_y = @height - @internal_height if @scroll_y <= @height - @internal_height
end @elements.each do |e|
end if true#e.active_element
puts "REACHED"
e.button_up(id)
end end
end end
@elements.each {|e| if defined?(e.button_up); e.button_up(id); end} end
def scroll_up
@scroll_y += @scroll_speed
@scroll_y = 0 if @scroll_y > 0
end
def scroll_down
@scroll_y -= @scroll_speed
if $window.height - @internal_height > 0
@scroll_y = 0
else
@scroll_y = @height - @internal_height if @scroll_y <= @height - @internal_height
end
end
def mouse_over?
$window.mouse_x.between?(@x, @x + @width) && $window.mouse_y.between?(@y, @y + @height)
end end
def theme def theme
@@ -116,7 +136,27 @@ module CyberarmEngine
end end
def background def background
Gosu.draw_rect(@x, @y, @width, @height, @background_color, @z) Gosu.draw_rect(@x, @y, @width, @height, @background_color, @z) unless active_container
end
def active_container(children = @children)
active = false
children.each do |child|
if child.mouse_over?
if child.children.count == 0
active = true
else
child.active_container(child.children)
end
end
end
return active
end
def active_element
false
end end
def recalculate def recalculate
@@ -126,13 +166,16 @@ module CyberarmEngine
@packing_x = 0 @packing_x = 0
@packing_y = 0 @packing_y = 0
@width = @origin_width
@height= @origin_height
if @parent if @parent
neighbors = @parent.children.size > 0 ? @parent.children.size : 1 neighbors = @parent.children.size > 0 ? @parent.children.size : 1
@width = @parent.width / neighbors
@height = @parent.height / neighbors if @mode == :flow
@width = @parent.width / neighbors
@height = @parent.height
else # :stack
@width = @parent.width / neighbors
@height = @parent.height / neighbors
end
end end
widest_element = nil widest_element = nil
@@ -162,6 +205,9 @@ module CyberarmEngine
end end
end end
# @internal_width = @width if @width < @internal_width
# @internal_height = @height if @height < @internal_height
@internal_width += widest_element.margin if widest_element && !@origin_width.nonzero? @internal_width += widest_element.margin if widest_element && !@origin_width.nonzero?
@internal_height += last_element.margin if last_element && !@origin_height.nonzero? @internal_height += last_element.margin if last_element && !@origin_height.nonzero?
end end

View File

@@ -82,6 +82,10 @@ module CyberarmEngine
end end
end end
def active_element
mouse_over? || @focus
end
def width def width
@width + (@padding * 2) @width + (@padding * 2)
end end

View File

@@ -18,6 +18,7 @@ module CyberarmEngine
case id case id
when Gosu::MsLeft when Gosu::MsLeft
if mouse_over? if mouse_over?
puts "HI"
@block.call(self) if @block @block.call(self) if @block
end end
end end