shape.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A thick wrapper around shapes with custom paths.
  16. * @author robbyw@google.com (Robby Walker)
  17. */
  18. goog.provide('goog.graphics.ext.Shape');
  19. goog.require('goog.graphics.ext.StrokeAndFillElement');
  20. /**
  21. * Wrapper for a graphics shape element.
  22. * @param {goog.graphics.ext.Group} group Parent for this element.
  23. * @param {!goog.graphics.ext.Path} path The path to draw.
  24. * @param {boolean=} opt_autoSize Optional flag to specify the path should
  25. * automatically resize to fit the element. Defaults to false.
  26. * @constructor
  27. * @extends {goog.graphics.ext.StrokeAndFillElement}
  28. * @final
  29. */
  30. goog.graphics.ext.Shape = function(group, path, opt_autoSize) {
  31. this.autoSize_ = !!opt_autoSize;
  32. var graphics = group.getGraphicsImplementation();
  33. var wrapper = graphics.drawPath(path, null, null, group.getWrapper());
  34. goog.graphics.ext.StrokeAndFillElement.call(this, group, wrapper);
  35. this.setPath(path);
  36. };
  37. goog.inherits(goog.graphics.ext.Shape, goog.graphics.ext.StrokeAndFillElement);
  38. /**
  39. * Whether or not to automatically resize the shape's path when the element
  40. * itself is resized.
  41. * @type {boolean}
  42. * @private
  43. */
  44. goog.graphics.ext.Shape.prototype.autoSize_ = false;
  45. /**
  46. * The original path, specified by the caller.
  47. * @type {goog.graphics.Path}
  48. * @private
  49. */
  50. goog.graphics.ext.Shape.prototype.path_;
  51. /**
  52. * The bounding box of the original path.
  53. * @type {goog.math.Rect?}
  54. * @private
  55. */
  56. goog.graphics.ext.Shape.prototype.boundingBox_ = null;
  57. /**
  58. * The scaled path.
  59. * @type {goog.graphics.Path}
  60. * @private
  61. */
  62. goog.graphics.ext.Shape.prototype.scaledPath_;
  63. /**
  64. * Get the path drawn by this shape.
  65. * @return {goog.graphics.Path?} The path drawn by this shape.
  66. */
  67. goog.graphics.ext.Shape.prototype.getPath = function() {
  68. return this.path_;
  69. };
  70. /**
  71. * Set the path to draw.
  72. * @param {goog.graphics.ext.Path} path The path to draw.
  73. */
  74. goog.graphics.ext.Shape.prototype.setPath = function(path) {
  75. this.path_ = path;
  76. if (this.autoSize_) {
  77. this.boundingBox_ = path.getBoundingBox();
  78. }
  79. this.scaleAndSetPath_();
  80. };
  81. /**
  82. * Scale the internal path to fit.
  83. * @private
  84. */
  85. goog.graphics.ext.Shape.prototype.scaleAndSetPath_ = function() {
  86. this.scaledPath_ = this.boundingBox_ ?
  87. this.path_.clone().modifyBounds(
  88. -this.boundingBox_.left, -this.boundingBox_.top,
  89. this.getWidth() / (this.boundingBox_.width || 1),
  90. this.getHeight() / (this.boundingBox_.height || 1)) :
  91. this.path_;
  92. var wrapper = this.getWrapper();
  93. if (wrapper) {
  94. wrapper.setPath(this.scaledPath_);
  95. }
  96. };
  97. /**
  98. * Redraw the ellipse. Called when the coordinate system is changed.
  99. * @protected
  100. * @override
  101. */
  102. goog.graphics.ext.Shape.prototype.redraw = function() {
  103. goog.graphics.ext.Shape.superClass_.redraw.call(this);
  104. if (this.autoSize_) {
  105. this.scaleAndSetPath_();
  106. }
  107. };
  108. /**
  109. * @return {boolean} Whether the shape is parent dependent.
  110. * @protected
  111. * @override
  112. */
  113. goog.graphics.ext.Shape.prototype.checkParentDependent = function() {
  114. return this.autoSize_ ||
  115. goog.graphics.ext.Shape.superClass_.checkParentDependent.call(this);
  116. };