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.



332
333
334
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
# File 'lib/minigl/forms.rb', line 332

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