changeset 188:6fba2c255319

More work on the MP.JS launcher.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Aug 2018 16:32:10 +0300
parents 28caa87348d3
children 283b2b657574
files mpui.js multipaint.pde
diffstat 2 files changed, 66 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/mpui.js	Thu Aug 23 14:35:08 2018 +0300
+++ b/mpui.js	Thu Aug 23 16:32:10 2018 +0300
@@ -2,7 +2,7 @@
  * Multipaint.JS - Javascript launcher
  * (C) Copyright 2018 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
  */
-var mpMachine = 0, mpUIScale, mpForcedWidth, mpForcedHeight;
+var mpMachine = 0, mpUIScale, mpUIForcedWidth, mpUIForcedHeight;
 var mpURL;
 
 var mpMachines =
@@ -23,14 +23,19 @@
 ];
 
 
-var mpResolutions =
+var mpUIDimensions =
 [
   [ -1, -1 ],
-  [ 1200, 980 ],
+  [ 1200, 800 ],
   [ 1600, 1200 ],
 ];
 
 
+var mpUIScales =
+[
+  2, 3, 1
+];
+
 var mpSources =
 [
   "buffers.pde",
@@ -92,7 +97,32 @@
 }
 
 
-function mpShowMachineSelector()
+function stGetSelValue(velem, vdef)
+{
+  if (velem)
+    return velem.selectedIndex >= 0 ? velem.options[velem.selectedIndex].value : vdef;
+  else
+    return vdef;
+}
+
+
+function stCreateSelect(vid, vlist, vcallback)
+{
+  var vobj = stCE("select", vid);
+
+  for (var n = 0; n < vlist.length; n++)
+  {
+    var mp = vlist[n];
+    var opt = stCE("option");
+    vcallback(n, opt, mp);
+    vobj.appendChild(opt);
+  }
+
+  return vobj;
+}
+
+
+function mpShowLauncher()
 {
   var mpCanvas = stGE("mpCanvas");
   mpCanvas.style.display = "none";
@@ -109,43 +139,28 @@
   mobj.textContent = "Choose your target machine:";
   mdiv.appendChild(mobj);
 
-  mobj = stCE("select", "machineID");
-  for (var n = 0; n < mpMachines.length; n++)
-  {
-    var mp = mpMachines[n];
-    var opt = stCE("option");
-    opt.value = mp[0];
-    opt.textContent = mp[1];
-    mobj.appendChild(opt);
-  }
-  mdiv.appendChild(mobj);
+
+  mdiv.appendChild(stCreateSelect("machineID", mpMachines,
+    function (vn, vopt, val) { vopt.value = val[0]; vopt.textContent = val[1]; }));
 
   mobj = stCE("button", "selectID");
   mobj.textContent = "RUN";
-  stAddEventOb(mobj.name, mobj, "click", mpMachineSelected);
+  stAddEventOb(mobj.name, mobj, "click", mpLauncherDone);
   mdiv.appendChild(mobj);
 
 
-  mdiv = stCE("div", "mresolution");
+  mdiv = stCE("div", "mdimensions");
   mpUI.appendChild(mdiv);
 
   mobj = stCE("p");
-  mobj.textContent = "Choose optional resolution:";
+  mobj.textContent = "Choose optional UI size:";
   mdiv.appendChild(mobj);
 
-  mobj = stCE("select", "resolutionID");
-  for (var n = 0; n < mpResolutions.length; n++)
-  {
-    var mp = mpResolutions[n];
-    var opt = stCE("option");
-    opt.value = n;
-    if (n == 0)
-      opt.textContent = "DEFAULT";
-    else
-      opt.textContent = mp[0] +" x " +mp[1];
-    mobj.appendChild(opt);
-  }
-  mdiv.appendChild(mobj);
+  mdiv.appendChild(stCreateSelect("dimensionsID", mpUIDimensions,
+    function (vn, vopt, val) { vopt.value = vn; vopt.textContent = (vn == 0) ? "DEFAULT" : val[0] +" x "+ val[1]; }));
+
+  mdiv.appendChild(stCreateSelect("scalesID", mpUIScales,
+    function (vn, vopt, val) { vopt.value = val; vopt.textContent = " x "+ val; }));
 
 
   mobj = stCE("div");
@@ -179,26 +194,30 @@
 }
 
 
