2017-10-21 05:19:48 +00: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
|
|
|
|
# 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 #####
|
|
|
|
|
|
|
|
# <pep8 compliant>
|
|
|
|
import bpy
|
2017-11-02 12:05:13 +00:00
|
|
|
from bpy.types import (
|
|
|
|
Menu,
|
|
|
|
)
|
2017-10-21 05:19:48 +00:00
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
"ToolSelectPanelHelper",
|
2018-04-27 11:23:29 +00:00
|
|
|
"ToolDef",
|
2017-10-21 05:19:48 +00:00
|
|
|
)
|
|
|
|
|
2018-04-26 13:18:47 +00:00
|
|
|
# Support reloading icons.
|
|
|
|
if "_icon_cache" in locals():
|
|
|
|
release = bpy.app.icons.release
|
|
|
|
for icon_value in _icon_cache.values():
|
2018-04-30 11:55:15 +00:00
|
|
|
if icon_value != 0:
|
|
|
|
release(icon_value)
|
2018-04-26 13:18:47 +00:00
|
|
|
del release
|
|
|
|
|
2017-10-21 05:19:48 +00:00
|
|
|
|
2018-04-24 07:19:28 +00:00
|
|
|
# (filename -> icon_value) map
|
|
|
|
_icon_cache = {}
|
|
|
|
|
2018-05-13 06:59:50 +00:00
|
|
|
|
2018-04-27 11:23:29 +00:00
|
|
|
def _keymap_fn_from_seq(keymap_data):
|
|
|
|
|
|
|
|
# standalone
|
|
|
|
def _props_assign_recursive(rna_props, py_props):
|
|
|
|
for prop_id, value in py_props.items():
|
|
|
|
if isinstance(value, dict):
|
|
|
|
_props_assign_recursive(getattr(rna_props, prop_id), value)
|
|
|
|
else:
|
|
|
|
setattr(rna_props, prop_id, value)
|
|
|
|
|
|
|
|
def keymap_fn(km):
|
|
|
|
for op_idname, op_props_dict, kmi_kwargs in keymap_fn.keymap_data:
|
|
|
|
kmi = km.keymap_items.new(op_idname, **kmi_kwargs)
|
|
|
|
kmi_props = kmi.properties
|
|
|
|
if op_props_dict:
|
|
|
|
_props_assign_recursive(kmi.properties, op_props_dict)
|
|
|
|
keymap_fn.keymap_data = keymap_data
|
|
|
|
return keymap_fn
|
|
|
|
|
|
|
|
|
2018-04-29 10:26:00 +00:00
|
|
|
def _item_is_fn(item):
|
2018-04-30 10:14:46 +00:00
|
|
|
return (not (type(item) is ToolDef) and callable(item))
|
2018-04-29 10:26:00 +00:00
|
|
|
|
|
|
|
|
2018-04-30 10:14:46 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
ToolDef = namedtuple(
|
|
|
|
"ToolDef",
|
|
|
|
(
|
|
|
|
# The name to display in the interface.
|
|
|
|
"text",
|
|
|
|
# The name of the icon to use (found in ``release/datafiles/icons``) or None for no icon.
|
|
|
|
"icon",
|
|
|
|
# An optional manipulator group to activate when the tool is set or None for no widget.
|
|
|
|
"widget",
|
|
|
|
# Optional keymap for tool, either:
|
|
|
|
# - A function that populates a keymaps passed in as an argument.
|
|
|
|
# - A tuple filled with triple's of:
|
|
|
|
# ``(operator_id, operator_properties, keymap_item_args)``.
|
|
|
|
#
|
|
|
|
# Warning: currently 'from_dict' this is a list of one item,
|
|
|
|
# so internally we can swap the keymap function for the keymap it's self.
|
|
|
|
# This isn't very nice and may change, tool definitions shouldn't care about this.
|
|
|
|
"keymap",
|
|
|
|
# Optional data-block assosiated with this tool.
|
|
|
|
# (Typically brush name, usage depends on mode, we could use for non-brush ID's in other modes).
|
|
|
|
"data_block",
|
|
|
|
# Optional draw settings (operator options, toolsettings).
|
|
|
|
"draw_settings",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
del namedtuple
|
2018-04-27 11:23:29 +00:00
|
|
|
|
2018-05-13 06:59:50 +00:00
|
|
|
|
2018-04-30 10:14:46 +00:00
|
|
|
def from_dict(kw_args):
|
|
|
|
"""
|
|
|
|
Use so each tool can avoid defining all members of the named tuple.
|
|
|
|
Also convert the keymap from a tuple into a function
|
|
|
|
(since keymap is a callback).
|
|
|
|
"""
|
|
|
|
kw = {
|
|
|
|
"icon": None,
|
|
|
|
"widget": None,
|
|
|
|
"keymap": None,
|
|
|
|
"data_block": None,
|
|
|
|
"draw_settings": None,
|
|
|
|
}
|
|
|
|
kw.update(kw_args)
|
|
|
|
|
|
|
|
keymap = kw["keymap"]
|
|
|
|
if kw["keymap"] is None:
|
|
|
|
pass
|
|
|
|
elif type(keymap) is tuple:
|
|
|
|
keymap = [_keymap_fn_from_seq(keymap)]
|
|
|
|
else:
|
|
|
|
keymap = [keymap]
|
|
|
|
kw["keymap"] = keymap
|
|
|
|
return ToolDef(**kw)
|
|
|
|
|
2018-05-13 06:59:50 +00:00
|
|
|
|
2018-04-30 10:14:46 +00:00
|
|
|
def from_fn(fn):
|
2018-04-27 11:23:29 +00:00
|
|
|
"""
|
2018-04-30 10:14:46 +00:00
|
|
|
Use as decorator so we can define functions.
|
|
|
|
"""
|
|
|
|
return ToolDef.from_dict(fn())
|
|
|
|
|
|
|
|
|
|
|
|
ToolDef.from_dict = from_dict
|
|
|
|
ToolDef.from_fn = from_fn
|
|
|
|
del from_dict
|
|
|
|
del from_fn
|
2018-04-27 11:23:29 +00:00
|
|
|
|
|
|
|
|
2017-10-21 05:19:48 +00:00
|
|
|
class ToolSelectPanelHelper:
|
|
|
|
"""
|
|
|
|
Generic Class, can be used for any toolbar.
|
|
|
|
|
|
|
|
- keymap_prefix:
|
|
|
|
The text prefix for each key-map for this spaces tools.
|
|
|
|
- tools_all():
|
2018-04-26 12:43:32 +00:00
|
|
|
Returns (context_mode, tools) tuple pair for all tools defined.
|
2017-10-21 05:19:48 +00:00
|
|
|
- tools_from_context(context):
|
|
|
|
Returns tools available in this context.
|
|
|
|
|
2018-04-27 11:23:29 +00:00
|
|
|
Each tool is a 'ToolDef' or None for a separator in the toolbar, use ``None``.
|
2017-10-21 05:19:48 +00:00
|
|
|
"""
|
|
|
|
|
2018-05-13 06:13:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def _tool_class_from_space_type(space_type):
|
|
|
|
return next(
|
|
|
|
(cls for cls in ToolSelectPanelHelper.__subclasses__()
|
|
|
|
if cls.bl_space_type == space_type),
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
2018-04-24 07:19:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def _icon_value_from_icon_handle(icon_name):
|
|
|
|
import os
|
|
|
|
if icon_name is not None:
|
|
|
|
assert(type(icon_name) is str)
|
|
|
|
icon_value = _icon_cache.get(icon_name)
|
|
|
|
if icon_value is None:
|
2018-04-25 12:05:48 +00:00
|
|
|
dirname = bpy.utils.resource_path('LOCAL')
|
2018-04-27 14:10:28 +00:00
|
|
|
if not os.path.exists(dirname):
|
2018-04-24 08:45:57 +00:00
|
|
|
# TODO(campbell): use a better way of finding datafiles.
|
2018-04-25 12:05:48 +00:00
|
|
|
dirname = bpy.utils.resource_path('SYSTEM')
|
2018-04-24 07:19:28 +00:00
|
|
|
filename = os.path.join(dirname, "datafiles", "icons", icon_name + ".dat")
|
|
|
|
try:
|
|
|
|
icon_value = bpy.app.icons.new_triangles_from_file(filename)
|
|
|
|
except Exception as ex:
|
2018-04-27 12:34:54 +00:00
|
|
|
if not os.path.exists(filename):
|
2018-04-24 07:19:28 +00:00
|
|
|
print("Missing icons:", filename, ex)
|
|
|
|
else:
|
|
|
|
print("Corrupt icon:", filename, ex)
|
2018-04-30 11:55:15 +00:00
|
|
|
# Use none as a fallback (avoids layout issues).
|
|
|
|
if icon_name != "none":
|
|
|
|
icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle("none")
|
|
|
|
else:
|
|
|
|
icon_value = 0
|
2018-04-24 07:19:28 +00:00
|
|
|
_icon_cache[icon_name] = icon_value
|
|
|
|
return icon_value
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2017-11-02 12:05:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def _tools_flatten(tools):
|
2018-04-29 10:26:00 +00:00
|
|
|
"""
|
|
|
|
Flattens, skips None and calls generators.
|
|
|
|
"""
|
2017-11-02 12:05:13 +00:00
|
|
|
for item in tools:
|
2018-05-13 06:59:50 +00:00
|
|
|
if item is None:
|
|
|
|
yield None
|
|
|
|
elif type(item) is tuple:
|
|
|
|
for sub_item in item:
|
|
|
|
if sub_item is None:
|
|
|
|
yield None
|
|
|
|
elif _item_is_fn(sub_item):
|
|
|
|
yield from sub_item(context)
|
|
|
|
else:
|
|
|
|
yield sub_item
|
|
|
|
else:
|
|
|
|
if _item_is_fn(item):
|
|
|
|
yield from item(context)
|
2017-11-03 05:30:51 +00:00
|
|
|
else:
|
2018-05-13 06:59:50 +00:00
|
|
|
yield item
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _tools_flatten_with_tool_index(tools):
|
|
|
|
for item in tools:
|
|
|
|
if item is None:
|
|
|
|
yield None, -1
|
|
|
|
elif type(item) is tuple:
|
|
|
|
i = 0
|
|
|
|
for sub_item in item:
|
|
|
|
if sub_item is None:
|
|
|
|
yield None
|
|
|
|
elif _item_is_fn(sub_item):
|
|
|
|
for item_dyn in sub_item(context):
|
|
|
|
yield item_dyn, i
|
|
|
|
i += 1
|
2018-04-29 10:26:00 +00:00
|
|
|
else:
|
2018-05-13 06:59:50 +00:00
|
|
|
yield sub_item, i
|
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
if _item_is_fn(item):
|
|
|
|
for item_dyn in item(context):
|
|
|
|
yield item_dyn, -1
|
|
|
|
else:
|
|
|
|
yield item, -1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _tool_get_active(context, with_icon=False):
|
|
|
|
"""
|
|
|
|
Return the active Python tool definition and icon name.
|
|
|
|
"""
|
|
|
|
|
|
|
|
workspace = context.workspace
|
|
|
|
cls = ToolSelectPanelHelper._tool_class_from_space_type(workspace.tool_space_type)
|
|
|
|
if cls is not None:
|
|
|
|
tool_def_active, index_active = ToolSelectPanelHelper._tool_vars_from_active_with_index(context)
|
|
|
|
|
|
|
|
context_mode = context.mode
|
|
|
|
for item in ToolSelectPanelHelper._tools_flatten(cls.tools_from_context(context)):
|
|
|
|
if item is not None:
|
|
|
|
tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
|
|
|
|
if (tool_def == tool_def_active):
|
|
|
|
if with_icon:
|
|
|
|
icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(icon_name)
|
|
|
|
else:
|
|
|
|
icon_value = 0
|
|
|
|
return (item, icon_value)
|
|
|
|
return None, 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _tool_get_by_name(context, text):
|
|
|
|
"""
|
|
|
|
Return the active Python tool definition and index (if in sub-group, else -1).
|
|
|
|
"""
|
|
|
|
workspace = context.workspace
|
|
|
|
cls = ToolSelectPanelHelper._tool_class_from_space_type(workspace.tool_space_type)
|
|
|
|
if cls is not None:
|
|
|
|
context_mode = context.mode
|
|
|
|
for item, index in ToolSelectPanelHelper._tools_flatten_with_tool_index(cls.tools_from_context(context)):
|
|
|
|
if item is not None:
|
|
|
|
if item.text == text:
|
|
|
|
return (item, index)
|
|
|
|
return None, -1
|
2017-11-03 05:30:51 +00:00
|
|
|
|
2018-05-13 06:07:17 +00:00
|
|
|
@staticmethod
|
|
|
|
def _tool_vars_from_def(item, context_mode):
|
2018-04-26 05:39:15 +00:00
|
|
|
# For now be strict about whats in this dict
|
|
|
|
# prevent accidental adding unknown keys.
|
2018-04-27 11:23:29 +00:00
|
|
|
text = item.text
|
|
|
|
icon_name = item.icon
|
|
|
|
mp_idname = item.widget
|
2018-04-29 12:30:09 +00:00
|
|
|
datablock_idname = item.data_block
|
2018-04-30 10:14:46 +00:00
|
|
|
keymap = item.keymap
|
|
|
|
if keymap is None:
|
|
|
|
km_idname = None
|
2018-04-26 06:04:50 +00:00
|
|
|
else:
|
2018-04-30 10:14:46 +00:00
|
|
|
km_idname = keymap[0].name
|
2018-04-29 12:30:09 +00:00
|
|
|
return (km_idname, mp_idname, datablock_idname), icon_name
|
2017-11-03 05:30:51 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _tool_vars_from_active_with_index(context):
|
|
|
|
workspace = context.workspace
|
|
|
|
return (
|
2018-04-29 14:24:40 +00:00
|
|
|
(
|
|
|
|
workspace.tool_keymap or None,
|
|
|
|
workspace.tool_manipulator_group or None,
|
|
|
|
workspace.tool_data_block or None,
|
|
|
|
),
|
2017-11-03 05:30:51 +00:00
|
|
|
workspace.tool_index,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _tool_vars_from_button_with_index(context):
|
|
|
|
props = context.button_operator
|
|
|
|
return (
|
2018-04-29 14:24:40 +00:00
|
|
|
(
|
|
|
|
props.keymap or None or None,
|
|
|
|
props.manipulator_group or None,
|
2018-04-30 10:14:46 +00:00
|
|
|
props.data_block or None,
|
2018-04-29 14:24:40 +00:00
|
|
|
),
|
2017-11-03 05:30:51 +00:00
|
|
|
props.index,
|
|
|
|
)
|
2017-11-02 12:05:13 +00:00
|
|
|
|
2017-10-21 05:19:48 +00:00
|
|
|
@classmethod
|
2018-04-27 11:23:29 +00:00
|
|
|
def _km_action_simple(cls, kc, context_mode, text, keymap_fn):
|
2018-04-26 06:10:52 +00:00
|
|
|
if context_mode is None:
|
|
|
|
context_mode = "All"
|
|
|
|
km_idname = f"{cls.keymap_prefix} {context_mode}, {text}"
|
2017-11-04 14:38:51 +00:00
|
|
|
km = kc.keymaps.get(km_idname)
|
2018-04-30 10:14:46 +00:00
|
|
|
if km is None:
|
|
|
|
km = kc.keymaps.new(km_idname, space_type=cls.bl_space_type, region_type='WINDOW')
|
|
|
|
keymap_fn[0](km)
|
|
|
|
keymap_fn[0] = km
|
2017-10-21 05:19:48 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def register(cls):
|
|
|
|
wm = bpy.context.window_manager
|
|
|
|
|
|
|
|
# XXX, should we be manipulating the user-keyconfig on load?
|
|
|
|
# Perhaps this should only add when keymap items don't already exist.
|
|
|
|
#
|
|
|
|
# This needs some careful consideration.
|
|
|
|
kc = wm.keyconfigs.user
|
|
|
|
|
2017-11-03 05:30:51 +00:00
|
|
|
# Track which tool-group was last used for non-active groups.
|
|
|
|
# Blender stores the active tool-group index.
|
|
|
|
#
|
2017-11-02 12:30:01 +00:00
|
|
|
# {tool_name_first: index_in_group, ...}
|
|
|
|
cls._tool_group_active = {}
|
|
|
|
|
2017-10-26 11:04:48 +00:00
|
|
|
# ignore in background mode
|
|
|
|
if kc is None:
|
|
|
|
return
|
|
|
|
|
2018-04-26 12:43:32 +00:00
|
|
|
for context_mode, tools in cls.tools_all():
|
2018-04-26 06:04:50 +00:00
|
|
|
for item_parent in tools:
|
|
|
|
if item_parent is None:
|
|
|
|
continue
|
2018-04-27 11:23:29 +00:00
|
|
|
for item in item_parent if (type(item_parent) is tuple) else (item_parent,):
|
2018-04-29 10:26:00 +00:00
|
|
|
# skip None or generator function
|
|
|
|
if item is None or _item_is_fn(item):
|
2018-04-26 06:04:50 +00:00
|
|
|
continue
|
2018-04-27 11:23:29 +00:00
|
|
|
keymap_data = item.keymap
|
2018-04-30 10:14:46 +00:00
|
|
|
if keymap_data is not None and callable(keymap_data[0]):
|
2018-04-27 11:23:29 +00:00
|
|
|
text = item.text
|
|
|
|
icon_name = item.icon
|
2018-04-30 10:14:46 +00:00
|
|
|
cls._km_action_simple(kc, context_mode, text, keymap_data)
|
2017-10-21 05:19:48 +00:00
|
|
|
|
2018-04-28 08:51:05 +00:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Layout Generators
|
|
|
|
#
|
|
|
|
# Meaning of recieved values:
|
|
|
|
# - Bool: True for a separator, otherwise False for regular tools.
|
|
|
|
# - None: Signal to finish (complete any final operations, e.g. add padding).
|
2017-10-21 05:19:48 +00:00
|
|
|
|
2018-04-28 08:51:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def _layout_generator_single_column(layout):
|
2018-04-24 07:19:28 +00:00
|
|
|
scale_y = 2.0
|
|
|
|
|
2018-04-28 08:51:05 +00:00
|
|
|
col = layout.column(align=True)
|
|
|
|
col.scale_y = scale_y
|
|
|
|
is_sep = False
|
|
|
|
while True:
|
|
|
|
if is_sep is True:
|
|
|
|
col = layout.column(align=True)
|
|
|
|
col.scale_y = scale_y
|
|
|
|
elif is_sep is None:
|
|
|
|
yield None
|
|
|
|
return
|
|
|
|
is_sep = yield col
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _layout_generator_multi_columns(layout, column_count):
|
|
|
|
scale_y = 2.0
|
|
|
|
scale_x = 2.2
|
|
|
|
column_last = column_count - 1
|
|
|
|
|
|
|
|
col = layout.column(align=True)
|
|
|
|
|
|
|
|
row = col.row(align=True)
|
|
|
|
|
|
|
|
row.scale_x = scale_x
|
|
|
|
row.scale_y = scale_y
|
|
|
|
|
|
|
|
is_sep = False
|
|
|
|
column_index = 0
|
|
|
|
while True:
|
|
|
|
if is_sep is True:
|
|
|
|
if column_index != column_last:
|
|
|
|
row.label("")
|
|
|
|
col = layout.column(align=True)
|
|
|
|
row = col.row(align=True)
|
|
|
|
row.scale_x = scale_x
|
|
|
|
row.scale_y = scale_y
|
|
|
|
column_index = 0
|
|
|
|
|
|
|
|
is_sep = yield row
|
|
|
|
if is_sep is None:
|
|
|
|
if column_index == column_last:
|
|
|
|
row.label("")
|
|
|
|
yield None
|
|
|
|
return
|
|
|
|
|
|
|
|
if column_index == column_count:
|
|
|
|
column_index = 0
|
|
|
|
row = col.row(align=True)
|
|
|
|
row.scale_x = scale_x
|
|
|
|
row.scale_y = scale_y
|
|
|
|
column_index += 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _layout_generator_detect_from_region(layout, region):
|
|
|
|
"""
|
|
|
|
Choose an appropriate layout for the toolbar.
|
|
|
|
"""
|
|
|
|
# Currently this just checks the width,
|
|
|
|
# we could have different layouts as preferences too.
|
2018-04-28 18:48:47 +00:00
|
|
|
system = bpy.context.user_preferences.system
|
2018-04-28 08:51:05 +00:00
|
|
|
view2d = region.view2d
|
2018-04-28 18:48:47 +00:00
|
|
|
view2d_scale = (
|
2018-04-24 14:33:38 +00:00
|
|
|
view2d.region_to_view(1.0, 0.0)[0] -
|
|
|
|
view2d.region_to_view(0.0, 0.0)[0]
|
|
|
|
)
|
2018-04-28 18:48:47 +00:00
|
|
|
width_scale = region.width * view2d_scale / system.ui_scale
|
2018-04-24 14:33:38 +00:00
|
|
|
|
2018-04-27 16:20:48 +00:00
|
|
|
if width_scale > 120.0:
|
2018-04-27 15:55:17 +00:00
|
|
|
show_text = True
|
2018-04-28 08:51:05 +00:00
|
|
|
column_count = 1
|
2018-04-27 15:55:17 +00:00
|
|
|
else:
|
|
|
|
show_text = False
|
2018-04-27 16:20:48 +00:00
|
|
|
# 2 column layout, disabled
|
|
|
|
if width_scale > 80.0:
|
2018-04-27 15:55:17 +00:00
|
|
|
column_count = 2
|
|
|
|
use_columns = True
|
|
|
|
else:
|
2018-04-28 08:51:05 +00:00
|
|
|
column_count = 1
|
2018-04-27 15:55:17 +00:00
|
|
|
|
2018-04-28 08:51:05 +00:00
|
|
|
if column_count == 1:
|
|
|
|
ui_gen = ToolSelectPanelHelper._layout_generator_single_column(layout)
|
|
|
|
else:
|
|
|
|
ui_gen = ToolSelectPanelHelper._layout_generator_multi_columns(layout, column_count=column_count)
|
|
|
|
|
|
|
|
return ui_gen, show_text
|
|
|
|
|
|
|
|
def draw(self, context):
|
|
|
|
# XXX, this UI isn't very nice.
|
|
|
|
# We might need to create new button types for this.
|
|
|
|
# Since we probably want:
|
|
|
|
# - tool-tips that include multiple key shortcuts.
|
|
|
|
# - ability to click and hold to expose sub-tools.
|
|
|
|
|
|
|
|
context_mode = context.mode
|
2018-05-13 06:07:17 +00:00
|
|
|
tool_def_active, index_active = ToolSelectPanelHelper._tool_vars_from_active_with_index(context)
|
2018-04-28 08:51:05 +00:00
|
|
|
|
|
|
|
ui_gen, show_text = self._layout_generator_detect_from_region(self.layout, context.region)
|
|
|
|
|
|
|
|
# Start iteration
|
|
|
|
ui_gen.send(None)
|
2018-04-27 15:55:17 +00:00
|
|
|
|
2018-04-29 10:26:00 +00:00
|
|
|
for item in self.tools_from_context(context):
|
2018-05-13 06:32:47 +00:00
|
|
|
if item is None:
|
|
|
|
ui_gen.send(True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if type(item) is tuple:
|
|
|
|
is_active = False
|
|
|
|
i = 0
|
|
|
|
for i, sub_item in enumerate(item):
|
|
|
|
if sub_item is None:
|
|
|
|
continue
|
|
|
|
tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(sub_item, context_mode)
|
|
|
|
is_active = (tool_def == tool_def_active)
|
2018-04-29 10:26:00 +00:00
|
|
|
if is_active:
|
2018-05-13 06:32:47 +00:00
|
|
|
index = i
|
|
|
|
break
|
|
|
|
del i, sub_item
|
2018-04-29 10:26:00 +00:00
|
|
|
|
2018-05-13 06:32:47 +00:00
|
|
|
if is_active:
|
|
|
|
# not ideal, write this every time :S
|
|
|
|
self._tool_group_active[item[0].text] = index
|
2018-04-29 10:26:00 +00:00
|
|
|
else:
|
2018-05-13 06:32:47 +00:00
|
|
|
index = self._tool_group_active.get(item[0].text, 0)
|
2018-04-29 10:26:00 +00:00
|
|
|
|
2018-05-13 06:32:47 +00:00
|
|
|
item = item[index]
|
|
|
|
use_menu = True
|
|
|
|
else:
|
|
|
|
index = -1
|
|
|
|
use_menu = False
|
2018-04-29 12:30:09 +00:00
|
|
|
|
2018-05-13 06:32:47 +00:00
|
|
|
tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
|
|
|
|
is_active = (tool_def == tool_def_active)
|
2018-04-29 10:26:00 +00:00
|
|
|
|
2018-05-13 06:32:47 +00:00
|
|
|
icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(icon_name)
|
2018-04-29 10:26:00 +00:00
|
|
|
|
2018-05-13 06:32:47 +00:00
|
|
|
sub = ui_gen.send(False)
|
|
|
|
|
|
|
|
if use_menu:
|
|
|
|
props = sub.operator_menu_hold(
|
|
|
|
"wm.tool_set",
|
|
|
|
text=item.text if show_text else "",
|
|
|
|
depress=is_active,
|
|
|
|
menu="WM_MT_toolsystem_submenu",
|
|
|
|
icon_value=icon_value,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
props = sub.operator(
|
|
|
|
"wm.tool_set",
|
|
|
|
text=item.text if show_text else "",
|
|
|
|
depress=is_active,
|
|
|
|
icon_value=icon_value,
|
|
|
|
)
|
|
|
|
props.keymap = tool_def[0] or ""
|
|
|
|
props.manipulator_group = tool_def[1] or ""
|
|
|
|
props.data_block = tool_def[2] or ""
|
|
|
|
props.index = index
|
2018-04-28 08:51:05 +00:00
|
|
|
# Signal to finish any remaining layout edits.
|
|
|
|
ui_gen.send(None)
|
2018-04-27 15:55:17 +00:00
|
|
|
|
2018-04-27 08:49:20 +00:00
|
|
|
@staticmethod
|
|
|
|
def draw_active_tool_header(context, layout):
|
2018-05-13 06:59:50 +00:00
|
|
|
item, icon_value = ToolSelectPanelHelper._tool_get_active(context, with_icon=True)
|
2018-04-27 08:49:20 +00:00
|
|
|
if item is None:
|
|
|
|
return
|
2018-04-29 15:49:39 +00:00
|
|
|
# Note: we could show 'item.text' here but it makes the layout jitter when switcuing tools.
|
|
|
|
layout.label(" ", icon_value=icon_value)
|
2018-04-27 12:13:16 +00:00
|
|
|
draw_settings = item.draw_settings
|
|
|
|
if draw_settings is not None:
|
|
|
|
draw_settings(context, layout)
|
|
|
|
|
2018-04-27 08:49:20 +00:00
|
|
|
|
2017-11-02 12:05:13 +00:00
|
|
|
# The purpose of this menu is to be a generic popup to select between tools
|
|
|
|
# in cases when a single tool allows to select alternative tools.
|
|
|
|
class WM_MT_toolsystem_submenu(Menu):
|
|
|
|
bl_label = ""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _tool_group_from_button(context):
|
2018-04-26 06:04:50 +00:00
|
|
|
context_mode = context.mode
|
2017-11-02 12:05:13 +00:00
|
|
|
# Lookup the tool definitions based on the space-type.
|
2018-05-13 06:13:13 +00:00
|
|
|
cls = ToolSelectPanelHelper._tool_class_from_space_type(context.space_data.type)
|
2017-11-02 12:05:13 +00:00
|
|
|
if cls is not None:
|
2018-05-13 06:07:17 +00:00
|
|
|
tool_def_button, index_button = ToolSelectPanelHelper._tool_vars_from_button_with_index(context)
|
2018-04-29 14:24:40 +00:00
|
|
|
for item_group in cls.tools_from_context(context):
|
|
|
|
if type(item_group) is tuple:
|
|
|
|
if index_button < len(item_group):
|
|
|
|
item = item_group[index_button]
|
2018-05-13 06:07:17 +00:00
|
|
|
tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
|
2018-04-29 14:24:40 +00:00
|
|
|
is_active = (tool_def == tool_def_button)
|
|
|
|
if is_active:
|
|
|
|
return cls, item_group, index_button
|
2017-11-02 12:05:13 +00:00
|
|
|
return None, None, -1
|
|
|
|
|
|
|
|
def draw(self, context):
|
2018-04-26 06:04:50 +00:00
|
|
|
context_mode = context.mode
|
2017-11-02 12:05:13 +00:00
|
|
|
layout = self.layout
|
2018-04-24 07:19:28 +00:00
|
|
|
layout.scale_y = 2.0
|
|
|
|
|
2017-11-02 12:05:13 +00:00
|
|
|
cls, item_group, index_active = self._tool_group_from_button(context)
|
|
|
|
if item_group is None:
|
|
|
|
# Should never happen, just in case
|
2017-11-03 05:30:51 +00:00
|
|
|
layout.label("Unable to find toolbar group")
|
2017-11-02 12:05:13 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
for item in item_group:
|
|
|
|
if item is None:
|
|
|
|
layout.separator()
|
|
|
|
continue
|
2018-05-13 06:07:17 +00:00
|
|
|
tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
|
2018-04-24 07:19:28 +00:00
|
|
|
icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(icon_name)
|
2017-11-02 12:05:13 +00:00
|
|
|
props = layout.operator(
|
|
|
|
"wm.tool_set",
|
2018-04-27 11:23:29 +00:00
|
|
|
text=item.text,
|
2018-04-24 07:19:28 +00:00
|
|
|
icon_value=icon_value,
|
2017-11-02 12:05:13 +00:00
|
|
|
)
|
2017-11-03 05:30:51 +00:00
|
|
|
props.keymap = tool_def[0] or ""
|
|
|
|
props.manipulator_group = tool_def[1] or ""
|
2018-04-29 12:30:09 +00:00
|
|
|
props.data_block = tool_def[2] or ""
|
2017-11-02 12:05:13 +00:00
|
|
|
props.index = index
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
|
2018-05-13 06:59:50 +00:00
|
|
|
def activate_by_name(context, text):
|
|
|
|
item, index = ToolSelectPanelHelper._tool_get_by_name(context, text)
|
|
|
|
if item is not None:
|
|
|
|
context_mode = context.mode
|
|
|
|
tool_def, icon_name = ToolSelectPanelHelper._tool_vars_from_def(item, context_mode)
|
|
|
|
bpy.ops.wm.tool_set(
|
|
|
|
keymap=tool_def[0] or "",
|
|
|
|
manipulator_group=tool_def[1] or "",
|
|
|
|
data_block=tool_def[2] or "",
|
|
|
|
index=index,
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2017-11-02 12:05:13 +00:00
|
|
|
classes = (
|
|
|
|
WM_MT_toolsystem_submenu,
|
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__": # only for live edit.
|
|
|
|
from bpy.utils import register_class
|
|
|
|
for cls in classes:
|
|
|
|
register_class(cls)
|