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

DynamicBitmap #21

Open
jamesdbrock opened this issue Feb 1, 2023 · 0 comments
Open

DynamicBitmap #21

jamesdbrock opened this issue Feb 1, 2023 · 0 comments

Comments

@jamesdbrock
Copy link
Collaborator

Here is a DynamicBitmap component which someone may enjoy.

class DynamicBitmap(GenStaticBitmap):
    """
    A `StaticBitmap` which redraws with every render if the `bitmap` props is present.

    If the `bitmap` props is not present, it leaves the last `bitmap`.

    props = {
        bitmap: wx.Bitmap
    }

    To update the `DynamicBitmap` rapidly without calling `set_state()`, consider using
    the [Refs](https://github.com/chriskiehl/re-wx/blob/main/docs/main-concepts.md#refs)
    technique and calling `my_dynamicbitmap_ref.SetBitmap(bitmap)` directly.

    https://discuss.wxpython.org/t/staticbitmap-fast-refresh-state-of-the-art/36197
    """

    def OnPaint(self, event):
        """
        Handles the ``wx.EVT_PAINT`` for :class:`GenStaticBitmap`.

        :param `event`: a :class:`wx.PaintEvent` event to be processed.

        We override this so we can Clear() for alpha-transparent images.
        """
        dc = wx.PaintDC(self)
        if self._bitmap:
            dc.Clear()
            dc.DrawBitmap(self._bitmap, 0, 0, True)

@mount.register(DynamicBitmap)
def dynamicbitmap(element, parent) -> DynamicBitmap:
    instance = DynamicBitmap(parent, -1, wx.Bitmap.FromRGBA(1,1,0,0,0,0))
    return update(element, instance)

@update.register(DynamicBitmap)
def dynamicbitmap(element, instance: DynamicBitmap) -> DynamicBitmap:
    props = element['props']
    bitmap = props.get('bitmap')
    if bitmap:
        instance.SetBitmap(bitmap)
    instance.Unbind(wx.EVT_LEFT_DOWN)
    if 'on_left_down' in props:
        instance.Bind(wx.EVT_LEFT_DOWN, props['on_left_down'])
    instance.Unbind(wx.EVT_LEFT_UP)
    if 'on_left_up' in props:
        instance.Bind(wx.EVT_LEFT_UP, props['on_left_up'])
    instance.Unbind(wx.EVT_RIGHT_DOWN)
    if 'on_right_down' in props:
        instance.Bind(wx.EVT_RIGHT_DOWN, props['on_right_down'])
    instance.Unbind(wx.EVT_RIGHT_UP)
    if 'on_right_up' in props:
        instance.Bind(wx.EVT_RIGHT_UP, props['on_right_up'])
    instance.Unbind(wx.EVT_MOTION)
    if 'on_mouse_motion' in props:
        instance.Bind(wx.EVT_MOTION, props['on_mouse_motion'])
    instance.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
    if 'on_mouse_capture_lost' in props:
        instance.Bind(wx.EVT_MOUSE_CAPTURE_LOST, props['on_mouse_capture_lost'])
    return instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant