Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Copyright (c) 2013, Thomas Siegmund
- -- All rights reserved.
- --
- -- Redistribution and use in source and binary forms, with or without modification, are permitted
- -- provided that the following conditions are met:
- --
- -- * Redistributions of source code must retain the above copyright notice,
- -- this list of conditions and the following disclaimer.
- --
- -- * Redistributions in binary form must reproduce the above copyright notice,
- -- this list of conditions and the following disclaimer in the documentation
- -- and/or other materials provided with the distribution.
- --
- -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
- -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- -- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
- -- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- -- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- local selectedObject = nil
- local selectedAnim = nil
- local leftMouseButtonIsPressed = 0
- local firstMousePosition = nil
- local mousePositionT1 = nil
- local reloadSceneAction = nil
- -- variables for saving the changed Parameters
- local animNames = {}
- local animPositions = {}
- local objVisibility = {}
- -- Find out if value v is in table t. If it is, then return the key, else return 0.
- local function inTable(t, v)
- for key, value in pairs(t) do
- if value == v then
- return key
- end
- end
- return 0
- end
- -- set the current position of the given anim to pos
- local function setAnimToPosition(anim, pos)
- local animPos = anim:getPoint(VAnimationCurrentPosition)
- animPos.x = pos.x
- animPos.y = pos.y
- anim:setValue(VAnimationCurrentPosition, animPos)
- end
- -- modify the current position of the given anim by the value of pos
- local function modifyAnimToPosition(anim, pos)
- local animPos = anim:getPoint(VAnimationCurrentPosition)
- animPos.x = animPos.x + pos.x
- animPos.y = animPos.y + pos.y
- anim:setValue(VAnimationCurrentPosition, animPos)
- end
- -- Modify all points of obj's polygon by the value of pos.
- -- You have to reload the scene to see any effect
- local function modifyObjectsPolygon(obj, pos)
- local polyPoints = obj:getPoints(VObjectPolygon)
- -- only change something if there is something to change
- if table.getn(polyPoints) ~= 0 then
- for i=1,table.maxn(polyPoints) do
- polyPoints[i].x = polyPoints[i].x + pos.x
- polyPoints[i].y = polyPoints[i].y + pos.y
- end
- obj:setValue(VObjectPolygon, polyPoints)
- end
- end
- -- called due to an eEvtMouseMove and modifies the animation of the currently selected object
- local function mouseMove(mousePosition)
- if selectedAnim ~= nil and leftMouseButtonIsPressed == 1 then
- modifyAnimToPosition(selectedAnim, {x=mousePosition.x - mousePositionT1.x, y=mousePosition.y - mousePositionT1.y})
- end
- mousePositionT1 = mousePosition
- end
- -- change the visibility value of the currently selected object or the object beneath the cursor
- -- if the selected object is nil
- local function addVisibility(i)
- local obj = nil
- if selectedObject == nil then
- obj = game:getLink(VGameCurrentObject)
- else
- obj = selectedObject
- end
- local v = obj:getInt(VObjectVisibility) + i
- local anim = getObject("ActiveAnimations["..obj:getName().."_Anim]")
- if v < 0 then
- v = 0;
- elseif v > 100 then
- v = 100
- end
- local it = inTable(animNames, anim:getName())
- if it ~= 0 then
- objVisibility[it] = v
- obj:setValue(VObjectVisibility, v)
- end
- end
- -- mouse event callback
- function mouseEvents(eventType, mousePosition)
- if eventType == eEvtMouseLeftButtonDown then
- selectedObject = game:getLink(VGameCurrentObject)
- -- there is an object beneath the cursor and it is of type eObjects
- if selectedObject ~= nil and selectedObject:getId().tableId == eObjects then
- -- only move objects with a movable == true condition
- local cond = selectedObject:getObject('.ObjectConditions[movable]')
- if cond == nil or not cond:getBool(VConditionValue) then
- return
- end
- selectedAnim = getObject("ActiveAnimations["..selectedObject:getName().."_Anim]")
- local it = inTable(animNames, selectedAnim:getName())
- -- the first time this element was selected ??? it == 0 means yes
- if it == 0 then
- table.insert(animNames, selectedAnim:getName())
- table.insert(animPositions, selectedAnim:getPoint(VAnimationCurrentPosition))
- table.insert(objVisibility, selectedObject:getInt(VObjectVisibility))
- end
- leftMouseButtonIsPressed = 1
- -- remember the position where the left button was pressed
- firstMousePosition = mousePosition
- end
- -- eEvtMouseLeftButtonUp and eEvtMouseLeftButtonHold are left-button-released events
- elseif eventType == eEvtMouseLeftButtonUp or eventType == eEvtMouseLeftButtonHold then
- -- is there anything to do
- if leftMouseButtonIsPressed == 1 then
- leftMouseButtonIsPressed = 0
- -- change the selected object's polygon
- modifyObjectsPolygon(selectedObject, {x=mousePosition.x - firstMousePosition.x, y=mousePosition.y - firstMousePosition.y})
- -- save the selected object's animation position
- local it = inTable(animNames, selectedAnim:getName())
- animPositions[it] = selectedAnim:getPoint(VAnimationCurrentPosition)
- -- initiate a scene reload
- local currentScene = game:getLink(VGameCurrentScene)
- if currentScene ~= nil then
- reloadSceneAction = currentScene:getObject(".SceneActions[reloadScene]")
- startAction(reloadSceneAction)
- end
- selectedObject = nil
- selectedAnim = nil
- end
- elseif eventType == eEvtMouseMove then
- mouseMove(mousePosition)
- elseif eventType == eEvtMouseWheelUp then
- addVisibility(5)
- elseif eventType == eEvtMouseWheelDown then
- addVisibility(-5)
- end
- end
- -- called from reloadSceneAction
- -- After a scene reload move all animations to the position we left them and set there visibility
- function setSelectedAnimPos()
- local currentScene = game:getLink(VGameCurrentScene)
- if currentScene ~= nil then
- local sceneObjects = currentScene:getLinks(VSceneObjects)
- for i=1,table.maxn(sceneObjects) do
- local anim = getObject("ActiveAnimations["..sceneObjects[i]:getName().."_Anim]")
- if anim ~= nil then
- local it = inTable(animNames, anim:getName())
- if it ~= 0 then
- setAnimToPosition(anim, animPositions[it])
- sceneObjects[i]:setValue(VObjectVisibility, objVisibility[it])
- end
- end
- end
- end
- end
- function init()
- registerEventHandler("mouseEvent", "mouseEvents", {eEvtMouseMove, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseWheelUp, eEvtMouseWheelDown})
- end
Advertisement
Add Comment
Please, Sign In to add comment