Skip to content

Commit

Permalink
gui: Check transparency to determine if background clears the widgets…
Browse files Browse the repository at this point in the history
… area or use rect draw.

This can be overwritten by setting UIWidget._strong_background for now.
  • Loading branch information
eruvanos committed Nov 19, 2024
1 parent 3663d70 commit fe61ff7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 15 additions & 6 deletions arcade/gui/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

from abc import ABC
from typing import NamedTuple, Iterable, Optional, Union, TYPE_CHECKING, TypeVar, Tuple, List, Dict
from typing import Dict, Iterable, List, NamedTuple, Optional, TYPE_CHECKING, Tuple, TypeVar, Union

from pyglet.event import EventDispatcher, EVENT_HANDLED, EVENT_UNHANDLED
from pyglet.event import EVENT_HANDLED, EVENT_UNHANDLED, EventDispatcher
from pyglet.math import Vec2
from typing_extensions import Self

import arcade
from arcade import Sprite, Texture, LBWH, Rect
from arcade import LBWH, Rect, Sprite, Texture
from arcade.color import TRANSPARENT_BLACK
from arcade.gui.events import (
UIEvent,
Expand All @@ -19,9 +19,9 @@
UIOnUpdateEvent,
)
from arcade.gui.nine_patch import NinePatchTexture
from arcade.gui.property import Property, bind, ListProperty
from arcade.gui.property import ListProperty, Property, bind
from arcade.gui.surface import Surface
from arcade.types import Color, AnchorPoint, AsFloat
from arcade.types import AnchorPoint, AsFloat, Color
from arcade.utils import copy_dunders_unimplemented

if TYPE_CHECKING:
Expand Down Expand Up @@ -73,6 +73,11 @@ class UIWidget(EventDispatcher, ABC):
_padding_right = Property(0)
_padding_bottom = Property(0)
_padding_left = Property(0)
_strong_background = Property(False)
"""If True, the background will clear the surface, even if it is not fully opaque.
This is not part of the public API and subject to change.
UILabel have a strong background if set.
"""

def __init__(
self,
Expand Down Expand Up @@ -116,6 +121,7 @@ def __init__(
bind(self, "_padding_right", self.trigger_render)
bind(self, "_padding_bottom", self.trigger_render)
bind(self, "_padding_left", self.trigger_render)
bind(self, "_strong_background", self.trigger_render)

def add(self, child: W, **kwargs) -> W:
"""Add a widget as a child.
Expand Down Expand Up @@ -253,7 +259,10 @@ def do_render_base(self, surface: Surface):

# draw background
if self._bg_color:
surface.clear(self._bg_color)
if self._bg_color.a == 255 or self._strong_background:
surface.clear(self._bg_color)
else:
arcade.draw_rect_filled(LBWH(0, 0, self.width, self.height), color=self._bg_color)
# draw background texture
if self._bg_tex:
surface.draw_texture(x=0, y=0, width=self.width, height=self.height, tex=self._bg_tex)
Expand Down
4 changes: 3 additions & 1 deletion arcade/gui/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from arcade.gui.widgets import UIWidget
from arcade.gui.widgets.layout import UIAnchorLayout
from arcade.text import FontNameOrNames
from arcade.types import LBWH, RGBA255, Color, RGBOrA255
from arcade.types import Color, LBWH, RGBA255, RGBOrA255


class UILabel(UIWidget):
Expand Down Expand Up @@ -119,6 +119,8 @@ def __init__(
multiline=multiline,
**kwargs,
)
self._strong_background = True

if adaptive_multiline:
# +1 is required to prevent line wrap
width = self._label.content_width + 1
Expand Down

0 comments on commit fe61ff7

Please sign in to comment.