Skip to content

Commit

Permalink
feat(gui): adjust texture loading strategy in gui examples (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
eruvanos authored Jun 23, 2024
1 parent 29c285c commit 49356d7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
5 changes: 4 additions & 1 deletion arcade/examples/gui_scrollable_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand Down Expand Up @@ -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,
Expand Down
46 changes: 28 additions & 18 deletions arcade/gui/examples/button_with_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -102,23 +112,23 @@ 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)

# UITextureButton with icon left
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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand Down
1 change: 1 addition & 0 deletions arcade/gui/examples/textured_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down
37 changes: 23 additions & 14 deletions arcade/gui/examples/widget_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
),
)

Expand All @@ -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,
),
),
)
Expand All @@ -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

Expand All @@ -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,
),
)

Expand All @@ -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,
),
)
Expand Down
3 changes: 1 addition & 2 deletions arcade/gui/experimental/password_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Empty file added tests/unit/__init__.py
Empty file.

0 comments on commit 49356d7

Please sign in to comment.