Tool System: store the fallback tool for re-use
The fallback tool was run-time only data, now it's stored in the blend file.
This commit is contained in:
		@@ -354,6 +354,16 @@ class ToolSelectPanelHelper:
 | 
			
		||||
                i += 1
 | 
			
		||||
        return None, -1
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def _tool_group_active_set_by_id(cls, context, idname_group, idname):
 | 
			
		||||
        item_group = cls._tool_get_group_by_id(context, idname_group, coerce=True)
 | 
			
		||||
        if item_group:
 | 
			
		||||
            for i, item in enumerate(item_group):
 | 
			
		||||
                if item and item.idname == idname:
 | 
			
		||||
                    cls._tool_group_active[item_group[0].idname] = i
 | 
			
		||||
                    return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def _tool_active_from_context(context, space_type, mode=None, create=False):
 | 
			
		||||
        if space_type in {'VIEW_3D', 'PROPERTIES'}:
 | 
			
		||||
@@ -660,10 +670,10 @@ class ToolSelectPanelHelper:
 | 
			
		||||
            *,
 | 
			
		||||
            is_horizontal_layout=False,
 | 
			
		||||
    ):
 | 
			
		||||
        tool_fallback = tool.tool_fallback
 | 
			
		||||
        idname_fallback = tool.idname_fallback
 | 
			
		||||
        space_type = tool.space_type
 | 
			
		||||
        cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
 | 
			
		||||
        item_fallback, _index = cls._tool_get_by_id(context, tool_fallback)
 | 
			
		||||
        item_fallback, _index = cls._tool_get_by_id(context, idname_fallback)
 | 
			
		||||
        if item_fallback is not None:
 | 
			
		||||
            draw_settings = item_fallback.draw_settings
 | 
			
		||||
            if draw_settings is not None:
 | 
			
		||||
@@ -700,11 +710,11 @@ class ToolSelectPanelHelper:
 | 
			
		||||
            draw_settings(context, layout, tool)
 | 
			
		||||
 | 
			
		||||
        if context.preferences.experimental.use_tool_fallback:
 | 
			
		||||
            tool_fallback = tool.tool_fallback
 | 
			
		||||
            idname_fallback = tool.idname_fallback
 | 
			
		||||
        else:
 | 
			
		||||
            tool_fallback = None
 | 
			
		||||
            idname_fallback = None
 | 
			
		||||
 | 
			
		||||
        if tool_fallback and tool_fallback != item.idname:
 | 
			
		||||
        if idname_fallback and idname_fallback != item.idname:
 | 
			
		||||
            tool_settings = context.tool_settings
 | 
			
		||||
 | 
			
		||||
            # Show popover which looks like an enum but isn't one.
 | 
			
		||||
@@ -862,6 +872,7 @@ class WM_MT_toolsystem_submenu(Menu):
 | 
			
		||||
 | 
			
		||||
def _activate_by_item(context, space_type, item, index, *, as_fallback=False):
 | 
			
		||||
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
 | 
			
		||||
    tool = ToolSelectPanelHelper._tool_active_from_context(context, space_type, create=True)
 | 
			
		||||
    tool_fallback_id = cls.tool_fallback_id
 | 
			
		||||
 | 
			
		||||
    if as_fallback:
 | 
			
		||||
@@ -889,6 +900,12 @@ def _activate_by_item(context, space_type, item, index, *, as_fallback=False):
 | 
			
		||||
        # Done, now get the current tool to replace the item & index.
 | 
			
		||||
        tool_active = ToolSelectPanelHelper._tool_active_from_context(context, space_type)
 | 
			
		||||
        item, index = cls._tool_get_by_id(context, getattr(tool_active, "idname", None))
 | 
			
		||||
    else:
 | 
			
		||||
        # Ensure the active fallback tool is read from saved state (even if the fallback tool is not in use).
 | 
			
		||||
        stored_idname_fallback = tool.idname_fallback
 | 
			
		||||
        if stored_idname_fallback:
 | 
			
		||||
            cls._tool_group_active_set_by_id(context, tool_fallback_id, stored_idname_fallback)
 | 
			
		||||
        del stored_idname_fallback
 | 
			
		||||
 | 
			
		||||
    # Find fallback keymap.
 | 
			
		||||
    item_fallback = None
 | 
			
		||||
