Class: CyberarmEngine::Element

Inherits:
Object
  • Object
show all
Includes:
Common, Event, Theme
Defined in:
lib/cyberarm_engine/ui/element.rb,
lib/cyberarm_engine/ui/elements/flow.rb,
lib/cyberarm_engine/ui/elements/image.rb,
lib/cyberarm_engine/ui/elements/radio.rb,
lib/cyberarm_engine/ui/elements/stack.rb,
lib/cyberarm_engine/ui/elements/button.rb,
lib/cyberarm_engine/ui/elements/slider.rb,
lib/cyberarm_engine/ui/elements/edit_box.rb,
lib/cyberarm_engine/ui/elements/list_box.rb,
lib/cyberarm_engine/ui/elements/progress.rb,
lib/cyberarm_engine/ui/elements/check_box.rb,
lib/cyberarm_engine/ui/elements/container.rb,
lib/cyberarm_engine/ui/elements/edit_line.rb,
lib/cyberarm_engine/ui/elements/text_block.rb,
lib/cyberarm_engine/ui/elements/toggle_button.rb

Direct Known Subclasses

Container, Image, Progress, Radio, TextBlock

Defined Under Namespace

Classes: Banner, Button, Caption, CheckBox, Container, EditBox, EditLine, Flow, Image, Inscription, Link, ListBox, Para, Progress, Radio, Slider, Stack, Subtitle, Tagline, TextBlock, Title, ToggleButton, ToolTip

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary collapse

Instance Method Summary collapse

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 included from Event

#event, #publish, #subscribe, #unsubscribe

Methods included from Theme

#deep_merge, #default, #theme_defaults

Constructor Details

#initialize(options = {}, block = nil) ⇒ Element

Returns a new instance of Element.



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cyberarm_engine/ui/element.rb', line 10

def initialize(options = {}, block = nil)
  @parent = options.delete(:parent) # parent Container (i.e. flow/stack)
  options = theme_defaults(options)
  @options = options
  @block = block

  @focus   = !@options.key?(:focus)   ? false : @options[:focus]
  @enabled = !@options.key?(:enabled) ? true  : @options[:enabled]
  @visible = !@options.key?(:visible) ? true  : @options[:visible]
  @tip     = @options[:tip] || ""

  @debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]

  @style = Style.new(options)

  @root ||= nil
  @gui_state ||= nil
  @element_visible = true

  @x = @style.x
  @y = @style.y
  @z = @style.z

  @old_width  = 0
  @old_height = 0
  @width  = 0
  @height = 0

  @style.width  = default(:width)  || nil
  @style.height = default(:height) || nil

  @style.background_canvas = Background.new
  @style.background_nine_slice_canvas = BackgroundNineSlice.new
  @style.background_image_canvas = BackgroundImage.new
  @style.border_canvas = BorderCanvas.new(element: self)

  @style_event = :default

  stylize

  default_events

  root.gui_state.request_focus(self) if @options[:autofocus]
end

Instance Attribute Details

#background_canvasObject (readonly)

Returns the value of attribute background_canvas.



8
9
10
# File 'lib/cyberarm_engine/ui/element.rb', line 8

def background_canvas
  @background_canvas
end

#border_canvasObject (readonly)

Returns the value of attribute border_canvas.



8
9
10
# File 'lib/cyberarm_engine/ui/element.rb', line 8

def border_canvas
  @border_canvas
end

#element_visibleObject

Returns the value of attribute element_visible.



7
8
9
# File 'lib/cyberarm_engine/ui/element.rb', line 7

def element_visible
  @element_visible
end

#event_handlerObject (readonly)

Returns the value of attribute event_handler.



8
9
10
# File 'lib/cyberarm_engine/ui/element.rb', line 8

def event_handler
  @event_handler
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/cyberarm_engine/ui/element.rb', line 8

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/cyberarm_engine/ui/element.rb', line 8

def parent
  @parent
end

#styleObject (readonly)

Returns the value of attribute style.



8
9
10
# File 'lib/cyberarm_engine/ui/element.rb', line 8

def style
  @style
end

#tipObject

Returns the value of attribute tip.



7
8
9
# File 'lib/cyberarm_engine/ui/element.rb', line 7

