Class: Wads::Widget

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

Overview

The base class for all widgets. This class provides basic functionality for all gui widgets including maintaining the coordinates and layout used. A widget has a border and background, whose colors are defined by the theme. These can be turned off using the disable_border and disable_background methods. Widgets support a hierarchy of visible elements on the screen. For example, a parent widget may be a form, and it may contain many child widgets such as text labels, input fields, and a submit button. You can add children to a widget using the add or add_child methods. Children are automatically rendered so any child does not need an explicit call to its draw or render method. Children can be placed with x, y positioning relative to their parent for convenience (see the relative_x and relative_y methods).

The draw and update methods are used by their Gosu counterparts. Typically there is one parent Wads widget used by a Gosu app, and it controls drawing all of the child widgets, invoking update on all widgets, and delegating user events. Widgets can override a render method for any specific drawing logic. It is worth showing the draw method here to amplify the point. You do not need to specifically call draw or render on any children. If you want to manage GUI elements outside of the widget hierarchy, then render is the best place to do it.

Likewise, the update method recursively calls the handle_update method on all children in this widget’s hierarchy.

A commonly used method is contains_click(mouse_x, mouse_y) which returns whether this widget contained the mouse event. For a example, a button widget uses this method to determine if it was clicked.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width = 10, height = 10, layout = nil, theme = nil) ⇒ Widget

Returns a new instance of Widget.



1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/wads/widgets.rb', line 1014

def initialize(x, y, width = 10, height = 10, layout = nil, theme = nil) 
    set_absolute_position(x, y)  
    set_dimensions(width, height)
    @base_z = 0
    if uses_layout
        if layout.nil? 
            @layout = WadsConfig.instance.create_layout(x, y, width, height, self)
        else
            @layout = layout
        end
    end
    if theme.nil?
        @gui_theme = WadsConfig.instance.current_theme
    else 
        @gui_theme = theme 
    end
    @visible = true 
    @children = []
    @show_background = true
    @show_border = true 
    @is_selected = false
    @text_input_fields = []
end

Instance Attribute Details

#base_zObject

Returns the value of attribute base_z.



1002
1003
1004
# File 'lib/wads/widgets.rb', line 1002

def base_z
  @base_z
end

#childrenObject

Returns the value of attribute children.



1008
1009
1010
# File 'lib/wads/widgets.rb', line 1008

def children
  @children
end

#gui_themeObject

Returns the value of attribute gui_theme.



1003
1004
1005
# File 'lib/wads/widgets.rb', line 1003

def gui_theme
  @gui_theme
end

#heightObject

Returns the value of attribute height.



1006
1007
1008
# File 'lib/wads/widgets.rb', line 1006

def height
  @height
end

#is_selectedObject

Returns the value of attribute is_selected.



1011
1012
1013
# File 'lib/wads/widgets.rb', line 1011

def is_selected
  @is_selected
end

#layoutObject

Returns the value of attribute layout.



1004
1005
1006
# File 'lib/wads/widgets.rb', line 1004

def layout
  @layout
end

#overlay_widgetObject

Returns the value of attribute overlay_widget.



1009
1010
1011
# File 'lib/wads/widgets.rb', line 1009

def overlay_widget
  @overlay_widget
end

#override_colorObject

Returns the value of attribute override_color.



1010
1011
1012
# File 'lib/wads/widgets.rb', line 1010

def override_color
  @override_color
end

#text_input_fieldsObject

Returns the value of attribute text_input_fields.



1012
1013
1014
# File 'lib/wads/widgets.rb', line 1012

def text_input_fields
  @text_input_fields
end

#visibleObject

Returns the value of attribute visible.



1007
1008
1009
# File 'lib/wads/widgets.rb', line 1007

def visible
  @visible
end

#widthObject

Returns the value of attribute width.



1005
1006
1007
# File 'lib/wads/widgets.rb', line 1005

def width
  @width
end

#xObject

Returns the value of attribute x.



1000
1001
1002
# File 'lib/wads/widgets.rb', line 1000

def x
  @x
end

#yObject

Returns the value of attribute y.



1001
1002
1003
# File 'lib/wads/widgets.rb', line 1001

def y
  @y
end

Instance Method Details

#add(child) ⇒ Object

Add a child widget that will automatically be drawn by this widget and will received delegated events. This is an alias for add_child



1140
1141
1142
# File 'lib/wads/widgets.rb', line 1140

