diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/LICENSE b/LICENSE index 8c59fc9..e640d68 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Yasuaki Uechi +Copyright (c) 2015 Yasuaki Uechi http://randompaper.co Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 7bc76a7..2a7d50d 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,19 @@ Sketch plugin to make paths be snapped to grid. -![](https://raw.githubusercontent.com/uetchy/Sketch-StickyGrid/master/assets/readme_images/stickygrid.gif) +![](https://raw.githubusercontent.com/uetchy/Sketch-StickyGrid/master/Contents/Resources/readme_images/stickygrid.gif) ## Installation 1. [Download the plugin](https://github.com/uetchy/Sketch-StickyGrid/archive/master.zip) -2. Unzip the archive -3. Place the folder into your Sketch Plugins folder. +2. Unzip the archive and double-click `StickyGrid.sketchplugin` + +If you prefer CLI-way: + +``` +$ cd $HOME/Library/Application Support/com.bohemiancoding.sketch3/Plugins +$ git clone https://github.com/uetchy/Sketch-StickyGrid.git +``` ## Usage diff --git a/Snap to Grid.sketchplugin b/Snap to Grid.sketchplugin deleted file mode 100644 index 00448ba..0000000 --- a/Snap to Grid.sketchplugin +++ /dev/null @@ -1,63 +0,0 @@ -// Snap to Grid (ctrl cmd g) - -(function() { - - var app = [NSApplication sharedApplication]; - var grid_interval = doc.grid().gridSize(); - - var compute_position = function(pos, intval) { - return Math.round(pos / intval) * intval; - }; - - var adjust_points = function(shape_layer) { - var path = shape_layer.path(); - var points_count = path.numberOfPoints(); - var artboard_frame = shape_layer.frameInArtboard() - - for (var j=0; j < points_count; j++) { - var point = path.pointAtIndex(j); - - var abs_point = shape_layer.absolutePoint(point.point()); - - var rel_x = compute_position( - artboard_frame.x() + abs_point.x, - grid_interval) - (artboard_frame.x() + abs_point.x); - - var rel_y = compute_position( - artboard_frame.y() + abs_point.y, - grid_interval) - (artboard_frame.y() + abs_point.y); - - var cg_point = shape_layer.relativePoint( - CGPointMake( - abs_point.x + rel_x, - abs_point.y + rel_y - )); - - point.movePointTo(cg_point); - } - - shape_layer.adjustFrameAfterEdit(); - } - - for (var i=0; i < [selection count]; i++) { - var object = [selection objectAtIndex: i]; - - if ([object isKindOfClass:[MSShapePathLayer class]]) { - // MSShapeGroup - adjust_points(object); - } else if ([object isMemberOfClass:[MSLayerGroup class]]) { - // MSLayerGroup - for (var l=0; l < [[object layers] count]; l++) { - var shape_layer_group = [[object layers] objectAtIndex:l]; - - for (var l2=0; l2 < [[shape_layer_group layers] count]; l2++) - adjust_points([[shape_layer_group layers] objectAtIndex:l2]); - } - } else { - // MSShapePathLayer - for (var l=0; l < [[object layers] count]; l++) - adjust_points([[object layers] objectAtIndex:l]); - } - } - -})(); diff --git a/StickyGrid.sketchplugin/Contents/Sketch/manifest.json b/StickyGrid.sketchplugin/Contents/Sketch/manifest.json new file mode 100644 index 0000000..db9056b --- /dev/null +++ b/StickyGrid.sketchplugin/Contents/Sketch/manifest.json @@ -0,0 +1,28 @@ +{ + "name": "StickyGrid", + "commands": [ + { + "script": "script.cocoascript", + "handler": "snapToGrid", + "shortcut": "ctrl cmd g", + "name": "Snap to Grid", + "identifier": "snaptogrid" + } + ], + "menu": { + "items": [ + "snaptogrid" + ], + "title": "StickyGrid" + }, + "identifier": "co.randompaper.sketch.stickygrid", + "version": "2.0.0", + "description": "Sketch plugin to make paths be snapped to grid.", + "author": "Yasuaki Uechi", + "authorEmail": "uetchy@randompaper.co", + "license": "MIT", + "homepage": "https://github.com/uetchy/Sketch-StickyGrid", + "keywords": [ + "grid" + ] +} diff --git a/StickyGrid.sketchplugin/Contents/Sketch/script.cocoascript b/StickyGrid.sketchplugin/Contents/Sketch/script.cocoascript new file mode 100644 index 0000000..f7fc04b --- /dev/null +++ b/StickyGrid.sketchplugin/Contents/Sketch/script.cocoascript @@ -0,0 +1,62 @@ +function computePosition(pos, intval) { + return Math.round(pos / intval) * intval; +} + +function adjustPointsTo(shapeLayer, gridInterval) { + var path = shapeLayer.path(); + var pointsCount = path.numberOfPoints(); + var artboardFrame = shapeLayer.frameInArtboard(); + + 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 relY = computePosition( + artboardFrame.origin.y + absolutePoint.y, + gridInterval) - (artboardFrame.origin.y + absolutePoint.y); + + var cgPoint = shapeLayer.relativePoint( + CGPointMake( + absolutePoint.x + relX, + absolutePoint.y + relY + )); + + point.movePointTo(cgPoint); + } + + shapeLayer.adjustFrameAfterEdit(); +} + +function snapToGrid(context) { + var app = [NSApplication sharedApplication]; + var doc = context.document; + var selection = context.selection; + var gridInterval = doc.grid().gridSize(); + + for (var i=0; i < [selection count]; i++) { + var object = [selection objectAtIndex: i]; + + if ([object isKindOfClass:[MSShapePathLayer class]]) { + // MSShapeGroup + adjustPointsTo(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); + } + } else { + // MSShapePathLayer + for (var l=0; l < [[object layers] count]; l++) + adjustPointsTo([[object layers] objectAtIndex:l], gridInterval); + } + } +} diff --git a/assets/readme_images/stickygrid.gif b/assets/readme_images/stickygrid.gif deleted file mode 100644 index db1facf..0000000 Binary files a/assets/readme_images/stickygrid.gif and /dev/null differ diff --git a/assets/readme_images/stickygrid_2.gif b/assets/readme_images/stickygrid_2.gif deleted file mode 100644 index 22ff8a1..0000000 Binary files a/assets/readme_images/stickygrid_2.gif and /dev/null differ diff --git a/chest.json b/chest.json deleted file mode 100644 index 316f5ed..0000000 --- a/chest.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "StickyGrid", - "version": "1.0.0", - "description": "Sketch plugin to make paths be snapped to grid.", - "keywords": [ - "grid" - ], - "authors": [ - "Yasuaki Uechi " - ], - "license": "MIT", - "homepage": "https://github.com/uetchy/Sketch-StickyGrid" -}