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

#game_objects, #global_pause, #options

Instance Method Summary collapse

Methods included from DSL

#background, #button, #check_box, #current_theme, #edit_box, #edit_line, #flow, #image, #label, #list_box, #progress, #slider, #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_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
26
27
28
29
30
31
32
33
# 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

  @menu = nil
  @focus = nil
  @mouse_over = nil
  @mouse_down_on = {}
  @mouse_down_position = {}
  @last_mouse_pos = nil
  @dragging_element = nil
  @pending_recalculate_request = false

  @tip = CyberarmEngine::Text.new("", size: 22, z: Float::INFINITY)
  @menu = nil
  @min_drag_distance = 0
  @mouse_pos = Vector.new
end

Instance Method Details

#button_down(id) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 113

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)
  when Gosu::KbF5
    request_recalculate
  end

  @focus.button_down(id) if @focus.respond_to?(:button_down)
end

#button_up(id) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 130

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

  @focus.button_up(id) if @focus.respond_to?(:button_up)
end

#drawObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 46

def draw
  super

  if @menu
    Gosu.flush
    @menu.draw
  end

  if @tip.text.length.positive?
    Gosu.flush
    Gosu.draw_rect(@tip.x - 2, @tip.y - 2, @tip.width + 4, @tip.height + 4, 0xff020202, Float::INFINITY)
    @tip.draw
  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



37
38
39
40
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 37

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

#focusedObject



42
43
44
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 42

def focused
  @focus
end

#hide_menuObject



214
215
216
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 214

def hide_menu
  @menu = nil
end

#redirect_holding_mouse_button(button) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 188

def redirect_holding_mouse_button(button)
  if !@dragging_element && @mouse_down_on[button] && @mouse_down_on[button].draggable?(button) && @mouse_pos.distance(@mouse_down_position[button]) > @min_drag_distance
    @dragging_element = @mouse_down_on[button]
    @dragging_element.publish(:begin_drag, window.mouse_x, window.mouse_y, button)
  end

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

#redirect_mouse_button(button) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 149

def redirect_mouse_button(button)
  hide_menu unless @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)

  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



201
202
203
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 201

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 168

def redirect_released_mouse_button(button)
  hide_menu if @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)

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

  if @dragging_element
    @dragging_element.publish(:end_drag, window.mouse_x, window.mouse_y, button)
    @dragging_element = nil
  end

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

#request_recalculateObject

Schedule a full GUI recalculation on next update



206
207
208
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 206

def request_recalculate
  @pending_recalculate_request = true
end

#show_menu(list_box) ⇒ Object



210
211
212
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 210

def show_menu(list_box)
  @menu = list_box
end

#tool_tip_delayObject



109
110
111
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 109

def tool_tip_delay
  500 # ms
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 61

def update
  if @pending_recalculate_request
    @root_container.recalculate
    @root_container.recalculate
    @root_container.recalculate

    @pending_recalculate_request = false
  end

  @menu&.update
  super

  new_mouse_over = @menu.hit_element?(window.mouse_x, window.mouse_y) if @menu
  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)

  if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
    if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
      @tip.text = @mouse_over.tip if @mouse_over
      @tip.x = window.mouse_x - @tip.width / 2
      @tip.y = window.mouse_y - @tip.height - 4
    else
      @tip.text = ""
    end
  else
    @mouse_moved_at = Gosu.milliseconds
  end

  @last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
  @mouse_pos = @last_mouse_pos.clone

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

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