glimmer-dsl-swt 4.20.13.7 → 4.20.13.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +5 -5
  4. data/VERSION +1 -1
  5. data/docs/reference/GLIMMER_SAMPLES.md +24 -0
  6. data/docs/reference/GLIMMER_STYLE_GUIDE.md +2 -1
  7. data/glimmer-dsl-swt.gemspec +0 -0
  8. data/lib/glimmer/rake_task/scaffold.rb +14 -14
  9. data/lib/glimmer/swt/custom/code_text.rb +4 -4
  10. data/samples/elaborate/calculator.rb +2 -2
  11. data/samples/elaborate/contact_manager.rb +2 -2
  12. data/samples/elaborate/game_of_life.rb +111 -0
  13. data/samples/elaborate/game_of_life/model/cell.rb +51 -0
  14. data/samples/elaborate/game_of_life/model/grid.rb +76 -0
  15. data/samples/elaborate/klondike_solitaire.rb +93 -0
  16. data/samples/elaborate/klondike_solitaire/model/column_pile.rb +60 -0
  17. data/samples/elaborate/klondike_solitaire/model/dealing_pile.rb +33 -0
  18. data/samples/elaborate/klondike_solitaire/model/dealt_pile.rb +25 -0
  19. data/samples/elaborate/klondike_solitaire/model/foundation_pile.rb +40 -0
  20. data/samples/elaborate/klondike_solitaire/model/game.rb +37 -0
  21. data/samples/elaborate/klondike_solitaire/model/playing_card.rb +86 -0
  22. data/samples/elaborate/klondike_solitaire/view/action_panel.rb +30 -0
  23. data/samples/elaborate/klondike_solitaire/view/column_pile.rb +66 -0
  24. data/samples/elaborate/klondike_solitaire/view/dealing_pile.rb +36 -0
  25. data/samples/elaborate/klondike_solitaire/view/dealt_pile.rb +38 -0
  26. data/samples/elaborate/klondike_solitaire/view/empty_playing_card.rb +33 -0
  27. data/samples/elaborate/klondike_solitaire/view/foundation_pile.rb +59 -0
  28. data/samples/elaborate/klondike_solitaire/view/hidden_playing_card.rb +26 -0
  29. data/samples/elaborate/klondike_solitaire/view/playing_card.rb +35 -0
  30. data/samples/elaborate/klondike_solitaire/view/tableau.rb +41 -0
  31. data/samples/elaborate/login.rb +2 -2
  32. data/samples/elaborate/mandelbrot_fractal.rb +12 -12
  33. data/samples/elaborate/meta_sample.rb +2 -2
  34. data/samples/elaborate/metronome.rb +2 -2
  35. data/samples/elaborate/stock_ticker.rb +8 -8
  36. data/samples/elaborate/tetris.rb +4 -4
  37. data/samples/elaborate/tetris/view/bevel.rb +2 -2
  38. data/samples/elaborate/tetris/view/score_lane.rb +2 -2
  39. data/samples/elaborate/tic_tac_toe.rb +6 -6
  40. data/samples/elaborate/timer.rb +4 -4
  41. data/samples/elaborate/weather.rb +4 -4
  42. data/samples/hello/hello_button.rb +2 -2
  43. data/samples/hello/hello_c_combo.rb +2 -2
  44. data/samples/hello/hello_canvas.rb +4 -4
  45. data/samples/hello/hello_canvas_animation.rb +2 -2
  46. data/samples/hello/hello_canvas_data_binding.rb +2 -2
  47. data/samples/hello/hello_checkbox.rb +2 -2
  48. data/samples/hello/hello_checkbox_group.rb +2 -2
  49. data/samples/hello/hello_code_text.rb +2 -2
  50. data/samples/hello/hello_color_dialog.rb +2 -2
  51. data/samples/hello/hello_combo.rb +2 -2
  52. data/samples/hello/hello_computed.rb +14 -22
  53. data/samples/hello/hello_cool_bar.rb +2 -2
  54. data/samples/hello/hello_cursor.rb +2 -2
  55. data/samples/hello/hello_custom_shape.rb +2 -2
  56. data/samples/hello/hello_custom_shell.rb +2 -2
  57. data/samples/hello/hello_custom_widget.rb +4 -4
  58. data/samples/hello/hello_date_time.rb +2 -2
  59. data/samples/hello/hello_directory_dialog.rb +2 -2
  60. data/samples/hello/hello_file_dialog.rb +2 -2
  61. data/samples/hello/hello_font_dialog.rb +2 -2
  62. data/samples/hello/hello_group.rb +2 -2
  63. data/samples/hello/hello_list_multi_selection.rb +2 -2
  64. data/samples/hello/hello_list_single_selection.rb +2 -2
  65. data/samples/hello/hello_progress_bar.rb +2 -2
  66. data/samples/hello/hello_radio.rb +2 -2
  67. data/samples/hello/hello_radio_group.rb +2 -2
  68. data/samples/hello/hello_scale.rb +2 -2
  69. data/samples/hello/hello_spinner.rb +2 -2
  70. data/samples/hello/hello_table.rb +2 -2
  71. data/samples/hello/hello_text.rb +2 -2
  72. data/samples/hello/hello_tool_bar.rb +2 -2
  73. data/samples/hello/hello_tray_item.rb +2 -2
  74. data/samples/hello/hello_tree.rb +4 -4
  75. metadata +21 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e20d1ff3e84168b2937444cf859e28249a859789adbd3d24993e0087a51addf5
