1
- require 'rubygo/model/greeting'
2
- require 'rubygo/model/settings'
1
+ class Rubygo
2
+ module Model
3
+ class Game
4
+
5
+ attr_accessor :height , :width , :scale , :name
6
+
7
+ def initialize ( height = 12 , width = 12 , scale = 70 , name = "Go Game" )
8
+ @height = height
9
+ @width = width
10
+ @scale = scale
11
+ @name = name
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class Rubygo
18
+ module View
19
+ class GameBoard
20
+ include Glimmer ::LibUI ::CustomControl
21
+ option :game
22
+
23
+ body {
24
+ vertical_box {
25
+ padded false
26
+ game . height . times . map do |row |
27
+ horizontal_box {
28
+ padded false
29
+
30
+ game . width . times . map do |column |
31
+ half = game . scale / 2
32
+ area {
33
+ square ( 0 , 0 , game . scale ) {
34
+ fill r : 240 , g : 215 , b : 141 , a : 1.0
35
+ }
36
+ line ( half , row == 0 ? half : 0 , half , row == ( game . height - 1 ) ? half : game . scale ) {
37
+ stroke 0x000000
38
+ }
39
+ line ( column == 0 ? half : 0 , half , column == ( game . width - 1 ) ? half : game . scale , half ) {
40
+ stroke 0x000000
41
+ }
42
+ }
43
+ end
44
+ }
45
+ end
46
+ }
47
+ }
48
+ end
49
+ end
50
+ end
51
+
52
+ class Rubygo
53
+ module View
54
+ class NewGameWindow
55
+ include Glimmer ::LibUI ::CustomWindow
56
+ option :on_create , default : lambda { |user | }
57
+
58
+ before_body do
59
+ @game = Model ::Game . new
60
+ end
61
+
62
+ body {
63
+ window { |new_game_window |
64
+ title "New Game"
65
+ margined true
66
+ vertical_box {
67
+ group ( "Game Size" ) {
68
+ vertical_box {
69
+ horizontal_box {
70
+ label ( 'Board Width' )
71
+ spinbox ( 2 , 20 ) {
72
+ value <=> [ @game , :width ]
73
+ }
74
+ }
75
+ horizontal_box {
76
+ label ( 'Board Height' )
77
+ spinbox ( 2 , 20 ) {
78
+ value <=> [ @game , :height ]
79
+ }
80
+ }
81
+ }
82
+ }
83
+ horizontal_box {
84
+ stretchy false
85
+ button ( "Cancel" ) {
86
+ on_clicked do
87
+ new_game_window . destroy
88
+ end
89
+ }
90
+ button ( "New Game" ) {
91
+ on_clicked do
92
+ on_create . call ( @game )
93
+ new_game_window . destroy
94
+ end
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+ end
101
+ end
102
+ end
3
103
4
104
class Rubygo
5
105
module View
@@ -12,19 +112,24 @@ class Rubygo
12
112
# option :width, default: 320
13
113
# option :height, default: 240
14
114
15
- ## Use before_body block to pre-initialize variables to use in body and
115
+ ## Use before_body block to pre-initialize variables to use in body and
16
116
# to setup application menu
17
117
#
18
118
before_body do
19
- @greeting = Model ::Greeting . new
20
- @settings = Model ::Settings . new
119
+ @game = Model ::Game . new
21
120
22
- menu ( 'File ' ) {
23
- menu_item ( 'Preferences... ' ) {
121
+ menu ( 'Game ' ) {
122
+ menu_item ( 'New Game ' ) {
24
123
on_clicked do
25
- display_preferences_dialog
124
+ on_create = lambda { |game |
125
+ @game . height = game . height
126
+ @game . width = game . width
127
+ }
128
+ new_game_window ( on_create : on_create ) . show
26
129
end
27
130
}
131
+ menu_item ( 'Edit Current Game' )
132
+ menu_item ( 'Load Game' )
28
133
29
134
# Enables quitting with CMD+Q on Mac with Mac Quit menu item
30
135
quit_menu_item if OS . mac?
@@ -46,51 +151,23 @@ class Rubygo
46
151
}
47
152
end
48
153
49
- ## Use after_body block to setup observers for controls in body
50
- #
51
- # after_body do
52
- #
53
- # end
54
-
55
- ## Add control content inside custom window body
56
- ## Top-most control must be a window or another custom window
57
- #
58
154
body {
59
155
window {
60
156
# Replace example content below with your own custom window content
61
- width <= [ @settings , :num_cols , on_read : -> ( num_cols ) { num_cols * @settings . board_scale } ]
62
- height <= [ @settings , :num_rows , on_read : -> ( num_rows ) { num_rows * @settings . board_scale } ]
157
+ width <= [ @game , :width , on_read : -> ( width ) { width * @game . scale } ]
158
+ height <= [ @game , :height , on_read : -> ( height ) { height * @game . scale } ]
63
159
title 'Ruby Go'
64
160
resizable false
65
161
66
162
margined true
67
163
68
164
label {
69
- text <= [ @greeting , :text ]
165
+ text <= [ @game , :name ]
70
166
}
71
167
vertical_box {
72
- padded false
73
- @settings . num_rows . times . map do |row |
74
- horizontal_box {
75
- padded false
76
-
77
- @settings . num_cols . times . map do |column |
78
- scale = @settings . board_scale
79
- half = scale / 2
80
- area {
81
- square ( 0 , 0 , scale ) {
82
- fill r : 240 , g : 215 , b : 141 , a : 1.0
83
- }
84
- line ( half , row == 0 ? half : 0 , half , row == ( @settings . num_rows - 1 ) ? half : scale ) {
85
- stroke 0x000000
86
- }
87
- line ( column == 0 ? half : 0 , half , column == ( @settings . num_cols - 1 ) ? half : scale , half ) {
88
- stroke 0x000000
89
- }
90
- }
91
- end
92
- }
93
- end
168
+ content ( @game , :width ) {
169
+ game_board ( game : @game )
170
+ }
94
171
}
95
172
}
96
173
}
@@ -100,28 +177,7 @@ def display_about_dialog
100
177
msg_box ( 'About' , message )
101
178
end
102
179
103
- def display_preferences_dialog
104
- window {
105
- title 'Preferences'
106
- content_size 200 , 100
107
-
108
- margined true
109
-
110
- vertical_box {
111
- padded true
112
-
113
- label ( 'Greeting:' ) {
114
- stretchy false
115
- }
116
-
117
- radio_buttons {
118
- stretchy false
119
-
120
- items Model ::Greeting ::GREETINGS
121
- selected <=> [ @greeting , :text_index ]
122
- }
123
- }
124
- } . show
180
+ def display_new_game_dialog
125
181
end
126
182
end
127
183
end
0 commit comments