Debian Codegraph @

closure-library/closure/goog/graphics/ext/shape.js

raw
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
16 /**
17 * @fileoverview A thick wrapper around shapes with custom paths.
18 * @author [email protected] (Robby Walker)
19 */
20
21
22 goog.provide('goog.graphics.ext.Shape');
23
24 goog.require('goog.graphics.ext.StrokeAndFillElement');
25
26
27
28 /**
29 * Wrapper for a graphics shape element.
30 * @param {goog.graphics.ext.Group} group Parent for this element.
31 * @param {!goog.graphics.ext.Path} path The path to draw.
32 * @param {boolean=} opt_autoSize Optional flag to specify the path should
33 * automatically resize to fit the element. Defaults to false.
34 * @constructor
35 * @extends {goog.graphics.ext.StrokeAndFillElement}
36 * @final
37 */
38 goog.graphics.ext.Shape = function(group, path, opt_autoSize) {
39 this.autoSize_ = !!opt_autoSize;
40
41 var graphics = group.getGraphicsImplementation();
42 var wrapper = graphics.drawPath(path, null, null, group.getWrapper());
43 goog.graphics.ext.StrokeAndFillElement.call(this, group, wrapper);
44 this.setPath(path);
45 };
46 goog.inherits(goog.graphics.ext.Shape, goog.graphics.ext.StrokeAndFillElement);
47
48
49 /**
50 * Whether or not to automatically resize the shape's path when the element
51 * itself is resized.
52 * @type {boolean}
53 * @private
54 */
55 goog.graphics.ext.Shape.prototype.autoSize_ = false;
56
57
58 /**
59 * The original path, specified by the caller.
60 * @type {goog.graphics.Path}
61 * @private
62 */
63 goog.graphics.ext.Shape.prototype.path_;
64
65
66 /**
67 * The bounding box of the original path.
68 * @type {goog.math.Rect?}
69 * @private
70 */
71 goog.graphics.ext.Shape.prototype.boundingBox_ = null;
72
73
74 /**
75 * The scaled path.
76 * @type {goog.graphics.Path}
77 * @private
78 */
79 goog.graphics.ext.Shape.prototype.scaledPath_;
80
81
82 /**
83 * Get the path drawn by this shape.
84 * @return {goog.graphics.Path?} The path drawn by this shape.
85 */
86 goog.graphics.ext.Shape.prototype.getPath = function() {
87 return this.path_;
88 };
89
90
91 /**
92 * Set the path to draw.
93 * @param {goog.graphics.ext.Path} path The path to draw.
94 */
95 goog.graphics.ext.Shape.prototype.setPath = function(path) {
96 this.path_ = path;
97
98 if (this.autoSize_) {
99 this.boundingBox_ = path.getBoundingBox();
100 }
101
102 this.scaleAndSetPath_();
103 };
104
105
106 /**
107 * Scale the internal path to fit.
108 * @private
109 */
110 goog.graphics.ext.Shape.prototype.scaleAndSetPath_ = function() {
111 this.scaledPath_ = this.boundingBox_ ?
112 this.path_.clone().modifyBounds(
113 -this.boundingBox_.left, -this.boundingBox_.top,
114 this.getWidth() / (this.boundingBox_.width || 1),
115 this.getHeight() / (this.boundingBox_.height || 1)) :
116 this.path_;
117
118 var wrapper = this.getWrapper();
119 if (wrapper) {
120 wrapper.setPath(this.scaledPath_);
121 }
122 };
123
124
125 /**
126 * Redraw the ellipse. Called when the coordinate system is changed.
127 * @protected
128 * @override
129 */
130 goog.graphics.ext.Shape.prototype.redraw = function() {
131 goog.graphics.ext.Shape.superClass_.redraw.call(this);
132 if (this.autoSize_) {
133 this.scaleAndSetPath_();
134 }
135 };
136
137
138 /**
139 * @return {boolean} Whether the shape is parent dependent.
140 * @protected
141 * @override
142 */
143 goog.graphics.ext.Shape.prototype.checkParentDependent = function() {
144 return this.autoSize_ ||
145 goog.graphics.ext.Shape.superClass_.checkParentDependent.call(this);
146 };