Method: MiniGL::ToggleButton#initialize

Defined in:
lib/minigl/forms.rb

#initialize(x, y = nil, font = nil, text = nil, img = nil, checked = false, 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) ⇒ ToggleButton

Creates a ToggleButton. All parameters work the same as in Button, except for the image, img, which now has to be composed of two columns and four rows, the first column with images for the unchecked state, and the second with images for the checked state, and for checked, which defines the initial state of the ToggleButton.

The action block now will always receive a first boolean parameter corresponding to the value of checked. So, if you want to pass parameters to the block, you should declare it like this:

b = ToggleButton.new ... { |checked, params|
  puts "button was checked" if checked
  # do something with params
}

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).



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/minigl/forms.rb', line 278

def initialize(x, y = nil, font = nil, text = nil, img = nil, checked = false,
               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]
    checked = x.fetch(:checked, false)
    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, nil, text_color, disabled_text_color, over_text_color, down_text_color,
        center_x, center_y, margin_x, margin_y, width, height, params, &action
  @img =
    if img; Res.imgs img, 2, 4, true
    else; nil; end
  @w =
    if img; @img[0].width
    else; width; end
  @h =
    if img; @img[0].height
    else; height; end
  @text_x = x + @w / 2 if center_x
  @text_y = y + @h / 2 if center_y
  @checked = checked
end