--
-- Author: superyyl
-- Date: 2014-08-27 10:20:02
--
local TestScene = class("TestScene", function()
return display.newScene("TestScene")
end)
function TestScene:ctor()
self.layer = CCLayerColor:create(ccc4(255, 255, 255, 255))
self:addChild(self.layer)
self.pos = {}
self.points = {}
self.layer:registerScriptTouchHandler(handler(self,self.onTouch), false)
self.layer:setTouchEnabled(true)
local delete = CCControlButton:create(
CCLabelTTF:create("删除最后一个节点", "Thonburi", 24),
CCScale9Sprite:create("res/ui/btn_default_normal.png")
)
delete:setPreferredSize(CCSize(200, 80))
delete:setPosition(120,display.height-60)
delete:addHandleOfControlEvent(
handler(self,self.onDeleteClick),
CCControlEventTouchUpInside
)
self:addChild(delete)
end
function TestScene:onDeleteClick()
table.remove(self.pos,#self.pos)
self:refresh()
end
function TestScene:onTouch(e,x,y)
if e == "began" then
self.touchLocation = ccp(x,y)
self.dragPoint = nil
self.dragIndex = nil
for idx,point in pairs(self.points) do
if tool.checkIfTouch(point,x,y) then
self.dragPoint = point
self.dragIndex = idx
print("self.dragIndex",self.dragIndex)
end
end
return true
elseif e == "moved" then
if self.dragPoint then
local sprite = self.dragPoint
local pos = ccp(sprite:getPositionX()+x-self.touchLocation.x,sprite:getPositionY()+y-self.touchLocation.y)
self.dragPoint:setPosition(pos)
self.touchLocation = ccp(x,y)
end
elseif e == "ended" then
if self.dragPoint then
self.pos[self.dragIndex] = ccp(x,y)
self.dragPoint = nil
self.dragIndex = nil
self:refresh()
else
local pos = ccp(x,y)
local distance = ccpDistance(self.touchLocation, pos)
if distance < 25 then
self.pos[#self.pos+1] = pos
end
self:refresh()
end
end
end
function TestScene:refresh()
self.layer:removeAllChildrenWithCleanup(true)
self.points = {}
for i,pos in pairs(self.pos) do
local point = CCSprite:create("res/ui/point.png")
point:setPosition(pos)
self.layer:addChild(point)
local label = CCLabelTTF:create(tostring(i), "Thonburi", 20)
label:setColor(ccc3(0, 0, 0))
label:setPosition(point:getContentSize().width/2,point:getContentSize().height+10)
point:addChild(label)
self.points[#self.points+1] = point
end
if #self.pos > 1 then
self.general = require("app.models.General").new()
self.general:setPosition(self.pos[1])
self.layer:addChild(self.general)
self.general:startAction(self.pos, 4)
local controlPos = {}
for idx,pos in pairs(self.pos) do
local newPos = ccp(pos.x,pos.y)
controlPos[#controlPos+1] = newPos
end
local node = CCDrawNode:create()
self.layer:addChild(node)
local tracePoints = {}
for t=0,1,0.001 do
local point = bezieerat(controlPos, t)
node:drawDot(point, 2, ccc4f(0, 255, 0, 128))
tracePoints[#tracePoints+1] = {}
tracePoints[#tracePoints].point = point
tracePoints[#tracePoints].percent = t
end
local distance = 0
for i=1,#tracePoints-1,1 do
local p1 = tracePoints[i]
local p2 = tracePoints[i+1]
local d = ccpDistance(p1.point, p2.point)
distance = distance + d
end
local len = 30
local pointInfo = {}
local count = tool.toint(distance / len)
local mod = distance % len
len = len + mod / count
pointInfo[1] = tracePoints[1]
local remain = len
for i=1,#tracePoints-1,1 do
local p1 = tracePoints[i]
local p2 = tracePoints[i+1]
local d = ccpDistance(p1.point, p2.point)
remain = remain - d
if remain <= 0 then
pointInfo[#pointInfo+1] = p2
remain = remain + len
end
end
for idx,p in pairs(pointInfo) do
local point = CCSprite:create("res/ui/trace.png")
point:setPosition(p.point)
print("percent:"..p.percent)
self.layer:addChild(point)
end
print(string.format("长度:%s#%s#%s#%s",distance,count,mod,len))
end
end
return TestScene