Method: MiniGL::Button#update

Defined in:
lib/minigl/forms.rb

#updateObject

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/minigl/forms.rb', line 147

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
      @img_index = 2
      @state = :down
    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
      click
    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