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.



998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/wads/widgets.rb', line 998

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.



986
987
988
# File 'lib/wads/widgets.rb', line 986

def base_z
  @base_z
end

#childrenObject

Returns the value of attribute children.



992
993
994
# File 'lib/wads/widgets.rb', line 992

def children
  @children
end

#gui_themeObject

Returns the value of attribute gui_theme.



987
988
989
# File 'lib/wads/widgets.rb', line 987

def gui_theme
  @gui_theme
end

#heightObject

Returns the value of attribute height.



990
991
992
# File 'lib/wads/widgets.rb', line 990

def height
  @height
end

#is_selectedObject

Returns the value of attribute is_selected.



995
996
997
# File 'lib/wads/widgets.rb', line 995

def is_selected
  @is_selected
end

#layoutObject

Returns the value of attribute layout.



988
989
990
# File 'lib/wads/widgets.rb', line 988

def layout
  @layout
end

#overlay_widgetObject

Returns the value of attribute overlay_widget.



993
994
995
# File 'lib/wads/widgets.rb', line 993

def overlay_widget
  @overlay_widget
end

#override_colorObject

Returns the value of attribute override_color.



994
995
996
# File 'lib/wads/widgets.rb', line 994

def override_color
  @override_color
end

#text_input_fieldsObject

Returns the value of attribute text_input_fields.



996
997
998
# File 'lib/wads/widgets.rb', line 996

def text_input_fields
  @text_input_fields
end

#visibleObject

Returns the value of attribute visible.



991
992
993
# File 'lib/wads/widgets.rb', line 991

def visible
  @visible
end

#widthObject

Returns the value of attribute width.



989
990
991
# File 'lib/wads/widgets.rb', line 989

def width
  @width
end

#xObject

Returns the value of attribute x.



984
985
986
# File 'lib/wads/widgets.rb', line 984

def x
  @x
end

#yObject

Returns the value of attribute y.



985
986
987
# File 'lib/wads/widgets.rb', line 985

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



1134
1135
1136
# File 'lib/wads/widgets.rb', line 1134

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.



1627
1628
1629
1630
1631
1632
1633
# File 'lib/wads/widgets.rb', line 1627

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


1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
# File 'lib/wads/widgets.rb', line 1538

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.



1142
1143
1144
# File 'lib/wads/widgets.rb', line 1142

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.



1556
1557
1558
1559
1560
1561
1562
1563
# File 'lib/wads/widgets.rb', line 1556

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



1520
1521
1522
1523
1524
1525
1526
1527
1528
# File 'lib/wads/widgets.rb', line 1520

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.



1606
1607
1608
1609
1610
1611
# File 'lib/wads/widgets.rb', line 1606

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.



1638
1639
1640
1641
1642
1643
1644
# File 'lib/wads/widgets.rb', line 1638

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.



1594
1595
1596
1597
1598
1599
1600
1601
# File 'lib/wads/widgets.rb', line 1594

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



1649
1650
1651
1652
1653
# File 'lib/wads/widgets.rb', line 1649

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

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



1071
1072
1073
1074
1075
1076
# File 'lib/wads/widgets.rb', line 1071

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

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

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



1616
1617
1618
1619
1620
1621
1622
# File 'lib/wads/widgets.rb', line 1616

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.



1581
1582
1583
1584
1585
1586
1587
1588
# File 'lib/wads/widgets.rb', line 1581

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.



1568
1569
1570
1571
1572
1573
1574
1575
# File 'lib/wads/widgets.rb', line 1568

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



1508
1509
1510
1511
1512
1513
1514
1515
# File 'lib/wads/widgets.rb', line 1508

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



1112
1113
1114
# File 'lib/wads/widgets.rb', line 1112

def border_color 
    @gui_theme.border_color 
end

#bottom_edgeObject

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



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

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)


1385
1386
1387
1388
1389
1390
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
# File 'lib/wads/widgets.rb', line 1385

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)


1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
# File 'lib/wads/widgets.rb', line 1447

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
    else 
        result = handle_key_up id, 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 
        else 
            result = handle_key_up id, mouse_x, mouse_y
            if not result.nil? and result.is_a? WidgetResult
                return result 
            end
        end
    end
end

#center_childrenObject

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



1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
# File 'lib/wads/widgets.rb', line 1657

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



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

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

#center_yObject

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



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

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

#clear_childrenObject

Remove all children from this widget



1182
1183
1184
# File 'lib/wads/widgets.rb', line 1182

def clear_children 
    @children = [] 
end

#contains_click(mouse_x, mouse_y) ⇒ Object



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

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



1030
1031
1032
# File 'lib/wads/widgets.rb', line 1030

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.



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

def disable_background
    @show_background = false
end

#disable_borderObject

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



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

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.



1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
# File 'lib/wads/widgets.rb', line 1294

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



1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
# File 'lib/wads/widgets.rb', line 1311

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



1325
1326
1327
1328
1329
1330
# File 'lib/wads/widgets.rb', line 1325

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



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

def enable_background
    @show_background = true 
end

#enable_borderObject

Turn back on drawing of the border



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

def enable_border
    @show_border = true 
end

#error(message) ⇒ Object



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

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

#get_layoutObject



1057
1058
1059
1060
1061
1062
1063
1064
1065
# File 'lib/wads/widgets.rb', line 1057

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



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

def get_theme 
    @gui_theme
end

#graphics_colorObject



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

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

#handle_key_held_down(id, mouse_x, mouse_y) ⇒ Object

This callback is invoked for any key registered by the register_hold_down_key(id) method.



1715
1716
1717
# File 'lib/wads/widgets.rb', line 1715

def handle_key_held_down id, mouse_x, mouse_y
    # empty base implementation
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.



1707
1708
1709
# File 'lib/wads/widgets.rb', line 1707

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

#handle_key_up(id, mouse_x, mouse_y) ⇒ Object

Override this method in your subclass to process when a key is released. 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.



1726
1727
1728
# File 'lib/wads/widgets.rb', line 1726

def handle_key_up 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



1679
1680
1681
# File 'lib/wads/widgets.rb', line 1679

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



1687
1688
1689
# File 'lib/wads/widgets.rb', line 1687

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



1696
1697
1698
# File 'lib/wads/widgets.rb', line 1696

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.



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

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

#info(message) ⇒ Object



1033
1034
1035
# File 'lib/wads/widgets.rb', line 1033

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

#intercept_widget_event(result) ⇒ Object



1758
1759
1760
1761
# File 'lib/wads/widgets.rb', line 1758

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.



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

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.



1263
1264
1265
1266
1267
# File 'lib/wads/widgets.rb', line 1263

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.



1276
1277
1278
1279
1280
1281
1282
# File 'lib/wads/widgets.rb', line 1276

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.



1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
# File 'lib/wads/widgets.rb', line 1341

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

#pad(str, size, left_align = false) ⇒ Object



1022
1023
1024
1025
1026
1027
1028
1029
# File 'lib/wads/widgets.rb', line 1022

def pad(str, size, left_align = false)
    str = str.to_s
    if left_align
        str[0, size].ljust(size, ' ')
    else
        str[0, size].rjust(size, ' ')
    end
end

#relative_x(x) ⇒ Object

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



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

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



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

def relative_y(y)
    y_pixel_to_screen(y)
end

#relative_z_order(relative_order) ⇒ Object



1126
1127
1128
# File 'lib/wads/widgets.rb', line 1126

def relative_z_order(relative_order)
    @base_z + relative_order 
end

#remove_child(child) ⇒ Object

Remove the given child widget



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

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

#remove_children(list) ⇒ Object

Remove a list of child widgets



1156
1157
1158
1159
1160
# File 'lib/wads/widgets.rb', line 1156

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.



1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/wads/widgets.rb', line 1167

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.



1744
1745
1746
# File 'lib/wads/widgets.rb', line 1744

def render 
    # Base implementation is empty
end

#right_edgeObject

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



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

def right_edge
    @x + @width - 1
end

#selection_colorObject



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

def selection_color 
    @gui_theme.selection_color 
end

#set_absolute_position(x, y) ⇒ Object



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

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

#set_dimensions(width, height) ⇒ Object



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

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

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



1067
1068
1069
# File 'lib/wads/widgets.rb', line 1067

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

#set_selectedObject



1086
1087
1088
# File 'lib/wads/widgets.rb', line 1086

def set_selected 
    @is_selected = true
end

#set_theme(new_theme) ⇒ Object



1082
1083
1084
# File 'lib/wads/widgets.rb', line 1082

def set_theme(new_theme)
    @gui_theme = new_theme
end

#text_colorObject



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

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.



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

def top_edge
    @y
end

#unset_selectedObject



1090
1091
1092
# File 'lib/wads/widgets.rb', line 1090

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.



1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/wads/widgets.rb', line 1366

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



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

def uses_layout
    true 
end

#warn(message) ⇒ Object



1036
1037
1038
# File 'lib/wads/widgets.rb', line 1036

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.



1754
1755
1756
# File 'lib/wads/widgets.rb', line 1754

def widget_z 
    0
end

#x_pixel_to_screen(x) ⇒ Object

An alias for relative_x



1489
1490
1491
# File 'lib/wads/widgets.rb', line 1489

def x_pixel_to_screen(x)
    @x + x
end

#y_pixel_to_screen(y) ⇒ Object

An alias for relative_y



1501
1502
1503
# File 'lib/wads/widgets.rb', line 1501

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.



1122
1123
1124
# File 'lib/wads/widgets.rb', line 1122

def z_order 
    @base_z + widget_z
end