Skip to content
Browse files

Fixed alignment centering (at last!). :fill still a mess, but...

  • Loading branch information...
1 parent 9a5cdd9 commit 410e8be81afc3fb43392ecbd02078772f0f6e1cc @Spooner committed Jun 10, 2011
View
9 config/default_schema.yml
@@ -113,6 +113,15 @@
:List::Item:
:border_thickness: 0
+ :MainPacker: # < Packer
+ :border_thickness: 0
+ :padding_top: 0
+ :padding_right: 0
+ :padding_bottom: 0
+ :padding_left: 0
+ :spacing_h: 0
+ :spacing_v: 0
+
:MenuPane:
:background_color: ?very_dark_gray
View
9 examples/align_example.rb
@@ -11,6 +11,13 @@ class ExampleState < Fidgit::GuiState
def initialize
super
+#=begin
+ vertical align: :center, width: 10, height: 10, background_color: Gosu::Color::RED do
+ label "x"
+ label "y"
+ end
+#=end
+=begin
vertical align: :center, background_color: OUTER_BACKGROUND do
label "h => align_h, v => align_v", align_h: :center
@@ -50,6 +57,8 @@ def initialize
label "Bazingo by jingo!"
end
end
+
+=end
end
end
View
36 lib/fidgit/elements/grid.rb
@@ -72,8 +72,8 @@ def rearrange
protected
# Repack all the elements into their positions.
def repack
- @widths = Array.new(@num_columns, 0)
- @heights = Array.new(@num_rows, 0)
+ @widths = Array.new(@rows.empty? ? 0 : @rows[0].size, 0)
+ @heights = Array.new(@rows.size, 0)
filled_columns = []
filled_rows = []
@@ -82,35 +82,39 @@ def repack
@rows.each_with_index do |row, row_num|
row.each_with_index do |element, column_num|
fills = (element.align_h == :fill)
- @widths[column_num] = [fills ? 0 : element.outer_width, @widths[column_num] || 0].max
+ @widths[column_num] = [fills ? 0 : element.outer_width, @widths[column_num]].max
filled_columns.push fills
fills = (element.align_v == :fill)
- @heights[row_num] = [fills ? 0 : element.outer_height, @heights[row_num] || 0].max
+ @heights[row_num] = [fills ? 0 : element.outer_height, @heights[row_num]].max
filled_rows.push fills
end
end
# Expand the size of each filled column to make the minimum size required.
- num_filled_columns = filled_columns.select {|value| value }.count
- if num_filled_columns > 0
+ unless @widths.empty?
+ num_filled_columns = filled_columns.select {|value| value }.count
total_width = @widths.inject(0, :+) + (padding_left + padding_right) + ((@num_columns - 1) * spacing_h)
- if total_width < min_width
- extra_width = total_width / num_filled_columns
- filled_columns.each_with_index do |filled, i|
- @widths[i] += extra_width if filled
+ extra_width = min_width - total_width
+ if extra_width > 0
+ if num_filled_columns > 0
+ @widths[filled_columns.index true] += extra_width
+ else
+ @widths[-1] += extra_width
end
end
end
# Expand the size of each filled row to make the minimum size required.
- num_filled_rows = filled_rows.select {|value| value }.count
- if num_filled_rows > 0
+ unless @heights.empty?
+ num_filled_rows = filled_rows.select {|value| value }.count
total_height = @heights.inject(0, :+) + (padding_left + padding_right) + ((@num_rows - 1) * spacing_v)
- if total_height < min_height
- extra_height = total_height / num_filled_rows
- filled_rows.each_with_index do |filled, i|
- @heights[i] += extra_height if filled
+ extra_height = min_height - total_height
+ if extra_height > 0
+ if num_filled_rows > 0
+ @heights[filled_rows.index true] += extra_height
+ else
+ @heights[-1] += extra_height
end
end
end
View
26 lib/fidgit/elements/main_packer.rb
@@ -0,0 +1,26 @@
+require_relative "vertical"
+
+module Fidgit
+ # Main container that can contains a single "proper" packing element.
+ class MainPacker < Vertical
+ def initialize(options = {})
+ options = {
+ width: $window.width,
+ height: $window.height,
+ }.merge! options
+
+ super options
+ end
+
+ def width; $window.width; end
+ def height; $window.height; end
+ def width=(value); ; end
+ def height=(value); ; end
+
+ def add(element)
+ raise "MainPacker can only contain one packing element" unless empty?
+ raise "MainPacker can only contain packing elements" unless element.is_a? Packer
+ super(element)
+ end
+ end
+end
View
2 lib/fidgit/gosu_ext/gosu_module.rb
@@ -9,8 +9,6 @@ def init_entities
public
def register_entity(name, image)
- raise ArgumentError.new("Entity already registered") if @entities.has_key? name
-
name = name.to_sym
register_entity_fidgit(name, image)
@entities[name] = image
View
2 lib/fidgit/states/gui_state.rb
@@ -49,7 +49,7 @@ def clear(*args, &block); @container.clear *args, &block; end
def initialize
# The container is where the user puts their content.
- @container = Vertical.new(padding: 0, width: $window.width, height: $window.height)
+ @container = MainPacker.new
unless defined? @@draw_pixel
media_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'media'))

0 comments on commit 410e8be

Please sign in to comment.
Something went wrong with that request. Please try again.