glimmer-dsl-libui 0.2.16 → 0.2.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,354 @@
1
+ require 'glimmer-dsl-libui'
2
+
3
+ require_relative 'tetris/model/game'
4
+
5
+ class Tetris
6
+ include Glimmer
7
+
8
+ BLOCK_SIZE = 25
9
+ BEVEL_CONSTANT = 20
10
+ COLOR_GRAY = {r: 192, g: 192, b: 192}
11
+
12
+ def initialize
13
+ @game = Model::Game.new
14
+ end
15
+
16
+ def launch
17
+ create_gui
18
+ register_observers
19
+ @game.start!
20
+ @main_window.show
21
+ end
22
+
23
+ def create_gui
24
+ menu_bar
25
+
26
+ @main_window = window('Glimmer Tetris') {
27
+ content_size Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE + 98
28
+ resizable false
29
+
30
+ vertical_box {
31
+ label { # filler
32
+ stretchy false
33
+ }
34
+
35
+ score_board(block_size: BLOCK_SIZE) {
36
+ stretchy false
37
+ }
38
+
39
+ @playfield_blocks = playfield(playfield_width: Model::Game::PLAYFIELD_WIDTH, playfield_height: Model::Game::PLAYFIELD_HEIGHT, block_size: BLOCK_SIZE)
40
+ }
41
+ }
42
+ end
43
+
44
+ def register_observers
45
+ Glimmer::DataBinding::Observer.proc do |game_over|
46
+ if game_over
47
+ @pause_menu_item.enabled = false
48
+ show_game_over_dialog
49
+ else
50
+ @pause_menu_item.enabled = true
51
+ start_moving_tetrominos_down
52
+ end
53
+ end.observe(@game, :game_over)
54
+
55
+ Model::Game::PLAYFIELD_HEIGHT.times do |row|
56
+ Model::Game::PLAYFIELD_WIDTH.times do |column|
57
+ Glimmer::DataBinding::Observer.proc do |new_color|
58
+ Glimmer::LibUI.queue_main do
59
+ color = Glimmer::LibUI.interpret_color(new_color)
60
+ block = @playfield_blocks[row][column]
61
+ block[:background_square].fill = color
62
+ block[:top_bevel_edge].fill = {r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT}
63
+ block[:right_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
64
+ block[:bottom_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
65
+ block[:left_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
66
+ block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
67
+ end
68
+ end.observe(@game.playfield[row][column], :color)
69
+ end
70
+ end
71
+
72
+ Model::Game::PREVIEW_PLAYFIELD_HEIGHT.times do |row|
73
+ Model::Game::PREVIEW_PLAYFIELD_WIDTH.times do |column|
74
+ Glimmer::DataBinding::Observer.proc do |new_color|
75
+ Glimmer::LibUI.queue_main do
76
+ color = Glimmer::LibUI.interpret_color(new_color)
77
+ block = @preview_playfield_blocks[row][column]
78
+ block[:background_square].fill = color
79
+ block[:top_bevel_edge].fill = {r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT}
80
+ block[:right_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
81
+ block[:bottom_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
82
+ block[:left_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
83
+ block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
84
+ end
85
+ end.observe(@game.preview_playfield[row][column], :color)
86
+ end
87
+ end
88
+
89
+ Glimmer::DataBinding::Observer.proc do |new_score|
90
+ Glimmer::LibUI.queue_main do
91
+ @score_label.text = new_score.to_s
92
+ end
93
+ end.observe(@game, :score)
94
+
95
+ Glimmer::DataBinding::Observer.proc do |new_lines|
96
+ Glimmer::LibUI.queue_main do
97
+ @lines_label.text = new_lines.to_s
98
+ end
99
+ end.observe(@game, :lines)
100
+
101
+ Glimmer::DataBinding::Observer.proc do |new_level|
102
+ Glimmer::LibUI.queue_main do
103
+ @level_label.text = new_level.to_s
104
+ end
105
+ end.observe(@game, :level)
106
+ end
107
+
108
+ def menu_bar
109
+ menu('Game') {
110
+ @pause_menu_item = check_menu_item('Pause') {
111
+ enabled false
112
+
113
+ on_clicked do
114
+ @game.paused = @pause_menu_item.checked?
115
+ end
116
+ }
117
+ menu_item('Restart') {
118
+ on_clicked do
119
+ @game.restart!
120
+ end
121
+ }
122
+ separator_menu_item
123
+ menu_item('Exit') {
124
+ on_clicked do
125
+ exit(0)
126
+ end
127
+ }
128
+ quit_menu_item if OS.mac?
129
+ }
130
+
131
+ menu('View') {
132
+ menu_item('Show High Scores') {
133
+ on_clicked do
134
+ show_high_scores
135
+ end
136
+ }
137
+ menu_item('Clear High Scores') {
138
+ on_clicked {
139
+ @game.clear_high_scores!
140
+ }
141
+ }
142
+ }
143
+
144
+ menu('Options') {
145
+ radio_menu_item('Instant Down on Up Arrow') {
146
+ on_clicked do
147
+ @game.instant_down_on_up = true
148
+ end
149
+ }
150
+ radio_menu_item('Rotate Right on Up Arrow') {
151
+ on_clicked do
152
+ @game.rotate_right_on_up = true
153
+ end
154
+ }
155
+ radio_menu_item('Rotate Left on Up Arrow') {
156
+ on_clicked do
157
+ @game.rotate_left_on_up = true
158
+ end
159
+ }
160
+ }
161
+
162
+ menu('Help') {
163
+ if OS.mac?
164
+ about_menu_item {
165
+ on_clicked do
166
+ show_about_dialog
167
+ end
168
+ }
169
+ end
170
+ menu_item('About') {
171
+ on_clicked do
172
+ show_about_dialog
173
+ end
174
+ }
175
+ }
176
+ end
177
+
178
+ def playfield(playfield_width: , playfield_height: , block_size: , &extra_content)
179
+ blocks = []
180
+ vertical_box {
181
+ padded false
182
+
183
+ playfield_height.times.map do |row|
184
+ blocks << []
185
+ horizontal_box {
186
+ padded false
187
+
188
+ playfield_width.times.map do |column|
189
+ blocks.last << block(row: row, column: column, block_size: block_size)
190
+ end
191
+ }
192
+ end
193
+
194
+ extra_content&.call
195
+ }
196
+ blocks
197
+ end
198
+
199
+ def block(row: , column: , block_size: , &extra_content)
200
+ block = {}
201
+ bevel_pixel_size = 0.16 * block_size.to_f
202
+ color = Glimmer::LibUI.interpret_color(Model::Block::COLOR_CLEAR)
203
+ area {
204
+ block[:background_square] = path {
205
+ square(0, 0, block_size)
206
+
207
+ fill color
208
+ }
209
+ block[:top_bevel_edge] = path {
210
+ polygon(0, 0, block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size, bevel_pixel_size)
211
+
212
+ fill r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT
213
+ }
214
+ block[:right_bevel_edge] = path {
215
+ polygon(block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size, block_size)
216
+
217
+ fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
218
+ }
219
+ block[:bottom_bevel_edge] = path {
220
+ polygon(block_size, block_size, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size)
221
+
222
+ fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
223
+ }
224
+ block[:left_bevel_edge] = path {
225
+ polygon(0, 0, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size)
226
+
227
+ fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
228
+ }
229
+ block[:border_square] = path {
230
+ square(0, 0, block_size)
231
+
232
+ stroke COLOR_GRAY
233
+ }
234
+
235
+ on_key_down do |key_event|
236
+ case key_event
237
+ in ext_key: :down
238
+ @game.down!
239
+ in key: ' '
240
+ @game.down!(instant: true)
241
+ in ext_key: :up
242
+ case @game.up_arrow_action
243
+ when :instant_down
244
+ @game.down!(instant: true)
245
+ when :rotate_right
246
+ @game.rotate!(:right)
247
+ when :rotate_left
248
+ @game.rotate!(:left)
249
+ end
250
+ in ext_key: :left
251
+ @game.left!
252
+ in ext_key: :right
253
+ @game.right!
254
+ in modifier: :shift
255
+ @game.rotate!(:right)
256
+ in modifier: :control
257
+ @game.rotate!(:left)
258
+ else
259
+ # Do Nothing
260
+ end
261
+ end
262
+
263
+ extra_content&.call
264
+ }
265
+ block
266
+ end
267
+
268
+ def score_board(block_size: , &extra_content)
269
+ vertical_box {
270
+ horizontal_box {
271
+ label # filler
272
+ @preview_playfield_blocks = playfield(playfield_width: Model::Game::PREVIEW_PLAYFIELD_WIDTH, playfield_height: Model::Game::PREVIEW_PLAYFIELD_HEIGHT, block_size: block_size)
273
+ label # filler
274
+ }
275
+
276
+ horizontal_box {
277
+ label # filler
278
+ grid {
279
+ stretchy false
280
+
281
+ label('Score') {
282
+ left 0
283
+ top 0
284
+ halign :fill
285
+ }
286
+ @score_label = label {
287
+ left 0
288
+ top 1
289
+ halign :center
290
+ }
291
+
292
+ label('Lines') {
293
+ left 1
294
+ top 0
295
+ halign :fill
296
+ }
297
+ @lines_label = label {
298
+ left 1
299
+ top 1
300
+ halign :center
301
+ }
302
+
303
+ label('Level') {
304
+ left 2
305
+ top 0
306
+ halign :fill
307
+ }
308
+ @level_label = label {
309
+ left 2
310
+ top 1
311
+ halign :center
312
+ }
313
+ }
314
+ label # filler
315
+ }
316
+
317
+ extra_content&.call
318
+ }
319
+ end
320
+
321
+ def start_moving_tetrominos_down
322
+ Glimmer::LibUI.timer(@game.delay) do
323
+ @game.down! if !@game.game_over? && !@game.paused?
324
+ end
325
+ end
326
+
327
+ def show_game_over_dialog
328
+ Glimmer::LibUI.queue_main do
329
+ msg_box('Game Over!', "Score: #{@game.high_scores.first.score}\nLines: #{@game.high_scores.first.lines}\nLevel: #{@game.high_scores.first.level}")
330
+ @game.restart!
331
+ end
332
+ end
333
+
334
+ def show_high_scores
335
+ Glimmer::LibUI.queue_main do
336
+ if @game.high_scores.empty?
337
+ high_scores_string = "No games have been scored yet."
338
+ else
339
+ high_scores_string = @game.high_scores.map do |high_score|
340
+ "#{high_score.name} | Score: #{high_score.score} | Lines: #{high_score.lines} | Level: #{high_score.level}"
341
+ end.join("\n")
342
+ end
343
+ msg_box('High Scores', high_scores_string)
344
+ end
345
+ end
346
+
347
+ def show_about_dialog
348
+ Glimmer::LibUI.queue_main do
349
+ msg_box('About', 'Glimmer Tetris - Glimmer DSL for LibUI Example - Copyright (c) 2021 Andy Maleh')
350
+ end
351
+ end
352
+ end
353
+
354
+ Tetris.new.launch
Binary file
@@ -37,6 +37,7 @@ module Glimmer
37
37
  end
38
38
 
39
39
  def interpret(parent, keyword, *args, &block)
40
+ args = [args] if args.size > 1 && Glimmer::LibUI::Shape.shape_class(keyword).parameters.size == 1
40
41
  Glimmer::LibUI::Shape.create(keyword, parent, args, &block)
41
42
  end
42
43
 
@@ -109,6 +109,31 @@ module Glimmer
109
109
  queue_redraw_all
110
110
  end
111
111
 
112
+ def request_auto_redraw
113
+ # TODO implement functionality to delay queuing area redraws until post_add_content has been called (area definition is done). Maybe offer an option to enable redrawing before area is closed too.
114
+ queue_redraw_all if auto_redraw_enabled?
115
+ end
116
+
117
+ def auto_redraw_enabled(value = nil)
118
+ if value.nil?
119
+ @auto_redraw_enabled = true if @auto_redraw_enabled.nil?
120
+ @auto_redraw_enabled
121
+ else
122
+ @auto_redraw_enabled = !!value
123
+ end
124
+ end
125
+ alias auto_redraw_enabled? auto_redraw_enabled
126
+ alias auto_redraw_enabled= auto_redraw_enabled
127
+ alias set_auto_redraw_enabled auto_redraw_enabled
128
+
129
+ def pause_auto_redraw
130
+ self.auto_redraw_enabled = false
131
+ end
132
+
133
+ def resume_auto_redraw
134
+ self.auto_redraw_enabled = true
135
+ end
136
+
112
137
  private
113
138
 
114
139
  def build_control
@@ -68,12 +68,16 @@ module Glimmer
68
68
  if args.empty?
69
69
  @fill ||= {}
70
70
  else
71
- @fill = Glimmer::LibUI.interpret_color(args)
72
- @parent_proxy&.queue_redraw_all
71
+ new_color = Glimmer::LibUI.interpret_color(args)
72
+ if new_color != @fill
73
+ @fill_observer&.unobserve(@fill) if @fill
74
+ @fill = new_color
75
+ request_auto_redraw
76
+ end
73
77
  end
74
78
  @fill.tap do
75
79
  @fill_observer ||= Glimmer::DataBinding::Observer.proc do
76
- @parent_proxy&.queue_redraw_all
80
+ request_auto_redraw
77
81
  end
78
82
  @fill_observer.observe(@fill)
79
83
  end
@@ -92,12 +96,16 @@ module Glimmer
92
96
  if args.empty?
93
97
  @stroke ||= {}
94
98
  else
95
- @stroke = Glimmer::LibUI.interpret_color(args)
96
- @parent_proxy&.queue_redraw_all
99
+ new_color = Glimmer::LibUI.interpret_color(args)
100
+ if new_color != @stroke
101
+ @stroke_observer&.unobserve(@stroke) if @stroke
102
+ @stroke = Glimmer::LibUI.interpret_color(args)
103
+ request_auto_redraw
104
+ end
97
105
  end
98
106
  @stroke.tap do
99
107
  @stroke_observer ||= Glimmer::DataBinding::Observer.proc do
100
- @parent_proxy&.queue_redraw_all
108
+ request_auto_redraw
101
109
  end
102
110
  @stroke_observer.observe(@stroke)
103
111
  end
@@ -137,7 +145,11 @@ module Glimmer
137
145
  end
138
146
 
139
147
  def redraw
140
- @parent_proxy&.queue_redraw_all
148
+ @parent_proxy&.redraw
149
+ end
150
+
151
+ def request_auto_redraw
152
+ @parent_proxy&.request_auto_redraw
141
153
  end
142
154
 
143
155
  private
@@ -78,6 +78,7 @@ module Glimmer
78
78
  when 'on_destroy'
79
79
  on_destroy(&listener)
80
80
  else
81
+ default_behavior_listener = nil
81
82
  if listener_name == 'on_closing'
82
83
  default_behavior_listener = Proc.new do
83
84
  return_value = listener.call(self)
@@ -90,9 +91,45 @@ module Glimmer
90
91
  end
91
92
  end
92
93
  end
93
- super(listener_name, &default_behavior_listener)
94
+ super(listener_name, &(default_behavior_listener || listener))
94
95
  end
95
96
  end
97
+
98
+ def content_size(*args)
99
+ if args.empty?
100
+ width = Fiddle::Pointer.malloc(8)
101
+ height = Fiddle::Pointer.malloc(8)
102
+ ::LibUI.window_content_size(@libui, width, height)
103
+ width = width[0, 8].unpack1('i')
104
+ height = height[0, 8].unpack1('i')
105
+ [width, height]
106
+ else
107
+ args = args.first if args.size == 1 && args.first.is_a?(Array)
108
+ super
109
+ @width = args[0]
110
+ @height = args[1]
111
+ end
112
+ end
113
+ alias content_size= content_size
114
+ alias set_content_size content_size
115
+
116
+ def resizable(value = nil)
117
+ if value.nil?
118
+ @resizable = true if @resizable.nil?
119
+ @resizable
120
+ else
121
+ @resizable = value
122
+ if !@resizable && !@resizable_listener_registered
123
+ handle_listener('on_content_size_changed') do
124
+ set_content_size(@width, @height) unless @resizable
125
+ end
126
+ @resizable_listener_registered = true
127
+ end
128
+ end
129
+ end
130
+ alias resizable? resizable
131
+ alias resizable= resizable
132
+ alias set_resizable resizable
96
133
 
97
134
  private
98
135
 
@@ -107,6 +144,8 @@ module Glimmer
107
144
  construction_args[2] = DEFAULT_HEIGHT if construction_args.size == 2
108
145
  construction_args[3] = DEFAULT_HAS_MENUBAR if construction_args.size == 3
109
146
  construction_args[3] = Glimmer::LibUI.boolean_to_integer(construction_args[3]) if construction_args.size == 4 && (construction_args[3].is_a?(TrueClass) || construction_args[3].is_a?(FalseClass))
147
+ @width = construction_args[1]
148
+ @height = construction_args[2]
110
149
  @libui = ControlProxy.new_control(@keyword, construction_args)
111
150
  @libui.tap do
112
151
  handle_listener('on_closing') do
@@ -41,8 +41,10 @@ module Glimmer
41
41
  if value.nil?
42
42
  @closed
43
43
  else
44
- @closed = value
45
- area_proxy&.queue_redraw_all
44
+ if !!value != !!@closed
45
+ @closed = value
46
+ request_auto_redraw
47
+ end
46
48
  end
47
49
  end
48
50
  alias closed= closed
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/shape'
23
+
24
+ module Glimmer
25
+ module LibUI
26
+ class Shape
27
+ class Polybezier < Shape
28
+ parameters :point_array
29
+ parameter_defaults []
30
+
31
+ def draw(area_draw_params)
32
+ alternating_x_y_array = point_array.to_a.compact.flatten
33
+ unless alternating_x_y_array.empty?
34
+ ::LibUI.draw_path_new_figure(path_proxy.libui, alternating_x_y_array[0], alternating_x_y_array[1])
35
+ ((alternating_x_y_array.size - 2) / 6).times do |n|
36
+ point_alternating_x_y_index = n * 6
37
+ ::LibUI.draw_path_bezier_to(path_proxy.libui, alternating_x_y_array[point_alternating_x_y_index + 2], alternating_x_y_array[point_alternating_x_y_index + 3], alternating_x_y_array[point_alternating_x_y_index + 4], alternating_x_y_array[point_alternating_x_y_index + 5], alternating_x_y_array[point_alternating_x_y_index + 6], alternating_x_y_array[point_alternating_x_y_index + 7])
38
+ end
39
+ end
40
+ super
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/shape'
23
+
24
+ module Glimmer
25
+ module LibUI
26
+ class Shape
27
+ class Polygon < Shape
28
+ parameters :point_array
29
+ parameter_defaults []
30
+
31
+ def draw(area_draw_params)
32
+ alternating_x_y_array = point_array.to_a.compact.flatten
33
+ unless alternating_x_y_array.empty?
34
+ ::LibUI.draw_path_new_figure(path_proxy.libui, alternating_x_y_array[0], alternating_x_y_array[1])
35
+ ((alternating_x_y_array.size - 2) / 2).times do |n|
36
+ point_alternating_x_y_index = n * 2
37
+ ::LibUI.draw_path_line_to(path_proxy.libui, alternating_x_y_array[point_alternating_x_y_index + 2], alternating_x_y_array[point_alternating_x_y_index + 3])
38
+ end
39
+ ::LibUI.draw_path_close_figure(path_proxy.libui)
40
+ end
41
+ super
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/shape'
23
+
24
+ module Glimmer
25
+ module LibUI
26
+ class Shape
27
+ class Polyline < Shape
28
+ parameters :point_array
29
+ parameter_defaults []
30
+
31
+ def draw(area_draw_params)
32
+ alternating_x_y_array = point_array.to_a.compact.flatten
33
+ unless alternating_x_y_array.empty?
34
+ ::LibUI.draw_path_new_figure(path_proxy.libui, alternating_x_y_array[0], alternating_x_y_array[1])
35
+ ((alternating_x_y_array.size - 2) / 2).times do |n|
36
+ point_alternating_x_y_index = n * 2
37
+ ::LibUI.draw_path_line_to(path_proxy.libui, alternating_x_y_array[point_alternating_x_y_index + 2], alternating_x_y_array[point_alternating_x_y_index + 3])
38
+ end
39
+ end
40
+ super
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -87,7 +87,11 @@ module Glimmer
87
87
  end
88
88
 
89
89
  def redraw
90
- area_proxy&.queue_redraw_all
90
+ area_proxy&.auto_redraw
91
+ end
92
+
93
+ def request_auto_redraw
94
+ area_proxy&.request_auto_redraw
91
95
  end
92
96
 
93
97
  def destroy
@@ -113,8 +117,10 @@ module Glimmer
113
117
  method_name = method_name.to_s
114
118
  parameter_index = self.class.parameters.index(method_name_parameter)
115
119
  if method_name.start_with?('set_') || method_name.end_with?('=') || !args.empty?
116
- @args[parameter_index] = args.first
117
- area_proxy&.queue_redraw_all
120
+ if args.first != @args[parameter_index]
121
+ @args[parameter_index] = args.first
122
+ request_auto_redraw
123
+ end
118
124
  else
119
125
  @args[parameter_index]
120
126
  end