Class: Wads::GuiContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/wads/widgets.rb

Overview

A Gui container is used to allocate space in the x, y two dimensional space to widgets and keep track of where the next widget in the container will be placed. The fill type is one of FILL_VERTICAL_STACK, FILL_HORIZONTAL_STACK, or FILL_FULL_SIZE. Layouts used containers to allocate space across the entire visible application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_x, start_y, width, height, fill_type = FILL_HORIZONTAL_STACK, padding = 5) ⇒ GuiContainer

Returns a new instance of GuiContainer.



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/wads/widgets.rb', line 471

def initialize(start_x, start_y, width, height, fill_type = FILL_HORIZONTAL_STACK, padding = 5)
    @start_x = start_x
    @start_y = start_y
    @next_x = start_x
    @next_y = start_y
    @max_width = width 
    @max_height = height 
    @padding = padding
    if [FILL_VERTICAL_STACK, FILL_HORIZONTAL_STACK, FILL_FULL_SIZE].include? fill_type
        @fill_type = fill_type 
    else 
        raise "#{fill_type} is not a valid fill type"
    end
    @elements = []
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



469
470
471
# File 'lib/wads/widgets.rb', line 469

def elements
  @elements
end

#fill_typeObject

Returns the value of attribute fill_type.



468
469
470
# File 'lib/wads/widgets.rb', line 468

def fill_type
  @fill_type
end

#max_heightObject

Returns the value of attribute max_height.



466
467
468
# File 'lib/wads/widgets.rb', line 466

def max_height
  @max_height
end

#max_widthObject

Returns the value of attribute max_width.



465
466
467
# File 'lib/wads/widgets.rb', line 465

def max_width
  @max_width
end

#next_xObject

Returns the value of attribute next_x.



463
464
465
# File 'lib/wads/widgets.rb', line 463

def next_x
  @next_x
end

#next_yObject

Returns the value of attribute next_y.



464
465
466
# File 'lib/wads/widgets.rb', line 464

def next_y
  @next_y
end

#paddingObject

Returns the value of attribute padding.



467
468
469
# File 'lib/wads/widgets.rb', line 467

def padding
  @padding
end

#start_xObject

Returns the value of attribute start_x.



461
462
463
# File 'lib/wads/widgets.rb', line 461

def start_x
  @start_x
end

#start_yObject

Returns the value of attribute start_y.



462
463
464
# File 'lib/wads/widgets.rb', line 462

def start_y
  @start_y
end

Instance Method Details

#get_coordinates(element_type, args = {}) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/wads/widgets.rb', line 487

def get_coordinates(element_type, args = {})
    default_dim =  WadsConfig.instance.default_dimensions(element_type)
    if default_dim.nil?
        raise "#{element_type} is an undefined element type"
    end
    default_width = default_dim[0]
    default_height = default_dim[1]
    specified_width = args[ARG_DESIRED_WIDTH]
    if specified_width.nil?
        if default_width == "max"
            if fill_type == FILL_VERTICAL_STACK or fill_type == FILL_FULL_SIZE
                the_width = max_width
            else 
                the_width = (@start_x + @max_width) - @next_x
            end
        else
            the_width = default_width 
        end
    else 
        if specified_width > @max_width 
            the_width = @max_width
        else
            the_width = specified_width
        end
    end

    specified_height = args[ARG_DESIRED_HEIGHT]
    if specified_height.nil?
        if default_height == "max"
            if fill_type == FILL_VERTICAL_STACK
                the_height = (@start_y + @max_height) - @next_y
            else
                the_height = max_height
            end
        else
            the_height = default_height 
        end
    else
        if specified_height > @max_height
            the_height = @max_height 
        else
            the_height = specified_height 
        end
    end

    # Not all elements require padding
    padding_exempt = [ELEMENT_IMAGE, ELEMENT_HORIZONTAL_PANEL, ELEMENT_PLOT,
        ELEMENT_VERTICAL_PANEL, ELEMENT_GENERIC, ELEMENT_MAX_PANEL].include? element_type
    if padding_exempt
        # No padding
        width_to_use = the_width
        height_to_use = the_height
        x_to_use = @next_x 
        y_to_use = @next_y
    else
        # Apply padding only if we are the max, i.e. the boundaries
        x_to_use = @next_x + @padding
        y_to_use = @next_y + @padding
        if the_width == @max_width 
            width_to_use = the_width - (2 * @padding)
        else 
            width_to_use = the_width
        end 
        if the_height == @max_height
            height_to_use = the_height - (2 * @padding)
        else 
            height_to_use = the_height
        end 
    end

    # Text elements also honor ARG_TEXT_ALIGN 
    arg_text_align = args[ARG_TEXT_ALIGN]
    if not arg_text_align.nil?
        # left is the default, so check for center or right
        if arg_text_align == TEXT_ALIGN_CENTER 
            x_to_use = @next_x + ((@max_width - specified_width) / 2)
        elsif arg_text_align == TEXT_ALIGN_RIGHT 
            x_to_use = @next_x + @max_width - specified_width - @padding
        end
    end

    coords = Coordinates.new(x_to_use, y_to_use,
                             width_to_use, height_to_use)

    if fill_type == FILL_VERTICAL_STACK
        @next_y = @next_y + the_height + (2 * @padding)
    elsif fill_type == FILL_HORIZONTAL_STACK
        @next_x = @next_x + the_width + (2 * @padding)
    end

    @elements << coords
    coords
end