Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 7, 2015
0 parents commit 5575a31
Show file tree
Hide file tree
Showing 14 changed files with 1,235 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/www/fsm.js
31 changes: 31 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python

import os, time, sys

def sources():
path = './src/'
return [os.path.join(base, f) for base, folders, files in os.walk(path) for f in files if f.endswith('.js')]

def build():
path = './www/fsm.js'
data = '\n'.join(open(file, 'r').read() for file in sources())
with open(path, 'w') as f:
f.write(data)
print 'built %s (%u bytes)' % (path, len(data))

def stat():
return [os.stat(file).st_mtime for file in sources()]

def monitor():
a = stat()
while True:
time.sleep(0.5)
b = stat()
if a != b:
a = b
build()

if __name__ == '__main__':
build()
if '--watch' in sys.argv:
monitor()
27 changes: 27 additions & 0 deletions src/_license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Finite State Machine Designer (http://madebyevan.com/fsm/)
License: MIT License (see below)
Copyright (c) 2010 Evan Wallace
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:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
145 changes: 145 additions & 0 deletions src/elements/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
function Link(a, b) {
this.nodeA = a;
this.nodeB = b;
this.text = '';
this.lineAngleAdjust = 0; // value to add to textAngle when link is straight line

// make anchor point relative to the locations of nodeA and nodeB
this.parallelPart = 0.5; // percentage from nodeA to nodeB
this.perpendicularPart = 0; // pixels from line between nodeA and nodeB
}

Link.prototype.getAnchorPoint = function() {
var dx = this.nodeB.x - this.nodeA.x;
var dy = this.nodeB.y - this.nodeA.y;
var scale = Math.sqrt(dx * dx + dy * dy);
return {
'x': this.nodeA.x + dx * this.parallelPart - dy * this.perpendicularPart / scale,
'y': this.nodeA.y + dy * this.parallelPart + dx * this.perpendicularPart / scale
};
};

Link.prototype.setAnchorPoint = function(x, y) {
var dx = this.nodeB.x - this.nodeA.x;
var dy = this.nodeB.y - this.nodeA.y;
var scale = Math.sqrt(dx * dx + dy * dy);
this.parallelPart = (dx * (x - this.nodeA.x) + dy * (y - this.nodeA.y)) / (scale * scale);
this.perpendicularPart = (dx * (y - this.nodeA.y) - dy * (x - this.nodeA.x)) / scale;
// snap to a straight line
if(this.parallelPart > 0 && this.parallelPart < 1 && Math.abs(this.perpendicularPart) < snapToPadding) {
this.lineAngleAdjust = (this.perpendicularPart < 0) * Math.PI;
this.perpendicularPart = 0;
}
};

Link.prototype.getEndPointsAndCircle = function() {
if(this.perpendicularPart == 0) {
var midX = (this.nodeA.x + this.nodeB.x) / 2;
var midY = (this.nodeA.y + this.nodeB.y) / 2;
var start = this.nodeA.closestPointOnCircle(midX, midY);
var end = this.nodeB.closestPointOnCircle(midX, midY);
return {
'hasCircle': false,
'startX': start.x,
'startY': start.y,
'endX': end.x,
'endY': end.y,
};
}
var anchor = this.getAnchorPoint();
var circle = circleFromThreePoints(this.nodeA.x, this.nodeA.y, this.nodeB.x, this.nodeB.y, anchor.x, anchor.y);
var isReversed = (this.perpendicularPart > 0);
var reverseScale = isReversed ? 1 : -1;
var startAngle = Math.atan2(this.nodeA.y - circle.y, this.nodeA.x - circle.x) - reverseScale * nodeRadius / circle.radius;
var endAngle = Math.atan2(this.nodeB.y - circle.y, this.nodeB.x - circle.x) + reverseScale * nodeRadius / circle.radius;
var startX = circle.x + circle.radius * Math.cos(startAngle);
var startY = circle.y + circle.radius * Math.sin(startAngle);
var endX = circle.x + circle.radius * Math.cos(endAngle);
var endY = circle.y + circle.radius * Math.sin(endAngle);
return {
'hasCircle': true,
'startX': startX,
'startY': startY,
'endX': endX,
'endY': endY,
'startAngle': startAngle,
'endAngle': endAngle,
'circleX': circle.x,
'circleY': circle.y,
'circleRadius': circle.radius,
'reverseScale': reverseScale,
'isReversed': isReversed,
};
};

Link.prototype.draw = function(c) {
var stuff = this.getEndPointsAndCircle();
// draw arc
c.beginPath();
if(stuff.hasCircle) {
c.arc(stuff.circleX, stuff.circleY, stuff.circleRadius, stuff.startAngle, stuff.endAngle, stuff.isReversed);
} else {
c.moveTo(stuff.startX, stuff.startY);
c.lineTo(stuff.endX, stuff.endY);
}
c.stroke();
// draw the head of the arrow
if(stuff.hasCircle) {
drawArrow(c, stuff.endX, stuff.endY, stuff.endAngle - stuff.reverseScale * (Math.PI / 2));
} else {
drawArrow(c, stuff.endX, stuff.endY, Math.atan2(stuff.endY - stuff.startY, stuff.endX - stuff.startX));
}
// draw the text
if(stuff.hasCircle) {
var startAngle = stuff.startAngle;
var endAngle = stuff.endAngle;
if(endAngle < startAngle) {
endAngle += Math.PI * 2;
}
var textAngle = (startAngle + endAngle) / 2 + stuff.isReversed * Math.PI;
var textX = stuff.circleX + stuff.circleRadius * Math.cos(textAngle);
var textY = stuff.circleY + stuff.circleRadius * Math.sin(textAngle);
drawText(c, this.text, textX, textY, textAngle, selectedObject == this);
} else {
var textX = (stuff.startX + stuff.endX) / 2;
var textY = (stuff.startY + stuff.endY) / 2;
var textAngle = Math.atan2(stuff.endX - stuff.startX, stuff.startY - stuff.endY);
drawText(c, this.text, textX, textY, textAngle + this.lineAngleAdjust, selectedObject == this);
}
};

Link.prototype.containsPoint = function(x, y) {
var stuff = this.getEndPointsAndCircle();
if(stuff.hasCircle) {
var dx = x - stuff.circleX;
var dy = y - stuff.circleY;
var distance = Math.sqrt(dx*dx + dy*dy) - stuff.circleRadius;
if(Math.abs(distance) < hitTargetPadding) {
var angle = Math.atan2(dy, dx);
var startAngle = stuff.startAngle;
var endAngle = stuff.endAngle;
if(stuff.isReversed) {
var temp = startAngle;
startAngle = endAngle;
endAngle = temp;
}
if(endAngle < startAngle) {
endAngle += Math.PI * 2;
}
if(angle < startAngle) {
angle += Math.PI * 2;
} else if(angle > endAngle) {
angle -= Math.PI * 2;
}
return (angle > startAngle && angle < endAngle);
}
} else {
var dx = stuff.endX - stuff.startX;
var dy = stuff.endY - stuff.startY;
var length = Math.sqrt(dx*dx + dy*dy);
var percent = (dx * (x - stuff.startX) + dy * (y - stuff.startY)) / (length * length);
var distance = (dx * (y - stuff.startY) - dy * (x - stuff.startX)) / length;
return (percent > 0 && percent < 1 && Math.abs(distance) < hitTargetPadding);
}
return false;
};
49 changes: 49 additions & 0 deletions src/elements/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Node(x, y) {
this.x = x;
this.y = y;
this.mouseOffsetX = 0;
this.mouseOffsetY = 0;
this.isAcceptState = false;
this.text = '';
}

Node.prototype.setMouseStart = function(x, y) {
this.mouseOffsetX = this.x - x;
this.mouseOffsetY = this.y - y;
};

Node.prototype.setAnchorPoint = function(x, y) {
this.x = x + this.mouseOffsetX;
this.y = y + this.mouseOffsetY;
};

Node.prototype.draw = function(c) {
// draw the circle
c.beginPath();
c.arc(this.x, this.y, nodeRadius, 0, 2 * Math.PI, false);
c.stroke();

// draw the text
drawText(c, this.text, this.x, this.y, null, selectedObject == this);

// draw a double circle for an accept state
if(this.isAcceptState) {
c.beginPath();
c.arc(this.x, this.y, nodeRadius - 6, 0, 2 * Math.PI, false);
c.stroke();
}
};

Node.prototype.closestPointOnCircle = function(x, y) {
var dx = x - this.x;
var dy = y - this.y;
var scale = Math.sqrt(dx * dx + dy * dy);
return {
'x': this.x + dx * nodeRadius / scale,
'y': this.y + dy * nodeRadius / scale,
};
};

Node.prototype.containsPoint = function(x, y) {
return (x - this.x)*(x - this.x) + (y - this.y)*(y - this.y) < nodeRadius*nodeRadius;
};
70 changes: 70 additions & 0 deletions src/elements/self_link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function SelfLink(node, mouse) {
this.node = node;
this.anchorAngle = 0;
this.mouseOffsetAngle = 0;
this.text = '';

if(mouse) {
this.setAnchorPoint(mouse.x, mouse.y);
}
}

SelfLink.prototype.setMouseStart = function(x, y) {
this.mouseOffsetAngle = this.anchorAngle - Math.atan2(y - this.node.y, x - this.node.x);
};

SelfLink.prototype.setAnchorPoint = function(x, y) {
this.anchorAngle = Math.atan2(y - this.node.y, x - this.node.x) + this.mouseOffsetAngle;
// snap to 90 degrees
var snap = Math.round(this.anchorAngle / (Math.PI / 2)) * (Math.PI / 2);
if(Math.abs(this.anchorAngle - snap) < 0.1) this.anchorAngle = snap;
// keep in the range -pi to pi so our containsPoint() function always works
if(this.anchorAngle < -Math.PI) this.anchorAngle += 2 * Math.PI;
if(this.anchorAngle > Math.PI) this.anchorAngle -= 2 * Math.PI;
};

SelfLink.prototype.getEndPointsAndCircle = function() {
var circleX = this.node.x + 1.5 * nodeRadius * Math.cos(this.anchorAngle);
var circleY = this.node.y + 1.5 * nodeRadius * Math.sin(this.anchorAngle);
var circleRadius = 0.75 * nodeRadius;
var startAngle = this.anchorAngle - Math.PI * 0.8;
var endAngle = this.anchorAngle + Math.PI * 0.8;
var startX = circleX + circleRadius * Math.cos(startAngle);
var startY = circleY + circleRadius * Math.sin(startAngle);
var endX = circleX + circleRadius * Math.cos(endAngle);
var endY = circleY + circleRadius * Math.sin(endAngle);
return {
'hasCircle': true,
'startX': startX,
'startY': startY,
'endX': endX,
'endY': endY,
'startAngle': startAngle,
'endAngle': endAngle,
'circleX': circleX,
'circleY': circleY,
'circleRadius': circleRadius
};
};

SelfLink.prototype.draw = function(c) {
var stuff = this.getEndPointsAndCircle();
// draw arc
c.beginPath();
c.arc(stuff.circleX, stuff.circleY, stuff.circleRadius, stuff.startAngle, stuff.endAngle, false);
c.stroke();
// draw the text on the loop farthest from the node
var textX = stuff.circleX + stuff.circleRadius * Math.cos(this.anchorAngle);
var textY = stuff.circleY + stuff.circleRadius * Math.sin(this.anchorAngle);
drawText(c, this.text, textX, textY, this.anchorAngle, selectedObject == this);
// draw the head of the arrow
drawArrow(c, stuff.endX, stuff.endY, stuff.endAngle + Math.PI * 0.4);
};

SelfLink.prototype.containsPoint = function(x, y) {
var stuff = this.getEndPointsAndCircle();
var dx = x - stuff.circleX;
var dy = y - stuff.circleY;
var distance = Math.sqrt(dx*dx + dy*dy) - stuff.circleRadius;
return (Math.abs(distance) < hitTargetPadding);
};
62 changes: 62 additions & 0 deletions src/elements/start_link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function StartLink(node, start) {
this.node = node;
this.deltaX = 0;
this.deltaY = 0;
this.text = '';

if(start) {
this.setAnchorPoint(start.x, start.y);
}
}

StartLink.prototype.setAnchorPoint = function(x, y) {
this.deltaX = x - this.node.x;
this.deltaY = y - this.node.y;

if(Math.abs(this.deltaX) < snapToPadding) {
this.deltaX = 0;
}

if(Math.abs(this.deltaY) < snapToPadding) {
this.deltaY = 0;
}
};

StartLink.prototype.getEndPoints = function() {
var startX = this.node.x + this.deltaX;
var startY = this.node.y + this.deltaY;
var end = this.node.closestPointOnCircle(startX, startY);
return {
'startX': startX,
'startY': startY,
'endX': end.x,
'endY': end.y,
};
};

StartLink.prototype.draw = function(c) {
var stuff = this.getEndPoints();

// draw the line
c.beginPath();
c.moveTo(stuff.startX, stuff.startY);
c.lineTo(stuff.endX, stuff.endY);
c.stroke();

// draw the text at the end without the arrow
var textAngle = Math.atan2(stuff.startY - stuff.endY, stuff.startX - stuff.endX);
drawText(c, this.text, stuff.startX, stuff.startY, textAngle, selectedObject == this);

// draw the head of the arrow
drawArrow(c, stuff.endX, stuff.endY, Math.atan2(-this.deltaY, -this.deltaX));
};

StartLink.prototype.containsPoint = function(x, y) {
var stuff = this.getEndPoints();
var dx = stuff.endX - stuff.startX;
var dy = stuff.endY - stuff.startY;
var length = Math.sqrt(dx*dx + dy*dy);
var percent = (dx * (x - stuff.startX) + dy * (y - stuff.startY)) / (length * length);
var distance = (dx * (y - stuff.startY) - dy * (x - stuff.startX)) / length;
return (percent > 0 && percent < 1 && Math.abs(distance) < hitTargetPadding);
};
Loading

0 comments on commit 5575a31

Please sign in to comment.