Source: core/drawing/shape/shapeCircle.js

  1. /**
  2. * @fileoverview A class representing an engine-independent
  3. * hoverable, selectable, modifiable and creatable circle shape.
  4. */
  5. goog.provide('xrx.shape.Circle');
  6. goog.provide('xrx.shape.CircleCreatable');
  7. goog.provide('xrx.shape.CircleHoverable');
  8. goog.provide('xrx.shape.CircleModifiable');
  9. goog.provide('xrx.shape.CircleSelectable');
  10. goog.require('xrx.geometry.Circle');
  11. goog.require('xrx.shape.Creatable');
  12. goog.require('xrx.shape.Geometry');
  13. goog.require('xrx.shape.Hoverable');
  14. goog.require('xrx.shape.Modifiable');
  15. goog.require('xrx.shape.Selectable');
  16. /**
  17. * A class representing an engine-independent hoverable, selectable,
  18. * modifiable and creatable circle shape.
  19. * @param {xrx.drawing.Drawing} drawing The parent drawing canvas.
  20. * @constructor
  21. * @extends {xrx.shape.Style}
  22. */
  23. xrx.shape.Circle = function(drawing) {
  24. goog.base(this, drawing, new xrx.geometry.Circle());
  25. /**
  26. * The engine element.
  27. * @type {(xrx.canvas.Circle|xrx.svg.Circle|xrx.vml.Circle)}
  28. * @private
  29. */
  30. this.engineElement_ = this.drawing_.getEngine().createCircle();
  31. };
  32. goog.inherits(xrx.shape.Circle, xrx.shape.Geometry);
  33. /**
  34. * Returns the center point of this circle.
  35. * @return {Array<number>}
  36. */
  37. xrx.shape.Circle.prototype.getCenter = function() {
  38. return [this.geometry_.cx, this.geometry_.cy];
  39. };
  40. /**
  41. * Sets the center point of this circle.
  42. * @param {number} cx The X coordinate of the center point.
  43. * @param {number} cy The Y coordinate of the center point.
  44. */
  45. xrx.shape.Circle.prototype.setCenter = function(cx, cy) {
  46. this.geometry_.cx = cx;
  47. this.geometry_.cy = cy;
  48. };
  49. /**
  50. * Returns the radius of this circle.
  51. * @return {number} The radius.
  52. */
  53. xrx.shape.Circle.prototype.getRadius = function() {
  54. return this.geometry_.r;
  55. };
  56. /**
  57. * Sets the radius of this circle.
  58. * @param {number} r The radius.
  59. */
  60. xrx.shape.Circle.prototype.setRadius = function(r) {
  61. this.geometry_.r = r;
  62. };
  63. /**
  64. * Returns the coordinates of this circle. We assume the center point.
  65. * @return {Array<Array<number>>} The coordinates.
  66. * @private
  67. */
  68. xrx.shape.Circle.prototype.getCoords = function() {
  69. return [this.getCenter()];
  70. };
  71. /**
  72. * Sets the coordinates of this circle. We assume the center point.
  73. * @param {Array<Array<number>>} coords The coordinate.
  74. * @private
  75. */
  76. xrx.shape.Circle.prototype.setCoords = function(coords) {
  77. this.setCenter(coords[0][0], coords[0][1]);
  78. };
  79. /**
  80. * Sets the x coordinate of this circle. We assume the center point.
  81. * @param {number} coords The x coordinate.
  82. * @private
  83. */
  84. xrx.shape.Circle.prototype.setCoordX = function(x) {
  85. this.geometry_.cx = x;
  86. };
  87. /**
  88. * Sets the y coordinate of this circle. We assume the center point.
  89. * @param {number} coords The y coordinate.
  90. * @private
  91. */
  92. xrx.shape.Circle.prototype.setCoordY = function(y) {
  93. this.geometry_.cy = y;
  94. };
  95. /**
  96. * Draws this circle shape.
  97. * @private
  98. */
  99. xrx.shape.Circle.prototype.draw = function() {
  100. this.startDrawing_();
  101. var center = this.getCenter();
  102. this.engineElement_.draw(center[0], center[1], this.getRadius(),
  103. this.getRenderingFillColor(), this.getRenderingFillOpacity(),
  104. this.getRenderingStrokeColor(), this.getRenderingStrokeWidth());
  105. this.finishDrawing_();
  106. };
  107. /**
  108. * Returns a helper shape that makes this shape hoverable.
  109. * @return {xrx.shape.CircleHoverable} The hoverable circle shape.
  110. */
  111. xrx.shape.Circle.prototype.getHoverable = function() {
  112. if (!this.hoverable_) this.hoverable_ = new xrx.shape.CircleHoverable(this);
  113. return this.hoverable_;
  114. };
  115. /**
  116. * Returns a helper shape that makes this shape selectable.
  117. * @return {xrx.shape.CircleSelectable} The selectable circle shape.
  118. */
  119. xrx.shape.Circle.prototype.getSelectable = function() {
  120. if (!this.selectable_) this.selectable_ = new xrx.shape.CircleSelectable(this);
  121. return this.selectable_;
  122. };
  123. /**
  124. * Returns a helper shape that makes this shape modifiable.
  125. * @return {xrx.shape.CircleModifiable} The modifiable circle shape.
  126. */
  127. xrx.shape.Circle.prototype.getModifiable = function() {
  128. if (!this.modifiable_) this.modifiable_ = new xrx.shape.CircleModifiable(this);
  129. return this.modifiable_;
  130. };
  131. /**
  132. * Returns a helper shape that makes this shape creatable.
  133. * @return {xrx.shape.CircleCreatable} The creatable circle shape.
  134. */
  135. xrx.shape.Circle.prototype.getCreatable = function() {
  136. if (!this.creatable_) this.creatable_ = new xrx.shape.CircleCreatable(this);
  137. return this.creatable_;
  138. };
  139. /**
  140. * Disposes this circle shape.
  141. */
  142. xrx.shape.Circle.prototype.disposeInternal = function() {
  143. goog.base(this, 'disposeInternal');
  144. };
  145. /**
  146. * A class representing a hoverable circle shape.
  147. * @param {xrx.shape.Circle} circle The parent circle shape.
  148. * @constructor
  149. * @private
  150. */
  151. xrx.shape.CircleHoverable = function(circle) {
  152. goog.base(this, circle);
  153. };
  154. goog.inherits(xrx.shape.CircleHoverable, xrx.shape.Hoverable);
  155. /**
  156. * Disposes this hoverable circle shape.
  157. */
  158. xrx.shape.CircleHoverable.prototype.disposeInternal = function() {
  159. goog.base(this, 'disposeInternal');
  160. };
  161. /**
  162. * A class representing a selectable circle shape.
  163. * @param {xrx.shape.Circle} circle The circle to be selected.
  164. * @constructor
  165. * @private
  166. */
  167. xrx.shape.CircleSelectable = function(circle) {
  168. goog.base(this, circle);
  169. };
  170. goog.inherits(xrx.shape.CircleSelectable, xrx.shape.Selectable);
  171. /**
  172. * Disposes this selectable circle shape.
  173. * @private
  174. */
  175. xrx.shape.CircleSelectable.prototype.disposeInternal = function() {
  176. goog.base(this, 'disposeInternal');
  177. };
  178. /**
  179. * A class representing a modifiable circle shape.
  180. * @param {xrx.shape.Circle} circle The circle to be modified.
  181. * @constructor
  182. * @private
  183. */
  184. xrx.shape.CircleModifiable = function(circle) {
  185. goog.base(this, circle);
  186. this.init_();
  187. };
  188. goog.inherits(xrx.shape.CircleModifiable, xrx.shape.Modifiable);
  189. /**
  190. * @private
  191. */
  192. xrx.shape.CircleModifiable.prototype.setCoords = function(coords) {
  193. this.shape_.setCoords(coords);
  194. this.dragger_[0].setCoordX(coords[0][0] + this.shape_.getRadius());
  195. this.dragger_[0].setCoordY(coords[0][1]);
  196. };
  197. /**
  198. * @private
  199. */
  200. xrx.shape.CircleModifiable.prototype.setCoordAt = function(pos, coord) {
  201. this.shape_.setRadius(Math.abs(coord[0] - this.shape_.getCenter()[0]));
  202. this.dragger_[0].setCoordX(coord[0]);
  203. };
  204. /**
  205. * @private
  206. */
  207. xrx.shape.CircleModifiable.prototype.move = function(distX, distY) {
  208. var coords = this.shape_.getCoordsCopy();
  209. coords[0][0] += distX;
  210. coords[0][1] += distY;
  211. this.setCoords(coords);
  212. };
  213. /**
  214. * @private
  215. */
  216. xrx.shape.CircleModifiable.prototype.init_ = function() {
  217. var center = this.shape_.getCenter();
  218. var radius = this.shape_.getRadius();
  219. var dragger = new xrx.shape.Dragger(this, 0);
  220. dragger.setCoords([[center[0] + radius, center[1]]]);
  221. this.setDragger([dragger]);
  222. };
  223. /**
  224. * Disposes this modifiable circle shape.
  225. * @private
  226. */
  227. xrx.shape.CircleModifiable.prototype.disposeInternal = function() {
  228. goog.base(this, 'disposeInternal');
  229. };
  230. /**
  231. * A class representing a creatable circle shape.
  232. * @param {xrx.shape.Circle} circle The target circle to be created.
  233. * @constructor
  234. * @private
  235. */
  236. xrx.shape.CircleCreatable = function(circle) {
  237. goog.base(this, circle, new xrx.shape.Circle(circle.getDrawing()));
  238. /**
  239. * Center point helper.
  240. * @type {Array<number>}
  241. */
  242. this.point_ = new Array(2);
  243. };
  244. goog.inherits(xrx.shape.CircleCreatable, xrx.shape.Creatable);
  245. /**
  246. * Returns the coordinates of the circle currently created.
  247. * @return Array<number> The coordinates.
  248. * @private
  249. */
  250. xrx.shape.CircleCreatable.prototype.getCoords = function() {
  251. return this.preview_.getCoords();
  252. };
  253. /**
  254. * Handles down gestures.
  255. * @param {goog.events.BrowserEvent} e The browser event.
  256. * @param {xrx.drawing.Cursor} cursor The drawing cursor.
  257. * @private
  258. */
  259. xrx.shape.CircleCreatable.prototype.handleDown = function(e, cursor) {
  260. var point = cursor.getPointTransformed();
  261. this.point_[0] = point[0];
  262. this.point_[1] = point[1];
  263. this.preview_.setCenter(point[0], point[1]);
  264. this.preview_.setRadius(0);
  265. this.target_.getDrawing().eventShapeCreate([this.preview_]);
  266. };
  267. /**
  268. * Handles <em>move</em> gestures.
  269. * @param {goog.events.BrowserEvent} e The browser event.
  270. * @param {xrx.drawing.Cursor} cursor The drawing cursor.
  271. * @private
  272. */
  273. xrx.shape.CircleCreatable.prototype.handleMove = function(e, cursor) {
  274. var point = cursor.getPointTransformed();
  275. var distX = point[0] - this.point_[0];
  276. var distY = point[1] - this.point_[1];
  277. this.preview_.setCenter(point[0] - distX / 2, point[1] - distY / 2);
  278. this.preview_.setRadius(Math.sqrt(distX * distX + distY * distY) / 2);
  279. };
  280. /**
  281. * Handles <em>up</em> gestures.
  282. * @param {goog.events.BrowserEvent} e The browser event.
  283. * @param {xrx.drawing.Cursor} cursor The drawing cursor.
  284. * @private
  285. */
  286. xrx.shape.CircleCreatable.prototype.handleUp = function(e, cursor) {
  287. var point = cursor.getPointTransformed();
  288. var circle = new xrx.shape.Circle(this.target_.getDrawing());
  289. var center = this.preview_.getCenter();
  290. circle.setStyle(this.target_);
  291. circle.setCenter(center[0], center[1]);
  292. circle.setRadius(this.preview_.getRadius());
  293. this.target_.getDrawing().eventShapeCreated(circle);
  294. };
  295. /**
  296. * Disposes this creatable circle shape.
  297. * @private
  298. */
  299. xrx.shape.CircleCreatable.prototype.disposeInternal = function() {
  300. this.point_ = null;
  301. goog.base(this, 'disposeInternal');
  302. };