diff --git a/PlatformPhysics.py b/PlatformPhysics.py index f4a976396d..5381892248 100644 --- a/PlatformPhysics.py +++ b/PlatformPhysics.py @@ -5,35 +5,27 @@ from UM.Math.Float import Float from UM.Math.Vector import Vector from UM.Application import Application +from PlatformPhysicsOperation import PlatformPhysicsOperation + +import time import threading class PlatformPhysics: def __init__(self, controller): super().__init__() self._controller = controller - self._controller.activeToolChanged.connect(self._onActiveToolChanged) - self._active_tool = None - self._operation = False - self._onActiveToolChanged() + self._controller.getScene().sceneChanged.connect(self._onSceneChanged) + self._signal_source = None - def _onActiveToolChanged(self): - 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): + def _onSceneChanged(self, source): root = self._controller.getScene().getRoot() for node in BreadthFirstIterator(root): - if type(node) is not SceneNode: + if node is root or type(node) is not SceneNode: continue bbox = node.getBoundingBox() if not Float.fuzzyCompare(bbox.bottom, 0.0): 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) self._signal_source = None diff --git a/PlatformPhysicsOperation.py b/PlatformPhysicsOperation.py new file mode 100644 index 0000000000..40ce20d453 --- /dev/null +++ b/PlatformPhysicsOperation.py @@ -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 +