| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <X11/Xlib.h> |
| 22 |
#include <X11/keysym.h> |
| 23 |
|
| 24 |
#include "table.h" |
| 25 |
#include "imagelib.h" |
| 26 |
#include "cards.h" |
| 27 |
#include "taipeilib.h" |
| 28 |
|
| 29 |
static Picture *tpe_bs, *tpe_bu, *tpe_bt, *splash; |
| 30 |
static Picture *tiles[9]; |
| 31 |
|
| 32 |
static int lbx, lby, lbw, lbh; |
| 33 |
|
| 34 |
extern Display *display; |
| 35 |
extern int screen; |
| 36 |
extern Window window; |
| 37 |
extern GC gc; |
| 38 |
|
| 39 |
static void |
| 40 |
save() |
| 41 |
{ |
| 42 |
int x, y, z, samey, w=-1; |
| 43 |
FILE *f; |
| 44 |
if (!filename) return; |
| 45 |
f = fopen(filename, "w"); |
| 46 |
if (!f) |
| 47 |
{ |
| 48 |
perror(filename); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
for (z=0; z<GRID_SZ; z++) |
| 53 |
for (y=0; y<GRID_SY; y++) |
| 54 |
{ |
| 55 |
samey = 0; |
| 56 |
for (x=0; x<GRID_SX; x++) |
| 57 |
if (grid[x][y][z]) |
| 58 |
{ |
| 59 |
if (w > 20) |
| 60 |
{ |
| 61 |
w = 0; |
| 62 |
fputc('\n', f); |
| 63 |
} |
| 64 |
if (samey) |
| 65 |
{ |
| 66 |
fprintf(f, "%d,", x); |
| 67 |
w += 1; |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
fprintf(f, "-%d,%d,%d,", x, y, z); |
| 72 |
samey = 1; |
| 73 |
w += 3; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
fprintf(f, "127\n"); |
| 78 |
fclose(f); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
init() |
| 83 |
{ |
| 84 |
lbx = table_width-MARGIN-tpe_bs->w; |
| 85 |
lby = table_height-MARGIN-tpe_bs->h; |
| 86 |
lbw = tpe_bs->w; |
| 87 |
lbh = tpe_bs->h; |
| 88 |
load(0); |
| 89 |
} |
| 90 |
|
| 91 |
static int tile_count = 0; |
| 92 |
|
| 93 |
static void |
| 94 |
show_tile_count() |
| 95 |
{ |
| 96 |
int x, y, z, b4; |
| 97 |
char tmp[10]; |
| 98 |
tile_count = 0; |
| 99 |
for (x=0; x<GRID_SX; x++) |
| 100 |
for (y=0; y<GRID_SY; y++) |
| 101 |
for (z=0; z<GRID_SZ; z++) |
| 102 |
if (grid[x][y][z]) |
| 103 |
tile_count++; |
| 104 |
sprintf(tmp, "%5d%c", tile_count, tile_count%4 ? '!' : ' '); |
| 105 |
text(tmp, lbx, lby-lbh*GRID_SZ+2); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
redraw() |
| 110 |
{ |
| 111 |
int x, y, z; |
| 112 |
int dy = GRID_DZ-1; |
| 113 |
XSetForeground(display, gc, BlackPixel(display, screen)); |
| 114 |
for (x=0; x<=GRID_SX; x++) |
| 115 |
XDrawLine(display, window, gc, |
| 116 |
gx2x(x,0), gy2y(0,0)+dy, gx2x(x,0), gy2y(GRID_SY,0)+dy); |
| 117 |
for (y=0; y<=GRID_SY; y++) |
| 118 |
XDrawLine(display, window, gc, |
| 119 |
gx2x(0,0), gy2y(y,0)+dy, gx2x(GRID_SX,0), gy2y(y,0)+dy); |
| 120 |
XDrawLine(display, window, gc, gx2x(0,0), gy2y(0,0)+dy, |
| 121 |
gx2x(GRID_SX, 0), gy2y(GRID_SY, 0)+dy); |
| 122 |
XDrawLine(display, window, gc, gx2x(0,0), gy2y(GRID_SY, 0)+dy, |
| 123 |
gx2x(GRID_SX, 0), gy2y(0,0)+dy); |
| 124 |
for (z=0; z<GRID_SZ; z++) |
| 125 |
{ |
| 126 |
Picture *p; |
| 127 |
if (z < layer) |
| 128 |
p = tpe_bs; |
| 129 |
else if (z == layer) |
| 130 |
p = tpe_bt; |
| 131 |
else |
| 132 |
p = tpe_bu; |
| 133 |
put_picture(p, lbx, lby-z*lbh, 0, 0, p->w, p->h); |
| 134 |
} |
| 135 |
|
| 136 |
for (z=0; z<=layer; z++) |
| 137 |
{ |
| 138 |
for (x=GRID_SX-1; x>=0; x--) |
| 139 |
for (y=0; x+y<GRID_SX && y<GRID_SY; y++) |
| 140 |
if (grid[x+y][y][z]) |
| 141 |
put_picture(tiles[z], gx2x(x+y, z), gy2y(y, z), 0, 0, TILE_SX, TILE_SY); |
| 142 |
for (y=0; y<GRID_SY; y++) |
| 143 |
for (x=0; x+y<GRID_SY && x<GRID_SX; x++) |
| 144 |
if (grid[x][x+y][z]) |
| 145 |
put_picture(tiles[z], gx2x(x, z), gy2y(x+y, z), 0, 0, TILE_SX, TILE_SY); |
| 146 |
} |
| 147 |
|
| 148 |
show_tile_count(); |
| 149 |
} |
| 150 |
|
| 151 |
static int |
| 152 |
check_layer_buttons(int x, int y) |
| 153 |
{ |
| 154 |
int z; |
| 155 |
if (x >= lbx) |
| 156 |
{ |
| 157 |
for (z=0; z<GRID_SZ; z++) |
| 158 |
{ |
| 159 |
if (y >= lby-z*lbh && y < lby-z*lbh+lbh) |
| 160 |
{ |
| 161 |
if (layer != z) |
| 162 |
{ |
| 163 |
layer = z; |
| 164 |
invalidate(0, 0, table_width, table_height); |
| 165 |
} |
| 166 |
return 1; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
add_tile(int x, int y) |
| 175 |
{ |
| 176 |
int nx, ny, nz; |
| 177 |
int cx, cy; |
| 178 |
if (tile_count >= 144) |
| 179 |
return; |
| 180 |
nx = x2gx(x-GRID_DX/2, layer); |
| 181 |
ny = y2gy(y-GRID_DY/2, layer); |
| 182 |
nz = layer; |
| 183 |
if (nx < 0 || nx >= GRID_SX-1 |
| 184 |
|| ny < 0 || ny >= GRID_SY-1 |
| 185 |
|| nz < 0 || nz >= GRID_SZ) |
| 186 |
return; |
| 187 |
for (cx=nx-1; cx<=nx+1; cx++) |
| 188 |
if (cx >= 0 && cx < GRID_SX) |
| 189 |
for (cy=ny-1; cy<=ny+1; cy++) |
| 190 |
if (cy >= 0 && cy < GRID_SY) |
| 191 |
if (grid[cx][cy][nz]) |
| 192 |
return; |
| 193 |
grid[nx][ny][nz] = 1; |
| 194 |
invalidate_tile(nx, ny, nz); |
| 195 |
show_tile_count(); |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
remove_tile(int x, int y) |
| 200 |
{ |
| 201 |
int nx, ny, nz; |
| 202 |
for (nx=0; nx<GRID_SX; nx++) |
| 203 |
if (x >= gx2x(nx, layer) && x < gx2x(nx, layer)+TILE_SX) |
| 204 |
for (ny=0; ny<GRID_SY; ny++) |
| 205 |
if (y >= gy2y(ny, layer) && y < gy2y(ny, layer)+TILE_SY) |
| 206 |
if (grid[nx][ny][layer]) |
| 207 |
{ |
| 208 |
grid[nx][ny][layer] = 0; |
| 209 |
invalidate_tile(nx, ny, layer); |
| 210 |
show_tile_count(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
click(int x, int y, int b) |
| 216 |
{ |
| 217 |
if (get_centered_pic()) |
| 218 |
{ |
| 219 |
set_centered_pic(0); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
if (check_layer_buttons(x, y)) |
| 224 |
return; |
| 225 |
if (b == 1) |
| 226 |
add_tile(x, y); |
| 227 |
else |
| 228 |
remove_tile(x, y); |
| 229 |
} |
| 230 |
|
| 231 |
static void |
| 232 |
drag(int x, int y, int b) |
| 233 |
{ |
| 234 |
click(x, y, b); |
| 235 |
} |
| 236 |
|
| 237 |
static int |
| 238 |
check(int x1, int x2, int y1, int y2, int z1, int z2) |
| 239 |
{ |
| 240 |
int x, y, z; |
| 241 |
for (x = x1; x <= x2; x++) |
| 242 |
for (y = y1; y <= y2; y++) |
| 243 |
for (z = z1; z <= z2; z++) |
| 244 |
if (grid[x][y][z]) |
| 245 |
return 0; |
| 246 |
return 1; |
| 247 |
} |
| 248 |
|
| 249 |
static int |
| 250 |
shift(int dx, int dy, int dz) |
| 251 |
{ |
| 252 |
int x, y, z, x1=0, x2=GRID_SX-1, y1=0, y2=GRID_SY-1, z1=0, z2=GRID_SZ-1; |
| 253 |
unsigned char temp[GRID_SX][GRID_SY][GRID_SZ]; |
| 254 |
memset(temp, 0, sizeof(temp)); |
| 255 |
if (dx > 0) x2 -= dx; |
| 256 |
if (dx < 0) x1 -= dx; |
| 257 |
if (dy > 0) y2 -= dy; |
| 258 |
if (dy < 0) y1 -= dy; |
| 259 |
if (dz > 0) z2 -= dz; |
| 260 |
if (dz < 0) z1 -= dz; |
| 261 |
for (x=x1; x<=x2; x++) |
| 262 |
for (y=y1; y<=y2; y++) |
| 263 |
for (z=z1; z<=z2; z++) |
| 264 |
temp[x][y][z] = grid[x+dx][y+dy][z+dz]; |
| 265 |
memcpy(grid, temp, sizeof(temp)); |
| 266 |
invalidate(0, 0, table_width, table_height); |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
auto_center() |
| 271 |
{ |
| 272 |
int x, y, z, minx=GRID_SX, maxx=0, miny=GRID_SY, maxy=0, minz=GRID_SZ, maxz=0; |
| 273 |
for (x=0; x<GRID_SX; x++) |
| 274 |
for (y=0; y<GRID_SY; y++) |
| 275 |
for (z=0; z<GRID_SZ; z++) |
| 276 |
if (grid[x][y][z]) |
| 277 |
{ |
| 278 |
if (minx > x) minx = x; |
| 279 |
if (maxx < x) maxx = x; |
| 280 |
if (miny > y) miny = y; |
| 281 |
if (maxy < y) maxy = y; |
| 282 |
if (minz > z) minz = z; |
| 283 |
if (maxz < z) maxz = z; |
| 284 |
} |
| 285 |
shift((minx+maxx-GRID_SX+2)/2, (miny+maxy-GRID_SY+2)/2, minz); |
| 286 |
} |
| 287 |
|
| 288 |
extern char taipedit_help[]; |
| 289 |
|
| 290 |
static void |
| 291 |
key(int k, int x, int y) |
| 292 |
{ |
| 293 |
if (get_centered_pic()) |
| 294 |
{ |
| 295 |
set_centered_pic(0); |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
if (k == 3 || k == 27 || k == 'q') |
| 300 |
exit(0); |
| 301 |
|
| 302 |
if (k == KEY_F(1) || k == 'h') |
| 303 |
{ |
| 304 |
help("taipedit.html", taipedit_help); |
| 305 |
} |
| 306 |
|
| 307 |
if (k == 'l') |
| 308 |
load(1); |
| 309 |
|
| 310 |
if (k == 's') |
| 311 |
save(); |
| 312 |
|
| 313 |
if (k >= '1' && k < '1'+GRID_SZ) |
| 314 |
{ |
| 315 |
if (layer != k - '1') |
| 316 |
{ |
| 317 |
layer = k - '1'; |
| 318 |
invalidate(0, 0, table_width, table_height); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
switch (k) |
| 323 |
{ |
| 324 |
case KEY_UP: |
| 325 |
if (check(0, GRID_SX-1, 0, 0, 0, GRID_SZ-1)) |
| 326 |
shift(0, 1, 0); |
| 327 |
break; |
| 328 |
case KEY_DOWN: |
| 329 |
if (check(0, GRID_SX-1, GRID_SY-2, GRID_SY-1, 0, GRID_SZ-1)) |
| 330 |
shift(0, -1, 0); |
| 331 |
break; |
| 332 |
case KEY_LEFT: |
| 333 |
if (check(0, 0, 0, GRID_SY-1, 0, GRID_SZ-1)) |
| 334 |
shift(1, 0, 0); |
| 335 |
break; |
| 336 |
case KEY_RIGHT: |
| 337 |
if (check(GRID_SX-2, GRID_SX-1, 0, GRID_SY-1, 0, GRID_SZ-1)) |
| 338 |
shift(-1, 0, 0); |
| 339 |
break; |
| 340 |
case KEY_PGUP: |
| 341 |
if (check(0, GRID_SX-1, 0, GRID_SY-1, GRID_SZ-1, GRID_SZ-1)) |
| 342 |
shift(0, 0, -1); |
| 343 |
break; |
| 344 |
case KEY_PGDN: |
| 345 |
if (check(0, GRID_SX-1, 0, GRID_SY-1, 0, 0)) |
| 346 |
shift(0, 0, 1); |
| 347 |
break; |
| 348 |
case KEY_HOME: |
| 349 |
auto_center(); |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
static FunctionMapping fmap[] = { |
| 355 |
{ "click", (void *)click }, |
| 356 |
{ "drag", (void *)drag }, |
| 357 |
{ "init", (void *)init }, |
| 358 |
{ "key", (void *)key }, |
| 359 |
{ "redraw", (void *)redraw }, |
| 360 |
{ 0, 0 } |
| 361 |
}; |
| 362 |
|
| 363 |
int |
| 364 |
main(int argc, char **argv) |
| 365 |
{ |
| 366 |
int x, y, z; |
| 367 |
char tmp[20]; |
| 368 |
memset(grid, 0, sizeof(grid)); |
| 369 |
|
| 370 |
register_imagelib(appimglib_imagelib); |
| 371 |
register_imagelib(tile_images); |
| 372 |
init_ace(argc, argv, fmap); |
| 373 |
|
| 374 |
layer = 0; |
| 375 |
|
| 376 |
for (x=1; x<9; x++) |
| 377 |
{ |
| 378 |
sprintf(tmp, "n%d", x); |
| 379 |
tiles[x-1] = get_picture(tmp); |
| 380 |
} |
| 381 |
|
| 382 |
tpe_bs = get_picture("taipedit-bs"); |
| 383 |
tpe_bu = get_picture("taipedit-bu"); |
| 384 |
tpe_bt = get_picture("taipedit-bt"); |
| 385 |
splash = get_picture("taipedit"); |
| 386 |
set_centered_pic(splash); |
| 387 |
|
| 388 |
init_table(GRID_SX*GRID_DX+GRID_SZ*GRID_DZ+3*MARGIN + tpe_bs->w, |
| 389 |
GRID_SY*GRID_DY+GRID_SZ*GRID_DZ+2*MARGIN); |
| 390 |
|
| 391 |
filename = argv[1]; |
| 392 |
table_loop(); |
| 393 |
} |