Class: AGL::Button

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

Instance Method Summary collapse

Constructor Details

#initialize(x, y, font, text, img, center = true, margin_x = 0, margin_y = 0, &action) ⇒ Button



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/minigl/forms.rb', line 5

def initialize x, y, font, text, img, center = true, margin_x = 0, margin_y = 0, &action
  @x = x
  @y = y
  @font = font
  @text = text
  @img = Res.imgs img, 1, 3, true
  @w = @img[0].width
  @h = @img[0].height
  if center
    @text_x = x + @w / 2
    @text_y = y + @h / 2
  else
    @text_x = x + margin_x
    @text_y = y + margin_y
  end
  @center = center
  @action = Proc.new &action

  @state = :up
  @img_index = 0
end

Instance Method Details

#draw(text_color = 0, alpha = 0xff) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/minigl/forms.rb', line 65

def draw text_color = 0, alpha = 0xff
  color = (alpha << 24) | 0xffffff
  text_color = (alpha << 24) | text_color
  @img[@img_index].draw @x, @y, 0, 1, 1, color
  if @center
    @font.draw_rel @text, @text_x, @text_y, 0, 0.5, 0.5, 1, 1, text_color
  else
    @font.draw @text, @text_x, @text_y, 0, 1, 1, text_color
  end
end

#updateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/minigl/forms.rb', line 27

def update
  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
    end
  elsif @state == :over
    if not mouse_over
      @img_index = 0
      @state = :up
    elsif mouse_press
      @img_index = 2
      @state = :down
    end
  elsif @state == :down
    if not mouse_over
      @img_index = 0
      @state = :down_out
    elsif mouse_rel
      @img_index = 0
      @state = :up
      @action.call
    end
  elsif @state == :down_out
    if mouse_over
      @img_index = 2
      @state = :down
    elsif mouse_rel
      @img_index = 0
      @state = :up
    end
  end
end