Can you change a rectangles color using an animation event?

28 views
Skip to first unread message

Greg Sealby

unread,
May 18, 2019, 7:13:46 AM5/18/19
to MPF Users
I am trying to change the color of a rectangle based on an event.  I've tried a few different ways and am a little stuck.  I seem to be able to change the height of the rectangle using the example from the docs without any issue: http://docs.missionpinball.org/en/latest/displays/widgets/animation.html?highlight=animations%3A#animating-a-progress-bar

I have set up a player variable and in my mode I respond to a specific event and set the colour I want, like so:
        mode_instruction_colour:
           
string: 'DARKRED'
            action
: set

And then in my slide I have the following animation code to change the rectangles colour based on a trigger whenever this variable is changed:
            animations:
                player_mode_instruction_colour
:
                   
- property: color
                      value
: (value)


When I try to do this, the game crashes and the MC log contains this error:

2019-05-18 21:01:43,772 : EventManager : Event: ======'player_mode_instruction_colour'====== Args={'value': 'DARKRED', 'prev_value': 'DARKGREEN', 'change': True, 'player_num': 1}
2019-05-18 21:01:43,774 : mpfmc : There is a mismatch between the number of values available and the number of values required to animate the following properties in the Rectangle widget: ['color'] (animation will be ignored).
2019-05-18 21:01:43,774 : kivy : Base: Leaving application in progress...
2019-05-18 21:01:43,775 : kivy : Exception while processing RegisteredHandler(callback=<bound method Widget.start_animation_from_event of <Rectangle Widget id=None>>, priority=1, kwargs={'event_name': 'player_mode_instruction_colour'}, key=UUID('6021dcb6-54e2-4f04-bbd0-8f514f07ef69'), condition=None, blocking_facility=None) for event player_mode_instruction_colour
Traceback (most recent call last):
  File "c:\users\gregs\appdata\local\programs\python\python36\lib\site-packages\mpf\core\events.py", line 657, in _run_handlers
    result = handler.callback(**merged_kwargs)
  File "c:\users\gregs\appdata\local\programs\python\python36\lib\site-packages\mpfmc\uix\widget.py", line 612, in start_animation_from_event
    self.config['animations'][event_name], kwargs)
  File "c:\users\gregs\appdata\local\programs\python\python36\lib\site-packages\mpfmc\uix\widget.py", line 578, in build_animation_from_config
    final_animation = reduce(lambda x, y: x + y, animation_sequence_list)
TypeError: reduce() of empty sequence with no initial value

...

    raise Exception("Exception while processing {} for event {}".format(handler, event)) from e
Exception: Exception while processing RegisteredHandler(callback=<bound method Widget.start_animation_from_event of <Rectangle Widget id=None>>, priority=1, kwargs={'event_name': 'player_mode_instruction_colour'}, key=UUID('6021dcb6-54e2-4f04-bbd0-8f514f07ef69'), condition=None, blocking_facility=None) for event player_mode_instruction_colour

It looks like I need to pass more parameters than just "value" to change a color property of a rectangle object.  Does anyone know what these might be, or if I have got this config wrong somewhere.

Quinn Capen

unread,
May 18, 2019, 11:09:29 AM5/18/19
to MPF Users
You can change the color of several widgets using animation, but it cannot be done using color names.  Animation target values must be specified as a comma-separated list with 4 values: r, g, b, a
Example:

  widget_bezier:
    - type: bezier
      points: 400, 300, 100, 100, 400, 0
      color: red
      thickness: 5
      animations:
        add_to_slide:
                    - property: rotation
            value: -300
            duration: 2s
            timing: with_previous
          - property: color
            value: 0, 0, 1, 1
            duration: 1s
          - property: color
            value: 1, 1, 0, 1
            duration: 1s


That example comes from the widget test config (there are a lot of good examples of animating all kinds of widget properties in those tests).  Note that the target color values are float values between 0 and 1 (corresponding to RGB values from 0 to 255) and the 4th value is the alpha value (also betwen 0 and 1).  Why is this format used?  Simply because that is how Kivy supports color animation values out of the box.

Quinn

Cole M

unread,
May 18, 2019, 6:53:06 PM5/18/19
to MPF Users
Quinn has the direct way to do it.  Another (indirect) way you can do this is by blending colors using transparency.  

Greg Sealby

unread,
May 18, 2019, 8:59:09 PM5/18/19
to MPF Users
Thanks Quinn and Cole, I will try those methods.  Appreciate your help.

Greg Sealby

unread,
May 18, 2019, 9:10:17 PM5/18/19
to MPF Users
I used the method suggested by Quinn, converted by RGB values into decimals and it worked perfectly.  I did notice I had to set the "W" value to 1 as it looks like 0 makes it transparent.

Thanks again guys for the help.  
Reply all
Reply to author
Forward
0 new messages