Class: MiniGL::DropDownList
Overview
This class represents a “drop-down list” form component, here composed of a group of Button objects.
Instance Attribute Summary collapse
-
#options ⇒ Object
An array containing all the options (each of them Strings) that can be selected in the drop-down list.
-
#value ⇒ Object
The selected value in the drop-down list.
Attributes inherited from Component
#anchor, #anchor_offset_x, #anchor_offset_y, #enabled, #h, #params, #visible, #w, #x, #y
Instance Method Summary collapse
-
#draw(alpha = 0xff, z_index = 0, color = 0xffffff, over_color = 0xcccccc) ⇒ Object
Draws the drop-down list.
-
#enabled=(value) ⇒ Object
:nodoc:.
-
#initialize(x, y = nil, font = nil, img = nil, opt_img = nil, options = nil, option = 0, text_margin = 0, width = nil, height = nil, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &on_changed) ⇒ DropDownList
constructor
Creates a new drop-down list.
- #set_position(x, y) ⇒ Object
-
#update ⇒ Object
Updates the control.
Constructor Details
#initialize(x, y = nil, font = nil, img = nil, opt_img = nil, options = nil, option = 0, text_margin = 0, width = nil, height = nil, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &on_changed) ⇒ DropDownList
Creates a new drop-down list.
Parameters:
- x
-
The x-coordinate of the object.
- y
-
The y-coordinate of the object.
- font
-
Font to be used by the buttons that compose the drop-donwn list.
- img
-
Image of the main button, i.e., the one at the top, that toggles visibility of the other buttons (the “option” buttons).
- opt_img
-
Image for the “option” buttons, as described above.
- options
-
Array of available options for this control (+String+s).
- option
-
Index of the firstly selected option.
- text_margin
-
Left margin of the text inside the buttons (vertically, the text will always be centered).
- width
-
Width of the control, used when no image is provided.
- height
-
Height of the control, used when no image is provided.
- text_color
-
Used as the
text_colorparameter in the constructor of the buttons. - disabled_text_color
-
Analogous to
text_color. - over_text_color
-
Same as above.
- down_text_color
-
Same as above.
- retro
-
Whether the images should be loaded with the ‘retro’ option set (see
Gosu::Imagefor details). If the value is omitted, theRes.retro_imagesvalue will be used. - scale_x
-
Horizontal scale to draw the component with.
- scale_y
-
Vertical scale to draw the component with.
- anchor
-
See parameter with the same name in
Panel#initializefor details. - on_changed
-
Action performed when the value of the dropdown is changed. It must be a block with two parameters, which will receive the old and the new value, respectively.
Obs.: This method accepts named parameters, but x, y, font and options are mandatory (also, img and opt_img are mandatory when width and height are not provided, and vice-versa).
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 |
# File 'lib/minigl/forms.rb', line 1272 def initialize(x, y = nil, font = nil, img = nil, opt_img = nil, = nil, option = 0, text_margin = 0, width = nil, height = nil, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &on_changed) if x.is_a? Hash y = x[:y] font = x[:font] img = x[:img] opt_img = x[:opt_img] = x[:options] option = x.fetch(:option, 0) text_margin = x.fetch(:text_margin, 0) width = x.fetch(:width, nil) height = x.fetch(:height, nil) 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) retro = x.fetch(:retro, nil) scale_x = x.fetch(:scale_x, 1) scale_y = x.fetch(:scale_y, 1) anchor = x.fetch(:anchor, nil) x = x[:x] end @img = img @opt_img = opt_img = @value = [option] @open = false = [] .push( Button.new(x, y, font, @value, img, text_color, disabled_text_color, over_text_color, down_text_color, false, true, text_margin, 0, width, height, nil, retro, scale_x, scale_y) { toggle } ) @scale_x = scale_x @scale_y = scale_y @w = [0].w @h = [0].h @max_h = (.size + 1) * @h @anchor_offset_x = x; @anchor_offset_y = y @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h) super x, y, font, [option], text_color, disabled_text_color [0].set_position(x, y) .each_with_index do |o, i| b = Button.new(x, y + (i+1) * @h, font, o, opt_img, text_color, disabled_text_color, over_text_color, down_text_color, false, true, text_margin, 0, width, height, nil, retro, scale_x, scale_y) { old = @value @value = [0].text = o @on_changed.call(old, o) if @on_changed toggle } b.visible = false .push b end @on_changed = on_changed end |
Instance Attribute Details
#options ⇒ Object
An array containing all the options (each of them Strings) that can be selected in the drop-down list.
1237 1238 1239 |
# File 'lib/minigl/forms.rb', line 1237 def end |
#value ⇒ Object
The selected value in the drop-down list. This is one of the options.
1233 1234 1235 |
# File 'lib/minigl/forms.rb', line 1233 def value @value end |
Instance Method Details
#draw(alpha = 0xff, z_index = 0, color = 0xffffff, over_color = 0xcccccc) ⇒ Object
Draws the drop-down list.
Parameters:
- alpha
-
(
Fixnum) The opacity with which the drop-down list will be drawn. Allowed values vary between 0 (fully transparent) and 255 (fully opaque). - z_index
-
(
Fixnum) The z-order to draw the object. Objects with larger z-orders will be drawn on top of the ones with smaller z-orders. - color
-
Color of the buttons, if no image was provided, or color to apply a filter to the images.
- over_color
-
Color of the buttons when the mouse is over them (when no image was provided).
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 |
# File 'lib/minigl/forms.rb', line 1378 def draw(alpha = 0xff, z_index = 0, color = 0xffffff, over_color = 0xcccccc) return unless @visible unless @img bottom = @y + (@open ? @max_h : @h) + @scale_y b_color = (alpha << 24) G.window.draw_quad @x - @scale_x, @y - @scale_y, b_color, @x + @w + @scale_x, @y - @scale_y, b_color, @x + @w + @scale_x, bottom, b_color, @x - @scale_x, bottom, b_color, z_index .each do |b| c = (alpha << 24) | (b.state == :over ? over_color : color) G.window.draw_quad b.x, b.y, c, b.x + b.w, b.y, c, b.x + b.w, b.y + b.h, c, b.x, b.y + b.h, c, z_index if b.visible end end .each { |b| b.draw alpha, z_index, color } end |
#enabled=(value) ⇒ Object
:nodoc:
1355 1356 1357 1358 1359 |
# File 'lib/minigl/forms.rb', line 1355 def enabled=(value) # :nodoc: toggle if @open [0].enabled = value @enabled = value end |
#set_position(x, y) ⇒ Object
1361 1362 1363 1364 |
# File 'lib/minigl/forms.rb', line 1361 def set_position(x, y) @x = x; @y = y .each_with_index { |b, i| b.set_position(x, y + i * @h) } end |
#update ⇒ Object
Updates the control.
1336 1337 1338 1339 1340 1341 1342 1343 |
# File 'lib/minigl/forms.rb', line 1336 def update return unless @enabled and @visible if @open and Mouse. :left and not Mouse.over?(@x, @y, @w, @max_h) toggle return end .each { |b| b.update } end |