Path: blob/trunk/third_party/closure/goog/graphics/ext/shape.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview A thick wrapper around shapes with custom paths.17* @author [email protected] (Robby Walker)18*/192021goog.provide('goog.graphics.ext.Shape');2223goog.require('goog.graphics.ext.StrokeAndFillElement');24252627/**28* Wrapper for a graphics shape element.29* @param {goog.graphics.ext.Group} group Parent for this element.30* @param {!goog.graphics.ext.Path} path The path to draw.31* @param {boolean=} opt_autoSize Optional flag to specify the path should32* automatically resize to fit the element. Defaults to false.33* @constructor34* @extends {goog.graphics.ext.StrokeAndFillElement}35* @final36*/37goog.graphics.ext.Shape = function(group, path, opt_autoSize) {38this.autoSize_ = !!opt_autoSize;3940var graphics = group.getGraphicsImplementation();41var wrapper = graphics.drawPath(path, null, null, group.getWrapper());42goog.graphics.ext.StrokeAndFillElement.call(this, group, wrapper);43this.setPath(path);44};45goog.inherits(goog.graphics.ext.Shape, goog.graphics.ext.StrokeAndFillElement);464748/**49* Whether or not to automatically resize the shape's path when the element50* itself is resized.51* @type {boolean}52* @private53*/54goog.graphics.ext.Shape.prototype.autoSize_ = false;555657/**58* The original path, specified by the caller.59* @type {goog.graphics.Path}60* @private61*/62goog.graphics.ext.Shape.prototype.path_;636465/**66* The bounding box of the original path.67* @type {goog.math.Rect?}68* @private69*/70goog.graphics.ext.Shape.prototype.boundingBox_ = null;717273/**74* The scaled path.75* @type {goog.graphics.Path}76* @private77*/78goog.graphics.ext.Shape.prototype.scaledPath_;798081/**82* Get the path drawn by this shape.83* @return {goog.graphics.Path?} The path drawn by this shape.84*/85goog.graphics.ext.Shape.prototype.getPath = function() {86return this.path_;87};888990/**91* Set the path to draw.92* @param {goog.graphics.ext.Path} path The path to draw.93*/94goog.graphics.ext.Shape.prototype.setPath = function(path) {95this.path_ = path;9697if (this.autoSize_) {98this.boundingBox_ = path.getBoundingBox();99}100101this.scaleAndSetPath_();102};103104105/**106* Scale the internal path to fit.107* @private108*/109goog.graphics.ext.Shape.prototype.scaleAndSetPath_ = function() {110this.scaledPath_ = this.boundingBox_ ?111this.path_.clone().modifyBounds(112-this.boundingBox_.left, -this.boundingBox_.top,113this.getWidth() / (this.boundingBox_.width || 1),114this.getHeight() / (this.boundingBox_.height || 1)) :115this.path_;116117var wrapper = this.getWrapper();118if (wrapper) {119wrapper.setPath(this.scaledPath_);120}121};122123124/**125* Redraw the ellipse. Called when the coordinate system is changed.126* @protected127* @override128*/129goog.graphics.ext.Shape.prototype.redraw = function() {130goog.graphics.ext.Shape.superClass_.redraw.call(this);131if (this.autoSize_) {132this.scaleAndSetPath_();133}134};135136137/**138* @return {boolean} Whether the shape is parent dependent.139* @protected140* @override141*/142goog.graphics.ext.Shape.prototype.checkParentDependent = function() {143return this.autoSize_ ||144goog.graphics.ext.Shape.superClass_.checkParentDependent.call(this);145};146147148