glimmer-dsl-gtk 0.0.3 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Dashes'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ dashes = [ 50.0, # ink
13
+ 10.0, # skip
14
+ 10.0, # ink
15
+ 10.0 # skip
16
+ ]
17
+ offset = -50.0
18
+
19
+ path {
20
+ move_to 128.0, 25.6
21
+ line_to 230.4, 230.4
22
+ rel_line_to -102.4, 0.0
23
+ curve_to 51.2, 230.4, 51.2, 128.0, 128.0, 128.0
24
+
25
+ line_width 10
26
+ dash dashes, offset
27
+ stroke 0, 0, 0
28
+ }
29
+ }
30
+ }.show
@@ -0,0 +1,36 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Fill and Stroke 2'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ path {
13
+ move_to 128.0, 25.6
14
+ line_to 230.4, 230.4
15
+ rel_line_to -102.4, 0.0
16
+ curve_to 51.2, 230.4, 51.2, 128.0, 128.0, 128.0
17
+ close_path
18
+
19
+ fill 0, 0, 255
20
+ stroke 0, 0, 0
21
+ line_width 10
22
+ }
23
+
24
+ path {
25
+ move_to 64.0, 25.6
26
+ rel_line_to 51.2, 51.2
27
+ rel_line_to -51.2, 51.2
28
+ rel_line_to -51.2, -51.2
29
+ close_path
30
+
31
+ fill 0, 0, 255
32
+ stroke 0, 0, 0
33
+ line_width 10
34
+ }
35
+ }
36
+ }.show
@@ -0,0 +1,43 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Fill Style'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ path {
13
+ rectangle 12, 12, 232, 70
14
+ path { # sub-path
15
+ arc 64, 64, 40, 0, 2*Math::PI
16
+ }
17
+ path { # sub-path
18
+ arc_negative 192, 64, 40, 0, -2*Math::PI
19
+ }
20
+
21
+ fill_rule Cairo::FILL_RULE_EVEN_ODD
22
+ line_width 6
23
+ fill 0, 178.5, 0
24
+ stroke 0, 0, 0
25
+ }
26
+
27
+ path {
28
+ rectangle 12, 12, 232, 70
29
+ path { # sub-path
30
+ arc 64, 64, 40, 0, 2*Math::PI
31
+ }
32
+ path { # sub-path
33
+ arc_negative 192, 64, 40, 0, -2*Math::PI
34
+ }
35
+
36
+ translate 0, 128
37
+ fill_rule Cairo::FILL_RULE_WINDING
38
+ line_width 6
39
+ fill 0, 0, 229.5
40
+ stroke 0, 0, 0
41
+ }
42
+ }
43
+ }.show
@@ -0,0 +1,31 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Gradient'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ # Create the Linear Pattern
13
+ rectangle(0, 0, 256, 256) {
14
+ pat = Cairo::LinearPattern.new(0.0, 0.0, 0.0, 256.0)
15
+ pat.add_color_stop_rgba(1, 0, 0, 0, 1)
16
+ pat.add_color_stop_rgba(0, 1, 1, 1, 1)
17
+
18
+ fill pat
19
+ }
20
+
21
+ # Create the radial pattern
22
+ arc(128.0, 128.0, 76.8, 0, 2 * Math::PI) {
23
+ pat = Cairo::RadialPattern.new(115.2, 102.4, 25.6,
24
+ 102.4, 102.4, 128.0)
25
+ pat.add_color_stop_rgba(0, 1, 1, 1, 1)
26
+ pat.add_color_stop_rgba(1, 0, 0, 0, 1)
27
+
28
+ fill pat
29
+ }
30
+ }
31
+ }.show
@@ -0,0 +1,23 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Image'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ image = Cairo::ImageSurface.from_png(File.expand_path(File.join('..', '..', 'images', 'breaking-blue-wave.png'), __dir__))
13
+ w = image.width
14
+ h = image.height
15
+
16
+ translate 128.0, 128.0
17
+ rotate 45*Math::PI/180
18
+ scale 256.0/w, 256.0/h
19
+ translate -0.5*w, -0.5*h
20
+
21
+ paint image, 0, 0
22
+ }
23
+ }.show
@@ -0,0 +1,32 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Image Gradient'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ image = Cairo::ImageSurface.from_png(File.expand_path(File.join('..', '..', 'images', 'breaking-blue-wave.png'), __dir__))
13
+ w = image.width
14
+ h = image.height
15
+
16
+ # Load the image as a surface pattern
17
+ pattern = Cairo::SurfacePattern.new(image)
18
+ pattern.extend = Cairo::EXTEND_REPEAT
19
+
20
+ # Set up the scale matrix
21
+ pattern.matrix = Cairo::Matrix.scale(w/256.0 * 5.0, h/256.0 * 5.0)
22
+
23
+ rectangle(0, 0, 256, 256) {
24
+ translate 128.0, 128.0
25
+ rotate Math::PI / 4
26
+ scale 1/Math.sqrt(2), 1/Math.sqrt(2)
27
+ translate -128.0, -128.0
28
+
29
+ fill pattern
30
+ }
31
+ }
32
+ }.show
@@ -0,0 +1,27 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Multi Segment Caps'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ path {
13
+ move_to 50.0, 75.0
14
+ line_to 200.0, 75.0
15
+
16
+ move_to 50.0, 125.0
17
+ line_to 200.0, 125.0
18
+
19
+ move_to 50.0, 175.0
20
+ line_to 200.0, 175.0
21
+
22
+ line_width 30
23
+ line_cap Cairo::LINE_CAP_ROUND
24
+ stroke 0, 0, 0
25
+ }
26
+ }
27
+ }.show
@@ -0,0 +1,20 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Rounded Rectangle'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ path {
13
+ rounded_rectangle(25.6, 25.6, 204.8, 204.8, 20)
14
+
15
+ fill 127.5, 127.5, 255
16
+ line_width 10.0
17
+ stroke 127.5, 0, 0, 0.5
18
+ }
19
+ }
20
+ }.show
@@ -0,0 +1,53 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Set line cap'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ # The main code
13
+ path {
14
+ move_to 64.0, 50.0
15
+ line_to 64.0, 200.0
16
+
17
+ line_cap Cairo::LINE_CAP_BUTT # default
18
+ line_width 30
19
+ stroke 0, 0, 0
20
+ }
21
+
22
+ path {
23
+ move_to 128.0, 50.0
24
+ line_to 128.0, 200.0
25
+
26
+ line_cap Cairo::LINE_CAP_ROUND
27
+ line_width 30
28
+ stroke 0, 0, 0
29
+ }
30
+
31
+ path {
32
+ move_to 192.0, 50.0
33
+ line_to 192.0, 200.0
34
+
35
+ line_cap Cairo::LINE_CAP_SQUARE
36
+ line_width 30
37
+ stroke 0, 0, 0
38
+ }
39
+
40
+ # draw helping lines */
41
+ path {
42
+ move_to 64.0, 50.0
43
+ line_to 64.0, 200.0
44
+ move_to 128.0, 50.0
45
+ line_to 128.0, 200.0
46
+ move_to 192.0, 50.0
47
+ line_to 192.0, 200.0
48
+
49
+ line_width 2.56
50
+ stroke 255, 51, 51
51
+ }
52
+ }
53
+ }.show
@@ -0,0 +1,43 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Set line join'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ # The main code
13
+ path {
14
+ move_to 76.8, 84.48
15
+ rel_line_to 51.2, -51.2
16
+ rel_line_to 51.2, 51.2
17
+
18
+ line_join Cairo::LINE_JOIN_MITER # default
19
+ line_width 40.96
20
+ stroke 0, 0, 0
21
+ }
22
+
23
+ path {
24
+ move_to 76.8, 161.28
25
+ rel_line_to 51.2, -51.2
26
+ rel_line_to 51.2, 51.2
27
+
28
+ line_join Cairo::LINE_JOIN_BEVEL
29
+ line_width 40.96
30
+ stroke 0, 0, 0
31
+ }
32
+
33
+ path {
34
+ move_to 76.8, 238.08
35
+ rel_line_to 51.2, -51.2
36
+ rel_line_to 51.2, 51.2
37
+
38
+ line_join Cairo::LINE_JOIN_ROUND
39
+ line_width 40.96
40
+ stroke 0, 0, 0
41
+ }
42
+ }
43
+ }.show
@@ -0,0 +1,46 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ include Glimmer
4
+
5
+ window {
6
+ title 'Text'
7
+ default_size 256, 256
8
+
9
+ drawing_area {
10
+ paint 242.25, 242.25, 242.25
11
+
12
+ font_family = OS.linux? ? 'Sans' : (OS.mac? ? 'Helvetica' : 'Arial')
13
+
14
+ # The main code
15
+ path {
16
+ move_to 10.0, 135.0
17
+ show_text 'Hello'
18
+
19
+ font_face font_family, Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD
20
+ font_size 90.0
21
+ line_width 2.56
22
+ fill 0, 0, 0
23
+ stroke 0, 0, 0
24
+ }
25
+
26
+ path {
27
+ move_to 70.0, 165.0
28
+ text_path 'void'
29
+
30
+ font_face font_family, Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD
31
+ font_size 90.0
32
+ line_width 2.56
33
+ fill 127.5, 127.5, 255
34
+ stroke 0, 0, 0
35
+ }
36
+
37
+ # draw helping lines
38
+ path {
39
+ arc 10.0, 135.0, 5.12, 0, 2*Math::PI
40
+ close_path
41
+ arc 70.0, 165.0, 5.12, 0, 2*Math::PI
42
+
43
+ fill 255, 51, 51, 0.6
44
+ }
45
+ }
46
+ }.show
@@ -215,6 +215,10 @@ class Tetris
215
215
  self.up_arrow_action == :instant_down