def tip
  @tip
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/cyberarm_engine/ui/element.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/cyberarm_engine/ui/element.rb', line 7

def y
  @y
end

#zObject

Returns the value of attribute z.



7
8
9
# File 'lib/cyberarm_engine/ui/element.rb', line 7

def z
  @z
end

Instance Method Details

#background=(_background) ⇒ Object



498
499
500
501
502
503
# File 'lib/cyberarm_engine/ui/element.rb', line 498

def background=(_background)
  root.gui_state.request_repaint

  @style.background_canvas.background = _background
  update_background
end

#background_image=(image_path) ⇒ Object



544
545
546
547
548
549
# File 'lib/cyberarm_engine/ui/element.rb', line 544

def background_image=(image_path)
  root.gui_state.request_repaint

  @style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path)
  update_background_image
end

#background_nine_slice=(_image_path) ⇒ Object



518
519
520
521
522
523
# File 'lib/cyberarm_engine/ui/element.rb', line 518

def background_nine_slice=(_image_path)
  root.gui_state.request_repaint

  @style.background_nine_slice_canvas.image = _image_path
  update_background_nine_slice
end

#blur(_sender) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cyberarm_engine/ui/element.rb', line 250

def blur(_sender)
  @focus = false

  if @enabled
    update_styles
  else
    update_styles(:disabled)
  end

  :handled
end

#button_down(id) ⇒ Object



350
351
# File 'lib/cyberarm_engine/ui/element.rb', line 350

def button_down(id)
end

#button_up(id) ⇒ Object



353
354
# File 'lib/cyberarm_engine/ui/element.rb', line 353

def button_up(id)
end

#child_of?(element) ⇒ Boolean

Returns:

  • (Boolean)


593
594
595
596
597
598
599
# File 'lib/cyberarm_engine/ui/element.rb', line 593

def child_of?(element)
  return element == self if is_root?
  return false unless element.is_a?(Container)
  return true if element.children.find { |child| child == self }

  element.children.find { |child| child.child_of?(element) if child.is_a?(Container) }
end

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



234
235
236
237
238
# File 'lib/cyberarm_engine/ui/element.rb', line 234

def clicked_left_mouse_button(_sender, _x, _y)
  @block&.call(self) if @enabled && !self.is_a?(Container)

  :handled
end

#content_heightObject



400
401
402
# File 'lib/cyberarm_engine/ui/element.rb', line 400

def content_height
  @height
end

#content_widthObject



376
377
378
# File 'lib/cyberarm_engine/ui/element.rb', line 376

def content_width
  @width
end

#debug_drawObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/cyberarm_engine/ui/element.rb', line 320

def debug_draw
  # FIXME
  return# if const_defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT

  Gosu.draw_line(
    x, y, @debug_color,
    x + outer_width, y, @debug_color,
    Float::INFINITY
  )
  Gosu.draw_line(
    x + outer_width, y, @debug_color,
    x + outer_width, y + outer_height, @debug_color,
    Float::INFINITY
  )
  Gosu.draw_line(
    x + outer_width, y + outer_height, @debug_color,
    x, y + outer_height, @debug_color,
    Float::INFINITY
  )
  Gosu.draw_line(
    x, outer_height, @debug_color,
    x, y, @debug_color,
    Float::INFINITY
  )
end

#default_eventsObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cyberarm_engine/ui/element.rb', line 179

def default_events
  %i[left middle right].each do |button|
    event(:"#{button}_mouse_button")
    event(:"released_#{button}_mouse_button")
    event(:"clicked_#{button}_mouse_button")
    event(:"holding_#{button}_mouse_button")
  end

  event(:mouse_wheel_up)
  event(:mouse_wheel_down)

  event(:enter)
  event(:hover)
  event(:leave)

  event(:focus)
  event(:blur)

  event(:changed)
end

#dimensional_size(size, dimension) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/cyberarm_engine/ui/element.rb', line 459

def dimensional_size(size, dimension)
  raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)

  new_size = if size.is_a?(Float) && size.between?(0.0, 1.0)
               (@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
             else
               size
             end

  # Handle fill behavior
  if @parent && @style.fill &&
     (dimension == :width && @parent.is_a?(Flow) ||
      dimension == :height && @parent.is_a?(Stack))
    new_size = space_available_width - noncontent_width if dimension == :width && @parent.is_a?(Flow)
    new_size = space_available_height - noncontent_height if dimension == :height && @parent.is_a?(Stack)
  end

  return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}")
  return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}")

  new_size
