component

警告

这部分内容涉及 JAnim 实现原理,可能较为复杂,若没有研究源码的需求,你应酌情阅读

class janim.components.component.Component

基类:Refreshable, Generic

class BindInfo(decl_cls: type[Item], at_item: Item, key: str)

基类:object

对组件定义信息的封装

  • decl_cls: 以 xxx = CmptInfo(...) 的形式被声明在哪个类中; 如果一个类及其父类都有 xxx = CmptInfo(...) ,那么 decl_cls 是父类

  • at_item: 这个组件对象是属于哪个物件对象的

  • key: 这个组件对象的变量名

例:

class MyCmpt(Component): ...

class MyItem(Item):
    cmpt1 = CmptInfo(MyCmpt[Self])
    cmpt2 = CmptInfo(MyCmpt[Self])

class MyItem2(MyItem):
    cmpt3 = CmptInfo(MyCmpt[Self])

item = MyItem()

# item.cmpt1.bind_info 与 BindInfo(MyItem, item, 'cmpt1') 一致
# item.cmpt2.bind_info 与 BindInfo(MyItem, item, 'cmpt2') 一致

item2 = MyItem2()

# item2.cmpt1.bind_info 与 BindInfo(MyItem, item2, 'cmpt1') 一致
# item2.cmpt3.bind_info 与 BindInfo(MyItem2, item2, 'cmpt3') 一致
decl_cls: type[Item]
at_item: Item
key: str
init_bind(bind: BindInfo) None

用于 Item._init_components

子类可以继承该函数,进行与所在物件相关的处理

fallback_check() bool
mark_refresh(func: Callable | str, *, recurse_up=False, recurse_down=False) Self

详见: broadcast_refresh_of_component()

copy() Self
become(other) Self
not_changed(other) bool
get_same_cmpt(item: Item) Self
get_same_cmpt_without_mock(item: Item) Self | None
get_same_cmpt_if_exists(item: Item) Self | None
walk_same_cmpt_of_self_and_descendants_without_mock(root_only: bool = False, *, timed: bool = False) Generator[Self, None, None]
walk_same_cmpt_of_descendants_without_mock(*, timed: bool = False) Generator[Self, None, None]
property r: ItemT

所位于的物件,便于链式调用同物件下其它的组件

classmethod align_for_interpolate(cmpt1, cmpt2) AlignedData[Self]
interpolate(cmpt1, cmpt2, alpha: float, *, path_func=None) None
class janim.components.component.CmptInfo(cls: type[T], *args, **kwargs)

基类:Generic

在类中定义组件需要使用该类

例:

class MyItem(Item):
    # Wrong!
    # cmpt1 = MyCmpt()

    # Right
    cmpt1 = CmptInfo(MyCmpt[Self])

    # Wrong!
    # cmpt2 = MyCmptWithArgs(1)

    # Right
    cmpt2 = CmptInfo(MyCmptWithArgs[Self], 1)
create() Component
janim.components.component.CmptGroup(*cmpt_info_list: CmptInfo) CmptInfo

用于将多个组件打包,使得可以同时调用

例:

class MyItem(Item):
    stroke = CmptInfo(Cmpt_Rgbas[Self])
    fill = CmptInfo(Cmpt_Rgbas[Self])
    color = CmptGroup(stroke, fill)

item = MyItem()
item.stroke.set(...)    # 只有 stroke 的被调用 | Only the method of stroke be called
item.color.set(...)     # stroke 和 fill 的都被调用了 | the methods of stroke and fill are both called