def add(child) 
    add_child(child)
end

#add_axis_lines(rel_x, rel_y, width, height) ⇒ Object

Add child axis lines widget using x, y positioning relative to this widget.



1623
1624
1625
1626
1627
1628
1629
# File 'lib/wads/widgets.rb', line 1623

def add_axis_lines(rel_x, rel_y, width, height)
    new_axis_lines = AxisLines.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y), width, height) 
    new_axis_lines.base_z = @base_z
    new_axis_lines.gui_theme = @gui_theme
    add_child(new_axis_lines)
    new_axis_lines
end

#add_button(label, rel_x, rel_y, width = nil, &block) ⇒ Object

Add a child button widget using x, y positioning relative to this widget. The width of the button will be determined based on the label text unless specified in the optional parameter. The code to execute is provided as a block, as shown in the example below.

add_button("Test Button", 10, 10) do 
  puts "User hit the test button"
end


1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
# File 'lib/wads/widgets.rb', line 1534

def add_button(label, rel_x, rel_y, width = nil, &block)
    if width.nil?
        args = {}
    else 
        args = { ARG_DESIRED_WIDTH => width }
    end
    new_button = Button.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y), label, args)
    new_button.set_action(&block)
    new_button.base_z = @base_z
    new_button.gui_theme = @gui_theme
    add_child(new_button)
    new_button
end

#add_child(child) ⇒ Object

Add a child widget that will automatically be drawn by this widget and will received delegated events.



1148
1149
1150
# File 'lib/wads/widgets.rb', line 1148

def add_child(child) 
    @children << child 
end

#add_delete_button(rel_x, rel_y, &block) ⇒ Object

Add a child delete button widget using x, y positioning relative to this widget. A delete button is a regular button that is rendered as a red X, instead of a text label.



1552
1553
1554
1555
1556
1557
1558
1559
# File 'lib/wads/widgets.rb', line 1552

def add_delete_button(rel_x, rel_y, &block)
    new_delete_button = DeleteButton.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y))
    new_delete_button.set_action(&block)
    new_delete_button.base_z = @base_z
    new_delete_button.gui_theme = @gui_theme
    add_child(new_delete_button)
    new_delete_button 
end

#add_document(content, rel_x, rel_y, width, height) ⇒ Object

Add a child document widget using x, y positioning relative to this widget



1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/wads/widgets.rb', line 1516

def add_document(content, rel_x, rel_y, width, height)
    new_doc = Document.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y),
                           width, height,
                           content)
    new_doc.base_z = @base_z
    new_doc.gui_theme = @gui_theme
    add_child(new_doc)
    new_doc
end

#add_graph_display(rel_x, rel_y, width, height, graph) ⇒ Object

Add a child graph display widget using x, y positioning relative to this widget.



1602
1603
1604
1605
1606
1607
# File 'lib/wads/widgets.rb', line 1602

def add_graph_display(rel_x, rel_y, width, height, graph)
    new_graph = GraphWidget.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y), width, height, graph) 
    new_graph.base_z = @base_z
    add_child(new_graph)
    new_graph
end

#add_image(filename, rel_x, rel_y) ⇒ Object

Add a child image widget using x, y positioning relative to this widget.



1634
1635
1636
1637
1638
1639
1640
# File 'lib/wads/widgets.rb', line 1634

def add_image(filename, rel_x, rel_y)
    new_image = ImageWidget.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y), img)
    new_image.base_z = @base_z
    new_image.gui_theme = @gui_theme
    add_child(new_image)
    new_image
end

#add_multi_select_table(rel_x, rel_y, width, height, column_headers, max_visible_rows = 10) ⇒ Object

Add a child table widget using x, y positioning relative to this widget. The user can zero to many items in the table.



1590
1591
1592
1593
1594
1595
1596
1597
# File 'lib/wads/widgets.rb', line 1590

def add_multi_select_table(rel_x, rel_y, width, height, column_headers, max_visible_rows = 10)
    new_table = MultiSelectTable.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y),
                      width, height, column_headers, max_visible_rows)
    new_table.base_z = @base_z
    new_table.gui_theme = @gui_theme
    add_child(new_table)
    new_table
end

#add_overlay(overlay) ⇒ Object

Add an overlay widget that is drawn on top of (at a higher z level) this widget



1645
1646
1647
1648
1649
# File 'lib/wads/widgets.rb', line 1645