end

#draggable?(_button) ⇒ Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/cyberarm_engine/ui/element.rb', line 356

def draggable?(_button)
  false
end

#drawObject



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/cyberarm_engine/ui/element.rb', line 308

def draw
  return unless visible?
  return unless element_visible?

  @style.background_canvas.draw
  @style.background_nine_slice_canvas.draw
  @style.background_image_canvas.draw
  @style.border_canvas.draw

  render
end

#element_visible?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/cyberarm_engine/ui/element.rb', line 284

def element_visible?
  @element_visible
end

#enabled=(boolean) ⇒ Object



262
263
264
265
266
267
268
269
270
# File 'lib/cyberarm_engine/ui/element.rb', line 262

def enabled=(boolean)
  root.gui_state.request_repaint if @enabled != boolean

  @enabled = boolean

  recalculate

  @enabled
end

#enabled?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/cyberarm_engine/ui/element.rb', line 272

def enabled?
  @enabled
end

#enter(_sender) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cyberarm_engine/ui/element.rb', line 200

def enter(_sender)
  @focus = false unless Gosu.button_down?(Gosu::MS_LEFT)

  if !@enabled
    update_styles(:disabled)
  elsif @focus
    update_styles(:active)
  else
    update_styles(:hover)
  end

  :handled
end

#focus(_) ⇒ Object



609
610
611
612
613
# File 'lib/cyberarm_engine/ui/element.rb', line 609

def focus(_)
  warn "#{self.class}#focus was not overridden!"

  :handled
end

#focused?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/cyberarm_engine/ui/element.rb', line 276

def focused?
  @focus
end

#heightObject



392
393
394
395
396
397
398
# File 'lib/cyberarm_engine/ui/element.rb', line 392

def height
  if visible?
    inner_height + @height
  else
    0
  end
end

#hideObject



301
302
303
304
305
306
# File 'lib/cyberarm_engine/ui/element.rb', line 301

def hide
  bool = visible?
  @visible = false
  root.gui_state.request_recalculate if bool
  root.gui_state.request_repaint if bool
end

#hit?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


363
364
365
366
# File 'lib/cyberarm_engine/ui/element.rb', line 363

def hit?(x, y)
  x.between?(@x, @x + width) &&
    y.between?(@y, @y + height)
end

#inner_heightObject



412
413
414
# File 'lib/cyberarm_engine/ui/element.rb', line 412

