glimmer-dsl-swt 4.20.13.10 → 4.20.13.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 982e9293313f88fc19f63d148e0b59b1dcda227e25bc47d9eb07377359b90084
4
- data.tar.gz: c90ad4be0b5ebf2aaa28b5b0376934e518f7fbe878a3ecb4a37982fa5feac554
3
+ metadata.gz: 9e7f58c70a78eb8867dd0116be71fb75a3a369deeb7f5676c673dde098304d55
4
+ data.tar.gz: '078a62bf56f74b1e156ef520d39c37e15b2324367bd813d0dd6a2a3f66e52e2d'
5
5
  SHA512:
6
- metadata.gz: 6946dfd8d245a2ef28df3df0c7eb3b74213f4c912c69a6de5ce0bd4f42d7b69f87671df845a816c532d6b580bd0f9da535639368aba6ed0abd76d689c66ded07
7
- data.tar.gz: c630515458b13ab4b08c6c662c27759c9a0adb8903e5f0a5e20aba958792d3949f48ae47d9849ffddbe52d4a7b33887955979d76ffe6223871cc51e8f844c443
6
+ metadata.gz: 4bb4fc88a86497ce00a50735ee2c189d9fdfb1698e8fa568e1ddad2565809d40e8602bf309fd29efc933481fe3f7d142c50ba80ee7d571dd2eb7c6d0e4dba405
7
+ data.tar.gz: c43f3edbc949886f21029c429229b73b4f5c08a89af9f7ad42a02539de8a8bc842eff606ccd090609fe05802cc9685499390bd1997f813be714e730ba2f8f801
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change Log
2
2
 
3
+ ### 4.20.13.14
4
+
5
+ - Connect 4 elaborate sample
6
+
7
+ ### 4.20.13.13
8
+
9
+ - Parking elaborate sample
10
+ - Support shape contain?/include? when a transform (e.g. rotation) is applied
11
+
12
+ ### 4.20.13.12
13
+
14
+ - Fixed issue with dragged shapes having `drag_source true` not going back to original position when not dropped in a target if they were part of a composite shape
15
+
16
+ ### 4.20.13.11
17
+
18
+ - Conway's Game of Life elaborate sample
19
+
3
20
  ### 4.20.13.10
4
21
 
5
22
  - 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
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.10
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.14
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.10 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.14 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.10
329
+ jgem install glimmer-dsl-swt -v 4.20.13.14
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.10'
357
+ gem 'glimmer-dsl-swt', '~> 4.20.13.14'
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.10
378
+ Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.14
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.10
1
+ 4.20.13.14
@@ -60,10 +60,13 @@
60
60
  - [User Profile](#user-profile)
61
61
  - [Login](#login)
62
62
  - [Tic Tac Toe](#tic-tac-toe)
63
+ - [Connect 4](#connect-4)
63
64
  - [Contact Manager](#contact-manager)
65
+ - [Game of Life](#game-of-life)
64
66
  - [Glimmer Tetris](#glimmer-tetris)
65
67
  - [Klondike Solitaire](#klondike-solitaire)
66
68
  - [Mandelbrot Fractal](#mandelbrot-fractal)
69
+ - [Parking](#parking)
67
70
  - [Stock Ticker](#stock-ticker)
68
71
  - [Metronome](#metronome)
69
72
  - [Weather](#weather)
@@ -1043,6 +1046,34 @@ Code:
1043
1046
  ![Tic Tac Toe In Progress](/images/glimmer-tic-tac-toe-in-progress.png)
1044
1047
  ![Tic Tac Toe Game Over](/images/glimmer-tic-tac-toe-game-over.png)
1045
1048
 
1049
+ #### Connect 4
1050
+
1051
+ This sample demonstrates a widget/shape hybrid MVC application, including GUI layout and data-binding.
1052
+
1053
+ Code:
1054
+
1055
+ [samples/elaborate/connect4.rb](/samples/elaborate/connect4.rb)
1056
+
1057
+ Connect 4
1058
+
1059
+ ![Connect 4](/images/glimmer-connect4.png)
1060
+
1061
+ Connect 4 - About To Drop
1062
+
1063
+ ![Connect 4 About To Drop](/images/glimmer-connect4-about-to-drop.png)
1064
+
1065
+ Connect 4 - Dropped Coin
1066
+
1067
+ ![Connect 4 Dropped Coin](/images/glimmer-connect4-dropped-coin.png)
1068
+
1069
+ Connect 4 - Player 1 Wins
1070
+
1071
+ ![Connect 4 Player 1 Wins](/images/glimmer-connect4-player1-wins.png)
1072
+
1073
+ Connect 4 - Game Over Message Box
1074
+
1075
+ ![Connect 4 Game Over Message Box](/images/glimmer-connect4-game-over-dialog.png)
1076
+
1046
1077
  #### Contact Manager
1047
1078
 
1048
1079
  This sample demonstrates table data-binding, sorting, filtering, GUI layout, MVP pattern, and test-driven development (has [specs](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/spec/samples/elaborate/contact_manager/contact_manager_presenter_spec.rb)).
@@ -1071,6 +1102,16 @@ Contact Manager - Edit Done
1071
1102
 
1072
1103
  ![Contact Manager](/images/glimmer-contact-manager-edit-done.png)
1073
1104
 
1105
+ #### Game of Life
1106
+
1107
+ This sample demonstrates how to build an interactive canvas-based visualization of Conway's Game of Life ([test-first](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/spec/samples/elaborate/game_of_life/model/grid_spec.rb)), taking advantage of data-binding and multi-threading.
1108
+
1109
+ Code:
1110
+
1111
+ [samples/elaborate/game_of_life.rb](/samples/elaborate/game_of_life.rb)
1112
+
1113
+ ![Game of Life](/images/glimmer-game-of-life.gif)
1114
+
1074
1115
  #### Glimmer Tetris
1075
1116
 
1076
1117
  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.
@@ -1101,15 +1142,15 @@ Code:
1101
1142
 
1102
1143
  #### Klondike Solitaire
1103
1144
 
1104
- This sample demonstrates how to build an interactive card game with MVC architecture, custom-shell/custom-widgets, data-binding, observers, and drag & drop.
1145
+ This sample demonstrates how to build an interactive card game with MVC architecture, canvas, custom-shapes, data-binding, observers, and drag & drop.
1105
1146
 
1106
1147
  Code:
1107
1148
 
1108
1149
  [samples/elaborate/klondike_solitaire.rb](/samples/elaborate/klondike_solitaire.rb)
1109
1150
 
1110
- ![Tetris](/images/glimmer-klondike-solitaire.png)
1151
+ ![Klondike Solitaire](/images/glimmer-klondike-solitaire.png)
1111
1152
 
1112
- ![Tetris](/images/glimmer-klondike-solitaire-played.png)
1153
+ ![Klondike Solitaire Played](/images/glimmer-klondike-solitaire-played.png)
1113
1154
 
1114
1155
  #### Mandelbrot Fractal
1115
1156
 
@@ -1137,6 +1178,28 @@ Code:
1137
1178
 
1138
1179
  ![Mandelbrot Fractal Help Menu](/images/glimmer-mandelbrot-menu-help.png)
1139
1180
 
1181
+ #### Parking
1182
+
1183
+ This sample demonstrates how to use method-based custom shapes and how to take advantage of transforms (e.g. rotation).
1184
+
1185
+ It enables booking a parking spot at the entrance of a building's parking, which in a real scenario would have prompted for payment too.
1186
+
1187
+ Code:
1188
+
1189
+ [samples/elaborate/parking.rb](/samples/elaborate/parking.rb)
1190
+
1191
+ Parking
1192
+
1193
+ ![Parking](/images/glimmer-parking.png)
1194
+
1195
+ Parking Floor 4
1196
+
1197
+ ![Parking Floor 4](/images/glimmer-parking-floor4.png)
1198
+
1199
+ Parking Booked
1200
+
1201
+ ![Parking Booked](/images/glimmer-parking-booked.png)
1202
+
1140
1203
  #### Stock Ticker
1141
1204
 
1142
1205
  This sample demonstrates a Stock Ticker that generates random stock price data for 4 different stocks and provides 4 different tab views of the graphed data using the [Canvas Path DSL](/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md#canvas-path-dsl). It leverages a thread that runs in the background and ticks the stocks to generate random new stock prices before amending the graphed paths with them.
Binary file
@@ -38,6 +38,7 @@ require 'super_module'
38
38
  require 'date'
39
39
  require 'facets/string/capitalized'
40
40
  require 'facets/hash/symbolize_keys'
41
+ require 'matrix'
41
42
 
42
43
  # Internal requires
43
44
  require 'ext/glimmer/config'
@@ -62,6 +62,7 @@ module Glimmer
62
62
  # Optimize further by having a collection of disposable_shapes independent of shapes, which is much smaller and only has shapes that require disposal (shapes with patterns or image)
63
63
  shapes.dup.each {|s| s.dispose(dispose_images: dispose_images, dispose_patterns: dispose_patterns) } if requires_shape_disposal?
64
64
  end
65
+ alias dispose_shapes clear_shapes
65
66
 
66
67
  def paint_pixel_by_pixel(width = nil, height = nil, &each_pixel_color)
67
68
  if @image_double_buffered
@@ -216,6 +216,7 @@ module Glimmer
216
216
  # Subclasses (like polygon) may override to indicate if a point x,y coordinates falls inside the shape
217
217
  # some shapes may choose to provide a fuzz factor to make usage of this method for mouse clicking more user friendly
218
218
  def contain?(x, y)
219
+ x, y = inverse_transform_point(x, y)
219
220
  # assume a rectangular filled shape by default (works for several shapes like image, text, and focus)
220
221
  x.between?(self.absolute_x, self.absolute_x + calculated_width.to_f) && y.between?(self.absolute_y, self.absolute_y + calculated_height.to_f)
221
222
  end
@@ -232,6 +233,24 @@ module Glimmer
232
233
  included = include?(x, y)
233
234
  included ||= expanded_shapes.reject {|shape| shape == except_child}.detect { |shape| shape.include?(x, y) }
234
235
  end
236
+
237
+ # if there is a transform, invert it and apply on x, y point coordinates
238
+ def inverse_transform_point(x, y)
239
+ current_transform = (transform || parent_shape_containers.map(&:transform).first)&.first
240
+ if current_transform
241
+ transform_array = [1,2,3,4,5,6].to_java(:float)
242
+ current_transform.getElements(transform_array)
243
+ inverse_transform = TransformProxy.new(DisplayProxy.instance.swt_display, *transform_array.to_a)
244
+ inverse_transform_array = [1,2,3,4,5,6].to_java(:float)
245
+ inverse_transform.getElements(inverse_transform_array)
246
+ matrix = Matrix[[inverse_transform_array[0], inverse_transform_array[1]], [inverse_transform_array[2], inverse_transform_array[3]]]
247
+ result = matrix * Matrix.column_vector([x, y])
248
+ x, y = result.to_a.flatten
249
+ x += inverse_transform_array[5]
250
+ y += inverse_transform_array[4]
251
+ end
252
+ [x, y]
253
+ end
235
254
 
236
255
  # Indicates if a shape's x, y, width, height differ from its bounds calculation (e.g. arc / polygon)
237
256
  def irregular?
@@ -713,7 +732,7 @@ module Glimmer
713
732
  end
714
733
  end
715
734
  @drawable_on_mouse_up = drawable.handle_observation_request('on_mouse_up') do |event|
716
- if Shape.dragging && Shape.dragged_shape == self && !Shape.drop_shapes.detect {|shape| shape.include_with_children?(event.x, event.y)}
735
+ if Shape.dragging && Shape.dragged_shape == self && !Shape.drop_shapes.detect {|shape| shape.include_with_children?(event.x, event.y, except_child: Shape.dragged_shape)}
717
736
  Shape.dragging = false
718
737
  Shape.dragged_shape.x = Shape.dragged_shape_original_x
719
738
  Shape.dragged_shape.y = Shape.dragged_shape_original_y
@@ -53,6 +53,7 @@ module Glimmer
53
53
 
54
54
  # checks if shape contains the point denoted by x and y
55
55
  def contain?(x, y)
56
+ x, y = inverse_transform_point(x, y)
56
57
  geometry.contains(x, y)
57
58
  end
58
59
 
@@ -60,6 +61,7 @@ module Glimmer
60
61
  if filled?
61
62
  contain?(x, y)
62
63
  else
64
+ x, y = inverse_transform_point(x, y)
63
65
  # give it some fuzz to allow a larger region around the drawn oval to accept including a point (helps with mouse clickability on a shape)
64
66
  outer_shape_geometry = java.awt.geom.Arc2D::Double.new(self.absolute_x, self.absolute_y, calculated_width + 3, calculated_height + 3, start_angle, arc_angle, java.awt.geom.Arc2D::PIE)
65
67
  inner_shape_geometry = java.awt.geom.Arc2D::Double.new(self.absolute_x, self.absolute_y, calculated_width - 3, calculated_height - 3, start_angle, arc_angle, java.awt.geom.Arc2D::PIE)
@@ -55,6 +55,7 @@ module Glimmer
55
55
 
56
56
  # checks if drawn or filled rectangle includes the point denoted by x and y (if drawn, it only returns true if point lies on the edge)
57
57
  def include?(x, y, filled: nil)
58
+ x, y = inverse_transform_point(x, y)
58
59
  filled = filled? if filled.nil?
59
60
  makeshift_gc = org.eclipse.swt.graphics.GC.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
60
61
  swt_path = org.eclipse.swt.graphics.Path.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
@@ -123,6 +123,7 @@ module Glimmer
123
123
  end
124
124
 
125
125
  def include?(x, y)
126
+ x, y = inverse_transform_point(x, y)
126
127
  # TODO must account for line width
127
128
  Line.include?(absolute_x1, absolute_y1, absolute_x2, absolute_y2, x, y)
128
129
  end
@@ -39,6 +39,7 @@ module Glimmer
39
39
 
40
40
  # checks if shape contains the point denoted by x and y
41
41
  def contain?(x, y)
42
+ x, y = inverse_transform_point(x, y)
42
43
  shape_geometry = java.awt.geom.Ellipse2D::Double.new(self.absolute_x, self.absolute_y, calculated_width, calculated_height)
43
44
  shape_geometry.contains(x, y)
44
45
  end
@@ -48,6 +49,7 @@ module Glimmer
48
49
  if filled?
49
50
  contain?(x, y)
50
51
  else
52
+ x, y = inverse_transform_point(x, y)
51
53
  # give it some fuzz to allow a larger region around the drawn oval to accept including a point (helps with mouse clickability on a shape)
52
54
  outer_shape_geometry = java.awt.geom.Ellipse2D::Double.new(self.absolute_x - 3, self.absolute_y - 3, calculated_width + 6, calculated_height + 6)
53
55
  inner_shape_geometry = java.awt.geom.Ellipse2D::Double.new(self.absolute_x + 3, self.absolute_y + 3, calculated_width - 6, calculated_height - 6)
@@ -61,17 +61,13 @@ module Glimmer
61
61
  end
62
62
  end
63
63
 
64
- def contain?(x, y)
65
- makeshift_gc = org.eclipse.swt.graphics.GC.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
66
- @swt_path.contains(x.to_f, y.to_f, makeshift_gc, false)
67
- end
68
-
69
64
  def contain?(x, y)
70
65
  include?(x, y, filled: true)
71
66
  end
72
67
 
73
68
  # checks if drawn or filled rectangle includes the point denoted by x and y (if drawn, it only returns true if point lies on the edge)
74
69
  def include?(x, y, filled: nil)
70
+ x, y = inverse_transform_point(x, y)
75
71
  filled = filled? if filled.nil?
76
72
  makeshift_gc = org.eclipse.swt.graphics.GC.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
77
73
  @swt_path.contains(x.to_f, y.to_f, makeshift_gc, !filled)
@@ -49,6 +49,7 @@ module Glimmer
49
49
  end
50
50
 
51
51
  def include?(x, y)
52
+ x, y = inverse_transform_point(x, y)
52
53
  # give it some fuzz (helps makes mouse clicking easier)
53
54
  x.to_i.between?(self.absolute_x.to_i - 2, self.absolute_x.to_i + 2) && y.to_i.between?(self.absolute_y.to_i - 2, self.absolute_y.to_i + 2)
54
55
  end
@@ -152,6 +152,7 @@ module Glimmer
152
152
  end
153
153
 
154
154
  def contain?(x, y)
155
+ x, y = inverse_transform_point(x, y)
155
156
  geometry.contains(x, y)
156
157
  end
157
158
 
@@ -159,6 +160,7 @@ module Glimmer
159
160
  if filled?
160
161
  contain?(x, y)
161
162
  else
163
+ x, y = inverse_transform_point(x, y)
162
164
  comparison_lines = absolute_point_xy_array.zip(absolute_point_xy_array.rotate(1))
163
165
  comparison_lines.any? {|line| Line.include?(line.first.first, line.first.last, line.last.first, line.last.last, x, y)}
164
166
  end
@@ -130,6 +130,7 @@ module Glimmer
130
130
  end
131
131
 
132
132
  def include?(x, y)
133
+ x, y = inverse_transform_point(x, y)
133
134
  comparison_lines = absolute_point_xy_array.zip(absolute_point_xy_array.rotate(1))
134
135
  comparison_lines.pop # ignore last pair since you don't want to compare last point with first point
135
136
  comparison_lines.any? {|line| Line.include?(line.first.first, line.first.last, line.last.first, line.last.last, x, y)}
@@ -85,6 +85,7 @@ module Glimmer
85
85
  if filled?
86
86
  contain?(x, y)
87
87
  else
88
+ x, y = inverse_transform_point(x, y)
88
89
  comparison_lines = absolute_point_xy_array.zip(absolute_point_xy_array.rotate(1))
89
90
  comparison_lines.any? {|line| Line.include?(line.first.first, line.first.last, line.last.first, line.last.last, x, y)}
90
91
  end
@@ -0,0 +1,118 @@
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 'connect4/model/grid'
25
+
26
+ class Connect4
27
+ include Glimmer::UI::CustomShell
28
+
29
+ WIDTH = 7
30
+ HEIGHT = 6
31
+ COLOR_BACKGROUND = rgb(63, 104, 212)
32
+ COLOR_EMPTY_SLOT = :white
33
+ COLOR_COIN1 = rgb(236, 223, 56)
34
+ COLOR_COIN2 = rgb(176, 11, 23)
35
+
36
+ attr_accessor :current_player_color
37
+
38
+ before_body do
39
+ @grid = Model::Grid.new(WIDTH, HEIGHT)
40
+ select_player_color
41
+ end
42
+
43
+ after_body do
44
+ observe(@grid, :current_player) do
45
+ select_player_color
46
+ end
47
+
48
+ observe(@grid, :game_over) do |game_over_value|
49
+ if game_over_value
50
+ game_over_message = case game_over_value
51
+ when true
52
+ "Game over!\nDraw!"
53
+ when 1
54
+ "Game over!\nPlayer 1 (yellow) wins!"
55
+ when 2
56
+ "Game over!\nPlayer 2 (red) wins!"
57
+ end
58
+
59
+ message_box {
60
+ text 'Game Over!'
61
+ message game_over_message
62
+ }.open
63
+
64
+ @grid.restart!
65
+ end
66
+ end
67
+ end
68
+
69
+ body {
70
+ shell(:no_resize) {
71
+ grid_layout(7, true)
72
+
73
+ text 'Glimmer Connect 4'
74
+ background COLOR_BACKGROUND
75
+
76
+ HEIGHT.times do |row_index|
77
+ WIDTH.times do |column_index|
78
+ canvas {
79
+ layout_data {
80
+ width_hint 50
81
+ height_hint 50
82
+ }
83
+
84
+ background :transparent
85
+
86
+ the_oval = oval(0, 0, 50, 50) {
87
+ background <= [@grid.slot_rows[row_index][column_index], :value, on_read: ->(v) { v == 0 ? COLOR_EMPTY_SLOT : (v == 1 ? COLOR_COIN1 : COLOR_COIN2)}]
88
+ }
89
+
90
+ if row_index == 0
91
+ entered = false
92
+ on_mouse_enter do
93
+ entered = true
94
+ the_oval.background = current_player_color if @grid.slot_rows[row_index][column_index].value == 0
95
+ end
96
+
97
+ on_mouse_exit do
98
+ entered = false
99
+ the_oval.background = COLOR_EMPTY_SLOT if @grid.slot_rows[row_index][column_index].value == 0
100
+ end
101
+
102
+ on_mouse_up do
103
+ @grid.insert!(column_index)
104
+ the_oval.background = current_player_color if entered && @grid.slot_rows[row_index][column_index].value == 0
105
+ end
106
+ end
107
+ }
108
+ end
109
+ end
110
+ }
111
+ }
112
+
113
+ def select_player_color
114
+ self.current_player_color = @grid.current_player == 1 ? COLOR_COIN1 : COLOR_COIN2
115
+ end
116
+ end
117
+
118
+ Connect4.launch
@@ -0,0 +1,173 @@
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_relative 'slot'
23
+
24
+ class Connect4
25
+ module Model
26
+ class Grid
27
+ attr_reader :width, :height, :slot_rows
28
+ attr_accessor :current_player, :game_over
29
+
30
+ def initialize(width, height)
31
+ @width = width
32
+ @height = height
33
+ build_slots
34
+ start!
35
+ end
36
+
37
+ def start!
38
+ self.game_over = false
39
+ self.current_player = 1
40
+ reset_slots
41
+ end
42
+ alias restart! start!
43
+
44
+ def insert!(column_index)
45
+ found_row_index = bottom_most_free_row_index(column_index)
46
+ raise "Illegal move at #{column_index}" if found_row_index == -1
47
+ @slot_rows[found_row_index][column_index].value = @current_player
48
+ self.current_player = @current_player%2 + 1
49
+ evaluate_game_over
50
+ end
51
+
52
+ # returns bottom most free row index
53
+ # returns -1 if none
54
+ def bottom_most_free_row_index(column_index)
55
+ found_row_index = @slot_rows.size - 1
56
+ found_row_index -= 1 until found_row_index == -1 || @slot_rows[found_row_index][column_index].value == 0
57
+ found_row_index
58
+ end
59
+
60
+ private
61
+
62
+ def build_slots
63
+ @slot_rows = height.times.map do |row_index|
64
+ width.times.map do |column_index|
65
+ Slot.new(self)
66
+ end
67
+ end
68
+ end
69
+
70
+ def reset_slots
71
+ @slot_rows.flatten.each {|s| s.value = 0}
72
+ end
73
+
74
+ def evaluate_game_over
75
+ evaluate_horizontal_win
76
+ evaluate_vertical_win
77
+ evaluate_sw_ne_diagonal_win
78
+ evaluate_se_nw_diagonal_win
79
+ self.game_over ||= true if @slot_rows.flatten.map(&:value).all? {|v| v > 0}
80
+ end
81
+
82
+ def evaluate_horizontal_win
83
+ connections = nil
84
+ last_slot_value = nil
85
+ height.times do |row_index|
86
+ connections = nil
87
+ last_slot_value = nil
88
+ width.times do |column_index|
89
+ slot = @slot_rows[row_index][column_index]
90
+ if slot.value.to_i > 0 && slot.value == last_slot_value
91
+ connections += 1
92
+ else
93
+ last_slot_value = slot.value
94
+ connections = 1
95
+ end
96
+ break if connections == 4
97
+ end
98
+ break if connections == 4
99
+ end
100
+ self.game_over = last_slot_value if connections == 4
101
+ end
102
+
103
+ def evaluate_vertical_win
104
+ connections = nil
105
+ last_slot_value = nil
106
+ width.times do |column_index|
107
+ connections = nil
108
+ last_slot_value = nil
109
+ height.times do |row_index|
110
+ slot = @slot_rows[row_index][column_index]
111
+ if slot.value.to_i > 0 && slot.value == last_slot_value
112
+ connections += 1
113
+ else
114
+ last_slot_value = slot.value
115
+ connections = 1
116
+ end
117
+ break if connections == 4
118
+ end
119
+ break if connections == 4
120
+ end
121
+ self.game_over = last_slot_value if connections == 4
122
+ end
123
+
124
+ def evaluate_sw_ne_diagonal_win
125
+ sw_ne_coordinates = [
126
+ [[3, 0], [2, 1], [1, 2], [0, 3]],
127
+ [[4, 0], [3, 1], [2, 2], [1, 3], [0, 4]],
128
+ [[5, 0], [4, 1], [3, 2], [2, 3], [1, 4], [0, 5]],
129
+ [[5, 1], [4, 2], [3, 3], [2, 4], [1, 5], [0, 6]],
130
+ [[5, 2], [4, 3], [3, 4], [2, 5], [1, 6]],
131
+ [[5, 3], [4, 4], [3, 5], [2, 6]],
132
+ ]
133
+ evaluate_diagonal_win(sw_ne_coordinates)
134
+ end
135
+
136
+ def evaluate_se_nw_diagonal_win
137
+ sw_ne_coordinates = [
138
+ [[0, 3], [1, 4], [2, 5], [3, 6]],
139
+ [[0, 2], [1, 3], [2, 4], [3, 5], [4, 6]],
140
+ [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]],
141
+ [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5]],
142
+ [[1, 0], [2, 1], [3, 2], [4, 3], [5, 4]],
143
+ [[2, 0], [3, 1], [4, 2], [5, 3]],
144
+ ]
145
+ evaluate_diagonal_win(sw_ne_coordinates)
146
+ end
147
+
148
+ def evaluate_diagonal_win(diagonal_coordinates)
149
+ connections = nil
150
+ last_slot_value = nil
151
+ diagonal_coordinates.each do |group|
152
+ connections = nil
153
+ last_slot_value = nil
154
+ group.each do |row_index, column_index|
155
+ slot = @slot_rows[row_index][column_index]
156
+ if slot.value.to_i > 0 && slot.value == last_slot_value
157
+ connections += 1
158
+ else
159
+ last_slot_value = slot.value
160
+ connections = 1
161
+ end
162
+ break if connections == 4
163
+ end
164
+ break if connections == 4
165
+ end
166
+ self.game_over = last_slot_value if connections == 4
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+ end
@@ -0,0 +1,36 @@
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
+ class Connect4
23
+ module Model
24
+ class Slot
25
+ attr_reader :grid
26
+
27
+ # value 0 means empty, 1 means player 1 coin, and 2 means player 2 coin
28
+ attr_accessor :value
29
+
30
+ def initialize(grid)
31
+ @value = 0
32
+ @grid = grid
33
+ end
34
+ end
35
+ end
36
+ end
@@ -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,72 @@
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
+ class GameOfLife
23
+ module Model
24
+ class Cell
25
+ attr_reader :grid, :row_index, :column_index
26
+ attr_accessor :alive
27
+ alias alive? alive
28
+
29
+ def initialize(grid, row_index, column_index, alive=false)
30
+ @grid = grid
31
+ @row_index = row_index
32
+ @column_index = column_index
33
+ @alive = alive
34
+ end
35
+
36
+ def live!
37
+ self.alive = true
38
+ end
39
+
40
+ def die!
41
+ self.alive = false
42
+ end
43
+
44
+ def toggle_aliveness!
45
+ return if grid.playing?
46
+ dead ? live! : die!
47
+ end
48
+
49
+ def dead?
50
+ !alive?
51
+ end
52
+ alias dead dead?
53
+
54
+ # step into the next generation
55
+ def step!
56
+ self.alive = (alive? && alive_neighbor_count.between?(2, 3)) || (dead? && alive_neighbor_count == 3)
57
+ end
58
+
59
+ def alive_neighbor_count
60
+ [row_index - 1, 0].max.upto([row_index + 1, grid.row_count - 1].min).map do |current_row_index|
61
+ [column_index - 1, 0].max.upto([column_index + 1, grid.column_count - 1].min).map do |current_column_index|
62
+ if current_row_index == row_index && current_column_index == column_index
63
+ 0
64
+ else
65
+ grid.previous_cell_rows[current_row_index][current_column_index].alive? ? 1 : 0
66
+ end
67
+ end
68
+ end.flatten.reduce(:+)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,97 @@
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_relative 'cell'
23
+
24
+ class GameOfLife
25
+ module Model
26
+ class Grid
27
+ include Glimmer # included only to utilize sync_exec/async_exec
28
+ DEFAULT_ROW_COUNT = 100
29
+ DEFAULT_COLUMN_COUNT = 100
30
+
31
+ attr_reader :row_count, :column_count
32
+ attr_accessor :cell_rows, :previous_cell_rows, :playing, :speed
33
+ alias playing? playing
34
+
35
+ def initialize(row_count=DEFAULT_ROW_COUNT, column_count=DEFAULT_COLUMN_COUNT)
36
+ @row_count = row_count
37
+ @column_count = column_count
38
+ @speed = 1.0
39
+ build_cells
40
+ end
41
+
42
+ def clear!
43
+ cell_rows.each do |row|
44
+ row.each do |cell|
45
+ cell.die!
46
+ end
47
+ end
48
+ end
49
+
50
+ # steps into the next generation of cells given their current neighbors
51
+ def step!
52
+ self.previous_cell_rows = cell_rows.map do |row|
53
+ row.map do |cell|
54
+ cell.clone
55
+ end
56
+ end
57
+ cell_rows.each do |row|
58
+ row.each do |cell|
59
+ cell.step!
60
+ end
61
+ end
62
+ end
63
+
64
+ def toggle_playback!
65
+ if playing?
66
+ stop!
67
+ else
68
+ play!
69
+ end
70
+ end
71
+
72
+ def play!
73
+ self.playing = true
74
+ Thread.new do
75
+ while @playing
76
+ sync_exec { step! }
77
+ sleep(1.0/@speed)
78
+ end
79
+ end
80
+ end
81
+
82
+ def stop!
83
+ self.playing = false
84
+ end
85
+
86
+ private
87
+
88
+ def build_cells
89
+ @cell_rows = @row_count.times.map do |row_index|
90
+ @column_count.times.map do |column_index|
91
+ Cell.new(self, row_index, column_index)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -29,7 +29,7 @@ class KlondikeSolitaire
29
29
  end
30
30
 
31
31
  body {
32
- shell(:no_resize) {
32
+ shell {
33
33
  row_layout(:vertical) {
34
34
  fill true
35
35
  center true
@@ -0,0 +1,146 @@
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 'parking/model/parking_presenter'
25
+
26
+ class Parking
27
+ include Glimmer::UI::CustomShell
28
+
29
+ FLOOR_COUNT = 4
30
+
31
+ before_body do
32
+ @parking_spots = Model::ParkingSpot::LETTERS.clone
33
+ @parking_presenter = Model::ParkingPresenter.new(FLOOR_COUNT)
34
+ end
35
+
36
+ body {
37
+ shell(:no_resize) {
38
+ row_layout(:vertical) {
39
+ center true
40
+ }
41
+
42
+ text 'Parking'
43
+
44
+ label {
45
+ text 'Select an available spot to park'
46
+ font height: 30
47
+ }
48
+ label {
49
+ text 'Floor:'
50
+ font height: 20
51
+ }
52
+ spinner {
53
+ minimum 1
54
+ maximum FLOOR_COUNT
55
+ font height: 20
56
+ selection <=> [@parking_presenter, :selected_floor]
57
+
58
+ on_widget_selected do
59
+ @parking_spots = Model::ParkingSpot::LETTERS.clone
60
+ @canvas.dispose_shapes
61
+ @canvas.content {
62
+ parking_floor
63
+ }
64
+ end
65
+ }
66
+ @canvas = canvas {
67
+ layout_data {
68
+ width 600
69
+ height 600
70
+ }
71
+
72
+ background :dark_gray
73
+
74
+ parking_floor
75
+ }
76
+ }
77
+ }
78
+
79
+ def parking_floor
80
+ parking_quad(67.5, 0, 125)
81
+ parking_quad(67.5, 0, 125) { |shp|
82
+ shp.rotate(90)
83
+ }
84
+ parking_quad(67.5, 0, 125) { |shp|
85
+ shp.rotate(180)
86
+ }
87
+ parking_quad(67.5, 0, 125) { |shp|
88
+ shp.rotate(270)
89
+ }
90
+ end
91
+
92
+ def parking_quad(location_x, location_y, length, &block)
93
+ distance = (1.0/3)*length
94
+ parking_spot(location_x, location_y, length, &block)
95
+ parking_spot(location_x + distance, location_y, length, &block)
96
+ parking_spot(location_x + 2*distance, location_y, length, &block)
97
+ parking_spot(location_x + 3*distance, location_y, length, &block)
98
+ end
99
+
100
+ def parking_spot(location_x, location_y, length, &block)
101
+ parking_spot_letter = @parking_spots.shift
102
+ height = length
103
+ width = (2.0/3)*length
104
+ shape(location_x, location_y) { |the_shape|
105
+ line_width (1.0/15)*length
106
+ foreground :white
107
+
108
+ block&.call(the_shape)
109
+
110
+ rectangle(location_x, location_y, width, height) {
111
+ background <= [
112
+ @parking_presenter.floors[@parking_presenter.selected_floor - 1].parking_spots[parking_spot_letter],
113
+ :booked,
114
+ on_read: ->(b) {b ? :red : :dark_gray}
115
+ ]
116
+
117
+ text {
118
+ x :default
119
+ y :default
120
+ string parking_spot_letter
121
+ font height: 20
122
+ }
123
+
124
+ on_mouse_up do
125
+ begin
126
+ @parking_presenter.floors[@parking_presenter.selected_floor - 1].book!(parking_spot_letter)
127
+ message_box {
128
+ text 'Parking Booked!'
129
+ message "Floor #{@parking_presenter.selected_floor} Parking Spot #{parking_spot_letter} Is Booked!"
130
+ }.open
131
+ rescue => e
132
+ # No Op if already booked
133
+ end
134
+ end
135
+ }
136
+
137
+ line(location_x, location_y, location_x, location_y + height)
138
+ line(location_x, location_y, location_x + width, location_y)
139
+ line(location_x + width, location_y, location_x + width, location_y + height)
140
+
141
+ }
142
+ end
143
+ end
144
+
145
+ Parking.launch
146
+
@@ -0,0 +1,41 @@
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_relative 'parking_spot'
23
+
24
+ class Parking
25
+ module Model
26
+ class ParkingFloor
27
+ attr_reader :number, :parking_spots
28
+
29
+ def initialize(floor_number)
30
+ @number = floor_number
31
+ @parking_spots = ParkingSpot::LETTERS.reduce({}) do |hash, letter|
32
+ hash.merge(letter => ParkingSpot.new(self, letter))
33
+ end
34
+ end
35
+
36
+ def book!(parking_spot_letter)
37
+ parking_spots[parking_spot_letter].book!
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
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_relative 'parking_floor'
23
+
24
+ class Parking
25
+ module Model
26
+ class ParkingPresenter
27
+ attr_reader :floor_count
28
+ attr_accessor :floors, :selected_floor
29
+
30
+ def initialize(floor_count)
31
+ self.floors = 1.upto(floor_count).map do |floor_number|
32
+ ParkingFloor.new(floor_number)
33
+ end
34
+ self.selected_floor = 1
35
+ end
36
+
37
+ def book(parking_spot_letter)
38
+ self.floors[selected_floor - 1].book(parking_spot_letter)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,46 @@
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
+ class Parking
23
+ module Model
24
+ class ParkingSpot
25
+ LETTERS = %W[A B C D E F G H I J K L M N O P]
26
+
27
+ attr_reader :parking_floor, :letter
28
+ attr_accessor :booked
29
+ alias booked? booked
30
+
31
+ def initialize(parking_floor, letter)
32
+ @parking_floor = parking_floor
33
+ @letter = letter
34
+ end
35
+
36
+ def book!
37
+ raise "Floor #{parking_floor.number} Parking Spot #{letter} Is Already Booked!" if booked?
38
+ self.booked = true
39
+ end
40
+
41
+ def free!
42
+ self.booked = false
43
+ end
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-swt
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.20.13.10
4
+ version: 4.20.13.14
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-07-31 00:00:00.000000000 Z
11
+ date: 2021-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -541,10 +541,16 @@ files:
541
541
  - samples/elaborate/calculator/model/command/operation/subtract.rb
542
542
  - samples/elaborate/calculator/model/command/point.rb
543
543
  - samples/elaborate/calculator/model/presenter.rb
544
+ - samples/elaborate/connect4.rb
545
+ - samples/elaborate/connect4/model/grid.rb
546
+ - samples/elaborate/connect4/model/slot.rb
544
547
  - samples/elaborate/contact_manager.rb
545
548
  - samples/elaborate/contact_manager/contact.rb
546
549
  - samples/elaborate/contact_manager/contact_manager_presenter.rb
547
550
  - samples/elaborate/contact_manager/contact_repository.rb
551
+ - samples/elaborate/game_of_life.rb
552
+ - samples/elaborate/game_of_life/model/cell.rb
553
+ - samples/elaborate/game_of_life/model/grid.rb
548
554
  - samples/elaborate/klondike_solitaire.rb
549
555
  - samples/elaborate/klondike_solitaire/model/column_pile.rb
550
556
  - samples/elaborate/klondike_solitaire/model/dealing_pile.rb
@@ -565,6 +571,10 @@ files:
565
571
  - samples/elaborate/mandelbrot_fractal.rb
566
572
  - samples/elaborate/meta_sample.rb
567
573
  - samples/elaborate/metronome.rb
574
+ - samples/elaborate/parking.rb
575
+ - samples/elaborate/parking/model/parking_floor.rb
576
+ - samples/elaborate/parking/model/parking_presenter.rb
577
+ - samples/elaborate/parking/model/parking_spot.rb
568
578
  - samples/elaborate/stock_ticker.rb
569
579
  - samples/elaborate/tetris.rb
570
580
  - samples/elaborate/tetris/model/block.rb