216
216
  end
217
217
 
218
+ def instant_down_on_up!
219
+ self.up_arrow_action = :instant_down
220
+ end
221
+
218
222
  def rotate_right_on_up=(value)
219
223
  self.up_arrow_action = :rotate_right if value
220
224
  end
@@ -223,6 +227,10 @@ class Tetris
223
227
  self.up_arrow_action == :rotate_right
224
228
  end
225
229
 
230
+ def rotate_right_on_up!
231
+ self.up_arrow_action = :rotate_right
232
+ end
233
+
226
234
  def rotate_left_on_up=(value)
227
235
  self.up_arrow_action = :rotate_left if value
228
236
  end
@@ -231,6 +239,10 @@ class Tetris
231
239
  self.up_arrow_action == :rotate_left
232
240
  end
233
241
 
242
+ def rotate_left_on_up!
243
+ self.up_arrow_action = :rotate_left
244
+ end
245
+
234
246
  def reset_tetrominoes
235
247
  @tetrominoes = []
236
248
  end
@@ -26,7 +26,14 @@ class Tetris
26
26
  default_size Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE # + 98
27
27
 
28
28
  box(:vertical) {
29
- @playfield_blocks = playfield(playfield_width: @game.playfield_width, playfield_height: @game.playfield_height, block_size: BLOCK_SIZE)
29
+ tetris_menu_bar
30
+
31
+ box(:horizontal) {
32
+ @playfield_blocks = playfield(playfield_width: @game.playfield_width, playfield_height: @game.playfield_height, block_size: BLOCK_SIZE)
33
+
34
+ score_board
35
+ }
36
+
30
37
  }
