From 49356d7bfdd979933a214f7a3aae1b6bfafc1fd2 Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Sun, 23 Jun 2024 20:14:32 +0200 Subject: [PATCH] feat(gui): adjust texture loading strategy in gui examples (#2126) --- arcade/examples/gui_scrollable_text.py | 5 ++- arcade/gui/examples/button_with_text.py | 46 ++++++++++++++--------- arcade/gui/examples/textured_slider.py | 1 + arcade/gui/examples/widget_gallery.py | 37 +++++++++++------- arcade/gui/experimental/password_input.py | 3 +- tests/unit/__init__.py | 0 6 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 tests/unit/__init__.py diff --git a/arcade/examples/gui_scrollable_text.py b/arcade/examples/gui_scrollable_text.py index e35d71e63..4811880b0 100644 --- a/arcade/examples/gui_scrollable_text.py +++ b/arcade/examples/gui_scrollable_text.py @@ -4,9 +4,12 @@ """ import arcade from arcade import load_texture + from arcade.gui import UIManager, UIInputText, UITextArea from arcade.gui.nine_patch import NinePatchTexture +TEX_GREY_PANEL = load_texture(":resources:gui_basic_assets/window/grey_panel.png") + LOREM_IPSUM = ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget pellentesque velit. " "Nam eu rhoncus nulla. Fusce ornare libero eget ex vulputate, vitae mattis orci eleifend. " @@ -35,7 +38,7 @@ def __init__(self): right=5, top=5, bottom=5, - texture=load_texture(":resources:gui_basic_assets/window/grey_panel.png")) + texture=TEX_GREY_PANEL) text_area = UITextArea( x=100, y=200, diff --git a/arcade/gui/examples/button_with_text.py b/arcade/gui/examples/button_with_text.py index 8065ce2eb..78f71483b 100644 --- a/arcade/gui/examples/button_with_text.py +++ b/arcade/gui/examples/button_with_text.py @@ -19,6 +19,16 @@ from arcade.gui.widgets.layout import UIGridLayout, UIAnchorLayout from arcade.gui.widgets.toggle import UITextureToggle +# Preload textures, because they are mostly used multiple times, so they are not loaded multiple times +ICON_SMALLER = load_texture(":resources:gui_basic_assets/icons/smaller.png") +ICON_LARGER = load_texture(":resources:gui_basic_assets/icons/larger.png") + +TEX_SWITCH_GREEN = load_texture(":resources:gui_basic_assets/toggle/switch_green.png") +TEX_SWITCH_RED = load_texture(":resources:gui_basic_assets/toggle/switch_red.png") +TEX_RED_BUTTON_NORMAL = load_texture(":resources:gui_basic_assets/red_button_normal.png") +TEX_RED_BUTTON_HOVER = load_texture(":resources:gui_basic_assets/red_button_hover.png") +TEX_RED_BUTTON_PRESS = load_texture(":resources:gui_basic_assets/red_button_press.png") + class MyView(arcade.View): def __init__(self): @@ -53,7 +63,7 @@ def __init__(self): flat_with_icon_left.place_text(align_x=+20) flat_with_icon_left.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/larger.png"), + texture=ICON_LARGER, width=30, height=30, ), @@ -67,7 +77,7 @@ def __init__(self): flat_with_icon_right.place_text(align_x=-20) flat_with_icon_right.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/smaller.png"), + texture=ICON_SMALLER, width=30, height=30, ), @@ -80,7 +90,7 @@ def __init__(self): flat_with_icon_right = UIFlatButton(text="UIFlatButton", width=200) flat_with_icon_right.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/smaller.png"), + texture=ICON_SMALLER, width=30, height=30, ), @@ -89,7 +99,7 @@ def __init__(self): ) flat_with_icon_right.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/smaller.png"), + texture=ICON_SMALLER, width=30, height=30, ), @@ -102,9 +112,9 @@ def __init__(self): texture_button = UITextureButton( text="UITextureButton", width=200, - texture=load_texture(":resources:gui_basic_assets/red_button_normal.png"), - texture_hovered=load_texture(":resources:gui_basic_assets/red_button_hover.png"), - texture_pressed=load_texture(":resources:gui_basic_assets/red_button_press.png"), + texture=TEX_RED_BUTTON_NORMAL, + texture_hovered=TEX_RED_BUTTON_HOVER, + texture_pressed=TEX_RED_BUTTON_PRESS, ) grid.add(texture_button, row_num=0, col_num=2) @@ -112,13 +122,13 @@ def __init__(self): texture_button_with_icon_left = UITextureButton( text="UITextureButton", width=200, - texture=load_texture(":resources:gui_basic_assets/red_button_normal.png"), - texture_hovered=load_texture(":resources:gui_basic_assets/red_button_hover.png"), - texture_pressed=load_texture(":resources:gui_basic_assets/red_button_press.png"), + texture=TEX_RED_BUTTON_NORMAL, + texture_hovered=TEX_RED_BUTTON_HOVER, + texture_pressed=TEX_RED_BUTTON_PRESS, ) texture_button_with_icon_left.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/smaller.png"), + texture=ICON_SMALLER, width=25, height=25, ), @@ -132,14 +142,14 @@ def __init__(self): text="UITextureButton\nwith a second line", multiline=True, width=200, - texture=load_texture(":resources:gui_basic_assets/red_button_normal.png"), - texture_hovered=load_texture(":resources:gui_basic_assets/red_button_hover.png"), - texture_pressed=load_texture(":resources:gui_basic_assets/red_button_press.png"), + texture=TEX_RED_BUTTON_NORMAL, + texture_hovered=TEX_RED_BUTTON_HOVER, + texture_pressed=TEX_RED_BUTTON_PRESS, ) texture_button_with_icon_left.place_text(anchor_x="left", align_x=45) texture_button_with_icon_left.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/smaller.png"), + texture=ICON_SMALLER, width=25, height=25, ), @@ -156,7 +166,7 @@ def __init__(self): texture_button_with_toggle.place_text(anchor_x="left", align_x=45) texture_button_with_toggle.add( child=UIImage( - texture=load_texture(":resources:gui_basic_assets/icons/smaller.png"), + texture=ICON_SMALLER, width=25, height=25, ), @@ -165,8 +175,8 @@ def __init__(self): ) toggle = texture_button_with_toggle.add( child=UITextureToggle( - on_texture=load_texture(":resources:gui_basic_assets/toggle/switch_red.png"), - off_texture=load_texture(":resources:gui_basic_assets/toggle/switch_green.png"), + on_texture=TEX_SWITCH_RED, + off_texture=TEX_SWITCH_GREEN, width=60, height=30, ), diff --git a/arcade/gui/examples/textured_slider.py b/arcade/gui/examples/textured_slider.py index 06ae6224c..0bdb183de 100644 --- a/arcade/gui/examples/textured_slider.py +++ b/arcade/gui/examples/textured_slider.py @@ -12,6 +12,7 @@ from arcade.gui import UIManager, UIAnchorLayout from arcade.gui.widgets.slider import UITextureSlider + class MyView(arcade.View): def __init__(self): super().__init__() diff --git a/arcade/gui/examples/widget_gallery.py b/arcade/gui/examples/widget_gallery.py index 78828d8cc..cd93b85f3 100644 --- a/arcade/gui/examples/widget_gallery.py +++ b/arcade/gui/examples/widget_gallery.py @@ -28,6 +28,15 @@ from arcade.gui.examples.textured_slider import UITextureSlider from arcade.gui.widgets.layout import UIAnchorLayout +# Preload textures, because they are mostly used multiple times, so they are not loaded multiple times +TEX_RED_BUTTON_NORMAL = load_texture(":resources:gui_basic_assets/red_button_normal.png") +TEX_RED_BUTTON_HOVER = load_texture(":resources:gui_basic_assets/red_button_hover.png") +TEX_RED_BUTTON_PRESS = load_texture(":resources:gui_basic_assets/red_button_press.png") +TEX_SWITCH_RED = load_texture(":resources:gui_basic_assets/toggle/switch_red.png") +TEX_SWITCH_GREEN = load_texture(":resources:gui_basic_assets/toggle/switch_green.png") +TEX_SLIDER_THUMB = arcade.load_texture(":resources:gui_basic_assets/slider_thumb.png") +TEX_SLIDER_TRACK = arcade.load_texture(":resources:gui_basic_assets/slider_track.png") + class MyView(arcade.View): def __init__(self): @@ -50,9 +59,9 @@ def __init__(self): col_num=0, row_num=0, child=UITextureButton( - texture=load_texture(":resources:gui_basic_assets/red_button_normal.png"), - texture_hovered=load_texture(":resources:gui_basic_assets/red_button_hover.png"), - texture_pressed=load_texture(":resources:gui_basic_assets/red_button_press.png"), + texture=TEX_RED_BUTTON_NORMAL, + texture_hovered=TEX_RED_BUTTON_HOVER, + texture_pressed=TEX_RED_BUTTON_PRESS, ), ) @@ -66,21 +75,21 @@ def __init__(self): right=5, bottom=5, top=5, - texture=load_texture(":resources:gui_basic_assets/red_button_normal.png"), + texture=TEX_RED_BUTTON_NORMAL, ), texture_hovered=NinePatchTexture( left=5, right=5, bottom=5, top=5, - texture=load_texture(":resources:gui_basic_assets/red_button_hover.png"), + texture=TEX_RED_BUTTON_HOVER, ), texture_pressed=NinePatchTexture( left=5, right=5, bottom=5, top=5, - texture=load_texture(":resources:gui_basic_assets/red_button_press.png"), + texture=TEX_RED_BUTTON_PRESS, ), ), ) @@ -101,14 +110,14 @@ def __init__(self): ) toggles.add( UITextureToggle( - on_texture=load_texture(":resources:gui_basic_assets/toggle/switch_green.png"), - off_texture=load_texture(":resources:gui_basic_assets/toggle/switch_red.png"), + on_texture=TEX_SWITCH_GREEN, + off_texture=TEX_SWITCH_RED, ) ) toggles.add( UITextureToggle( - on_texture=load_texture(":resources:gui_basic_assets/toggle/switch_green.png"), - off_texture=load_texture(":resources:gui_basic_assets/toggle/switch_red.png"), + on_texture=TEX_SWITCH_GREEN, + off_texture=TEX_SWITCH_RED, ) ).disabled = True @@ -117,8 +126,8 @@ def __init__(self): col_num=0, row_num=4, child=UITextureSlider( - track=arcade.load_texture(":resources:gui_basic_assets/slider_track.png"), - thumb=arcade.load_texture(":resources:gui_basic_assets/slider_thumb.png"), + track=TEX_SLIDER_TRACK, + thumb=TEX_SLIDER_THUMB, ), ) @@ -128,13 +137,13 @@ def __init__(self): row_num=5, child=UITextureSlider( track=NinePatchTexture( - texture=arcade.load_texture(":resources:gui_basic_assets/slider_track.png"), + texture=TEX_SLIDER_TRACK, left=30, right=33, bottom=18, top=18, ), - thumb=arcade.load_texture(":resources:gui_basic_assets/slider_thumb.png"), + thumb=TEX_SLIDER_THUMB, height=40, ), ) diff --git a/arcade/gui/experimental/password_input.py b/arcade/gui/experimental/password_input.py index 62011b521..7ef717f82 100644 --- a/arcade/gui/experimental/password_input.py +++ b/arcade/gui/experimental/password_input.py @@ -10,8 +10,7 @@ class UIPasswordInput(UIInputText): def on_event(self, event: UIEvent) -> Optional[bool]: if isinstance(event, UITextEvent): - event.text = event.text.replace("\n", "") # remove new lines! - event.text = event.text.replace("\r", "") # remove new lines! + event.text = event.text.replace("\n", "").replace("\r", "") # remove new lines! return super().on_event(event) def do_render(self, surface: Surface): diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 000000000..e69de29bb