4 #include <WINGs/WINGs.h>
10 WMButton
*Button
[MAX_SIZE
];
11 signed char Map
[MAX_SIZE
];
15 #define MAP(x,y) Map[(x)+(y)*Size]
23 for (i
= 0; i
< Size
* Size
- 1; i
++) {
31 void MoveButton(int button
, int x
, int y
)
33 WMMoveWidget(Button
[button
], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
36 Bool
SlideButton(int button
)
38 int x
= 0, y
= 0, done
= 0;
40 /* locate the button */
41 for (y
= 0; y
< Size
; y
++) {
42 for (x
= 0; x
< Size
; x
++) {
43 if (MAP(x
, y
) == button
) {
52 if (x
> 0 && MAP(x
- 1, y
) < 0) {
54 MoveButton(button
, x
- 1, y
);
55 MAP(x
- 1, y
) = button
;
56 } else if (x
< Size
- 1 && MAP(x
+ 1, y
) < 0) {
58 MoveButton(button
, x
+ 1, y
);
59 MAP(x
+ 1, y
) = button
;
60 } else if (y
> 0 && MAP(x
, y
- 1) < 0) {
62 MoveButton(button
, x
, y
- 1);
63 MAP(x
, y
- 1) = button
;
64 } else if (y
< Size
- 1 && MAP(x
, y
+ 1) < 0) {
66 MoveButton(button
, x
, y
+ 1);
67 MAP(x
, y
+ 1) = button
;
74 #define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;}
82 for (i
= 0; i
< Size
* Size
- 1; i
++) {
88 for (i
= 0; i
< 1000; i
++) {
118 MoveButton(MAP(x
, y
), ox
, oy
);
120 SWAP(MAP(ox
, oy
), MAP(x
, y
));
122 while (XPending(WMScreenDisplay(WMWidgetScreen(win
)))) {
124 WMNextEvent(WMScreenDisplay(WMWidgetScreen(win
)), &ev
);
133 void buttonClick(WMWidget
* w
, void *ptr
)
137 if (SlideButton((uintptr_t)ptr
)) {
141 sprintf(buffer
, "You finished the game in %i moves.", MoveCount
);
143 if (WMRunAlertPanel(WMWidgetScreen(w
), win
, "You Won!", buffer
,
144 "Wee!", "Gah! Lemme retry!", NULL
) == WAPRDefault
) {
153 static void resizeObserver(void *self
, WMNotification
* notif
)
155 WMSize size
= WMGetViewSize(WMWidgetView(win
));
161 WinSize
= size
.width
;
162 for (y
= 0; y
< Size
; y
++) {
163 for (x
= 0; x
< Size
; x
++) {
164 if (MAP(x
, y
) >= 0) {
165 WMResizeWidget(Button
[(int)MAP(x
, y
)], WinSize
/ Size
, WinSize
/ Size
);
166 WMMoveWidget(Button
[(int)MAP(x
, y
)], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
173 int main(int argc
, char **argv
)
179 WMInitializeApplication("Puzzle", &argc
, argv
);
181 dpy
= XOpenDisplay("");
183 printf("could not open display\n");
187 scr
= WMCreateScreen(dpy
, DefaultScreen(dpy
));
189 win
= WMCreateWindow(scr
, "puzzle");
190 WMResizeWidget(win
, WinSize
, WinSize
);
191 WMSetWindowTitle(win
, "zuPzel");
192 WMSetWindowMinSize(win
, 80, 80);
193 WMSetWindowAspectRatio(win
, 2, 2, 2, 2);
194 WMSetWindowResizeIncrements(win
, Size
, Size
);
195 WMSetViewNotifySizeChanges(WMWidgetView(win
), True
);
196 WMAddNotificationObserver(resizeObserver
, NULL
, WMViewSizeDidChangeNotification
, WMWidgetView(win
));
198 for (i
= y
= 0; y
< Size
&& i
< Size
* Size
- 1; y
++) {
199 for (x
= 0; x
< Size
&& i
< Size
* Size
- 1; x
++) {
205 hsv
.hue
= i
* 360 / (Size
* Size
- 1);
206 hsv
.saturation
= 120;
209 RHSVtoRGB(&hsv
, &col
);
211 color
= WMCreateRGBColor(scr
, col
.red
<< 8, col
.green
<< 8, col
.blue
<< 8, False
);
214 Button
[i
] = WMCreateButton(win
, WBTMomentaryLight
);
215 WMSetWidgetBackgroundColor(Button
[i
], color
);
216 WMReleaseColor(color
);
217 WMSetButtonAction(Button
[i
], buttonClick
, (void *)(uintptr_t) i
);
218 WMResizeWidget(Button
[i
], WinSize
/ Size
, WinSize
/ Size
);
219 WMMoveWidget(Button
[i
], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
220 sprintf(buf
, "%i", i
+ 1);
221 WMSetButtonText(Button
[i
], buf
);
222 WMSetButtonTextAlignment(Button
[i
], WACenter
);
227 WMMapSubwidgets(win
);
229 WMRealizeWidget(win
);
233 WMScreenMainLoop(scr
);