AFRLme

divo: Drag'n'Drop Example [VS] (...)

Jan 9th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.98 KB | None | 0 0
  1. -- Copyright (c) 2013, Thomas Siegmund
  2. -- All rights reserved.
  3. --
  4. -- Redistribution and use in source and binary forms, with or without modification, are permitted
  5. -- provided that the following conditions are met:
  6. --
  7. --    * Redistributions of source code must retain the above copyright notice,
  8. --      this list of conditions and the following disclaimer.
  9. --
  10. --    * Redistributions in binary form must reproduce the above copyright notice,
  11. --      this list of conditions and the following disclaimer in the documentation
  12. --      and/or other materials provided with the distribution.
  13. --
  14. -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  15. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. -- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17. -- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  18. -- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22.  
  23. local selectedObject = nil
  24. local selectedAnim = nil
  25. local leftMouseButtonIsPressed = 0
  26. local firstMousePosition = nil
  27. local mousePositionT1 = nil
  28. local reloadSceneAction = nil
  29.  
  30. -- variables for saving the changed Parameters
  31. local animNames = {}
  32. local animPositions = {}
  33. local objVisibility = {}
  34.  
  35. -- Find out if value v is in table t. If it is, then return the key, else return 0.
  36. local function inTable(t, v)
  37.     for key, value in pairs(t) do
  38.         if value == v then
  39.             return key
  40.         end
  41.     end
  42.     return 0
  43. end
  44.  
  45. -- set the current position of the given anim to pos
  46. local function setAnimToPosition(anim, pos)
  47.     local animPos = anim:getPoint(VAnimationCurrentPosition)
  48.     animPos.x = pos.x
  49.     animPos.y = pos.y
  50.     anim:setValue(VAnimationCurrentPosition, animPos)
  51. end
  52.  
  53. -- modify the current position of the given anim by the value of pos
  54. local function modifyAnimToPosition(anim, pos)
  55.     local animPos = anim:getPoint(VAnimationCurrentPosition)
  56.     animPos.x = animPos.x + pos.x
  57.     animPos.y = animPos.y + pos.y
  58.     anim:setValue(VAnimationCurrentPosition, animPos)
  59. end
  60.  
  61. -- Modify all points of obj's polygon by the value of pos.
  62. -- You have to reload the scene to see any effect
  63. local function modifyObjectsPolygon(obj, pos)
  64.     local polyPoints = obj:getPoints(VObjectPolygon)
  65.     -- only change something if there is something to change
  66.     if table.getn(polyPoints) ~= 0 then
  67.         for i=1,table.maxn(polyPoints) do
  68.             polyPoints[i].x = polyPoints[i].x + pos.x
  69.             polyPoints[i].y = polyPoints[i].y + pos.y
  70.         end
  71.         obj:setValue(VObjectPolygon, polyPoints)
  72.     end
  73. end
  74.  
  75. -- called due to an eEvtMouseMove and modifies the animation of the currently selected object
  76. local function mouseMove(mousePosition)
  77.     if selectedAnim ~= nil and leftMouseButtonIsPressed == 1 then
  78.         modifyAnimToPosition(selectedAnim, {x=mousePosition.x - mousePositionT1.x, y=mousePosition.y - mousePositionT1.y})
  79.     end
  80.     mousePositionT1 = mousePosition
  81. end
  82.  
  83. -- change the visibility value of the currently selected object or the object beneath the cursor
  84. -- if the selected object is nil
  85. local function addVisibility(i)
  86.     local obj = nil
  87.     if selectedObject == nil then
  88.         obj = game:getLink(VGameCurrentObject)
  89.     else
  90.         obj = selectedObject
  91.     end
  92.  
  93.     local v = obj:getInt(VObjectVisibility) + i
  94.     local anim = getObject("ActiveAnimations["..obj:getName().."_Anim]")
  95.  
  96.     if v < 0 then
  97.         v = 0;
  98.     elseif v > 100 then
  99.         v = 100
  100.     end
  101.  
  102.     local it = inTable(animNames, anim:getName())
  103.     if it ~= 0 then
  104.         objVisibility[it] = v
  105.         obj:setValue(VObjectVisibility, v)
  106.     end
  107. end
  108.  
  109. -- mouse event callback
  110. function mouseEvents(eventType, mousePosition)
  111.     if eventType == eEvtMouseLeftButtonDown then
  112.         selectedObject = game:getLink(VGameCurrentObject)
  113.         -- there is an object beneath the cursor and it is of type eObjects
  114.         if selectedObject ~= nil and selectedObject:getId().tableId == eObjects then
  115.             -- only move objects with a movable == true condition
  116.             local cond = selectedObject:getObject('.ObjectConditions[movable]')
  117.             if cond == nil or not cond:getBool(VConditionValue) then
  118.                 return
  119.             end
  120.             selectedAnim = getObject("ActiveAnimations["..selectedObject:getName().."_Anim]")
  121.             local it = inTable(animNames, selectedAnim:getName())
  122.             -- the first time this element was selected ??? it == 0 means yes
  123.             if it == 0 then
  124.                 table.insert(animNames, selectedAnim:getName())
  125.                 table.insert(animPositions, selectedAnim:getPoint(VAnimationCurrentPosition))
  126.                 table.insert(objVisibility, selectedObject:getInt(VObjectVisibility))
  127.             end
  128.             leftMouseButtonIsPressed = 1
  129.             -- remember the position where the left button was pressed
  130.             firstMousePosition = mousePosition
  131.         end
  132.     -- eEvtMouseLeftButtonUp and eEvtMouseLeftButtonHold are left-button-released events
  133.     elseif eventType == eEvtMouseLeftButtonUp or eventType == eEvtMouseLeftButtonHold then
  134.         -- is there anything to do
  135.         if leftMouseButtonIsPressed == 1 then
  136.             leftMouseButtonIsPressed = 0
  137.             -- change the selected object's polygon
  138.             modifyObjectsPolygon(selectedObject, {x=mousePosition.x - firstMousePosition.x, y=mousePosition.y - firstMousePosition.y})
  139.  
  140.             -- save the selected object's animation position           
  141.             local it = inTable(animNames, selectedAnim:getName())
  142.             animPositions[it] = selectedAnim:getPoint(VAnimationCurrentPosition)
  143.  
  144.             -- initiate a scene reload
  145.             local currentScene = game:getLink(VGameCurrentScene)
  146.             if currentScene ~= nil then
  147.                 reloadSceneAction = currentScene:getObject(".SceneActions[reloadScene]")
  148.                 startAction(reloadSceneAction)
  149.             end
  150.             selectedObject = nil
  151.             selectedAnim = nil
  152.         end
  153.     elseif eventType == eEvtMouseMove then
  154.         mouseMove(mousePosition)
  155.     elseif eventType == eEvtMouseWheelUp then
  156.         addVisibility(5)
  157.     elseif eventType == eEvtMouseWheelDown then
  158.         addVisibility(-5)
  159.     end
  160. end
  161.  
  162. -- called from reloadSceneAction
  163. -- After a scene reload move all animations to the position we left them and set there visibility
  164. function setSelectedAnimPos()
  165.     local currentScene = game:getLink(VGameCurrentScene)
  166.     if currentScene ~= nil then
  167.         local sceneObjects = currentScene:getLinks(VSceneObjects)
  168.         for i=1,table.maxn(sceneObjects) do
  169.             local anim = getObject("ActiveAnimations["..sceneObjects[i]:getName().."_Anim]")
  170.             if anim ~= nil then
  171.                 local it = inTable(animNames, anim:getName())
  172.                 if it ~= 0 then
  173.                     setAnimToPosition(anim, animPositions[it])
  174.                     sceneObjects[i]:setValue(VObjectVisibility, objVisibility[it])
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. function init()
  182.     registerEventHandler("mouseEvent", "mouseEvents", {eEvtMouseMove, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseWheelUp, eEvtMouseWheelDown})
  183. end
Advertisement
Add Comment
Please, Sign In to add comment