Module: GlimR::Layoutable
- Included in:
- Widget
- Defined in:
- lib/glimr/layoutable.rb
Overview
Layoutable is a mixin for creating GlimR nodes that are suspectible to layout rules.
Layoutable objects have dimensions, margins, and padding.
The margins define how much space should be left around the Layoutable. Padding defines how much distance should be left between the borders of the Layoutable and its child nodes.
They also have the following attributes that control their dynamic resizing behaviour: expand_width, expand_height, fit_children, min_width, min_height, max_width and max_height.
Dynamic layout rules:
An object expands in a dimension if either its corresponding expand attribute is set to true, or if its fit_children is set to true and one of its children expands in the dimension.
If an object neither fits its children nor expands, it is a constant size object.
When a layoutable object is attached to the scene graph it expands to its maximum dimensions if it is an expanding object.
When a layoutable object changes size, it informs its parent if the parent is a layoutable. If the object was a constant size object, or the size change resulted from changing the object’s dimension constraints (min_width, max_width, etc.), the expanding children of the object are also informed of the size change.
When computing the dimensions of a Layoutable, dimension constraints take predecence over expanding, which takes predecence over fit_children.
E.g. the final width of a layoutable node is computed as follows:
1. set width to max child width if fit_children
2. set width to the inner width of the parent node if
3. clamp the width between min_width and max_width if they're set
Constant Summary collapse
- DIMENSIONS =
, :front, :back]
[:left, :right, :top, :bottom]
Instance Attribute Summary collapse
-
#align ⇒ Object
Returns the value of attribute align.
-
#valign ⇒ Object
Returns the value of attribute valign.
Class Method Summary collapse
Instance Method Summary collapse
- #attach(*a, &b) ⇒ Object
-
#children_change ⇒ Object
Children changed, let’s accommodate.
-
#constant_size? ⇒ Boolean
Returns true if the object’s size is constant, i.e it does not expand and does not fit its children.
- #default_config ⇒ Object
- #detach(*a, &b) ⇒ Object
-
#expand!(given_width = nil, given_height = nil) ⇒ Object
Expands self to maximum available dimensions.
-
#expand? ⇒ Boolean
Whether the Layoutable object expands to fill its parent’s width or height.
-
#expand_height ⇒ Object
Whether the object expands to fill its parent’s height or not.
-
#expand_to_max_height!(given_height = nil) ⇒ Object
Expands self, parent and children to maximum available height if expand_height is true.
-
#expand_to_max_width!(given_width = nil) ⇒ Object
Expands self, parent and children to maximum available width if expand_width is true.
-
#expand_width ⇒ Object
Whether the object expands to fill its parent’s width or not.
-
#fit_height! ⇒ Object
Sets height equal to the largest child + padding.
-
#fit_to_children! ⇒ Object
Sets dimensions equal to the largest child + padding.
-
#fit_width! ⇒ Object
Sets width equal to the largest child + padding.
-
#free_height ⇒ Object
Returns the maximum height an expanding child can expand to.
-
#free_width ⇒ Object
Returns the maximum width an expanding child can expand to.
- #full_depth ⇒ Object
-
#full_height ⇒ Object
Height, scaled, plus margins.
-
#full_width ⇒ Object
Width, scaled, plus margins.
- #initialize(*a, &b) ⇒ Object
-
#inner_depth ⇒ Object
Depth sans padding.
-
#inner_height ⇒ Object
Height sans padding.
-
#inner_width ⇒ Object
Width sans padding.
- #inspect ⇒ Object
- #layout ⇒ Object
-
#layoutable_children ⇒ Object
Returns an Array of all children that respond true to #is_a?(Layoutable).
-
#margin ⇒ Object
Returns current margins as an OpenStruct with #left, #right, #top, and #bottom.
-
#margin=(new_margin) ⇒ Object
Sets margins to new_margin.
-
#max_height=(mh) ⇒ Object
Sets max_height to mw and sets height to max_height if it’s larger than the new max_height.
-
#max_width=(mw) ⇒ Object
Sets max_width to mw and sets width to max_width if it’s larger than the new max_width.
-
#min_height=(mh) ⇒ Object
Sets min_height to mw and sets height to min_height if it’s smaller than the new min_height.
-
#min_width=(mw) ⇒ Object
Sets min_width to mw and sets width to min_width if it’s smaller than the new min_width.
-
#padding ⇒ Object
Returns current padding as an OpenStruct with #left, #right, #top, and #bottom.
-
#padding=(new_padding) ⇒ Object
Sets padding to new_padding.
- #parent=(p) ⇒ Object
-
#size_changing(&block) ⇒ Object
Yields to block and informs relevant parties if size changed during it.
-
#tell_children_of_size_change ⇒ Object
Expands all expanding children.
-
#x=(nx) ⇒ Object
Sets x to the given integer value.
-
#y=(ny) ⇒ Object
Sets y to the given integer value.
Instance Attribute Details
#align ⇒ Object
Returns the value of attribute align.
72 73 74 |
# File 'lib/glimr/layoutable.rb', line 72 def align @align end |
#valign ⇒ Object
Returns the value of attribute valign.
72 73 74 |
# File 'lib/glimr/layoutable.rb', line 72 def valign @valign end |
Class Method Details
.size_changing_accessor(*mnames) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/glimr/layoutable.rb', line 51 def self.size_changing_accessor(*mnames) attr_reader *mnames mnames.each{|mn| ivar_name = :"@#{mn}" define_method("#{mn}=") do |value| size_changing{ instance_variable_set(ivar_name, value) } end } end |
Instance Method Details
#attach(*a, &b) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/glimr/layoutable.rb', line 272 def attach(*a, &b) size_changing{ super children_change } end |
#children_change ⇒ Object
Children changed, let’s accommodate.
351 352 353 354 |
# File 'lib/glimr/layoutable.rb', line 351 def children_change fit_to_children! if fit_children? layout end |
#constant_size? ⇒ Boolean
Returns true if the object’s size is constant, i.e it does not expand and does not fit its children.
268 269 270 |
# File 'lib/glimr/layoutable.rb', line 268 def constant_size? not ( or fit_children?) end |
#default_config ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/glimr/layoutable.rb', line 85 def default_config super.merge( :expand_width => false, :expand_height => false, :fit_children => true, :align => :left, :valign => :top, :min_width => 0, :min_height => 0, :max_width => 1.0/0.0, :max_height => 1.0/0.0, :width => 0, :height => 0, :depth => 0, :margin_left => 0, :margin_right => 0, :margin_top => 0, :margin_bottom => 0, :margin_front => 0, :margin_back => 0, :padding_left => 0, :padding_right => 0, :padding_top => 0, :padding_bottom => 0, :padding_front => 0, :padding_back => 0 ) end |
#detach(*a, &b) ⇒ Object
279 280 281 282 283 284 |
# File 'lib/glimr/layoutable.rb', line 279 def detach(*a, &b) size_changing{ super children_change } end |
#expand!(given_width = nil, given_height = nil) ⇒ Object
Expands self to maximum available dimensions. Brought on by parent size change.
364 365 366 367 368 369 |
# File 'lib/glimr/layoutable.rb', line 364 def (given_width=nil,given_height=nil) size_changing{ (given_width) if (given_width) if } end |
#expand? ⇒ Boolean
Whether the Layoutable object expands to fill its parent’s width or height.
262 263 264 |
# File 'lib/glimr/layoutable.rb', line 262 def or end |
#expand_height ⇒ Object
Whether the object expands to fill its parent’s height or not.
Returns true if either @expand_height is true or fit_children is true and any of the layoutable children returns true to #expand_height.
255 256 257 258 |
# File 'lib/glimr/layoutable.rb', line 255 def or (fit_children and layoutable_children.any?{|c| c. }) end |
#expand_to_max_height!(given_height = nil) ⇒ Object
Expands self, parent and children to maximum available height if expand_height is true.
If expand_height is true, expands to either the given_height, the result of calling @parent.expand_to_max_height! or If height changes as a result of this call, calls each Layoutable child’s expand_to_max_height! with the new height.
Returns free_height.
414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/glimr/layoutable.rb', line 414 def (given_height=nil) if remove_event_listener(:resize, @__resize_handler) if given_height @height = given_height elsif @parent.is_a? Layoutable @height = @parent. elsif ||= (@parent && @parent.) on_resize(@__resize_handler) @height = .height end end free_height end |
#expand_to_max_width!(given_width = nil) ⇒ Object
Expands self, parent and children to maximum available width if expand_width is true.
If expand_width is true, expands to either the given_width, the result of calling @parent.expand_to_max_width! or If width changes as a result of this call, calls each Layoutable child’s expand_to_max_width! with the new width.
Returns free_width.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/glimr/layoutable.rb', line 385 def (given_width=nil) if remove_event_listener(:resize, @__resize_handler) if given_width @width = given_width elsif @parent.is_a? Layoutable @width = @parent. elsif ||= (@parent && @parent.) on_resize(@__resize_handler) @width = .width end end free_width end |
#expand_width ⇒ Object
Whether the object expands to fill its parent’s width or not.
Returns true if either @expand_width is true or fit_children is true and any of the layoutable children returns true to #expand_width.
237 238 239 |
# File 'lib/glimr/layoutable.rb', line 237 def or (fit_children and layoutable_children.any?{|c| c. }) end |
#fit_height! ⇒ Object
Sets height equal to the largest child + padding.
302 303 304 305 306 |
# File 'lib/glimr/layoutable.rb', line 302 def fit_height! self.height = layoutable_children.map{|c| c.full_height + padding_top + padding_bottom }.max unless layoutable_children.empty? end |
#fit_to_children! ⇒ Object
Sets dimensions equal to the largest child + padding.
287 288 289 290 291 292 |
# File 'lib/glimr/layoutable.rb', line 287 def fit_to_children! size_changing do fit_width! unless parent and fit_height! unless parent and end end |
#fit_width! ⇒ Object
Sets width equal to the largest child + padding.
295 296 297 298 299 |
# File 'lib/glimr/layoutable.rb', line 295 def fit_width! self.width = layoutable_children.map{|c| c.full_width + padding_left + padding_right }.max unless layoutable_children.empty? end |
#free_height ⇒ Object
Returns the maximum height an expanding child can expand to.
458 459 460 |
# File 'lib/glimr/layoutable.rb', line 458 def free_height inner_height end |
#free_width ⇒ Object
Returns the maximum width an expanding child can expand to.
453 454 455 |
# File 'lib/glimr/layoutable.rb', line 453 def free_width inner_width end |
#full_depth ⇒ Object
501 502 503 504 505 506 507 508 509 510 |
# File 'lib/glimr/layoutable.rb', line 501 def full_depth case scale when Numeric depth * scale when Array depth * scale[2] else depth end + @margin_front + @margin_back end |
#full_height ⇒ Object
Height, scaled, plus margins.
490 491 492 493 494 495 496 497 498 499 |
# File 'lib/glimr/layoutable.rb', line 490 def full_height case scale when Numeric height * scale when Array height * scale[1] else height end + @margin_top + @margin_bottom end |
#full_width ⇒ Object
Width, scaled, plus margins.
478 479 480 481 482 483 484 485 486 487 |
# File 'lib/glimr/layoutable.rb', line 478 def full_width case scale when Numeric width * scale when Array width * scale[0] else width end + @margin_left + @margin_right end |
#initialize(*a, &b) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/glimr/layoutable.rb', line 101 def initialize(*a, &b) super @__resize_handler = lambda{|o,e| o. } self.margin = @margin if @margin self.padding = @padding if @padding end |
#inner_depth ⇒ Object
Depth sans padding.
473 474 475 |
# File 'lib/glimr/layoutable.rb', line 473 def inner_depth @depth - @padding_front - @padding_back end |
#inner_height ⇒ Object
Height sans padding.
468 469 470 |
# File 'lib/glimr/layoutable.rb', line 468 def inner_height @height - @padding_bottom - @padding_top end |
#inner_width ⇒ Object
Width sans padding.
463 464 465 |
# File 'lib/glimr/layoutable.rb', line 463 def inner_width @width - @padding_left - @padding_right end |
#inspect ⇒ Object
512 513 514 |
# File 'lib/glimr/layoutable.rb', line 512 def inspect super[0..-2] + "::w#@width:h#@height>" end |
#layout ⇒ Object
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/glimr/layoutable.rb', line 429 def layout @layouted_width = @width @layouted_height = @height layoutable_children.each{|c| case align when :left c.x = padding_left + c.margin_left when :right c.x = width - padding_right - c.full_width when :center c.x = padding_left + c.margin_left + ((inner_width - c.margin_left - c.margin_right) - c.width) / 2.0 end case valign when :top c.y = padding_top + c.margin_top when :bottom c.y = height - padding_bottom - c.full_height when :middle c.y = padding_top + c.margin_top + ((inner_height - c.margin_top - c.margin_bottom) - c.height) / 2.0 end } end |
#layoutable_children ⇒ Object
Returns an Array of all children that respond true to #is_a?(Layoutable).
229 230 231 |
# File 'lib/glimr/layoutable.rb', line 229 def layoutable_children children.find_all{|c| c.is_a? Layoutable } end |
#margin ⇒ Object
Returns current margins as an OpenStruct with #left, #right, #top, and #bottom.
165 166 167 168 169 170 171 |
# File 'lib/glimr/layoutable.rb', line 165 def margin OpenStruct.new( :left => margin_left, :right => margin_right, :top => margin_top, :bottom => margin_bottom, :front => margin_front, :back => margin_back ) end |
#margin=(new_margin) ⇒ Object
Sets margins to new_margin. If given a Numeric, sets all margins to the given value. If given a hash, sets the margins to the corresponding values in the hash. If given any other object, acts as if it is an OpenStruct returned by #margin.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/glimr/layoutable.rb', line 178 def margin= new_margin size_changing do case new_margin when Numeric @margin_left = @margin_right = @margin_top = @margin_bottom = new_margin touch! when Hash new_margin.each{|k,v| __send__("margin_#{k}=", v) } else DIMENSIONS.each{|d| __send__("margin_#{d}=", new_margin.__send__(d)) } end end end |
#max_height=(mh) ⇒ Object
Sets max_height to mw and sets height to max_height if it’s larger than the new max_height.
159 160 161 162 |
# File 'lib/glimr/layoutable.rb', line 159 def max_height= mh @max_height = mh self.height = mh if self.height > mh end |
#max_width=(mw) ⇒ Object
Sets max_width to mw and sets width to max_width if it’s larger than the new max_width.
152 153 154 155 |
# File 'lib/glimr/layoutable.rb', line 152 def max_width= mw @max_width = mw self.width = mw if self.width > mw end |
#min_height=(mh) ⇒ Object
Sets min_height to mw and sets height to min_height if it’s smaller than the new min_height.
144 145 146 147 148 |
# File 'lib/glimr/layoutable.rb', line 144 def min_height= mh @min_height = mh self.height = mh if self.height < mh fit_to_children! if fit_children? end |
#min_width=(mw) ⇒ Object
Sets min_width to mw and sets width to min_width if it’s smaller than the new min_width.
136 137 138 139 140 |
# File 'lib/glimr/layoutable.rb', line 136 def min_width= mw @min_width = mw self.width = mw if self.width < mw fit_to_children! if fit_children? end |
#padding ⇒ Object
Returns current padding as an OpenStruct with #left, #right, #top, and #bottom.
197 198 199 200 201 202 203 |
# File 'lib/glimr/layoutable.rb', line 197 def padding OpenStruct.new( :left => padding_left, :right => padding_right, :top => padding_top, :bottom => padding_bottom, :front => padding_front, :back => padding_back ) end |
#padding=(new_padding) ⇒ Object
Sets padding to new_padding. If given a Numeric, sets all paddings to the given value. If given a hash, sets the paddings to the corresponding values in the hash. If given any other object, acts as if it is an OpenStruct returned by #padding.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/glimr/layoutable.rb', line 210 def padding= new_padding size_changing do case new_padding when Numeric @padding_left = @padding_right = @padding_top = @padding_bottom = new_padding touch! when Hash new_padding.each{|k,v| __send__("padding_#{k}=", v) } else DIMENSIONS.each{|d| __send__("padding_#{d}=", new_padding.__send__(d)) } end end end |
#parent=(p) ⇒ Object
241 242 243 244 245 246 247 248 249 |
# File 'lib/glimr/layoutable.rb', line 241 def parent= p super ew = eh = = p.is_a?(Viewport) and (ew or eh) = (p.is_a?(Layoutable) and !p.fit_children?) and ((ew and !parent.) or (eh and !parent.)) if ( or ) end |
#size_changing(&block) ⇒ Object
Yields to block and informs relevant parties if size changed during it.
In detail:
If the node turns into an expanding one, it is expanded. This may cause a size change.
If the inner dimensions (caused by padding changes) are altered, children are told and relayouted.
If the outer dimensions (caused by dimension changes and margin changes) are altered, children are told and relayouted and possible Layoutable parent is told (unless the node is an expanding one, in which case the parent already knows.)
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/glimr/layoutable.rb', line 321 def size_changing(&block) return yield if @__doing_size_changing_operation @__doing_size_changing_operation = true ow, oh, iw, ih = full_width, full_height, inner_width, inner_height cs = constant_size? fc = fit_children? ew = eh = rv = yield fit_to_children! if fit_children? and ((!fc and fit_children?) or (ew and !) or (eh and !)) if (!ew and ) or (!eh and ) @width = [[min_width, width].max, max_width].min @height = [[min_height, height].max, max_height].min dw = (ow != full_width) dh = (oh != full_height) diw = (iw != inner_width) dih = (ih != inner_height) if dw or dh or diw or dih tell_children_of_size_change if diw or dih parent.children_change if parent.is_a?(Layoutable) and (dw or dh) layout dispatch_event(Event.new(:layout)) if respond_to? :dispatch_event @width = [[min_width, width].max, max_width].min @height = [[min_height, height].max, max_height].min end @__doing_size_changing_operation = false rv end |
#tell_children_of_size_change ⇒ Object
Expands all expanding children.
357 358 359 |
# File 'lib/glimr/layoutable.rb', line 357 def tell_children_of_size_change layoutable_children.each{|c| c. if c. } end |
#x=(nx) ⇒ Object
Sets x to the given integer value.
109 110 111 |
# File 'lib/glimr/layoutable.rb', line 109 def x= nx super nx.to_i end |
#y=(ny) ⇒ Object
Sets y to the given integer value.
114 115 116 |
# File 'lib/glimr/layoutable.rb', line 114 def y= ny super ny.to_i end |