Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated Images: end_frame, frame_skips, image_templates #411

Merged
merged 10 commits into from
Dec 15, 2020
Next Next commit
Support 'end_frame' animation property on images
  • Loading branch information
avanwinkle committed Dec 6, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 3e27159a48ff25b35191fc33f4aa605a6295d9a3
8 changes: 8 additions & 0 deletions mpfmc/assets/image.py
Original file line number Diff line number Diff line change
@@ -237,6 +237,14 @@ def do_load(self):
# load first texture to speed up first display
self._callbacks.add(lambda x: self._image.texture)

self.machine.log.info("Loaded image {}, is anim? {}".format(self._image.filename, self._image.anim_available))
# self.machine.log.info(dir(self._image))
# if self._image.anim_available:
self._image.on_texture(self._on_texture)

def _on_texture(self, **kwargs):
self.machine.log.info("Image texture: {}".format(kwargs))

def _do_unload(self):
# This is the method that's called to unload the asset. It's called by
# the main thread so you don't have to worry about thread
28 changes: 26 additions & 2 deletions mpfmc/widgets/image.py
Original file line number Diff line number Diff line change
@@ -18,14 +18,15 @@ class ImageWidget(Widget):

widget_type_name = 'Image'
merge_settings = ('height', 'width')
animation_properties = ('x', 'y', 'color', 'rotation', 'scale', 'fps', 'current_frame', 'opacity')
animation_properties = ('x', 'y', 'color', 'rotation', 'scale', 'fps', 'current_frame', 'end_frame', 'opacity')

def __init__(self, mc: "MpfMc", config: dict, key: Optional[str] = None, **kwargs) -> None:
super().__init__(mc=mc, config=config, key=key)
self.size = (0, 0)

self._image = None # type: ImageAsset
self._current_loop = 0
self._end_frame = -1

# Retrieve the specified image asset to display. This widget simply
# draws a rectangle using the texture from the loaded image asset to
@@ -118,8 +119,13 @@ def _on_texture_change(self, *args) -> None:
self.size = self.texture.size
self._draw_widget()

# Handle animation looping (when applicable)
ci = self._image.image
if self._end_frame > -1 and ci.anim_index == self._end_frame:
self._end_frame = -1
ci.anim_reset(False)
return

# Handle animation looping (when applicable)
if ci.anim_available and self.loops > -1 and ci.anim_index == len(ci.image.textures) - 1:
self._current_loop += 1
if self._current_loop > self.loops:
@@ -225,6 +231,24 @@ def _set_current_frame(self, value: Union[int, float]):
'''The current frame of the animation.
'''

def _get_end_frame(self) -> int:
return self._end_frame

def _set_end_frame(self, value: int):
if not self._image.image.anim_available or not hasattr(self._image.image.image, 'textures'):
return
frame = (int(value) - 1) % len(self._image.image.image.textures)
self.mc.log.info("Setting end frame to {}, current frame is {}".format(frame, self._image.image.anim_index))
if frame == self._image.image.anim_index - 1:
return

self._end_frame = frame
self._image.image.anim_reset(True)

end_frame = AliasProperty(_get_end_frame, _set_end_frame)
'''The target frame at which the animation will stop.
'''

rotation = NumericProperty(0)
'''Rotation angle value of the widget.