From 3c8c1841d72d8b9b13799c9a12cd8637b38be51d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 4 Oct 2018 08:54:48 +1000 Subject: UI: use shared toolbar region callbacks Image toolbar now starts with a single column width and snaps to button increments. --- release/scripts/startup/bl_ui/space_image.py | 2 +- source/blender/editors/include/ED_screen.h | 8 ++ source/blender/editors/screen/CMakeLists.txt | 1 + source/blender/editors/screen/area_utils.c | 89 ++++++++++++++++++++++ source/blender/editors/space_image/space_image.c | 21 +---- source/blender/editors/space_view3d/space_view3d.c | 39 +--------- 6 files changed, 105 insertions(+), 55 deletions(-) create mode 100644 source/blender/editors/screen/area_utils.c diff --git a/release/scripts/startup/bl_ui/space_image.py b/release/scripts/startup/bl_ui/space_image.py index 31ff6e4e5fc..8fe5761cc33 100644 --- a/release/scripts/startup/bl_ui/space_image.py +++ b/release/scripts/startup/bl_ui/space_image.py @@ -356,7 +356,7 @@ class IMAGE_MT_uvs_weldalign(Menu): class IMAGE_MT_uvs(Menu): - bl_label = "UVs" + bl_label = "UV" def draw(self, context): layout = self.layout diff --git a/source/blender/editors/include/ED_screen.h b/source/blender/editors/include/ED_screen.h index 8d7b723002a..2f2f4847a6f 100644 --- a/source/blender/editors/include/ED_screen.h +++ b/source/blender/editors/include/ED_screen.h @@ -342,6 +342,14 @@ void ED_region_cache_draw_background(const struct ARegion *ar); void ED_region_cache_draw_curfra_label(const int framenr, const float x, const float y); void ED_region_cache_draw_cached_segments(const struct ARegion *ar, const int num_segments, const int *points, const int sfra, const int efra); +/* area_utils.c */ +void ED_region_generic_tools_region_message_subscribe( + const struct bContext *C, + struct WorkSpace *workspace, struct Scene *scene, + struct bScreen *screen, struct ScrArea *sa, struct ARegion *ar, + struct wmMsgBus *mbus); +int ED_region_generic_tools_region_snap_size(const struct ARegion *ar, int size, int axis); + /* interface_region_hud.c */ struct ARegionType *ED_area_type_hud(int space_type); void ED_area_type_hud_clear(struct wmWindowManager *wm, ScrArea *sa_keep); diff --git a/source/blender/editors/screen/CMakeLists.txt b/source/blender/editors/screen/CMakeLists.txt index 4be65f60b21..e01be2ed709 100644 --- a/source/blender/editors/screen/CMakeLists.txt +++ b/source/blender/editors/screen/CMakeLists.txt @@ -42,6 +42,7 @@ set(INC_SYS set(SRC area.c + area_utils.c glutil.c screen_context.c screen_draw.c diff --git a/source/blender/editors/screen/area_utils.c b/source/blender/editors/screen/area_utils.c new file mode 100644 index 00000000000..53e2f96d6c4 --- /dev/null +++ b/source/blender/editors/screen/area_utils.c @@ -0,0 +1,89 @@ +/* + * ***** 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 + * of the License, or (at your option) any later version. + * + * 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/editors/screen/area_utils.c + * \ingroup edscr + * + * Helper functions for area/region API. + */ + +#include "DNA_userdef_types.h" + +#include "BLI_blenlib.h" +#include "BLI_utildefines.h" + +#include "BKE_context.h" + +#include "RNA_access.h" +#include "RNA_types.h" + +#include "WM_api.h" +#include "WM_message.h" + +#include "ED_screen.h" + +#include "UI_interface.h" + +/* -------------------------------------------------------------------- */ +/** \name Generic Tool System Region Callbacks + * \{ */ + +/** + * Callback for #ARegionType.message_subscribe + */ +void ED_region_generic_tools_region_message_subscribe( + const struct bContext *UNUSED(C), + struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene), + struct bScreen *UNUSED(screen), struct ScrArea *UNUSED(sa), struct ARegion *ar, + struct wmMsgBus *mbus) +{ + wmMsgSubscribeValue msg_sub_value_region_tag_redraw = { + .owner = ar, + .user_data = ar, + .notify = ED_region_do_msg_notify_tag_redraw, + }; + WM_msg_subscribe_rna_anon_prop(mbus, WorkSpace, tools, &msg_sub_value_region_tag_redraw); +} + +/** + * Callback for #ARegionType.snap_size + */ +int ED_region_generic_tools_region_snap_size(const ARegion *ar, int size, int axis) +{ + if (axis == 0) { + /* Note, this depends on the icon size: see #ICON_DEFAULT_HEIGHT_TOOLBAR. */ + const float snap_units[] = {2 + 0.8f, 4 + 0.8f}; + const float aspect = BLI_rctf_size_x(&ar->v2d.cur) / (BLI_rcti_size_x(&ar->v2d.mask) + 1); + int best_diff = INT_MAX; + int best_size = size; + for (uint i = 0; i < ARRAY_SIZE(snap_units); i += 1) { + const int test_size = (snap_units[i] * U.widget_unit) / (UI_DPI_FAC * aspect); + const int test_diff = ABS(test_size - size); + if (test_diff < best_diff) { + best_size = test_size; + best_diff = test_diff; + } + } + return best_size; + } + return size; +} + +/** \} */ diff --git a/source/blender/editors/space_image/space_image.c b/source/blender/editors/space_image/space_image.c index acf928fc1a5..60431adad0e 100644 --- a/source/blender/editors/space_image/space_image.c +++ b/source/blender/editors/space_image/space_image.c @@ -977,21 +977,6 @@ static void image_tools_region_listener( } } -static void image_tools_region_message_subscribe( - const struct bContext *UNUSED(C), - struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene), - struct bScreen *UNUSED(screen), struct ScrArea *UNUSED(sa), struct ARegion *ar, - struct wmMsgBus *mbus) -{ - wmMsgSubscribeValue msg_sub_value_region_tag_redraw = { - .owner = ar, - .user_data = ar, - .notify = ED_region_do_msg_notify_tag_redraw, - }; - WM_msg_subscribe_rna_anon_prop(mbus, WorkSpace, tools, &msg_sub_value_region_tag_redraw); -} - - /************************* header region **************************/ /* add handlers, stuff you only do once or on area/region changes */ @@ -1109,10 +1094,12 @@ void ED_spacetype_image(void) /* regions: statistics/scope buttons */ art = MEM_callocN(sizeof(ARegionType), "spacetype image region"); art->regionid = RGN_TYPE_TOOLS; - art->prefsizex = 220; // XXX + art->prefsizex = 58; /* XXX */ + art->prefsizey = 50; /* XXX */ art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES; art->listener = image_tools_region_listener; - art->message_subscribe = image_tools_region_message_subscribe; + art->message_subscribe = ED_region_generic_tools_region_message_subscribe; + art->snap_size = ED_region_generic_tools_region_snap_size; art->init = image_tools_region_init; art->draw = image_tools_region_draw; BLI_addhead(&st->regiontypes, art); diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c index 6f6b94e000c..c7f02f3c74c 100644 --- a/source/blender/editors/space_view3d/space_view3d.c +++ b/source/blender/editors/space_view3d/space_view3d.c @@ -1103,20 +1103,6 @@ static void view3d_main_region_message_subscribe( } } -static void view3d_tools_region_message_subscribe( - const struct bContext *UNUSED(C), - struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene), - struct bScreen *UNUSED(screen), struct ScrArea *UNUSED(sa), struct ARegion *ar, - struct wmMsgBus *mbus) -{ - wmMsgSubscribeValue msg_sub_value_region_tag_redraw = { - .owner = ar, - .user_data = ar, - .notify = ED_region_do_msg_notify_tag_redraw, - }; - WM_msg_subscribe_rna_anon_prop(mbus, WorkSpace, tools, &msg_sub_value_region_tag_redraw); -} - /* concept is to retrieve cursor type context-less */ static void view3d_main_region_cursor(wmWindow *win, ScrArea *sa, ARegion *ar) { @@ -1319,27 +1305,6 @@ static void view3d_buttons_region_listener( } } -static int view3d_tools_region_snap_size(const ARegion *ar, int size, int axis) -{ - if (axis == 0) { - /* Note, this depends on the icon size: see #ICON_DEFAULT_HEIGHT_TOOLBAR. */ - const float snap_units[] = {2 + 0.8f, 4 + 0.8f}; - const float aspect = BLI_rctf_size_x(&ar->v2d.cur) / (BLI_rcti_size_x(&ar->v2d.mask) + 1); - int best_diff = INT_MAX; - int best_size = size; - for (uint i = 0; i < ARRAY_SIZE(snap_units); i += 1) { - const int test_size = (snap_units[i] * U.widget_unit) / (UI_DPI_FAC * aspect); - const int test_diff = ABS(test_size - size); - if (test_diff < best_diff) { - best_size = test_size; - best_diff = test_diff; - } - } - return best_size; - } - return size; -} - /* add handlers, stuff you only do once or on area/region changes */ static void view3d_tools_region_init(wmWindowManager *wm, ARegion *ar) { @@ -1546,8 +1511,8 @@ void ED_spacetype_view3d(void) art->prefsizey = 50; /* XXX */ art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES; art->listener = view3d_buttons_region_listener; - art->message_subscribe = view3d_tools_region_message_subscribe; - art->snap_size = view3d_tools_region_snap_size; + art->message_subscribe = ED_region_generic_tools_region_message_subscribe; + art->snap_size = ED_region_generic_tools_region_snap_size; art->init = view3d_tools_region_init; art->draw = view3d_tools_region_draw; BLI_addhead(&st->regiontypes, art); -- cgit v1.2.3