Tool System: set a default tool for each mode

Also clear tools for the default startup file
so changes to defaults apply to new files.
This commit is contained in:
Campbell Barton 2018-09-27 11:20:27 +10:00
parent 6ebe211644
commit ed83075402
Notes: blender-bot 2023-02-14 05:53:37 +01:00
Referenced by issue #57227, Hair Edit General Todo
Referenced by issue #55036, Tool System Todo
4 changed files with 75 additions and 14 deletions

View File

@ -28,6 +28,7 @@
#include "BLI_compiler_attrs.h"
struct bScreen;
struct bToolRef;
struct Main;
struct Scene;
struct TransformOrientation;
@ -70,6 +71,8 @@ struct WorkSpaceLayout *BKE_workspace_layout_iter_circular(
bool (*callback)(const struct WorkSpaceLayout *layout, void *arg),
void *arg, const bool iter_backward);
void BKE_workspace_tool_remove(
struct WorkSpace *workspace, struct bToolRef *tref) ATTR_NONNULL(1, 2);
/* -------------------------------------------------------------------- */
/* Getters/Setters */

View File

@ -161,17 +161,9 @@ void BKE_workspace_free(WorkSpace *workspace)
BLI_freelistN(&workspace->owner_ids);
BLI_freelistN(&workspace->layouts);
for (bToolRef *tref = workspace->tools.first, *tref_next; tref; tref = tref_next) {
tref_next = tref->next;
if (tref->runtime) {
MEM_freeN(tref->runtime);
}
if (tref->properties) {
IDP_FreeProperty(tref->properties);
MEM_freeN(tref->properties);
}
while (!BLI_listbase_is_empty(&workspace->tools)) {
BKE_workspace_tool_remove(workspace, workspace->tools.first);
}
BLI_freelistN(&workspace->tools);
if (workspace->status_text) {
MEM_freeN(workspace->status_text);
@ -352,6 +344,19 @@ WorkSpaceLayout *BKE_workspace_layout_iter_circular(
return NULL;
}
void BKE_workspace_tool_remove(
struct WorkSpace *workspace, struct bToolRef *tref)
{
if (tref->runtime) {
MEM_freeN(tref->runtime);
}
if (tref->properties) {
IDP_FreeProperty(tref->properties);
MEM_freeN(tref->properties);
}
BLI_remlink(&workspace->tools, tref);
MEM_freeN(tref);
}
/* -------------------------------------------------------------------- */
/* Getters/Setters */

View File

@ -45,6 +45,7 @@
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_screen.h"
#include "BKE_workspace.h"
#include "BLO_readfile.h"
@ -94,6 +95,15 @@ void BLO_update_defaults_startup_blend(Main *bmain, const char *app_template)
}
}
if (app_template == NULL) {
/* Clear all tools to use default options instead, ignore the tool saved in the file. */
for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
while (!BLI_listbase_is_empty(&workspace->tools)) {
BKE_workspace_tool_remove(workspace, workspace->tools.first);
}
}
}
/* For 2D animation template. */
if (app_template && STREQ(app_template, "2D_Animation")) {
for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {

View File

@ -58,7 +58,7 @@
static void toolsystem_reinit_with_toolref(
bContext *C, WorkSpace *UNUSED(workspace), bToolRef *tref);
static void toolsystem_reinit_ensure_toolref(
static bToolRef *toolsystem_reinit_ensure_toolref(
bContext *C, WorkSpace *workspace, const bToolKey *tkey, const char *default_tool);
static void toolsystem_refresh_screen_from_active_tool(
Main *bmain, WorkSpace *workspace, bToolRef *tref);
@ -364,6 +364,11 @@ void WM_toolsystem_init(bContext *C)
tref->tag = 1;
}
}
else {
/* Without this we may load a file without a default tool. */
tref = toolsystem_reinit_ensure_toolref(C, workspace, &tkey, NULL);
tref->tag = 1;
}
}
CTX_wm_window_set(C, NULL);
}
@ -526,18 +531,56 @@ static void toolsystem_reinit_with_toolref(
WM_toolsystem_ref_set_by_name(C, workspace, &tkey, tref->idname, false);
}
static const char *toolsystem_default_tool(const bToolKey *tkey)
{
switch (tkey->space_type) {
case SPACE_VIEW3D:
switch (tkey->mode) {
/* XXX(campbell): hard coded paint-brush names.
* Eventyally we plan to move away from using brush names as tools,
* in favor of having tool types in the toolbar, which can each select their own brush.
* so keep this as a temporary hack.
*/
case CTX_MODE_SCULPT:
return "SculptDraw";
case CTX_MODE_PAINT_VERTEX:
case CTX_MODE_PAINT_WEIGHT:
case CTX_MODE_GPENCIL_WEIGHT:
return "Draw";
case CTX_MODE_PAINT_TEXTURE:
return "TexDraw";
case CTX_MODE_GPENCIL_PAINT:
return "Draw Pencil";
case CTX_MODE_GPENCIL_SCULPT:
return "Push";
/* end temporary hack. */
case CTX_MODE_PARTICLE:
return "Comb";
default:
return "Select Border";
}
break;
}
return "Cursor";
}
/**
* Run after changing modes.
*/
static void toolsystem_reinit_ensure_toolref(
static bToolRef *toolsystem_reinit_ensure_toolref(
bContext *C, WorkSpace *workspace, const bToolKey *tkey, const char *default_tool)
{
bToolRef *tref;
if (WM_toolsystem_ref_ensure(workspace, tkey, &tref)) {
if (default_tool == NULL) {
default_tool = toolsystem_default_tool(tkey);
}
STRNCPY(tref->idname, default_tool);
}
toolsystem_reinit_with_toolref(C, workspace, tref);
return tref;
}
void WM_toolsystem_update_from_context_view3d(bContext *C)
@ -549,7 +592,7 @@ void WM_toolsystem_update_from_context_view3d(bContext *C)
.space_type = space_type,
.mode = WM_toolsystem_mode_from_spacetype(view_layer, NULL, space_type),
};
toolsystem_reinit_ensure_toolref(C, workspace, &tkey, "Cursor");
toolsystem_reinit_ensure_toolref(C, workspace, &tkey, NULL);
}
/**