Advertisement
podoko_Lua

Joint drawing [v0.99]

Jun 8th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 KB | None | 0 0
  1.  
  2. --[[ Joint drawing [v0.99]
  3.        
  4.         !color ff00ff           => changer la couleur (mettez un code hexadecimal)
  5.         !alpha xx               => changer la transparence de ses joints (entre 0 et 1)
  6.         !line xx                => Changer l'épaisseur de ses joints
  7.         !bezier xx              => Changer le nombre de joints pour former un bezier
  8.        
  9.  
  10. ]]--
  11.  
  12.  
  13. interface = {}
  14. interface.joint = {}
  15.  
  16. xml = {}
  17. xml.joint = {}
  18.  
  19. editor = {}
  20. editor.joint = {}
  21.  
  22. tool = {}
  23.  
  24.  
  25.  
  26.  
  27. function init ()
  28.    
  29.     info = {}
  30.     for name in pairs(tfm.get.room.playerList) do eventNewPlayer(name) end
  31.     eventNewGame()
  32.    
  33. end
  34.  
  35.  
  36.  
  37. function interface.joint.affiche (name)
  38.    
  39.     ui.addTextArea( 0, "<b>type de joint : <a href='event:joint type •'>•</a> <a href='event:joint type /'>/</a> <a href='event:joint type ~'>~</a></b>", name, 0, -30, 150, 20, 0x324650, 0x324650, 0.8, true)
  40.     ui.addTextArea( 1, "<b>taille : "..tostring(info[name].prop.line).."</b>", name, 160, -30, 80, 20, 0x324650, 0x324650, 0.8, true)
  41.     ui.addTextArea( 2, "<b>couleur : <font color='#"..string.format('%x', info[name].prop.color).."'>•</font></b>", name, 250, -30, 80, 20, 0x324650, 0x324650, 0.8, true)
  42.     ui.addTextArea( 3, "<b>alpha : "..tostring(info[name].prop.alpha).."</b>", name, 340, -30, 80, 20, 0x324650, 0x324650, 0.8, true)
  43.     ui.addTextArea( 4, "<b>bezier : "..tostring(info[name].bezier).."</b>", name, 430, -30, 80, 20, 0x324650, 0x324650, 0.8, true)
  44.    
  45. end
  46.  
  47.  
  48.  
  49. function eventNewPlayer(name)
  50.    
  51.     system.bindMouse(name, true)
  52.     tfm.exec.bindKeyboard(name, 8, true, true)
  53.     info[name] = {joint="/", point={}, bezier=10, prop = { color=0xffffff, alpha=0.8, line=10, foreground=true}}
  54.    
  55.     interface.joint.affiche (name)
  56.    
  57. end
  58.  
  59. function eventNewGame ()
  60.    
  61.     joint = {}
  62.     tfm.exec.addPhysicObject(1, -500, -500, {['type']=1})
  63.    
  64. end
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. function eventKeyboard(name, key, down, x, y)
  72.     if key == 8 then editor.joint.delete(#xml.joint) end
  73. end
  74.  
  75. function eventChatCommand(name, command)
  76.    
  77.     if command:sub(0,5) == "color" then
  78.         info[name].prop.color = tonumber('0x'..command:sub(7))
  79.         ui.updateTextArea(2, "<b>couleur : <font color='#"..string.format('%x', info[name].prop.color).."'>•</font></b>", name)
  80.     elseif command:sub(0,4) == "line" then
  81.         info[name].prop.line = tonumber(command:sub(6))
  82.         ui.updateTextArea(1, "<b>taille : "..tostring(info[name].prop.line).."</b>", name)
  83.     elseif command:sub(0,5) == "alpha" then
  84.         info[name].prop.alpha = tonumber(command:sub(7))
  85.         ui.updateTextArea(3, "<b>alpha : "..tostring(info[name].prop.alpha).."</b>", name)
  86.     elseif command:sub(0,6) == "bezier" then
  87.         local n = math.abs(tonumber(command:sub(8)))
  88.         info[name].bezier = n>0 and n or 10
  89.         ui.updateTextArea(4, "<b>bezier : "..tostring(info[name].bezier).."</b>", name)
  90.     end
  91. end
  92.  
  93. function eventTextAreaCallback (id, name, call)
  94.    
  95.     local section = {}
  96.     for w in string.gmatch(call, "%S+") do
  97.         table.insert(section, w)
  98.        
  99.     end
  100.    
  101.     if section[1] == "joint" then
  102.         if section[2] == "type" then
  103.             info[name].joint = section[3]
  104.             tool.reset_point(name)
  105.         end
  106.     end
  107.    
  108. end
  109.  
  110. function eventMouse (name, x, y)
  111.    
  112.     if info[name].joint == '•' then
  113.         local prop = info[name].prop
  114.         prop.point1 = tool.s_coord(x,y)
  115.         prop.point2 = tool.s_coord(x+1,y)
  116.         editor.joint.add(prop)
  117.         tool.reset_point(name)
  118.        
  119.     elseif info[name].point[1] then
  120.         if info[name].joint == '/' then
  121.             local prop = info[name].prop
  122.             prop.point1 = tool.s_coord(info[name].point[1].x, info[name].point[1].y)
  123.             prop.point2 = tool.s_coord(x,y)
  124.             editor.joint.add(prop)
  125.             tool.reset_point(name)
  126.            
  127.         elseif info[name].point[2] then
  128.             if info[name].point[3] then
  129.                 editor.joint.bezier (info[name].point[1].x, info[name].point[1].y , info[name].point[2].x, info[name].point[2].y , info[name].point[3].x, info[name].point[3].y , x,y , info[name].bezier , info[name].prop)
  130.                 tool.reset_point(name)
  131.             else
  132.                 info[name].point[3] = {x=x, y=y}
  133.             end
  134.         else
  135.             info[name].point[2] = {x=x, y=y}
  136.         end
  137.     else
  138.         info[name].point[1] = {x=x, y=y}
  139.     end
  140.    
  141. end
  142.  
  143.  
  144.  
  145.  
  146.  
  147. function editor.joint.add (prop)
  148.    
  149.     table.insert(xml.joint, prop)
  150.     tfm.exec.addJoint (#xml.joint, 1, 1, prop)
  151.    
  152. end
  153.  
  154. function editor.joint.delete (id)
  155.    
  156.     if id ~= 0 then
  157.         tfm.exec.removeJoint (id)
  158.         table.remove(xml.joint, id)
  159.     end
  160.    
  161. end
  162.  
  163.  
  164.  
  165. function editor.joint.bezier ( x1,y1 , x2,y2 , x3,y3 , x4,y4 , div, prop )
  166.     local x = 1/div
  167.     local xb, yb = x1,y1
  168.    
  169.     prop.point1 = tostring(xb)..','..tostring(yb)
  170.    
  171.     for i=1, div do
  172.         xn =  math.floor((1-x)^3*x1 + 3*x*(1-x)^2*x2 + 3*x^2*(1-x)*x3 +x ^3*x4)
  173.         yn =  math.floor((1-x)^3*y1 + 3*x*(1-x)^2*y2 + 3*x^2*(1-x)*y3 +x ^3*y4)
  174.         x= x+1/div
  175.        
  176.         prop.point2 = tool.s_coord(xn,yn)
  177.         editor.joint.add(prop)
  178.         prop.point1 = prop.point2
  179.     end
  180. end
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. function tool.s_coord (x, y)
  188.     return tostring(x)..','..tostring(y)
  189. end
  190.  
  191. function tool.reset_point (name)
  192.     info[name].point = {}
  193. end
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement