improved gripRadius ternary assignment in Selector and misc formating

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2385 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Bruno Heridet
2013-02-14 12:47:32 +00:00
parent a9ab2f2c7e
commit d51f96012b

View File

@@ -24,13 +24,11 @@ if (!svgedit.select) {
var svgFactory_; var svgFactory_;
var config_; var config_;
var selectorManager_; // A Singleton var selectorManager_; // A Singleton
var gripRadius = svgedit.browser.isTouch() ? 10 : 4;
var gripRadius;
svgedit.browser.isTouch() ? gripRadius = 10 : gripRadius = 4;
// Class: svgedit.select.Selector // Class: svgedit.select.Selector
// Private class for DOM element selection boxes // Private class for DOM element selection boxes
// //
// Parameters: // Parameters:
// id - integer to internally indentify the selector // id - integer to internally indentify the selector
// elem - DOM element associated with this selector // elem - DOM element associated with this selector
@@ -82,10 +80,10 @@ svgedit.select.Selector = function(id, elem) {
}; };
// Function: svgedit.select.Selector.reset // Function: svgedit.select.Selector.reset
// Used to reset the id and element that the selector is attached to // Used to reset the id and element that the selector is attached to
// //
// Parameters: // Parameters:
// e - DOM element associated with this selector // e - DOM element associated with this selector
svgedit.select.Selector.prototype.reset = function(e) { svgedit.select.Selector.prototype.reset = function(e) {
this.locked = true; this.locked = true;
@@ -102,11 +100,11 @@ svgedit.select.Selector.prototype.reset = function(e) {
svgedit.select.Selector.prototype.updateGripCursors = function(angle) { svgedit.select.Selector.prototype.updateGripCursors = function(angle) {
var dir_arr = []; var dir_arr = [];
var steps = Math.round(angle / 45); var steps = Math.round(angle / 45);
if(steps < 0) steps += 8; if (steps < 0) steps += 8;
for (var dir in selectorManager_.selectorGrips) { for (var dir in selectorManager_.selectorGrips) {
dir_arr.push(dir); dir_arr.push(dir);
} }
while(steps > 0) { while (steps > 0) {
dir_arr.push(dir_arr.shift()); dir_arr.push(dir_arr.shift());
steps--; steps--;
} }
@@ -114,7 +112,7 @@ svgedit.select.Selector.prototype.updateGripCursors = function(angle) {
for (var dir in selectorManager_.selectorGrips) { for (var dir in selectorManager_.selectorGrips) {
selectorManager_.selectorGrips[dir].setAttribute('style', ('cursor:' + dir_arr[i] + '-resize')); selectorManager_.selectorGrips[dir].setAttribute('style', ('cursor:' + dir_arr[i] + '-resize'));
i++; i++;
}; }
}; };
// Function: svgedit.select.Selector.showGrips // Function: svgedit.select.Selector.showGrips
@@ -128,7 +126,7 @@ svgedit.select.Selector.prototype.showGrips = function(show) {
selectorManager_.selectorGripsGroup.setAttribute('display', bShow); selectorManager_.selectorGripsGroup.setAttribute('display', bShow);
var elem = this.selectedElement; var elem = this.selectedElement;
this.hasGrips = show; this.hasGrips = show;
if(elem && show) { if (elem && show) {
this.selectorGroup.appendChild(selectorManager_.selectorGripsGroup); this.selectorGroup.appendChild(selectorManager_.selectorGripsGroup);
this.updateGripCursors(svgedit.utilities.getRotationAngle(elem)); this.updateGripCursors(svgedit.utilities.getRotationAngle(elem));
} }
@@ -163,32 +161,32 @@ svgedit.select.Selector.prototype.resize = function() {
m.f *= current_zoom; m.f *= current_zoom;
var bbox = svgedit.utilities.getBBox(selected); var bbox = svgedit.utilities.getBBox(selected);
if(tagName === 'g' && !$.data(selected, 'gsvg')) { if (tagName === 'g' && !$.data(selected, 'gsvg')) {
// The bbox for a group does not include stroke vals, so we // The bbox for a group does not include stroke vals, so we
// get the bbox based on its children. // get the bbox based on its children.
var stroked_bbox = svgFactory_.getStrokedBBox(selected.childNodes); var stroked_bbox = svgFactory_.getStrokedBBox(selected.childNodes);
if(stroked_bbox) { if (stroked_bbox) {
bbox = stroked_bbox; bbox = stroked_bbox;
} }
} }
// apply the transforms // apply the transforms
var l=bbox.x, t=bbox.y, w=bbox.width, h=bbox.height, var l = bbox.x, t = bbox.y, w = bbox.width, h = bbox.height,
bbox = {x:l, y:t, width:w, height:h}; bbox = {x:l, y:t, width:w, height:h};
// we need to handle temporary transforms too // we need to handle temporary transforms too
// if skewed, get its transformed box, then find its axis-aligned bbox // if skewed, get its transformed box, then find its axis-aligned bbox
//* //*
offset *= current_zoom; offset *= current_zoom;
var nbox = svgedit.math.transformBox(l*current_zoom, t*current_zoom, w*current_zoom, h*current_zoom, m), var nbox = svgedit.math.transformBox(l*current_zoom, t*current_zoom, w*current_zoom, h*current_zoom, m),
aabox = nbox.aabox, aabox = nbox.aabox,
nbax = aabox.x - offset, nbax = aabox.x - offset,
nbay = aabox.y - offset, nbay = aabox.y - offset,
nbaw = aabox.width + (offset * 2), nbaw = aabox.width + (offset * 2),
nbah = aabox.height + (offset * 2); nbah = aabox.height + (offset * 2);
// now if the shape is rotated, un-rotate it // now if the shape is rotated, un-rotate it
var cx = nbax + nbaw/2, var cx = nbax + nbaw/2,
cy = nbay + nbah/2; cy = nbay + nbah/2;
@@ -229,12 +227,12 @@ svgedit.select.Selector.prototype.resize = function() {
+ ' ' + (nbax+nbaw) + ',' + (nbay+nbah) + ' ' + (nbax+nbaw) + ',' + (nbay+nbah)
+ ' ' + nbax + ',' + (nbay+nbah) + 'z'; + ' ' + nbax + ',' + (nbay+nbah) + 'z';
selectedBox.setAttribute('d', dstr); selectedBox.setAttribute('d', dstr);
var xform = angle ? 'rotate(' + [angle,cx,cy].join(',') + ')' : ''; var xform = angle ? 'rotate(' + [angle,cx,cy].join(',') + ')' : '';
this.selectorGroup.setAttribute('transform', xform); this.selectorGroup.setAttribute('transform', xform);
// TODO(codedread): Is this if needed? // TODO(codedread): Is this if needed?
// if(selected === selectedElements[0]) { // if (selected === selectedElements[0]) {
this.gripCoords = { this.gripCoords = {
'nw': [nbax, nbay], 'nw': [nbax, nbay],
'ne': [nbax+nbaw, nbay], 'ne': [nbax+nbaw, nbay],
@@ -246,11 +244,11 @@ svgedit.select.Selector.prototype.resize = function() {
's': [nbax + (nbaw)/2, nbay + nbah] 's': [nbax + (nbaw)/2, nbay + nbah]
}; };
for(var dir in this.gripCoords) { for (var dir in this.gripCoords) {
var coords = this.gripCoords[dir]; var coords = this.gripCoords[dir];
selectedGrips[dir].setAttribute('cx', coords[0]); selectedGrips[dir].setAttribute('cx', coords[0]);
selectedGrips[dir].setAttribute('cy', coords[1]); selectedGrips[dir].setAttribute('cy', coords[1]);
}; }
// we want to go 20 pixels in the negative transformed y direction, ignoring scale // we want to go 20 pixels in the negative transformed y direction, ignoring scale
mgr.rotateGripConnector.setAttribute('x1', nbax + (nbaw)/2); mgr.rotateGripConnector.setAttribute('x1', nbax + (nbaw)/2);
@@ -258,7 +256,7 @@ svgedit.select.Selector.prototype.resize = function() {
mgr.rotateGripConnector.setAttribute('x2', nbax + (nbaw)/2); mgr.rotateGripConnector.setAttribute('x2', nbax + (nbaw)/2);
mgr.rotateGripConnector.setAttribute('y2', nbay - (gripRadius*5)); mgr.rotateGripConnector.setAttribute('y2', nbay - (gripRadius*5));
mgr.rotateGrip.setAttribute('cx', nbax + (nbaw)/2); mgr.rotateGrip.setAttribute('cx', nbax + (nbaw)/2);
mgr.rotateGrip.setAttribute('cy', nbay - (gripRadius*5)); mgr.rotateGrip.setAttribute('cy', nbay - (gripRadius*5));
// } // }
@@ -340,7 +338,7 @@ svgedit.select.SelectorManager.prototype.initGroup = function() {
'pointer-events': 'all' 'pointer-events': 'all'
} }
}); });
$.data(grip, 'dir', dir); $.data(grip, 'dir', dir);
$.data(grip, 'type', 'resize'); $.data(grip, 'type', 'resize');
this.selectorGrips[dir] = this.selectorGripsGroup.appendChild(grip); this.selectorGrips[dir] = this.selectorGripsGroup.appendChild(grip);
@@ -373,7 +371,7 @@ svgedit.select.SelectorManager.prototype.initGroup = function() {
); );
$.data(this.rotateGrip, 'type', 'rotate'); $.data(this.rotateGrip, 'type', 'rotate');
if($('#canvasBackground').length) return; if ($('#canvasBackground').length) return;
var dims = config_.dimensions; var dims = config_.dimensions;
var canvasbg = svgFactory_.createSVGElement({ var canvasbg = svgFactory_.createSVGElement({
@@ -439,7 +437,7 @@ svgedit.select.SelectorManager.prototype.requestSelector = function(elem) {
}; };
// Function: svgedit.select.SelectorManager.releaseSelector // Function: svgedit.select.SelectorManager.releaseSelector
// Removes the selector of the given element (hides selection box) // Removes the selector of the given element (hides selection box)
// //
// Parameters: // Parameters:
// elem - DOM element to remove the selector for // elem - DOM element to remove the selector for