def inner_height
  (@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
end

#inner_widthObject



388
389
390
# File 'lib/cyberarm_engine/ui/element.rb', line 388

def inner_width
  (@style.border_thickness_left + @style.padding_left) + (@style.padding_right + @style.border_thickness_right)
end

#inspectObject



634
635
636
# File 'lib/cyberarm_engine/ui/element.rb', line 634

def inspect
  to_s
end

#is_root?Boolean

Returns:

  • (Boolean)


589
590
591
# File 'lib/cyberarm_engine/ui/element.rb', line 589

def is_root?
  @gui_state != nil
end

#leave(_sender) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/cyberarm_engine/ui/element.rb', line 240

def leave(_sender)
  if @enabled
    update_styles
  else
    update_styles(:disabled)
  end

  :handled
end

#left_mouse_button(_sender, _x, _y) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cyberarm_engine/ui/element.rb', line 214

def left_mouse_button(_sender, _x, _y)
  @focus = true

  unless @enabled
    update_styles(:disabled)
  else
    update_styles(:active)
  end

  window.current_state.focus = self

  :handled
end

#max_scroll_heightObject



455
456
457
# File 'lib/cyberarm_engine/ui/element.rb', line 455

def max_scroll_height
  (scroll_height - outer_height).positive? ? scroll_height - outer_height : scroll_height
end

#max_scroll_widthObject



451
452
453
# File 'lib/cyberarm_engine/ui/element.rb', line 451

def max_scroll_width
  (scroll_width - outer_width).positive? ? scroll_width - outer_width : scroll_width
end

#noncontent_heightObject



404
405
406
# File 'lib/cyberarm_engine/ui/element.rb', line 404

def noncontent_height
  (inner_height + outer_height) - height
end

#noncontent_widthObject



380
381
382
# File 'lib/cyberarm_engine/ui/element.rb', line 380

def noncontent_width
  (inner_width + outer_width) - width
end

#outer_heightObject



408
409
410
# File 'lib/cyberarm_engine/ui/element.rb', line 408

def outer_height
  @style.margin_top + height + @style.margin_bottom
end

#outer_widthObject



384
385
386
# File 'lib/cyberarm_engine/ui/element.rb', line 384

def outer_width
  @style.margin_left + width + @style.margin_right
end

#parent_of?(element) ⇒ Boolean

Returns:

  • (Boolean)


601
602
603
604
605
606
607
# File 'lib/cyberarm_engine/ui/element.rb', line 601

def parent_of?(element)
  return false if element == self
  return false unless is_a?(Container)
  return true if @children.find { |child| child == element }

  @children.find { |child| child.parent_of?(element) if child.is_a?(Container) }
end

#recalculateObject



615
616
617
# File 'lib/cyberarm_engine/ui/element.rb', line 615

def recalculate
  raise "#{self.class}#recalculate was not overridden!"
end

#recalculate_if_size_changedObject



564
565
566
567
568
569
570
571
# File 'lib/cyberarm_engine/ui/element.rb', line 564

def recalculate_if_size_changed
  if !is_a?(ToolTip) && (@old_width != width || @old_height != height)
    root.gui_state.request_recalculate

    @old_width = width
    @old_height = height
  end
end

#released_left_mouse_button(sender, _x, _y) ⇒ Object



228
229
230
231
232
# File 'lib/cyberarm_engine/ui/element.rb', line 228

def released_left_mouse_button(sender, _x, _y)
  enter(sender)

  :handled
end

#renderObject



360
361
# File 'lib/cyberarm_engine/ui/element.rb', line 360

def render
end

#repositionObject



619
620
# File 'lib/cyberarm_engine/ui/element.rb', line 619

def reposition
end

#rootObject



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/cyberarm_engine/ui/element.rb', line 573

def root
  return self if is_root?

  unless @root && @root.parent.nil?
    @root = parent

    loop do
      break unless @root&.parent

      @root = @root.parent
    end
  end

  @root
end

#safe_style_fetch(*args) ⇒ Object



74
75
76
# File 'lib/cyberarm_engine/ui/element.rb', line 74

def safe_style_fetch(*args)
  @style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args)
end

#scroll_heightObject



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/cyberarm_engine/ui/element.rb', line 422

def scroll_height
  return @cached_scroll_height if @cached_scroll_height && is_a?(Container)

  if is_a?(CyberarmEngine::Element::Flow)
    return 0 if @children.size.zero?

    pairs_ = []
    sorted_children_ = @children.sort_by(&:y)
    a_ = []
    y_position_ = sorted_children_.first.y

    sorted_children_.each do |child|
      unless child.y == y_position_
        y_position_ = child.y
        pairs_ << a_
        a_ = []
      end

      a_ << child
    end

    pairs_ << a_ unless pairs_.last == a_

    @cached_scroll_height = pairs_.sum { |pair|  + @style.padding_top + @style.border_thickness_top + pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom
  else
    @cached_scroll_height = @style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom
  end
end

#scroll_widthObject



416
417
418
419
420
# File 'lib/cyberarm_engine/ui/element.rb', line 416

def scroll_width
  return @cached_scroll_width if @cached_scroll_width && is_a?(Container)

  @cached_scroll_width = @children.sum(&:outer_width)
end

#set_backgroundObject



92
93
94
95
96
# File 'lib/cyberarm_engine/ui/element.rb', line 92

def set_background
  @style.background = safe_style_fetch(:background)

  @style.background_canvas.background = @style.background
end

#set_background_imageObject



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

def set_background_image
  @style.background_image = safe_style_fetch(:background_image)
  @style.background_image_mode = safe_style_fetch(:background_image_mode) || :stretch
  @style.background_image_color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE
  @style.background_image_canvas.mode = @style.background_image_mode
  @style.background_image_canvas.color = @style.background_image_color
end

#set_background_nine_sliceObject



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

def set_background_nine_slice
  @style.background_nine_slice = safe_style_fetch(:background_nine_slice)

  @style.background_nine_slice_mode = safe_style_fetch(:background_nine_slice_mode) || :stretch
  @style.background_nine_slice_color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE
  @style.background_nine_slice_canvas.color = @style.background_nine_slice_color

  @style.background_nine_slice_from_edge = safe_style_fetch(:background_nine_slice_from_edge)

  @style.background_nine_slice_left      = safe_style_fetch(:background_nine_slice_left)   || @style.background_nine_slice_from_edge
  @style.background_nine_slice_top       = safe_style_fetch(:background_nine_slice_top)    || @style.background_nine_slice_from_edge
  @style.background_nine_slice_right     = safe_style_fetch(:background_nine_slice_right)  || @style.background_nine_slice_from_edge
  @style.background_nine_slice_bottom    = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge
end

#set_border_colorObject



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

def set_border_color
  @style.border_color = safe_style_fetch(:border_color)

  @style.border_color_left   = safe_style_fetch(:border_color_left)   || @style.border_color
  @style.border_color_right  = safe_style_fetch(:border_color_right)  || @style.border_color
  @style.border_color_top    = safe_style_fetch(:border_color_top)    || @style.border_color
  @style.border_color_bottom = safe_style_fetch(:border_color_bottom) || @style.border_color

  @style.border_canvas.color = [
    @style.border_color_top,
    @style.border_color_right,
    @style.border_color_bottom,
    @style.border_color_left
  ]
end

#set_border_thicknessObject



121
122
123
124
125
126
127
128
# File 'lib/cyberarm_engine/ui/element.rb', line 121

def set_border_thickness
  @style.border_thickness = safe_style_fetch(:border_thickness)

  @style.border_thickness_left   = safe_style_fetch(:border_thickness_left)   || @style.border_thickness
  @style.border_thickness_right  = safe_style_fetch(:border_thickness_right)  || @style.border_thickness
  @style.border_thickness_top    = safe_style_fetch(:border_thickness_top)    || @style.border_thickness
  @style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom) || @style.border_thickness
end

#set_colorObject



83
84
85
86
# File 'lib/cyberarm_engine/ui/element.rb', line 83

def set_color
  @style.color = safe_style_fetch(:color)
  @text&.color = @style.color
end

#set_fontObject



88
89
90
# File 'lib/cyberarm_engine/ui/element.rb', line 88

def set_font
  @text&.swap_font(safe_style_fetch(:text_size), safe_style_fetch(:font))
end

#set_marginObject



155
156
157
158
159
160
161
162
# File 'lib/cyberarm_engine/ui/element.rb', line 155

def set_margin
  @style.margin = safe_style_fetch(:margin)

  @style.margin_left   = safe_style_fetch(:margin_left)   || @style.margin
  @style.margin_right  = safe_style_fetch(:margin_right)  || @style.margin
  @style.margin_top    = safe_style_fetch(:margin_top)    || @style.margin
  @style.margin_bottom = safe_style_fetch(:margin_bottom) || @style.margin
end

#set_paddingObject



146
147
148
149
150
151
152
153
# File 'lib/cyberarm_engine/ui/element.rb', line 146

def set_padding
  @style.padding = safe_style_fetch(:padding)

  @style.padding_left   = safe_style_fetch(:padding_left)   || @style.padding
  @style.padding_right  = safe_style_fetch(:padding_right)  || @style.padding
  @style.padding_top    = safe_style_fetch(:padding_top)    || @style.padding
  @style.padding_bottom = safe_style_fetch(:padding_bottom) || @style.padding
end

#set_static_positionObject



78
79
80
81
# File 'lib/cyberarm_engine/ui/element.rb', line 78

def set_static_position
  @x = @style.x if @style.x != 0
  @y = @style.y if @style.y != 0
end

#showObject



294
295
296
297
298
299
# File 'lib/cyberarm_engine/ui/element.rb', line 294

def show
  bool = visible?
  @visible = true
  root.gui_state.request_recalculate unless bool
  root.gui_state.request_repaint unless bool
end

