glimmer-dsl-libui 0.2.14 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,155 @@
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
+ attr_reader :game
13
+
14
+ def initialize
15
+ @game = Model::Game.new
16
+ create_gui
17
+ register_observers
18
+ end
19
+
20
+ def launch
21
+ @game.start!
22
+ @main_window.show
23
+ end
24
+
25
+ def create_gui
26
+ @main_window = window('Glimmer Tetris', Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE) {
27
+ playfield(playfield_width: Model::Game::PLAYFIELD_WIDTH, playfield_height: Model::Game::PLAYFIELD_HEIGHT, block_size: BLOCK_SIZE)
28
+ }
29
+ end
30
+
31
+ def register_observers
32
+ Glimmer::DataBinding::Observer.proc do |game_over|
33
+ if game_over
34
+ show_game_over_dialog
35
+ else
36
+ start_moving_tetrominos_down
37
+ end
38
+ end.observe(@game, :game_over)
39
+
40
+ Model::Game::PLAYFIELD_HEIGHT.times do |row|
41
+ Model::Game::PLAYFIELD_HEIGHT.times do |column|
42
+ Glimmer::DataBinding::Observer.proc do |new_color|
43
+ Glimmer::LibUI.queue_main do
44
+ color = Glimmer::LibUI.interpret_color(new_color)
45
+ block = @blocks[row][column]
46
+ block[:background_square].fill = color
47
+ block[:top_bevel_edge].fill = {r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT}
48
+ block[:right_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
49
+ block[:bottom_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
50
+ block[:left_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
51
+ block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
52
+ end
53
+ end.observe(@game.playfield[row][column], :color)
54
+ end
55
+ end
56
+ end
57
+
58
+ def playfield(playfield_width: , playfield_height: , block_size: )
59
+ @blocks = []
60
+ vertical_box {
61
+ padded false
62
+
63
+ playfield_height.times.map do |row|
64
+ @blocks << []
65
+ horizontal_box {
66
+ padded false
67
+
68
+ playfield_width.times.map do |column|
69
+ @blocks.last << block(row: row, column: column, block_size: block_size)
70
+ end
71
+ }
72
+ end
73
+ }
74
+ end
75
+
76
+ def block(row: , column: , block_size: )
77
+ block = {}
78
+ bevel_pixel_size = 0.16 * block_size.to_f
79
+ color = Glimmer::LibUI.interpret_color(Model::Block::COLOR_CLEAR)
80
+ area {
81
+ block[:background_square] = path {
82
+ square(0, 0, block_size)
83
+
84
+ fill color
85
+ }
86
+ block[:top_bevel_edge] = path {
87
+ polygon(0, 0, block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size, bevel_pixel_size)
88
+
89
+ fill r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT
90
+ }
91
+ block[:right_bevel_edge] = path {
92
+ 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)
93
+
94
+ fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
95
+ }
96
+ block[:bottom_bevel_edge] = path {
97
+ 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)
98
+
99
+ fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
100
+ }
101
+ block[:left_bevel_edge] = path {
102
+ polygon(0, 0, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size)
103
+
104
+ fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
105
+ }
106
+ block[:border_square] = path {
107
+ square(0, 0, block_size)
108
+
109
+ stroke COLOR_GRAY
110
+ }
111
+
112
+ on_key_down do |key_event|
113
+ case key_event
114
+ in ext_key: :down
115
+ game.down!
116
+ in ext_key: :up
117
+ case game.up_arrow_action
118
+ when :instant_down
119
+ game.down!(instant: true)
120
+ when :rotate_right
121
+ game.rotate!(:right)
122
+ when :rotate_left
123
+ game.rotate!(:left)
124
+ end
125
+ in ext_key: :left
126
+ game.left!
127
+ in ext_key: :right
128
+ game.right!
129
+ in modifier: :shift
130
+ game.rotate!(:right)
131
+ in modifier: :control
132
+ game.rotate!(:left)
133
+ else
134
+ # Do Nothing
135
+ end
136
+ end
137
+ }
138
+ block
139
+ end
140
+
141
+ def start_moving_tetrominos_down
142
+ Glimmer::LibUI.timer(@game.delay) do
143
+ @game.down! if !@game.game_over? && !@game.paused?
144
+ end
145
+ end
146
+
147
+ def show_game_over_dialog
148
+ Glimmer::LibUI.queue_main do
149
+ msg_box('Game Over', "Score: #{@game.high_scores.first.score}\nLines: #{@game.high_scores.first.lines}\nLevel: #{@game.high_scores.first.level}")
150
+ @game.restart!
151
+ end
152
+ end
153
+ end
154
+
155
+ 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
@@ -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
data/lib/glimmer/libui.rb CHANGED
@@ -27,11 +27,11 @@ module Glimmer
27
27
  include Glimmer::FiddleConsumer
28
28
 
29
29
  def integer_to_boolean(int, allow_nil: true)
30
- int.nil? ? (allow_nil ? nil : false) : ((int.is_a?(TrueClass) || int.is_a?(FalseClass)) ? int : int == 1)
30
+ int.nil? ? (allow_nil ? nil : false) : ((int.is_a?(TrueClass) || int.is_a?(FalseClass)) ? int : (int.is_a?(Integer) ? int == 1 : (allow_nil ? nil : false)))
31
31
  end
32
32
 
33
33
  def boolean_to_integer(bool, allow_nil: true)
34
- bool.nil? ? (allow_nil ? nil : 0) : (bool.is_a?(Integer) ? bool : (bool == true ? 1 : 0))
34
+ bool.nil? ? (allow_nil ? nil : 0) : (bool.is_a?(Integer) ? bool : (bool.is_a?(TrueClass) || bool.is_a?(FalseClass) ? (bool == true ? 1 : 0) : (allow_nil ? nil : 0)))
35
35
  end
36
36
 
37
37
  def degrees_to_radians(degrees)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-libui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.14
4
+ version: 0.2.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-24 00:00:00.000000000 Z
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -252,6 +252,11 @@ files:
252
252
  - examples/method_based_custom_keyword.rb
253
253
  - examples/midi_player.rb
254
254
  - examples/simple_notepad.rb
255
+ - examples/tetris.rb
256
+ - examples/tetris/model/block.rb
257
+ - examples/tetris/model/game.rb
258
+ - examples/tetris/model/past_game.rb
259
+ - examples/tetris/model/tetromino.rb
255
260
  - examples/timer.rb
256
261
  - glimmer-dsl-libui.gemspec
257
262
  - icons/glimmer.png
@@ -339,6 +344,9 @@ files:
339
344
  - lib/glimmer/libui/shape/circle.rb
340
345
  - lib/glimmer/libui/shape/figure.rb
341
346
  - lib/glimmer/libui/shape/line.rb
347
+ - lib/glimmer/libui/shape/polybezier.rb
348
+ - lib/glimmer/libui/shape/polygon.rb
349
+ - lib/glimmer/libui/shape/polyline.rb
342
350
  - lib/glimmer/libui/shape/rectangle.rb
343
351
  - lib/glimmer/libui/shape/square.rb
344
352
  - sounds/AlanWalker-Faded.mid