# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Scene.SceneNode import SceneNode

from . import Operation


class TranslateOperation(Operation.Operation):
    def __init__(self, node, translation, **kwargs):
        super().__init__()
        self._node = node
        self._old_transformation = node.getLocalTransformation()
        self._translation = translation
        self._set_position = kwargs.get("set_position", False)

    def undo(self):
        self._node.setTransformation(self._old_transformation)

    def redo(self):
        if self._set_position:
            self._node.setPosition(self._translation, SceneNode.TransformSpace.World)
        else:
            self._node.translate(self._translation, SceneNode.TransformSpace.World)

    def mergeWith(self, other):
        if type(other) is not TranslateOperation:
            return False

        if other._node != self._node:
            return False

        op = TranslateOperation(self._node, self._translation + other._translation)
        op._old_transformation = other._old_transformation
        return op

    def __repr__(self):
        return "TranslateOperation(node = {0}, translation={1})".format(self._node, self._translation)

# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Scene.SceneNode import SceneNode

from . import Operation

##  An operation that moves a scene node.
#
#   This has nothing to do with languages. It is a linear transformation on
#   geometry.
class TranslateOperation(Operation.Operation):
    ##  Initialises this TranslateOperation.
    #
    #   \param node The node to translate.
    #   \param translation A translation matrix to transform the node by.
    #   \param kwargs Key-word arguments, including:
    #    - set_position: Whether to change the position (True) or add the
    #      positions, making a relative move (False).
    def __init__(self, node, translation, **kwargs):
        super().__init__()
        self._node = node
        self._old_transformation = node.getLocalTransformation() # To restore the transformation to in case of an undo.
        self._translation = translation
        self._set_position = kwargs.get("set_position", False)

    ##  Undoes the translate operation, restoring the old transformation.
    def undo(self):
        self._node.setTransformation(self._old_transformation)

    ##  Re-applies the translate operation.
    def redo(self):
        if self._set_position: # Change the position to the new position.
            self._node.setPosition(self._translation, SceneNode.TransformSpace.World)
        else: # Add the translation to the current position.
            self._node.translate(self._translation, SceneNode.TransformSpace.World)

    ##  Merges this operation with another translate operation.
    #
    #   This prevents the user from having to undo multiple operations if they
    #   were not his operations.
    #
    #   You should ONLY merge this operation with an older operation. It is NOT
    #   symmetric.
    #
    #   \param other The older translate operation to merge this operation with.
    def mergeWith(self, other):
        if type(other) is not TranslateOperation:
            return False
        if other._node != self._node: # Must be moving the same node.
            return False

        op = TranslateOperation(self._node, self._translation + other._translation)
        op._old_transformation = other._old_transformation # Use the oldest transformation of the two to restore if we undo.
        return op

    ##  Returns a programmer-readable representation of this operation.
    #
    #   \return A programmer-readable representation of this operation.
    def __repr__(self):
        return "TranslateOperation(node = {0}, translation={1})".format(self._node, self._translation)

Document TranslateOperation