#space_available_heightObject



490
491
492
493
494
495
496
# File 'lib/cyberarm_engine/ui/element.rb', line 490

def space_available_height
  # TODO: This may get expensive if there are a lot of children, probably should cache it somehow
  fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing

  available_space = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings)
  (available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end

#space_available_widthObject



482
483
484
485
486
487
488
# File 'lib/cyberarm_engine/ui/element.rb', line 482

def space_available_width
  # TODO: This may get expensive if there are a lot of children, probably should cache it somehow
  fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing

  available_space = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings)
  (available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end

#stylizeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cyberarm_engine/ui/element.rb', line 55

def stylize
  set_static_position

  set_color
  set_font

  set_padding
  set_margin

  set_background
  set_background_nine_slice
  set_background_image

  set_border_thickness
  set_border_color

  root.gui_state.request_repaint
end

#to_sObject



630
631
632
# File 'lib/cyberarm_engine/ui/element.rb', line 630

def to_s
  "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{value.is_a?(String) ? "\"#{value}\"" : value}"
end

#toggleObject



288
289
290
291
292
# File 'lib/cyberarm_engine/ui/element.rb', line 288

def toggle
  @visible = !@visible
  root.gui_state.request_recalculate
  root.gui_state.request_repaint
end

#updateObject



346
347
348
# File 'lib/cyberarm_engine/ui/element.rb', line 346

def update
  recalculate_if_size_changed
end

#update_backgroundObject



505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/cyberarm_engine/ui/element.rb', line 505

def update_background
  @style.background_canvas.x = @x
  @style.background_canvas.y = @y
  @style.background_canvas.z = @z
  @style.background_canvas.width  = width
  @style.background_canvas.height = height

  @style.background_canvas.update
  update_background_nine_slice
  update_background_image
  @style.border_canvas.update
end

#update_background_imageObject



551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/cyberarm_engine/ui/element.rb', line 551

def update_background_image
  @style.background_image_canvas.x = @x
  @style.background_image_canvas.y = @y
  @style.background_image_canvas.z = @z
  @style.background_image_canvas.width = width
  @style.background_image_canvas.height = height

  @style.background_image_canvas.mode = @style.background_image_mode
  @style.background_image_canvas.color = @style.background_image_color

  @style.background_image_canvas.image = @style.background_image
end

#update_background_nine_sliceObject



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/cyberarm_engine/ui/element.rb', line 525

def update_background_nine_slice
  @style.background_nine_slice_canvas.x = @x
  @style.background_nine_slice_canvas.y = @y
  @style.background_nine_slice_canvas.z = @z
  @style.background_nine_slice_canvas.width = width
  @style.background_nine_slice_canvas.height = height

  @style.background_nine_slice_canvas.mode = @style.background_nine_slice_mode

  @style.background_nine_slice_canvas.color = @style.background_nine_slice_color

  @style.background_nine_slice_canvas.left   = @style.background_nine_slice_left
  @style.background_nine_slice_canvas.top    = @style.background_nine_slice_top
  @style.background_nine_slice_canvas.right  = @style.background_nine_slice_right
  @style.background_nine_slice_canvas.bottom = @style.background_nine_slice_bottom

  @style.background_nine_slice_canvas.image = @style.background_nine_slice
end

#update_styles(event = :default) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cyberarm_engine/ui/element.rb', line 164

def update_styles(event = :default)
  old_width = width
  old_height = height

  @style_event = event

  return if self.is_a?(ToolTip)

  if old_width != width || old_height != height
    root.gui_state.request_recalculate
  end

  stylize
end

#valueObject



622
623
624
# File 'lib/cyberarm_engine/ui/element.rb', line 622

def value
  raise "#{self.class}#value was not overridden!"
end

#value=(_value) ⇒ Object



626
627
628
# File 'lib/cyberarm_engine/ui/element.rb', line 626

def value=(_value)
  raise "#{self.class}#value= was not overridden!"
end

#visible?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/cyberarm_engine/ui/element.rb', line 280

def visible?
  @visible
end

#widthObject



368
369
370
371
372
373
374
# File 'lib/cyberarm_engine/ui/element.rb', line 368

def width
  if visible?
    inner_width + @width
  else
    0
  end
end