This commit is contained in:
2019-02-18 17:41:14 -06:00
parent 998f27b742
commit 04ec6682ee
6 changed files with 99 additions and 64 deletions

View File

@@ -65,7 +65,11 @@ module CyberarmEngine
end end
def previous_state def previous_state
state = @states[@states.size-2] if @states.size > 1 && state = @states[@states.size-2]
return state
else
return nil
end
end end
def pop_state def pop_state

View File

@@ -3,7 +3,7 @@ module CyberarmEngine
include Common include Common
include DSL include DSL
attr_accessor :options, :global_pause, :active_container, :active_grid attr_accessor :options, :global_pause
attr_reader :game_objects, :containers attr_reader :game_objects, :containers
def initialize(options={}) def initialize(options={})

View File

@@ -3,7 +3,7 @@ module CyberarmEngine
include Common include Common
attr_accessor :stroke_color, :fill_color, :background_color, :x, :y, :z, :width, :height attr_accessor :stroke_color, :fill_color, :background_color, :x, :y, :z, :width, :height
attr_reader :elements, :children, :options attr_reader :elements, :children, :options, :parent
attr_reader :scroll_x, :scroll_y, :internal_width, :internal_height attr_reader :scroll_x, :scroll_y, :internal_width, :internal_height
attr_reader :origin_x, :origin_y, :origin_width, :origin_height attr_reader :origin_x, :origin_y, :origin_width, :origin_height
@@ -35,6 +35,8 @@ module CyberarmEngine
@scroll_x, @scroll_y = 0, 0 @scroll_x, @scroll_y = 0, 0
@scroll_speed = 10 @scroll_speed = 10
@layout = options[:layout] || :match_parent # or :wrap_content
@block = block @block = block
@options = options @options = options
@@ -61,6 +63,7 @@ module CyberarmEngine
@elements << container @elements << container
recalculate recalculate
@internal_height+=container.height
end end
def add(element) def add(element)
@@ -91,36 +94,54 @@ module CyberarmEngine
case id case id
when Gosu::MsWheelUp when Gosu::MsWheelUp
scroll_down scroll_down if mouse_over? && active_container
when Gosu::MsWheelDown when Gosu::MsWheelDown
scroll_up scroll_up if mouse_over? && active_container
end end
@elements.each do |e| @elements.each {|e| if e.active_element; e.button_up(id) end } if mouse_over? && active_container
if true#e.active_element
puts "REACHED"
e.button_up(id)
end
end
end
def scroll_up
@scroll_y += @scroll_speed
@scroll_y = 0 if @scroll_y > 0
end end
def scroll_down def scroll_down
@scroll_y -= @scroll_speed return if @height == @internal_height
if $window.height - @internal_height > 0
puts "ROOT down #{$window.current_state.containers.first.scroll_y}"
puts "#{@scroll_y} -> internal_height: #{@internal_height}, height: #{@height}, #{@y}"
@scroll_y += @scroll_speed
if @scroll_y > 0
@scroll_y = 0 @scroll_y = 0
else @parent.scroll_down if @parent
@scroll_y = @height - @internal_height if @scroll_y <= @height - @internal_height
end end
end end
def scroll_up
return if @height == @internal_height
puts "ROOT UP #{$window.current_state.containers.first.scroll_y}"
@scroll_y -= @scroll_speed
puts "#{@scroll_y} -> internal_height: #{@internal_height}, height: #{@height}, #{@y}"
if @scroll_y < @height - @internal_height
@scroll_y = @height - @internal_height
@parent.scroll_up if @parent
end
end
def deep_scroll_y
scroll = @scroll_y
arch = @parent if parent
while(arch)
scroll += arch.scroll_y
arch = arch.parent
end
return scroll
end
def mouse_over? def mouse_over?
$window.mouse_x.between?(@x, @x + @width) && $window.mouse_y.between?(@y, @y + @height) $window.mouse_x.between?(@x + @scroll_x, (@x + @scroll_x) + @width) && $window.mouse_y.between?(@y + @scroll_y, (@y + @scroll_y) + @height)
end end
def theme def theme
@@ -136,18 +157,17 @@ module CyberarmEngine
end end
def background def background
Gosu.draw_rect(@x, @y, @width, @height, @background_color, @z) unless active_container Gosu.draw_rect(@x, @y, @width, @height, @background_color, @z)
end end
def active_container(children = @children) def active_container
active = false
children.each do |child|
if child.mouse_over?
if child.children.count == 0
active = true active = true
else
child.active_container(child.children) if mouse_over?
@children.each do |child|
if child.mouse_over?
active = false
break
end end
end end
end end
@@ -161,22 +181,34 @@ module CyberarmEngine
def recalculate def recalculate
raise "mode was not defined!" unless @mode raise "mode was not defined!" unless @mode
# puts "<#{self.class}:#{self.object_id}> X: #{@x}, Y: #{@y}, width: #{@width}, height: #{@height} (children: #{@children.count}, parents: #{@parent&.children&.count})"
@packing_x = 0
@packing_y = 0
if @parent if @parent
neighbors = @parent.children.size > 0 ? @parent.children.size : 1 neighbors = @parent.children.size > 0 ? @parent.children.size : 1
if @layout == :match_parent
if @mode == :flow if @mode == :flow
@width = @parent.width / neighbors @width = @parent.width
@height = @parent.height @height = @parent.height / neighbors
else # :stack else # :stack
@width = @parent.width / neighbors @width = @parent.width / neighbors
@height = @parent.height / neighbors @height = @parent.height
end end
else # :wrap_content
raise "Not implemented"
end end
else
@width = $window.width
@height = $window.height
end
position_elements
puts "<#{self.class} X: #{@x}, Y: #{@y}, width: #{@width}, height: #{@height}, internal_width: #{@internal_width}, internal_height: #{@internal_height} (children: #{@children.count})"
end
def position_elements
@packing_x = 0
@packing_y = 0
widest_element = nil widest_element = nil
last_element = nil last_element = nil
@@ -208,8 +240,10 @@ module CyberarmEngine
# @internal_width = @width if @width < @internal_width # @internal_width = @width if @width < @internal_width
# @internal_height = @height if @height < @internal_height # @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?
@children.each(&:recalculate)
end end
def flow(element) def flow(element)

View File

@@ -75,11 +75,9 @@ module CyberarmEngine
end end
def mouse_over? def mouse_over?
if $window.mouse_x.between?(relative_x, relative_x + width) @parent.mouse_over? &&
if $window.mouse_y.between?(relative_y, relative_y + height) $window.mouse_x.between?(relative_x, relative_x + width) &&
true $window.mouse_y.between?(relative_y, relative_y + height)
end
end
end end
def active_element def active_element

View File

@@ -18,7 +18,6 @@ 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