Advertisement
podoko_Lua

Joint drawing [v1.0]

Jun 9th, 2014
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. --[[ Joint drawing [v1.0]
  2.        
  3.         !color ff00ff           => Changer la couleur (mettez un code hexadecimal)
  4.         !alpha xx               => Changer la transparence de ses joints (entre 0 et 1)
  5.         !line xx                => Changer l'épaisseur de ses joints
  6.         !bezier xx              => Changer le nombre de joints pour former un bezier
  7.         !xml                    => Afficher le code xml du dessin pour l'éditeur de cartes
  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(command:sub(7),16)
  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.     elseif command == 'xml' then
  91.         tool.get_xml()
  92.     end
  93. end
  94.  
  95. function eventTextAreaCallback (id, name, call)
  96.    
  97.     local section = {}
  98.     for w in string.gmatch(call, "%S+") do
  99.         table.insert(section, w)
  100.        
  101.     end
  102.    
  103.     if section[1] == "joint" then
  104.         if section[2] == "type" then
  105.             info[name].joint = section[3]
  106.             tool.reset_point(name)
  107.         end
  108.     end
  109.    
  110. end
  111.  
  112. function eventMouse (name, x, y)
  113.    
  114.     if info[name].joint == '•' then
  115.         local prop = info[name].prop
  116.         prop.point1 = tool.s_coord(x,y)
  117.         prop.point2 = tool.s_coord(x+1,y)
  118.         editor.joint.add(prop)
  119.         tool.reset_point(name)
  120.        
  121.     elseif info[name].point[1] then
  122.         if info[name].joint == '/' then
  123.             local prop = info[name].prop
  124.             prop.point1 = tool.s_coord(info[name].point[1].x, info[name].point[1].y)
  125.             prop.point2 = tool.s_coord(x,y)
  126.             editor.joint.add(prop)
  127.             tool.reset_point(name)
  128.            
  129.         elseif info[name].point[2] then
  130.             if info[name].point[3] then
  131.                 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)
  132.                 tool.reset_point(name)
  133.             else
  134.                 info[name].point[3] = {x=x, y=y}
  135.             end
  136.         else
  137.             info[name].point[2] = {x=x, y=y}
  138.         end
  139.     else
  140.         info[name].point[1] = {x=x, y=y}
  141.     end
  142.    
  143. end
  144.  
  145.  
  146.  
  147.  
  148.  
  149. function editor.joint.add (prop)
  150.    
  151.     local last = #xml.joint+1
  152.     xml.joint[last] = {}
  153.     for key, val in pairs(prop) do
  154.         xml.joint[last][key] = val
  155.     end
  156. --  table.insert(xml.joint, prop)
  157.     tfm.exec.addJoint (#xml.joint, 1, 1, prop)
  158.    
  159. end
  160.  
  161. function editor.joint.delete (id)
  162.    
  163.     if id ~= 0 then
  164.         tfm.exec.removeJoint (id)
  165.         table.remove(xml.joint, id)
  166.     end
  167.    
  168. end
  169.  
  170. function editor.joint.bezier ( x1,y1 , x2,y2 , x3,y3 , x4,y4 , div, prop )
  171.     local x = 1/div
  172.     local xb, yb = x1,y1
  173.     local floor = math.floor
  174.    
  175.     prop.point1 = tostring(xb)..','..tostring(yb)
  176.    
  177.  
  178.     for i=1, div do
  179.         xn =  floor((1-x)^3*x1 + 3*x*(1-x)^2*x2 + 3*x^2*(1-x)*x3 +x ^3*x4)
  180.         yn =  floor((1-x)^3*y1 + 3*x*(1-x)^2*y2 + 3*x^2*(1-x)*y3 +x ^3*y4)
  181.         x= x+1/div
  182.        
  183.         prop.point2 = tool.s_coord (xn,yn)
  184.         editor.joint.add(prop)
  185.         prop.point1 = prop.point2
  186.     end
  187.  
  188. end
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. function tool.s_coord (x, y)
  198.     return tostring(x)..','..tostring(y)
  199. end
  200.  
  201. function tool.reset_point (name)
  202.     info[name].point = {}
  203. end
  204.  
  205. function tool.get_xml ()
  206.     local code = "&lt;L&gt;"
  207.     for _, tab in ipairs(xml.joint) do
  208.         code = code.. '&lt;JD P1="'..tab.point1..'" P2="'..tab.point2..'" c="'..string.format('%x', tab.color)..','..tab.line..','..tab.alpha..',1" /&gt;'
  209.     end
  210.     code = code.."&lt;L /&gt;&lt;/L&gt;"
  211.    
  212.     comp = 0
  213.     while comp < #code do
  214.         print(code:sub(comp, comp+4500))
  215.         comp = comp+4501
  216.     end
  217.    
  218. end
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement