not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.3 KiB

  1. //Copyright (c) 2013, Playful Invention Company.
  2. //Permission is hereby granted, free of charge, to any person obtaining a copy
  3. //of this software and associated documentation files (the "Software"), to deal
  4. //in the Software without restriction, including without limitation the rights
  5. //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. //copies of the Software, and to permit persons to whom the Software is
  7. //furnished to do so, subject to the following conditions:
  8. //The above copyright notice and this permission notice shall be included in
  9. //all copies or substantial portions of the Software.
  10. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. //THE SOFTWARE.
  17. // -----
  18. // GridPaint has been kept extremely minimal as an explicit design choice.
  19. // If you want to add features please make a fork with a different name.
  20. // Thanks in advance
  21. var thumbcnvs = [];
  22. var thumbframe;
  23. var mode, selected=0;
  24. var tl;
  25. /////////////////////////
  26. //
  27. // Thumbnails
  28. //
  29. /////////////////////////
  30. function thumbsInit(){
  31. tl = {x0: 20, y0: 100, xd: 200, yd: 200, xn: 5};
  32. thumbframe.style.width = '1024px';
  33. thumbframe.style.height = '748px';
  34. newThumbCanvases();
  35. select(selected);
  36. }
  37. function newThumbCanvases(){
  38. var x, y=tl.y0;
  39. for(i=0;i<15;i++){
  40. if((i%tl.xn)==0) x=tl.x0;
  41. var cnv = newThumbCanvas(x, y);
  42. var pos = gallery[i];
  43. drawThumb(cnv, pos);
  44. thumbframe.appendChild(cnv);
  45. thumbcnvs.push(cnv);
  46. x+=tl.xd;
  47. if((i%tl.xn)==(tl.xn-1)) y+=tl.yd;
  48. }
  49. }
  50. function newThumbCanvas(x, y){
  51. var cnv = document.createElement('canvas');
  52. cnv.style.position = 'absolute'
  53. cnv.style.width = 176+'px';
  54. cnv.style.height = 147+'px';
  55. cnv.style.left = x+'px';
  56. cnv.style.top = y+'px';
  57. cnv.width = 176;
  58. cnv.height = 147;
  59. return cnv;
  60. }
  61. function drawThumb(cnv, pos){
  62. var ctx = cnv.getContext('2d');
  63. ctx.save();
  64. ctx.fillStyle = 'lightblue';
  65. ctx.scale(176/720, 176/720);
  66. ctx.strokeStyle = '#404040';
  67. ctx.lineWidth = 1;
  68. for(var i=0;i<shapes.length;i++){
  69. ctx.fillStyle= fromColorLetter(pos[i]);;
  70. shapePath(i, ctx);
  71. ctx.fill();
  72. }
  73. for(var i=0;i<shapes.length;i++){
  74. shapePath(i, ctx);
  75. ctx.stroke();
  76. }
  77. ctx.restore();
  78. }
  79. /////////////////////////
  80. //
  81. // Events
  82. //
  83. /////////////////////////
  84. function handleThumbStart(x, y){
  85. var n = Math.floor((y-tl.y0)/tl.yd)*tl.xn+Math.floor((x-tl.x0)/tl.xd);
  86. if((n<0)||(n>=15)) return;
  87. select(n);
  88. editMode(true);
  89. }
  90. function handleRotate(e){
  91. var o = window.orientation;
  92. if((o==90)||(o==-90)) {
  93. frame.style.width = '1024px';
  94. frame.style.height = '748px';
  95. editMode(true);
  96. } else {
  97. frame.style.width = '768px';
  98. frame.style.height = '1004px';
  99. selectorMode(true);
  100. }
  101. }
  102. function editMode(save){
  103. mode = 'edit';
  104. thumbframe.style.visibility = 'hidden';
  105. onStart = handleStart;
  106. loadPos(selected);
  107. changed = false;
  108. if (save) saveGallery();
  109. clearbtn.style.display = "inline";
  110. }
  111. function selectorMode(save){
  112. mode = 'selector';
  113. thumbframe.style.visibility = 'visible';
  114. onStart = handleThumbStart;
  115. if(changed) savePos(selected);
  116. if (save) saveGallery();
  117. clearbtn.style.display = "none";
  118. }
  119. function select(n){
  120. thumbcnvs[selected].style.border = '0px';
  121. thumbcnvs[n].style.border = '3px solid black';
  122. selected=n;
  123. }
  124. /////////////////////////
  125. //
  126. // Persistance
  127. //
  128. /////////////////////////
  129. function loadPos(n){
  130. var pos = gallery[n];
  131. for(var i=0;i<colors.length;i++) colors[i]= fromColorLetter(pos[i]);
  132. for(var i=0;i<shapes.length;i++) fillPiece(i);
  133. for(var i=0;i<shapes.length;i++) strokePiece(i);
  134. }
  135. function savePos(n){
  136. var pos = posString();
  137. drawThumb(thumbcnvs[n], pos);
  138. gallery[n] = pos;
  139. saveGallery();
  140. }
  141. function storedPos(n){
  142. var pos = '';
  143. for(var i=0;i<colors.length;i++) pos+='w';
  144. return pos;
  145. }
  146. function posString(){
  147. var res = '';
  148. for(var i=0;i<colors.length;i++) res+=toColorLetter(colors[i]);
  149. return res;
  150. }
  151. function timestamp(){
  152. return Math.floor(Date.now()/1000);
  153. }
  154. function fromColorLetter(c){return cnames[cletters.indexOf(c)];}
  155. function toColorLetter(c){return cletters[cnames.indexOf(c)];}