cyberarm_engine 0.16.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -4
  3. data/assets/textures/logo.png +0 -0
  4. data/cyberarm_engine.gemspec +2 -2
  5. data/lib/cyberarm_engine/animator.rb +172 -9
  6. data/lib/cyberarm_engine/background_nine_slice.rb +51 -34
  7. data/lib/cyberarm_engine/builtin/intro_state.rb +131 -0
  8. data/lib/cyberarm_engine/common.rb +19 -2
  9. data/lib/cyberarm_engine/console/command.rb +158 -0
  10. data/lib/cyberarm_engine/console/commands/help_command.rb +43 -0
  11. data/lib/cyberarm_engine/console/subcommand.rb +100 -0
  12. data/lib/cyberarm_engine/console.rb +248 -0
  13. data/lib/cyberarm_engine/game_state.rb +5 -0
  14. data/lib/cyberarm_engine/model.rb +6 -1
  15. data/lib/cyberarm_engine/opengl/perspective_camera.rb +2 -2
  16. data/lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb +9 -0
  17. data/lib/cyberarm_engine/opengl/texture.rb +1 -1
  18. data/lib/cyberarm_engine/text.rb +82 -45
  19. data/lib/cyberarm_engine/ui/dsl.rb +3 -2
  20. data/lib/cyberarm_engine/ui/element.rb +236 -47
  21. data/lib/cyberarm_engine/ui/elements/button.rb +0 -63
  22. data/lib/cyberarm_engine/ui/elements/check_box.rb +4 -1
  23. data/lib/cyberarm_engine/ui/elements/container.rb +65 -35
  24. data/lib/cyberarm_engine/ui/elements/edit_line.rb +20 -13
  25. data/lib/cyberarm_engine/ui/elements/text_block.rb +13 -7
  26. data/lib/cyberarm_engine/ui/event.rb +9 -2
  27. data/lib/cyberarm_engine/ui/gui_state.rb +37 -4
  28. data/lib/cyberarm_engine/ui/style.rb +14 -3
  29. data/lib/cyberarm_engine/ui/theme.rb +26 -1
  30. data/lib/cyberarm_engine/version.rb +1 -1
  31. data/lib/cyberarm_engine/window.rb +7 -1
  32. data/lib/cyberarm_engine.rb +11 -4
  33. metadata +12 -7
  34. data/lib/cyberarm_engine/ui/elements/label.rb +0 -156
@@ -3,7 +3,9 @@ module CyberarmEngine
3
3
  CACHE = {}
4
4
 
5
5
  attr_accessor :x, :y, :z, :size, :options
6
- attr_reader :text, :textobject, :factor_x, :factor_y, :color, :shadow, :shadow_size, :shadow_alpha, :shadow_color
6
+ attr_reader :text, :textobject, :factor_x, :factor_y, :color,
7
+ :border, :border_size, :border_alpha, :border_color,
8
+ :shadow, :shadow_size, :shadow_alpha, :shadow_color
7
9
 
8
10
  def initialize(text, options = {})
9
11
  @text = text.to_s || ""
@@ -15,16 +17,25 @@ module CyberarmEngine
15
17
  @z = options[:z] || 1025
16
18
  @factor_x = options[:factor_x] || 1
17
19
  @factor_y = options[:factor_y] || 1
18
- @color = options[:color] || Gosu::Color::WHITE
20
+ if options[:color]
21
+ @color = options[:color].is_a?(Gosu::Color) ? options[:color] : Gosu::Color.new(options[:color])
22
+ else
23
+ @color = Gosu::Color::WHITE
24
+ end
19
25
  @mode = options[:mode] || :default
20
26
  @alignment = options[:alignment] || nil
