Perform Platform physics changes on scene changed again and use a custom operation for better op merging

This commit is contained in:
Arjen Hiemstra 2015-01-06 17:16:24 +01:00
parent bfa3c00200
commit e24c120e82
2 changed files with 35 additions and 16 deletions

View file

@ -5,35 +5,27 @@ from UM.Math.Float import Float
from UM.Math.Vector import Vector from UM.Math.Vector import Vector
from UM.Application import Application from UM.Application import Application
from PlatformPhysicsOperation import PlatformPhysicsOperation
import time
import threading import threading
class PlatformPhysics: class PlatformPhysics:
def __init__(self, controller): def __init__(self, controller):
super().__init__() super().__init__()
self._controller = controller self._controller = controller
self._controller.activeToolChanged.connect(self._onActiveToolChanged) self._controller.getScene().sceneChanged.connect(self._onSceneChanged)
self._active_tool = None self._signal_source = None
self._operation = False
self._onActiveToolChanged()
def _onActiveToolChanged(self): def _onSceneChanged(self, source):
if self._active_tool:
self._active_tool.endOperation.disconnect(self._onEndOperation)
self._active_tool = self._controller.getActiveTool()
if self._active_tool:
self._active_tool.endOperation.connect(self._onEndOperation)
def _onEndOperation(self):
root = self._controller.getScene().getRoot() root = self._controller.getScene().getRoot()
for node in BreadthFirstIterator(root): for node in BreadthFirstIterator(root):
if type(node) is not SceneNode: if node is root or type(node) is not SceneNode:
continue continue
bbox = node.getBoundingBox() bbox = node.getBoundingBox()
if not Float.fuzzyCompare(bbox.bottom, 0.0): if not Float.fuzzyCompare(bbox.bottom, 0.0):
self._signal_source = node self._signal_source = node
op = TranslateOperation(node, Vector(0, -bbox.bottom, 0)) op = PlatformPhysicsOperation(node, Vector(0, -bbox.bottom, 0))
Application.getInstance().getOperationStack().push(op) Application.getInstance().getOperationStack().push(op)
self._signal_source = None self._signal_source = None

View file

@ -0,0 +1,27 @@
from UM.Operations.Operation import Operation
from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
from UM.Operations.TranslateOperation import TranslateOperation
## A specialised operation designed specifically to modify the previous operation.
class PlatformPhysicsOperation(Operation):
def __init__(self, node, translation):
super().__init__()
self._node = node
self._translation = translation
def undo(self):
pass
def redo(self):
pass
def mergeWith(self, other):
if type(other) is AddSceneNodeOperation:
other._node.translate(self._translation)
return other
elif type(other) is TranslateOperation:
other._translation += self._translation
return other
else:
return False