ext-polygon.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*globals svgEditor, svgCanvas, svgedit, $*/
  2. /*jslint vars: true, eqeq: true, todo: true */
  3. /*
  4. * ext-polygon.js
  5. *
  6. *
  7. * Copyright(c) 2010 CloudCanvas, Inc.
  8. * All rights reserved
  9. *
  10. */
  11. svgEditor.addExtension("polygon", function(S) {'use strict';
  12. var // NS = svgedit.NS,
  13. // svgcontent = S.svgcontent,
  14. // addElem = S.addSvgElementFromJson,
  15. selElems,
  16. editingitex = false,
  17. // svgdoc = S.svgroot.parentNode.ownerDocument,
  18. // newFOG, newFOGParent, newDef, newImageName, newMaskID, modeChangeG,
  19. // edg = 0,
  20. // undoCommand = "Not image";
  21. started, newFO;
  22. // var ccZoom;
  23. // var wEl, hEl;
  24. // var wOffset, hOffset;
  25. // var ccRBG;
  26. var ccRgbEl;
  27. // var ccOpacity;
  28. // var brushW, brushH;
  29. var shape;
  30. // var ccDebug = document.getElementById('debugpanel');
  31. /* var properlySourceSizeTextArea = function(){
  32. // TODO: remove magic numbers here and get values from CSS
  33. var height = $('#svg_source_container').height() - 80;
  34. $('#svg_source_textarea').css('height', height);
  35. }; */
  36. function showPanel(on){
  37. var fc_rules = $('#fc_rules');
  38. if (!fc_rules.length) {
  39. fc_rules = $('<style id="fc_rules"></style>').appendTo('head');
  40. }
  41. fc_rules.text(!on ? "" : " #tool_topath { display: none !important; }");
  42. $('#polygon_panel').toggle(on);
  43. }
  44. /*
  45. function toggleSourceButtons(on){
  46. $('#tool_source_save, #tool_source_cancel').toggle(!on);
  47. $('#polygon_save, #polygon_cancel').toggle(on);
  48. }
  49. */
  50. function setAttr(attr, val){
  51. svgCanvas.changeSelectedAttribute(attr, val);
  52. S.call("changed", selElems);
  53. }
  54. function cot(n){
  55. return 1 / Math.tan(n);
  56. }
  57. function sec(n){
  58. return 1 / Math.cos(n);
  59. }
  60. /**
  61. * Obtained from http://code.google.com/p/passenger-top/source/browse/instiki/public/svg-edit/editor/extensions/ext-itex.js?r=3
  62. * This function sets the content of of the currently-selected foreignObject element,
  63. * based on the itex contained in string.
  64. * @param {string} tex The itex text.
  65. * @returns This function returns false if the set was unsuccessful, true otherwise.
  66. */
  67. /*
  68. function setItexString(tex) {
  69. var mathns = 'http://www.w3.org/1998/Math/MathML',
  70. xmlnsns = 'http://www.w3.org/2000/xmlns/',
  71. ajaxEndpoint = '../../itex';
  72. var elt = selElems[0];
  73. try {
  74. var math = svgdoc.createElementNS(mathns, 'math');
  75. math.setAttributeNS(xmlnsns, 'xmlns', mathns);
  76. math.setAttribute('display', 'inline');
  77. var semantics = document.createElementNS(mathns, 'semantics');
  78. var annotation = document.createElementNS(mathns, 'annotation');
  79. annotation.setAttribute('encoding', 'application/x-tex');
  80. annotation.textContent = tex;
  81. var mrow = document.createElementNS(mathns, 'mrow');
  82. semantics.appendChild(mrow);
  83. semantics.appendChild(annotation);
  84. math.appendChild(semantics);
  85. // make an AJAX request to the server, to get the MathML
  86. $.post(ajaxEndpoint, {'tex': tex, 'display': 'inline'}, function(data){
  87. var children = data.documentElement.childNodes;
  88. while (children.length > 0) {
  89. mrow.appendChild(svgdoc.adoptNode(children[0], true));
  90. }
  91. S.sanitizeSvg(math);
  92. S.call("changed", [elt]);
  93. });
  94. elt.replaceChild(math, elt.firstChild);
  95. S.call("changed", [elt]);
  96. svgCanvas.clearSelection();
  97. } catch(e) {
  98. console.log(e);
  99. return false;
  100. }
  101. return true;
  102. }
  103. */
  104. return {
  105. name: "polygon",
  106. svgicons: svgEditor.curConfig.extPath + "polygon-icons.svg",
  107. buttons: [{
  108. id: "tool_polygon",
  109. type: "mode",
  110. title: "Polygon Tool",
  111. position: 11,
  112. events: {
  113. 'click': function(){
  114. svgCanvas.setMode('polygon');
  115. showPanel(true);
  116. }
  117. }
  118. }],
  119. context_tools: [{
  120. type: "input",
  121. panel: "polygon_panel",
  122. title: "Number of Sides",
  123. id: "polySides",
  124. label: "sides",
  125. size: 3,
  126. defval: 5,
  127. events: {
  128. change: function(){
  129. setAttr('sides', this.value);
  130. }
  131. }
  132. }],
  133. callback: function(){
  134. $('#polygon_panel').hide();
  135. var endChanges = function(){
  136. };
  137. // TODO: Needs to be done after orig icon loads
  138. setTimeout(function(){
  139. // Create source save/cancel buttons
  140. var save = $('#tool_source_save').clone().hide().attr('id', 'polygon_save').unbind().appendTo("#tool_source_back").click(function(){
  141. if (!editingitex) {
  142. return;
  143. }
  144. // Todo: Uncomment the setItexString() function above and handle ajaxEndpoint?
  145. if (!setItexString($('#svg_source_textarea').val())) {
  146. $.confirm("Errors found. Revert to original?", function(ok){
  147. if (!ok) {
  148. return false;
  149. }
  150. endChanges();
  151. });
  152. }
  153. else {
  154. endChanges();
  155. }
  156. // setSelectMode();
  157. });
  158. var cancel = $('#tool_source_cancel').clone().hide().attr('id', 'polygon_cancel').unbind().appendTo("#tool_source_back").click(function(){
  159. endChanges();
  160. });
  161. }, 3000);
  162. },
  163. mouseDown: function(opts){
  164. // var e = opts.event;
  165. var rgb = svgCanvas.getColor("fill");
  166. ccRgbEl = rgb.substring(1, rgb.length);
  167. var sRgb = svgCanvas.getColor("stroke");
  168. // ccSRgbEl = sRgb.substring(1, rgb.length);
  169. var sWidth = svgCanvas.getStrokeWidth();
  170. if (svgCanvas.getMode() == "polygon") {
  171. started = true;
  172. newFO = S.addSvgElementFromJson({
  173. "element": "polygon",
  174. "attr": {
  175. "cx": opts.start_x,
  176. "cy": opts.start_y,
  177. "id": S.getNextId(),
  178. "shape": "regularPoly",
  179. "sides": document.getElementById("polySides").value,
  180. "orient": "x",
  181. "edge": 0,
  182. "fill": rgb,
  183. "strokecolor": sRgb,
  184. "strokeWidth": sWidth
  185. }
  186. });
  187. return {
  188. started: true
  189. };
  190. }
  191. },
  192. mouseMove: function(opts){
  193. if (!started) {
  194. return;
  195. }
  196. if (svgCanvas.getMode() == "polygon") {
  197. // var e = opts.event;
  198. var x = opts.mouse_x;
  199. var y = opts.mouse_y;
  200. var c = $(newFO).attr(["cx", "cy", "sides", "orient", "fill", "strokecolor", "strokeWidth"]);
  201. var cx = c.cx, cy = c.cy, fill = c.fill, strokecolor = c.strokecolor, strokewidth = c.strokeWidth, sides = c.sides,
  202. // orient = c.orient,
  203. edg = (Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy))) / 1.5;
  204. newFO.setAttributeNS(null, "edge", edg);
  205. var inradius = (edg / 2) * cot(Math.PI / sides);
  206. var circumradius = inradius * sec(Math.PI / sides);
  207. var points = '';
  208. var s;
  209. for (s = 0; sides >= s; s++) {
  210. var angle = 2.0 * Math.PI * s / sides;
  211. x = (circumradius * Math.cos(angle)) + cx;
  212. y = (circumradius * Math.sin(angle)) + cy;
  213. points += x + ',' + y + ' ';
  214. }
  215. //var poly = newFO.createElementNS(NS.SVG, 'polygon');
  216. newFO.setAttributeNS(null, 'points', points);
  217. newFO.setAttributeNS(null, 'fill', fill);
  218. newFO.setAttributeNS(null, 'stroke', strokecolor);
  219. newFO.setAttributeNS(null, 'stroke-width', strokewidth);
  220. // newFO.setAttributeNS(null, 'transform', "rotate(-90)");
  221. shape = newFO.getAttributeNS(null, 'shape');
  222. //newFO.appendChild(poly);
  223. //DrawPoly(cx, cy, sides, edg, orient);
  224. return {
  225. started: true
  226. };
  227. }
  228. },
  229. mouseUp: function(opts){
  230. if (svgCanvas.getMode() == "polygon") {
  231. var attrs = $(newFO).attr("edge");
  232. var keep = (attrs.edge != 0);
  233. // svgCanvas.addToSelection([newFO], true);
  234. return {
  235. keep: keep,
  236. element: newFO
  237. };
  238. }
  239. },
  240. selectedChanged: function(opts){
  241. // Use this to update the current selected elements
  242. selElems = opts.elems;
  243. var i = selElems.length;
  244. while (i--) {
  245. var elem = selElems[i];
  246. if (elem && elem.getAttributeNS(null, 'shape') === 'regularPoly') {
  247. if (opts.selectedElement && !opts.multiselected) {
  248. $('#polySides').val(elem.getAttribute("sides"));
  249. showPanel(true);
  250. }
  251. else {
  252. showPanel(false);
  253. }
  254. }
  255. else {
  256. showPanel(false);
  257. }
  258. }
  259. },
  260. elementChanged: function(opts){
  261. // var elem = opts.elems[0];
  262. }
  263. };
  264. });