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

#current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #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
# 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[:focus].nil?   ? false : @options[:focus]
  @enabled = @options[:enabled].nil? ? true  : @options[:enabled]
  @visible = @options[:visible].nil? ? 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

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

  @width  = 0
  @height = 0

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

  @style.background_canvas = Background.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

#enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
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



373
374
375
376
# File 'lib/cyberarm_engine/ui/element.rb', line 373

def background=(_background)
  @style.background_canvas.background = _background
  update_background
end

#blur(_sender) ⇒ Object



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

def blur(_sender)
  @focus = false

  if @enabled
    update_styles
  else
    update_styles(:disabled)
  end

  :handled
end

#button_down(id) ⇒ Object



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

def button_down(id)
end

#button_up(id) ⇒ Object



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

def button_up(id)
end

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



187
188
189
190
191
# File 'lib/cyberarm_engine/ui/element.rb', line 187

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

  :handled
end

#content_heightObject



329
330
331
# File 'lib/cyberarm_engine/ui/element.rb', line 329

def content_height
  @height
end

#content_widthObject



305
306
307
# File 'lib/cyberarm_engine/ui/element.rb', line 305

def content_width
  @width
end

#debug_drawObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/cyberarm_engine/ui/element.rb', line 251

def debug_draw
  return if 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



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/element.rb', line 132

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



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/cyberarm_engine/ui/element.rb', line 361

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

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

#draggable?(_button) ⇒ Boolean

Returns:

  • (Boolean)


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

def draggable?(_button)
  false
end

#drawObject



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

def draw
  return unless visible?

  @style.background_canvas.draw
  @style.border_canvas.draw

  Gosu.clip_to(@x, @y, width, height) do
    render
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/cyberarm_engine/ui/element.rb', line 215

def enabled?
  @enabled
end

#enter(_sender) ⇒ Object



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

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

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

  :handled
end

#focus(_) ⇒ Object



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

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

  :handled
end

#heightObject



321
322
323
324
325
326
327
# File 'lib/cyberarm_engine/ui/element.rb', line 321

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

#hideObject



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

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

#hit?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
295
# File 'lib/cyberarm_engine/ui/element.rb', line 292

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

#inner_heightObject



341
342
343
# File 'lib/cyberarm_engine/ui/element.rb', line 341

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

#inner_widthObject



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

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

#inspectObject



435
436
437
# File 'lib/cyberarm_engine/ui/element.rb', line 435

def inspect
  to_s
end

#is_root?Boolean

Returns:

  • (Boolean)


406
407
408
# File 'lib/cyberarm_engine/ui/element.rb', line 406

def is_root?
  @gui_state != nil
end

#leave(_sender) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/cyberarm_engine/ui/element.rb', line 193

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

  :handled
end

#left_mouse_button(_sender, _x, _y) ⇒ Object



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

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



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

def max_scroll_height
  scroll_height - height
end

#max_scroll_widthObject



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

def max_scroll_width
  scroll_width - width
end

#noncontent_heightObject



333
334
335
# File 'lib/cyberarm_engine/ui/element.rb', line 333

def noncontent_height
  (inner_height + outer_height) - height
end

#noncontent_widthObject



309
310
311
# File 'lib/cyberarm_engine/ui/element.rb', line 309

def noncontent_width
  (inner_width + outer_width) - width
end

#outer_heightObject



337
338
339
# File 'lib/cyberarm_engine/ui/element.rb', line 337

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

#outer_widthObject



313
314
315
# File 'lib/cyberarm_engine/ui/element.rb', line 313

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

#recalculateObject



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

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

#released_left_mouse_button(sender, _x, _y) ⇒ Object



181
182
183
184
185
# File 'lib/cyberarm_engine/ui/element.rb', line 181

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

  :handled
end

#renderObject



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

def render
end

#repositionObject



420
421
# File 'lib/cyberarm_engine/ui/element.rb', line 420

def reposition
end

#rootObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/cyberarm_engine/ui/element.rb', line 390

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



62
63
64
# File 'lib/cyberarm_engine/ui/element.rb', line 62

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

#scroll_heightObject



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

def scroll_height
  @children.sum { |c| c.height } + noncontent_height
end

#scroll_widthObject



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

def scroll_width
  @children.sum { |c| c.width } + noncontent_width
end

#set_backgroundObject



71
72
73
74
75
# File 'lib/cyberarm_engine/ui/element.rb', line 71

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

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

#set_border_colorObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cyberarm_engine/ui/element.rb', line 86

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



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

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_marginObject



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

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



102
103
104
105
106
107
108
109
# File 'lib/cyberarm_engine/ui/element.rb', line 102

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



66
67
68
69
# File 'lib/cyberarm_engine/ui/element.rb', line 66

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

#showObject



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

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

#stylizeObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cyberarm_engine/ui/element.rb', line 50

def stylize
  set_static_position

  set_padding
  set_margin

  set_background

  set_border_thickness
  set_border_color
end

#to_sObject



431
432
433
# File 'lib/cyberarm_engine/ui/element.rb', line 431

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

#toggleObject



223
224
225
226
# File 'lib/cyberarm_engine/ui/element.rb', line 223

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

#updateObject



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

def update
end

#update_backgroundObject



378
379
380
381
382
383
384
385
386
387
388
# File 'lib/cyberarm_engine/ui/element.rb', line 378

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

  @style.border_canvas.update
end

#update_styles(event = :default) ⇒ Object



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

def update_styles(event = :default)
  _style = @style.send(event)
  @style_event = event

  if @text.is_a?(CyberarmEngine::Text)
    @text.color = _style&.dig(:color) || @style.default[:color]
    @text.swap_font(_style&.dig(:text_size) || @style.default[:text_size], _style&.dig(:font) || @style.default[:font])
  end

  (root&.gui_state || @gui_state).request_recalculate
end

#valueObject



423
424
425
# File 'lib/cyberarm_engine/ui/element.rb', line 423

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

#value=(_value) ⇒ Object



427
428
429
# File 'lib/cyberarm_engine/ui/element.rb', line 427

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

#visible?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/cyberarm_engine/ui/element.rb', line 219

def visible?
  @visible
end

#widthObject



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

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