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
10 changes: 5 additions & 5 deletions arcade/examples/gui/6_size_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self):
width=800, # give text enough space to not wrap
font_size=14,
size_hint=(1, 1),
bold=True,
)
)
text.with_padding(top=10)
Expand All @@ -97,11 +98,11 @@ def __init__(self):
)

width_slider_box = center_box.add(UIBoxLayout(vertical=False, size_hint=(1, 0)))
width_slider_box.add(UILabel("Modify size_hint:"))
width_slider_box.add(UILabel("Modify size_hint:", bold=True))
width_slider = width_slider_box.add(
arcade.gui.UISlider(min_value=0, max_value=10, value=0, size_hint=None, height=30)
)
width_value = width_slider_box.add(UILabel())
width_value = width_slider_box.add(UILabel(bold=True))

content_anchor.add(UISpace(height=50))

Expand All @@ -110,9 +111,9 @@ def __init__(self):
demo_box.with_background(color=arcade.uicolor.GRAY_ASBESTOS)

# create a dummy widget to show the effect of the sliders
dummy1 = demo_box.add(UILabel())
dummy1 = demo_box.add(UILabel(bold=True))
dummy1.with_background(color=arcade.uicolor.YELLOW_ORANGE)
dummy2 = demo_box.add(UILabel())
dummy2 = demo_box.add(UILabel(bold=True))
dummy2.with_background(color=arcade.uicolor.GREEN_EMERALD)

def update_size_hint_value(value: float):
Expand Down Expand Up @@ -145,4 +146,3 @@ def main():

if __name__ == "__main__":
main()

6 changes: 6 additions & 0 deletions arcade/gui/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ class UITextArea(UIWidget):
success.
font_size: Font size of font.
text_color: Color of the text.
bold: If enabled, the label's text will be in a **bold** style.
italic: If enabled, the label's text will be in an *italic*
multiline: If enabled, a ``\\n`` will start a new line.
scroll_speed: Speed of mouse scrolling.
size_hint: A tuple of floats between 0 and 1 defining the amount
Expand All @@ -650,6 +652,8 @@ def __init__(
text: str = "",
font_name=("arial", "calibri"),
font_size: float = 12,
bold=False,
italic=False,
text_color: RGBA255 = arcade.color.WHITE,
multiline: bool = True,
scroll_speed: Optional[float] = None,
Expand Down Expand Up @@ -689,6 +693,8 @@ def __init__(
font_name=font_name,
font_size=font_size,
color=Color.from_iterable(text_color),
bold=bold,
italic=italic,
),
)

Expand Down
2 changes: 2 additions & 0 deletions arcade/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def __init__(
batch: pyglet.graphics.Batch | None = None,
group: pyglet.graphics.Group | None = None,
z: float = 0,
**kwargs,
):
# Raises a RuntimeError if no window for better user feedback
arcade.get_window()
Expand Down Expand Up @@ -255,6 +256,7 @@ def __init__(
rotation=rotation, # type: ignore # pending https://github.com/pyglet/pyglet/issues/843
batch=batch,
group=group,
**kwargs,
)

def __enter__(self):
Expand Down
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
WINDOW = None
OFFSCREEN = None

arcade.resources.load_system_fonts()

def make_window_caption(request=None, prefix='Testing', sep=' - ') -> str:

def make_window_caption(request=None, prefix="Testing", sep=" - ") -> str:
"""Centralizes test name customization.

It helps with:
Expand Down Expand Up @@ -179,11 +181,11 @@ def size(self):
@size.setter
def size(self, size):
self.window.size = size

@property
def center_x(self):
return self.window.center_x

@property
def center_y(self):
return self.window.center_y
Expand Down
50 changes: 25 additions & 25 deletions tests/unit/gui/test_uilabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

def test_constructor_only_text_no_size(window):
"""Should fit text"""
label = UILabel(text="Example")
label = UILabel(text="Example", font_name="Kenney Pixel")

assert label.rect.width == pytest.approx(
63, abs=10
) # on windows the width differs about 6 pixel
assert label.rect.height == pytest.approx(19, abs=1)
assert label.rect.width == 43
assert label.rect.height == 12


def test_constructor_text_and_size(window):
Expand All @@ -28,11 +26,11 @@ def test_constructor_size_smaller_then_text(window):


def test_constructor_fix_width_and_multiline(window):
label = UILabel(text="E x a m p l e", width=10, multiline=True)
assert label.rect.left == pytest.approx(0, abs=2)
assert label.rect.bottom == pytest.approx(0, abs=2)
assert label.rect.width == pytest.approx(10, abs=2)
assert label.rect.height == pytest.approx(133, abs=7)
label = UILabel(text="E x a m p l e", width=10, multiline=True, font_name="Kenney Pixel")
assert label.rect.left == 0
assert label.rect.bottom == 0
assert label.rect.width == 10
assert label.rect.height == 84


def test_constructor_adaptive_width_support_for_multiline_text(window):
Expand All @@ -47,23 +45,23 @@ def test_constructor_adaptive_width_support_for_multiline_text(window):


def test_with_border_keeps_previous_size(window):
label = UILabel(text="Example")
assert label.rect.width == pytest.approx(63, abs=10)
assert label.rect.height == pytest.approx(19, abs=6)
label = UILabel(text="Example", font_name="Kenney Pixel")
assert label.rect.width == 43
assert label.rect.height == 12

label.with_border()
assert label.rect.width == pytest.approx(63, abs=10)
assert label.rect.height == pytest.approx(19, abs=6)
assert label.rect.width == 43
assert label.rect.height == 12


def test_with_padding_keeps_previous_size(window):
label = UILabel(text="Example")
assert label.rect.width == pytest.approx(63, abs=10)
assert label.rect.height == pytest.approx(19, abs=6)
label = UILabel(text="Example", font_name="Kenney Pixel")
assert label.rect.width == 43
assert label.rect.height == 12

label.with_padding(all=2)
assert label.rect.width == pytest.approx(63, abs=10)
assert label.rect.height == pytest.approx(19, abs=6)
assert label.rect.width == 43
assert label.rect.height == 12


def test_internals_text_placed_at_0_0(window):
Expand Down Expand Up @@ -187,21 +185,22 @@ def test_integration_with_layout_fit_to_content(ui):
label = UILabel(
text="Example",
size_hint=(0, 0), # default, enables auto size
font_name="Kenney Pixel",
)

ui.add(label)
ui.execute_layout()

# auto size should fit the text
assert label.rect.width == pytest.approx(63, abs=10)
assert label.rect.height == pytest.approx(19, abs=6)
assert label.rect.width == 44
assert label.rect.height == 12

# even when text changed
label.text = "Example, which is way longer"
ui.execute_layout()

assert label.rect.width > 63
assert label.rect.height == pytest.approx(19, abs=6)
assert label.rect.height == 12

# or font
label.text = "Example"
Expand All @@ -217,12 +216,13 @@ def test_fit_content_overrides_width(ui):
text="Example",
width=100,
height=50,
font_name="Kenney Pixel",
)

label.fit_content()

assert label.rect.width == pytest.approx(63, abs=10)
assert label.rect.height == pytest.approx(19, abs=6)
assert label.rect.width == 44
assert label.rect.height == 12


def test_fit_content_uses_adaptive_multiline_width(ui):
Expand Down
Loading