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.

233 lines
5.6 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 frame, cnv, hitbuffer, clearbtn;
  22. var shapes=[], colors=[];
  23. var cnames = ['red', 'yellow', '#ff6600', '#764300', 'limegreen', 'blue', 'darkmagenta', 'black', 'white'];
  24. var cletters = ['r', 'y', 'o', 'n', 'g', 'b', 'v', 'k', 'w'];
  25. var bselected = 0;
  26. var newcolor=cnames[bselected], changed=false;
  27. var scale = 708/600;
  28. var fname = 'sqgridpos';
  29. /////////////////////////
  30. //
  31. // Setup
  32. //
  33. /////////////////////////
  34. function appInit(){
  35. frame = document.getElementById('frame');
  36. clearbtn = document.getElementById("clear-button");
  37. thumbframe = document.getElementById('thumbframe');
  38. cnv = document.getElementById('gridcnv');
  39. cnv.width=1024; cnv.height=748;
  40. hitbuffer = document.getElementById('hitbuffer');
  41. hitbuffer.width=1024; hitbuffer.height=748;
  42. var hsplit = location.href.split('--');
  43. if(hsplit.length==3) fname = hsplit[1];
  44. for(var i=0;i<shapedefs.length;i++) newShape(shapedefs[i]);
  45. for(var i=0;i<shapes.length;i++) colors.push('red');
  46. eventInit();
  47. if (mode == 'edit')
  48. editMode();
  49. else
  50. selectorMode();
  51. loadPos(selected);
  52. hittestInit();
  53. thumbsInit();
  54. drawButtons();
  55. clearbtn.addEventListener("click", handleClearButton);
  56. }
  57. function newShape(points){
  58. var s = {};
  59. s.points = points;
  60. shapes.push(s);
  61. }
  62. /////////////////////////
  63. //
  64. // Events
  65. //
  66. /////////////////////////
  67. function handleStart(x,y){
  68. if(x<120) {handleButton(y); return;}
  69. var i = findPiece(x,y);
  70. onMove = handleMove;
  71. onEnd = handleEnd;
  72. handleMove(x,y);
  73. }
  74. function handleMove(x,y){
  75. changed = true;
  76. var p = findPiece(x,y);
  77. if(p<0) return;
  78. colors[p] = newcolor;
  79. fillPiece(p);
  80. strokePiece(p);
  81. }
  82. function handleEnd(e){
  83. onMove = undefined;
  84. onUp = undefined;
  85. savePos(selected);
  86. }
  87. function handleButton(y){
  88. if(y>650) selectorMode(true);
  89. var b = Math.floor((y-20)/70);
  90. if(b<0) return;
  91. if(b>=cnames.length) return;
  92. bselected = b;
  93. newcolor = cnames[b];
  94. drawButtons();
  95. }
  96. /////////////////////////
  97. //
  98. // Hit Test
  99. //
  100. /////////////////////////
  101. function hittestInit(){
  102. hitbuffer.style.visibility = 'hidden';
  103. var ctx = hitbuffer.getContext('2d');
  104. ctx.save();
  105. ctx.translate(140, 20);
  106. ctx.scale(scale, scale);
  107. for(var i=0;i<shapes.length;i++){
  108. var low = hexdigit(i&0xf);
  109. var mid = hexdigit((i>>4)&0xf);
  110. var high = hexdigit((i>>8)&0xf);
  111. ctx.fillStyle = '#'+high+'F'+mid+'F'+low+'F';
  112. shapePath(i, ctx, true);
  113. ctx.fill();
  114. }
  115. ctx.restore();
  116. }
  117. function findPiece(x,y){
  118. var ctx = hitbuffer.getContext('2d');
  119. var pixel = ctx.getImageData(x, y, 1, 1).data;
  120. if(pixel[3]!=255) return -1;
  121. var high = pixel[0]>>4;
  122. var mid = pixel[1]>>4;
  123. var low = pixel[2]>>4;
  124. return (high<<8)+(mid<<4)+low;
  125. }
  126. function hexdigit(n){return '0123456789ABCDEF'.charAt(n);}
  127. /////////////////////////
  128. //
  129. // Drawing
  130. //
  131. /////////////////////////
  132. function fillPiece(i){
  133. var ctx = cnv.getContext('2d');
  134. ctx.save();
  135. ctx.translate(140, 20);
  136. ctx.scale(scale, scale);
  137. ctx.fillStyle = colors[i];
  138. shapePath(i, ctx);
  139. ctx.fill();
  140. ctx.restore();
  141. }
  142. function strokePiece(i){
  143. var ctx = cnv.getContext('2d');
  144. ctx.save();
  145. ctx.translate(140, 20);
  146. ctx.scale(scale, scale);
  147. ctx.strokeStyle = '#404040';
  148. ctx.lineWidth = 1;
  149. shapePath(i, ctx);
  150. ctx.stroke();
  151. ctx.restore();
  152. }
  153. function shapePath(n, ctx, shift){
  154. var pp = shapes[n].points;
  155. ctx.beginPath();
  156. ctx.moveTo(pp[0], pp[1]);
  157. for (var i=2; i < pp.length; i+=2) ctx.lineTo(pp[i], pp[i+1]);
  158. ctx.lineTo(pp[0], pp[1]);
  159. }
  160. function drawButtons(){
  161. var ctx = cnv.getContext('2d');
  162. ctx.fillStyle = '#f0f0f0';
  163. ctx.fillRect(0, 0, 120, 700);
  164. ctx.strokeStyle = '#505050';
  165. for(var i=0;i<cnames.length;i++) drawButton(ctx, i);
  166. drawSaveButton(ctx);
  167. }
  168. function drawButton(ctx, n, c){
  169. ctx.fillStyle = cnames[n];
  170. ctx.lineWidth = (n==bselected)?4:2;
  171. roundRectPath(ctx, 60,30+70*n,30,30);
  172. ctx.fill();
  173. ctx.stroke();
  174. }
  175. function roundRectPath(ctx, x, y, w, h){
  176. var r = 4;
  177. ctx.beginPath();
  178. ctx.moveTo(x+r, y);
  179. ctx.lineTo(x+w-r, y);
  180. ctx.quadraticCurveTo(x+w,y,x+w,y+r);
  181. ctx.lineTo(x+w, y+h-r);
  182. ctx.quadraticCurveTo(x+w,y+h,x+w-r,y+h);
  183. ctx.lineTo(x+r, y+h);
  184. ctx.quadraticCurveTo(x,y+h,x,y+h-r);
  185. ctx.lineTo(x, y+r);
  186. ctx.quadraticCurveTo(x,y,x+r,y);
  187. }
  188. function drawSaveButton(ctx){
  189. ctx.fillStyle = 'green';
  190. ctx.lineWidth = 3;
  191. ctx.beginPath();
  192. ctx.arc(75,690,15,0,359.2*Math.PI-.001);
  193. ctx.fill();
  194. ctx.stroke();
  195. }
  196. function handleClearButton(){
  197. for(var i in colors){
  198. colors[i] = "white";
  199. fillPiece(i);
  200. strokePiece(i);
  201. }
  202. }