-function mpMachineSelected()
+function mpLauncherDone()
 {
+  // Get selected values from DOM elements
   var mpUI = stGE("mpUI");
-  var id = stGE("machineID");
-  mpMachine = id.selectedIndex >= 0 ? id.options[id.selectedIndex].value : 0;
+  mpMachine = stGetSelValue(stGE("machineID"), 0);
 
-  id = stGE("resolutionID");
-  var index = id.selectedIndex >= 0 ? id.options[id.selectedIndex].value : 0;
+  var index = stGetSelValue(stGE("dimensionsID"), -1);
   if (index > 0)
   {
-    mpForcedWidth = mpResolutions[index][0];
-    mpForcedHeight = mpResolutions[index][1];
+    mpUIForcedWidth = mpDimensions[index][0];
+    mpUIForcedHeight = mpDimensions[index][1];
   }
+  else
+    mpUIForcedWidth = mpUIForcedHeight = 0;
+
+  mpUIScale = stGetSelValue(stGE("scalesID"), -1);
 
   stClearChildren(mpUI);
 
+  // Initialize the canvas etc.
   var mpCanvas = stGE("mpCanvas");
   mpCanvas.style.display = "block";
 
-  window.location.href = mpURL +"?"+ mpMachine.toString() +":"+ mpForcedWidth +":"+ mpForcedHeight;
+  window.location.href = mpURL +"?"+ mpMachine.toString() +":"+ mpUIForcedWidth +":"+ mpUIForcedHeight +":"+ mpUIScale;
 
   mpRunSketch(mpCanvas);
 }
@@ -210,29 +229,20 @@
     "<a href=\"http://multipaint.kameli.net/\">Multipaint</a> (C) 2016-2018 <b>Tero 'Dr. TerrorZ' Heikkinen</b>, "+
     "ProcessingJS port and modifications by <b>Matti 'ccr' Hämäläinen</b> (2018)";
 
+  // Check for URL parameters
   var slink = window.location.href;
   var spos, found = false;
   if ((spos = slink.indexOf("?")) >= 0)
   {
     var sargs = unescape(slink.substr(spos + 1)).split(":");
     mpURL = slink.substr(0, spos);
-
-    var tmp = parseInt(sargs[0]);
-    for (var n = 0; n < mpMachines.length; n++)
-    {
-      var mp = mpMachines[n];
-      if (mp[0] == tmp)
-      {
-        mpMachine = mp[0];
-        found = true;
-        break;
-      }
-    }
+    mpMachine = parseInt(sargs[0]);
+    found = true;
 
     if (sargs.length >= 3)
     {
-      mpForcedWidth = parseInt(sargs[1]);
-      mpForcedHeight = parseInt(sargs[2]);
+      mpUIForcedWidth = parseInt(sargs[1]);
+      mpUIForcedHeight = parseInt(sargs[2]);
     }
     if (sargs.length >= 4)
       mpUIScale = parseInt(sargs[3]);
@@ -240,12 +250,13 @@
   else
     mpURL = slink;
 
+  // Either run sketch or show the launcher
   if (found)
   {
     mpRunSketch(stGE("mpCanvas"));
   }
   else
   {
-    stAddEventOb("DOM", document, "DOMContentLoaded", mpShowMachineSelector);
+    stAddEventOb("DOM", document, "DOMContentLoaded", mpShowLauncher);
   }
 }
--- a/multipaint.pde	Thu Aug 23 14:35:08 2018 +0300
+++ b/multipaint.pde	Thu Aug 23 16:32:10 2018 +0300
@@ -109,8 +109,8 @@
     // Get settings from Javascript runner, or use defaults
     g_machine = (mpMachine >= 0) ? mpMachine : C64;
     g_uiscale = (mpUIScale >= 1 && mpUIScale <= 3) ? mpUIScale : 2;
-    g_forced_width = mpForcedWidth > 0 ? mpForcedWidth : 0;
-    g_forced_height = mpForcedHeight > 0 ? mpForcedHeight : 0;
+    g_forced_width = mpUIForcedWidth > 0 ? mpUIForcedWidth : 0;
+    g_forced_height = mpUIForcedHeight > 0 ? mpUIForcedHeight : 0;
     g_animspeed = 1;
 
     //normi