def add_overlay(overlay)
    overlay.base_z = @base_z + 10
    add_child(overlay)
    @overlay_widget = overlay
end

#add_panel(section, args = {}) ⇒ Object



1079
1080
1081
1082
# File 'lib/wads/widgets.rb', line 1079

def add_panel(section, args = {})
    get_layout.add_max_panel({ ARG_SECTION => section,
                               ARG_THEME => @gui_theme}.merge(args))
end

#add_plot(rel_x, rel_y, width, height) ⇒ Object

Add a child plot display widget using x, y positioning relative to this widget.



1612
1613
1614
1615
1616
1617
1618
# File 'lib/wads/widgets.rb', line 1612

def add_plot(rel_x, rel_y, width, height)
    new_plot = Plot.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y), width, height) 
    new_plot.base_z = @base_z
    new_plot.gui_theme = @gui_theme
    add_child(new_plot)
    new_plot
end

#add_single_select_table(rel_x, rel_y, width, height, column_headers, max_visible_rows = 10) ⇒ Object

Add a child table widget using x, y positioning relative to this widget. The user can select up to one and only one item in the table.



1577
1578
1579
1580
1581
1582
1583
1584
# File 'lib/wads/widgets.rb', line 1577

def add_single_select_table(rel_x, rel_y, width, height, column_headers, max_visible_rows = 10)
    new_table = SingleSelectTable.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y),
                      width, height, column_headers, max_visible_rows)
    new_table.base_z = @base_z
    new_table.gui_theme = @gui_theme
    add_child(new_table)
    new_table
end

#add_table(rel_x, rel_y, width, height, column_headers, max_visible_rows = 10) ⇒ Object

Add a child table widget using x, y positioning relative to this widget.



1564
1565
1566
1567
1568
1569
1570
1571
# File 'lib/wads/widgets.rb', line 1564

def add_table(rel_x, rel_y, width, height, column_headers, max_visible_rows = 10)
    new_table = Table.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y),
                      width, height, column_headers, max_visible_rows)
    new_table.base_z = @base_z
    new_table.gui_theme = @gui_theme
    add_child(new_table)
    new_table
end

#add_text(message, rel_x, rel_y, color = nil, use_large_font = false) ⇒ Object

Add a child text widget using x, y positioning relative to this widget



1504
1505
1506
1507
1508
1509
1510
1511
# File 'lib/wads/widgets.rb', line 1504

def add_text(message, rel_x, rel_y, color = nil, use_large_font = false)
    new_text = Text.new(x_pixel_to_screen(rel_x), y_pixel_to_screen(rel_y), message,
                                          { ARG_COLOR => color, ARG_USE_LARGE_FONT => use_large_font})
    new_text.base_z = @base_z
    new_text.gui_theme = @gui_theme
    add_child(new_text)
    new_text
end

#border_colorObject



1118
1119
1120
# File 'lib/wads/widgets.rb', line 1118

def border_color 
    @gui_theme.border_color 
end

#bottom_edgeObject

A convenience method to return the bottom y coordinate of this widget



1244
1245
1246
# File 'lib/wads/widgets.rb', line 1244

def bottom_edge
    @y + @height - 1
end

#button_down(id, mouse_x, mouse_y) ⇒ Object

The framework implementation of the main Gosu button down method. This method separates out mouse events from keyboard events, and calls the appropriate callback. As a widget author, do not override this method. Your callbacks to implement are:

handle_mouse_down(mouse_x, mouse_y)
handle_right_mouse(mouse_x, mouse_y)
handle_key_press(id, mouse_x, mouse_y)


1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
# File 'lib/wads/widgets.rb', line 1391

