Source: core/drawing/shape/abstract/shapeRenderStylable.js

  1. /**
  2. * @fileoverview A class implementing dynamic rendering of shapes.
  3. * @private
  4. */
  5. goog.provide('xrx.shape.RenderStylable');
  6. goog.require('xrx.engine');
  7. goog.require('xrx.shape.Style');
  8. /**
  9. * A class implementing dynamic rendering of shapes.
  10. * @param {xrx.drawing.Drawing} drawing The parent drawing canvas.
  11. * @constructor
  12. * @extends {xrx.shape.Style}
  13. * @private
  14. */
  15. xrx.shape.RenderStylable = function(drawing) {
  16. goog.base(this);
  17. /**
  18. * The current zoom factor to realize constant
  19. * line width and constant size of dragging elements.
  20. * @type {number}
  21. */
  22. this.zoomFactor_ = 1;
  23. /**
  24. * The shape group, if the shape belongs to a group.
  25. * @type {xrx.shape.ShapeGroup}
  26. */
  27. this.shapeGroup_;
  28. /**
  29. * Whether this shape has a DOM representation.
  30. * @type {boolean}
  31. * @private
  32. */
  33. this.hasDom_ = xrx.engine.hasDom(drawing.getEngine().getName());
  34. /**
  35. * Object describing whether the fill style did change
  36. * since the last drawing.
  37. * @type {Object}
  38. * @private
  39. */
  40. this.fillChanged_ = {
  41. color: true,
  42. opacity: true
  43. };
  44. /**
  45. * Object describing whether the stroke style did change
  46. * since the last drawing.
  47. * @type {Object}
  48. * @private
  49. */
  50. this.strokeChanged_ = {
  51. color: true,
  52. width: true
  53. };
  54. };
  55. goog.inherits(xrx.shape.RenderStylable, xrx.shape.Style);
  56. /**
  57. * Returns the current shape group.
  58. * @return {xrx.shape.ShapeGroup} The shape group.
  59. * @private
  60. */
  61. xrx.shape.RenderStylable.prototype.getShapeGroup = function() {
  62. return this.shapeGroup_;
  63. };
  64. /**
  65. * Sets this shape to be part of a shape group.
  66. * @param {xrx.shape.ShapeGroup} shapeGroup The shape group.
  67. * @private
  68. */
  69. xrx.shape.RenderStylable.prototype.setShapeGroup = function(shapeGroup) {
  70. this.shapeGroup_ = shapeGroup;
  71. };
  72. /**
  73. * Returns the underlying engine element.
  74. * @return {xrx.engine.Element} The engine element.
  75. */
  76. xrx.shape.RenderStylable.prototype.getEngineElement = function() {
  77. return this.engineElement_;
  78. };
  79. /**
  80. * @private
  81. */
  82. xrx.shape.RenderStylable.prototype.setZoomFactor = function(factor) {
  83. this.zoomFactor_ = factor;
  84. };
  85. /**
  86. * @private
  87. */
  88. xrx.shape.RenderStylable.prototype.setStyle = function(style) {
  89. goog.base(this, 'setStyle', style);
  90. this.fillChanged_.color = true;
  91. this.fillChanged_.opacity = true;
  92. this.strokeChanged_.color = true;
  93. this.strokeChanged_.width = true;
  94. };
  95. /**
  96. * @private
  97. */
  98. xrx.shape.RenderStylable.prototype.setFillColor = function(color) {
  99. goog.base(this, 'setFillColor', color);
  100. this.fillChanged_.color = true;
  101. };
  102. /**
  103. * @private
  104. */
  105. xrx.shape.RenderStylable.prototype.getRenderingFillColor = function() {
  106. var color = this.shapeGroup_ ? this.shapeGroup_.getFillColor() :
  107. ((this.hasDom_ && (this.fillChanged_.color === false)) ? undefined : this.fill_.color);
  108. this.fillChanged_.color = false;
  109. return color;
  110. };
  111. /**
  112. * @private
  113. */
  114. xrx.shape.RenderStylable.prototype.setFillOpacity = function(factor) {
  115. goog.base(this, 'setFillOpacity', factor);
  116. this.fillChanged_.opacity = true;
  117. };
  118. /**
  119. * @private
  120. */
  121. xrx.shape.RenderStylable.prototype.getRenderingFillOpacity = function() {
  122. var opacity = this.shapeGroup_ ? this.shapeGroup_.getFillOpacity() :
  123. this.hasDom_ && this.fillChanged_.opacity === false ? undefined : this.fill_.opacity;
  124. this.fillChanged_.opacity = false;
  125. return opacity;
  126. };
  127. /**
  128. * @private
  129. */
  130. xrx.shape.RenderStylable.prototype.setStrokeColor = function(color) {
  131. goog.base(this, 'setStrokeColor', color);
  132. this.strokeChanged_.color = true;
  133. };
  134. /**
  135. * @private
  136. */
  137. xrx.shape.RenderStylable.prototype.getRenderingStrokeColor = function() {
  138. var color = this.shapeGroup_ ? this.shapeGroup_.getStrokeColor() :
  139. this.hasDom_ && this.strokeChanged_.color === false ? undefined : this.stroke_.color;
  140. this.strokeChanged_.color = false;
  141. return color;
  142. };
  143. /**
  144. * @private
  145. */
  146. xrx.shape.RenderStylable.prototype.setStrokeWidth = function(width) {
  147. goog.base(this, 'setStrokeWidth', width);
  148. this.strokeChanged_.width = true;
  149. };
  150. /**
  151. * Returns the stroke width of this shape.
  152. * @return {number} The stroke width.
  153. */
  154. xrx.shape.RenderStylable.prototype.getRenderingStrokeWidth = function() {
  155. var width = this.shapeGroup_ ? this.shapeGroup_.getStrokeWidth() / this.zoomFactor_ :
  156. this.hasDom_ && this.strokeChanged_.width === false ? undefined :
  157. this.stroke_.width / this.zoomFactor_;
  158. this.strokeChanged_.width = false;
  159. return width;
  160. };
  161. /**
  162. * @private
  163. */
  164. xrx.shape.RenderStylable.prototype.startDrawing_ = function() {
  165. this.drawing_.eventBeforeRendering(this);
  166. this.engineElement_.applyTransform(this.ctm_);
  167. this.engineElement_.startDrawing();
  168. };
  169. /**
  170. * @private
  171. */
  172. xrx.shape.RenderStylable.prototype.finishDrawing_ = function() {
  173. this.engineElement_.finishDrawing();
  174. };
  175. /**
  176. * Disposes this rendering style object.
  177. */
  178. xrx.shape.RenderStylable.prototype.disposeInternal = function() {
  179. this.engineElement_.dispose();
  180. this.engineElement_ = null;
  181. goog.dispose(this.shapeGroup_);
  182. this.shapeGroup_ = null;
  183. goog.base(this, 'disposeInternal');
  184. };