31
38
 
32
39
  on(:key_press_event) do |widget, key_event|
@@ -84,6 +91,135 @@ class Tetris
84
91
  end
85
92
  end
86
93
  end
94
+
95
+ Model::Game::PREVIEW_PLAYFIELD_HEIGHT.times do |row|
96
+ Model::Game::PREVIEW_PLAYFIELD_WIDTH.times do |column|
97
+ observe(@game.preview_playfield[row][column], :color) do |new_color|
98
+ color = new_color
99
+ block = @preview_playfield_blocks[row][column]
100
+ block[:background_square].fill = color
101
+ block[:top_bevel_edge].fill = [color[0] + 4*BEVEL_CONSTANT, color[1] + 4*BEVEL_CONSTANT, color[2] + 4*BEVEL_CONSTANT]
102
+ block[:right_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
103
+ block[:bottom_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
104
+ block[:left_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
105
+ block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
106
+ block[:drawing_area].queue_draw
107
+ end
108
+ end
109
+ end
110
+
111
+ observe(@game, :score) do |new_score|
112
+ @score_label.text = new_score.to_s
113
+ end
114
+
115
+ observe(@game, :lines) do |new_lines|
116
+ @lines_label.text = new_lines.to_s
117
+ end
118
+
119
+ observe(@game, :level) do |new_level|
120
+ @level_label.text = new_level.to_s
121
+ end
122
+ end
123
+
124
+ def tetris_menu_bar
125
+ menu_bar {
126
+ menu_item(label: 'Game') { |mi|
127
+ m = menu {
128
+ check_menu_item(label: 'Pause') {
129
+ on(:activate) do
130
+ @game.paused = !@game.paused?
131
+ end
132
+ }
133
+
134
+ menu_item(label: 'Restart') {
135
+ on(:activate) do
136
+ @game.restart!
137
+ end
138
+ }
139
+
140
+ separator_menu_item
141
+
142
+ menu_item(label: 'Exit') {
143
+ on(:activate) do
144
+ @main_window.close
145
+ end
146
+ }
147
+ }
148
+ mi.submenu = m.gtk
149
+ }
150
+
151
+ menu_item(label: 'View') { |mi|
152
+ m = menu {
153
+ menu_item(label: 'Show High Scores') {
154
+ on(:activate) do
155
+ show_high_score_dialog
156
+ end
157
+ }
158
+
159
+ menu_item(label: 'Clear High Scores') {
160
+ on(:activate) do
161
+ @game.clear_high_scores!
162
+ end
163
+ }
164
+ }
165
+ mi.submenu = m.gtk
166
+ }
167
+
168
+ menu_item(label: 'Options') { |mi|
169
+ m = menu {
170
+ rmi = radio_menu_item(nil, 'Instant Down on Up') {
171
+ on(:activate) do
172
+ @game.instant_down_on_up!
173
+ end
174
+ }
175
+
176
+ default_rmi = radio_menu_item(rmi.group, 'Rotate Right on Up') {
177
+ on(:activate) do
178
+ @game.rotate_right_on_up!
179
+ end
180
+ }
181
+ default_rmi.activate
182
+
183
+ radio_menu_item(rmi.group, 'Rotate Left on Up') {
184
+ on(:activate) do
185
+ @game.rotate_left_on_up!
186
+ end
187
+ }
188
+ }
189
+ mi.submenu = m.gtk
190
+ }
191
+
192
+ menu_item(label: 'Options') { |mi|
193
+ m = menu {
194
+ menu_item(label: 'About') {
195
+ on(:activate) do
196
+ show_about_dialog
197
+ end
198
+ }
199
+ }
200
+ mi.submenu = m.gtk
201
+ }
202
+ }
203
+ end
204
+
205
+ def score_board
206
+ box(:vertical) {
207
+ label
208
+ @preview_playfield_blocks = playfield(playfield_width: Model::Game::PREVIEW_PLAYFIELD_WIDTH, playfield_height: Model::Game::PREVIEW_PLAYFIELD_HEIGHT, block_size: BLOCK_SIZE)
209
+
210
+ label
211
+ label('Score')
212
+ @score_label = label
213
+
214
+ label
215
+ label('Lines')
216
+ @lines_label = label
217
+
218
+ label
219
+ label('Level')
220
+ @level_label = label
221
+ label
222
+ }
87
223
  end
88
224
 
89
225
  def playfield(playfield_width: , playfield_height: , block_size: , &extra_content)
@@ -162,6 +298,41 @@ class Tetris
162
298
  @game.restart!
163
299
  false
164
300
  end
301
+
302
+ def show_high_score_dialog
303
+ game_paused = !!@game.paused
304
+ @game.paused = true
305
+
306
+ if @game.high_scores.empty?
307
+ high_scores_string = "No games have been scored yet."
308
+ else
309
+ high_scores_string = @game.high_scores.map do |high_score|
310
+ "#{high_score.name} | Score: #{high_score.score} | Lines: #{high_score.lines} | Level: #{high_score.level}"
311
+ end.join("\n")
312
+ end
313
+
314
+ message_dialog(@main_window) { |md|
315
+ title 'High Scores'
316
+ text high_scores_string
317
+
318
+ on(:response) do
319
+ md.destroy
320
+ end
321
+ }.show
322
+
323
+ @game.paused = game_paused
324
+ end
325
+
326
+ def show_about_dialog
327
+ message_dialog(@main_window) { |md|
328
+ title 'About'
329
+ text "Glimmer Tetris\n\nGlimmer DSL for GTK\n\nElaborate Sample\n\nCopyright (c) 2021-2022 Andy Maleh"
330
+
331
+ on(:response) do
332
+ md.destroy
333
+ end
334
+ }.show
335
+ end
165
336
  end
166
337
 
167
338
  Tetris.new.launch
@@ -7,9 +7,7 @@ window {
7
7
  default_size 400, 400
8
8
 
9
9
  drawing_area {
10
- rectangle(0, 0, 400, 400) {
11
- fill 255, 255, 255
12
- }
10
+ paint 255, 255, 255
13
11
 
14
12
  arc(85, 85, 45, (Math::PI/180)*90, -(Math::PI/180)*90) {
15
13
  fill 255, 0, 0