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
));
158 WinSize
= size
.width
;
159 for (y
= 0; y
< Size
; y
++) {
160 for (x
= 0; x
< Size
; x
++) {
161 if (MAP(x
, y
) >= 0) {
162 WMResizeWidget(Button
[(int)MAP(x
, y
)], WinSize
/ Size
, WinSize
/ Size
);
163 WMMoveWidget(Button
[(int)MAP(x
, y
)], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
170 int main(int argc
, char **argv
)
176 WMInitializeApplication("Puzzle", &argc
, argv
);
178 dpy
= XOpenDisplay("");
180 printf("could not open display\n");
184 scr
= WMCreateScreen(dpy
, DefaultScreen(dpy
));
186 win
= WMCreateWindow(scr
, "puzzle");
187 WMResizeWidget(win
, WinSize
, WinSize
);
188 WMSetWindowTitle(win
, "zuPzel");
189 WMSetWindowMinSize(win
, 80, 80);
190 WMSetWindowAspectRatio(win
, 2, 2, 2, 2);
191 WMSetWindowResizeIncrements(win
, Size
, Size
);
192 WMSetViewNotifySizeChanges(WMWidgetView(win
), True
);
193 WMAddNotificationObserver(resizeObserver
, NULL
, WMViewSizeDidChangeNotification
, WMWidgetView(win
));
195 for (i
= y
= 0; y
< Size
&& i
< Size
* Size
- 1; y
++) {
196 for (x
= 0; x
< Size
&& i
< Size
* Size
- 1; x
++) {
202 hsv
.hue
= i
* 360 / (Size
* Size
- 1);
203 hsv
.saturation
= 120;
206 RHSVtoRGB(&hsv
, &col
);
208 color
= WMCreateRGBColor(scr
, col
.red
<< 8, col
.green
<< 8, col
.blue
<< 8, False
);
211 Button
[i
] = WMCreateButton(win
, WBTMomentaryLight
);
212 WMSetWidgetBackgroundColor(Button
[i
], color
);
213 WMReleaseColor(color
);
214 WMSetButtonAction(Button
[i
], buttonClick
, (void *)(uintptr_t) i
);
215 WMResizeWidget(Button
[i
], WinSize
/ Size
, WinSize
/ Size
);
216 WMMoveWidget(Button
[i
], x
* (WinSize
/ Size
), y
* (WinSize
/ Size
));
217 sprintf(buf
, "%i", i
+ 1);
218 WMSetButtonText(Button
[i
], buf
);
219 WMSetButtonTextAlignment(Button
[i
], WACenter
);
224 WMMapSubwidgets(win
);
226 WMRealizeWidget(win
);
230 WMScreenMainLoop(scr
);