Tool System: store the active tool in ScrArea
authorCampbell Barton <ideasman42@gmail.com>
Fri, 18 May 2018 07:35:10 +0000 (09:35 +0200)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 18 May 2018 07:40:52 +0000 (09:40 +0200)
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.

source/blender/blenloader/intern/readfile.c
source/blender/editors/mesh/editmesh_extrude.c
source/blender/editors/screen/area.c
source/blender/editors/transform/transform_manipulator_3d.c
source/blender/makesdna/DNA_screen_types.h
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_event_system.c
source/blender/windowmanager/intern/wm_toolsystem.c

index 3d1707f2e77ab8e925793c2e53a9282d4cfdeca0..85c67047be0358a55eeb2e3a378e8a604899115a 100644 (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:
index 95bf8e89b0edb1faf7e99d7d03cdaa72b3ed5525..4841de3c856911ea91d420e83a60d68ff167deee 100644 (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))
index 24d6b7c6ecf997f88ac433e8edab6dcd786f3a45..4c5ba38984b1f4332a47f5565a5a040ab63860d4 100644 (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)
index c24291953c8dc4a6644da585f04c4e9fd409d727..8b635bb26dbac2bb9f7a79c0dfe1b3b6fa0f6747 100644 (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;
 
index 395fcddb8615a216886e4ceb1e2eaa3502165a2f..1f14bb58e7861db72e73a7f8561610a0e4a9fdd9 100644 (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 {
index 39132184371e0d41c5ff23f3c69d2e9bf0fc746b..afa6e71e61043e5bb7dbd998cb16ed02daca433f 100644 (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
index 8bbbd1b60dfcce4ba270a12a2a8914795095c9b0..ad10475906da9de452ea6591fe24c17a4a0faaec 100644 (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);
index 93481dcd56c1a4fe5e6dd820492bc587770ea301..122e292985a56a39cf215fb4788ca1f83ce20581 100644 (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.
- */
+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 *UNUSED(workspace), const bToolRef *tref)
+        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)
 {