Tool System: per space/mode tool support
[blender.git] / release / scripts / startup / bl_ui / space_toolsystem_common.py
index b58bff7cf32ab221d1ab11c61585a69918e39105..ad5c970a6d515cd9ea2b12d57bce0b2962750fc0 100644 (file)
@@ -140,7 +140,7 @@ class ToolSelectPanelHelper:
       The text prefix for each key-map for this spaces tools.
     - tools_all():
       Returns (context_mode, tools) tuple pair for all tools defined.
-    - tools_from_context(context):
+    - tools_from_context(context, mode=None):
       Returns tools available in this context.
 
     Each tool is a 'ToolDef' or None for a separator in the toolbar, use ``None``.
@@ -230,23 +230,23 @@ class ToolSelectPanelHelper:
                     yield item, -1
 
     @staticmethod
-    def _tool_get_active(context, with_icon=False):
+    def _tool_get_active(context, space_type, mode, with_icon=False):
         """
         Return the active Python tool definition and icon name.
         """
 
         workspace = context.workspace
-        cls = ToolSelectPanelHelper._tool_class_from_space_type(workspace.tool_space_type)
+        cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
         if cls is not None:
-            tool_def_active, index_active = ToolSelectPanelHelper._tool_vars_from_active_with_index(context)
+            tool_active_text = getattr(
+                ToolSelectPanelHelper._tool_active_from_context(context, space_type, mode),
+                "name", None)
 
-            context_mode = context.mode
-            for item in ToolSelectPanelHelper._tools_flatten(cls.tools_from_context(context)):
+            for item in ToolSelectPanelHelper._tools_flatten(cls.tools_from_context(context, mode)):
                 if item is not None:
-                    tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
-                    if (tool_def == tool_def_active):
+                    if item.text == tool_active_text:
                         if with_icon:
-                            icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(icon_name)
+                            icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(item.icon)
                         else:
                             icon_value = 0
                         return (item, icon_value)
@@ -259,7 +259,6 @@ class ToolSelectPanelHelper:
         """
         cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
         if cls is not None:
-            context_mode = context.mode
             for item, index in ToolSelectPanelHelper._tools_flatten_with_tool_index(cls.tools_from_context(context)):
                 if item is not None:
                     if item.text == text:
@@ -267,7 +266,7 @@ class ToolSelectPanelHelper:
         return None, -1
 
     @staticmethod
-    def _tool_vars_from_def(item, context_mode):
+    def _tool_vars_from_def(item):
         # For now be strict about whats in this dict
         # prevent accidental adding unknown keys.
         text = item.text
@@ -282,16 +281,22 @@ class ToolSelectPanelHelper:
         return (km_idname, mp_idname, datablock_idname), icon_name
 
     @staticmethod
-    def _tool_vars_from_active_with_index(context):
-        workspace = context.workspace
-        return (
-            (
-                workspace.tool_keymap or None,
-                workspace.tool_manipulator_group or None,
-                workspace.tool_data_block or None,
-            ),
-            workspace.tool_index,
-        )
+    def _tool_active_from_context(context, space_type, mode=None, create=False):
+        if space_type == 'VIEW_3D':
+            if mode is None:
+                obj = context.active_object
+                mode = obj.mode if obj is not None else 'OBJECT'
+            tool = context.workspace.tools.from_space_view3d_mode(mode, create)
+            if tool is not None:
+                return tool
+        elif space_type == 'IMAGE_EDITOR':
+            space_data = context.space_data
+            if mode is None:
+                mode = space_data.mode
+            tool = context.workspace.tools.from_space_image_mode(mode, create)
+            if tool is not None:
+                return tool
+        return None
 
     @staticmethod
     def _tool_text_from_button(context):
@@ -445,8 +450,11 @@ class ToolSelectPanelHelper:
         # - tool-tips that include multiple key shortcuts.
         # - ability to click and hold to expose sub-tools.
 
-        context_mode = context.mode
-        tool_def_active, index_active = ToolSelectPanelHelper._tool_vars_from_active_with_index(context)
+        space_type = context.space_data.type
+        tool_active_text = getattr(
+            ToolSelectPanelHelper._tool_active_from_context(context, space_type),
+            "name", None,
+        )
 
         ui_gen, show_text = self._layout_generator_detect_from_region(self.layout, context.region)
 
@@ -464,8 +472,7 @@ class ToolSelectPanelHelper:
                 for i, sub_item in enumerate(item):
                     if sub_item is None:
                         continue
-                    tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(sub_item, context_mode)
-                    is_active = (tool_def == tool_def_active)
+                    is_active = (sub_item.text == tool_active_text)
                     if is_active:
                         index = i
                         break
@@ -483,8 +490,8 @@ class ToolSelectPanelHelper:
                 index = -1
                 use_menu = False
 
-            tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
-            is_active = (tool_def == tool_def_active)
+            tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item)
+            is_active = (item.text == tool_active_text)
 
             icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(icon_name)
 
@@ -510,7 +517,11 @@ class ToolSelectPanelHelper:
 
     @staticmethod
     def draw_active_tool_header(context, layout):
-        item, icon_value = ToolSelectPanelHelper._tool_get_active(context, with_icon=True)
+        # BAD DESIGN WARNING: last used tool
+        workspace = context.workspace
+        space_type = workspace.tools_space_type
+        mode = workspace.tools_mode
+        item, icon_value = ToolSelectPanelHelper._tool_get_active(context, space_type, mode, with_icon=True)
         if item is None:
             return
         # Note: we could show 'item.text' here but it makes the layout jitter when switcuing tools.
@@ -527,7 +538,6 @@ class WM_MT_toolsystem_submenu(Menu):
 
     @staticmethod
     def _tool_group_from_button(context):
-        context_mode = context.mode
         # Lookup the tool definitions based on the space-type.
         cls = ToolSelectPanelHelper._tool_class_from_space_type(context.space_data.type)
         if cls is not None:
@@ -540,7 +550,6 @@ class WM_MT_toolsystem_submenu(Menu):
         return None, None
 
     def draw(self, context):
-        context_mode = context.mode
         layout = self.layout
         layout.scale_y = 2.0
 
@@ -554,7 +563,7 @@ class WM_MT_toolsystem_submenu(Menu):
             if item is None:
                 layout.separator()
                 continue
-            tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
+            tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item)
             icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(icon_name)
             layout.operator(
                 "wm.tool_set_by_name",
@@ -566,9 +575,10 @@ class WM_MT_toolsystem_submenu(Menu):
 def activate_by_name(context, space_type, text):
     item, index = ToolSelectPanelHelper._tool_get_by_name(context, space_type, text)
     if item is not None:
-        context_mode = context.mode
-        tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
-        bpy.ops.wm.tool_set(
+        tool = ToolSelectPanelHelper._tool_active_from_context(context, space_type, create=True)
+        tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item)
+        tool.setup(
+            name=text,
             keymap=tool_def[0] or "",
             manipulator_group=tool_def[1] or "",
             data_block=tool_def[2] or "",