Method: MiniGL::Button#initialize
- Defined in:
- lib/minigl/forms.rb
#initialize(x, y = nil, font = nil, text = nil, img = nil, 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).
Obs.: This method accepts named parameters, but x and y are mandatory (also, img is mandatory when width and height are not provided, and vice-versa).
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/minigl/forms.rb', line 98 def initialize(x, y = nil, font = nil, text = nil, img = nil, 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) if x.is_a? Hash y = x[:y] font = x[:font] text = x[:text] img = x[:img] text_color = x.fetch(:text_color, 0) disabled_text_color = x.fetch(:disabled_text_color, 0) over_text_color = x.fetch(:over_text_color, 0) down_text_color = x.fetch(:down_text_color, 0) center_x = x.fetch(:center_x, true) center_y = x.fetch(:center_y, true) margin_x = x.fetch(:margin_x, 0) margin_y = x.fetch(:margin_y, 0) width = x.fetch(:width, nil) height = x.fetch(:height, nil) params = x.fetch(:params, nil) x = x[:x] end 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 |