def button_down(id, mouse_x, mouse_y)
    if @overlay_widget 
        result = @overlay_widget.button_down(id, mouse_x, mouse_y)
        if not result.nil? and result.is_a? WidgetResult
            intercept_widget_event(result)
            if result.close_widget
                # remove the overlay widget frmo children, set to null
                # hopefully this closes and gets us back to normal
                remove_child(@overlay_widget)
                @overlay_widget = nil
            end
        end
        return
    end

    if id == Gosu::MsLeft
        # Special handling for text input fields
        # Mouse click: Select text field based on mouse position.
        if not @text_input_fields.empty?
            WadsConfig.instance.get_window.text_input = @text_input_fields.find { |tf| tf.under_point?(mouse_x, mouse_y) }
            # Advanced: Move caret to clicked position
            WadsConfig.instance.get_window.text_input.move_caret(mouse_x) unless WadsConfig.instance.get_window.text_input.nil?
        end

        result = handle_mouse_down mouse_x, mouse_y
    elsif id == Gosu::MsRight
        result = handle_right_mouse mouse_x, mouse_y
    else 
        result = handle_key_press id, mouse_x, mouse_y
    end

    if not result.nil? and result.is_a? WidgetResult
        return result 
    end

    @children.each do |child| 
        if id == Gosu::MsLeft
            if child.contains_click(mouse_x, mouse_y) 
                result = child.button_down id, mouse_x, mouse_y
                if not result.nil? and result.is_a? WidgetResult
                    intercept_widget_event(result)
                    return result 
                end
            end 
        else 
            result = child.button_down id, mouse_x, mouse_y
            if not result.nil? and result.is_a? WidgetResult
                intercept_widget_event(result)
                return result 
            end
        end
    end 
end

#button_up(id, mouse_x, mouse_y) ⇒ Object

The framework implementation of the main Gosu button up method. This method separates out mouse events from keyboard events. Only the mouse up event is propagated through the child hierarchy. As a widget author, do not override this method. Your callback to implement is:

handle_mouse_up(mouse_x, mouse_y)


1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
# File 'lib/wads/widgets.rb', line 1453

def button_up(id, mouse_x, mouse_y)
    if @overlay_widget 
        return @overlay_widget.button_up(id, mouse_x, mouse_y)
    end
    
    if id == Gosu::MsLeft
        result = handle_mouse_up mouse_x, mouse_y
        if not result.nil? and result.is_a? WidgetResult
            return result 
        end
    end

    @children.each do |child| 
        if id == Gosu::MsLeft
            if child.contains_click(mouse_x, mouse_y) 
                result = child.handle_mouse_up mouse_x, mouse_y
                if not result.nil? and result.is_a? WidgetResult
                    return result 
                end
            end 
        end
    end
end

#center_childrenObject

For all child widgets, adjust the x coordinate so that they are centered.



1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
# File 'lib/wads/widgets.rb', line 1653

def center_children
    if @children.empty?
        return 
    end
    number_of_children = @children.size 
    total_width_of_children = 0
    @children.each do |child|
        total_width_of_children = total_width_of_children + child.width + 5
    end
    total_width_of_children = total_width_of_children - 5

    start_x = (@width - total_width_of_children) / 2
    @children.each do |child|
        child.x = start_x 
        start_x = start_x + child.width + 5
    end
end

#center_xObject

A convenience method to return the center x coordinate of this widget



1251
1252
1253
# File 'lib/wads/widgets.rb', line 1251

def center_x
    @x + ((right_edge - @x) / 2)
end

#center_yObject

A convenience method to return the center y coordinate of this widget



1258
1259
1260
# File 'lib/wads/widgets.rb', line 1258

def center_y
    @y + ((bottom_edge - @y) / 2)
end

#clear_childrenObject

Remove all children from this widget



1188
1189
1190
# File 'lib/wads/widgets.rb', line 1188

def clear_children 
    @children = [] 
end

#contains_click(mouse_x, mouse_y) ⇒ Object



1338
1339
1340
# File 'lib/wads/widgets.rb', line 1338

def contains_click(mouse_x, mouse_y)
    mouse_x >= @x and mouse_x <= right_edge and mouse_y >= @y and mouse_y <= bottom_edge
end

#debug(message) ⇒ Object



1038
1039
1040
# File 'lib/wads/widgets.rb', line 1038

def debug(message)
    WadsConfig.instance.get_logger.debug message 
end

#disable_backgroundObject

Drawing the background is on by default. Use this method to prevent drawing a background.



1195
1196
1197
# File 'lib/wads/widgets.rb', line 1195

def disable_background
    @show_background = false
end

#disable_borderObject

Drawing the border is on by default. Use this method to prevent drawing a border.



1202
1203
1204
# File 'lib/wads/widgets.rb', line 1202

def disable_border
    @show_border = false 
end

#drawObject

The primary draw method, used by the main Gosu loop draw method. A common usage pattern is to have a primary widget in your Gosu app that calls this draw method. All children of this widget are then automatically drawn by this method recursively. Note that as a widget author, you should only implement/override the render method. This is a framework implementation that will handle child rendering and invoke render as a user-implemented callback.



