diff --git a/lib/cyberarm_engine/common.rb b/lib/cyberarm_engine/common.rb index f11e997..133b82c 100644 --- a/lib/cyberarm_engine/common.rb +++ b/lib/cyberarm_engine/common.rb @@ -8,8 +8,11 @@ module CyberarmEngine window.current_state end - def previous_state - window.previous_state + def previous_state(state = nil) + raise "Only available for CyberarmEngine::GameState and subclasses" unless is_a?(CyberarmEngine::GameState) || state.is_a?(CyberarmEngine::GameState) + + i = window.states.index(state || self) + window.states[i - 1] unless (i - 1).negative? end def pop_state diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index cddba7f..5fbf7ab 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -447,18 +447,10 @@ module CyberarmEngine end if @parent && @style.fill # Handle fill behavior - fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing - if dimension == :width && @parent.is_a?(Flow) - space_available_width = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings) - space_available_width = space_available_width.nan? ? 0 : space_available_width.floor # The parent element might not have its dimensions, yet. - return space_available_width - noncontent_width elsif dimension == :height && @parent.is_a?(Stack) - space_available_height = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings) - space_available_height = space_available_height.nan? ? 0 : space_available_height.floor # The parent element might not have its dimensions, yet. - return space_available_height - noncontent_height end @@ -470,6 +462,22 @@ module CyberarmEngine new_size end + def space_available_width + # TODO: This may get expensive if there are a lot of children, probably should cache it somehow + fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing + + available_space = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings) + (available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet. + end + + def space_available_height + # TODO: This may get expensive if there are a lot of children, probably should cache it somehow + fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing + + available_space = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings) + (available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet. + end + def background=(_background) @style.background_canvas.background = _background update_background diff --git a/lib/cyberarm_engine/ui/elements/container.rb b/lib/cyberarm_engine/ui/elements/container.rb index 4f75e3d..6e4fd20 100644 --- a/lib/cyberarm_engine/ui/elements/container.rb +++ b/lib/cyberarm_engine/ui/elements/container.rb @@ -136,7 +136,31 @@ module CyberarmEngine @height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor end - # Move child to parent after positioning + # FIXME: Correctly handle alignment when element has siblings + # FIXME: Enable alignment for any element, not just containers + if @style.v_align + space = space_available_height + + case @style.v_align + when :center + @y = parent.height / 2 - height / 2 + when :bottom + @y = parent.height - height + end + end + + if @style.h_align + space = space_available_width + + case @style.h_align + when :center + @x = parent.width / 2 - width / 2 + when :right + @x = parent.width - width + end + end + + # Move children to parent after positioning @children.each do |child| child.x += (@x + @style.border_thickness_left) - style.margin_left child.y += (@y + @style.border_thickness_top) - style.margin_top diff --git a/lib/cyberarm_engine/window.rb b/lib/cyberarm_engine/window.rb index 763589a..c5c7de8 100644 --- a/lib/cyberarm_engine/window.rb +++ b/lib/cyberarm_engine/window.rb @@ -139,12 +139,6 @@ module CyberarmEngine @states.last end - def previous_state - if @states.size > 1 && (state = @states[@states.size - 2]) - state - end - end - def pop_state @states.pop end