Class: CyberarmEngine::Element::Container
Constant Summary
Constants included
from Theme
Theme::THEME
Instance Attribute Summary collapse
#background_canvas, #border_canvas, #element_visible, #event_handler, #options, #parent, #style, #tip, #x, #y, #z
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add(element) ⇒ Object
-
#append(&block) ⇒ Object
-
#build ⇒ Object
-
#clear(&block) ⇒ Object
-
#debug_draw ⇒ Object
-
#fits_on_line?(element) ⇒ Boolean
-
#hit_element?(x, y) ⇒ Boolean
-
#initialize(options = {}, block = nil) ⇒ Container
constructor
A new instance of Container.
-
#layout ⇒ Object
-
#max_width ⇒ Object
-
#mouse_wheel_down(sender, x, y) ⇒ Object
-
#mouse_wheel_up(sender, x, y) ⇒ Object
-
#move_to_next_line(element) ⇒ Object
-
#position_on_current_line(element) ⇒ Object
-
#position_on_next_line(element) ⇒ Object
-
#recalculate ⇒ Object
-
#remove(element) ⇒ Object
-
#render ⇒ Object
-
#scroll_jump_to_end(sender, x, y) ⇒ Object
-
#scroll_jump_to_top(sender, x, y) ⇒ Object
-
#scroll_page_down(sender, x, y) ⇒ Object
-
#scroll_page_up(sender, x, y) ⇒ Object
-
#scroll_top ⇒ Object
-
#scroll_top=(n) ⇒ Object
-
#tallest_neighbor(querier, _y_position) ⇒ Object
-
#to_s ⇒ Object
-
#update ⇒ Object
-
#update_child_element_visibity(child) ⇒ Object
-
#update_scroll ⇒ Object
-
#value ⇒ Object
-
#write_tree(indent = "", _index = 0) ⇒ Object
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
#background=, #background_image=, #background_nine_slice=, #blur, #button_down, #button_up, #child_of?, #clicked_left_mouse_button, #content_height, #content_width, #default_events, #dimensional_size, #draggable?, #draw, #element_visible?, #enabled=, #enabled?, #enter, #focus, #focused?, #height, #hide, #hit?, #inner_height, #inner_width, #inspect, #is_root?, #leave, #left_mouse_button, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #parent_of?, #recalculate_if_size_changed, #released_left_mouse_button, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_background_image, #set_background_nine_slice, #set_border_color, #set_border_thickness, #set_color, #set_font, #set_margin, #set_padding, #set_static_position, #show, #space_available_height, #space_available_width, #stylize, #toggle, #update_background, #update_background_image, #update_background_nine_slice, #update_styles, #value=, #visible?, #width
#event, #publish, #subscribe, #unsubscribe
Methods included from Theme
#deep_merge, #default, #theme_defaults
Constructor Details
#initialize(options = {}, block = nil) ⇒ Container
Returns a new instance of Container.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 19
def initialize(options = {}, block = nil)
@gui_state = options.delete(:gui_state)
super
@last_scroll_position = Vector.new(0, 0)
@scroll_position = Vector.new(0, 0)
@scroll_target_position = Vector.new(0, 0)
@scroll_chunk = 120
@scroll_speed = 40
@text_color = options[:color]
@children = []
event(:window_size_changed)
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
7
8
9
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def children
@children
end
|
#fill_color ⇒ Object
Returns the value of attribute fill_color.
6
7
8
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 6
def fill_color
@fill_color
end
|
#gui_state ⇒ Object
Returns the value of attribute gui_state.
7
8
9
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def gui_state
@gui_state
end
|
Returns the value of attribute scroll_position.
7
8
9
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def scroll_position
@scroll_position
end
|
Returns the value of attribute scroll_target_position.
7
8
9
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def scroll_target_position
@scroll_target_position
end
|
#stroke_color ⇒ Object
Returns the value of attribute stroke_color.
6
7
8
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 6
def stroke_color
@stroke_color
end
|
Class Method Details
.current_container ⇒ Object
9
10
11
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 9
def self.current_container
@@current_container
end
|
.current_container=(container) ⇒ Object
13
14
15
16
17
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 13
def self.current_container=(container)
raise ArgumentError, "Expected container to an an instance of CyberarmEngine::Element::Container, got #{container.class}" unless container.is_a?(CyberarmEngine::Element::Container)
@@current_container = container
end
|
Instance Method Details
#add(element) ⇒ Object
42
43
44
45
46
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 42
def add(element)
@children << element
root.gui_state.request_recalculate_for(self)
end
|
#build ⇒ Object
36
37
38
39
40
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 36
def build
@block.call(self) if @block
root.gui_state.request_recalculate_for(self)
end
|
#debug_draw ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 89
def debug_draw
super
@children.each do |child|
child.debug_draw
end
end
|
#fits_on_line?(element) ⇒ Boolean
259
260
261
262
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 259
def fits_on_line?(element) @current_position.x + element.outer_width <= max_width &&
@current_position.x + element.outer_width <= window.width
end
|
#hit_element?(x, y) ⇒ Boolean
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 102
def hit_element?(x, y)
return unless hit?(x, y)
child_x = x - @scroll_position.x
child_y = y - @scroll_position.y
@children.reverse_each do |child|
next unless child.visible?
case child
when Container
if element = child.hit_element?(child_x, child_y)
return element
end
else
return child if child.hit?(child_x, child_y)
end
end
self if hit?(x, y)
end
|
#layout ⇒ Object
244
245
246
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 244
def layout
raise "Not overridden"
end
|
#max_width ⇒ Object
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 248
def max_width
outer_width
end
|
#mouse_wheel_down(sender, x, y) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 313
def mouse_wheel_down(sender, x, y)
return unless @style.scroll
return unless height < scroll_height
if @scroll_target_position.y > 0
@scroll_target_position.y = -@scroll_chunk
else
@scroll_target_position.y -= @scroll_chunk
end
return :handled
end
|
#mouse_wheel_up(sender, x, y) ⇒ Object
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 298
def mouse_wheel_up(sender, x, y)
return unless @style.scroll
if height < scroll_height
if @scroll_target_position.y > 0
@scroll_target_position.y = @scroll_chunk
else
@scroll_target_position.y += @scroll_chunk
end
return :handled
end
end
|
#move_to_next_line(element) ⇒ Object
291
292
293
294
295
296
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 291
def move_to_next_line(element) element.x = element.style.margin_left + @current_position.x
element.y = element.style.margin_top + @current_position.y
@current_position.y += element.outer_height
end
|
#position_on_current_line(element) ⇒ Object
264
265
266
267
268
269
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 264
def position_on_current_line(element) element.x = element.style.margin_left + @current_position.x
element.y = element.style.margin_top + @current_position.y
@current_position.x += element.outer_width
end
|
#position_on_next_line(element) ⇒ Object
281
282
283
284
285
286
287
288
289
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 281
def position_on_next_line(element) @current_position.x = @style.margin_left + @style.padding_left
@current_position.y += tallest_neighbor(element, @current_position.y).outer_height
element.x = element.style.margin_left + @current_position.x
element.y = element.style.margin_top + @current_position.y
@current_position.x += element.outer_width
end
|
#recalculate ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 153
def recalculate
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
return unless visible?
Stats.frame&.increment(:gui_recalculations)
stylize
layout
old_width = width
old_height = height
@cached_scroll_width = nil
@cached_scroll_height = nil
if is_root?
@width = @style.width = window.width
@height = @style.height = window.height
else
@width = 0
@height = 0
_width = dimensional_size(@style.width, :width)
_height = dimensional_size(@style.height, :height)
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).floor
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
end
if @style.v_align
space = space_available_height
case @style.v_align
when :center
@y = parent.height / 2 - height / 2
when :bottom
@y = parent.height - height
end
end
if @style.h_align
space = space_available_width
case @style.h_align
when :center
@x = parent.width / 2 - width / 2
when :right
@x = parent.width - width
end
end
@children.each do |child|
child.x += (@x + @style.border_thickness_left) - style.margin_left
child.y += (@y + @style.border_thickness_top) - style.margin_top
child.stylize
child.recalculate
child.reposition
Stats.frame&.increment(:gui_recalculations)
update_child_element_visibity(child)
end
update_background
if old_height != @height
self.scroll_top = -@scroll_position.y
@scroll_target_position.y = @scroll_position.y
end
if scroll_height < height
@scroll_target_position.y = 0
end
recalculate_if_size_changed
end
|
#remove(element) ⇒ Object
48
49
50
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 48
def remove(element)
root.gui_state.request_recalculate_for(self) if @children.delete(element)
end
|
#render ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 76
def render
Gosu.clip_to(
@x + @style.border_thickness_left + @style.padding_left,
@y + @style.border_thickness_top + @style.padding_top,
content_width + 1,
content_height + 1
) do
Gosu.translate(@scroll_position.x, @scroll_position.y) do
@children.each(&:draw)
end
end
end
|
336
337
338
339
340
341
342
343
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 336
def scroll_jump_to_end(sender, x, y)
return unless @style.scroll
@scroll_position.y = -max_scroll_height
@scroll_target_position.y = -max_scroll_height
return :handled
end
|
327
328
329
330
331
332
333
334
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 327
def scroll_jump_to_top(sender, x, y)
return unless @style.scroll
@scroll_position.y = 0
@scroll_target_position.y = 0
return :handled
end
|
#scroll_page_down(sender, x, y) ⇒ Object
355
356
357
358
359
360
361
362
363
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 355
def scroll_page_down(sender, x, y)
return unless @style.scroll
@scroll_position.y -= height
@scroll_position.y = -max_scroll_height if @scroll_position.y < -max_scroll_height
@scroll_target_position.y = @scroll_position.y
return :handled
end
|
#scroll_page_up(sender, x, y) ⇒ Object
345
346
347
348
349
350
351
352
353
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 345
def scroll_page_up(sender, x, y)
return unless @style.scroll
@scroll_position.y += height
@scroll_position.y = 0 if @scroll_position.y > 0
@scroll_target_position.y = @scroll_position.y
return :handled
end
|
365
366
367
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 365
def scroll_top
@scroll_position.y
end
|
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 369
def scroll_top=(n)
n = 0 if n <= 0
@scroll_position.y = -n
if max_scroll_height.positive?
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
else
@scroll_position.y = 0
end
end
|
#tallest_neighbor(querier, _y_position) ⇒ Object
271
272
273
274
275
276
277
278
279
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 271
def tallest_neighbor(querier, _y_position) response = querier
@children.each do |child|
response = child if child.outer_height > response.outer_height
break if child == querier
end
response
end
|
#to_s ⇒ Object
384
385
386
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 384
def to_s
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} children=#{@children.size}"
end
|
#update ⇒ Object
97
98
99
100
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 97
def update
update_scroll if @style.scroll
@children.each(&:update)
end
|
#update_child_element_visibity(child) ⇒ Object
125
126
127
128
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 125
def update_child_element_visibity(child)
child.element_visible = child.x >= (@x - @scroll_position.x) - child.width && child.x <= (@x - @scroll_position.x) + width &&
child.y >= (@y - @scroll_position.y) - child.height && child.y <= (@y - @scroll_position.y) + height
end
|
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 130
def update_scroll
dt = window.dt.clamp(0.000001, 0.025)
@scroll_position.x += (((@scroll_target_position.x - @scroll_position.x) * (@scroll_speed / 4.0) * 0.98) * dt).round
@scroll_position.y += (((@scroll_target_position.y - @scroll_position.y) * (@scroll_speed / 4.0) * 0.98) * dt).round
if @scroll_position.y > 0
@scroll_target_position.y = 0
elsif @scroll_position.y < -max_scroll_height
@scroll_target_position.y = -max_scroll_height
end
if @last_scroll_position != @scroll_position
@children.each { |child| update_child_element_visibity(child) }
root.gui_state.request_repaint
end
@last_scroll_position.x = @scroll_position.x
@last_scroll_position.y = @scroll_position.y
end
|
#value ⇒ Object
380
381
382
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 380
def value
@children.map(&:class).join(", ")
end
|
#write_tree(indent = "", _index = 0) ⇒ Object
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 388
def write_tree(indent = "", _index = 0)
puts self
indent += " "
@children.each_with_index do |child, i|
print "#{indent}#{i}: "
if child.is_a?(Container)
child.write_tree(indent)
else
puts child
end
end
end
|