Skip to content
Permalink
Browse files

Fix to retrieve grid settings correctly (#6)

* Fix to retrieve grid settings correctly

* Rename to #adjustFrameAfterEditIntegral

* Fix typo

* New feature "Snap to Pixel Grid"
  • Loading branch information...
uetchy committed Jun 7, 2016
1 parent c08279a commit fdacce19be80dc82d500f47a149aa21842450002
@@ -1,22 +1,27 @@
{
"name": "StickyGrid",
"commands": [
{
"script": "script.cocoascript",
"handler": "snapToGrid",
"shortcut": "ctrl cmd g",
"name": "Snap to Grid",
"identifier": "snaptogrid"
}
],
"commands": [{
"script": "script.cocoascript",
"handler": "snapToGrid",
"shortcut": "ctrl cmd g",
"name": "Snap to Grid",
"identifier": "snaptogrid"
}, {
"script": "script.cocoascript",
"handler": "snapToPixelGrid",
"shortcut": "ctrl cmd x",
"name": "Snap to Pixel Grid",
"identifier": "snaptopixelgrid"
}],
"menu": {
"items": [
"snaptogrid"
"snaptogrid",
"snaptopixelgrid"
],
"title": "StickyGrid"
},
"identifier": "co.randompaper.sketch.stickygrid",
"version": "2.0.0",
"version": "3.0.0",
"description": "Sketch plugin to make paths be snapped to grid.",
"author": "Yasuaki Uechi",
"authorEmail": "uetchy@randompaper.co",
@@ -2,24 +2,21 @@ function computePosition(pos, intval) {
return Math.round(pos / intval) * intval;
}

function adjustPointsTo(shapeLayer, gridInterval) {
function adjustPointsOf(shapeLayer, gridInterval) {
var path = shapeLayer.path();
var pointsCount = path.numberOfPoints();
var artboardFrame = shapeLayer.frameInArtboard();

for (var j=0; j < pointsCount; j++) {
for (var j = 0; j < pointsCount; j++) {
var point = path.pointAtIndex(j);

var absolutePoint = shapeLayer.absolutePoint(point.point());

var relX = computePosition(
artboardFrame.origin.x + absolutePoint.x,
gridInterval) - (artboardFrame.origin.x + absolutePoint.x
);
var absX = artboardFrame.origin.x + absolutePoint.x;
var relX = computePosition(absX, gridInterval) - absX;

var relY = computePosition(
artboardFrame.origin.y + absolutePoint.y,
gridInterval) - (artboardFrame.origin.y + absolutePoint.y);
var absY = artboardFrame.origin.y + absolutePoint.y;
var relY = computePosition(absY, gridInterval) - absY;

var cgPoint = shapeLayer.relativePoint(
CGPointMake(
@@ -30,33 +27,41 @@ function adjustPointsTo(shapeLayer, gridInterval) {
point.movePointTo(cgPoint);
}

shapeLayer.adjustFrameAfterEdit();
shapeLayer.adjustFrameAfterEditIntegral(false);
}

function snapToGrid(context) {
var app = [NSApplication sharedApplication];
var doc = context.document;
var selection = context.selection;
var gridInterval = doc.grid().gridSize();

function adjustSelection(selection, gridInterval) {
for (var i=0; i < [selection count]; i++) {
var object = [selection objectAtIndex: i];

if ([object isKindOfClass:[MSShapePathLayer class]]) {
// MSShapeGroup
adjustPointsTo(object, gridInterval);
adjustPointsOf(object, gridInterval);
} else if ([object isMemberOfClass:[MSLayerGroup class]]) {
// MSLayerGroup
for (var l=0; l < [[object layers] count]; l++) {
var shapeLayerGroup = [[object layers] objectAtIndex:l];

for (var l2=0; l2 < [[shapeLayerGroup layers] count]; l2++)
adjustPointsTo([[shapeLayerGroup layers] objectAtIndex:l2], gridInterval);
adjustPointsOf([[shapeLayerGroup layers] objectAtIndex:l2], gridInterval);
}
} else {
// MSShapePathLayer
for (var l=0; l < [[object layers] count]; l++)
adjustPointsTo([[object layers] objectAtIndex:l], gridInterval);
adjustPointsOf([[object layers] objectAtIndex:l], gridInterval);
}
}
}

function snapToGrid(context) {
var selection = context.selection;
var gridInterval = MSDefaultGrid.defaultGrid().gridSize();

adjustSelection(selection, gridInterval);
}

function snapToPixelGrid(context) {
var selection = context.selection;

adjustSelection(selection, 1.0);
}

0 comments on commit fdacce1

Please sign in to comment.
You can’t perform that action at this time.