6 #include <WINGs/WINGs.h>
10 #define MAX_SIZE 10*10
14 WMButton
*Button
[MAX_SIZE
];
19 #define MAP(x,y) Map[(x)+(y)*Size]
29 for (i
= 0; i
< Size
*Size
-1; i
++) {
39 MoveButton(int button
, int x
, int y
)
41 WMMoveWidget(Button
[button
], x
*(WinSize
/Size
), y
*(WinSize
/Size
));
46 SlideButton(int button
)
48 int x
=0, y
=0, done
= 0;
50 /* locate the button */
51 for (y
= 0; y
< Size
; y
++) {
52 for (x
= 0; x
< Size
; x
++) {
53 if (MAP(x
,y
) == button
) {
62 if (x
> 0 && MAP(x
-1, y
) < 0) {
64 MoveButton(button
, x
-1, y
);
66 } else if (x
< Size
-1 && MAP(x
+1, y
) < 0) {
68 MoveButton(button
, x
+1, y
);
70 } else if (y
> 0 && MAP(x
, y
-1) < 0) {
72 MoveButton(button
, x
, y
-1);
74 } else if (y
< Size
-1 && MAP(x
, y
+1) < 0) {
76 MoveButton(button
, x
, y
+1);
85 #define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;}
94 for (i
= 0; i
< Size
*Size
-1; i
++) {
100 for (i
= 0; i
< 1000; i
++) {
105 if (x
> 0) x
--; else ok
= 0;
108 if (x
< Size
-1) x
++; else ok
= 0;
111 if (y
> 0) y
--; else ok
= 0;
114 if (y
< Size
-1) y
++; else ok
= 0;
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
);
134 void buttonClick(WMWidget
*w
, void *ptr
)
138 if (SlideButton((int)(uintptr_t)ptr
)) {
142 sprintf(buffer
, "You finished the game in %i moves.", MoveCount
);
144 if (WMRunAlertPanel(WMWidgetScreen(w
), win
, "You Won!", buffer
,
145 "Wee!", "Gah! Lemme retry!", NULL
) == WAPRDefault
) {
155 static void resizeObserver(void *self
, WMNotification
*notif
)
157 WMSize size
= WMGetViewSize(WMWidgetView(win
));
160 WinSize
= size
.width
;
161 for (y
= 0; y
< Size
; y
++) {
162 for (x
= 0; x
< Size
; x
++) {
164 WMResizeWidget(Button
[(int)MAP(x
,y
)],
165 WinSize
/Size
, WinSize
/Size
);
166 WMMoveWidget(Button
[(int)MAP(x
,y
)],
167 x
*(WinSize
/Size
), y
*(WinSize
/Size
));
175 int main(int argc
, char **argv
)
181 WMInitializeApplication("Puzzle", &argc
, argv
);
184 dpy
= XOpenDisplay("");
186 printf("could not open display\n");
190 scr
= WMCreateScreen(dpy
, DefaultScreen(dpy
));
192 win
= WMCreateWindow(scr
, "puzzle");
193 WMResizeWidget(win
, WinSize
, WinSize
);
194 WMSetWindowTitle(win
, "zuPzel");
195 WMSetWindowMinSize(win
, 80, 80);
196 WMSetWindowAspectRatio(win
, 2, 2, 2, 2);
197 WMSetWindowResizeIncrements(win
, Size
, Size
);
198 WMSetViewNotifySizeChanges(WMWidgetView(win
), True
);
199 WMAddNotificationObserver(resizeObserver
, NULL
,
200 WMViewSizeDidChangeNotification
,
205 for (i
= y
= 0; y
< Size
&& i
< Size
*Size
-1; y
++) {
206 for (x
= 0; x
< Size
&& i
< Size
*Size
-1; x
++) {
212 hsv
.hue
= i
*360/(Size
*Size
-1);
213 hsv
.saturation
= 120;
216 RHSVtoRGB(&hsv
, &col
);
218 color
= WMCreateRGBColor(scr
, col
.red
<<8, col
.green
<<8,
222 Button
[i
] = WMCreateButton(win
, WBTMomentaryLight
);
223 WMSetWidgetBackgroundColor(Button
[i
], color
);
224 WMReleaseColor(color
);
225 WMSetButtonAction(Button
[i
], buttonClick
, (void*)(uintptr_t)i
);
226 WMResizeWidget(Button
[i
], WinSize
/Size
, WinSize
/Size
);
227 WMMoveWidget(Button
[i
], x
*(WinSize
/Size
), y
*(WinSize
/Size
));
228 sprintf(buf
, "%i", i
+1);
229 WMSetButtonText(Button
[i
], buf
);
230 WMSetButtonTextAlignment(Button
[i
], WACenter
);
235 WMMapSubwidgets(win
);
237 WMRealizeWidget(win
);
241 WMScreenMainLoop(scr
);