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/label.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/toggle_button.rb

Direct Known Subclasses

Container, Image, Label, Progress, Radio

Defined Under Namespace

Classes: Button, CheckBox, Container, EditBox, EditLine, Flow, Image, Label, ListBox, Progress, Radio, Slider, Stack, ToggleButton

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, #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
# 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   = false
  @enabled = true
  @visible = true
  @tip = @options[:tip] || ""

  @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)

  stylize

  default_events
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



241
242
243
244
# File 'lib/cyberarm_engine/ui/element.rb', line 241

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

#button_down(id) ⇒ Object



163
164
# File 'lib/cyberarm_engine/ui/element.rb', line 163

def button_down(id)
end

#button_up(id) ⇒ Object



166
167
# File 'lib/cyberarm_engine/ui/element.rb', line 166

def button_up(id)
end

#content_heightObject



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

def content_height
  @height
end

#content_widthObject



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

def content_width
  @width
end

#default_eventsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cyberarm_engine/ui/element.rb', line 104

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(:blur)

  event(:changed)
end

#dimensional_size(size, dimension) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cyberarm_engine/ui/element.rb', line 229

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)


169
170
171
# File 'lib/cyberarm_engine/ui/element.rb', line 169

def draggable?(_button)
  false
end

#drawObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/cyberarm_engine/ui/element.rb', line 149

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)


124
125
126
# File 'lib/cyberarm_engine/ui/element.rb', line 124

def enabled?
  @enabled
end

#heightObject



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

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

#hideObject



143
144
145
146
147
# File 'lib/cyberarm_engine/ui/element.rb', line 143

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

#hit?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/cyberarm_engine/ui/element.rb', line 176

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

#inner_heightObject



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

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

#inner_widthObject



201
202
203
# File 'lib/cyberarm_engine/ui/element.rb', line 201

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

#is_root?Boolean

Returns:

  • (Boolean)


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

def is_root?
  @gui_state != nil
end

#noncontent_heightObject



217
218
219
# File 'lib/cyberarm_engine/ui/element.rb', line 217

def noncontent_height
  (inner_height + outer_height) - height
end

#noncontent_widthObject



193
194
195
# File 'lib/cyberarm_engine/ui/element.rb', line 193

def noncontent_width
  (inner_width + outer_width) - width
end

#outer_heightObject



221
222
223
# File 'lib/cyberarm_engine/ui/element.rb', line 221

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

#outer_widthObject



197
198
199
# File 'lib/cyberarm_engine/ui/element.rb', line 197

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

#recalculateObject



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

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

#renderObject



173
174
# File 'lib/cyberarm_engine/ui/element.rb', line 173

def render
end

#repositionObject



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

def reposition
end

#rootObject



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 258

def root
  return self if is_root?

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

    loop do
      if @root.parent.nil?
        break
      else
        @root = @root.parent
      end
    end
  end

  @root
end

#set_background(background) ⇒ Object



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

def set_background(background)
  @style.background = background
  @style.background_canvas.background = background
end

#set_border_color(color) ⇒ Object



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

def set_border_color(color)
  @style.border_color = color

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

  @style.border_canvas.color = color
end

#set_border_thickness(border_thickness) ⇒ Object



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

def set_border_thickness(border_thickness)
  @style.border_thickness = border_thickness

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

#set_margin(margin) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/cyberarm_engine/ui/element.rb', line 95

def set_margin(margin)
  @style.margin = margin

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

#set_padding(padding) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/cyberarm_engine/ui/element.rb', line 86

def set_padding(padding)
  @style.padding = padding

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

#set_static_positionObject



56
57
58
59
# File 'lib/cyberarm_engine/ui/element.rb', line 56

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

#showObject



137
138
139
140
141
# File 'lib/cyberarm_engine/ui/element.rb', line 137

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

#stylizeObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cyberarm_engine/ui/element.rb', line 44

def stylize
  set_static_position
  set_border_thickness(@style.border_thickness)

  set_padding(@style.padding)

  set_margin(@style.margin)

  set_background(@style.background)
  set_border_color(@style.border_color)
end

#to_sObject



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

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

#toggleObject



132
133
134
135
# File 'lib/cyberarm_engine/ui/element.rb', line 132

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

#updateObject



160
161
# File 'lib/cyberarm_engine/ui/element.rb', line 160

def update
end

#update_backgroundObject



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cyberarm_engine/ui/element.rb', line 246

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

#valueObject



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

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

#value=(_value) ⇒ Object



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

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

#visible?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/cyberarm_engine/ui/element.rb', line 128

def visible?
  @visible
end

#widthObject



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

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