goocanvas 0.90.6
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.
- data/ChangeLog +87 -0
- data/README +37 -0
- data/Rakefile +14 -0
- data/ext/goocanvas/Makefile +162 -0
- data/ext/goocanvas/depend +5 -0
- data/ext/goocanvas/extconf.rb +66 -0
- data/ext/goocanvas/goocanvas.def +2 -0
- data/ext/goocanvas/goocanvas.so +0 -0
- data/ext/goocanvas/rbgoo_canvasversion.h +25 -0
- data/ext/goocanvas/rbgoocairo.c +74 -0
- data/ext/goocanvas/rbgoocairo.o +0 -0
- data/ext/goocanvas/rbgoocanvas.c +236 -0
- data/ext/goocanvas/rbgoocanvas.h +66 -0
- data/ext/goocanvas/rbgoocanvas.o +0 -0
- data/ext/goocanvas/rbgoocanvasellipse.c +50 -0
- data/ext/goocanvas/rbgoocanvasellipse.o +0 -0
- data/ext/goocanvas/rbgoocanvasgroup.c +41 -0
- data/ext/goocanvas/rbgoocanvasgroup.o +0 -0
- data/ext/goocanvas/rbgoocanvasimage.c +45 -0
- data/ext/goocanvas/rbgoocanvasimage.o +0 -0
- data/ext/goocanvas/rbgoocanvasitem.c +358 -0
- data/ext/goocanvas/rbgoocanvasitem.o +0 -0
- data/ext/goocanvas/rbgoocanvaspolyline.c +102 -0
- data/ext/goocanvas/rbgoocanvaspolyline.o +0 -0
- data/ext/goocanvas/rbgoocanvasrect.c +47 -0
- data/ext/goocanvas/rbgoocanvasrect.o +0 -0
- data/ext/goocanvas/rbgoocanvasstyle.c +61 -0
- data/ext/goocanvas/rbgoocanvasstyle.o +0 -0
- data/ext/goocanvas/rbgoocanvastable.c +41 -0
- data/ext/goocanvas/rbgoocanvastable.o +0 -0
- data/ext/goocanvas/rbgoocanvastext.c +58 -0
- data/ext/goocanvas/rbgoocanvastext.o +0 -0
- data/ext/goocanvas/rbgoocanvaswidget.c +48 -0
- data/ext/goocanvas/rbgoocanvaswidget.o +0 -0
- data/ext/goocanvas/rbgooutils.c +44 -0
- data/ext/goocanvas/rbgooutils.o +0 -0
- data/ext/goocanvas/ruby-goocanvas.pc +3 -0
- data/extconf.rb +49 -0
- data/lib/goocanvas.rb +145 -0
- data/sample/demo-arrowhead.rb +315 -0
- data/sample/demo-fifteen.rb +218 -0
- data/sample/demo-primitives.rb +720 -0
- data/sample/demo.rb +84 -0
- data/sample/flower.png +0 -0
- data/sample/scalability-demo.rb +130 -0
- data/sample/simple-demo.rb +35 -0
- data/sample/table-demo.rb +137 -0
- data/sample/toroid.png +0 -0
- data/sample/units-demo.rb +80 -0
- data/sample/widgets-demo.rb +197 -0
- metadata +133 -0
@@ -0,0 +1,315 @@
|
|
1
|
+
class CanvasSampleArrowhead < Gtk::VBox
|
2
|
+
LEFT = 50.0
|
3
|
+
RIGHT = 350.0
|
4
|
+
MIDDLE = 150.0
|
5
|
+
DEFAULT_WIDTH = 2
|
6
|
+
DEFAULT_SHAPE_A = 4
|
7
|
+
DEFAULT_SHAPE_B = 5
|
8
|
+
DEFAULT_SHAPE_C = 4
|
9
|
+
|
10
|
+
def initialize()
|
11
|
+
super(false, 4)
|
12
|
+
border_width = 4
|
13
|
+
show()
|
14
|
+
|
15
|
+
w = Gtk::Label.new <<-END
|
16
|
+
This demo allows you to edit arrowhead shapes. Drag the little boxes
|
17
|
+
to change the shape of the line and its arrowhead. You can see the
|
18
|
+
arrows at their normal scale on the right hand side of the window.
|
19
|
+
END
|
20
|
+
pack_start(w, false, false, 0)
|
21
|
+
w.show
|
22
|
+
|
23
|
+
w = Gtk::Alignment.new(0.5, 0.5, 0.0, 0.0)
|
24
|
+
pack_start(w, true, true, 0)
|
25
|
+
w.show
|
26
|
+
|
27
|
+
frame = Gtk::Frame.new
|
28
|
+
frame.shadow_type = Gtk::SHADOW_IN
|
29
|
+
w.add(frame)
|
30
|
+
frame.show
|
31
|
+
|
32
|
+
canvas = Goo::Canvas.new
|
33
|
+
root = canvas.root_item
|
34
|
+
|
35
|
+
canvas.set_size_request(500, 350)
|
36
|
+
canvas.set_bounds(0, 0, 500, 350)
|
37
|
+
frame.add(canvas)
|
38
|
+
canvas.show
|
39
|
+
|
40
|
+
canvas.instance_variable_set(:@width, DEFAULT_WIDTH)
|
41
|
+
canvas.instance_variable_set(:@shape_a, DEFAULT_SHAPE_A)
|
42
|
+
canvas.instance_variable_set(:@shape_b, DEFAULT_SHAPE_B)
|
43
|
+
canvas.instance_variable_set(:@shape_c, DEFAULT_SHAPE_C)
|
44
|
+
|
45
|
+
# Big arrow
|
46
|
+
|
47
|
+
item = Goo::CanvasPolyline.new_line(root,
|
48
|
+
LEFT, MIDDLE, RIGHT, MIDDLE,
|
49
|
+
:stroke_color => "mediumseagreen",
|
50
|
+
:end_arrow => true)
|
51
|
+
canvas.instance_variable_set(:@big_arrow, item)
|
52
|
+
|
53
|
+
# Arrow outline
|
54
|
+
|
55
|
+
item = Goo::CanvasPolyline.new(root, true, [],
|
56
|
+
:stroke_color, "black",
|
57
|
+
:line_width => 2.0,
|
58
|
+
:line_cap => Cairo::LINE_CAP_ROUND,
|
59
|
+
:line_join => Cairo::LINE_JOIN_ROUND)
|
60
|
+
canvas.instance_variable_set(:@outline, item)
|
61
|
+
|
62
|
+
# Drag boxes
|
63
|
+
|
64
|
+
create_drag_box(canvas, root, "width_drag_box")
|
65
|
+
create_drag_box(canvas, root, "shape_a_drag_box")
|
66
|
+
create_drag_box(canvas, root, "shape_b_c_drag_box")
|
67
|
+
|
68
|
+
# Dimensions
|
69
|
+
|
70
|
+
create_dimension(canvas, root, "width_arrow", "width_text", Gtk::ANCHOR_E)
|
71
|
+
create_dimension(canvas, root, "shape_a_arrow", "shape_a_text", Gtk::ANCHOR_N)
|
72
|
+
create_dimension(canvas, root, "shape_b_arrow", "shape_b_text", Gtk::ANCHOR_N)
|
73
|
+
create_dimension(canvas, root, "shape_c_arrow", "shape_c_text", Gtk::ANCHOR_W)
|
74
|
+
|
75
|
+
# Info
|
76
|
+
|
77
|
+
create_info(canvas, root, "width_info", LEFT, 260)
|
78
|
+
create_info(canvas, root, "shape_a_info", LEFT, 280)
|
79
|
+
create_info(canvas, root, "shape_b_info", LEFT, 300)
|
80
|
+
create_info(canvas, root, "shape_c_info", LEFT, 320)
|
81
|
+
|
82
|
+
# Division line
|
83
|
+
|
84
|
+
Goo::CanvasPolyline.new_line(root, RIGHT + 50, 0, RIGHT + 50, 1000,
|
85
|
+
:fill_color => "black", :line_width => 2.0)
|
86
|
+
|
87
|
+
# Sample arrows
|
88
|
+
|
89
|
+
create_sample_arrow(canvas, root, "sample_1",
|
90
|
+
RIGHT + 100, 30, RIGHT + 100, MIDDLE - 30)
|
91
|
+
create_sample_arrow(canvas, root, "sample_2",
|
92
|
+
RIGHT + 70, MIDDLE, RIGHT + 130, MIDDLE)
|
93
|
+
create_sample_arrow(canvas, root, "sample_3",
|
94
|
+
RIGHT + 70, MIDDLE + 30, RIGHT + 130, MIDDLE + 120)
|
95
|
+
|
96
|
+
# Done!
|
97
|
+
set_arrow_shape(canvas)
|
98
|
+
end
|
99
|
+
|
100
|
+
def set_dimension(canvas, arrow_name, text_name, x1, y1, x2, y2, tx, ty, dim)
|
101
|
+
points = [ x1, y1, x2, y2 ]
|
102
|
+
|
103
|
+
arrow = canvas.instance_variable_get("@#{arrow_name}")
|
104
|
+
arrow.points = points
|
105
|
+
|
106
|
+
text = canvas.instance_variable_get("@#{text_name}")
|
107
|
+
text.text = dim.to_s
|
108
|
+
text.x = tx
|
109
|
+
text.y = ty
|
110
|
+
end
|
111
|
+
|
112
|
+
def move_drag_box(item, x, y)
|
113
|
+
item.x = x - 5.0
|
114
|
+
item.y = y - 5.0
|
115
|
+
end
|
116
|
+
|
117
|
+
def set_arrow_shape(canvas)
|
118
|
+
width = canvas.instance_variable_get(:@width)
|
119
|
+
shape_a = canvas.instance_variable_get(:@shape_a)
|
120
|
+
shape_b = canvas.instance_variable_get(:@shape_b)
|
121
|
+
shape_c = canvas.instance_variable_get(:@shape_c)
|
122
|
+
|
123
|
+
# Big arrow
|
124
|
+
|
125
|
+
big_arrow = canvas.instance_variable_get(:@big_arrow)
|
126
|
+
big_arrow.line_width = 10.0 * width
|
127
|
+
big_arrow.arrow_tip_length = shape_a
|
128
|
+
big_arrow.arrow_length = shape_b
|
129
|
+
big_arrow.arrow_width = shape_c
|
130
|
+
|
131
|
+
# Outline
|
132
|
+
|
133
|
+
points = []
|
134
|
+
points[0] = RIGHT - 10 * shape_a * width
|
135
|
+
points[1] = MIDDLE - 10 * width / 2
|
136
|
+
points[2] = RIGHT - 10 * shape_b * width
|
137
|
+
points[3] = MIDDLE - 10 * (shape_c * width / 2.0)
|
138
|
+
points[4] = RIGHT
|
139
|
+
points[5] = MIDDLE
|
140
|
+
points[6] = points[2]
|
141
|
+
points[7] = MIDDLE + 10 * (shape_c * width / 2.0)
|
142
|
+
points[8] = points[0]
|
143
|
+
points[9] = MIDDLE + 10 * width / 2
|
144
|
+
|
145
|
+
outline = canvas.instance_variable_get(:@outline)
|
146
|
+
outline.points = points
|
147
|
+
|
148
|
+
# Drag boxes
|
149
|
+
move_drag_box(canvas.instance_variable_get(:@width_drag_box), LEFT, MIDDLE - 10 * width / 2.0)
|
150
|
+
move_drag_box(canvas.instance_variable_get(:@shape_a_drag_box), RIGHT - 10 * shape_a * width, MIDDLE)
|
151
|
+
move_drag_box(canvas.instance_variable_get(:@shape_b_c_drag_box), RIGHT - 10 * shape_b * width, MIDDLE - 10 * (shape_c * width / 2.0))
|
152
|
+
|
153
|
+
# Dimensions
|
154
|
+
|
155
|
+
set_dimension(canvas, "width_arrow", "width_text",
|
156
|
+
LEFT - 10,
|
157
|
+
MIDDLE - 10 * width / 2.0,
|
158
|
+
LEFT - 10,
|
159
|
+
MIDDLE + 10 * width / 2.0,
|
160
|
+
LEFT - 15,
|
161
|
+
MIDDLE,
|
162
|
+
width)
|
163
|
+
|
164
|
+
set_dimension(canvas, "shape_a_arrow", "shape_a_text",
|
165
|
+
RIGHT - 10 * shape_a * width,
|
166
|
+
MIDDLE + 10 * (shape_c * width / 2.0) + 10,
|
167
|
+
RIGHT,
|
168
|
+
MIDDLE + 10 * (shape_c * width / 2.0) + 10,
|
169
|
+
RIGHT - 10 * shape_a * width / 2.0,
|
170
|
+
MIDDLE + 10 * (shape_c * width / 2.0) + 15,
|
171
|
+
shape_a)
|
172
|
+
|
173
|
+
set_dimension(canvas, "shape_b_arrow", "shape_b_text",
|
174
|
+
RIGHT - 10 * shape_b * width,
|
175
|
+
MIDDLE + 10 * (shape_c * width / 2.0) + 35,
|
176
|
+
RIGHT,
|
177
|
+
MIDDLE + 10 * (shape_c * width / 2.0) + 35,
|
178
|
+
RIGHT - 10 * shape_b * width / 2.0,
|
179
|
+
MIDDLE + 10 * (shape_c * width / 2.0) + 40,
|
180
|
+
shape_b)
|
181
|
+
|
182
|
+
set_dimension(canvas, "shape_c_arrow", "shape_c_text",
|
183
|
+
RIGHT + 10,
|
184
|
+
MIDDLE - 10 * shape_c * width / 2.0,
|
185
|
+
RIGHT + 10,
|
186
|
+
MIDDLE + 10 * shape_c * width / 2.0,
|
187
|
+
RIGHT + 15,
|
188
|
+
MIDDLE,
|
189
|
+
shape_c)
|
190
|
+
|
191
|
+
# Info
|
192
|
+
|
193
|
+
width_info = canvas.instance_variable_get(:@width_info)
|
194
|
+
width_info.text = "line-width: #{width}"
|
195
|
+
|
196
|
+
shape_a_info = canvas.instance_variable_get(:@shape_a_info)
|
197
|
+
shape_a_info.text = "arrow-tip-length: #{shape_a} (* line-width)"
|
198
|
+
|
199
|
+
shape_b_info = canvas.instance_variable_get(:@shape_b_info)
|
200
|
+
shape_b_info.text = "arrow-length: #{shape_b} (* line-width)"
|
201
|
+
|
202
|
+
shape_c_info = canvas.instance_variable_get(:@shape_c_info)
|
203
|
+
shape_c_info.text = "arrow-length: #{shape_c} (* line-width)"
|
204
|
+
|
205
|
+
# Sample arrows
|
206
|
+
|
207
|
+
sample_1 = canvas.instance_variable_get(:@sample_1)
|
208
|
+
sample_1.line_width = width
|
209
|
+
sample_1.arrow_tip_length = shape_a
|
210
|
+
sample_1.arrow_length = shape_b
|
211
|
+
sample_1.arrow_width = shape_c
|
212
|
+
sample_2 = canvas.instance_variable_get(:@sample_2)
|
213
|
+
sample_2.line_width = width
|
214
|
+
sample_2.arrow_tip_length = shape_a
|
215
|
+
sample_2.arrow_length = shape_b
|
216
|
+
sample_2.arrow_width = shape_c
|
217
|
+
sample_3 = canvas.instance_variable_get(:@sample_3)
|
218
|
+
sample_3.line_width = width
|
219
|
+
sample_3.arrow_tip_length = shape_a
|
220
|
+
sample_3.arrow_length = shape_b
|
221
|
+
sample_3.arrow_width = shape_c
|
222
|
+
end
|
223
|
+
|
224
|
+
def create_dimension(canvas, root, arrow_name, text_name, anchor)
|
225
|
+
item = Goo::CanvasPolyline.new(root, false, 0,
|
226
|
+
:fill_color => "black",
|
227
|
+
:start_arrow => true,
|
228
|
+
:end_arrow => true)
|
229
|
+
canvas.instance_variable_set("@#{arrow_name}", item)
|
230
|
+
|
231
|
+
item = Goo::CanvasText.new(root, nil, 0, 0, -1, anchor,
|
232
|
+
:fill_color => "black",
|
233
|
+
:font => "Sans 12")
|
234
|
+
canvas.instance_variable_set("@#{text_name}", item)
|
235
|
+
end
|
236
|
+
|
237
|
+
def create_info(canvas, root, info_name, x, y)
|
238
|
+
item = Goo::CanvasText.new(root, nil, x, y, -1, Gtk::ANCHOR_NW,
|
239
|
+
:fill_color => "black",
|
240
|
+
:font => "Sans 14")
|
241
|
+
canvas.instance_variable_set("@#{info_name}", item)
|
242
|
+
end
|
243
|
+
|
244
|
+
def create_sample_arrow(canvas, root, sample_name, x1, y1, x2, y2)
|
245
|
+
item = Goo::CanvasPolyline.new_line(root, x1, y1, x2, y2,
|
246
|
+
:start_arrow => true,
|
247
|
+
:end_arrow => true)
|
248
|
+
canvas.instance_variable_set("@#{sample_name}", item)
|
249
|
+
end
|
250
|
+
|
251
|
+
def create_drag_box(canvas, root, box_name)
|
252
|
+
item = Goo::CanvasRect.new(root, 0, 0, 10, 10,
|
253
|
+
:fill_color => 'black',
|
254
|
+
:stroke_color => 'black',
|
255
|
+
:line_width => 1.0)
|
256
|
+
canvas.instance_variable_set("@#{box_name}", item)
|
257
|
+
|
258
|
+
item.signal_connect('enter_notify_event') do
|
259
|
+
item.fill_color = 'red'
|
260
|
+
true
|
261
|
+
end
|
262
|
+
item.signal_connect('leave_notify_event') do
|
263
|
+
item.fill_color = 'black'
|
264
|
+
true
|
265
|
+
end
|
266
|
+
item.signal_connect('button_press_event') do |item, target, event|
|
267
|
+
fleur = Gdk::Cursor.new(Gdk::Cursor::FLEUR)
|
268
|
+
canvas.pointer_grab(item, Gdk::Event::POINTER_MOTION_MASK | Gdk::Event::BUTTON_RELEASE_MASK, fleur, event.time)
|
269
|
+
true
|
270
|
+
end
|
271
|
+
item.signal_connect('button_release_event') do |item, target, event|
|
272
|
+
canvas.pointer_ungrab(item, event.time)
|
273
|
+
true
|
274
|
+
end
|
275
|
+
item.signal_connect('motion_notify_event') do |item, target, event|
|
276
|
+
catch :done do
|
277
|
+
throw :done, false unless event.state & Gdk::Window::BUTTON1_MASK == Gdk::Window::BUTTON1_MASK
|
278
|
+
|
279
|
+
if item == canvas.instance_variable_get(:@width_drag_box)
|
280
|
+
y = event.y
|
281
|
+
width = (MIDDLE - y) / 5
|
282
|
+
throw :done, false if width < 0
|
283
|
+
canvas.instance_variable_set(:@width, width)
|
284
|
+
set_arrow_shape(canvas)
|
285
|
+
elsif item == canvas.instance_variable_get(:@shape_a_drag_box)
|
286
|
+
x = event.x
|
287
|
+
width = canvas.instance_variable_get(:@width)
|
288
|
+
shape_a = (RIGHT - x) / 10 / width
|
289
|
+
throw :done, false if (shape_a < 0) || (shape_a > 30)
|
290
|
+
width = canvas.instance_variable_set(:@shape_a, shape_a)
|
291
|
+
set_arrow_shape(canvas)
|
292
|
+
elsif item == canvas.instance_variable_get(:@shape_b_c_drag_box)
|
293
|
+
change = false
|
294
|
+
width = canvas.instance_variable_get(:@width)
|
295
|
+
x = event.x
|
296
|
+
shape_b = (RIGHT - x) / 10 / width
|
297
|
+
if (shape_b >= 0) && (shape_b <= 30)
|
298
|
+
canvas.instance_variable_set(:@shape_b, shape_b)
|
299
|
+
change = true
|
300
|
+
end
|
301
|
+
|
302
|
+
y = event.y
|
303
|
+
shape_c = (MIDDLE - y) * 2 / 10 / width
|
304
|
+
if shape_c >= 0
|
305
|
+
canvas.instance_variable_set(:@shape_c, shape_c)
|
306
|
+
change = true
|
307
|
+
end
|
308
|
+
|
309
|
+
set_arrow_shape(canvas) if change
|
310
|
+
end
|
311
|
+
true
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
# -*- indent-tabs-mode: nil -*-
|
2
|
+
=begin header
|
3
|
+
|
4
|
+
demo-fifteen.rb - Canvas test rewritten in Ruby/GNOME
|
5
|
+
|
6
|
+
Rewritten by Emmanuel Pinault <seatmanu@yahoo.com>
|
7
|
+
|
8
|
+
Original Copyright:
|
9
|
+
|
10
|
+
Author : Richard Hestilow <hestgray@ionet.net>
|
11
|
+
|
12
|
+
Copyright (C) 1998 Free Software Foundation
|
13
|
+
|
14
|
+
This program is free software; you can redistribute it and/or modify
|
15
|
+
it under the terms of the GNU General Public License as published by
|
16
|
+
the Free Software Foundation; either version 2, or (at your option)
|
17
|
+
any later version.
|
18
|
+
|
19
|
+
This program is distributed in the hope that it will be useful,
|
20
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
+
GNU General Public License for more details.
|
23
|
+
|
24
|
+
You should have received a copy of the GNU General Public License
|
25
|
+
along with this program; if not, write to the Free Software
|
26
|
+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
27
|
+
02111-1307, USA.
|
28
|
+
|
29
|
+
=end
|
30
|
+
|
31
|
+
class CanvasSampleFifteen < Gtk::VBox
|
32
|
+
PIECE_SIZE = 50
|
33
|
+
SCRAMBLE_MOVES = 32
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
super(false, 4)
|
37
|
+
border_width = 4
|
38
|
+
show()
|
39
|
+
|
40
|
+
alignment = Gtk::Alignment.new(0.5, 0.5, 0.0, 0.0)
|
41
|
+
pack_start(alignment, true, true, 0)
|
42
|
+
alignment.show()
|
43
|
+
|
44
|
+
frame = Gtk::Frame.new
|
45
|
+
frame.set_shadow_type(Gtk::SHADOW_IN);
|
46
|
+
alignment.add(frame)
|
47
|
+
frame.show()
|
48
|
+
|
49
|
+
# Create the canvas and board
|
50
|
+
|
51
|
+
@canvas = Goo::Canvas.new()
|
52
|
+
@canvas.set_size_request(PIECE_SIZE * 4 + 1, PIECE_SIZE * 4 + 1)
|
53
|
+
#@canvas.set_scroll_region(0, 0, PIECE_SIZE * 4 + 1, PIECE_SIZE * 4 + 1)
|
54
|
+
frame.add(@canvas)
|
55
|
+
@canvas.show()
|
56
|
+
|
57
|
+
@board = Array.new(16)
|
58
|
+
|
59
|
+
0.upto(14) do |i|
|
60
|
+
@board[i] = Piece.new(@canvas.root_item)
|
61
|
+
@board[i].setup(self, i)
|
62
|
+
end
|
63
|
+
@board[15] = nil;
|
64
|
+
|
65
|
+
# Scramble button
|
66
|
+
button = Gtk::Button.new("Scramble")
|
67
|
+
pack_start(button, false, false, 0)
|
68
|
+
button.signal_connect("clicked") do |button|
|
69
|
+
scramble()
|
70
|
+
end
|
71
|
+
button.show()
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_win
|
75
|
+
0.upto(14) do |i|
|
76
|
+
if @board[i].nil? || @board[i].num != i
|
77
|
+
return
|
78
|
+
end
|
79
|
+
end
|
80
|
+
dialog = Gtk::MessageDialog.new(parent_window,
|
81
|
+
Gtk::Dialog::DESTROY_WITH_PARENT,
|
82
|
+
Gtk::MessageDialog::INFO,
|
83
|
+
Gtk::MessageDialog::BUTTONS_OK,
|
84
|
+
"You stud, you win!")
|
85
|
+
dialog.set_modal(true)
|
86
|
+
dialog.run_and_close()
|
87
|
+
end
|
88
|
+
|
89
|
+
def piece_enter_notify(item)
|
90
|
+
item.text.set_property(:fill_color, "white")
|
91
|
+
end
|
92
|
+
|
93
|
+
def piece_leave_notify(item)
|
94
|
+
item.text.set_property(:fill_color, "black")
|
95
|
+
end
|
96
|
+
|
97
|
+
def piece_button_press(item)
|
98
|
+
y = item.pos / 4
|
99
|
+
x = item.pos % 4
|
100
|
+
|
101
|
+
move = true
|
102
|
+
|
103
|
+
if (y > 0) && @board[(y - 1) * 4 + x].nil?
|
104
|
+
dx = 0.0
|
105
|
+
dy = -1.0
|
106
|
+
y -= 1
|
107
|
+
elsif (y < 3) && @board[(y + 1) * 4 + x].nil?
|
108
|
+
dx = 0.0
|
109
|
+
dy = 1.0
|
110
|
+
y += 1
|
111
|
+
elsif (x > 0) && @board[y * 4 + x - 1].nil?
|
112
|
+
dx = -1.0
|
113
|
+
dy = 0.0
|
114
|
+
x -= 1
|
115
|
+
elsif (x < 3) && @board[y * 4 + x + 1].nil?
|
116
|
+
dx = 1.0
|
117
|
+
dy = 0.0
|
118
|
+
x += 1
|
119
|
+
else
|
120
|
+
move = false
|
121
|
+
end
|
122
|
+
|
123
|
+
if move
|
124
|
+
newpos = y * 4 + x
|
125
|
+
@board[item.pos] = nil
|
126
|
+
@board[newpos] = item
|
127
|
+
item.pos = newpos
|
128
|
+
item.translate(dx * PIECE_SIZE, dy * PIECE_SIZE)
|
129
|
+
test_win()
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
def scramble
|
135
|
+
srand()
|
136
|
+
# First, find the blank spot
|
137
|
+
pos = 0
|
138
|
+
0.upto(15) do |i|
|
139
|
+
if @board[i].nil?
|
140
|
+
pos = i
|
141
|
+
break
|
142
|
+
end
|
143
|
+
end
|
144
|
+
# "Move the blank spot" around in order to scramble the pieces
|
145
|
+
|
146
|
+
0.upto(SCRAMBLE_MOVES) do
|
147
|
+
dir = rand(4).to_i
|
148
|
+
x = y = 0
|
149
|
+
|
150
|
+
if (dir == 0) && (pos > 3) # up
|
151
|
+
y = -1
|
152
|
+
elsif (dir == 1) && (pos < 12) # down
|
153
|
+
y = 1
|
154
|
+
elsif (dir == 2) && ((pos % 4) != 0) # left
|
155
|
+
x = -1
|
156
|
+
elsif (dir == 3) && ((pos % 4) != 3) # right
|
157
|
+
x = 1
|
158
|
+
else
|
159
|
+
retry
|
160
|
+
end
|
161
|
+
|
162
|
+
oldpos = pos + y * 4 + x;
|
163
|
+
@board[pos] = @board[oldpos];
|
164
|
+
@board[oldpos] = nil
|
165
|
+
@board[pos].pos = pos
|
166
|
+
@board[pos].translate(-x * PIECE_SIZE, -y * PIECE_SIZE)
|
167
|
+
pos = oldpos
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
class Piece < Goo::CanvasGroup
|
172
|
+
attr_reader :text, :num, :pos
|
173
|
+
attr_writer :pos
|
174
|
+
|
175
|
+
def initialize(*arg)
|
176
|
+
super(*arg)
|
177
|
+
end
|
178
|
+
|
179
|
+
def setup(app, i)
|
180
|
+
y = i / 4
|
181
|
+
x = i % 4
|
182
|
+
translate( x * PIECE_SIZE,y * PIECE_SIZE)
|
183
|
+
|
184
|
+
Goo::CanvasRect.new(self, 0, 0, PIECE_SIZE, PIECE_SIZE,
|
185
|
+
:line_width => 1.0,
|
186
|
+
:fill_color => get_piece_color(x, y),
|
187
|
+
:stroke_color => "black")
|
188
|
+
|
189
|
+
|
190
|
+
@text = Goo::CanvasText.new(self,i.to_s, PIECE_SIZE / 2.0,
|
191
|
+
PIECE_SIZE / 2.0, -1, Gtk::ANCHOR_CENTER,
|
192
|
+
:font=>"Sans bold 24",
|
193
|
+
:fill_color => "black")
|
194
|
+
@num = i
|
195
|
+
@pos = i
|
196
|
+
|
197
|
+
self.signal_connect("enter_notify_event") do |item, event|
|
198
|
+
app.piece_enter_notify(item)
|
199
|
+
end
|
200
|
+
|
201
|
+
self.signal_connect("leave_notify_event") do |item, event|
|
202
|
+
app.piece_leave_notify(item)
|
203
|
+
end
|
204
|
+
|
205
|
+
self.signal_connect("button_press_event") do |item, event|
|
206
|
+
app.piece_button_press(item)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
def get_piece_color(x, y)
|
212
|
+
r = ((4 - x) * 255) / 4;
|
213
|
+
g = ((4 - y) * 255) / 4;
|
214
|
+
b = 128;
|
215
|
+
return sprintf("#%02x%02x%02x", r, g, b)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|