21
- @shadow = true if options[:shadow] == true
22
- @shadow = false if options[:shadow] == false
23
- @shadow = true if options[:shadow].nil?
24
- @shadow_size = options[:shadow_size] || 1
25
- @shadow_alpha = options[:shadow_alpha] || 30
27
+
28
+ @border = options[:border]
29
+ @border = true if options[:border].nil?
30
+ @border_size = options[:border_size] || 1
31
+ @border_alpha = options[:border_alpha] || 30
32
+ @border_color = options[:border_color]
33
+
34
+ @shadow = options[:shadow]
35
+ @shadow_size = options[:shadow_size] || 2
26
36
  @shadow_alpha = options[:shadow_alpha] || 30
27
37
  @shadow_color = options[:shadow_color]
38
+
28
39
  @textobject = check_cache(@size, @font)
29
40
 
30
41
  if @alignment
@@ -37,8 +48,6 @@ module CyberarmEngine
37
48
  @x = $window.width - BUTTON_PADDING - @textobject.text_width(@text)
38
49
  end
39
50
  end
40
-
41
- self
42
51
  end
43
52
 
44
53
  def check_cache(size, font_name)
@@ -65,82 +74,110 @@ module CyberarmEngine
65
74
  font
66
75
  end
67
76
 
77
+ def swap_font(size, font_name = @font)
78
+ if @size != size || @font != font_name
79
+ @size = size
80
+ @font = font_name
81
+
82
+ @textobject = check_cache(size, font_name)
83
+ end
84
+ end
85
+
68
86
  def text=(string)
69
- @rendered_shadow = nil
87
+ @rendered_border = nil
70
88
  @text = string
71
89
  end
72
90
 
73
91
  def factor_x=(n)
74
- @rendered_shadow = nil
92
+ @rendered_border = nil
75
93
  @factor_x = n
76
94
  end
77
95
 
78
96
  def factor_y=(n)
79
- @rendered_shadow = nil
97
+ @rendered_border = nil
80
98
  @factor_y = n
81
99
  end
82
100
 
83
101
  def color=(color)
84
- @rendered_shadow = nil
85
- @color = color
102
+ @rendered_border = nil
103
+ if color
104
+ @color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color)
105
+ else
106
+ raise "color cannot be nil"
107
+ end
86
108
  end
87
109
 
88
- def shadow=(boolean)
89
- @rendered_shadow = nil
90
- @shadow = boolean
110
+ def border=(boolean)
111
+ @rendered_border = nil
112
+ @border = boolean
91
113
  end
92
114
 
93
- def shadow_size=(n)
94
- @rendered_shadow = nil
95
- @shadow_size = n
115
+ def border_size=(n)
116
+ @rendered_border = nil
117
+ @border_size = n
96
118
  end
97
119
 
98
- def shadow_alpha=(n)
99
- @rendered_shadow = nil
100
- @shadow_alpha = n
120
+ def border_alpha=(n)
121
+ @rendered_border = nil
122
+ @border_alpha = n
101
123
  end
102
124
 
103
- def shadow_color=(n)
104
- @rendered_shadow = nil
105
- @shadow_color = n
125
+ def border_color=(n)
126
+ @rendered_border = nil
127
+ @border_color = n
106
128
  end
107
129
 
108
130
  def width(text = @text)
109
- textobject.text_width(text)
131
+ markup_width(text)
132
+ end
133
+
134
+ def text_width(text = @text)
135
+ textobject.text_width(text) + @border_size + @shadow_size
110
136
  end
111
137
 
112
138
  def markup_width(text = @text)
113
- textobject.markup_width(text)
139
+ textobject.markup_width(text) + @border_size + @shadow_size
114
140
  end
115
141
 
116
142
  def height(text = @text)
117
- text.lines.count > 0 ? text.lines.count * textobject.height : @textobject.height
143
+ if text.lines.count > 0
144
+ text.lines.count * textobject.height + @border_size + @shadow_size
145
+ else
146
+ @textobject.height + @border_size + @shadow_size
147
+ end
118
148
  end
119
149
 
120
150
  def draw(method = :draw_markup)
121
- if @shadow && !ARGV.join.include?("--no-shadow")
122
- shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
123
- shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue,
124
- shadow_alpha)
151
+ if @border && !ARGV.join.include?("--no-border")
152
+ border_alpha = @color.alpha <= 30 ? @color.alpha : @border_alpha
153
+ border_color = @border_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue,
154
+ border_alpha)
125
155
  white = Gosu::Color::WHITE
126
156
 
127
- _x = @shadow_size
128
- _y = @shadow_size
157
+ _x = @border_size
158
+ _y = @border_size
159
+ _width = method == :draw_markup ? text_width : markup_width
129
160
 
130
- @rendered_shadow ||= Gosu.render((width + (shadow_size * 2)).ceil, (height + (@shadow_size * 2)).ceil) do
131
- @textobject.send(method, @text, _x - @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
132
- @textobject.send(method, @text, _x - @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
161
+ @rendered_border ||= Gosu.render((_width + (border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do
162
+ @textobject.send(method, @text, _x - @border_size, _y, @z, @factor_x, @factor_y, white, @mode)
163
+ @textobject.send(method, @text, _x - @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
133
164
 
134
- @textobject.send(method, @text, _x, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
135
- @textobject.send(method, @text, _x + @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
165
+ @textobject.send(method, @text, _x, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
166
+ @textobject.send(method, @text, _x + @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
136
167
 
137
- @textobject.send(method, @text, _x, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
138
- @textobject.send(method, @text, _x - @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
168
+ @textobject.send(method, @text, _x, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
169
+ @textobject.send(method, @text, _x - @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
139
170
 
140
- @textobject.send(method, @text, _x + @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
141
- @textobject.send(method, @text, _x + @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
171
+ @textobject.send(method, @text, _x + @border_size, _y, @z, @factor_x, @factor_y, white, @mode)
172
+ @textobject.send(method, @text, _x + @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
142
173
  end
143
- @rendered_shadow.draw(@x - @shadow_size, @y - @shadow_size, @z, @factor_x, @factor_y, shadow_color)
174
+
175
+ @rendered_border.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color)
176
+ end
177
+
178
+ if @shadow
179
+ shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha)
180
+ @textobject.send(method, @text, @x + @shadow_size, @y + @shadow_size, @z, @factor_x, @factor_y, shadow_color, @mode)
144
181
  end
145
182
 
146
183
  @textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode)
@@ -23,7 +23,8 @@ module CyberarmEngine
23
23
  "Tagline",
24
24
  "Caption",
25
25
  "Para",
26
- "Inscription"
26
+ "Inscription",
27
+ "Link"
27
28
  ].each do |const|
28
29
  define_method(:"#{const.downcase}") do |text, options = {}, &block|
29
30
  options[:parent] = element_parent
@@ -97,7 +98,7 @@ module CyberarmEngine
97
98
  end
98
99
 
99
100
  def background(color = Gosu::Color::NONE)
100
- element_parent.style.background = color
101
+ element_parent.style.default[:background] = color
101
102
  end
102
103
 
103
104
  def theme(theme)
@@ -18,6 +18,8 @@ module CyberarmEngine
18
18
  @visible = @options[:visible].nil? ? true : @options[:visible]
19
19
  @tip = @options[:tip] || ""
20
20
 
21
+ @debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]
22
+
21
23
  @style = Style.new(options)
22
24
 
23
25
  @root ||= nil
@@ -34,23 +36,33 @@ module CyberarmEngine
34
36
  @style.height = default(:height) || nil
35
37
 
36
38
  @style.background_canvas = Background.new
37
- @style.border_canvas = BorderCanvas.new(element: self)
39
+ @style.background_nine_slice_canvas = BackgroundNineSlice.new
40
+ @style.border_canvas = BorderCanvas.new(element: self)
41
+
42
+ @style_event = :default
38
43
 
39
44
  stylize
40
45
 
41
46
  default_events
47
+
48
+ root.gui_state.request_focus(self) if @options[:autofocus]
42
49
  end
43
50
 
44
51
  def stylize
45
52
  set_static_position
46
- set_border_thickness(@style.border_thickness)
47
53
 
48
- set_padding(@style.padding)
54
+ set_padding
55
+ set_margin
49
56
 
50
- set_margin(@style.margin)
57
+ set_background
58
+ set_background_nine_slice
59
+
60
+ set_border_thickness
61
+ set_border_color
62
+ end
51
63
 
52
- set_background(@style.background)
53
- set_border_color(@style.border_color)
64
+ def safe_style_fetch(*args)
65
+ @style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args)
54
66
  end
55
67
 
56
68
  def set_static_position
@@ -58,47 +70,89 @@ module CyberarmEngine
58
70
  @y = @style.y if @style.y != 0
59
71
  end
60
72
 
61
- def set_background(background)
62
- @style.background = background
63
- @style.background_canvas.background = background
73
+ def set_background
74
+ @style.background = safe_style_fetch(:background)
75
+
76
+ @style.background_canvas.background = @style.background
77
+ end
78
+
79
+ def set_background_nine_slice
80
+ @style.background_nine_slice = safe_style_fetch(:background_nine_slice)
81
+
82
+ @style.background_nine_slice_mode = safe_style_fetch(:background_nine_slice_mode) || :stretch
83
+ @style.background_nine_slice_color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE
84
+ @style.background_nine_slice_canvas.color = @style.background_nine_slice_color
85
+
86
+ @style.background_nine_slice_from_edge = safe_style_fetch(:background_nine_slice_from_edge)
87
+
88
+ @style.background_nine_slice_left = safe_style_fetch(:background_nine_slice_left) || @style.background_nine_slice_from_edge
89
+ @style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top) || @style.background_nine_slice_from_edge
90
+ @style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right) || @style.background_nine_slice_from_edge
91
+ @style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge
64
92
  end
65
93
 
66
- def set_border_thickness(border_thickness)
67
- @style.border_thickness = border_thickness
94
+ def set_border_thickness
95
+ @style.border_thickness = safe_style_fetch(:border_thickness)
68
96
 
69
- @style.border_thickness_left = default(:border_thickness_left) || @style.border_thickness
70
- @style.border_thickness_right = default(:border_thickness_right) || @style.border_thickness
71
- @style.border_thickness_top = default(:border_thickness_top) || @style.border_thickness
72
- @style.border_thickness_bottom = default(:border_thickness_bottom) || @style.border_thickness
97
+ @style.border_thickness_left = safe_style_fetch(:border_thickness_left) || @style.border_thickness
98
+ @style.border_thickness_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness
99
+ @style.border_thickness_top = safe_style_fetch(:border_thickness_top) || @style.border_thickness
100
+ @style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom) || @style.border_thickness
73
101
  end
74
102
 
75
- def set_border_color(color)
76
- @style.border_color = color
103
+ def set_border_color
104
+ @style.border_color = safe_style_fetch(:border_color)
77
105
 
78
- @style.border_color_left = default(:border_color_left) || @style.border_color
79
- @style.border_color_right = default(:border_color_right) || @style.border_color
80
- @style.border_color_top = default(:border_color_top) || @style.border_color
81
- @style.border_color_bottom = default(:border_color_bottom) || @style.border_color
106
+ @style.border_color_left = safe_style_fetch(:border_color_left) || @style.border_color
107
+ @style.border_color_right = safe_style_fetch(:border_color_right) || @style.border_color
108
+ @style.border_color_top = safe_style_fetch(:border_color_top) || @style.border_color
109
+ @style.border_color_bottom = safe_style_fetch(:border_color_bottom) || @style.border_color
82
110
 
83
- @style.border_canvas.color = color
111
+ @style.border_canvas.color = [
112
+ @style.border_color_top,
113
+ @style.border_color_right,
114
+ @style.border_color_bottom,
115
+ @style.border_color_left
116
+ ]
84
117
  end
85
118
 
86
- def set_padding(padding)
87
- @style.padding = padding
119
+ def set_padding
120
+ @style.padding = safe_style_fetch(:padding)
88
121
 
89
- @style.padding_left = default(:padding_left) || @style.padding
90
- @style.padding_right = default(:padding_right) || @style.padding
91
- @style.padding_top = default(:padding_top) || @style.padding
92
- @style.padding_bottom = default(:padding_bottom) || @style.padding
122
+ @style.padding_left = safe_style_fetch(:padding_left) || @style.padding
123
+ @style.padding_right = safe_style_fetch(:padding_right) || @style.padding
124
+ @style.padding_top = safe_style_fetch(:padding_top) || @style.padding
125
+ @style.padding_bottom = safe_style_fetch(:padding_bottom) || @style.padding
93
126
  end
94
127
 
95
- def set_margin(margin)
96
- @style.margin = margin
128
+ def set_margin
129
+ @style.margin = safe_style_fetch(:margin)
130
+
131
+ @style.margin_left = safe_style_fetch(:margin_left) || @style.margin
132
+ @style.margin_right = safe_style_fetch(:margin_right) || @style.margin
133
+ @style.margin_top = safe_style_fetch(:margin_top) || @style.margin
134
+ @style.margin_bottom = safe_style_fetch(:margin_bottom) || @style.margin
135
+ end
136
+
137
+ def update_styles(event = :default)
138
+ old_width = width
139
+ old_height = height
140
+
141
+ _style = @style.send(event)
142
+ @style_event = event
97
143
 
98
- @style.margin_left = default(:margin_left) || @style.margin
99
- @style.margin_right = default(:margin_right) || @style.margin
100
- @style.margin_top = default(:margin_top) || @style.margin
101
- @style.margin_bottom = default(:margin_bottom) || @style.margin
144
+ if @text.is_a?(CyberarmEngine::Text)
145
+ @text.color = _style&.dig(:color) || @style.default[:color]
146
+ @text.swap_font(_style&.dig(:text_size) || @style.default[:text_size], _style&.dig(:font) || @style.default[:font])
147
+ end
148
+
149
+ return if self.is_a?(ToolTip)
150
+
151
+ if old_width != width || old_height != height
152
+ (root&.gui_state || @gui_state).request_recalculate
153
+ else
154
+ stylize
155
+ end
102
156
  end
103
157
 
104
158
  def default_events
@@ -116,11 +170,74 @@ module CyberarmEngine
116
170
  event(:hover)
117
171
  event(:leave)
118
172
 
173
+ event(:focus)
119
174
  event(:blur)
120
175
 
121
176
  event(:changed)
122
177
  end
123
178
 
179
+ def enter(_sender)
180
+ @focus = false unless window.button_down?(Gosu::MsLeft)
181
+
182
+ if !@enabled
183
+ update_styles(:disabled)
184
+ elsif @focus
185
+ update_styles(:active)
186
+ else
187
+ update_styles(:hover)
188
+ end
189
+
190
+ :handled
191
+ end
192
+
193
+ def left_mouse_button(_sender, _x, _y)
194
+ @focus = true
195
+
196
+ unless @enabled
197
+ update_styles(:disabled)
198
+ else
199
+ update_styles(:active)
200
+ end
201
+
202
+ window.current_state.focus = self
203
+
204
+ :handled
205
+ end
206
+
207
+ def released_left_mouse_button(sender, _x, _y)
208
+ enter(sender)
209
+
210
+ :handled
211
+ end
212
+
213
+ def clicked_left_mouse_button(_sender, _x, _y)
214
+ @block&.call(self) if @enabled && !self.is_a?(Container)
215
+
216
+ :handled
217
+ end
218
+
219
+ def leave(_sender)
220
+ if @enabled
221
+ update_styles
222
+ else
223
+ update_styles(:disabled)
224
+ end
225
+
226
+ :handled
227
+ end
228
+
229
+ def blur(_sender)
230
+ @focus = false
231
+
232
+ if @enabled
233
+ update_styles
234
+ else
235
+ update_styles(:disabled)
236
+ end
237
+
238
+ :handled
239
+ end
240
+
124
241
  def enabled?
125
242
  @enabled
126
243
  end
@@ -150,6 +267,7 @@ module CyberarmEngine
150
267
  return unless visible?
151
268
 
152
269
  @style.background_canvas.draw
270
+ @style.background_nine_slice_canvas.draw
153
271
  @style.border_canvas.draw
154
272
 
155
273
  Gosu.clip_to(@x, @y, width, height) do
@@ -157,6 +275,31 @@ module CyberarmEngine
157
275
  end
158
276
  end
159
277
 
278
+ def debug_draw
279
+ return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
280
+
281
+ Gosu.draw_line(
282
+ x, y, @debug_color,
283
+ x + outer_width, y, @debug_color,
284
+ Float::INFINITY
285
+ )
286
+ Gosu.draw_line(
287
+ x + outer_width, y, @debug_color,
288
+ x + outer_width, y + outer_height, @debug_color,
289
+ Float::INFINITY
290
+ )
291
+ Gosu.draw_line(
292
+ x + outer_width, y + outer_height, @debug_color,
293
+ x, y + outer_height, @debug_color,
294
+ Float::INFINITY
295
+ )
296
+ Gosu.draw_line(
297
+ x, outer_height, @debug_color,
298
+ x, y, @debug_color,
299
+ Float::INFINITY
300
+ )
301
+ end
302
+
160
303
  def update
161
304
  end
162
305
 
@@ -226,20 +369,34 @@ module CyberarmEngine
226
369
  (@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
227
370
  end
228
371
 
372
+ def scroll_width
373
+ @children.sum(&:width) + noncontent_width
374
+ end
375
+
376
+ def scroll_height
377
+ @children.sum(&:height) + noncontent_height
378
+ end
379
+
380
+ def max_scroll_width
381
+ scroll_width - width
382
+ end
383
+
384
+ def max_scroll_height
385
+ scroll_height - height
386
+ end
387
+
229
388
  def dimensional_size(size, dimension)
230
389
  raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
231
390
 
232
- if size && size.is_a?(Numeric)
233
- if size.between?(0.0, 1.0)
234
- ((@parent.send(:"content_#{dimension}") - send(:"noncontent_#{dimension}")) * size).round
235
- else
236
- size
237
- end
391
+ if size.is_a?(Numeric) && size.between?(0.0, 1.0)
392
+ (@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round
393
+ else
394
+ size
238
395
  end
239
396
  end
240
397
 
241
398
  def background=(_background)
242
- @style.background_canvas.background = (_background)
399
+ @style.background_canvas.background = _background
243
400
  update_background
244
401
  end
245
402
 
@@ -251,10 +408,34 @@ module CyberarmEngine
251
408
  @style.background_canvas.height = height
252
409
 
253
410
  @style.background_canvas.update
254
-
411
+ update_background_nine_slice
255
412
  @style.border_canvas.update
256
413
  end
257
414
 
415
+ def background_nine_slice=(_image_path)
416
+ @style.background_nine_slice_canvas.image = _image_path
417
+ update_background_nine_slice
418
+ end
419
+
420
+ def update_background_nine_slice
421
+ @style.background_nine_slice_canvas.x = @x
422
+ @style.background_nine_slice_canvas.y = @y
423
+ @style.background_nine_slice_canvas.z = @z
424
+ @style.background_nine_slice_canvas.width = width
425
+ @style.background_nine_slice_canvas.height = height
426
+
427
+ @style.background_nine_slice_canvas.mode = @style.background_nine_slice_mode
428
+
429
+ @style.background_nine_slice_canvas.color = @style.background_nine_slice_color
430
+
431
+ @style.background_nine_slice_canvas.left = @style.background_nine_slice_left
432
+ @style.background_nine_slice_canvas.top = @style.background_nine_slice_top
433
+ @style.background_nine_slice_canvas.right = @style.background_nine_slice_right
434
+ @style.background_nine_slice_canvas.bottom = @style.background_nine_slice_bottom
435
+
436
+ @style.background_nine_slice_canvas.image = @style.background_nine_slice
437
+ end
438
+
258
439
  def root
259
440
  return self if is_root?
260
441
 
@@ -262,11 +443,9 @@ module CyberarmEngine
262
443
  @root = parent
263
444
 
264
445
  loop do
265
- if @root.parent.nil?
266
- break
267
- else
268
- @root = @root.parent
269
- end
446
+ break unless @root&.parent
447
+
448
+ @root = @root.parent
270
449
  end
271
450
  end
272
451
 
@@ -277,6 +456,12 @@ module CyberarmEngine
277
456
  @gui_state != nil
278
457
  end
279
458
 
459
+ def focus(_)
460
+ warn "#{self.class}#focus was not overridden!"
461
+
462
+ :handled
463
+ end
464
+
280
465
  def recalculate
281
466
  raise "#{self.class}#recalculate was not overridden!"
282
467
  end
@@ -295,5 +480,9 @@ module CyberarmEngine
295
480
  def to_s
296
481
  "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{value.is_a?(String) ? "\"#{value}\"" : value}"
297
482
  end
483
+
484
+ def inspect
485
+ to_s
486
+ end
298
487
  end
299
488
  end
@@ -34,69 +34,6 @@ module CyberarmEngine
34
34
  @text.draw
35
35
  end
36
36
 
37
- def enter(_sender)
38
- @focus = false unless window.button_down?(Gosu::MsLeft)
39
-
40
- if !@enabled
41
- @style.background_canvas.background = @style.disabled[:background]
42
- @text.color = @style.disabled[:color]
43
- elsif @focus
44
- @style.background_canvas.background = @style.active[:background]
45
- @text.color = @style.active[:color]
46
- else
47
- @style.background_canvas.background = @style.hover[:background]
48
- @text.color = @style.hover[:color]
49
- end
50
-
51
- :handled
52
- end
53
-
54
- def left_mouse_button(_sender, _x, _y)
55
- @focus = true
56
-
57
- unless @enabled
58
- @style.background_canvas.background = @style.disabled[:background]
59
- @text.color = @style.disabled[:color]
60
- else
61
- @style.background_canvas.background = @style.active[:background]
62
- @text.color = @style.active[:color]
63
- end
64
-
65
- window.current_state.focus = self
66
-
67
- :handled
68
- end
69
-
70
- def released_left_mouse_button(sender, _x, _y)
71
- enter(sender)
72
-
73
- :handled
74
- end
75
-
76
- def clicked_left_mouse_button(_sender, _x, _y)
77
- @block.call(self) if @enabled && @block
78
-
79
- :handled
80
- end
81
-
82
- def leave(_sender)
83
- unless @enabled
84
- @style.background_canvas.background = @style.disabled[:background]
85
- @text.color = @style.disabled[:color]
86
- else
87
- @style.background_canvas.background = @style.background
88
- @text.color = @style.color
89
- end
90
-
91
- :handled
92
- end
93
-
94
- def blur(_sender)
95
- @focus = false
96
-
97
- :handled
98
- end
99
-
100
37
  def recalculate
101
38
  unless @enabled
102
39
  @style.background_canvas.background = @style.disabled[:background]
@@ -5,8 +5,11 @@ module CyberarmEngine
5
5
  super(options, block)
6
6
  options[:toggled] = options[:checked]
7
7
 
8
+ options[:parent] = self
8
9
  @toggle_button = ToggleButton.new(options)
9
- @label = TextBlock.new(text, options)
10
+
11
+ options[:parent] = self
12
+ @label = TextBlock.new(text, options)
10
13
 
11
14
  @label.subscribe(:holding_left_mouse_button) do |sender, x, y|
12
15
  @toggle_button.left_mouse_button(sender, x, y)