2017-11-27 18:15:47 +11:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2018-04-22 08:44:23 +02:00
|
|
|
* of the License, or (at your option) any later version.
|
2017-11-27 18:15:47 +11:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file blender/windowmanager/intern/wm_toolsystem.c
|
|
|
|
* \ingroup wm
|
|
|
|
*
|
|
|
|
* Experimental tool-system>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-05-03 07:31:55 +02:00
|
|
|
#include "CLG_log.h"
|
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2017-11-27 18:15:47 +11:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
#include "BLI_string.h"
|
2018-05-16 18:41:11 +02:00
|
|
|
#include "BLI_listbase.h"
|
2017-11-27 18:15:47 +11:00
|
|
|
|
|
|
|
#include "DNA_ID.h"
|
|
|
|
#include "DNA_scene_types.h"
|
2018-05-16 18:41:11 +02:00
|
|
|
#include "DNA_space_types.h"
|
2017-11-27 18:15:47 +11:00
|
|
|
#include "DNA_windowmanager_types.h"
|
|
|
|
#include "DNA_workspace_types.h"
|
2018-05-16 18:41:11 +02:00
|
|
|
#include "DNA_object_types.h"
|
2017-11-27 18:15:47 +11:00
|
|
|
|
|
|
|
#include "BKE_context.h"
|
|
|
|
#include "BKE_library.h"
|
|
|
|
#include "BKE_main.h"
|
2018-04-29 14:30:09 +02:00
|
|
|
#include "BKE_paint.h"
|
2018-04-30 20:07:32 +02:00
|
|
|
#include "BKE_workspace.h"
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-04-27 08:24:20 +02:00
|
|
|
#include "RNA_access.h"
|
|
|
|
|
2017-11-27 18:15:47 +11:00
|
|
|
#include "WM_api.h"
|
|
|
|
#include "WM_types.h"
|
2018-04-27 08:24:20 +02:00
|
|
|
#include "WM_message.h"
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Tool Reference API
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
struct bToolRef *WM_toolsystem_ref_from_context(struct bContext *C)
|
2017-11-27 18:15:47 +11:00
|
|
|
{
|
2018-05-16 18:41:11 +02:00
|
|
|
WorkSpace *workspace = CTX_wm_workspace(C);
|
|
|
|
Scene *scene = CTX_data_scene(C);
|
|
|
|
ScrArea *sa = CTX_wm_area(C);
|
|
|
|
const bToolKey tkey = {
|
|
|
|
.space_type = sa->spacetype,
|
|
|
|
.mode = WM_toolsystem_mode_from_spacetype(workspace, scene, sa, sa->spacetype),
|
|
|
|
};
|
|
|
|
return WM_toolsystem_ref_find(workspace, &tkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bToolRef_Runtime *WM_toolsystem_runtime_from_context(struct bContext *C)
|
|
|
|
{
|
|
|
|
bToolRef *tref = WM_toolsystem_ref_from_context(C);
|
|
|
|
return tref ? tref->runtime : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bToolRef *WM_toolsystem_ref_find(WorkSpace *workspace, const bToolKey *tkey)
|
|
|
|
{
|
|
|
|
LISTBASE_FOREACH (bToolRef *, tref, &workspace->tools) {
|
|
|
|
if ((tref->space_type == tkey->space_type) &&
|
|
|
|
(tref->mode == tkey->mode))
|
|
|
|
{
|
|
|
|
return tref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bToolRef_Runtime *WM_toolsystem_runtime_find(WorkSpace *workspace, const bToolKey *tkey)
|
|
|
|
{
|
|
|
|
bToolRef *tref = WM_toolsystem_ref_find(workspace, tkey);
|
|
|
|
return tref ? tref->runtime : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WM_toolsystem_ref_ensure(
|
|
|
|
struct WorkSpace *workspace, const bToolKey *tkey,
|
|
|
|
bToolRef **r_tref)
|
|
|
|
{
|
|
|
|
bToolRef *tref = WM_toolsystem_ref_find(workspace, tkey);
|
|
|
|
if (tref) {
|
|
|
|
*r_tref = tref;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tref = MEM_callocN(sizeof(*tref), __func__);
|
|
|
|
BLI_addhead(&workspace->tools, tref);
|
|
|
|
tref->space_type = tkey->space_type;
|
|
|
|
tref->mode = tkey->mode;
|
|
|
|
*r_tref = tref;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
|
|
static void toolsystem_unlink_ref(bContext *C, WorkSpace *workspace, bToolRef *tref)
|
|
|
|
{
|
|
|
|
bToolRef_Runtime *tref_rt = tref->runtime;
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
if (tref_rt->manipulator_group[0]) {
|
|
|
|
wmManipulatorGroupType *wgt = WM_manipulatorgrouptype_find(tref_rt->manipulator_group, false);
|
2017-11-27 18:15:47 +11:00
|
|
|
if (wgt != NULL) {
|
|
|
|
bool found = false;
|
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
/* TODO(campbell) */
|
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
#if 0
|
|
|
|
wmWindowManager *wm = bmain->wm.first;
|
2017-11-27 18:15:47 +11:00
|
|
|
/* Check another workspace isn't using this tool. */
|
|
|
|
for (wmWindow *win = wm->windows.first; win; win = win->next) {
|
|
|
|
const WorkSpace *workspace_iter = WM_window_get_active_workspace(win);
|
|
|
|
if (workspace != workspace_iter) {
|
|
|
|
if (STREQ(workspace->tool.manipulator_group, workspace_iter->tool.manipulator_group)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 18:41:11 +02:00
|
|
|
#else
|
|
|
|
UNUSED_VARS(workspace);
|
|
|
|
#endif
|
2017-11-27 18:15:47 +11:00
|
|
|
if (!found) {
|
|
|
|
wmManipulatorMapType *mmap_type = WM_manipulatormaptype_ensure(&wgt->mmap_params);
|
|
|
|
WM_manipulatormaptype_group_unlink(C, bmain, mmap_type, wgt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 18:41:11 +02:00
|
|
|
void WM_toolsystem_unlink(bContext *C, WorkSpace *workspace, const bToolKey *tkey)
|
|
|
|
{
|
|
|
|
bToolRef *tref = WM_toolsystem_ref_find(workspace, tkey);
|
|
|
|
if (tref && tref->runtime) {
|
|
|
|
toolsystem_unlink_ref(C, workspace, tref);
|
|
|
|
}
|
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
static void toolsystem_ref_link(bContext *C, WorkSpace *workspace, bToolRef *tref)
|
2017-11-27 18:15:47 +11:00
|
|
|
{
|
2018-05-16 18:41:11 +02:00
|
|
|
bToolRef_Runtime *tref_rt = tref->runtime;
|
|
|
|
if (tref_rt->manipulator_group[0]) {
|
|
|
|
const char *idname = tref_rt->manipulator_group;
|
2018-05-03 07:31:55 +02:00
|
|
|
wmManipulatorGroupType *wgt = WM_manipulatorgrouptype_find(idname, false);
|
|
|
|
if (wgt != NULL) {
|
|
|
|
WM_manipulator_group_type_ensure_ptr(wgt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CLOG_WARN(WM_LOG_TOOLS, "'%s' widget not found", idname);
|
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
}
|
2018-04-30 20:07:32 +02:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
if (tref_rt->data_block[0]) {
|
2018-04-30 20:07:32 +02:00
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
|
2018-04-29 14:30:09 +02:00
|
|
|
/* Currently only brush data-blocks supported. */
|
2018-05-16 18:41:11 +02:00
|
|
|
struct Brush *brush = (struct Brush *)BKE_libblock_find_name(ID_BR, tref_rt->data_block);
|
2018-04-30 20:07:32 +02:00
|
|
|
|
|
|
|
if (brush) {
|
|
|
|
wmWindowManager *wm = bmain->wm.first;
|
|
|
|
for (wmWindow *win = wm->windows.first; win; win = win->next) {
|
|
|
|
if (workspace == WM_window_get_active_workspace(win)) {
|
|
|
|
Scene *scene = win->scene;
|
|
|
|
ViewLayer *view_layer = BKE_workspace_view_layer_get(workspace, scene);
|
|
|
|
Paint *paint = BKE_paint_get_active(scene, view_layer);
|
|
|
|
if (paint) {
|
|
|
|
if (brush) {
|
|
|
|
BKE_paint_brush_set(paint, brush);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-29 19:14:51 +02:00
|
|
|
}
|
2018-04-29 14:30:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
}
|
2018-05-16 18:41:11 +02:00
|
|
|
void WM_toolsystem_link(bContext *C, WorkSpace *workspace, const bToolKey *tkey)
|
|
|
|
{
|
|
|
|
bToolRef *tref = WM_toolsystem_ref_find(workspace, tkey);
|
|
|
|
if (tref) {
|
|
|
|
toolsystem_ref_link(C, workspace, tref);
|
|
|
|
}
|
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
static void toolsystem_refresh_ref(bContext *C, WorkSpace *workspace, bToolRef *tref)
|
|
|
|
{
|
|
|
|
/* currently same operation. */
|
|
|
|
toolsystem_ref_link(C, workspace, tref);
|
|
|
|
}
|
|
|
|
void WM_toolsystem_refresh(bContext *C, WorkSpace *workspace, const bToolKey *tkey)
|
2018-05-08 07:25:36 +02:00
|
|
|
{
|
2018-05-16 18:41:11 +02:00
|
|
|
bToolRef *tref = WM_toolsystem_ref_find(workspace, tkey);
|
|
|
|
if (tref) {
|
|
|
|
toolsystem_refresh_ref(C, workspace, tref);
|
|
|
|
}
|
2018-05-08 07:25:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
/* Operate on all active tools. */
|
|
|
|
void WM_toolsystem_unlink_all(struct bContext *C, struct WorkSpace *workspace)
|
2017-11-27 18:15:47 +11:00
|
|
|
{
|
2018-05-16 18:41:11 +02:00
|
|
|
LISTBASE_FOREACH (bToolRef *, tref, &workspace->tools) {
|
|
|
|
if (tref->runtime) {
|
|
|
|
toolsystem_unlink_ref(C, workspace, tref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void WM_toolsystem_link_all(struct bContext *C, struct WorkSpace *workspace)
|
|
|
|
{
|
|
|
|
LISTBASE_FOREACH (bToolRef *, tref, &workspace->tools) {
|
|
|
|
if (tref->runtime) {
|
|
|
|
toolsystem_ref_link(C, workspace, tref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void WM_toolsystem_refresh_all(struct bContext *C, struct WorkSpace *workspace)
|
|
|
|
{
|
|
|
|
LISTBASE_FOREACH (bToolRef *, tref, &workspace->tools) {
|
|
|
|
if (tref->runtime) {
|
|
|
|
toolsystem_refresh_ref(C, workspace, tref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WM_toolsystem_ref_set_from_runtime(
|
|
|
|
struct bContext *C, struct WorkSpace *workspace, bToolRef *tref,
|
|
|
|
const bToolRef_Runtime *tref_rt, const char *idname)
|
|
|
|
{
|
|
|
|
if (tref->runtime) {
|
|
|
|
toolsystem_unlink_ref(C, workspace, tref);
|
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
STRNCPY(tref->idname, idname);
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
/* BAD DESIGN WARNING: used for topbar. */
|
|
|
|
workspace->tools_space_type = tref->space_type;
|
|
|
|
workspace->tools_mode = tref->mode;
|
2017-11-27 18:15:47 +11:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
if (tref->runtime == NULL) {
|
|
|
|
tref->runtime = MEM_callocN(sizeof(*tref->runtime), __func__);
|
2017-11-27 18:15:47 +11:00
|
|
|
}
|
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
if (tref_rt != tref->runtime) {
|
|
|
|
*tref->runtime = *tref_rt;
|
|
|
|
}
|
2018-04-27 08:24:20 +02:00
|
|
|
|
2018-05-16 18:41:11 +02:00
|
|
|
toolsystem_ref_link(C, workspace, tref);
|
|
|
|
|
|
|
|
/* TODO(campbell): fix message. */
|
2018-04-27 08:24:20 +02:00
|
|
|
{
|
|
|
|
struct wmMsgBus *mbus = CTX_wm_message_bus(C);
|
|
|
|
WM_msg_publish_rna_prop(
|
2018-05-16 18:41:11 +02:00
|
|
|
mbus, &workspace->id, workspace, WorkSpace, tools);
|
2018-04-27 08:24:20 +02:00
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM_toolsystem_init(bContext *C)
|
|
|
|
{
|
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
wmWindowManager *wm = bmain->wm.first;
|
|
|
|
|
|
|
|
for (wmWindow *win = wm->windows.first; win; win = win->next) {
|
|
|
|
WorkSpace *workspace = WM_window_get_active_workspace(win);
|
2018-05-16 18:41:11 +02:00
|
|
|
WM_toolsystem_link_all(C, workspace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int WM_toolsystem_mode_from_spacetype(
|
|
|
|
WorkSpace *workspace, Scene *scene, ScrArea *sa,
|
|
|
|
int spacetype)
|
|
|
|
{
|
|
|
|
int mode = -1;
|
|
|
|
switch (spacetype) {
|
|
|
|
case SPACE_VIEW3D:
|
|
|
|
{
|
|
|
|
/* 'sa' may be NULL in this case. */
|
|
|
|
ViewLayer *view_layer = BKE_workspace_view_layer_get(workspace, scene);
|
|
|
|
Object *obact = OBACT(view_layer);
|
2018-05-17 20:28:14 +02:00
|
|
|
if (obact != NULL) {
|
|
|
|
Object *obedit = OBEDIT_FROM_OBACT(obact);
|
|
|
|
mode = CTX_data_mode_enum_ex(obedit, obact, obact->mode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = CTX_MODE_OBJECT;
|
|
|
|
}
|
2018-05-16 18:41:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SPACE_IMAGE:
|
|
|
|
{
|
|
|
|
SpaceImage *sima = sa->spacedata.first;
|
|
|
|
mode = sima->mode;
|
|
|
|
break;
|
|
|
|
}
|
2017-11-27 18:15:47 +11:00
|
|
|
}
|
2018-05-16 18:41:11 +02:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WM_toolsystem_key_from_context(
|
|
|
|
WorkSpace *workspace, Scene *scene, ScrArea *sa,
|
|
|
|
bToolKey *tkey)
|
|
|
|
{
|
|
|
|
int space_type = SPACE_EMPTY;
|
|
|
|
int mode = -1;
|
|
|
|
|
|
|
|
if (sa != NULL) {
|
|
|
|
space_type = sa->spacetype;
|
|
|
|
mode = WM_toolsystem_mode_from_spacetype(workspace, scene, sa, space_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != -1) {
|
|
|
|
tkey->space_type = space_type;
|
|
|
|
tkey->mode = mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run after changing modes.
|
|
|
|
*/
|
|
|
|
static void toolsystem_update_with_toolref(
|
|
|
|
bContext *C, WorkSpace *workspace, const bToolKey *tkey, const char *default_tool)
|
|
|
|
{
|
|
|
|
bToolRef *tref;
|
|
|
|
if (WM_toolsystem_ref_ensure(workspace, tkey, &tref)) {
|
|
|
|
STRNCPY(tref->idname, default_tool);
|
|
|
|
}
|
|
|
|
|
|
|
|
wmOperatorType *ot = WM_operatortype_find("WM_OT_tool_set_by_name", false);
|
|
|
|
PointerRNA op_props;
|
|
|
|
WM_operator_properties_create_ptr(&op_props, ot);
|
|
|
|
RNA_string_set(&op_props, "name", tref->idname);
|
|
|
|
RNA_enum_set(&op_props, "space_type", tkey->space_type);
|
|
|
|
WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &op_props);
|
|
|
|
WM_operator_properties_free(&op_props);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WM_toolsystem_update_from_context_view3d(bContext *C)
|
|
|
|
{
|
|
|
|
WorkSpace *workspace = CTX_wm_workspace(C);
|
|
|
|
Scene *scene = CTX_data_scene(C);
|
|
|
|
int space_type = SPACE_VIEW3D;
|
|
|
|
const bToolKey tkey = {
|
|
|
|
.space_type = space_type,
|
|
|
|
.mode = WM_toolsystem_mode_from_spacetype(workspace, scene, NULL, space_type),
|
|
|
|
};
|
|
|
|
toolsystem_update_with_toolref(C, workspace, &tkey, "Cursor");
|
2017-11-27 18:15:47 +11:00
|
|
|
}
|
2018-05-01 11:22:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For paint modes to support non-brush tools.
|
|
|
|
*/
|
|
|
|
bool WM_toolsystem_active_tool_is_brush(const bContext *C)
|
|
|
|
{
|
2018-05-16 18:41:11 +02:00
|
|
|
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C);
|
|
|
|
return tref_rt->data_block[0] != '\0';
|
2018-05-01 11:22:12 +02:00
|
|
|
}
|
2018-05-08 07:25:36 +02:00
|
|
|
|
|
|
|
/* Follow wmMsgNotifyFn spec */
|
|
|
|
void WM_toolsystem_do_msg_notify_tag_refresh(
|
2018-05-16 18:41:11 +02:00
|
|
|
bContext *C, wmMsgSubscribeKey *UNUSED(msg_key), wmMsgSubscribeValue *msg_val)
|
2018-05-08 07:25:36 +02:00
|
|
|
{
|
|
|
|
WorkSpace *workspace = CTX_wm_workspace(C);
|
2018-05-16 18:41:11 +02:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
|
|
|
ScrArea *sa = msg_val->user_data;
|
|
|
|
int space_type = sa->spacetype;
|
|
|
|
const bToolKey tkey = {
|
|
|
|
.space_type = space_type,
|
|
|
|
.mode = WM_toolsystem_mode_from_spacetype(workspace, scene, sa, sa->spacetype),
|
|
|
|
};
|
|
|
|
WM_toolsystem_refresh(C, workspace, &tkey);
|
2018-05-08 07:25:36 +02:00
|
|
|
}
|