Skip to content

Commit

Permalink
feat: now spring bones can be sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed May 6, 2024
1 parent b6ce163 commit 77b388f
Show file tree
Hide file tree
Showing 15 changed files with 3,290 additions and 987 deletions.
143 changes: 142 additions & 1 deletion src/io_scene_vrm/editor/panel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from collections.abc import Set as AbstractSet
from typing import Callable, Optional, Protocol, TypeVar, Union, runtime_checkable

from bpy.app.translations import pgettext
from bpy.types import Armature, Context, Operator, Panel
from bpy.types import (
AnyType,
Armature,
Context,
Operator,
Panel,
UILayout,
)

from ..common import version
from ..common.preferences import get_preferences
Expand All @@ -14,6 +22,139 @@
)
from .ops import layout_operator

__AddOperator = TypeVar("__AddOperator", bound=Operator)
__RemoveOperator = TypeVar("__RemoveOperator", bound=Operator)
__MoveUpOperator = TypeVar("__MoveUpOperator", bound=Operator)
__MoveDownOperator = TypeVar("__MoveDownOperator", bound=Operator)


@runtime_checkable
class TemplateListCollectionProtocol(Protocol):
def __len__(self) -> int: ...
def __getitem__(self, index: int) -> object: ...


def draw_template_list(
layout: UILayout,
template_list_idname: str,
base_object: AnyType,
collection_attribue_name: str,
active_index_attribute_name: str,
add_operator_type: type[__AddOperator],
remove_operator_type: type[__RemoveOperator],
move_up_operator_type: type[__MoveUpOperator],
move_down_operator_type: type[__MoveDownOperator],
*,
can_remove: Callable[[int], bool] = lambda _: True,
can_move: Callable[[int], bool] = lambda _: True,
compact: bool = False,
) -> tuple[
list[Union[__AddOperator, __RemoveOperator, __MoveUpOperator, __MoveDownOperator]],
list[Union[__RemoveOperator, __MoveUpOperator, __MoveDownOperator]],
int,
object,
tuple[
__AddOperator,
__RemoveOperator,
Optional[__MoveUpOperator],
Optional[__MoveDownOperator],
],
]:
collection = getattr(base_object, collection_attribue_name, None)
if not isinstance(collection, TemplateListCollectionProtocol):
message = (
f"{collection}.{collection_attribue_name}"
+ " is not a Template List Collection Protocol."
)
raise TypeError(message)

active_index = getattr(base_object, active_index_attribute_name, None)
if not isinstance(active_index, int):
message = f"{base_object}.{active_index_attribute_name} is not an int."
raise TypeError(message)

if 0 <= active_index < len(collection):
active_object = collection[active_index]
else:
active_object = None

length = len(collection)
list_row_len = 4 if length > int(compact) else 2

list_row = layout.row()
list_row.template_list(
template_list_idname,
"",
base_object,
collection_attribue_name,
base_object,
active_index_attribute_name,
rows=list_row_len,
)
list_side_column = list_row.column(align=True)
add_operator = layout_operator(
list_side_column, add_operator_type, icon="ADD", text="", translate=False
)

if length >= 1 and 0 <= active_index < length and can_remove(active_index):
remove_operator_parent = list_side_column
else:
remove_operator_parent = list_side_column.column(align=True)
remove_operator_parent.enabled = False

remove_operator = layout_operator(
remove_operator_parent,
remove_operator_type,
icon="REMOVE",
text="",
translate=False,
)

if length >= 2 and 0 <= active_index < length and can_move(active_index):
move_operator_parent = list_side_column
else:
move_operator_parent = list_side_column.column(align=True)
move_operator_parent.enabled = False

collection_ops: list[
Union[__AddOperator, __RemoveOperator, __MoveUpOperator, __MoveDownOperator]
] = [add_operator]
collection_item_ops: list[
Union[__RemoveOperator, __MoveUpOperator, __MoveDownOperator]
] = [remove_operator]

if length > int(compact):
move_operator_parent.separator()
move_up_operator = layout_operator(
move_operator_parent,
move_up_operator_type,
icon="TRIA_UP",
text="",
translate=False,
)
collection_item_ops.append(move_up_operator)
move_down_operator = layout_operator(
move_operator_parent,
move_down_operator_type,
icon="TRIA_DOWN",
text="",
translate=False,
)
collection_item_ops.append(move_down_operator)
else:
move_up_operator = None
move_down_operator = None

collection_ops.extend(collection_item_ops)

return (
collection_ops,
collection_item_ops,
active_index,
active_object,
(add_operator, remove_operator, move_up_operator, move_down_operator),
)


class VRM_PT_vrm_armature_object_property(Panel):
bl_idname = "VRM_PT_vrm_armature_object_property"
Expand Down
Loading

0 comments on commit 77b388f

Please sign in to comment.