在杂波中使用BindConstraint约束

2024-09-22 20:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在杂乱中发现了constraints,但是,我找不到关于如何按比例约束actor大小的信息。例如,我希望一个演员的宽度保持在另一个演员的1/2,比如说它是父演员。它似乎只能强制宽度与源缩放100%。你知道吗


Tags: 信息宽度比例actorconstraints演员杂乱
1条回答
网友
1楼 · 发布于 2024-09-22 20:23:57

Clutter.BindConstraint匹配源参与者的位置和/或维度属性。对于分数定位,可以使用Clutter.AlignConstraint,但是没有Clutter.Constraint类允许您设置分数维属性。您可以通过子类化Clutter.Constraint并重写Clutter.Constraint.do_update_allocation()虚函数来实现自己的ClutterConstraint,该虚函数传递应该由约束修改的参与者的分配。类似于此(未经测试)代码的内容应该可以工作:

class MyConstraint (Clutter.Constraint):
    def __init__(self, source, width_fraction=1.0, height_fraction=1.0):
        Clutter.Constraint.__init__(self)
        self._source = source
        self._widthf = width_fraction
        self._heightf = height_fraction
    def do_update_allocation(self, actor, allocation):
        source_alloc = self._source.get_allocation()
        width = source_alloc.get_width() * self._widthf
        height = source_alloc.get_height() * self._heightf
        allocation.x2 = allocation.x1 + width
        allocation.y2 = allocation.y1 + height

这应该说明Clutter.Constraint用来修改参与者分配的机制。你知道吗

相关问题 更多 >