Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion arcade/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from arcade.gui.constructs import UIMessageBox
from arcade.gui.constructs import UIMessageBox, UIButtonRow
from arcade.gui.events import UIEvent
from arcade.gui.events import UIKeyEvent
from arcade.gui.events import UIKeyPressEvent
Expand Down Expand Up @@ -46,6 +46,7 @@
__all__ = [
"UIAnchorLayout",
"UIBoxLayout",
"UIButtonRow",
"UIGridLayout",
"UIManager",
"UIMessageBox",
Expand Down
62 changes: 60 additions & 2 deletions arcade/gui/constructs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
Constructs, are prepared widget combinations, you can use for common use-cases
"""
from arcade.gui.nine_patch import NinePatchTexture
from typing import Any, Optional

import arcade
from arcade.gui.events import UIOnActionEvent
from arcade.gui.events import UIOnActionEvent, UIOnClickEvent
from arcade.gui.mixins import UIMouseFilterMixin
from arcade.gui.nine_patch import NinePatchTexture
from arcade.gui.widgets.buttons import UIFlatButton
from arcade.gui.widgets.layout import UIBoxLayout, UIAnchorLayout
from arcade.gui.widgets.text import UITextArea
Expand Down Expand Up @@ -93,3 +94,60 @@ def _on_choice(self, event):
def on_action(self, event: UIOnActionEvent):
"""Called when button was pressed"""
pass


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: Optional[Any] = None,
size_hint_max: Optional[Any] = None,
space_between: int = 10,
style: Optional[Any] = None,
button_factory: type = UIFlatButton,
):
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")

self.button_factory = button_factory

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

def on_action(self, event: UIOnActionEvent):
pass

def _on_click(self, event: UIOnClickEvent):
self.dispatch_event("on_action", UIOnActionEvent(self, event.source.text))
65 changes: 4 additions & 61 deletions arcade/gui/examples/style_v2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from random import choice
from typing import Optional, Any

import arcade
from arcade.gui import UIManager, UIOnClickEvent
from arcade.gui.events import UIOnActionEvent
from arcade.gui.constructs import UIButtonRow
from arcade.gui.widgets.buttons import UIFlatButton
from arcade.gui.widgets.layout import UIAnchorLayout, UIBoxLayout
from arcade.gui.widgets.layout import UIAnchorLayout

STYLES = [
UIFlatButton.DEFAULT_STYLE,
Expand Down Expand Up @@ -80,62 +79,6 @@
]


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: Optional[Any] = None,
size_hint_max: Optional[Any] = None,
space_between: int = 10,
style: Optional[Any] = None,
button_factory: type = UIFlatButton,
):
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")

self.button_factory = button_factory

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

def on_action(self, event: UIOnActionEvent):
pass

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


class DemoWindow(arcade.Window):
def __init__(self):
super().__init__(800, 600, "UI Mockup", resizable=True)
Expand All @@ -151,15 +94,15 @@ def __init__(self):
anchor = self.manager.add(UIAnchorLayout())
row = anchor.add(UIButtonRow(button_factory=UIFlatButton))

button1 = row.add_button("Click me to switch style")
button1 = row.add_button("Click me to switch style", multiline=True)

@button1.event("on_click")
def change_style(event: UIOnClickEvent):
btn: UIFlatButton = event.source
btn.style = choice([s for s in STYLES if s is not btn.style])
btn.trigger_render()

button2 = row.add_button("Toggle disable")
button2 = row.add_button("Toggle disable", multiline=True)

@button2.event("on_click")
def toggle(*_):
Expand Down
1 change: 1 addition & 0 deletions doc/programming_guide/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Changes

* :py:class:`~arcade.gui.widgets.dropdown.UIDropdown`
* :py:class:`~arcade.gui.widgets.slider.UISlider`
* :py:class:`~arcade.gui.widgets.constructs.UIButtonRow` (`PR1580 <https://github.com/pythonarcade/arcade/pull/1580>`_ and `PR1253 <https://github.com/pythonarcade/arcade/pull/1253>`_)

* Arcade :py:class:`~arcade.gui.property.Property`:

Expand Down