glimmer-dsl-swt 4.20.13.12 → 4.20.13.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +5 -10
  4. data/VERSION +1 -1
  5. data/docs/reference/GLIMMER_SAMPLES.md +84 -2
  6. data/glimmer-dsl-swt.gemspec +0 -0
  7. data/lib/glimmer-dsl-swt.rb +1 -0
  8. data/lib/glimmer/swt/custom/drawable.rb +1 -0
  9. data/lib/glimmer/swt/custom/shape.rb +19 -0
  10. data/lib/glimmer/swt/custom/shape/arc.rb +2 -0
  11. data/lib/glimmer/swt/custom/shape/cubic.rb +1 -0
  12. data/lib/glimmer/swt/custom/shape/line.rb +1 -0
  13. data/lib/glimmer/swt/custom/shape/oval.rb +2 -0
  14. data/lib/glimmer/swt/custom/shape/path.rb +1 -5
  15. data/lib/glimmer/swt/custom/shape/point.rb +1 -0
  16. data/lib/glimmer/swt/custom/shape/polygon.rb +2 -0
  17. data/lib/glimmer/swt/custom/shape/polyline.rb +1 -0
  18. data/lib/glimmer/swt/custom/shape/rectangle.rb +1 -0
  19. data/lib/glimmer/swt/display_proxy.rb +8 -6
  20. data/lib/glimmer/ui/custom_shell.rb +1 -1
  21. data/lib/glimmer/ui/custom_widget.rb +3 -3
  22. data/samples/elaborate/battleship.rb +79 -0
  23. data/samples/elaborate/battleship/model/cell.rb +84 -0
  24. data/samples/elaborate/battleship/model/game.rb +143 -0
  25. data/samples/elaborate/battleship/model/grid.rb +54 -0
  26. data/samples/elaborate/battleship/model/ship.rb +114 -0
  27. data/samples/elaborate/battleship/model/ship_collection.rb +56 -0
  28. data/samples/elaborate/battleship/view/action_panel.rb +59 -0
  29. data/samples/elaborate/battleship/view/cell.rb +163 -0
  30. data/samples/elaborate/battleship/view/grid.rb +77 -0
  31. data/samples/elaborate/battleship/view/ship.rb +65 -0
  32. data/samples/elaborate/battleship/view/ship_collection.rb +49 -0
  33. data/samples/elaborate/connect4.rb +116 -0
  34. data/samples/elaborate/connect4/model/grid.rb +174 -0
  35. data/samples/elaborate/connect4/model/slot.rb +36 -0
  36. data/samples/elaborate/klondike_solitaire.rb +1 -1
  37. data/samples/elaborate/klondike_solitaire/view/column_pile.rb +0 -1
  38. data/samples/elaborate/klondike_solitaire/view/foundation_pile.rb +0 -2
  39. data/samples/elaborate/parking.rb +146 -0
  40. data/samples/elaborate/parking/model/parking_floor.rb +41 -0
  41. data/samples/elaborate/parking/model/parking_presenter.rb +42 -0
  42. data/samples/elaborate/parking/model/parking_spot.rb +46 -0
  43. data/samples/hello/hello_custom_shell.rb +2 -0
  44. metadata +20 -2
@@ -0,0 +1,56 @@
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 'ship'
23
+
24
+ class Battleship
25
+ module Model
26
+ class ShipCollection
27
+ BATTLESHIPS = {
28
+ aircraft_carrier: 5,
29
+ battleship: 4,
30
+ submarine: 3,
31
+ cruiser: 3,
32
+ destroyer: 2
33
+ }
34
+
35
+ attr_reader :game, :player, :ships
36
+ attr_accessor :placed_count
37
+
38
+ def initialize(game, player)
39
+ @game = game
40
+ @player = player
41
+ @ships = BATTLESHIPS.reduce({}) do |hash, pair|
42
+ name, width = pair
43
+ ship = Ship.new(self, name, width)
44
+ Glimmer::DataBinding::Observer.proc do |cell_value|
45
+ self.placed_count = ships.values.map(&:top_left_cell).compact.size
46
+ end.observe(ship, :top_left_cell)
47
+ hash.merge(name => ship)
48
+ end
49
+ end
50
+
51
+ def reset!
52
+ ships.values.each(&:reset!)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,59 @@
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 Battleship
23
+ module View
24
+ class ActionPanel
25
+ include Glimmer::UI::CustomWidget
26
+
27
+ options :game
28
+
29
+ body {
30
+ composite {
31
+ row_layout(:horizontal) {
32
+ margin_width 0
33
+ margin_height 0
34
+ }
35
+
36
+ layout_data(:center, :center, true, false)
37
+
38
+ @battle_button = button {
39
+ text 'Battle!'
40
+ enabled <= [game.ship_collections[:you], :placed_count, on_read: ->(c) {c == 5}]
41
+
42
+ on_widget_selected do
43
+ game.battle!
44
+ @battle_button.enabled = false
45
+ end
46
+ }
47
+
48
+ button {
49
+ text 'Restart'
50
+
51
+ on_widget_selected do
52
+ game.reset!
53
+ end
54
+ }
55
+ }
56
+ }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,163 @@
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 Battleship
23
+ module View
24
+ class Cell
25
+ include Glimmer::UI::CustomWidget
26
+
27
+ class << self
28
+ attr_accessor :dragging
29
+ alias dragging? dragging
30
+ end
31
+
32
+ COLOR_WATER = rgb(156, 211, 219)
33
+ COLOR_SHIP = :gray
34
+ COLOR_PLACED = :white
35
+ COLOR_EMPTY = :black
36
+ COLOR_NO_HIT = :white
37
+ COLOR_HIT = :red
38
+
39
+ options :game, :player, :row_index, :column_index, :ship
40
+ option :type, default: :grid # other type is :ship
41
+
42
+ body {
43
+ canvas {
44
+ if type == :grid
45
+ if player == :you
46
+ background <= [model, :ship, on_read: ->(s) {s ? COLOR_SHIP : COLOR_WATER}]
47
+ else
48
+ background COLOR_WATER
49
+ end
50
+ else
51
+ background <= [ship, :sunk, on_read: ->(s) {s ? COLOR_HIT : COLOR_PLACED}]
52
+ background <= [ship, :top_left_cell, on_read: ->(c) {c ? COLOR_PLACED : COLOR_SHIP}]
53
+ end
54
+
55
+ rectangle(0, 0, [:max, -1], [:max, -1])
56
+ oval(:default, :default, 10, 10) {
57
+ foreground <= [model, :hit, on_read: ->(h) {h == nil ? COLOR_EMPTY : (h ? COLOR_HIT : COLOR_NO_HIT)}]
58
+ }
59
+ oval(:default, :default, 5, 5) {
60
+ background <= [model, :hit, on_read: ->(h) {h == nil ? COLOR_EMPTY : (h ? COLOR_HIT : COLOR_NO_HIT)}]
61
+ }
62
+
63
+ on_mouse_move do |event|
64
+ if game.started?
65
+ if type == :grid
66
+ if player == :enemy
67
+ body_root.cursor = :cross
68
+ else
69
+ body_root.cursor = :arrow
70
+ end
71
+ else
72
+ body_root.cursor = :arrow
73
+ end
74
+ end
75
+ end
76
+
77
+ if player == :enemy
78
+ on_mouse_up do
79
+ game.attack!(row_index, column_index)
80
+ end
81
+ end
82
+
83
+ if player == :you
84
+ on_drag_detected do |event|
85
+ unless game.started? || game.over?
86
+ Cell.dragging = true
87
+ body_root.cursor = :hand if type == :grid
88
+ end
89
+ end
90
+
91
+ on_drag_set_data do |event|
92
+ the_ship = ship || model&.ship
93
+ if the_ship && !game.started? && !game.over? && !(type == :ship && the_ship.top_left_cell)
94
+ event.data = the_ship.name.to_s
95
+ else
96
+ event.doit = false
97
+ Cell.dragging = false
98
+ end
99
+ end
100
+
101
+ on_mouse_up do
102
+ unless game.started? || game.over?
103
+ Cell.dragging = false
104
+ change_cursor
105
+ end
106
+ end
107
+
108
+ if type == :grid
109
+ on_mouse_move do |event|
110
+ unless game.started? || game.over?
111
+ change_cursor
112
+ end
113
+ end
114
+
115
+ on_mouse_hover do |event|
116
+ unless game.started? || game.over?
117
+ change_cursor
118
+ end
119
+ end
120
+
121
+ on_mouse_up do |event|
122
+ unless game.started? || game.over?
123
+ begin
124
+ model.ship&.toggle_orientation!
125
+ rescue => e
126
+ Glimmer::Config.logger.debug e.full_message
127
+ end
128
+ change_cursor
129
+ end
130
+ end
131
+
132
+ on_drop do |event|
133
+ unless game.started? || game.over?
134
+ ship_name = event.data
135
+ place_ship(ship_name.to_s.to_sym) if ship_name
136
+ Cell.dragging = false
137
+ change_cursor
138
+ end
139
+ end
140
+ end
141
+ end
142
+ }
143
+ }
144
+
145
+ def model
146
+ game.grids[player].cell_rows[row_index][column_index] if type == :grid
147
+ end
148
+
149
+ def place_ship(ship_name)
150
+ ship = game.ship_collections[player].ships[ship_name]
151
+ model.place_ship!(ship)
152
+ end
153
+
154
+ def change_cursor
155
+ if type == :grid && model.ship && !Cell.dragging?
156
+ body_root.cursor = model.ship.orientation == :horizontal ? :sizens : :sizewe
157
+ elsif !Cell.dragging?
158
+ body_root.cursor = :arrow
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,77 @@
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 '../model/grid'
23
+
24
+ require_relative 'cell'
25
+
26
+ class Battleship
27
+ module View
28
+ class Grid
29
+ include Glimmer::UI::CustomWidget
30
+
31
+ options :game, :player
32
+
33
+ body {
34
+ composite {
35
+ grid_layout(Model::Grid::WIDTH + 1, true) {
36
+ margin_width 0
37
+ margin_height 0
38
+ horizontal_spacing 0
39
+ vertical_spacing 0
40
+ }
41
+
42
+ label(:center) {
43
+ layout_data(:fill, :center, true, false) {
44
+ horizontal_span (Model::Grid::WIDTH + 1)
45
+ }
46
+
47
+ text player.to_s.capitalize
48
+ font height: 20, style: :bold
49
+ }
50
+
51
+ label # filler
52
+ Model::Grid::WIDTH.times do |column_index|
53
+ label {
54
+ text (column_index + 1).to_s
55
+ font height: 16
56
+ }
57
+ end
58
+
59
+ Model::Grid::HEIGHT.times do |row_index|
60
+ label {
61
+ text Model::Grid::ROW_ALPHABETS[row_index]
62
+ font height: 16
63
+ }
64
+ Model::Grid::WIDTH.times do |column_index|
65
+ cell(game: game, player: player, row_index: row_index, column_index: column_index) {
66
+ layout_data {
67
+ width_hint 25
68
+ height_hint 25
69
+ }
70
+ }
71
+ end
72
+ end
73
+ }
74
+ }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,65 @@
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 Battleship
25
+ module View
26
+ class Ship
27
+ include Glimmer::UI::CustomWidget
28
+
29
+ options :game, :player, :ship_name, :ship_length
30
+
31
+ body {
32
+ composite {
33
+ row_layout(:vertical) {
34
+ fill true
35
+ margin_width 0
36
+ margin_height 0
37
+ }
38
+
39
+ label {
40
+ text ship_name.to_s.titlecase
41
+ font height: 16
42
+ }
43
+
44
+ composite {
45
+ grid_layout(ship_length, true) {
46
+ margin_width 0
47
+ margin_height 0
48
+ horizontal_spacing 0
49
+ vertical_spacing 0
50
+ }
51
+
52
+ ship_length.times do |column_index|
53
+ cell(game: game, player: player, type: :ship, column_index: column_index, ship: game.ship_collections[player].ships[ship_name]) {
54
+ layout_data {
55
+ width_hint 25
56
+ height_hint 25
57
+ }
58
+ }
59
+ end
60
+ }
61
+ }
62
+ }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,49 @@
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 '../model/game'
23
+ require_relative '../model/ship_collection'
24
+
25
+ require_relative 'ship'
26
+
27
+ class Battleship
28
+ module View
29
+ class ShipCollection
30
+ include Glimmer::UI::CustomWidget
31
+
32
+ options :game, :player
33
+
34
+ body {
35
+ composite {
36
+ row_layout(:vertical) {
37
+ fill true
38
+ margin_width 0
39
+ margin_height 0
40
+ }
41
+
42
+ Model::ShipCollection::BATTLESHIPS.each do |ship_name, ship_length|
43
+ ship(game: game, player: player, ship_name: ship_name, ship_length: ship_length)
44
+ end
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end