glimmer-dsl-swt 4.22.1.2 → 4.22.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +12 -9
  5. data/RUBY_VERSION +1 -1
  6. data/VERSION +1 -1
  7. data/bin/girb +1 -1
  8. data/bin/girb_runner.rb +1 -1
  9. data/bin/glimmer +1 -1
  10. data/bin/glimmer-setup +1 -1
  11. data/docs/reference/GLIMMER_COMMAND.md +2 -2
  12. data/docs/reference/GLIMMER_CONFIGURATION.md +1 -1
  13. data/docs/reference/GLIMMER_GIRB.md +1 -1
  14. data/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md +146 -1
  15. data/docs/reference/GLIMMER_PACKAGING_AND_DISTRIBUTION.md +13 -32
  16. data/docs/reference/GLIMMER_SAMPLES.md +16 -1
  17. data/docs/reference/GLIMMER_STYLE_GUIDE.md +6 -0
  18. data/glimmer-dsl-swt.gemspec +0 -0
  19. data/lib/ext/glimmer/config.rb +1 -1
  20. data/lib/ext/glimmer.rb +4 -4
  21. data/lib/ext/rouge/themes/glimmer.rb +21 -0
  22. data/lib/glimmer/Rakefile +6 -6
  23. data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
  24. data/lib/glimmer/data_binding/observable_widget.rb +1 -1
  25. data/lib/glimmer/data_binding/table_items_binding.rb +1 -1
  26. data/lib/glimmer/data_binding/tree_items_binding.rb +1 -1
  27. data/lib/glimmer/data_binding/widget_binding.rb +1 -1
  28. data/lib/glimmer/dsl/swt/animation_expression.rb +1 -1
  29. data/lib/glimmer/dsl/swt/async_exec_expression.rb +1 -1
  30. data/lib/glimmer/dsl/swt/auto_exec_expression.rb +1 -1
  31. data/lib/glimmer/dsl/swt/bind_expression.rb +1 -1
  32. data/lib/glimmer/dsl/swt/block_property_expression.rb +1 -1
  33. data/lib/glimmer/dsl/swt/c_tab_item_expression.rb +1 -1
  34. data/lib/glimmer/dsl/swt/checkbox_group_selection_data_binding_expression.rb +1 -1
  35. data/lib/glimmer/dsl/swt/color_expression.rb +1 -1
  36. data/lib/glimmer/dsl/swt/column_properties_expression.rb +1 -1
  37. data/lib/glimmer/dsl/swt/combo_selection_data_binding_expression.rb +1 -1
  38. data/lib/glimmer/dsl/swt/cursor_expression.rb +1 -1
  39. data/lib/glimmer/dsl/swt/custom_shape_expression.rb +1 -1
  40. data/lib/glimmer/dsl/swt/custom_widget_expression.rb +1 -1
  41. data/lib/glimmer/dsl/swt/data_binding_expression.rb +1 -1
  42. data/lib/glimmer/dsl/swt/dialog_expression.rb +1 -1
  43. data/lib/glimmer/dsl/swt/display_expression.rb +1 -1
  44. data/lib/glimmer/dsl/swt/dnd_expression.rb +1 -1
  45. data/lib/glimmer/dsl/swt/dsl.rb +1 -1
  46. data/lib/glimmer/dsl/swt/exec_expression.rb +2 -3
  47. data/lib/glimmer/dsl/swt/expand_item_expression.rb +1 -1
  48. data/lib/glimmer/dsl/swt/font_expression.rb +1 -1
  49. data/lib/glimmer/dsl/swt/image_expression.rb +1 -1
  50. data/lib/glimmer/dsl/swt/layout_data_expression.rb +1 -1
  51. data/lib/glimmer/dsl/swt/layout_expression.rb +1 -1
  52. data/lib/glimmer/dsl/swt/list_selection_data_binding_expression.rb +1 -1
  53. data/lib/glimmer/dsl/swt/listener_expression.rb +1 -1
  54. data/lib/glimmer/dsl/swt/menu_bar_expression.rb +1 -1
  55. data/lib/glimmer/dsl/swt/menu_expression.rb +1 -1
  56. data/lib/glimmer/dsl/swt/message_box_expression.rb +1 -1
  57. data/lib/glimmer/dsl/swt/multiply_expression.rb +1 -1
  58. data/lib/glimmer/dsl/swt/observe_expression.rb +1 -1
  59. data/lib/glimmer/dsl/swt/pixel_expression.rb +1 -1
  60. data/lib/glimmer/dsl/swt/property_expression.rb +1 -1
  61. data/lib/glimmer/dsl/swt/radio_group_selection_data_binding_expression.rb +1 -1
  62. data/lib/glimmer/dsl/swt/rgb_expression.rb +1 -1
  63. data/lib/glimmer/dsl/swt/rgba_expression.rb +1 -1
  64. data/lib/glimmer/dsl/swt/shape_expression.rb +1 -1
  65. data/lib/glimmer/dsl/swt/shell_expression.rb +1 -1
  66. data/lib/glimmer/dsl/swt/shine_data_binding_expression.rb +1 -1
  67. data/lib/glimmer/dsl/swt/swt_expression.rb +1 -1
  68. data/lib/glimmer/dsl/swt/sync_call_expression.rb +1 -1
  69. data/lib/glimmer/dsl/swt/sync_exec_expression.rb +1 -1
  70. data/lib/glimmer/dsl/swt/tab_item_expression.rb +1 -1
  71. data/lib/glimmer/dsl/swt/table_items_data_binding_expression.rb +1 -1
  72. data/lib/glimmer/dsl/swt/timer_exec_expression.rb +1 -1
  73. data/lib/glimmer/dsl/swt/transform_expression.rb +1 -1
  74. data/lib/glimmer/dsl/swt/tray_expression.rb +1 -1
  75. data/lib/glimmer/dsl/swt/tray_item_expression.rb +1 -1
  76. data/lib/glimmer/dsl/swt/tree_items_data_binding_expression.rb +1 -1
  77. data/lib/glimmer/dsl/swt/tree_properties_expression.rb +1 -1
  78. data/lib/glimmer/dsl/swt/widget_expression.rb +1 -1
  79. data/lib/glimmer/launcher.rb +1 -1
  80. data/lib/glimmer/rake_task/list.rb +1 -1
  81. data/lib/glimmer/rake_task/package.rb +1 -1
  82. data/lib/glimmer/rake_task/scaffold.rb +1 -3
  83. data/lib/glimmer/rake_task.rb +1 -1
  84. data/lib/glimmer/swt/c_tab_item_proxy.rb +1 -1
  85. data/lib/glimmer/swt/color_proxy.rb +1 -1
  86. data/lib/glimmer/swt/combo_proxy.rb +1 -1
  87. data/lib/glimmer/swt/cursor_proxy.rb +1 -1
  88. data/lib/glimmer/swt/custom/animation.rb +1 -1
  89. data/lib/glimmer/swt/custom/checkbox_group.rb +1 -1
  90. data/lib/glimmer/swt/custom/code_text.rb +1 -1
  91. data/lib/glimmer/swt/custom/drawable.rb +1 -1
  92. data/lib/glimmer/swt/custom/radio_group.rb +1 -1
  93. data/lib/glimmer/swt/custom/shape/arc.rb +1 -1
  94. data/lib/glimmer/swt/custom/shape/cubic.rb +1 -1
  95. data/lib/glimmer/swt/custom/shape/focus.rb +1 -1
  96. data/lib/glimmer/swt/custom/shape/image.rb +1 -1
  97. data/lib/glimmer/swt/custom/shape/line.rb +1 -1
  98. data/lib/glimmer/swt/custom/shape/oval.rb +1 -1
  99. data/lib/glimmer/swt/custom/shape/path.rb +1 -1
  100. data/lib/glimmer/swt/custom/shape/path_segment.rb +1 -1
  101. data/lib/glimmer/swt/custom/shape/point.rb +1 -1
  102. data/lib/glimmer/swt/custom/shape/polygon.rb +1 -1
  103. data/lib/glimmer/swt/custom/shape/polyline.rb +1 -1
  104. data/lib/glimmer/swt/custom/shape/quad.rb +1 -1
  105. data/lib/glimmer/swt/custom/shape/rectangle.rb +1 -1
  106. data/lib/glimmer/swt/custom/shape/text.rb +1 -1
  107. data/lib/glimmer/swt/custom/shape.rb +1 -1
  108. data/lib/glimmer/swt/date_time_proxy.rb +1 -1
  109. data/lib/glimmer/swt/dialog_proxy.rb +1 -1
  110. data/lib/glimmer/swt/display_proxy.rb +1 -1
  111. data/lib/glimmer/swt/dnd_proxy.rb +1 -1
  112. data/lib/glimmer/swt/expand_item_proxy.rb +1 -1
  113. data/lib/glimmer/swt/font_proxy.rb +1 -1
  114. data/lib/glimmer/swt/image_proxy.rb +1 -1
  115. data/lib/glimmer/swt/layout_data_proxy.rb +5 -1
  116. data/lib/glimmer/swt/layout_proxy.rb +1 -1
  117. data/lib/glimmer/swt/menu_proxy.rb +1 -1
  118. data/lib/glimmer/swt/message_box_proxy.rb +1 -1
  119. data/lib/glimmer/swt/packages.rb +1 -1
  120. data/lib/glimmer/swt/properties.rb +1 -1
  121. data/lib/glimmer/swt/proxy_properties.rb +1 -1
  122. data/lib/glimmer/swt/sash_form_proxy.rb +1 -1
  123. data/lib/glimmer/swt/scrolled_composite_proxy.rb +1 -1
  124. data/lib/glimmer/swt/shape_listener_proxy.rb +1 -1
  125. data/lib/glimmer/swt/shell_proxy.rb +1 -1
  126. data/lib/glimmer/swt/style_constantizable.rb +1 -1
  127. data/lib/glimmer/swt/styled_text_proxy.rb +1 -1
  128. data/lib/glimmer/swt/swt_proxy.rb +1 -1
  129. data/lib/glimmer/swt/tab_folder_proxy.rb +1 -1
  130. data/lib/glimmer/swt/tab_item_proxy.rb +1 -1
  131. data/lib/glimmer/swt/table_column_proxy.rb +1 -1
  132. data/lib/glimmer/swt/table_proxy.rb +1 -1
  133. data/lib/glimmer/swt/tool_bar_proxy.rb +1 -1
  134. data/lib/glimmer/swt/transform_proxy.rb +1 -1
  135. data/lib/glimmer/swt/tray_item_proxy.rb +1 -1
  136. data/lib/glimmer/swt/tray_proxy.rb +1 -1
  137. data/lib/glimmer/swt/tree_proxy.rb +1 -1
  138. data/lib/glimmer/swt/widget_listener_proxy.rb +1 -1
  139. data/lib/glimmer/swt/widget_proxy.rb +1 -1
  140. data/lib/glimmer/ui/custom_shape.rb +1 -1
  141. data/lib/glimmer/ui/custom_shell.rb +1 -1
  142. data/lib/glimmer/ui/custom_widget.rb +1 -1
  143. data/lib/glimmer/ui.rb +21 -0
  144. data/lib/glimmer/util/proc_tracker.rb +4 -4
  145. data/lib/glimmer-dsl-swt.rb +1 -1
  146. data/samples/elaborate/battleship/model/cell.rb +1 -1
  147. data/samples/elaborate/battleship/model/game.rb +1 -1
  148. data/samples/elaborate/battleship/model/grid.rb +1 -1
  149. data/samples/elaborate/battleship/model/ship.rb +1 -1
  150. data/samples/elaborate/battleship/model/ship_collection.rb +1 -1
  151. data/samples/elaborate/battleship/view/action_panel.rb +1 -1
  152. data/samples/elaborate/battleship/view/cell.rb +1 -1
  153. data/samples/elaborate/battleship/view/grid.rb +1 -1
  154. data/samples/elaborate/battleship/view/ship.rb +1 -1
  155. data/samples/elaborate/battleship/view/ship_collection.rb +1 -1
  156. data/samples/elaborate/battleship.rb +1 -1
  157. data/samples/elaborate/calculator.rb +1 -1
  158. data/samples/elaborate/clock.rb +1 -1
  159. data/samples/elaborate/connect4/model/grid.rb +1 -1
  160. data/samples/elaborate/connect4/model/slot.rb +1 -1
  161. data/samples/elaborate/connect4.rb +1 -1
  162. data/samples/elaborate/contact_manager/contact.rb +1 -1
  163. data/samples/elaborate/contact_manager/contact_manager_presenter.rb +1 -1
  164. data/samples/elaborate/contact_manager/contact_repository.rb +1 -1
  165. data/samples/elaborate/contact_manager.rb +1 -1
  166. data/samples/elaborate/game_of_life/model/cell.rb +1 -1
  167. data/samples/elaborate/game_of_life/model/grid.rb +1 -1
  168. data/samples/elaborate/game_of_life.rb +1 -1
  169. data/samples/elaborate/login.rb +1 -1
  170. data/samples/elaborate/mandelbrot_fractal.rb +1 -1
  171. data/samples/elaborate/meta_sample.rb +1 -1
  172. data/samples/elaborate/metronome.rb +1 -1
  173. data/samples/elaborate/parking/model/parking_floor.rb +1 -1
  174. data/samples/elaborate/parking/model/parking_presenter.rb +1 -1
  175. data/samples/elaborate/parking/model/parking_spot.rb +1 -1
  176. data/samples/elaborate/parking.rb +1 -1
  177. data/samples/elaborate/quarto/model/game.rb +1 -1
  178. data/samples/elaborate/quarto/model/piece/cube.rb +1 -1
  179. data/samples/elaborate/quarto/model/piece/cylinder.rb +1 -1
  180. data/samples/elaborate/quarto/model/piece.rb +1 -1
  181. data/samples/elaborate/quarto/view/available_pieces_area.rb +1 -1
  182. data/samples/elaborate/quarto/view/board.rb +1 -1
  183. data/samples/elaborate/quarto/view/cell.rb +1 -1
  184. data/samples/elaborate/quarto/view/cube.rb +1 -1
  185. data/samples/elaborate/quarto/view/cylinder.rb +1 -1
  186. data/samples/elaborate/quarto/view/message_box_panel.rb +1 -1
  187. data/samples/elaborate/quarto/view/piece.rb +1 -1
  188. data/samples/elaborate/quarto/view/selected_piece_area.rb +1 -1
  189. data/samples/elaborate/quarto.rb +1 -1
  190. data/samples/elaborate/snake/model/apple.rb +33 -0
  191. data/samples/elaborate/snake/model/game.rb +68 -0
  192. data/samples/elaborate/snake/model/snake.rb +95 -0
  193. data/samples/elaborate/snake/model/vertebra.rb +22 -0
  194. data/samples/elaborate/snake/presenter/cell.rb +27 -0
  195. data/samples/elaborate/snake/presenter/grid.rb +49 -0
  196. data/samples/elaborate/snake.rb +103 -0
  197. data/samples/elaborate/stock_ticker.rb +1 -1
  198. data/samples/elaborate/tetris/model/block.rb +1 -1
  199. data/samples/elaborate/tetris/model/game.rb +1 -1
  200. data/samples/elaborate/tetris/model/past_game.rb +1 -1
  201. data/samples/elaborate/tetris/model/tetromino.rb +1 -1
  202. data/samples/elaborate/tetris/view/bevel.rb +1 -1
  203. data/samples/elaborate/tetris/view/block.rb +1 -1
  204. data/samples/elaborate/tetris/view/high_score_dialog.rb +1 -1
  205. data/samples/elaborate/tetris/view/playfield.rb +1 -1
  206. data/samples/elaborate/tetris/view/score_lane.rb +1 -1
  207. data/samples/elaborate/tetris/view/tetris_menu_bar.rb +1 -1
  208. data/samples/elaborate/tetris.rb +8 -8
  209. data/samples/elaborate/tic_tac_toe/board.rb +1 -1
  210. data/samples/elaborate/tic_tac_toe/cell.rb +1 -1
  211. data/samples/elaborate/tic_tac_toe.rb +1 -1
  212. data/samples/elaborate/timer.rb +1 -1
  213. data/samples/elaborate/user_profile.rb +1 -1
  214. data/samples/elaborate/weather.rb +1 -1
  215. data/samples/hello/hello_arrow.rb +1 -1
  216. data/samples/hello/hello_browser.rb +1 -1
  217. data/samples/hello/hello_button.rb +1 -1
  218. data/samples/hello/hello_c_combo.rb +1 -1
  219. data/samples/hello/hello_c_tab.rb +1 -1
  220. data/samples/hello/hello_canvas.rb +1 -1
  221. data/samples/hello/hello_canvas_animation.rb +1 -1
  222. data/samples/hello/hello_canvas_animation_multi.rb +1 -1
  223. data/samples/hello/hello_canvas_data_binding.rb +1 -1
  224. data/samples/hello/hello_canvas_path.rb +1 -1
  225. data/samples/hello/hello_canvas_shape_listeners.rb +1 -1
  226. data/samples/hello/hello_checkbox.rb +1 -1
  227. data/samples/hello/hello_checkbox_group.rb +1 -1
  228. data/samples/hello/hello_code_text.rb +1 -1
  229. data/samples/hello/hello_color_dialog.rb +1 -1
  230. data/samples/hello/hello_combo.rb +1 -1
  231. data/samples/hello/hello_composite.rb +1 -1
  232. data/samples/hello/hello_computed.rb +1 -1
  233. data/samples/hello/hello_cool_bar.rb +1 -1
  234. data/samples/hello/hello_cursor.rb +1 -1
  235. data/samples/hello/hello_custom_shape.rb +1 -1
  236. data/samples/hello/hello_custom_shell.rb +1 -1
  237. data/samples/hello/hello_custom_widget.rb +1 -1
  238. data/samples/hello/hello_date_time.rb +1 -1
  239. data/samples/hello/hello_dialog.rb +1 -1
  240. data/samples/hello/hello_directory_dialog.rb +1 -1
  241. data/samples/hello/hello_drag_and_drop.rb +1 -1
  242. data/samples/hello/hello_expand_bar.rb +1 -1
  243. data/samples/hello/hello_file_dialog.rb +1 -1
  244. data/samples/hello/hello_font_dialog.rb +1 -1
  245. data/samples/hello/hello_group.rb +1 -1
  246. data/samples/hello/hello_label.rb +1 -1
  247. data/samples/hello/hello_layout.rb +1 -1
  248. data/samples/hello/hello_link.rb +1 -1
  249. data/samples/hello/hello_list_multi_selection.rb +1 -1
  250. data/samples/hello/hello_list_single_selection.rb +1 -1
  251. data/samples/hello/hello_menu_bar.rb +1 -1
  252. data/samples/hello/hello_message_box.rb +1 -1
  253. data/samples/hello/hello_pop_up_context_menu.rb +1 -1
  254. data/samples/hello/hello_print.rb +1 -1
  255. data/samples/hello/hello_print_dialog.rb +1 -1
  256. data/samples/hello/hello_progress_bar.rb +1 -1
  257. data/samples/hello/hello_radio.rb +1 -1
  258. data/samples/hello/hello_radio_group.rb +1 -1
  259. data/samples/hello/hello_sash_form.rb +1 -1
  260. data/samples/hello/hello_scale.rb +1 -1
  261. data/samples/hello/hello_scrolled_composite.rb +1 -1
  262. data/samples/hello/hello_shape.rb +1 -1
  263. data/samples/hello/hello_shell.rb +1 -1
  264. data/samples/hello/hello_slider.rb +1 -1
  265. data/samples/hello/hello_spinner.rb +1 -1
  266. data/samples/hello/hello_styled_text.rb +1 -1
  267. data/samples/hello/hello_tab.rb +1 -1
  268. data/samples/hello/hello_table.rb +1 -1
  269. data/samples/hello/hello_text.rb +1 -1
  270. data/samples/hello/hello_toggle.rb +1 -1
  271. data/samples/hello/hello_tool_bar.rb +1 -1
  272. data/samples/hello/hello_tray_item.rb +1 -1
  273. data/samples/hello/hello_tree.rb +1 -1
  274. data/samples/hello/hello_world.rb +1 -1
  275. metadata +11 -18
