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_line, #flow, #image, #label, #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
# 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

  @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



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

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
end

#button_up(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_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

#drawObject



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

def draw
  super

  if @tip.text.length > 0
    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



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

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

#focusedObject



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

def focused
  @focus
end

#redirect_holding_mouse_button(button) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 162

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
  else
    @mouse_over.publish(:"holding_#{button}_mouse_button", window.mouse_x, window.mouse_y) if @mouse_over
  end
end

#redirect_mouse_button(button) ⇒ Object



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

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



175
176
177
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 175

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 147

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

  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



180
181
182
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 180

def request_recalculate
  @pending_recalculate_request = true
end

#tool_tip_delayObject



94
95
96
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 94

def tool_tip_delay
  500 # ms
end

#updateObject



52
53
54
55
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
# File 'lib/cyberarm_engine/ui/gui_state.rb', line 52

def update
  if @pending_recalculate_request
    @root_container.recalculate
    @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)

  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, @tip.y = window.mouse_x - @tip.width / 2, 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