Tool System: store the active tool in ScrArea

Without this we need to have the context to get the
(space_type, mode) args for an active tool lookup.

For event handling & poll its more convenient to have direct access.
This commit is contained in:
Campbell Barton
2018-05-18 09:35:10 +02:00
parent 8faa594413
commit 987d1df571
8 changed files with 101 additions and 25 deletions

View File

@@ -6603,6 +6603,8 @@ static void lib_link_area(FileData *fd, ID *parent_id, ScrArea *area)
{
area->full = newlibadr(fd, parent_id->lib, area->full);
memset(&area->runtime, 0x0, sizeof(area->runtime));
for (SpaceLink *sl = area->spacedata.first; sl; sl= sl->next) {
switch (sl->spacetype) {
case SPACE_VIEW3D:

View File

@@ -419,9 +419,8 @@ static void manipulator_mesh_extrude_orientation_matrix_set(
static bool manipulator_mesh_extrude_poll(const bContext *C, wmManipulatorGroupType *wgt)
{
WorkSpace *workspace = CTX_wm_workspace(C);
const bToolKey tkey = { .space_type = SPACE_VIEW3D, .mode = CTX_MODE_EDIT_MESH, };
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_find(workspace, &tkey);
ScrArea *sa = CTX_wm_area(C);
bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
if ((tref_rt == NULL) ||
!STREQ(wgt->idname, tref_rt->manipulator_group) ||
!ED_operator_editmesh_view3d((bContext *)C))

View File

@@ -1393,7 +1393,10 @@ void ED_area_update_region_sizes(wmWindowManager *wm, wmWindow *win, ScrArea *ar
/* called in screen_refresh, or screens_init, also area size changes */
void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
{
const bScreen *screen = WM_window_get_active_screen(win);
WorkSpace *workspace = WM_window_get_active_workspace(win);
const bScreen *screen = BKE_workspace_active_screen_get(win->workspace_hook);
Scene *scene = WM_window_get_active_scene(win);
const int window_size_x = WM_window_pixels_x(win);
const int window_size_y = WM_window_pixels_y(win);
ARegion *ar;
@@ -1452,6 +1455,8 @@ void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
/* Some AZones use View2D data which is only updated in region init, so call that first! */
region_azones_add(screen, sa, ar, ar->alignment & ~RGN_SPLIT_PREV);
}
WM_toolsystem_refresh_screen_area(workspace, scene, sa);
}
static void region_update_rect(ARegion *ar)

View File

@@ -1282,15 +1282,9 @@ static void WIDGETGROUP_manipulator_setup(const bContext *C, wmManipulatorGroup
{
/* TODO: support mixing modes again? - it's supported but tool system makes it unobvious. */
man->twtype = 0;
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, NULL, sa->spacetype),
};
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_find(workspace, &tkey);
wmKeyMap *km = WM_keymap_find_all(C, tref_rt->keymap, sa->spacetype, RGN_TYPE_WINDOW);
bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
wmKeyMap *km = tref_rt ? WM_keymap_find_all(C, tref_rt->keymap, sa->spacetype, RGN_TYPE_WINDOW) : NULL;
/* Weak, check first event */
wmKeyMapItem *kmi = km ? km->items.first : NULL;

View File

@@ -248,6 +248,12 @@ enum GlobalAreaFlag {
GLOBAL_AREA_IS_HIDDEN = (1 << 0),
};
typedef struct ScrArea_Runtime {
struct bToolRef *tool;
char is_tool_set;
char _pad0[7];
} ScrArea_Runtime;
typedef struct ScrArea {
struct ScrArea *next, *prev;
@@ -288,6 +294,8 @@ typedef struct ScrArea {
ListBase handlers; /* wmEventHandler */
ListBase actionzones; /* AZone */
ScrArea_Runtime runtime;
} ScrArea;
typedef struct ARegion {

View File

@@ -653,6 +653,9 @@ void WM_tooltip_clear(struct bContext *C, struct wmWindow *win);
void WM_tooltip_init(struct bContext *C, struct wmWindow *win);
void WM_tooltip_refresh(struct bContext *C, struct wmWindow *win);
void WM_toolsystem_refresh_screen_area(struct WorkSpace *workspace, struct Scene *scene, struct ScrArea *sa);
void WM_toolsystem_refresh_screen_all(struct Main *bmain);
#ifdef __cplusplus
}
#endif

View File

@@ -2874,12 +2874,7 @@ void wm_event_do_handlers(bContext *C)
*/
wmEventHandler sneaky_handler = {NULL};
if (ar->regiontype == RGN_TYPE_WINDOW) {
WorkSpace *workspace = WM_window_get_active_workspace(win);
const bToolKey tkey = {
.space_type = sa->spacetype,
.mode = WM_toolsystem_mode_from_spacetype(workspace, win->scene, sa, sa->spacetype),
};
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_find(workspace, &tkey);
bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
if (tref_rt && tref_rt->keymap[0]) {
wmKeyMap *km = WM_keymap_find_all(
C, tref_rt->keymap, sa->spacetype, RGN_TYPE_WINDOW);

View File

@@ -54,7 +54,7 @@
#include "WM_message.h"
static void toolsystem_reinit_with_toolref(
bContext *C, WorkSpace *UNUSED(workspace), const bToolRef *tref);
bContext *C, WorkSpace *UNUSED(workspace), bToolRef *tref);
static void toolsystem_reinit_ensure_toolref(
bContext *C, WorkSpace *workspace, const bToolKey *tkey, const char *default_tool);
@@ -71,7 +71,12 @@ struct bToolRef *WM_toolsystem_ref_from_context(struct bContext *C)
.space_type = sa->spacetype,
.mode = WM_toolsystem_mode_from_spacetype(workspace, scene, sa, sa->spacetype),
};
return WM_toolsystem_ref_find(workspace, &tkey);
bToolRef *tref = WM_toolsystem_ref_find(workspace, &tkey);
/* We could return 'sa->runtime.tool' in this case. */
if (sa->runtime.is_tool_set) {
BLI_assert(tref == sa->runtime.tool);
}
return tref;
}
struct bToolRef_Runtime *WM_toolsystem_runtime_from_context(struct bContext *C)
@@ -357,12 +362,71 @@ bool WM_toolsystem_key_from_context(
return false;
}
/**
* Run after changing modes.
*/
static void toolsystem_reinit_with_toolref(
bContext *C, WorkSpace *UNUSED(workspace), const bToolRef *tref)
void WM_toolsystem_refresh_screen_area(WorkSpace *workspace, Scene *scene, ScrArea *sa)
{
sa->runtime.tool = NULL;
sa->runtime.is_tool_set = true;
const int mode = WM_toolsystem_mode_from_spacetype(workspace, scene, sa, sa->spacetype);
for (bToolRef *tref = workspace->tools.first; tref; tref = tref->next) {
if ((tref->space_type == sa->spacetype)) {
if (tref->mode == mode) {
sa->runtime.tool = tref;
break;
}
}
}
}
void WM_toolsystem_refresh_screen_all(Main *bmain)
{
/* Update all ScrArea's tools */
for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
for (wmWindow *win = wm->windows.first; win; win = win->next) {
WorkSpace *workspace = WM_window_get_active_workspace(win);
bool space_type_has_tools[SPACE_TYPE_LAST + 1] = {0};
for (bToolRef *tref = workspace->tools.first; tref; tref = tref->next) {
space_type_has_tools[tref->space_type] = true;
}
bScreen *screen = WM_window_get_active_screen(win);
Scene *scene = WM_window_get_active_scene(win);
for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
sa->runtime.tool = NULL;
sa->runtime.is_tool_set = true;
if (space_type_has_tools[sa->spacetype]) {
WM_toolsystem_refresh_screen_area(workspace, scene, sa);
}
}
}
}
}
static void toolsystem_refresh_screen_from_active_tool(
Main *bmain, WorkSpace *workspace, bToolRef *tref)
{
/* Update all ScrArea's tools */
for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
for (wmWindow *win = wm->windows.first; win; win = win->next) {
if (workspace == WM_window_get_active_workspace(win)) {
bScreen *screen = WM_window_get_active_screen(win);
Scene *scene = WM_window_get_active_scene(win);
for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
if (sa->spacetype == tref->space_type) {
int mode = WM_toolsystem_mode_from_spacetype(workspace, scene, sa, sa->spacetype);
if (mode == tref->mode) {
sa->runtime.tool = tref;
sa->runtime.is_tool_set = true;
}
}
}
}
}
}
}
static void toolsystem_reinit_with_toolref(
bContext *C, WorkSpace *workspace, bToolRef *tref)
{
wmOperatorType *ot = WM_operatortype_find("WM_OT_tool_set_by_name", false);
/* On startup, Python operatores are not yet loaded. */
if (ot == NULL) {
@@ -374,8 +438,14 @@ static void toolsystem_reinit_with_toolref(
RNA_enum_set(&op_props, "space_type", tref->space_type);
WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &op_props);
WM_operator_properties_free(&op_props);
Main *bmain = CTX_data_main(C);
toolsystem_refresh_screen_from_active_tool(bmain, workspace, tref);
}
/**
* Run after changing modes.
*/
static void toolsystem_reinit_ensure_toolref(
bContext *C, WorkSpace *workspace, const bToolKey *tkey, const char *default_tool)
{