9 WMButton
*Button
[MAX_SIZE
];
14 #define MAP(x,y) Map[(x)+(y)*Size]
22 for (i
= 0; i
< Size
* Size
- 1; i
++) {
30 void MoveButton(int button
, int x
, int y
)
32 WMMoveWidget(Button
[button
], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
35 Bool
SlideButton(int button
)
39 /* locate the button */
40 for (y
= 0; y
< Size
; y
++) {
41 for (x
= 0; x
< Size
; x
++) {
42 if (MAP(x
, y
) == button
) {
51 if (x
> 0 && MAP(x
- 1, y
) < 0) {
53 MoveButton(button
, x
- 1, y
);
54 MAP(x
- 1, y
) = button
;
55 } else if (x
< Size
- 1 && MAP(x
+ 1, y
) < 0) {
57 MoveButton(button
, x
+ 1, y
);
58 MAP(x
+ 1, y
) = button
;
59 } else if (y
> 0 && MAP(x
, y
- 1) < 0) {
61 MoveButton(button
, x
, y
- 1);
62 MAP(x
, y
- 1) = button
;
63 } else if (y
< Size
- 1 && MAP(x
, y
+ 1) < 0) {
65 MoveButton(button
, x
, y
+ 1);
66 MAP(x
, y
+ 1) = button
;
73 #define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;}
81 for (i
= 0; i
< Size
* Size
- 1; i
++) {
87 for (i
= 0; i
< 5; i
++) {
117 MoveButton(MAP(x
, y
), ox
, oy
);
119 SWAP(MAP(ox
, oy
), MAP(x
, y
));
121 while (XPending(WMScreenDisplay(WMWidgetScreen(win
)))) {
123 WMNextEvent(WMScreenDisplay(WMWidgetScreen(win
)), &ev
);
132 void buttonClick(WMWidget
* w
, void *ptr
)
136 if (SlideButton((int)ptr
)) {
140 sprintf(buffer
, "You finished the game in %i moves.", MoveCount
);
142 if (WMRunAlertPanel(WMWidgetScreen(w
), win
, "You Won!", buffer
,
143 "Wee!", "Gah! Lemme retry!", NULL
) == WAPRDefault
) {
152 static void resizeObserver(void *self
, WMNotification
* notif
)
154 WMSize size
= WMGetViewSize(WMWidgetView(win
));
157 WinSize
= size
.width
;
158 for (y
= 0; y
< Size
; y
++) {
159 for (x
= 0; x
< Size
; x
++) {
160 if (MAP(x
, y
) >= 0) {
161 WMResizeWidget(Button
[(int)MAP(x
, y
)], WinSize
/ Size
, WinSize
/ Size
);
162 WMMoveWidget(Button
[(int)MAP(x
, y
)], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
169 int main(int argc
, char **argv
)
175 WMInitializeApplication("Puzzle", &argc
, argv
);
177 dpy
= XOpenDisplay("");
179 printf("could not open display\n");
183 scr
= WMCreateScreen(dpy
, DefaultScreen(dpy
));
185 win
= WMCreateWindow(scr
, "puzzle");
186 WMResizeWidget(win
, WinSize
, WinSize
);
187 WMSetWindowTitle(win
, "zuPzel");
188 WMSetWindowMinSize(win
, 80, 80);
189 WMSetWindowAspectRatio(win
, 2, 2, 2, 2);
190 WMSetWindowResizeIncrements(win
, Size
, Size
);
191 WMSetViewNotifySizeChanges(WMWidgetView(win
), True
);
192 WMAddNotificationObserver(resizeObserver
, NULL
, WMViewSizeDidChangeNotification
, WMWidgetView(win
));
194 for (i
= y
= 0; y
< Size
&& i
< Size
* Size
- 1; y
++) {
195 for (x
= 0; x
< Size
&& i
< Size
* Size
- 1; x
++) {
201 hsv
.hue
= i
* 360 / (Size
* Size
- 1);
202 hsv
.saturation
= 120;
205 RHSVtoRGB(&hsv
, &col
);
207 color
= WMCreateRGBColor(scr
, col
.red
<< 8, col
.green
<< 8, col
.blue
<< 8, False
);
210 Button
[i
] = WMCreateButton(win
, WBTMomentaryLight
);
211 WMSetWidgetBackgroundColor(Button
[i
], color
);
212 WMReleaseColor(color
);
213 WMSetButtonAction(Button
[i
], buttonClick
, (void *)i
);
214 WMResizeWidget(Button
[i
], WinSize
/ Size
, WinSize
/ Size
);
215 WMMoveWidget(Button
[i
], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
216 sprintf(buf
, "%i", i
+ 1);
217 WMSetButtonText(Button
[i
], buf
);
218 WMSetButtonTextAlignment(Button
[i
], WACenter
);
223 WMMapSubwidgets(win
);
225 WMRealizeWidget(win
);
229 WMScreenMainLoop(scr
);