X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=emcc.c;h=c1b1f7ac33bb28356022713893205b8d8a53e24e;hb=b95812963a8d187526145f5e1a9693f10eee2b65;hp=d85a0e85843372bbfbec7ca3d6a67d33a2e3e010;hpb=2360b77812e184902e44c7f4ea2f6fc79729e370;p=sgt-puzzles.git diff --git a/emcc.c b/emcc.c index d85a0e8..c1b1f7a 100644 --- a/emcc.c +++ b/emcc.c @@ -21,12 +21,6 @@ * by using the DOM File API to ask the user to select a file and * permit us to see its contents. * - * - it ought to be possible to make the puzzle canvases resizable, - * by superimposing some kind of draggable resize handle. Also I - * quite like the idea of having a few buttons for standard sizes: - * reset to default size, maximise to the browser window dimensions - * (if we can find those out), and perhaps even go full-screen. - * * - I should think about whether these webified puzzles can support * touchscreen-based tablet browsers (assuming there are any that * can cope with the reasonably modern JS and run it fast enough to @@ -53,7 +47,10 @@ * that using whatever they normally use to print PDFs!) */ +#include +#include #include +#include #include "puzzles.h" @@ -193,10 +190,12 @@ void timer_callback(double tplus) } /* ---------------------------------------------------------------------- - * Helper function to resize the canvas, and variables to remember its - * size for other functions (e.g. trimming blitter rectangles). + * Helper functions to resize the canvas, and variables to remember + * its size for other functions (e.g. trimming blitter rectangles). */ static int canvas_w, canvas_h; + +/* Called when we resize as a result of changing puzzle settings */ static void resize(void) { int w, h; @@ -207,6 +206,26 @@ static void resize(void) canvas_h = h; } +/* Called from JS when the user uses the resize handle */ +void resize_puzzle(int w, int h) +{ + midend_size(me, &w, &h, TRUE); + if (canvas_w != w || canvas_h != h) { + js_canvas_set_size(w, h); + canvas_w = w; + canvas_h = h; + midend_force_redraw(me); + } +} + +/* Called from JS when the user uses the restore button */ +void restore_puzzle_size(int w, int h) +{ + midend_reset_tilesize(me); + resize(); + midend_force_redraw(me); +} + /* * HTML doesn't give us a default frontend colour of its own, so we * just make up a lightish grey ourselves. @@ -298,6 +317,8 @@ void key(int keycode, int charcode, const char *key, const char *chr, keyevent = keycode + (shift ? 0 : 32); } else if (keycode >= 48 && keycode <= 57) { keyevent = keycode; + } else if (keycode == 32) { /* space / CURSOR_SELECT2 */ + keyevent = keycode; } if (keyevent >= 0) { @@ -528,14 +549,14 @@ const struct drawing_api js_drawing = { * Presets and game-configuration dialog support. */ static game_params **presets; -static int custom_preset; +static int npresets; int have_presets_dropdown; void select_appropriate_preset(void) { if (have_presets_dropdown) { int preset = midend_which_preset(me); - js_select_preset(preset < 0 ? custom_preset : preset); + js_select_preset(preset < 0 ? -1 : preset); } } @@ -656,7 +677,7 @@ void command(int n) case 2: /* game parameter dropdown changed */ { int i = js_get_selected_preset(); - if (i == custom_preset) { + if (i < 0) { /* * The user selected 'Custom', so launch the config * box. @@ -668,12 +689,14 @@ void command(int n) * The user selected a preset, so just switch straight * to that. */ + assert(i < npresets); midend_set_params(me, presets[i]); midend_new_game(me); resize(); midend_redraw(me); update_undo_redo(); js_focus_canvas(); + select_appropriate_preset(); /* sort out Custom/Customise */ } } break; @@ -762,12 +785,10 @@ int main(int argc, char **argv) /* * Set up the game-type dropdown with presets and/or the Custom - * option. We remember the index of the Custom option (as - * custom_preset) so that we can easily treat it specially when - * it's selected. + * option. */ - custom_preset = midend_num_presets(me); - if (custom_preset == 0) { + npresets = midend_num_presets(me); + if (npresets == 0) { /* * This puzzle doesn't have selectable game types at all. * Completely remove the drop-down list from the page. @@ -775,10 +796,8 @@ int main(int argc, char **argv) js_remove_type_dropdown(); have_presets_dropdown = FALSE; } else { - int preset; - - presets = snewn(custom_preset, game_params *); - for (i = 0; i < custom_preset; i++) { + presets = snewn(npresets, game_params *); + for (i = 0; i < npresets; i++) { char *name; midend_fetch_preset(me, i, &name, &presets[i]); js_add_preset(name);