1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
# File 'lib/wads/widgets.rb', line 1300

def draw 
    if @visible 
        render
        if @is_selected
            draw_background(Z_ORDER_SELECTION_BACKGROUND, @gui_theme.selection_color)
        elsif @show_background
            draw_background
        end
        if @show_border
            draw_border
        end
        @children.each do |child| 
            child.draw 
        end 
    end 
end

#draw_background(z_override = nil, color_override = nil) ⇒ Object



1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/wads/widgets.rb', line 1317

def draw_background(z_override = nil, color_override = nil)
    if color_override.nil? 
        bgcolor = @gui_theme.background_color
    else 
        bgcolor = color_override
    end
    if z_override 
        z = relative_z_order(z_override)
    else 
        z = relative_z_order(Z_ORDER_BACKGROUND) 
    end
    Gosu::draw_rect(@x + 1, @y + 1, @width - 3, @height - 3, bgcolor, z) 
end

#draw_borderObject



1331
1332
1333
1334
1335
1336
# File 'lib/wads/widgets.rb', line 1331

def draw_border
    Gosu::draw_line @x, @y, @gui_theme.border_color, right_edge, @y, @gui_theme.border_color, relative_z_order(Z_ORDER_BORDER)
    Gosu::draw_line @x, @y, @gui_theme.border_color, @x, bottom_edge, @gui_theme.border_color, relative_z_order(Z_ORDER_BORDER)
    Gosu::draw_line @x,bottom_edge, @gui_theme.border_color, right_edge, bottom_edge, @gui_theme.border_color, relative_z_order(Z_ORDER_BORDER)
    Gosu::draw_line right_edge, @y, @gui_theme.border_color, right_edge, bottom_edge, @gui_theme.border_color, relative_z_order(Z_ORDER_BORDER)
end

#enable_backgroundObject

Turn back on drawing of the background



1216
1217
1218
# File 'lib/wads/widgets.rb', line 1216

def enable_background
    @show_background = true 
end

#enable_borderObject

Turn back on drawing of the border



1209
1210
1211
# File 'lib/wads/widgets.rb', line 1209

def enable_border
    @show_border = true 
end

#error(message) ⇒ Object



1047
1048
1049
# File 'lib/wads/widgets.rb', line 1047

def error(message)
    WadsConfig.instance.get_logger.error message 
end

#get_layoutObject



1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/wads/widgets.rb', line 1065

def get_layout 
    if not uses_layout 
        raise "The widget #{self.class.name} does not support layouts"
    end
    if @layout.nil? 
        raise "No layout was defined for #{self.class.name}"
    end 
    @layout 
end

#get_themeObject



1084
1085
1086
# File 'lib/wads/widgets.rb', line 1084

def get_theme 
    @gui_theme
end

#graphics_colorObject



1100
1101
1102
1103
1104
1105
# File 'lib/wads/widgets.rb', line 1100

def graphics_color 
    if @override_color 
        return @override_color 
    end 
    @gui_theme.graphic_elements_color 
end

#handle_key_press(id, mouse_x, mouse_y) ⇒ Object

Override this method in your subclass to process keyboard events. The base implementation is empty. Note that the mouse was not necessarily positioned over this widget. You can check this using the contains_click(mouse_x, mouse_y) method and decide if you want to process the event based on that, if desired.



1703
1704
1705
# File 'lib/wads/widgets.rb', line 1703

def handle_key_press id, mouse_x, mouse_y
    # empty base implementation
end

#handle_mouse_down(mouse_x, mouse_y) ⇒ Object

Override this method in your subclass to process mouse down events. The base implementation is empty



1675
1676
1677
# File 'lib/wads/widgets.rb', line 1675

def handle_mouse_down mouse_x, mouse_y
    # empty base implementation
end

#handle_mouse_up(mouse_x, mouse_y) ⇒ Object

Override this method in your subclass to process mouse up events. The base implementation is empty



1683
1684
1685
# File 'lib/wads/widgets.rb', line 1683

def handle_mouse_up mouse_x, mouse_y
    # empty base implementation
end

#handle_right_mouse(mouse_x, mouse_y) ⇒ Object

Override this method in your subclass to process the right mouse click event. Note we do not differentiate between up and down for the right mouse button. The base implementation is empty



1692
1693
1694
# File 'lib/wads/widgets.rb', line 1692

def handle_right_mouse mouse_x, mouse_y
    # empty base implementation
