arcade icon indicating copy to clipboard operation
arcade copied to clipboard

Created UIButtonRow.

Open JackAshwell11 opened this issue 3 years ago • 2 comments

JackAshwell11 avatar Jun 21 '22 09:06 JackAshwell11

Resolves #1248

JackAshwell11 avatar Jun 21 '22 09:06 JackAshwell11

Hi, I checked out your PR and after some feedback session with @pushfoo, I would be happy to merge it.

We talked through a few changes, would you like to update your PR or should we just merge it and then update the class?

from typing import Any

import arcade
from arcade.gui import UIBoxLayout, UIFlatButton, UIOnClickEvent, UIManager, UIAnchorLayout
from arcade.gui.events import UIOnActionEvent


class UIButtonRow(UIBoxLayout):
    """
    Places buttons in a row.
    :param bool vertical: Whether the button row is vertical or not.
    :param str align: Where to align the button row.
    :param Any size_hint: Tuple of floats (0.0 - 1.0) of how much space of the parent should be requested.
    :param size_hint_min: Min width and height in pixel.
    :param size_hint_max: Max width and height in pixel.
    :param int space_between: The space between the children.
    :param Any style: Not used.
    :param Tuple[str, ...] button_labels: The labels for the buttons.
    :param Callable callback: The callback function which will receive the text of the clicked button.
    """

    def __init__(
        self,
        vertical: bool = False,
        align: str = "center",
        size_hint: Any = (0, 0),
        size_hint_min: Any = None,
        size_hint_max: Any = None,
        space_between: int = 10,
        style: Any = None,
    ):
        super().__init__(
            vertical=vertical,
            align=align,
            size_hint=size_hint,
            size_hint_min=size_hint_min,
            size_hint_max=size_hint_max,
            space_between=space_between,
            style=style
        )
        self.register_event_type("on_action")

    def add_button(self, label: str, *, on_click=None, style=None):
        button = UIFlatButton(text=label, style=style)
        button.on_click = self._on_click  # type: ignore
        self.add(button)

        if on_click:
            # Add on_click callback as event handler
            button.event("on_click")(on_click)

    def on_action(self, event: UIOnActionEvent):
        pass

    def _on_click(self, event: UIOnClickEvent):
        self.dispatch_event("on_action", UIOnActionEvent(event.source, event.source.text))


class DemoWindow(arcade.Window):
    def __init__(self):
        super().__init__()
        self.mng = UIManager()
        self.mng.enable()

        row = UIButtonRow()
        row.add_button("Cancel", on_click=self.click_cancel)

        def click_ok(event: UIOnClickEvent):
            print("Thanks")

        row.add_button("OK", on_click=click_ok)

        self.mng.add(UIAnchorLayout(
            children=[row]
        ))

    def click_cancel(self, event: UIOnClickEvent):
        print("Bye")

    def on_draw(self):
        self.clear()
        self.mng.draw()

if __name__ == '__main__':
    DemoWindow().run()

eruvanos avatar Jun 22 '22 19:06 eruvanos

I rebased the commits and added a PR #1580 with the commit under your name @Aspect1103 .

Thank you for the contribution and sorry for the long delay until this was cleared.

eruvanos avatar Feb 24 '23 15:02 eruvanos