diff options
| author | Simon Tatham <anakin@pobox.com> | 2008-09-13 18:26:53 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 2008-09-13 18:26:53 +0000 |
| commit | 5ead207060a3e1f74ad6200fdf02934457394bc2 (patch) | |
| tree | 453b3ebe7556d1a1222c0a1e23f190d6eb119893 /misc.c | |
| parent | fe1b91ac49cc2f4cd07801535dc19103a8ebeb70 (diff) | |
| download | puzzles-5ead207060a3e1f74ad6200fdf02934457394bc2.zip puzzles-5ead207060a3e1f74ad6200fdf02934457394bc2.tar.gz puzzles-5ead207060a3e1f74ad6200fdf02934457394bc2.tar.bz2 puzzles-5ead207060a3e1f74ad6200fdf02934457394bc2.tar.xz | |
Patch from James H to centralise some generally useful cursor-
handling functionality into misc.c.
[originally from svn r8176]
Diffstat (limited to 'misc.c')
| -rw-r--r-- | misc.c | 73 |
1 files changed, 73 insertions, 0 deletions
@@ -244,4 +244,77 @@ void draw_rect_outline(drawing *dr, int x, int y, int w, int h, int colour) draw_polygon(dr, coords, 4, -1, colour); } +void move_cursor(int button, int *x, int *y, int maxw, int maxh, int wrap) +{ + int dx = 0, dy = 0; + switch (button) { + case CURSOR_UP: dy = -1; break; + case CURSOR_DOWN: dy = 1; break; + case CURSOR_RIGHT: dx = 1; break; + case CURSOR_LEFT: dx = -1; break; + default: return; + } + if (wrap) { + *x = (*x + dx + maxw) % maxw; + *y = (*y + dy + maxh) % maxh; + } else { + *x = min(max(*x+dx, 0), maxw - 1); + *y = min(max(*y+dy, 0), maxh - 1); + } +} + +/* Used in netslide.c and sixteen.c for cursor movement around edge. */ + +int c2pos(int w, int h, int cx, int cy) +{ + if (cy == -1) + return cx; /* top row, 0 .. w-1 (->) */ + else if (cx == w) + return w + cy; /* R col, w .. w+h -1 (v) */ + else if (cy == h) + return w + h + (w-cx-1); /* bottom row, w+h .. w+h+w-1 (<-) */ + else if (cx == -1) + return w + h + w + (h-cy-1); /* L col, w+h+w .. w+h+w+h-1 (^) */ + + assert(!"invalid cursor pos!"); + return -1; /* not reached */ +} + +void pos2c(int w, int h, int pos, int *cx, int *cy) +{ + int max = w+h+w+h; + + pos = (pos + max) % max; + + if (pos < w) { + *cx = pos; *cy = -1; return; + } + pos -= w; + if (pos < h) { + *cx = w; *cy = pos; return; + } + pos -= h; + if (pos < w) { + *cx = w-pos-1; *cy = h; return; + } + pos -= w; + if (pos < h) { + *cx = -1; *cy = h-pos-1; return; + } + assert(!"invalid pos, huh?"); /* limited by % above! */ +} + +void draw_text_outline(drawing *dr, int x, int y, int fonttype, + int fontsize, int align, + int text_colour, int outline_colour, char *text) +{ + if (outline_colour > -1) { + draw_text(dr, x-1, y, fonttype, fontsize, align, outline_colour, text); + draw_text(dr, x+1, y, fonttype, fontsize, align, outline_colour, text); + draw_text(dr, x, y-1, fonttype, fontsize, align, outline_colour, text); + draw_text(dr, x, y+1, fonttype, fontsize, align, outline_colour, text); + } + draw_text(dr, x, y, fonttype, fontsize, align, text_colour, text); +} + /* vim: set shiftwidth=4 tabstop=8: */ |