From bc2c1f69fddac3a51d086fb379f0ec8954f4b894 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 26 Apr 2017 14:39:45 +0100 Subject: [PATCH] Javascript puzzles: switch to a CSS-based drop-down system. The previous control buttons and dropdowns based on form elements were always a bit ugly: partly in a purely visual sense, and partly because of the nasty bodge I had to do with splitting the usual 'Custom' game type menu item into two (to get round the fact that if an element of a doesn't support that at all. So here's a replacement system which does everything by CSS properties, including the popping-up of menus when the mouse moves over their parent menu item. (Thanks to the Internet in general for showing me how that trick is done.) --- emcc.c | 2 +- emcclib.js | 90 ++++++++++++------------------- emccpre.js | 33 +++++------- html/jspage.pl | 142 +++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 180 insertions(+), 87 deletions(-) diff --git a/emcc.c b/emcc.c index c1b1f7a..74f17cf 100644 --- a/emcc.c +++ b/emcc.c @@ -696,7 +696,7 @@ void command(int n) midend_redraw(me); update_undo_redo(); js_focus_canvas(); - select_appropriate_preset(); /* sort out Custom/Customise */ + select_appropriate_preset(); } } break; diff --git a/emcclib.js b/emcclib.js index 385281a..1dde2b3 100644 --- a/emcclib.js +++ b/emcclib.js @@ -45,7 +45,7 @@ mergeInto(LibraryManager.library, { * provides neither presets nor configurability. */ js_remove_type_dropdown: function() { - document.getElementById("gametype").style.display = "none"; + gametypelist.style.display = "none"; }, /* @@ -67,34 +67,35 @@ mergeInto(LibraryManager.library, { * index back to the C code when a selection is made.) * * The special 'Custom' preset is requested by passing NULL to - * this function, rather than the string "Custom", since in that - * case we need to do something special - see below. + * this function. */ js_add_preset: function(ptr) { - var name = (ptr == 0 ? "Customise..." : Pointer_stringify(ptr)); - var value = gametypeoptions.length; - - var option = document.createElement("option"); - option.value = value; - option.appendChild(document.createTextNode(name)); - gametypeselector.appendChild(option); - gametypeoptions.push(option); + var name = (ptr == 0 ? "Custom" : Pointer_stringify(ptr)); + var value = gametypeitems.length; + var item = document.createElement("li"); if (ptr == 0) { // The option we've just created is the one for inventing // a new custom setup. - gametypenewcustom = option; - option.value = -1; - - // Now create another element called 'Custom', which will - // be auto-selected by us to indicate the custom settings - // you've previously selected. However, we don't add it to - // the game type selector; it will only appear when the - // user actually has custom settings selected. - option = document.createElement("option"); - option.value = -2; - option.appendChild(document.createTextNode("Custom")); - gametypethiscustom = option; + gametypecustom = item; + value = -1; + } + + item.setAttribute("data-index", value); + var tick = document.createElement("span"); + tick.appendChild(document.createTextNode("\u2713")); + tick.style.color = "transparent"; + tick.style.paddingRight = "0.5em"; + item.appendChild(tick); + item.appendChild(document.createTextNode(name)); + gametypelist.appendChild(item); + gametypeitems.push(item); + + item.onclick = function(event) { + if (dlg_dimmer === null) { + gametypeselectedindex = value; + command(2); + } } }, @@ -105,12 +106,7 @@ mergeInto(LibraryManager.library, { * dropdown. */ js_get_selected_preset: function() { - for (var i in gametypeoptions) { - if (gametypeoptions[i].selected) { - return gametypeoptions[i].value; - } - } - return 0; + return gametypeselectedindex; }, /* @@ -121,33 +117,15 @@ mergeInto(LibraryManager.library, { * which turn out to exactly match a preset). */ js_select_preset: function(n) { - if (gametypethiscustom !== null) { - // Fiddle with the Custom/Customise options. If we're - // about to select the Custom option, then it should be in - // the menu, and the other one should read "Re-customise"; - // if we're about to select another one, then the static - // Custom option should disappear and the other one should - // read "Customise". - - if (gametypethiscustom.parentNode == gametypeselector) - gametypeselector.removeChild(gametypethiscustom); - if (gametypenewcustom.parentNode == gametypeselector) - gametypeselector.removeChild(gametypenewcustom); - - if (n < 0) { - gametypeselector.appendChild(gametypethiscustom); - gametypenewcustom.lastChild.data = "Re-customise..."; + gametypeselectedindex = n; + for (var i in gametypeitems) { + var item = gametypeitems[i]; + var tick = item.firstChild; + if (item.getAttribute("data-index") == n) { + tick.style.color = "inherit"; } else { - gametypenewcustom.lastChild.data = "Customise..."; + tick.style.color = "transparent"; } - gametypeselector.appendChild(gametypenewcustom); - gametypenewcustom.selected = false; - } - - if (n < 0) { - gametypethiscustom.selected = true; - } else { - gametypeoptions[n].selected = true; } }, @@ -192,8 +170,8 @@ mergeInto(LibraryManager.library, { * after a move. */ js_enable_undo_redo: function(undo, redo) { - undo_button.disabled = (undo == 0); - redo_button.disabled = (redo == 0); + disable_menu_item(undo_button, (undo == 0)); + disable_menu_item(redo_button, (redo == 0)); }, /* diff --git a/emccpre.js b/emccpre.js index ebf67d1..b10bf29 100644 --- a/emccpre.js +++ b/emccpre.js @@ -79,22 +79,11 @@ var dlg_return_funcs = null; // pass back the final value in each dialog control. var dlg_return_sval, dlg_return_ival; -// The - - - - - - - -

+
-- 2.30.2