4
- data.tar.gz: 272ffc7415a569b4ef635da5919128399c19cd2077c11b280e34357d37b1ac09
3
+ metadata.gz: 431a3ee9e9fdbc1565e616a3ebee9e7bc7f026cae40d9c95a2df304647b3880b
4
+ data.tar.gz: 7b0bea9d898d0f42d5788ef36ce4279e81f3b3491c1102c148656f5c0e979328
5
5
  SHA512:
6
- metadata.gz: 1057ae99a81ceb633ff2ed436ef150036ab3475ca663a4b0514173f6af1e136ec0a4a822898ef3c320b4f285180d65ef1c254a245959e219667e12beca1353a6
7
- data.tar.gz: 7d62cc987fe8bbd20972d33ef7a7331844635f28f3d19273d99e4305ded7bdb60f1cb2d2b021d27f598f916fb501689017ff90208864026e5345a219778b7717
6
+ metadata.gz: 03a6fd9ea40d222acd37118cf42a7b299995ded46e334f8d9bff73fed74dc4db1e57913eaca40109bda7962977eafb546b92dbb8fdb72309ec9f5cf7c591d644
7
+ data.tar.gz: a9a3ed0e5e6cd57b26c64b6d030bdc35d14a29cb3576b11ed9e086d66a8dc7f76b206dd40539ed271e042b1073a85bca53f24415c68058df71b4571de93a161b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change Log
2
2
 
3
+ ### 4.20.13.10
4
+
5
+ - Update Glimmer Style Guide, scaffolding, and samples to have `before_body` and `after_body` in custom widgets/shells/shapes always take a `do; end` block since they contain logic not visuals
6
+
7
+ ### 4.20.13.9
8
+
9
+ - Update Klondike Solitaire to have playing card suit symbols and avoid clipping of cards on the boundaries
10
+
11
+ ### 4.20.13.8
12
+
13
+ - Glimmer Klondike Solitaire elaborate sample
14
+
3
15
  ### 4.20.13.7
4
16
 
5
17
  - Support accepting ImageProxy objects in Canvas Shape DSL (not just image paths)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for SWT 4.20.13.7
1
+ # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for SWT 4.20.13.11
2
2
  ## JRuby Desktop Development GUI Framework
3
3
  [![Gem Version](https://badge.fury.io/rb/glimmer-dsl-swt.svg)](http://badge.fury.io/rb/glimmer-dsl-swt)
4
4
  [![Travis CI](https://travis-ci.com/AndyObtiva/glimmer-dsl-swt.svg?branch=master)](https://travis-ci.com/github/AndyObtiva/glimmer-dsl-swt)
@@ -15,7 +15,7 @@
15
15
  [<img src="https://covers.oreillystatic.com/images/9780596519650/lrg.jpg" width=105 /><br />
16
16
  Featured in JRuby Cookbook](http://shop.oreilly.com/product/9780596519650.do) and [Chalmers/Gothenburg University Software Engineering Master's Lecture Material](http://www.cse.chalmers.se/~bergert/slides/guest_lecture_DSLs.pdf)
17
17
 
18
- [Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) 4.20.13.7 includes [SWT 4.20](https://download.eclipse.org/eclipse/downloads/drops4/R-4.20-202106111600/), which was released on June 11, 2021. Gem version numbers are in sync with the SWT library versions. The first two digits represent the SWT version number. The last two digits represent the minor and patch versions of Glimmer DSL for SWT. Note that SWT now supports AARCH64 on Mac and Linux, but it is not fully tested in Glimmer DSL for SWT yet, so deem its support experimental for the time being without guarantees for functionality until declared otherwise (report any issues you may encounter).
18
+ [Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) 4.20.13.11 includes [SWT 4.20](https://download.eclipse.org/eclipse/downloads/drops4/R-4.20-202106111600/), which was released on June 11, 2021. Gem version numbers are in sync with the SWT library versions. The first two digits represent the SWT version number. The last two digits represent the minor and patch versions of Glimmer DSL for SWT. Note that SWT now supports AARCH64 on Mac and Linux, but it is not fully tested in Glimmer DSL for SWT yet, so deem its support experimental for the time being without guarantees for functionality until declared otherwise (report any issues you may encounter).
19
19
 
20
20
  **Starting in version 4.20.0.0, [Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) comes with the new [***Shine***](/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md#shine) syntax** for highly intuitive and visually expressive View/Model Attribute Mapping, relying on `<=>` for bidirectional (two-way) data-binding and `<=` for unidirectional (one-way) data-binding, providing an alternative to the `bind` keyword (keep in mind that it is still a beta, so default back to `bind` whenever needed).
21
21
 
@@ -326,7 +326,7 @@ jgem install glimmer-dsl-swt
326
326
 
327
327
  Or this command if you want a specific version:
328
328
  ```
329
- jgem install glimmer-dsl-swt -v 4.20.13.7
329
+ jgem install glimmer-dsl-swt -v 4.20.13.11
330
330
  ```
331
331
 
332
332
  `jgem` is JRuby's version of `gem` command.
@@ -354,7 +354,7 @@ Note: if you're using activerecord or activesupport, keep in mind that Glimmer u
354
354
 
355
355
  Add the following to `Gemfile`:
356
356
  ```
357
- gem 'glimmer-dsl-swt', '~> 4.20.13.7'
357
+ gem 'glimmer-dsl-swt', '~> 4.20.13.11'
358
358
  ```
359
359
 
360
360
  And, then run:
@@ -375,7 +375,7 @@ glimmer
375
375
  ```
376
376
 
377
377
  ```
378
- Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.7
378
+ Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.11
379
379
 
380
380
  Usage: glimmer [--bundler] [--pd] [--quiet] [--debug] [--log-level=VALUE] [[ENV_VAR=VALUE]...] [[-jruby-option]...] (application.rb or task[task_args]) [[application2.rb]...]
381
381
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.20.13.7
1
+ 4.20.13.11
@@ -61,7 +61,9 @@
61
61
  - [Login](#login)
62
62
  - [Tic Tac Toe](#tic-tac-toe)
63
63
  - [Contact Manager](#contact-manager)
64
+ - [Game of Life](#game-of-life)
64
65
  - [Glimmer Tetris](#glimmer-tetris)
66
+ - [Klondike Solitaire](#klondike-solitaire)
65
67
  - [Mandelbrot Fractal](#mandelbrot-fractal)
66
68
  - [Stock Ticker](#stock-ticker)
67
69
  - [Metronome](#metronome)
@@ -1070,6 +1072,16 @@ Contact Manager - Edit Done
1070
1072
 
1071
1073
  ![Contact Manager](/images/glimmer-contact-manager-edit-done.png)
1072
1074
 
1075
+ #### Game of Life
1076
+
1077
+ This sample demonstrates how to build an interactive canvas-based visualization of Conway's Game of Life (test-first), taking advantage of data-binding and multi-threading.
1078
+
1079
+ Code:
1080
+
1081
+ [samples/elaborate/game_of_life.rb](/samples/elaborate/game_of_life.rb)
1082
+
1083
+ ![Game of Life](/images/glimmer-game-of-life.gif)
1084
+
1073
1085
  #### Glimmer Tetris
1074
1086
 
1075
1087
  This sample demonstrates how to build an interactive animated game with MVC architecture, custom-shell/custom-widgets, multi-threading, asynchronous programming, data-binding, canvas shape graphic decorations, canvas shape icon image generation, and keyboard events/shortcuts.
@@ -1098,6 +1110,18 @@ Code:
1098
1110
 
1099
1111
  ![Tetris Help Menu](/images/glimmer-tetris-help-menu.png)
1100
1112
 
1113
+ #### Klondike Solitaire
1114
+
1115
+ This sample demonstrates how to build an interactive card game with MVC architecture, canvas, custom-shapes, data-binding, observers, and drag & drop.
1116
+
1117
+ Code:
1118
+
1119
+ [samples/elaborate/klondike_solitaire.rb](/samples/elaborate/klondike_solitaire.rb)
1120
+
1121
+ ![Klondike Solitaire](/images/glimmer-klondike-solitaire.png)
1122
+
1123
+ ![Klondike Solitaire Played](/images/glimmer-klondike-solitaire-played.png)
1124
+
1101
1125
  #### Mandelbrot Fractal
1102
1126
 
1103
1127
  This sample demonstrates how to render canvas graphics with multi-threaded processing taking advantage of all CPU cores and doing background processing of images.
@@ -10,6 +10,7 @@
10
10
  - Widget listeners are always declared starting with `on_` prefix and affixing listener event method name afterwards in underscored lowercase form. Their multi-line blocks rely on the `do; end` style.
11
11
  - Widget listeners are always followed by a block using curly braces (Only when declared in DSL. When invoked on widget object directly outside of GUI declarations, standard Ruby conventions apply)
12
12
  - Data-binding is done via `bind` keyword, which always takes arguments wrapped in parentheses
13
- - Custom widget `body`, `before_body`, and `after_body` blocks open their blocks and close them with curly braces.
13
+ - Custom widget/shell/shape `body` blocks open and close with curly braces.
14
+ - Custom widget/shell/shape `before_body` and `after_body` blocks are declared as `do; end` blocks.
14
15
  - Custom widgets receive additional keyword arguments called options, which come after the SWT styles.
15
16
  - Pure logic multi-line blocks that do not constitute GUI DSL view elements (such as `Thread.new`, `loop`, `each` and `observe` blocks) rely on the `do; end` style to clearly separate logic code from view code.
Binary file
@@ -607,7 +607,7 @@ module Glimmer
607
607
 
608
608
  if %i[gem app desktopify].include?(shell_type)
609
609
  custom_shell_file_content += <<-MULTI_LINE_STRING
610
- before_body {
610
+ before_body do
611
611
  Display.app_name = '#{shell_type == :gem ? human_name(custom_shell_name) : human_name(namespace)}'
612
612
  Display.app_version = VERSION
613
613
  @display = display {
@@ -618,13 +618,13 @@ module Glimmer
618
618
  #{shell_type == :desktopify ? 'display_about_dialog' : 'display_preferences_dialog'}
619
619
  }
620
620
  }
621
- }
621
+ end
622
622
  MULTI_LINE_STRING
623
623
  else
624
624
  custom_shell_file_content += <<-MULTI_LINE_STRING
625
- # before_body {
625
+ # before_body do
626
626
  #
627
- # }
627
+ # end
628
628
  MULTI_LINE_STRING
629
629
  end
630
630
 
@@ -632,9 +632,9 @@ module Glimmer
632
632
 
633
633
  ## Use after_body block to setup observers for widgets in body
634
634
  #
635
- # after_body {
635
+ # after_body do
636
636
  #
637
- # }
637
+ # end
638
638
 
639
639
  ## Add widget content inside custom shell body
640
640
  ## Top-most widget must be a shell or another custom shell
@@ -767,15 +767,15 @@ end
767
767
  ## Use before_body block to pre-initialize variables to use in body
768
768
  #
769
769
  #
770
- # before_body {
770
+ # before_body do
771
771
  #
772
- # }
772
+ # end
773
773
 
774
774
  ## Use after_body block to setup observers for widgets in body
775
775
  #
776
- # after_body {
776
+ # after_body do
777
777
  #
778
- # }
778
+ # end
779
779
 
780
780
  ## Add widget content under custom widget body
781
781
  ##
@@ -817,15 +817,15 @@ end
817
817
  ## Use before_body block to pre-initialize variables to use in body
818
818
  #
819
819
  #
820
- # before_body {
820
+ # before_body do
821
821
  #
822
- # }
822
+ # end
823
823
 
824
824
  ## Use after_body block to setup observers for shapes in body
825
825
  #
826
- # after_body {
826
+ # after_body do
827
827
  #
828
- # }
828
+ # end
829
829
 
830
830
  ## Add shape content under custom shape body
831
831
  #
@@ -121,7 +121,7 @@ module Glimmer
121
121
  @line_numbers_styled_text_proxy.content(&block)
122
122
  end
123
123
 
124
- before_body {
124
+ before_body do
125
125
  require 'rouge'
126
126
  require 'ext/rouge/themes/glimmer'
127
127
  @swt_style = swt_style == 0 ? [:border, :multi, :v_scroll, :h_scroll] : swt_style
@@ -132,11 +132,11 @@ module Glimmer
132
132
  @lines_width = lines[:width]
133
133
  end
134
134
  @dsl_mode = true
135
- }
135
+ end
136
136
 
137
- after_body {
137
+ after_body do
138
138
  @dsl_mode = nil
139
- }
139
+ end
140
140
 
141
141
  body {
142
142
  if lines
@@ -15,7 +15,7 @@ class Calculator
15
15
 
16
16
  attr_reader :presenter
17
17
 
18
- before_body {
18
+ before_body do
19
19
  @presenter = Model::Presenter.new
20
20
 
21
21
  Display.setAppName('Glimmer Calculator')
@@ -30,7 +30,7 @@ class Calculator
30
30
  display_about_dialog
31
31
  }
32
32
  }
33
- }
33
+ end
34
34
 
35
35
  body {
36
36
  shell {
@@ -26,10 +26,10 @@ require_relative "contact_manager/contact_manager_presenter"
26
26
  class ContactManager
27
27
  include Glimmer::UI::CustomShell
28
28
 
29
- before_body {
29
+ before_body do
30
30
  @contact_manager_presenter = ContactManagerPresenter.new
31
31
  @contact_manager_presenter.list
32
- }
32
+ end
33
33
 
34
34
  body {
35
35
  shell {
@@ -0,0 +1,111 @@
1
+ # Copyright (c) 2007-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-dsl-swt'
23
+
24
+ require_relative 'game_of_life/model/grid'
25
+
26
+ class GameOfLife
27
+ include Glimmer::UI::CustomShell
28
+
29
+ WIDTH = 20
30
+ HEIGHT = 20
31
+ CELL_WIDTH = 25
32
+ CELL_HEIGHT = 25
33
+
34
+ before_body do
35
+ @grid = GameOfLife::Model::Grid.new(WIDTH, HEIGHT)
36
+ end
37
+
38
+ body {
39
+ shell {
40
+ row_layout :vertical
41
+ text "Conway's Game of Life (Glimmer Edition)"
42
+
43
+ canvas {
44
+ layout_data {
45
+ width WIDTH*CELL_WIDTH
46
+ height HEIGHT*CELL_HEIGHT
47
+ }
48
+ (0...HEIGHT).each do |row_index|
49
+ (0...WIDTH).each do |column_index|
50
+ rectangle(column_index*CELL_WIDTH, row_index*CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT) {
51
+ background <= [@grid.cell_rows[row_index][column_index], "alive", on_read: ->(a) {a ? :black : :white}]
52
+
53
+ on_mouse_down {
54
+ @grid.cell_rows[row_index][column_index].toggle_aliveness!
55
+ }
56
+ }
57
+ end
58
+ end
59
+ }
60
+
61
+ composite {
62
+ row_layout(:horizontal) {
63
+ margin_width 0
64
+ }
65
+
66
+ button {
67
+ text 'Step'
68
+ enabled <= [@grid, :playing, on_read: :! ]
69
+
70
+ on_widget_selected {
71
+ @grid.step!
72
+ }
73
+ }
74
+
75
+ button {
76
+ text 'Clear'
77
+ enabled <= [@grid, :playing, on_read: :! ]
78
+
79
+ on_widget_selected {
80
+ @grid.clear!
81
+ }
82
+ }
83
+
84
+ button {
85
+ text <= [@grid, :playing, on_read: ->(p) { p ? 'Stop' : 'Play' }]
86
+
87
+ on_widget_selected {
88
+ @grid.toggle_playback!
89
+ }
90
+ }
91
+
92
+ label {
93
+ text 'Slower'
94
+ }
95
+
96
+ scale {
97
+ minimum 1
98
+ maximum 100
99
+ selection <=> [@grid, :speed]
100
+ }
101
+
102
+ label {
103
+ text 'Faster'
104
+ }
105
+
106
+ }
107
+ }
108
+ }
109
+ end
110
+
111
+ GameOfLife.launch
@@ -0,0 +1,51 @@
1
+ class GameOfLife
2
+ module Model
3
+ class Cell
4
+ attr_reader :grid, :row_index, :column_index
5
+ attr_accessor :alive
6
+ alias alive? alive
7
+
8
+ def initialize(grid, row_index, column_index, alive=false)
9
+ @grid = grid
10
+ @row_index = row_index
11
+ @column_index = column_index
12
+ @alive = alive
13
+ end
14
+
15
+ def live!
16
+ self.alive = true
17
+ end
18
+
19
+ def die!
20
+ self.alive = false
21
+ end
22
+
23
+ def toggle_aliveness!
24
+ return if grid.playing?
25
+ dead ? live! : die!
26
+ end
27
+
28
+ def dead?
29
+ !alive?
30
+ end
31
+ alias dead dead?
32
+
33
+ # step into the next generation
34
+ def step!
35
+ self.alive = (alive? && alive_neighbor_count.between?(2, 3)) || (dead? && alive_neighbor_count == 3)
36
+ end
37
+
38
+ def alive_neighbor_count
39
+ [row_index - 1, 0].max.upto([row_index + 1, grid.row_count - 1].min).map do |current_row_index|
40
+ [column_index - 1, 0].max.upto([column_index + 1, grid.column_count - 1].min).map do |current_column_index|
41
+ if current_row_index == row_index && current_column_index == column_index
42
+ 0
43
+ else
44
+ grid.previous_cell_rows[current_row_index][current_column_index].alive? ? 1 : 0
45
+ end
46
+ end
47
+ end.flatten.reduce(:+)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,76 @@
1
+ require_relative 'cell'
2
+
3
+ class GameOfLife
4
+ module Model
5
+ class Grid
6
+ include Glimmer # included only to utilize sync_exec/async_exec
7
+ DEFAULT_ROW_COUNT = 100
8
+ DEFAULT_COLUMN_COUNT = 100
9
+
10
+ attr_reader :row_count, :column_count
11
+ attr_accessor :cell_rows, :previous_cell_rows, :playing, :speed
12
+ alias playing? playing
13
+
14
+ def initialize(row_count=DEFAULT_ROW_COUNT, column_count=DEFAULT_COLUMN_COUNT)
15
+ @row_count = row_count
16
+ @column_count = column_count
17
+ @speed = 1.0
18
+ build_cells
19
+ end
20
+
21
+ def clear!
22
+ cell_rows.each do |row|
23
+ row.each do |cell|
24
+ cell.die!
25
+ end
26
+ end
27
+ end
28
+
29
+ # steps into the next generation of cells given their current neighbors
30
+ def step!
31
+ self.previous_cell_rows = cell_rows.map do |row|
32
+ row.map do |cell|
33
+ cell.clone
34
+ end
35
+ end
36
+ cell_rows.each do |row|
37
+ row.each do |cell|
38
+ cell.step!
39
+ end
40
+ end
41
+ end
42
+
43
+ def toggle_playback!
44
+ if playing?
45
+ stop!
46
+ else
47
+ play!
48
+ end
49
+ end
50
+
51
+ def play!
52
+ self.playing = true
53
+ Thread.new do
54
+ while @playing
55
+ sync_exec { step! }
56
+ sleep(1.0/@speed)
57
+ end
58
+ end
59
+ end
60
+
61
+ def stop!
62
+ self.playing = false
63
+ end
64
+
65
+ private
66
+
67
+ def build_cells
68
+ @cell_rows = @row_count.times.map do |row_index|
69
+ @column_count.times.map do |column_index|
70
+ Cell.new(self, row_index, column_index)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end