aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/graphics/ext/shape.js
blob: 3531bc0054bcc67b9eebf373b50d6ce5b6d53129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview A thick wrapper around shapes with custom paths.
 * @author robbyw@google.com (Robby Walker)
 */


goog.provide('goog.graphics.ext.Shape');

goog.require('goog.graphics.ext.Path');
goog.require('goog.graphics.ext.StrokeAndFillElement');
goog.require('goog.math.Rect');



/**
 * Wrapper for a graphics shape element.
 * @param {goog.graphics.ext.Group} group Parent for this element.
 * @param {!goog.graphics.ext.Path} path  The path to draw.
 * @param {boolean=} opt_autoSize Optional flag to specify the path should
 *     automatically resize to fit the element.  Defaults to false.
 * @constructor
 * @extends {goog.graphics.ext.StrokeAndFillElement}
 */
goog.graphics.ext.Shape = function(group, path, opt_autoSize) {
  this.autoSize_ = !!opt_autoSize;

  var graphics = group.getGraphicsImplementation();
  var wrapper = graphics.drawPath(path, null, null,
      group.getWrapper());
  goog.graphics.ext.StrokeAndFillElement.call(this, group, wrapper);
  this.setPath(path);
};
goog.inherits(goog.graphics.ext.Shape, goog.graphics.ext.StrokeAndFillElement);


/**
 * Whether or not to automatically resize the shape's path when the element
 * itself is resized.
 * @type {boolean}
 * @private
 */
goog.graphics.ext.Shape.prototype.autoSize_ = false;


/**
 * The original path, specified by the caller.
 * @type {goog.graphics.Path}
 * @private
 */
goog.graphics.ext.Shape.prototype.path_;


/**
 * The bounding box of the original path.
 * @type {goog.math.Rect?}
 * @private
 */
goog.graphics.ext.Shape.prototype.boundingBox_ = null;


/**
 * The scaled path.
 * @type {goog.graphics.Path}
 * @private
 */
goog.graphics.ext.Shape.prototype.scaledPath_;


/**
 * Get the path drawn by this shape.
 * @return {goog.graphics.Path?} The path drawn by this shape.
 */
goog.graphics.ext.Shape.prototype.getPath = function() {
  return this.path_;
};


/**
 * Set the path to draw.
 * @param {goog.graphics.ext.Path} path The path to draw.
 */
goog.graphics.ext.Shape.prototype.setPath = function(path) {
  this.path_ = path;

  if (this.autoSize_) {
    this.boundingBox_ = path.getBoundingBox();
  }

  this.scaleAndSetPath_();
};


/**
 * Scale the internal path to fit.
 * @private
 */
goog.graphics.ext.Shape.prototype.scaleAndSetPath_ = function() {
  this.scaledPath_ = this.boundingBox_ ? this.path_.clone().modifyBounds(
      -this.boundingBox_.left, -this.boundingBox_.top,
      this.getWidth() / (this.boundingBox_.width || 1),
      this.getHeight() / (this.boundingBox_.height || 1)) : this.path_;

  var wrapper = this.getWrapper();
  if (wrapper) {
    wrapper.setPath(this.scaledPath_);
  }
};


/**
 * Redraw the ellipse.  Called when the coordinate system is changed.
 * @protected
 * @override
 */
goog.graphics.ext.Shape.prototype.redraw = function() {
  goog.graphics.ext.Shape.superClass_.redraw.call(this);
  if (this.autoSize_) {
    this.scaleAndSetPath_();
  }
};


/**
 * @return {boolean} Whether the shape is parent dependent.
 * @protected
 * @override
 */
goog.graphics.ext.Shape.prototype.checkParentDependent = function() {
  return this.autoSize_ ||
      goog.graphics.ext.Shape.superClass_.checkParentDependent.call(this);
};