end

#handle_update(update_count, mouse_x, mouse_y) ⇒ Object

Override this method in your subclass to perform any logic needed as part of the main Gosu update loop. In most cases, this method is invoked 60 times per second.



1712
1713
1714
# File 'lib/wads/widgets.rb', line 1712

def handle_update update_count, mouse_x, mouse_y
    # empty base implementation
end

#info(message) ⇒ Object



1041
1042
1043
# File 'lib/wads/widgets.rb', line 1041

def info(message)
    WadsConfig.instance.get_logger.info message 
end

#intercept_widget_event(result) ⇒ Object



1735
1736
1737
1738
# File 'lib/wads/widgets.rb', line 1735

def intercept_widget_event(result)
    # Base implementation just relays the event
    result
end

#left_edgeObject

A convenience method, or alias, to return the left x coordinate of this widget.



1223
1224
1225
# File 'lib/wads/widgets.rb', line 1223

def left_edge
    @x
end

#move_recursive_absolute(new_x, new_y) ⇒ Object

Move this widget to an absolute x, y position on the screen. It will automatically move all child widgets, however be warned that if you are manually rendering any elements within your own render logic, you will need to deal with that seperately as the base class does not have access to its coordinates.



1269
1270
1271
1272
1273
# File 'lib/wads/widgets.rb', line 1269

def move_recursive_absolute(new_x, new_y)
    delta_x = new_x - @x 
    delta_y = new_y - @y
    move_recursive_delta(delta_x, delta_y)
end

#move_recursive_delta(delta_x, delta_y) ⇒ Object

Move this widget to a relative number of x, y pixels on the screen. It will automatically move all child widgets, however be warned that if you are manually rendering any elements within your own render logic, you will need to deal with that seperately as the base class does not have access to its coordinates.



1282
1283
1284
1285
1286
1287
1288
# File 'lib/wads/widgets.rb', line 1282

def move_recursive_delta(delta_x, delta_y)
    @x = @x + delta_x
    @y = @y + delta_y
    @children.each do |child| 
        child.move_recursive_delta(delta_x, delta_y) 
    end 
end

#overlaps_with(other_widget) ⇒ Object

Return true if any part of the given widget overlaps on the screen with this widget as defined by the rectangle from the upper left corner to the bottom right. Note that your widget may not necessariliy draw pixels in this entire space.



1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/wads/widgets.rb', line 1347

def overlaps_with(other_widget)
    if other_widget.contains_click(@x, @y)
        return true 
    end 
    if other_widget.contains_click(right_edge, @y)
        return true 
    end 
    if other_widget.contains_click(right_edge, bottom_edge - 1)
        return true 
    end 
    if other_widget.contains_click(@x, bottom_edge - 1)
        return true 
    end 
    if other_widget.contains_click(center_x, center_y)
        return true 
    end 
    return false
end

#relative_x(x) ⇒ Object

Return the absolute x coordinate given the relative x pixel to this widget



1480
1481
1482
# File 'lib/wads/widgets.rb', line 1480

def relative_x(x)
    x_pixel_to_screen(x)
end

#relative_y(y) ⇒ Object

Return the absolute y coordinate given the relative y pixel to this widget



1492
1493
1494
# File 'lib/wads/widgets.rb', line 1492

def relative_y(y)
    y_pixel_to_screen(y)
end

#relative_z_order(relative_order) ⇒ Object



1132
1133
1134
# File 'lib/wads/widgets.rb', line 1132

def relative_z_order(relative_order)
    @base_z + relative_order 
end

#remove_child(child) ⇒ Object

Remove the given child widget



1155
1156
1157
# File 'lib/wads/widgets.rb', line 1155

def remove_child(child)
    @children.delete(child)
end

#remove_children(list) ⇒ Object

Remove a list of child widgets



1162
1163
1164
1165
1166
# File 'lib/wads/widgets.rb', line 1162

def remove_children(list)
    list.each do |child|
        @children.delete(child)
    end
end

#remove_children_by_type(class_name_token) ⇒ Object

Remove all children whose class name includes the given token. This method can be used if you do not have a saved list of the widgets you want to remove.



1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
# File 'lib/wads/widgets.rb', line 1173

def remove_children_by_type(class_name_token)
    children_to_remove = []
    @children.each do |child|
        if child.class.name.include? class_name_token 
            children_to_remove << child 
        end 
    end 
    children_to_remove.each do |child|
        @children.delete(child)
    end
