Patch from James H to centralise some generally useful cursor-
[sgt/puzzles] / drawing.c
CommitLineData
dafd6cf6 1/*
2 * drawing.c: Intermediary between the drawing interface as
3 * presented to the back end, and that implemented by the front
4 * end.
5 *
6 * Mostly just looks up calls in a vtable and passes them through
7 * unchanged. However, on the printing side it tracks print colours
8 * so the front end API doesn't have to.
9 *
83c0438f 10 * FIXME:
11 *
12 * - I'd _like_ to do automatic draw_updates, but it's a pain for
13 * draw_text in particular. I'd have to invent a front end API
14 * which retrieved the text bounds.
15 * + that might allow me to do the alignment centrally as well?
16 * * perhaps not, because PS can't return this information,
17 * so there would have to be a special case for it.
18 * + however, that at least doesn't stand in the way of using
19 * the text bounds for draw_update, because PS doesn't need
20 * draw_update since it's printing-only. Any _interactive_
21 * drawing API couldn't get away with refusing to tell you
22 * what parts of the screen a text draw had covered, because
23 * you would inevitably need to erase it later on.
dafd6cf6 24 */
25
26#include <stdio.h>
27#include <stdlib.h>
83c0438f 28#include <string.h>
dafd6cf6 29#include <assert.h>
30#include <math.h>
31
32#include "puzzles.h"
33
34struct print_colour {
35 int hatch;
60aa1c74 36 int hatch_when; /* 0=never 1=only-in-b&w 2=always */
dafd6cf6 37 float r, g, b;
60aa1c74 38 float grey;
dafd6cf6 39};
40
41struct drawing {
42 const drawing_api *api;
43 void *handle;
44 struct print_colour *colours;
45 int ncolours, coloursize;
46 float scale;
83c0438f 47 /* `me' is only used in status_bar(), so print-oriented instances of
48 * this may set it to NULL. */
49 midend *me;
50 char *laststatus;
dafd6cf6 51};
52
83c0438f 53drawing *drawing_new(const drawing_api *api, midend *me, void *handle)
dafd6cf6 54{
55 drawing *dr = snew(drawing);
56 dr->api = api;
57 dr->handle = handle;
58 dr->colours = NULL;
59 dr->ncolours = dr->coloursize = 0;
60 dr->scale = 1.0F;
83c0438f 61 dr->me = me;
62 dr->laststatus = NULL;
dafd6cf6 63 return dr;
64}
65
66void drawing_free(drawing *dr)
67{
83c0438f 68 sfree(dr->laststatus);
dafd6cf6 69 sfree(dr->colours);
70 sfree(dr);
71}
72
73void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
74 int align, int colour, char *text)
75{
76 dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
77 colour, text);
78}
79
80void draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
81{
82 dr->api->draw_rect(dr->handle, x, y, w, h, colour);
83}
84
85void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
86{
87 dr->api->draw_line(dr->handle, x1, y1, x2, y2, colour);
88}
89
90void draw_polygon(drawing *dr, int *coords, int npoints,
91 int fillcolour, int outlinecolour)
92{
93 dr->api->draw_polygon(dr->handle, coords, npoints, fillcolour,
94 outlinecolour);
95}
96
97void draw_circle(drawing *dr, int cx, int cy, int radius,
98 int fillcolour, int outlinecolour)
99{
100 dr->api->draw_circle(dr->handle, cx, cy, radius, fillcolour,
101 outlinecolour);
102}
103
104void draw_update(drawing *dr, int x, int y, int w, int h)
105{
106 if (dr->api->draw_update)
107 dr->api->draw_update(dr->handle, x, y, w, h);
108}
109
110void clip(drawing *dr, int x, int y, int w, int h)
111{
112 dr->api->clip(dr->handle, x, y, w, h);
113}
114
115void unclip(drawing *dr)
116{
117 dr->api->unclip(dr->handle);
118}
119
120void start_draw(drawing *dr)
121{
122 dr->api->start_draw(dr->handle);
123}
124
125void end_draw(drawing *dr)
126{
127 dr->api->end_draw(dr->handle);
128}
129
130void status_bar(drawing *dr, char *text)
131{
83c0438f 132 char *rewritten;
133
134 if (!dr->api->status_bar)
135 return;
136
137 assert(dr->me);
138
139 rewritten = midend_rewrite_statusbar(dr->me, text);
140 if (!dr->laststatus || strcmp(rewritten, dr->laststatus)) {
141 dr->api->status_bar(dr->handle, rewritten);
142 sfree(dr->laststatus);
143 dr->laststatus = rewritten;
144 } else {
145 sfree(rewritten);
146 }
dafd6cf6 147}
148
149blitter *blitter_new(drawing *dr, int w, int h)
150{
151 return dr->api->blitter_new(dr->handle, w, h);
152}
153
154void blitter_free(drawing *dr, blitter *bl)
155{
156 dr->api->blitter_free(dr->handle, bl);
157}
158
159void blitter_save(drawing *dr, blitter *bl, int x, int y)
160{
161 dr->api->blitter_save(dr->handle, bl, x, y);
162}
163
164void blitter_load(drawing *dr, blitter *bl, int x, int y)
165{
166 dr->api->blitter_load(dr->handle, bl, x, y);
167}
168
169void print_begin_doc(drawing *dr, int pages)
170{
171 dr->api->begin_doc(dr->handle, pages);
172}
173
174void print_begin_page(drawing *dr, int number)
175{
176 dr->api->begin_page(dr->handle, number);
177}
178
179void print_begin_puzzle(drawing *dr, float xm, float xc,
180 float ym, float yc, int pw, int ph, float wmm,
181 float scale)
182{
183 dr->scale = scale;
184 dr->ncolours = 0;
185 dr->api->begin_puzzle(dr->handle, xm, xc, ym, yc, pw, ph, wmm);
186}
187
188void print_end_puzzle(drawing *dr)
189{
190 dr->api->end_puzzle(dr->handle);
191 dr->scale = 1.0F;
192}
193
194void print_end_page(drawing *dr, int number)
195{
196 dr->api->end_page(dr->handle, number);
197}
198
199void print_end_doc(drawing *dr)
200{
201 dr->api->end_doc(dr->handle);
202}
203
60aa1c74 204void print_get_colour(drawing *dr, int colour, int printing_in_colour,
205 int *hatch, float *r, float *g, float *b)
dafd6cf6 206{
207 assert(colour >= 0 && colour < dr->ncolours);
60aa1c74 208 if (dr->colours[colour].hatch_when == 2 ||
209 (dr->colours[colour].hatch_when == 1 && !printing_in_colour)) {
210 *hatch = dr->colours[colour].hatch;
211 } else {
212 *hatch = -1;
213 if (printing_in_colour) {
214 *r = dr->colours[colour].r;
215 *g = dr->colours[colour].g;
216 *b = dr->colours[colour].b;
217 } else {
218 *r = *g = *b = dr->colours[colour].grey;
219 }
220 }
dafd6cf6 221}
222
60aa1c74 223static int print_generic_colour(drawing *dr, float r, float g, float b,
224 float grey, int hatch, int hatch_when)
dafd6cf6 225{
226 if (dr->ncolours >= dr->coloursize) {
227 dr->coloursize = dr->ncolours + 16;
228 dr->colours = sresize(dr->colours, dr->coloursize,
229 struct print_colour);
230 }
231 dr->colours[dr->ncolours].hatch = hatch;
60aa1c74 232 dr->colours[dr->ncolours].hatch_when = hatch_when;
dafd6cf6 233 dr->colours[dr->ncolours].r = r;
234 dr->colours[dr->ncolours].g = g;
235 dr->colours[dr->ncolours].b = b;
60aa1c74 236 dr->colours[dr->ncolours].grey = grey;
dafd6cf6 237 return dr->ncolours++;
238}
239
60aa1c74 240int print_mono_colour(drawing *dr, int grey)
dafd6cf6 241{
60aa1c74 242 return print_generic_colour(dr, grey, grey, grey, grey, -1, 0);
dafd6cf6 243}
244
60aa1c74 245int print_grey_colour(drawing *dr, float grey)
246{
247 return print_generic_colour(dr, grey, grey, grey, grey, -1, 0);
248}
249
250int print_hatched_colour(drawing *dr, int hatch)
251{
252 return print_generic_colour(dr, 0, 0, 0, 0, hatch, 2);
253}
254
255int print_rgb_mono_colour(drawing *dr, float r, float g, float b, int grey)
256{
257 return print_generic_colour(dr, r, g, b, grey, -1, 0);
258}
259
260int print_rgb_grey_colour(drawing *dr, float r, float g, float b, float grey)
261{
262 return print_generic_colour(dr, r, g, b, grey, -1, 0);
263}
264
265int print_rgb_hatched_colour(drawing *dr, float r, float g, float b, int hatch)
dafd6cf6 266{
60aa1c74 267 return print_generic_colour(dr, r, g, b, 0, hatch, 1);
dafd6cf6 268}
269
270void print_line_width(drawing *dr, int width)
271{
272 /*
273 * I don't think it's entirely sensible to have line widths be
274 * entirely relative to the puzzle size; there is a point
275 * beyond which lines are just _stupidly_ thick. On the other
276 * hand, absolute line widths aren't particularly nice either
277 * because they start to feel a bit feeble at really large
278 * scales.
279 *
280 * My experimental answer is to scale line widths as the
281 * _square root_ of the main puzzle scale. Double the puzzle
282 * size, and the line width multiplies by 1.4.
283 */
284 dr->api->line_width(dr->handle, sqrt(dr->scale) * width);
285}