Class: CyberarmEngine::GuiState

Inherits:
GameState show all
Includes:
Common, DSL
Defined in:
lib/cyberarm_engine/ui/gui_state.rb

Instance Attribute Summary

Attributes inherited from GameState

#containers, #game_objects, #global_pause, #options

Instance Method Summary collapse

Methods included from DSL

#background, #button, #check_box, #current_theme, #edit_line, #flow, #image, #label, #progress, #stack, #theme, #toggle_button

Methods included from Common

#current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #show_cursor, #show_cursor=, #window

Methods inherited from GameState

#add_game_object, #destroy, #draw, #draw_bounding_box, #setup

Constructor Details

#initialize(options = {}) ⇒ GuiState

Returns a new instance of GuiState.



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

def initialize(options = {})
  @options = options
  @game_objects = []
  @global_pause = false

  @down_keys = {}

  @root_container = Element::Stack.new(gui_state: self)
  @game_objects << @root_container
  $__current_container__ = @root_container

  @active_width  = window.width
  @active_height = window.height

  @focus = nil
  @mouse_over = nil
  @mouse_down_on = {}
  @mouse_down_position = {}
  @pending_recalculate_request = false
end

Instance Method Details

#button_down(id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 65

def button_down(id)
  super

  case id
  when Gosu::MsLeft
    redirect_mouse_button(:left)
  when Gosu::MsMiddle
    redirect_mouse_button(:middle)
  when Gosu::MsRight
    redirect_mouse_button(:right)
  end
end

#button_up(id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 78

def button_up(id)
  super

  case id
  when Gosu::MsLeft
    redirect_released_mouse_button(:left)
  when Gosu::MsMiddle
    redirect_released_mouse_button(:middle)
  when Gosu::MsRight
    redirect_released_mouse_button(:right)
  when Gosu::MsWheelUp
    redirect_mouse_wheel(:up)
  when Gosu::MsWheelDown
    redirect_mouse_wheel(:down)
  end
end

#focus=(element) ⇒ Object

throws :blur event to focused element and sets GuiState focused element Does NOT throw :focus event at element or set element as focused



29
30
31
32
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 29

def focus=(element)
  @focus.publish(:blur) if @focus and element && @focus != element
  @focus = element
end

#focusedObject



34
35
36
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 34

def focused
  @focus
end

#redirect_holding_mouse_button(button) ⇒ Object



122
123
124
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 122

def redirect_holding_mouse_button(button)
  @mouse_over.publish(:"holding_#{button}_mouse_button", window.mouse_x, window.mouse_y) if @mouse_over
end

#redirect_mouse_button(button) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 95

def redirect_mouse_button(button)
  if @focus && @mouse_over != @focus
    @focus.publish(:blur)
    @focus = nil
  end

  if @mouse_over
    @mouse_down_position[button] = Vector.new(window.mouse_x, window.mouse_y)
    @mouse_down_on[button]       = @mouse_over

    @mouse_over.publish(:"#{button}_mouse_button", window.mouse_x, window.mouse_y)
  else
    @mouse_down_position[button] = nil
    @mouse_down_on[button]       = nil
  end
end

#redirect_mouse_wheel(button) ⇒ Object



126
127
128
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 126

def redirect_mouse_wheel(button)
  @mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over
end

#redirect_released_mouse_button(button) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 112

def redirect_released_mouse_button(button)
  if @mouse_over
    @mouse_over.publish(:"released_#{button}_mouse_button", window.mouse_x, window.mouse_y)
    @mouse_over.publish(:"clicked_#{button}_mouse_button", window.mouse_x, window.mouse_y) if @mouse_over == @mouse_down_on[button]
  end

  @mouse_down_position[button] = nil
  @mouse_down_on[button]       = nil
end

#request_recalculateObject

Schedule a full GUI recalculation on next update



131
132
133
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 131

def request_recalculate
  @pending_recalculate_request = true
end

#updateObject



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/cyberarm_engine/ui/gui_state.rb', line 38

def update
  if @pending_recalculate_request
    @root_container.recalculate
    @pending_recalculate_request = false
  end

  super

  new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y)
  if new_mouse_over
    new_mouse_over.publish(:enter) if new_mouse_over != @mouse_over
    new_mouse_over.publish(:hover)
    # puts "#{new_mouse_over.class}[#{new_mouse_over.value}]: #{new_mouse_over.x}:#{new_mouse_over.y} #{new_mouse_over.width}:#{new_mouse_over.height}" if new_mouse_over != @mouse_over

  end
  @mouse_over.publish(:leave) if @mouse_over && new_mouse_over != @mouse_over
  @mouse_over = new_mouse_over

  redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
  redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
  redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)

  request_recalculate if @active_width != window.width || @active_height != window.height

  @active_width  = window.width
  @active_height = window.height
end