end

#renderObject

Override this method in your subclass to perform any custom rendering logic. Note that child widgets are automatically drawn and you do not need to do that yourself.



1721
1722
1723
# File 'lib/wads/widgets.rb', line 1721

def render 
    # Base implementation is empty
end

#right_edgeObject

A convenience method to return the right x coordinate of this widget.



1230
1231
1232
# File 'lib/wads/widgets.rb', line 1230

def right_edge
    @x + @width - 1
end

#selection_colorObject



1114
1115
1116
# File 'lib/wads/widgets.rb', line 1114

def selection_color 
    @gui_theme.selection_color 
end

#set_absolute_position(x, y) ⇒ Object



1051
1052
1053
1054
# File 'lib/wads/widgets.rb', line 1051

def set_absolute_position(x, y) 
    @x = x 
    @y = y 
end

#set_dimensions(width, height) ⇒ Object



1056
1057
1058
1059
# File 'lib/wads/widgets.rb', line 1056

def set_dimensions(width, height)
    @width = width
    @height = height 
end

#set_layout(layout_type, args = {}) ⇒ Object



1075
1076
1077
# File 'lib/wads/widgets.rb', line 1075

def set_layout(layout_type, args = {})
    @layout = WadsConfig.instance.create_layout_for_widget(self, layout_type, args)
end

#set_selectedObject



1092
1093
1094
# File 'lib/wads/widgets.rb', line 1092

def set_selected 
    @is_selected = true
end

#set_theme(new_theme) ⇒ Object



1088
1089
1090
# File 'lib/wads/widgets.rb', line 1088

def set_theme(new_theme)
    @gui_theme = new_theme
end

#text_colorObject



1107
1108
1109
1110
1111
1112
# File 'lib/wads/widgets.rb', line 1107

def text_color 
    if @override_color 
        return @override_color 
    end 
    @gui_theme.text_color 
end

#top_edgeObject

A convenience method, or alias, to return the top y coordinate of this widget.



1237
1238
1239
# File 'lib/wads/widgets.rb', line 1237

def top_edge
    @y
end

#unset_selectedObject



1096
1097
1098
# File 'lib/wads/widgets.rb', line 1096

def unset_selected 
    @is_selected = false
end

#update(update_count, mouse_x, mouse_y) ⇒ Object

The framework implementation of the main Gosu update loop. This method propagates the event to all child widgets as well. As a widget author, do not override this method. Your callback to implement is the handle_update(update_count, mouse_x, mouse_y) method.



1372
1373
1374
1375
1376
1377
1378
1379
1380
# File 'lib/wads/widgets.rb', line 1372

def update(update_count, mouse_x, mouse_y)
    if @overlay_widget 
        @overlay_widget.update(update_count, mouse_x, mouse_y)
    end
    handle_update(update_count, mouse_x, mouse_y) 
    @children.each do |child| 
        child.update(update_count, mouse_x, mouse_y) 
    end 
end

#uses_layoutObject



1061
1062
1063
# File 'lib/wads/widgets.rb', line 1061

def uses_layout
    true 
end

#warn(message) ⇒ Object



1044
1045
1046
# File 'lib/wads/widgets.rb', line 1044

def warn(message)
    WadsConfig.instance.get_logger.warn message 
end

#widget_zObject

Return the relative z order compared to other widgets. The absolute z order is the base plus this value. Its calculated relative so that overlay widgets can be on top of base displays.



1731
1732
1733
# File 'lib/wads/widgets.rb', line 1731

def widget_z 
    0
end

#x_pixel_to_screen(x) ⇒ Object

An alias for relative_x



1485
1486
1487
# File 'lib/wads/widgets.rb', line 1485

def x_pixel_to_screen(x)
    @x + x
end

#y_pixel_to_screen(y) ⇒ Object

An alias for relative_y



1497
1498
1499
# File 'lib/wads/widgets.rb', line 1497

def y_pixel_to_screen(y)
    @y + y
end

#z_orderObject

The z order is determined by taking the base_z and adding the widget specific value. An overlay widget has a base_z that is +10 higher than the widget underneath it. The widget_z method provides a relative ordering that is common for user interfaces. For example, text is higher than graphic elements and backgrounds.



1128
1129
1130
# File 'lib/wads/widgets.rb', line 1128

def z_order 
    @base_z + widget_z
end