misc/graves_js_anim.xhtml
changeset 15978 20adaa127663
parent 15977 f81fe8250ed9
equal deleted inserted replaced
15977:f81fe8250ed9 15978:20adaa127663
    59     width: 32px;
    59     width: 32px;
    60 }
    60 }
    61     </style>
    61     </style>
    62     <script type="application/ecmascript">
    62     <script type="application/ecmascript">
    63 //<![CDATA[
    63 //<![CDATA[
    64 var IS_LOCAL=false; // set to true to fetch graves locally. Useful for testing.
    64 let IS_LOCAL=false; // set to true to fetch flags locally. Useful for testing.
    65 var graves;
    65 let graves;
    66 if (IS_LOCAL) {
    66 if (IS_LOCAL) {
    67 /* JavaScript version of a sprite sheet - this could be pretty trivially done in pure HTML, but maintenance
    67 /* JavaScript version of a sprite sheet - this could be pretty trivially done in pure HTML, but maintenance
    68 would be easier with a server-side portion. list of sprites could be gotten from server, but would require XSS whitelisting */
    68 would be easier with a server-side portion. list of sprites could be gotten from server, but would require XSS whitelisting */
    69 // Last updated: 1.0.0
    69 // Last updated: 1.0.0
    70 graves=["Badger","Bone","bp2","bubble","Cherry","chest","Clover","coffin",
    70 graves=["Badger","Bone","bp2","bubble","Cherry","chest","Clover","coffin",
    76 else
    76 else
    77 {
    77 {
    78 graves = [];
    78 graves = [];
    79 }
    79 }
    80 
    80 
    81 var themes = {
    81 let themes = {
    82 // Last updated: 1.0.0
    82 // Last updated: 1.0.0
    83 "Art":1,
    83 "Art":1,
    84 "Beach":1,
    84 "Beach":1,
    85 "Bamboo":1,
    85 "Bamboo":1,
    86 "Bath":1,
    86 "Bath":1,
   111 "Planes":0,
   111 "Planes":0,
   112 "Sheep":1,
   112 "Sheep":1,
   113 "Snow":1,
   113 "Snow":1,
   114 "Stage":1,
   114 "Stage":1,
   115 "Underwater":1};
   115 "Underwater":1};
   116 var girder;
   116 let girder;
   117 var animationInterval;
   117 let animationInterval;
   118 
   118 
   119 on_xml_loaded = function(ex)
   119 on_xml_loaded = function(ex)
   120 {
   120 {
   121     var resp = this.responseText;
   121     let resp = this.responseText;
   122     var r = />([^<]*).png</g;
   122     let r = />([^<]*).png</g;
   123     var x;
   123     let x;
   124     while(x = r.exec(resp))
   124     while(x = r.exec(resp))
   125     {
   125     {
   126         graves.push(x[1]);
   126         graves.push(x[1]);
   127     }
   127     }
   128     on_graves_loaded();
   128     on_graves_loaded();
   129 }
   129 }
   130 
   130 
   131 on_xml_error = function()
   131 on_xml_error = function()
   132 {
   132 {
   133     var p = document.createElement("p");
   133     let p = document.createElement("p");
   134     p.appendChild(document.createTextNode("ERROR: List of graves could not be fetched from the server!"));
   134     p.appendChild(document.createTextNode("ERROR: List of graves could not be fetched from the server!"));
   135     document.body.appendChild(p);
   135     document.body.appendChild(p);
   136 }
   136 }
   137 
   137 
   138 window.onload = function()
   138 window.onload = function()
   139 {
   139 {
   140     // Load list of graves
   140     // Load list of graves
   141     if (!IS_LOCAL) {
   141     if (!IS_LOCAL) {
   142         // Request list of graves from repository URL
   142         // Request list of graves from repository URL
   143         var xml=new XMLHttpRequest();
   143         let xml=new XMLHttpRequest();
   144         xml.open("GET", "//hg.hedgewars.org/hedgewars/file/tip/share/hedgewars/Data/Graphics/Graves/");
   144         xml.open("GET", "//hg.hedgewars.org/hedgewars/file/tip/share/hedgewars/Data/Graphics/Graves/");
   145         xml.addEventListener("error", on_xml_error);
   145         xml.addEventListener("error", on_xml_error);
   146         xml.onload = on_xml_loaded;
   146         xml.onload = on_xml_loaded;
   147         xml.send();
   147         xml.send();
   148     }
   148     }
   153 }
   153 }
   154 
   154 
   155 on_graves_loaded = function()
   155 on_graves_loaded = function()
   156 {
   156 {
   157     // Render girders
   157     // Render girders
   158     var s = document.styleSheets[0].cssRules;
   158     let s = document.styleSheets[0].cssRules;
   159     for(var i=0;i<s.length;i++)
   159     for(let i=0;i<s.length;i++)
   160     {
   160     {
   161         if (s[i].selectorText.toLowerCase() === ".girder")
   161         if (s[i].selectorText.toLowerCase() === ".girder")
   162             girder = s[i];
   162             girder = s[i];
   163     }
   163     }
   164 
   164 
   165     var a = document.createElement("a");
   165     let a = document.createElement("a");
   166     var g = document.createElement("div");
   166     let g = document.createElement("div");
   167     g.className="girder";
   167     g.className="girder";
   168     a.className="grave";
   168     a.className="grave";
   169     a.appendChild(document.createElement("div"));
   169     a.appendChild(document.createElement("div"));
   170     a.lastChild.appendChild(document.createTextNode(""));
   170     a.lastChild.appendChild(document.createTextNode(""));
   171 
   171 
   172     // Render graves
   172     // Render graves
   173     var missingGraves = [];
   173     let missingGraves = [];
   174     var img;
   174     let img;
   175     var j = 0;
   175     let j = 0;
   176     var toDelete = [];
   176     let toDelete = [];
   177     for (var i=0;i<graves.length;i++)
   177     for (let i=0;i<graves.length;i++)
   178     {
   178     {
   179         var h = document.body.appendChild(a.cloneNode(true));
   179         let h = document.body.appendChild(a.cloneNode(true));
   180         if (IS_LOCAL)
   180         if (IS_LOCAL)
   181             h.href = "../share/hedgewars/Data/Graphics/Graves/"+graves[i]+".png";
   181             h.href = "../share/hedgewars/Data/Graphics/Graves/"+graves[i]+".png";
   182         else
   182         else
   183             h.href = "//hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Graphics/Graves/"+graves[i]+".png";
   183             h.href = "//hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Graphics/Graves/"+graves[i]+".png";
   184 
   184 
   192 
   192 
   193     // Quick and dirty animation
   193     // Quick and dirty animation
   194     animationInterval = setInterval(animateGraves, 128);
   194     animationInterval = setInterval(animateGraves, 128);
   195 
   195 
   196     // Theme selection drop-down list
   196     // Theme selection drop-down list
   197     var form = document.body.appendChild(document.createElement("form"));
   197     let form = document.body.appendChild(document.createElement("form"));
   198 
   198 
   199     var opt = document.createElement("option");
   199     let opt = document.createElement("option");
   200     opt.appendChild(document.createTextNode(""));
   200     opt.appendChild(document.createTextNode(""));
   201 
   201 
   202     var label = document.createElement("label");
   202     let label = document.createElement("label");
   203     label.htmlFor = "theme_select";
   203     label.htmlFor = "theme_select";
   204     label.appendChild(document.createTextNode("Theme: "));
   204     label.appendChild(document.createTextNode("Theme: "));
   205     form.appendChild(label);
   205     form.appendChild(label);
   206 
   206 
   207     var sel = form.appendChild(document.createElement("select"));
   207     let sel = form.appendChild(document.createElement("select"));
   208     sel.id = "theme_select";
   208     sel.id = "theme_select";
   209     sel.onchange = switchTheme;
   209     sel.onchange = switchTheme;
   210     for(var theme in themes)
   210     for(let theme in themes)
   211     {
   211     {
   212         sel.appendChild(opt.cloneNode(true));
   212         sel.appendChild(opt.cloneNode(true));
   213         sel.lastChild.value = theme;
   213         sel.lastChild.value = theme;
   214         sel.lastChild.lastChild.data = theme;
   214         sel.lastChild.lastChild.data = theme;
   215         if(theme === "Nature")
   215         if(theme === "Nature")
   216             sel.lastChild.selected = true;
   216             sel.lastChild.selected = true;
   217     }
   217     }
   218     form.appendChild(document.createElement("br"));
   218     form.appendChild(document.createElement("br"));
   219 
   219 
   220     // Checkbox: Switch animation
   220     // Checkbox: Switch animation
   221     var chk = document.createElement("input");
   221     let chk = document.createElement("input");
   222     chk.id = "anim";
   222     chk.id = "anim";
   223     chk.type = "checkbox";
   223     chk.type = "checkbox";
   224     chk.onclick = switchAnim;
   224     chk.onclick = switchAnim;
   225     chk.checked = true;
   225     chk.checked = true;
   226     form.appendChild(chk);
   226     form.appendChild(chk);
   248     switchTheme();
   248     switchTheme();
   249 }
   249 }
   250 
   250 
   251 function animateGraves()
   251 function animateGraves()
   252 {
   252 {
   253     var a = document.getElementsByTagName("a");
   253     let a = document.getElementsByTagName("a");
   254     for (var i=0;i<a.length;i++)
   254     for (let i=0;i<a.length;i++)
   255     {
   255     {
   256         if (a[i].className !== "grave")
   256         if (a[i].className !== "grave")
   257             continue;
   257             continue;
   258         // Cycle thru animation frames
   258         // Cycle thru animation frames
   259 
   259 
   260         var maskName = a[i].title;
   260         let maskName = a[i].title;
   261         // Grave
   261         // Grave
   262         a[i].firstChild.style.backgroundPosition=Math.floor(a[i].idle/16)*-32+"px "+(a[i].idle%16)*-32+"px";
   262         a[i].firstChild.style.backgroundPosition=Math.floor(a[i].idle/16)*-32+"px "+(a[i].idle%16)*-32+"px";
   263 
   263 
   264         // Next frame
   264         // Next frame
   265         a[i].idle++;
   265         a[i].idle++;
   280 }
   280 }
   281 
   281 
   282 // Turn on or off girders
   282 // Turn on or off girders
   283 function hideGirders()
   283 function hideGirders()
   284 {
   284 {
   285     var g = document.getElementsByClassName("girder");
   285     let g = document.getElementsByClassName("girder");
   286     for(var i=0;i<g.length;i++)
   286     for(let i=0;i<g.length;i++)
   287         if (this.checked)
   287         if (this.checked)
   288             g[i].className = "girder";
   288             g[i].className = "girder";
   289         else
   289         else
   290             g[i].className = "girder hide";
   290             g[i].className = "girder hide";
   291 
   291 
   292 }
   292 }
   293 
   293 
   294 // Select theme according to drop-down list value
   294 // Select theme according to drop-down list value
   295 function switchTheme()
   295 function switchTheme()
   296 {
   296 {
   297     var prefix;
   297     let prefix;
   298     if (!IS_LOCAL)
   298     if (!IS_LOCAL)
   299         prefix = "//hg.hedgewars.org/hedgewars/raw-file/tip";
   299         prefix = "//hg.hedgewars.org/hedgewars/raw-file/tip";
   300     else
   300     else
   301         prefix = "..";
   301         prefix = "..";
   302     var theme = this.value || "Nature";
   302     let theme = this.value || "Nature";
   303     document.body.style.backgroundImage='url("'+prefix+'/share/hedgewars/Data/Themes/'+theme+'/Sky.png")';
   303     document.body.style.backgroundImage='url("'+prefix+'/share/hedgewars/Data/Themes/'+theme+'/Sky.png")';
   304     if (themes[theme])
   304     if (themes[theme])
   305         girder.style.backgroundImage='url("'+prefix+'/share/hedgewars/Data/Themes/'+theme+'/Girder.png")';
   305         girder.style.backgroundImage='url("'+prefix+'/share/hedgewars/Data/Themes/'+theme+'/Girder.png")';
   306     else
   306     else
   307         girder.style.backgroundImage='url("'+prefix+'/share/hedgewars/Data/Graphics/Girder.png")';
   307         girder.style.backgroundImage='url("'+prefix+'/share/hedgewars/Data/Graphics/Girder.png")';