@@ -897,7 +914,6 @@ def _activate_by_item(context, space_type, item, index, *, as_fallback=False):
 | 
			
		||||
        item_fallback, _index = cls._tool_get_active_by_index(context, select_index)
 | 
			
		||||
    # End calculating fallback.
 | 
			
		||||
 | 
			
		||||
    tool = ToolSelectPanelHelper._tool_active_from_context(context, space_type, create=True)
 | 
			
		||||
    tool.setup(
 | 
			
		||||
        idname=item.idname,
 | 
			
		||||
        keymap=item.keymap[0] if item.keymap is not None else "",
 | 
			
		||||
 
 | 
			
		||||
@@ -35,8 +35,7 @@ typedef struct bToolRef_Runtime {
 | 
			
		||||
  char gizmo_group[64];
 | 
			
		||||
  char data_block[64];
 | 
			
		||||
 | 
			
		||||
  /** Optionally use these when not interacting directly with the primary tools gizmo. */
 | 
			
		||||
  char idname_fallback[64];
 | 
			
		||||
  /** Keymap for #bToolRef.idname_fallback, if set. */
 | 
			
		||||
  char keymap_fallback[64];
 | 
			
		||||
 | 
			
		||||
  /** Use to infer primary operator to use when setting accelerator keys. */
 | 
			
		||||
@@ -51,6 +50,9 @@ typedef struct bToolRef {
 | 
			
		||||
  struct bToolRef *next, *prev;
 | 
			
		||||
  char idname[64];
 | 
			
		||||
 | 
			
		||||
  /** Optionally use these when not interacting directly with the primary tools gizmo. */
 | 
			
		||||
  char idname_fallback[64];
 | 
			
		||||
 | 
			
		||||
  /** Use to avoid initializing the same tool multiple times. */
 | 
			
		||||
  short tag;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -192,18 +192,6 @@ static int rna_WorkSpaceTool_widget_length(PointerRNA *ptr)
 | 
			
		||||
  return tref->runtime ? strlen(tref->runtime->gizmo_group) : 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rna_WorkSpaceTool_tool_fallback_get(PointerRNA *ptr, char *value)
 | 
			
		||||
{
 | 
			
		||||
  bToolRef *tref = ptr->data;
 | 
			
		||||
  strcpy(value, tref->runtime ? tref->runtime->idname_fallback : "");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int rna_WorkSpaceTool_tool_fallback_length(PointerRNA *ptr)
 | 
			
		||||
{
 | 
			
		||||
  bToolRef *tref = ptr->data;
 | 
			
		||||
  return tref->runtime ? strlen(tref->runtime->idname_fallback) : 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#else /* RNA_RUNTIME */
 | 
			
		||||
 | 
			
		||||
static void rna_def_workspace_owner(BlenderRNA *brna)
 | 
			
		||||
@@ -270,6 +258,10 @@ static void rna_def_workspace_tool(BlenderRNA *brna)
 | 
			
		||||
  RNA_def_property_ui_text(prop, "Identifier", "");
 | 
			
		||||
  RNA_def_struct_name_property(srna, prop);
 | 
			
		||||
 | 
			
		||||
  prop = RNA_def_property(srna, "idname_fallback", PROP_STRING, PROP_NONE);
 | 
			
		||||
  RNA_def_property_ui_text(prop, "Identifier Fallback", "");
 | 
			
		||||
  RNA_def_struct_name_property(srna, prop);
 | 
			
		||||
 | 
			
		||||
  prop = RNA_def_property(srna, "index", PROP_INT, PROP_NONE);
 | 
			
		||||
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
 | 
			
		||||
  RNA_def_property_ui_text(prop, "Index", "");
 | 
			
		||||
@@ -300,14 +292,6 @@ static void rna_def_workspace_tool(BlenderRNA *brna)
 | 
			
		||||
  RNA_def_property_ui_text(prop, "Widget", "");
 | 
			
		||||
  RNA_def_property_string_funcs(
 | 
			
		||||
      prop, "rna_WorkSpaceTool_widget_get", "rna_WorkSpaceTool_widget_length", NULL);
 | 
			
		||||
  RNA_define_verify_sdna(1);
 | 
			
		||||
 | 
			
		||||
  prop = RNA_def_property(srna, "tool_fallback", PROP_STRING, PROP_NONE);
 | 
			
		||||
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
 | 
			
		||||
  RNA_def_property_ui_text(prop, "Fallback", "");
 | 
			
		||||
  RNA_def_property_string_funcs(
 | 
			
		||||
      prop, "rna_WorkSpaceTool_tool_fallback_get", "rna_WorkSpaceTool_tool_fallback_length", NULL);
 | 
			
		||||
  RNA_define_verify_sdna(1);
 | 
			
		||||
 | 
			
		||||
  RNA_api_workspace_tool(srna);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -63,7 +63,9 @@ static void rna_WorkSpaceTool_setup(ID *id,
 | 
			
		||||
  STRNCPY(tref_rt.op, op_idname);
 | 
			
		||||
  tref_rt.index = index;
 | 
			
		||||
 | 
			
		||||
  STRNCPY(tref_rt.idname_fallback, idname_fallback);
 | 
			
		||||
  /* While it's logical to assign both these values from setup,
 | 
			
		||||
   * it's useful to stored this in DNA for re-use, exceptional case: write to the 'tref'. */
 | 
			
		||||
  STRNCPY(tref->idname_fallback, idname_fallback);
 | 
			
		||||
  STRNCPY(tref_rt.keymap_fallback, keymap_fallback);
 | 
			
		||||
 | 
			
		||||
  WM_toolsystem_ref_set_from_runtime(C, (WorkSpace *)id, tref, &tref_rt, idname);
 | 
			
		||||
 
 | 
			
		||||
@@ -366,7 +366,7 @@ void WM_toolsystem_ref_set_from_runtime(struct bContext *C,
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (use_fallback_keymap == false) {
 | 
			
		||||
    tref->runtime->idname_fallback[0] = '\0';
 | 
			
		||||
    tref->idname_fallback[0] = '\0';
 | 
			
		||||
    tref->runtime->keymap_fallback[0] = '\0';
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user