--Connector part 1/2
LINE = 0
ARROW_NONE = 1
ARROW_START = 2
ARROW_END = 3
ARROW_BOTH = 4
LINE_STRAIGHT = 1
LINE_CURVE = 2
Connector = class()
function Connector:init(s1,p1,s2,p2)
-- you can accept and set parameters here
self.shape1 = s1
self.point1 = p1
self.shape2 = s2
self.point2 = p2
self.type = LINE
self.lineType = LINE_CURVE
self.color = color(0, 0, 255, 255)
self.strokeWidth = 5
self.arrowType = ARROW_END
self.points = {}
self.mode = NORMAL
World():add(self)
end
function Connector:draw()
-- Codea does not automatically call this method
local shape1 = self.shape1
local shape2 = self.shape2
local point1 = self.point1
local point2 = self.point2
local x1,y1,x2,y2
x1,y1 = self:getPoint(shape1,point1)
local p1 = vec2(x1,y1)
local lowdist = 0
for i = 1,4 do
x2,y2 = self:getPoint(shape2,i)
dist = p1:dist(vec2(x2,y2))
if dist<lowdist or lowdist==0 then
lowdist = dist
point2 = i
end
end
x2,y2 = self:getPoint(shape2,point2)
local p2 = vec2(x2,y2)
local lowdist = 0
for i = 1,4 do
x1,y1 = self:getPoint(shape1,i)
dist = p2:dist(vec2(x1,y1))
if dist<lowdist or lowdist==0 then
lowdist = dist
point1 = i
end
end
x1,y1 = self:getPoint(shape1,point1)
pushStyle()
strokeWidth(self.strokeWidth)
stroke(self.color)
--Calculate points
if (point1 == 1 or point1 == 2) and (point2 == 1 or point2 == 2) then
local xm = math.floor((x2-x1)/2)
self.points = {}
if math.abs(y1-y2) <5 then
self.points[1] = vec2(x1,y1)
self.points[2] = vec2(x2,y2)
else
self.points[1] = vec2(x1,y1)
self.points[2] = vec2(x1+xm,y1)
self.points[3] = vec2(x1+xm,y2)
self.points[4] = vec2(x2,y2)
end
elseif (point1 == 3 or point1 == 4) and (point2 == 3 or point2 == 4) then
local ym = math.floor((y2-y1)/2)
self.points = {}
if math.abs(x1-x2)< 5 then
self.points[1] = vec2(x1,y1)
self.points[2] = vec2(x2,y2)
else
self.points[1] = vec2(x1,y1)
self.points[2] = vec2(x1,y1+ym)
self.points[3] = vec2(x2,y1+ym)
self.points[4] = vec2(x2,y2)
end
elseif (point1 == 1 or point1 == 2) and (point2 == 3 or point2 == 4) then
self.points = {}
self.points[1] = vec2(x1,y1)
self.points[2] = vec2(x2,y1)
self.points[3] = vec2(x2,y2)
elseif (point1 == 3 or point1 == 4) and (point2 == 1 or point2 == 2) then
self.points = {}
self.points[1] = vec2(x1,y1)
self.points[2] = vec2(x1,y2)
self.points[3] = vec2(x2,y2)
end
--draw line
if self.lineType == LINE_STRAIGHT then
for j = 1,#self.points-1 do
line(self.points[j].x,self.points[j].y,self.points[j+1].x,self.points[j+1].y)
end
else
self:Spline()
end
--draw arrow
if (self.arrowType == ARROW_START or self.arrowType == ARROW_BOTH)
and point1 == 1 then
line(x1,y1,x1-20,y1+10)
line(x1,y1,x1-20,y1-10)
end
if (self.arrowType == ARROW_START or self.arrowType == ARROW_BOTH)
and point1 == 2 then
line(x1,y1,x1+20,y1+10)
line(x1,y1,x1+20,y1-10)
end
if (self.arrowType == ARROW_START or self.arrowType == ARROW_BOTH)
and point1 == 3 then
line(x1-10,y1-20,x1,y1)
line(x1+10,y1-20,x1,y1)
end
if (self.arrowType == ARROW_START or self.arrowType == ARROW_BOTH)
and point1 == 4 then
line(x1-10,y1+20,x1,y1)
line(x1+10,y1+20,x1,y1)
end
if (self.arrowType == ARROW_END or self.arrowType == ARROW_BOTH)
and point2 == 1 then
line(x2-20,y2-10,x2,y2)
line(x2-20,y2+10,x2,y2)
end
if (self.arrowType == ARROW_END or self.arrowType == ARROW_BOTH)
and point2 == 2 then
line(x2,y2,x2+20,y2+10)
line(x2,y2,x2+20,y2-10)
end
if (self.arrowType == ARROW_END or self.arrowType == ARROW_BOTH)
and point2 == 3 then
line(x2-10,y2-20,x2,y2)
line(x2+10,y2-20,x2,y2)
end
if (self.arrowType == ARROW_END or self.arrowType == ARROW_BOTH)
and point2 == 4 then
line(x2-10,y2+20,x2,y2)
line(x2+10,y2+20,x2,y2)
end
if self.mode == EDIT then
pushStyle()
strokeWidth(5)
stroke(127, 127, 127, 255)
fill(127,127,127, 255)
rectMode(CENTER)
rect(self.points[1].x,self.points[1].y,10,10)
rect(self.points[#self.points].x,self.points[#self.points].y,10,10)
popStyle()
end
popStyle()
end