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, #list_box, #menu_item, #progress, #slider, #stack, #theme, #toggle_button

Methods included from Common

#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window

Methods inherited from GameState

#add_game_object, #close, #destroy, #draw_bounding_box, #drop, #gain_focus, #gamepad_connected, #gamepad_disconnected, #lose_focus, #needs_redraw?, #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
34
35
# 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
  CyberarmEngine::Element::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
  @pending_element_recalculate_requests = []

  @needs_repaint = false

  @menu = nil
  @min_drag_distance = 0
  @mouse_pos = Vector.new
end

Instance Method Details

#button_down(id) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 166

def button_down(id)
  super

  case id
  when Gosu::MS_LEFT
    redirect_mouse_button(:left)
  when Gosu::MS_MIDDLE
    redirect_mouse_button(:middle)
  when Gosu::MS_RIGHT
    redirect_mouse_button(:right)
  when Gosu::KB_F5
    request_recalculate
  end

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

#button_up(id) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 183

def button_up(id)
  super

  case id
  when Gosu::MS_LEFT
    redirect_released_mouse_button(:left)
  when Gosu::MS_MIDDLE
    redirect_released_mouse_button(:middle)
  when Gosu::MS_RIGHT
    redirect_released_mouse_button(:right)
  when Gosu::MS_WHEEL_UP
    redirect_mouse_wheel(:up)
  when Gosu::MS_WHEEL_DOWN
    redirect_mouse_wheel(:down)
  when Gosu::KB_HOME
    redirect_scroll_jump_to(:top)
  when Gosu::KB_END
    redirect_scroll_jump_to(:end)
  when Gosu::KB_PAGE_UP
    redirect_scroll_page(:up)
  when Gosu::KB_PAGE_DOWN
    redirect_scroll_page(:down)
  end

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

  # Prevents menu from popping back up if the listbox is clicked to hide it.

  @hid_menu_for = nil
end

#drawObject



56
57
58
59
60
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
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 56

def draw
  # t = Gosu.milliseconds

  # report_recalculate_time = @pending_recalculate_request || @pending_element_recalculate_requests.size.positive?


  StackProf.start(mode: :wall) if RUBY_ENGINE != "mruby" && defined?(StackProf)
  Stats.frame.start_timing(:gui_element_recalculate_requests)

  # puts "PENDING REQUESTS: #{@pending_element_recalculate_requests.size}" if @pending_element_recalculate_requests.size.positive?

  @pending_element_recalculate_requests.shift(&:recalculate)

  Stats.frame.end_timing(:gui_element_recalculate_requests)

  Stats.frame.start_timing(:gui_recalculate)

  while(@pending_recalculate_request)
    @pending_recalculate_request = false

    @root_container.recalculate
  end

  StackProf.stop if RUBY_ENGINE != "mruby" && defined?(StackProf)
  Stats.frame.end_timing(:gui_recalculate)
  # puts "TOOK: #{Gosu.milliseconds - t}ms to recalculate #{self.class}:0x#{object_id.to_s(16)}" if report_recalculate_time && Gosu.milliseconds - t > 0


  super

  if @menu
    Gosu.flush
    @menu.draw
  end

  if @tip && @tip.value.length.positive?
    Gosu.flush

    @tip.draw
  end

  if CyberarmEngine.const_defined?("GUI_DEBUG")
    Gosu.flush

    @root_container.debug_draw
  end

  @needs_repaint = false
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



47
48
49
50
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 47

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

#focusedObject



52
53
54
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 52

def focused
  @focus
end

#hide_menuObject



308
309
310
311
312
313
314
315
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 308

def hide_menu
  return unless @menu

  request_repaint

  @hid_menu_for = @menu.parent
  @menu = nil
end

#inspectObject



322
323
324
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 322

def inspect
  to_s
end


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

def menu
  @menu
end

#needs_repaint?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 102

def needs_repaint?
  @needs_repaint
end

#post_setupObject



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

def post_setup
  @tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
end

#redirect_holding_mouse_button(button) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 256

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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 217

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 && @hid_menu_for != @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



269
270
271
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 269

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

#redirect_released_mouse_button(button) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 236

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

  if @mouse_over && @hid_menu_for != @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

#redirect_scroll_jump_to(edge) ⇒ Object



273
274
275
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 273

def redirect_scroll_jump_to(edge)
  @mouse_over.publish(:"scroll_jump_to_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
end

#redirect_scroll_page(edge) ⇒ Object



277
278
279
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 277

def redirect_scroll_page(edge)
  @mouse_over.publish(:"scroll_page_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
end

#request_focus(element) ⇒ Object



293
294
295
296
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 293

def request_focus(element)
  @pending_focus_request = true
  @pending_focus_element = element
end

#request_recalculateObject

Schedule a full GUI recalculation on next update



282
283
284
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 282

def request_recalculate
  @pending_recalculate_request = true
end

#request_recalculate_for(element) ⇒ Object



286
287
288
289
290
291
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 286

def request_recalculate_for(element)
  # element is already queued

  return if @pending_element_recalculate_requests.detect { |e| e == element }

  @pending_element_recalculate_requests << element
end

#request_repaintObject



298
299
300
301
302
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 298

def request_repaint
  # puts caller[0..4]

  # puts

  @needs_repaint = true
end

#show_menu(list_box) ⇒ Object



304
305
306
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 304

def show_menu(list_box)
  @menu = list_box
end

#to_sObject



317
318
319
320
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 317

def to_s
  # "#{self.class} children=#{@children.map { |c| c.to_s }}"

  @root_container.to_s
end

#tool_tip_delayObject



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

def tool_tip_delay
  @tip.style.delay || 250 # ms

end

#updateObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 106

def update
  if @pending_focus_request
    @pending_focus_request = false

    self.focus = @pending_focus_element
    @pending_focus_element.publish(:focus)
  end

  @menu&.update

  super

  if @active_width != window.width || @active_height != window.height
    request_recalculate
    @root_container.publish(:window_size_changed)
  end

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

  return unless window.has_focus?
  return unless window.current_state == self

  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::MS_LEFT)
  redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MS_MIDDLE)
  redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MS_RIGHT)

  if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
    if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
      @tip.value = @mouse_over.tip if @mouse_over
      @tip.x = window.mouse_x
      @tip.x = 0 if @tip.x < 0
      @tip.x = window.width - @tip.width if @tip.x + @tip.width > window.width
      @tip.y = window.mouse_y - (@tip.height + 5)
      @tip.y = 0 if @tip.y < 0
      @tip.y = window.height - @tip.height if @tip.y + @tip.height > window.height
      @tip.update
      @tip.recalculate
    else
      @tip.value = ""
    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
end