Class: MiniGL::Button

Inherits:
Component show all
Defined in:
lib/minigl/forms.rb

Overview

This class represents a button.

Direct Known Subclasses

ToggleButton

Instance Attribute Summary collapse

Attributes inherited from Component

#anchor, #anchor_offset_x, #anchor_offset_y, #enabled, #h, #panel, #params, #visible, #w, #x, #y

Instance Method Summary collapse

Constructor Details

#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, retro = nil, scale_x = 1, scale_y = 1, anchor = 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::Font object that will be used to draw the button text.

text

The button text. Can be nil or 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, the width and height parameters 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 width and height parameters, 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 width and height parameters, otherwise).

margin_x

The x offset, from the button x-coordinate, to draw the text. This parameter is used only if center is false.

margin_y

The y offset, from the button y-coordinate, to draw the text. This parameter is used only if center is false.

width

Width of the button clickable area. This parameter is used only if img is nil.

height

Height of the button clickable area. This parameter is used only if img is nil.

params

An object containing any parameters you want passed to the action block. When the button is clicked, the following is called:

@action.call @params

Note that this doesn’t force you to declare a block that takes parameters.

retro

Whether the image should be loaded with the ‘retro’ option set (see Gosu::Image for details). If the value is omitted, the Res.retro_images value 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#initialize for details.

action

The block of code executed when the button is clicked (or by calling the click method).

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



274
275
276
277
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/minigl/forms.rb', line 274

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, retro = nil, scale_x = 1, scale_y = 1, anchor = 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)
    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

  retro = Res.retro_images if retro.nil?
  @scale_x = scale_x
  @scale_y = scale_y
  @img =
      if img; Res.imgs img, 1, 4, true, '.png', retro
      else; nil; end
  @w =
      if img; @img[0].width * @scale_x
      else; width * @scale_x; end
  @h =
      if img; @img[0].height * @scale_y
      else; height * @scale_y; end

  @anchor_offset_x = x; @anchor_offset_y = y
  @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h)

  super x, y, font, text, text_color, disabled_text_color
  @over_text_color = over_text_color
  @down_text_color = down_text_color
  @center_x = center_x
  @center_y = center_y
  @margin_x = margin_x
  @margin_y = margin_y
  set_position(x, y)

  @action = action
  @params = params

  @state = :up
  @img_index = @enabled ? 0 : 3
end

Instance Attribute Details

#stateObject (readonly)

The current state of the button.



216
217
218
# File 'lib/minigl/forms.rb', line 216

def state
  @state
end

#textObject

The text of the button.



219
220
221
# File 'lib/minigl/forms.rb', line 219

def text
  @text
end

Instance Method Details

#clickObject

Executes the button click action.



386
387
388
# File 'lib/minigl/forms.rb', line 386

def click
  perform_action
end

#draw(alpha = 0xff, z_index = 0, color = 0xffffff) ⇒ 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.

color

Color to apply a filter to the image.



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/minigl/forms.rb', line 411

def draw(alpha = 0xff, z_index = 0, color = 0xffffff)
  @z_index = z_index
  return unless @visible

  color = (alpha << 24) | color
  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, @scale_x, @scale_y, 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_text_rel @text, @text_x, @text_y, z_index, rel_x, rel_y, @scale_x, @scale_y, text_color
    else
      @font.draw_text @text, @text_x, @text_y, z_index, @scale_x, @scale_y, text_color
    end
  end
end

#enabled=(value) ⇒ Object

:nodoc:



439
440
441
442
443
# File 'lib/minigl/forms.rb', line 439

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.



395
396
397
398
399
400
401
# File 'lib/minigl/forms.rb', line 395

def set_position(x, y)
  @text_x = @center_x ? x + @w / 2 : x
  @text_y = @center_y ? y + @h / 2 : y
  @text_x += @margin_x
  @text_y += @margin_y
  @x = x; @y = y
end

#updateObject

Updates the button, checking the mouse movement and buttons to define the button state.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/minigl/forms.rb', line 335

def update
  return unless @enabled and @visible

  mouse_over = Mouse.over? @x, @y, @w, @h
  mouse_press = Mouse.button_pressed? :left
  mouse_rel = Mouse.button_released? :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
      Mouse.add_click(@z_index || 0, lambda do
        @img_index = 2
        @state = :down
      end)
    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
      enqueue_action
    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