Skip to content

Commit

Permalink
Properly track the selection center and use it for the tool handle po…
Browse files Browse the repository at this point in the history
…sition

Fixes #34
  • Loading branch information
awhiemstra committed Jun 24, 2015
1 parent 3660ca0 commit 01eb0fe
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
41 changes: 21 additions & 20 deletions UM/Scene/Selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ class Selection:
def add(cls, object):
if object not in cls.__selection:
cls.__selection.append(object)
object.transformationChanged.connect(cls._onTransformationChanged)
cls._onTransformationChanged(object)
cls.selectionChanged.emit()

@classmethod
def remove(cls, object):
if object in cls.__selection:
cls.__selection.remove(object)
object.transformationChanged.disconnect(cls._onTransformationChanged)
cls._onTransformationChanged(object)
cls.selectionChanged.emit()

@classmethod
Expand Down Expand Up @@ -44,29 +48,14 @@ def hasSelection(cls):

selectionChanged = Signal()

## Calculate the average position of the selection.
selectionCenterChanged = Signal()

@classmethod
def getAveragePosition(cls):
def getSelectionCenter(cls):
if not cls.__selection:
return Vector(0, 0, 0)

if len(cls.__selection) == 1:
node = cls.__selection[0]
if node.getBoundingBox() and node.getBoundingBox().isValid():
return node.getBoundingBox().center
else:
return node.getWorldPosition()

pos = Vector()
for node in cls.__selection:
if node.getBoundingBox() and node.getBoundingBox().isValid():
pos += node.getBoundingBox().center
else:
pos += node.getWorldPosition()

pos /= len(cls.__selection)
cls.__selection_center = Vector(0, 0, 0)

return pos
return cls.__selection_center

## Apply an operation to the entire selection
#
Expand Down Expand Up @@ -96,4 +85,16 @@ def applyOperation(cls, operation, *args, **kwargs):

op.push()

@classmethod
def _onTransformationChanged(cls, node):
cls.__selection_center = Vector(0, 0, 0)

for object in cls.__selection:
cls.__selection_center += object.getWorldPosition()

cls.__selection_center /= len(cls.__selection)

cls.selectionCenterChanged.emit()

__selection = []
__selection_center = Vector(0, 0, 0)
7 changes: 7 additions & 0 deletions UM/Scene/ToolHandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from UM.Math.Color import Color
from UM.Math.Vector import Vector

from UM.Scene.Selection import Selection

class ToolHandle(SceneNode.SceneNode):
NoAxis = 1
XAxis = 2
Expand Down Expand Up @@ -39,6 +41,8 @@ def __init__(self, parent = None):

self.setCalculateBoundingBox(False)

Selection.selectionCenterChanged.connect(self._onSelectionCenterChanged)

def getLineMesh(self):
return self._line_mesh

Expand Down Expand Up @@ -114,3 +118,6 @@ def isAxis(cls, value):
ZAxis: ZAxisColor,
AllAxis: AllAxisColor
}

def _onSelectionCenterChanged(self):
self.setPosition(Selection.getSelectionCenter())
7 changes: 0 additions & 7 deletions UM/Tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def event(self, event):
if event.type == Event.ToolActivateEvent:
if Selection.hasSelection() and self._handle:
self._handle.setParent(self.getController().getScene().getRoot())
self._handle.setPosition(Selection.getAveragePosition())

if event.type == Event.MouseMoveEvent and self._handle:
if self._locked_axis:
Expand All @@ -61,12 +60,6 @@ def event(self, event):

return False

## Update the position of the ToolHandle
# \sa ToolHandle
def updateHandlePosition(self):
if Selection.hasSelection():
self._handle.setPosition(Selection.getAveragePosition())

## Convenience function
def getController(self):
return self._controller
Expand Down
1 change: 0 additions & 1 deletion plugins/Tools/RotateTool/RotateTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def event(self, event):
Selection.applyOperation(RotateOperation, rotation)

self.setDragStart(event.x, event.y)
self.updateHandlePosition()

if event.type == Event.MouseReleaseEvent:
if self.getDragPlane():
Expand Down
1 change: 0 additions & 1 deletion plugins/Tools/ScaleTool/ScaleTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def event(self, event):
Selection.applyOperation(ScaleOperation, scale)

self._drag_length = (handle_position - drag_position).length()
self.updateHandlePosition()
return True

if event.type == Event.MouseReleaseEvent:
Expand Down
1 change: 0 additions & 1 deletion plugins/Tools/TranslateTool/TranslateTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def event(self, event):
Selection.applyOperation(TranslateOperation, drag)

self.setDragStart(event.x, event.y)
self.updateHandlePosition()
return True

if event.type == Event.MouseReleaseEvent:
Expand Down

0 comments on commit 01eb0fe

Please sign in to comment.