Class: Wads::Button

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

Overview

Displays a button at the specified x, y location. The button width is based on the label text unless specified using the optional parameter. The code to executeon a button click is specified using the set_action method, however typical using involves the widget or layout form of add_button. For example: add_button(“Test Button”, 10, 10) do

puts "User hit the test button"

end

Direct Known Subclasses

DeleteButton, NodeWidget

Instance Attribute Summary collapse

Attributes inherited from Widget

#base_z, #children, #gui_theme, #height, #is_selected, #layout, #overlay_widget, #override_color, #text_input_fields, #visible, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Widget

#add, #add_axis_lines, #add_button, #add_child, #add_delete_button, #add_document, #add_graph_display, #add_image, #add_multi_select_table, #add_overlay, #add_panel, #add_plot, #add_single_select_table, #add_table, #add_text, #border_color, #bottom_edge, #button_down, #button_up, #center_children, #center_x, #center_y, #clear_children, #contains_click, #debug, #disable_background, #disable_border, #draw, #draw_background, #draw_border, #enable_background, #enable_border, #error, #get_layout, #get_theme, #graphics_color, #handle_key_press, #handle_mouse_up, #handle_right_mouse, #info, #intercept_widget_event, #left_edge, #move_recursive_absolute, #move_recursive_delta, #overlaps_with, #relative_x, #relative_y, #relative_z_order, #remove_child, #remove_children, #remove_children_by_type, #right_edge, #selection_color, #set_absolute_position, #set_dimensions, #set_layout, #set_selected, #set_theme, #text_color, #top_edge, #unset_selected, #update, #uses_layout, #warn, #x_pixel_to_screen, #y_pixel_to_screen, #z_order

Constructor Details

#initialize(x, y, label, args = {}) ⇒ Button

Returns a new instance of Button.



1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
# File 'lib/wads/widgets.rb', line 1909

def initialize(x, y, label, args = {}) 
    super(x, y) 
    @label = label
    if args[ARG_THEME]
        @gui_theme = args[ARG_THEME]
    end
    @text_pixel_width = @gui_theme.font.text_width(@label)
    if args[ARG_DESIRED_WIDTH]
        @width = args[ARG_DESIRED_WIDTH] 
    else 
        @width = @text_pixel_width + 10
    end
    @height = 26
    @is_pressed = false
    @is_pressed_update_count = -100
end

Instance Attribute Details

#action_codeObject

Returns the value of attribute action_code.



1907
1908
1909
# File 'lib/wads/widgets.rb', line 1907

def action_code
  @action_code
end

#is_pressedObject

Returns the value of attribute is_pressed.



1906
1907
1908
# File 'lib/wads/widgets.rb', line 1906

def is_pressed
  @is_pressed
end

#labelObject

Returns the value of attribute label.



1905
1906
1907
# File 'lib/wads/widgets.rb', line 1905

def label
  @label
end

Instance Method Details

#handle_mouse_down(mouse_x, mouse_y) ⇒ Object



1939
1940
1941
1942
1943
1944
# File 'lib/wads/widgets.rb', line 1939

def handle_mouse_down mouse_x, mouse_y
    @is_pressed = true
    if @action_code
        @action_code.call
    end
end

#handle_update(update_count, mouse_x, mouse_y) ⇒ Object



1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
# File 'lib/wads/widgets.rb', line 1946

def handle_update update_count, mouse_x, mouse_y
    if @is_pressed
        @is_pressed_update_count = update_count
        @is_pressed = false
    end

    if update_count < @is_pressed_update_count + 15
        unset_selected
    elsif contains_click(mouse_x, mouse_y)
        set_selected
    else 
        unset_selected
    end
end

#renderObject



1926
1927
1928
1929
# File 'lib/wads/widgets.rb', line 1926

def render 
    text_x = center_x - (@text_pixel_width / 2)
    @gui_theme.font.draw_text(@label, text_x, @y, z_order, 1, 1, text_color)
end

#set_action(&block) ⇒ Object



1935
1936
1937
# File 'lib/wads/widgets.rb', line 1935

def set_action(&block) 
    @action_code = block
end

#widget_zObject



1931
1932
1933
# File 'lib/wads/widgets.rb', line 1931

def widget_z 
    Z_ORDER_TEXT
end