Skip to content

Commit c4e759a

Browse files
committed
Add dynamic grid sizing
1 parent 21385e5 commit c4e759a

File tree

7 files changed

+183
-112
lines changed

7 files changed

+183
-112
lines changed

.byebug_history

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
quit
2+
c
3+
game.height
4+
game
5+
c
6+
game.height
7+
game
8+
quit
9+
game
10+
c
11+
n
12+
c
13+
n
14+
@game.width
15+
@game
16+
n
17+
where
18+
help
19+
?
20+
where
21+
whereami
22+
@game.width
23+
@game.height
24+
@game
25+
game.width
26+
game.height
27+
game
28+
c
29+
self.methods
30+
self.@game
31+
self.game
32+
self
33+
@game.width
34+
@game
35+
game.width
36+
game
37+
c
38+
n
39+
@game
40+
self
41+
settings[:settings].num_rows
42+
settings[:settings].methods
43+
settings[:settings]
44+
settings

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ group :development do
1111
gem 'juwelier', '2.4.9'
1212
gem 'simplecov', '>= 0'
1313
end
14+
15+
gem "byebug", "~> 11.1"

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ GEM
1818
base64 (0.2.0)
1919
bigdecimal (3.1.8)
2020
builder (3.3.0)
21+
byebug (11.1.3)
2122
chunky_png (1.4.0)
2223
color (1.8)
2324
concurrent-ruby (1.3.3)
@@ -199,6 +200,7 @@ PLATFORMS
199200
x86_64-linux
200201

201202
DEPENDENCIES
203+
byebug (~> 11.1)
202204
glimmer-dsl-libui (~> 0.11.4)
203205
juwelier (= 2.4.9)
204206
rspec (~> 3.5.0)

app/rubygo/model/game.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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

app/rubygo/model/greeting.rb

-25
This file was deleted.

app/rubygo/model/settings.rb

-23
This file was deleted.

app/rubygo/view/rubygo.rb

+120-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,105 @@
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
3103

4104
class Rubygo
5105
module View
@@ -12,19 +112,24 @@ class Rubygo
12112
# option :width, default: 320
13113
# option :height, default: 240
14114

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
16116
# to setup application menu
17117
#
18118
before_body do
19-
@greeting = Model::Greeting.new
20-
@settings = Model::Settings.new
119+
@game = Model::Game.new
21120

22-
menu('File') {
23-
menu_item('Preferences...') {
121+
menu('Game') {
122+
menu_item('New Game') {
24123
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
26129
end
27130
}
131+
menu_item('Edit Current Game')
132+
menu_item('Load Game')
28133

29134
# Enables quitting with CMD+Q on Mac with Mac Quit menu item
30135
quit_menu_item if OS.mac?
@@ -46,51 +151,23 @@ class Rubygo
46151
}
47152
end
48153

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-
#
58154
body {
59155
window {
60156
# 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}]
63159
title 'Ruby Go'
64160
resizable false
65161

66162
margined true
67163

68164
label {
69-
text <= [@greeting, :text]
165+
text <= [@game, :name]
70166
}
71167
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+
}
94171
}
95172
}
96173
}
@@ -100,28 +177,7 @@ def display_about_dialog
100177
msg_box('About', message)
101178
end
102179

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
125181
end
126182
end
127183
end

0 commit comments

Comments
 (0)