Class: Engine::Components::UI::SpriteClickbox

Inherits:
Engine::Component show all
Defined in:
lib/engine/components/ui/sprite_clickbox.rb

Instance Attribute Summary collapse

Attributes inherited from Engine::Component

#game_object

Instance Method Summary collapse

Methods inherited from Engine::Component

#_erase!, #destroy, #destroy!, #destroyed?, destroyed_components, erase_destroyed_components, method_added, #renderer?, #set_game_object, #ui_renderer?

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Attribute Details

#clickedObject (readonly)

Returns the value of attribute clicked.



6
7
8
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 6

def clicked
  @clicked
end

#mouse_enteredObject (readonly)

Returns the value of attribute mouse_entered.



6
7
8
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 6

def mouse_entered
  @mouse_entered
end

#mouse_exitedObject (readonly)

Returns the value of attribute mouse_exited.



6
7
8
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 6

def mouse_exited
  @mouse_exited
end

#mouse_insideObject (readonly)

Returns the value of attribute mouse_inside.



6
7
8
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 6

def mouse_inside
  @mouse_inside
end

Instance Method Details

#awakeObject



8
9
10
11
12
13
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 8

def awake
  @mouse_inside = false
  @clicked = false
  @mouse_entered = false
  @mouse_exited = false
end

#startObject



15
16
17
18
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 15

def start
  @ui_rect = game_object.component(UI::Rect)
  raise "UI::SpriteClickbox requires a UI::Rect component" unless @ui_rect
end

#update(delta_time) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/engine/components/ui/sprite_clickbox.rb', line 20

def update(delta_time)
  mouse_pos = Engine::Input.mouse_pos
  return unless mouse_pos

  if point_inside?(mouse_pos)
    @mouse_entered = !@mouse_inside
    @mouse_inside = true
    @clicked = Engine::Input.key_down?(Engine::Input::MOUSE_BUTTON_LEFT)
  else
    @mouse_exited = @mouse_inside
    @mouse_inside = false
  end
end