Class: MiniGL::Button
Overview
This class represents a button.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
The current state of the button.
-
#text ⇒ Object
The text of the button.
Attributes inherited from Component
#enabled, #h, #params, #visible, #w, #x, #y
Instance Method Summary collapse
-
#click ⇒ Object
Executes the button click action.
-
#draw(alpha = 0xff, z_index = 0) ⇒ Object
Draws the button in the screen.
-
#enabled=(value) ⇒ Object
:nodoc:.
-
#initialize(x, y, font, text, img, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action) ⇒ Button
constructor
Creates a button.
-
#set_position(x, y) ⇒ Object
Sets the position of the button in the screen.
-
#update ⇒ Object
Updates the button, checking the mouse movement and buttons to define the button state.
Constructor Details
#initialize(x, y, font, text, img, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action) ⇒ Button
Creates a button.
Parameters:
- x
-
The x-coordinate where the button will be drawn in the screen.
- y
-
The y-coordinate where the button will be drawn in the screen.
- font
-
The
Gosu::Fontobject that will be used to draw the button text. - text
-
The button text. Can be
nilor empty. - img
-
A spritesheet containing four images in a column, representing, from top to bottom, the default state, the hover state (when the mouse is over the button), the pressed state (when the mouse button is down and the cursor is over the button) and the disabled state. If
nil, thewidthandheightparameters must be provided. - text_color
-
Color of the button text, in hexadecimal RRGGBB format.
- disabled_text_color
-
Color of the button text, when it’s disabled, in hexadecimal RRGGBB format.
- over_text_color
-
Color of the button text, when the cursor is over it (hexadecimal RRGGBB).
- down_text_color
-
Color of the button text, when it is pressed (hexadecimal RRGGBB).
- center_x
-
Whether the button text should be horizontally centered in its area (the area is defined by the image size, if an image is given, or by the
widthandheightparameters, otherwise). - center_y
-
Whether the button text should be vertically centered in its area (the area is defined by the image size, if an image is given, or by the
widthandheightparameters, otherwise). - margin_x
-
The x offset, from the button x-coordinate, to draw the text. This parameter is used only if
centeris false. - margin_y
-
The y offset, from the button y-coordinate, to draw the text. This parameter is used only if
centeris false. - width
-
Width of the button clickable area. This parameter is used only if
imgisnil. - height
-
Height of the button clickable area. This parameter is used only if
imgisnil. - params
-
An object containing any parameters you want passed to the
actionblock. When the button is clicked, the following is called:@action.call @paramsNote that this doesn’t force you to declare a block that takes parameters.
- action
-
The block of code executed when the button is clicked (or by calling the
clickmethod).
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/minigl/forms.rb', line 96 def initialize(x, y, font, text, img, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action) super x, y, font, text, text_color, disabled_text_color @over_text_color = over_text_color @down_text_color = down_text_color @img = if img; Res.imgs img, 1, 4, true else; nil; end @w = if img; @img[0].width else; width; end @h = if img; @img[0].height else; height; end if center_x; @text_x = x + @w / 2 if @w else; @text_x = x + margin_x; end if center_y; @text_y = y + @h / 2 if @h else; @text_y = y + margin_y; end @center_x = center_x @center_y = center_y @action = action @params = params @state = :up @img_index = @enabled ? 0 : 3 end |
Instance Attribute Details
#state ⇒ Object (readonly)
The current state of the button.
48 49 50 |
# File 'lib/minigl/forms.rb', line 48 def state @state end |
#text ⇒ Object
The text of the button.
51 52 53 |
# File 'lib/minigl/forms.rb', line 51 def text @text end |
Instance Method Details
#click ⇒ Object
Executes the button click action.
174 175 176 |
# File 'lib/minigl/forms.rb', line 174 def click @action.call @params end |
#draw(alpha = 0xff, z_index = 0) ⇒ Object
Draws the button in the screen.
Parameters:
- alpha
-
The opacity with which the button will be drawn. Allowed values vary between 0 (fully transparent) and 255 (fully opaque).
- z_index
-
The z-order to draw the object. Objects with larger z-orders will be drawn on top of the ones with smaller z-orders.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/minigl/forms.rb', line 198 def draw(alpha = 0xff, z_index = 0) return unless @visible color = (alpha << 24) | 0xffffff text_color = if @enabled if @state == :down @down_text_color else @state == :over ? @over_text_color : @text_color end else @disabled_text_color end text_color = (alpha << 24) | text_color @img[@img_index].draw @x, @y, z_index, 1, 1, color if @img if @text if @center_x or @center_y rel_x = @center_x ? 0.5 : 0 rel_y = @center_y ? 0.5 : 0 @font.draw_rel @text, @text_x, @text_y, z_index, rel_x, rel_y, 1, 1, text_color else @font.draw @text, @text_x, @text_y, z_index, 1, 1, text_color end end end |
#enabled=(value) ⇒ Object
:nodoc:
225 226 227 228 229 |
# File 'lib/minigl/forms.rb', line 225 def enabled=(value) # :nodoc: @enabled = value @state = :up @img_index = 3 end |
#set_position(x, y) ⇒ Object
Sets the position of the button in the screen.
Parameters:
- x
-
The new x-coordinate for the button.
- y
-
The new y-coordinate for the button.
183 184 185 186 187 188 189 |
# File 'lib/minigl/forms.rb', line 183 def set_position(x, y) if @center_x; @text_x = x + @w / 2 else; @text_x += x - @x; end if @center_y; @text_y = y + @h / 2 else; @text_y += y - @y; end @x = x; @y = y end |
#update ⇒ Object
Updates the button, checking the mouse movement and buttons to define the button state.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/minigl/forms.rb', line 125 def update return unless @enabled and @visible mouse_over = Mouse.over? @x, @y, @w, @h mouse_press = Mouse. :left mouse_rel = Mouse. :left if @state == :up if mouse_over @img_index = 1 @state = :over else @img_index = 0 end elsif @state == :over if not mouse_over @img_index = 0 @state = :up elsif mouse_press @img_index = 2 @state = :down else @img_index = 1 end elsif @state == :down if not mouse_over @img_index = 0 @state = :down_out elsif mouse_rel @img_index = 1 @state = :over click else @img_index = 2 end else # :down_out if mouse_over @img_index = 2 @state = :down elsif mouse_rel @img_index = 0 @state = :up else @img_index = 0 end end end |