@@ -0,0 +1,33 @@
1
+ class Snake
2
+ module Model
3
+ class Apple
4
+ attr_reader :game
5
+ attr_accessor :row, :column
6
+
7
+ def initialize(game)
8
+ @game = game
9
+ end
10
+
11
+ # generates a new location from scratch or via dependency injection of what cell is (for testing purposes)
12
+ def generate(initial_row: nil, initial_column: nil)
13
+ if initial_row && initial_column
14
+ self.row, self.column = initial_row, initial_column
15
+ else
16
+ self.row, self.column = @game.height.times.zip(@game.width.times).reject do |row, column|
17
+ @game.snake.vertebrae.map {|v| [v.row, v.column]}.include?([row, column])
18
+ end.sample
19
+ end
20
+ end
21
+
22
+ def remove
23
+ self.row = nil
24
+ self.column = nil
25
+ end
26
+
27
+ # inspect is overridden to prevent printing very long stack traces
28
+ def inspect
29
+ "#{super[0, 120]}... >"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,68 @@
1
+ require 'fileutils'
2
+
3
+ require_relative 'snake'
4
+ require_relative 'apple'
5
+
6
+ class Snake
7
+ module Model
8
+ class Game
9
+ WIDTH_DEFAULT = 20
10
+ HEIGHT_DEFAULT = 20
11
+ FILE_HIGH_SCORE = File.expand_path(File.join(Dir.home, '.glimmer-snake'))
12
+
13
+ attr_reader :width, :height
14
+ attr_accessor :snake, :apple, :over, :score, :high_score, :paused
15
+ alias over? over
16
+ alias paused? paused
17
+
18
+ def initialize(width = WIDTH_DEFAULT, height = HEIGHT_DEFAULT)
19
+ @width = width
20
+ @height = height
21
+ @snake = Snake.new(self)
22
+ @apple = Apple.new(self)
23
+ FileUtils.touch(FILE_HIGH_SCORE)
24
+ @high_score = File.read(FILE_HIGH_SCORE).to_i rescue 0
25
+ end
26
+
27
+ def score=(new_score)
28
+ @score = new_score
29
+ self.high_score = @score if @score > @high_score
30
+ end
31
+
32
+ def high_score=(new_high_score)
33
+ @high_score = new_high_score
34
+ File.write(FILE_HIGH_SCORE, @high_score.to_s)
35
+ rescue => e
36
+ puts e.full_message
37
+ end
38
+
39
+ def start
40
+ self.over = false
41
+ self.score = 0
42
+ self.snake.generate
43
+ self.apple.generate
44
+ end
45
+
46
+ def pause
47
+ self.paused = true
48
+ end
49
+
50
+ def resume
51
+ self.paused = false
52
+ end
53
+
54
+ def toggle_pause
55
+ unless paused?
56
+ pause
57
+ else
58
+ resume
59
+ end
60
+ end
61
+
62
+ # inspect is overridden to prevent printing very long stack traces
63
+ def inspect
64
+ "#{super[0, 75]}... >"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,95 @@
1
+ require_relative 'vertebra'
2
+
3
+ class Snake
4
+ module Model
5
+ class Snake
6
+ SCORE_EAT_APPLE = 50
7
+ RIGHT_TURN_MAP = {
8
+ north: :east,
9
+ east: :south,
10
+ south: :west,
11
+ west: :north
12
+ }
13
+ LEFT_TURN_MAP = RIGHT_TURN_MAP.invert
14
+
15
+ attr_accessor :collided
16
+ alias collided? collided
17
+
18
+ attr_reader :game
19
+ # vertebrae and joins are ordered from tail to head
20
+ attr_accessor :vertebrae
21
+
22
+ def initialize(game)
23
+ @game = game
24
+ end
25
+
26
+ # generates a new snake location and orientation from scratch or via dependency injection of what head_cell and orientation are (for testing purposes)
27
+ def generate(initial_row: nil, initial_column: nil, initial_orientation: nil)
28
+ self.collided = false
29
+ initial_vertebra = Vertebra.new(snake: self, row: initial_row, column: initial_column, orientation: initial_orientation)
30
+ self.vertebrae = [initial_vertebra]
31
+ end
32
+
33
+ def length
34
+ @vertebrae.length
35
+ end
36
+
37
+ def head
38
+ @vertebrae.last
39
+ end
40
+
41
+ def tail
42
+ @vertebrae.first
43
+ end
44
+
45
+ def remove
46
+ self.vertebrae.clear
47
+ self.joins.clear
48
+ end
49
+
50
+ def move
51
+ @old_tail = tail.dup
52
+ @new_head = head.dup
53
+ case @new_head.orientation
54
+ when :east
55
+ @new_head.column = (@new_head.column + 1) % @game.width
56
+ when :west
57
+ @new_head.column = (@new_head.column - 1) % @game.width
58
+ when :south
59
+ @new_head.row = (@new_head.row + 1) % @game.height
60
+ when :north
61
+ @new_head.row = (@new_head.row - 1) % @game.height
62
+ end
63
+ if @vertebrae.map {|v| [v.row, v.column]}.include?([@new_head.row, @new_head.column])
64
+ self.collided = true
65
+ @game.over = true
66
+ else
67
+ @vertebrae.append(@new_head)
68
+ @vertebrae.delete(tail)
69
+ if head.row == @game.apple.row && head.column == @game.apple.column
70
+ grow
71
+ @game.apple.generate
72
+ end
73
+ end
74
+ end
75
+
76
+ def turn_right
77
+ head.orientation = RIGHT_TURN_MAP[head.orientation]
78
+ end
79
+
80
+ def turn_left
81
+ head.orientation = LEFT_TURN_MAP[head.orientation]
82
+ end
83
+
84
+ def grow
85
+ @game.score += SCORE_EAT_APPLE
86
+ @vertebrae.prepend(@old_tail)
87
+ end
88
+
89
+ # inspect is overridden to prevent printing very long stack traces
90
+ def inspect
91
+ "#{super[0, 150]}... >"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,22 @@
1
+ class Snake
2
+ module Model
3
+ class Vertebra
4
+ ORIENTATIONS = %i[north east south west]
5
+ # orientation is needed for snake occuppied cells (but not apple cells)
6
+ attr_reader :snake
7
+ attr_accessor :row, :column, :orientation
8
+
9
+ def initialize(snake: , row: , column: , orientation: )
10
+ @row = row || rand(snake.game.height)
11
+ @column = column || rand(snake.game.width)
12
+ @orientation = orientation || ORIENTATIONS.sample
13
+ @snake = snake
14
+ end
15
+
16
+ # inspect is overridden to prevent printing very long stack traces
17
+ def inspect
18
+ "#{super[0, 150]}... >"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ class Snake
2
+ module Presenter
3
+ class Cell
4
+ COLOR_CLEAR = :white
5
+ COLOR_SNAKE = :green
6
+ COLOR_APPLE = :red
7
+
8
+ attr_reader :row, :column, :grid
9
+ attr_accessor :color
10
+
11
+ def initialize(grid: ,row: ,column: )
12
+ @row = row
13
+ @column = column
14
+ @grid = grid
15
+ end
16
+
17
+ def clear
18
+ self.color = COLOR_CLEAR unless color == COLOR_CLEAR
19
+ end
20
+
21
+ # inspect is overridden to prevent printing very long stack traces
22
+ def inspect
23
+ "#{super[0, 150]}... >"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ require 'glimmer'
2
+ require_relative '../model/game'
3
+ require_relative 'cell'
4
+
5
+ class Snake
6
+ module Presenter
7
+ class Grid
8
+ include Glimmer
9
+
10
+ attr_reader :game, :cells
11
+
12
+ def initialize(game = Model::Game.new)
13
+ @game = game
14
+ @cells = @game.height.times.map do |row|
15
+ @game.width.times.map do |column|
16
+ Cell.new(grid: self, row: row, column: column)
17
+ end
18
+ end
19
+ observe(@game.snake, :vertebrae) do |new_vertebrae|
20
+ occupied_snake_positions = @game.snake.vertebrae.map {|v| [v.row, v.column]}
21
+ @cells.each_with_index do |row_cells, row|
22
+ row_cells.each_with_index do |cell, column|
23
+ if [@game.apple.row, @game.apple.column] == [row, column]
24
+ cell.color = Cell::COLOR_APPLE
25
+ elsif occupied_snake_positions.include?([row, column])
26
+ cell.color = Cell::COLOR_SNAKE
27
+ else
28
+ cell.clear
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def clear
36
+ @cells.each do |row_cells|
37
+ row_cells.each do |cell|
38
+ cell.clear
39
+ end
40
+ end
41
+ end
42
+
43
+ # inspect is overridden to prevent printing very long stack traces
44
+ def inspect
45
+ "#{super[0, 75]}... >"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,103 @@
1
+ require 'glimmer-dsl-swt'
2
+
3
+ require_relative 'snake/presenter/grid'
4
+
5
+ class Snake
6
+ include Glimmer::UI::CustomShell
7
+
8
+ CELL_SIZE = 15
9
+ SNAKE_MOVE_DELAY = 100 # millis
10
+
11
+ before_body do
12
+ @game = Model::Game.new
13
+ @grid = Presenter::Grid.new(@game)
14
+ @game.start
15
+ @keypress_queue = []
16
+
17
+ display {
18
+ on_swt_keydown do |key_event|
19
+ if key_event.keyCode == 32
20
+ @game.toggle_pause
21
+ else
22
+ @keypress_queue << key_event.keyCode
23
+ end
24
+ end
25
+ }
26
+ end
27
+
28
+ after_body do
29
+ register_observers
30
+ end
31
+
32
+ body {
33
+ shell(:no_resize) {
34
+ grid_layout(@game.width, true) {
35
+ margin_width 0
36
+ margin_height 0
37
+ horizontal_spacing 0
38
+ vertical_spacing 0
39
+ }
40
+
41
+ # data-bind window title to game score, converting it to a title string on read from the model
42
+ text <= [@game, :score, on_read: -> (score) {"Snake (Score: #{@game.score})"}]
43
+ minimum_size @game.width * CELL_SIZE, @game.height * CELL_SIZE
44
+
45
+ @game.height.times do |row|
46
+ @game.width.times do |column|
47
+ canvas {
48
+ layout_data {
49
+ width_hint CELL_SIZE
50
+ height_hint CELL_SIZE
51
+ }
52
+
53
+ rectangle(0, 0, CELL_SIZE, CELL_SIZE) {
54
+ background <= [@grid.cells[row][column], :color] # data-bind square fill to grid cell color
55
+ }
56
+ }
57
+ end
58
+ end
59
+ }
60
+ }
61
+
62
+ def register_observers
63
+ observe(@game, :over) do |game_over|
64
+ async_exec do
65
+ if game_over
66
+ message_box {
67
+ text 'Game Over!'
68
+ message "Score: #{@game.score} | High Score: #{@game.high_score}"
69
+ }.open
70
+ @game.start
71
+ end
72
+ end
73
+ end
74
+
75
+ timer_exec(SNAKE_MOVE_DELAY, &method(:move_snake))
76
+ end
77
+
78
+ def move_snake
79
+ unless @game.paused? || @game.over?
80
+ process_queued_keypress
81
+ @game.snake.move
82
+ end
83
+ timer_exec(SNAKE_MOVE_DELAY, &method(:move_snake))
84
+ end
85
+
86
+ def process_queued_keypress
87
+ # key press queue ensures one turn per snake move to avoid a double-turn resulting in instant death (due to snake illogically going back against itself)
88
+ key = @keypress_queue.shift
89
+ if [@game.snake.head.orientation, key] == [:north, swt(:arrow_right)] ||
90
+ [@game.snake.head.orientation, key] == [:east, swt(:arrow_down)] ||
91
+ [@game.snake.head.orientation, key] == [:south, swt(:arrow_left)] ||
92
+ [@game.snake.head.orientation, key] == [:west, swt(:arrow_up)]
93
+ @game.snake.turn_right
94
+ elsif [@game.snake.head.orientation, key] == [:north, swt(:arrow_left)] ||
95
+ [@game.snake.head.orientation, key] == [:west, swt(:arrow_down)] ||
96
+ [@game.snake.head.orientation, key] == [:south, swt(:arrow_right)] ||
97
+ [@game.snake.head.orientation, key] == [:east, swt(:arrow_up)]
98
+ @game.snake.turn_left
99
+ end
100
+ end
101
+ end
102
+
103
+ Snake.launch
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -54,7 +54,7 @@ class Tetris
54
54
  Display.app_name = 'Glimmer Tetris'
55
55
 
56
56
  display {
57
- on_swt_keydown { |key_event|
57
+ on_swt_keydown do |key_event|
58
58
  case key_event.keyCode
59
59
  when swt(:arrow_down), 's'.bytes.first
60
60
  if OS.mac?
@@ -88,16 +88,16 @@ class Tetris
88
88
  game.rotate!(:left)
89
89
  end
90
90
  end
91
- }
91
+ end
92
92
 
93
93
  # if running in app mode, set the Mac app about dialog (ignored in platforms)
94
- on_about {
94
+ on_about do
95
95
  show_about_dialog
96
- }
96
+ end
97
97
 
98
- on_quit {
98
+ on_quit do
99
99
  exit(0)
100
- }
100
+ end
101
101
  }
102
102
  end
103
103
 
@@ -185,7 +185,7 @@ class Tetris
185
185
  def show_about_dialog
186
186
  message_box {
187
187
  text 'Glimmer Tetris'
188
- message "Glimmer Tetris\n\nGlimmer DSL for SWT Sample\n\nCopyright (c) 2007-2021 Andy Maleh"
188
+ message "Glimmer Tetris\n\nGlimmer DSL for SWT Sample\n\nCopyright (c) 2007-2022 Andy Maleh"
189
189
  }.open
190
190
  end
191
191
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -202,7 +202,7 @@ class Timer
202
202
  def display_about_dialog
203
203
  message_box(body_root) {
204
204
  text 'About'
205
- message "Glimmer Timer\n\nCopyright (c) 2007-2021 Andy Maleh"
205
+ message "Glimmer Timer\n\nCopyright (c) 2007-2022 Andy Maleh"
206
206
  }.open
207
207
  end
208
208
 
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
1
+ # Copyright (c) 2007-2022 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the