Skip to content

Created UIButtonRow.#1253

Closed
JackAshwell11 wants to merge 4 commits intopythonarcade:developmentfrom
JackAshwell11:uibuttonrow
Closed

Created UIButtonRow.#1253
JackAshwell11 wants to merge 4 commits intopythonarcade:developmentfrom
JackAshwell11:uibuttonrow

Conversation

@JackAshwell11
Copy link
Copy Markdown
Contributor

No description provided.

@JackAshwell11
Copy link
Copy Markdown
Contributor Author

Resolves #1248

@eruvanos
Copy link
Copy Markdown
Member

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()

Copy link
Copy Markdown
Member

@pushfoo pushfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me, although my understanding of the event system is still limited.

@eruvanos eruvanos added the gui Related to arcade GUI (sub module arcade.gui) label Sep 25, 2022
@eruvanos
Copy link
Copy Markdown
Member

eruvanos commented Feb 24, 2023

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 eruvanos mentioned this pull request Feb 24, 2023
@eruvanos eruvanos closed this Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gui Related to arcade GUI (sub module arcade.gui)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants