1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# Copyright (c) 2019 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
from typing import Optional
from enum import IntEnum
import random
from UM.Logger import Logger
from UM.Mesh.MeshData import MeshData
from . import SceneNode
from UM.Resources import Resources
from UM.Application import Application
from UM.Math.Color import Color
from UM.Math.Vector import Vector
from UM.Scene.Selection import Selection
from UM.View.GL.OpenGL import OpenGL
from UM.View.RenderBatch import RenderBatch
class ToolHandle(SceneNode.SceneNode):
"""A tool handle is a object in the scene that gives queues for what the tool it is
'paired' with can do. ToolHandles are, for example, used for translation, rotation & scale handles.
They can also be used as actual objects to interact with (in the case of translation,
pressing one arrow of the toolhandle locks the translation in that direction)
"""
NoAxis = 1
XAxis = 2
YAxis = 3
ZAxis = 4
AllAxis = 5
# These colors are used to draw the selection pass only. They must be unique, which is
# why we cannot rely on themed colors
DisabledSelectionColor = Color(0.5, 0.5, 0.5, 1.0)
XAxisSelectionColor = Color(1.0, 0.0, 0.0, 1.0)
YAxisSelectionColor = Color(0.0, 0.0, 1.0, 1.0)
ZAxisSelectionColor = Color(0.0, 1.0, 0.0, 1.0)
AllAxisSelectionColor = Color(1.0, 1.0, 1.0, 1.0)
class ExtraWidgets(IntEnum):
"""Toolhandle subclasses can optionally register additional widgets by overriding this enum.
The ExtraWidgetsEnum should start with Toolhanlde.AllAxis + 1 in order not to overlap with the native axes.
"""
pass
def __init__(self, parent = None):
super().__init__(parent)
self._disabled_axis_color = None
self._x_axis_color = None
self._y_axis_color = None
self._z_axis_color = None
self._all_axis_color = None
self._axis_color_map = {}
self._extra_widgets_color_map = {}
self._scene = Application.getInstance().getController().getScene()
self._solid_mesh = None # type: Optional[MeshData]
self._line_mesh = None # type: Optional[MeshData]
self._selection_mesh = None # type: Optional[MeshData]
self._shader = None
self._active_axis = None # type: Optional[int]
# Auto scale is used to ensure that the tool handle will end up the same size on the camera no matter the zoom
# This should be used to ensure that the tool handles are still usable even if the camera is zoomed in all the way.
self._auto_scale = True
self._enabled = False
self.setCalculateBoundingBox(False)
Selection.selectionCenterChanged.connect(self._onSelectionCenterChanged)
Application.getInstance().engineCreatedSignal.connect(self._onEngineCreated)
def getLineMesh(self) -> Optional[MeshData]:
return self._line_mesh
def setLineMesh(self, mesh: MeshData) -> None:
self._line_mesh = mesh
self.meshDataChanged.emit(self)
def getSolidMesh(self) -> Optional[MeshData]:
return self._solid_mesh
def setSolidMesh(self, mesh: MeshData) -> None:
self._solid_mesh = mesh
self.meshDataChanged.emit(self)
def getSelectionMesh(self) -> Optional[MeshData]:
return self._selection_mesh
def setSelectionMesh(self, mesh: MeshData) -> None:
self._selection_mesh = mesh
self.meshDataChanged.emit(self)
def render(self, renderer) -> bool:
if not self._enabled:
return True
if not self._shader:
self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "toolhandle.shader"))
if self._auto_scale:
active_camera = self._scene.getActiveCamera()
if active_camera.isPerspective():
camera_position = active_camera.getWorldPosition()
dist = (camera_position - self.getWorldPosition()).length()
scale = dist / 400
else:
view_width = active_camera.getViewportWidth()
current_size = view_width + (2 * active_camera.getZoomFactor() * view_width)
scale = current_size / view_width * 5
self.setScale(Vector(scale, scale, scale))
if self._line_mesh:
renderer.queueNode(self, mesh = self._line_mesh, mode = RenderBatch.RenderMode.Lines, overlay = True, shader = self._shader)
if self._solid_mesh:
renderer.queueNode(self, mesh = self._solid_mesh, overlay = True, shader = self._shader)
return True
def setActiveAxis(self, axis: Optional[int]) -> None:
if axis == self._active_axis or not self._shader:
return
if axis:
self._shader.setUniformValue("u_activeColor", self._axis_color_map.get(axis, Color()))
else:
self._shader.setUniformValue("u_activeColor", self._disabled_axis_color)
self._active_axis = axis
self._scene.sceneChanged.emit(self)
def getActiveAxis(self) -> Optional[int]:
return self._active_axis
def isAxis(self, value):
return value in self._axis_color_map
def getExtraWidgetsColorMap(self):
return self._extra_widgets_color_map
def buildMesh(self) -> None:
# This method should be overridden by toolhandle implementations
pass
def _onSelectionCenterChanged(self) -> None:
if self._enabled:
self.setPosition(Selection.getSelectionCenter())
def setEnabled(self, enable: bool):
super().setEnabled(enable)
# Force an update
self._onSelectionCenterChanged()
def _onEngineCreated(self) -> None:
from UM.Qt.QtApplication import QtApplication
theme = QtApplication.getInstance().getTheme()
if theme is None:
Logger.log("w", "Could not get theme, so unable to create tool handle meshes.")
return
self._disabled_axis_color = Color(*theme.getColor("disabled_axis").getRgb())
self._x_axis_color = Color(*theme.getColor("x_axis").getRgb())
self._y_axis_color = Color(*theme.getColor("y_axis").getRgb())
self._z_axis_color = Color(*theme.getColor("z_axis").getRgb())
self._all_axis_color = Color(*theme.getColor("all_axis").getRgb())
self._axis_color_map = {
self.NoAxis: self._disabled_axis_color,
self.XAxis: self._x_axis_color,
self.YAxis: self._y_axis_color,
self.ZAxis: self._z_axis_color,
self.AllAxis: self._all_axis_color
}
for value in self.ExtraWidgets:
self._extra_widgets_color_map[value] = self._getUnusedColor()
self.buildMesh()
def _getUnusedColor(self):
while True:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
a = 255
color = Color(r, g, b, a)
if color not in self._axis_color_map.values() and color not in self._extra_widgets_color_map.values():
break
return color
|