field_angle.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2013 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Angle input field.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldAngle');
  26. goog.require('Blockly.FieldTextInput');
  27. goog.require('goog.math');
  28. goog.require('goog.userAgent');
  29. /**
  30. * Class for an editable angle field.
  31. * @param {string} text The initial content of the field.
  32. * @param {Function=} opt_validator An optional function that is called
  33. * to validate any constraints on what the user entered. Takes the new
  34. * text as an argument and returns the accepted text or null to abort
  35. * the change.
  36. * @extends {Blockly.FieldTextInput}
  37. * @constructor
  38. */
  39. Blockly.FieldAngle = function(text, opt_validator) {
  40. // Add degree symbol: "360°" (LTR) or "°360" (RTL)
  41. this.symbol_ = Blockly.createSvgElement('tspan', {}, null);
  42. this.symbol_.appendChild(document.createTextNode('\u00B0'));
  43. Blockly.FieldAngle.superClass_.constructor.call(this, text, opt_validator);
  44. };
  45. goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput);
  46. /**
  47. * Round angles to the nearest 15 degrees when using mouse.
  48. * Set to 0 to disable rounding.
  49. */
  50. Blockly.FieldAngle.ROUND = 15;
  51. /**
  52. * Half the width of protractor image.
  53. */
  54. Blockly.FieldAngle.HALF = 100 / 2;
  55. /* The following two settings work together to set the behaviour of the angle
  56. * picker. While many combinations are possible, two modes are typical:
  57. * Math mode.
  58. * 0 deg is right, 90 is up. This is the style used by protractors.
  59. * Blockly.FieldAngle.CLOCKWISE = false;
  60. * Blockly.FieldAngle.OFFSET = 0;
  61. * Compass mode.
  62. * 0 deg is up, 90 is right. This is the style used by maps.
  63. * Blockly.FieldAngle.CLOCKWISE = true;
  64. * Blockly.FieldAngle.OFFSET = 90;
  65. */
  66. /**
  67. * Angle increases clockwise (true) or counterclockwise (false).
  68. */
  69. Blockly.FieldAngle.CLOCKWISE = false;
  70. /**
  71. * Offset the location of 0 degrees (and all angles) by a constant.
  72. * Usually either 0 (0 = right) or 90 (0 = up).
  73. */
  74. Blockly.FieldAngle.OFFSET = 0;
  75. /**
  76. * Maximum allowed angle before wrapping.
  77. * Usually either 360 (for 0 to 359.9) or 180 (for -179.9 to 180).
  78. */
  79. Blockly.FieldAngle.WRAP = 360;
  80. /**
  81. * Radius of protractor circle. Slightly smaller than protractor size since
  82. * otherwise SVG crops off half the border at the edges.
  83. */
  84. Blockly.FieldAngle.RADIUS = Blockly.FieldAngle.HALF - 1;
  85. /**
  86. * Clean up this FieldAngle, as well as the inherited FieldTextInput.
  87. * @return {!Function} Closure to call on destruction of the WidgetDiv.
  88. * @private
  89. */
  90. Blockly.FieldAngle.prototype.dispose_ = function() {
  91. var thisField = this;
  92. return function() {
  93. Blockly.FieldAngle.superClass_.dispose_.call(thisField)();
  94. thisField.gauge_ = null;
  95. if (thisField.clickWrapper_) {
  96. Blockly.unbindEvent_(thisField.clickWrapper_);
  97. }
  98. if (thisField.moveWrapper1_) {
  99. Blockly.unbindEvent_(thisField.moveWrapper1_);
  100. }
  101. if (thisField.moveWrapper2_) {
  102. Blockly.unbindEvent_(thisField.moveWrapper2_);
  103. }
  104. };
  105. };
  106. /**
  107. * Show the inline free-text editor on top of the text.
  108. * @private
  109. */
  110. Blockly.FieldAngle.prototype.showEditor_ = function() {
  111. var noFocus =
  112. goog.userAgent.MOBILE || goog.userAgent.ANDROID || goog.userAgent.IPAD;
  113. // Mobile browsers have issues with in-line textareas (focus & keyboards).
  114. Blockly.FieldAngle.superClass_.showEditor_.call(this, noFocus);
  115. var div = Blockly.WidgetDiv.DIV;
  116. if (!div.firstChild) {
  117. // Mobile interface uses window.prompt.
  118. return;
  119. }
  120. // Build the SVG DOM.
  121. var svg = Blockly.createSvgElement('svg', {
  122. 'xmlns': 'http://www.w3.org/2000/svg',
  123. 'xmlns:html': 'http://www.w3.org/1999/xhtml',
  124. 'xmlns:xlink': 'http://www.w3.org/1999/xlink',
  125. 'version': '1.1',
  126. 'height': (Blockly.FieldAngle.HALF * 2) + 'px',
  127. 'width': (Blockly.FieldAngle.HALF * 2) + 'px'
  128. }, div);
  129. var circle = Blockly.createSvgElement('circle', {
  130. 'cx': Blockly.FieldAngle.HALF, 'cy': Blockly.FieldAngle.HALF,
  131. 'r': Blockly.FieldAngle.RADIUS,
  132. 'class': 'blocklyAngleCircle'
  133. }, svg);
  134. this.gauge_ = Blockly.createSvgElement('path',
  135. {'class': 'blocklyAngleGauge'}, svg);
  136. this.line_ = Blockly.createSvgElement('line',
  137. {'x1': Blockly.FieldAngle.HALF,
  138. 'y1': Blockly.FieldAngle.HALF,
  139. 'class': 'blocklyAngleLine'}, svg);
  140. // Draw markers around the edge.
  141. for (var angle = 0; angle < 360; angle += 15) {
  142. Blockly.createSvgElement('line', {
  143. 'x1': Blockly.FieldAngle.HALF + Blockly.FieldAngle.RADIUS,
  144. 'y1': Blockly.FieldAngle.HALF,
  145. 'x2': Blockly.FieldAngle.HALF + Blockly.FieldAngle.RADIUS -
  146. (angle % 45 == 0 ? 10 : 5),
  147. 'y2': Blockly.FieldAngle.HALF,
  148. 'class': 'blocklyAngleMarks',
  149. 'transform': 'rotate(' + angle + ',' +
  150. Blockly.FieldAngle.HALF + ',' + Blockly.FieldAngle.HALF + ')'
  151. }, svg);
  152. }
  153. svg.style.marginLeft = (15 - Blockly.FieldAngle.RADIUS) + 'px';
  154. this.clickWrapper_ =
  155. Blockly.bindEvent_(svg, 'click', this, Blockly.WidgetDiv.hide);
  156. this.moveWrapper1_ =
  157. Blockly.bindEvent_(circle, 'mousemove', this, this.onMouseMove);
  158. this.moveWrapper2_ =
  159. Blockly.bindEvent_(this.gauge_, 'mousemove', this, this.onMouseMove);
  160. this.updateGraph_();
  161. };
  162. /**
  163. * Set the angle to match the mouse's position.
  164. * @param {!Event} e Mouse move event.
  165. */
  166. Blockly.FieldAngle.prototype.onMouseMove = function(e) {
  167. var bBox = this.gauge_.ownerSVGElement.getBoundingClientRect();
  168. var dx = e.clientX - bBox.left - Blockly.FieldAngle.HALF;
  169. var dy = e.clientY - bBox.top - Blockly.FieldAngle.HALF;
  170. var angle = Math.atan(-dy / dx);
  171. if (isNaN(angle)) {
  172. // This shouldn't happen, but let's not let this error propogate further.
  173. return;
  174. }
  175. angle = goog.math.toDegrees(angle);
  176. // 0: East, 90: North, 180: West, 270: South.
  177. if (dx < 0) {
  178. angle += 180;
  179. } else if (dy > 0) {
  180. angle += 360;
  181. }
  182. if (Blockly.FieldAngle.CLOCKWISE) {
  183. angle = Blockly.FieldAngle.OFFSET + 360 - angle;
  184. } else {
  185. angle -= Blockly.FieldAngle.OFFSET;
  186. }
  187. if (Blockly.FieldAngle.ROUND) {
  188. angle = Math.round(angle / Blockly.FieldAngle.ROUND) *
  189. Blockly.FieldAngle.ROUND;
  190. }
  191. angle = this.callValidator(angle);
  192. Blockly.FieldTextInput.htmlInput_.value = angle;
  193. this.setValue(angle);
  194. this.validate_();
  195. this.resizeEditor_();
  196. };
  197. /**
  198. * Insert a degree symbol.
  199. * @param {?string} text New text.
  200. */
  201. Blockly.FieldAngle.prototype.setText = function(text) {
  202. Blockly.FieldAngle.superClass_.setText.call(this, text);
  203. if (!this.textElement_) {
  204. // Not rendered yet.
  205. return;
  206. }
  207. this.updateGraph_();
  208. // Insert degree symbol.
  209. if (this.sourceBlock_.RTL) {
  210. this.textElement_.insertBefore(this.symbol_, this.textElement_.firstChild);
  211. } else {
  212. this.textElement_.appendChild(this.symbol_);
  213. }
  214. // Cached width is obsolete. Clear it.
  215. this.size_.width = 0;
  216. };
  217. /**
  218. * Redraw the graph with the current angle.
  219. * @private
  220. */
  221. Blockly.FieldAngle.prototype.updateGraph_ = function() {
  222. if (!this.gauge_) {
  223. return;
  224. }
  225. var angleDegrees = Number(this.getText()) + Blockly.FieldAngle.OFFSET;
  226. var angleRadians = goog.math.toRadians(angleDegrees);
  227. var path = ['M ', Blockly.FieldAngle.HALF, ',', Blockly.FieldAngle.HALF];
  228. var x2 = Blockly.FieldAngle.HALF;
  229. var y2 = Blockly.FieldAngle.HALF;
  230. if (!isNaN(angleRadians)) {
  231. var angle1 = goog.math.toRadians(Blockly.FieldAngle.OFFSET);
  232. var x1 = Math.cos(angle1) * Blockly.FieldAngle.RADIUS;
  233. var y1 = Math.sin(angle1) * -Blockly.FieldAngle.RADIUS;
  234. if (Blockly.FieldAngle.CLOCKWISE) {
  235. angleRadians = 2 * angle1 - angleRadians;
  236. }
  237. x2 += Math.cos(angleRadians) * Blockly.FieldAngle.RADIUS;
  238. y2 -= Math.sin(angleRadians) * Blockly.FieldAngle.RADIUS;
  239. // Don't ask how the flag calculations work. They just do.
  240. var largeFlag = Math.abs(Math.floor((angleRadians - angle1) / Math.PI) % 2);
  241. if (Blockly.FieldAngle.CLOCKWISE) {
  242. largeFlag = 1 - largeFlag;
  243. }
  244. var sweepFlag = Number(Blockly.FieldAngle.CLOCKWISE);
  245. path.push(' l ', x1, ',', y1,
  246. ' A ', Blockly.FieldAngle.RADIUS, ',', Blockly.FieldAngle.RADIUS,
  247. ' 0 ', largeFlag, ' ', sweepFlag, ' ', x2, ',', y2, ' z');
  248. }
  249. this.gauge_.setAttribute('d', path.join(''));
  250. this.line_.setAttribute('x2', x2);
  251. this.line_.setAttribute('y2', y2);
  252. };
  253. /**
  254. * Ensure that only an angle may be entered.
  255. * @param {string} text The user's text.
  256. * @return {?string} A string representing a valid angle, or null if invalid.
  257. */
  258. Blockly.FieldAngle.prototype.classValidator = function(text) {
  259. if (text === null) {
  260. return null;
  261. }
  262. var n = parseFloat(text || 0);
  263. if (isNaN(n)) {
  264. return null;
  265. }
  266. n = n % 360;
  267. if (n < 0) {
  268. n += 360;
  269. }
  270. if (n > Blockly.FieldAngle.WRAP) {
  271. n -= 360;
  272. }
  273. return String(n);
  274. };