简体   繁体   English

在杂波中使用BindConstraint限制角色的大小

[英]Using BindConstraint in Clutter to constrain the size of an actor

I recently discovered constraints in Clutter, however, I can't find information on how to constrain the size of actor by proportion. 我最近在Clutter中发现了约束 ,但是,我找不到有关如何按比例约束actor大小的信息。 For example, I want an actor to keep to 1/2 the width of another actor, say it's parent. 例如,我希望一个演员保持另一个演员的宽度的1/2,比如说它是父母。 It seems like it can only force the width to scale 100% with the source. 似乎只能强制宽度与源一起缩放100%。

Clutter.BindConstraint matches the positional and/or dimensional attributes of the source actor. Clutter.BindConstraint与源actor的位置和/或尺寸属性匹配。 for fractional positioning, you can use Clutter.AlignConstraint , but there is no Clutter.Constraint class that allows you to set a fractional dimensional attribute. 对于小数定位,可以使用Clutter.AlignConstraint ,但是没有Clutter.Constraint类可用于设置小数维属性。 you can implement your own ClutterConstraint that does so by subclassing Clutter.Constraint and overriding the Clutter.Constraint.do_update_allocation() virtual function, which gets passed the allocation's of the actor that should be modified by the constraint. 您可以通过Clutter.Constraint的子类并覆盖Clutter.Constraint.do_update_allocation()虚拟函数来实现自己的ClutterConstraint ,该虚拟函数将通过应该由约束条件修改的actor的分配。 something similar to this (untested) code should work: 类似于以下(未经测试)的代码应该可以工作:

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

this should illustrate the mechanism used by Clutter.Constraint to modify the allocation of an actor. 这应该说明Clutter.Constraint用来修改Clutter.Constraint分配的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM