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/stack.rb,
lib/cyberarm_engine/ui/elements/button.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

Defined Under Namespace

Classes: Button, CheckBox, Container, EditLine, Flow, Image, Label, Progress, 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
# 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

  @style = Style.new(options)

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

  @width  = 0
  @height = 0

  @fixed_x = @x if @x != 0
  @fixed_y = @y if @y != 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

#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



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

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

#button_down(id) ⇒ Object



149
150
# File 'lib/cyberarm_engine/ui/element.rb', line 149

def button_down(id)
end

#button_up(id) ⇒ Object



152
153
# File 'lib/cyberarm_engine/ui/element.rb', line 152

def button_up(id)
end

#default_eventsObject



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

def default_events
  [: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)
end

#drawObject



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

def draw
  return unless @visible

  @style.background_canvas.draw
  @style.border_canvas.draw
  render
end

#enabled?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/cyberarm_engine/ui/element.rb', line 115

def enabled?
  @enabled
end

#heightObject



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

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

#hideObject



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

def hide
  @visible = false
  root.gui_state.request_recalculate
end

#hit?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inner_heightObject



191
192
193
# File 'lib/cyberarm_engine/ui/element.rb', line 191

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

#inner_widthObject



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

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

#is_root?Boolean

Returns:

  • (Boolean)


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

def is_root?
  @gui_state != nil
end

#outer_heightObject



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

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

#outer_widthObject



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

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

#recalculateObject



245
246
247
# File 'lib/cyberarm_engine/ui/element.rb', line 245

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

#renderObject



155
156
# File 'lib/cyberarm_engine/ui/element.rb', line 155

def render
end

#repositionObject



249
250
# File 'lib/cyberarm_engine/ui/element.rb', line 249

def reposition
end

#rootObject



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

def 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



54
55
56
57
# File 'lib/cyberarm_engine/ui/element.rb', line 54

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

#set_border_color(color) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/cyberarm_engine/ui/element.rb', line 68

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



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

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



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

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



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

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

#showObject



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

def show
  @visible = true
  root.gui_state.request_recalculate
end

#stylizeObject



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

def stylize
  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

#toggleObject



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

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

#updateObject



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

def update
end

#update_backgroundObject



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

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



252
253
254
# File 'lib/cyberarm_engine/ui/element.rb', line 252

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

#value=(value) ⇒ Object



256
257
258
# File 'lib/cyberarm_engine/ui/element.rb', line 256

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

#visible?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/cyberarm_engine/ui/element.rb', line 119

def visible?
  @visible
end

#widthObject



163
164
165
166
167
168
169
# File 'lib/cyberarm_engine/ui/element.rb', line 163

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