Skip to content

Commit 3de6b5c

Browse files
authored
sneak in experimental pixelated support for UIManager (#2508)
1 parent 2a7a177 commit 3de6b5c

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

arcade/examples/gui/2_widgets.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@
1313

1414
import arcade
1515
from arcade.gui import (
16+
NinePatchTexture,
1617
UIAnchorLayout,
18+
UIBoxLayout,
1719
UIButtonRow,
20+
UIDropdown,
21+
UIDummy,
1822
UIFlatButton,
23+
UIImage,
1924
UIInputText,
2025
UILabel,
21-
UISpace,
26+
UIManager,
27+
UIMessageBox,
2228
UIOnActionEvent,
23-
UITextArea,
2429
UIOnChangeEvent,
25-
UITextureButton,
26-
UITextureToggle,
2730
UISlider,
28-
UITextureSlider,
29-
UIBoxLayout,
30-
UIImage,
31-
UIDummy,
31+
UISpace,
3232
UISpriteWidget,
33-
NinePatchTexture,
34-
UIDropdown,
35-
UIMessageBox,
36-
UIManager,
33+
UITextArea,
34+
UITextureButton,
35+
UITextureSlider,
36+
UITextureToggle,
3737
UIView,
3838
)
3939

@@ -612,6 +612,7 @@ def _show_other_widgets(self):
612612
text_area.with_padding(left=10, right=10)
613613
text_area.with_border(color=arcade.uicolor.GRAY_CONCRETE, width=2)
614614

615+
615616
def main():
616617
window = arcade.Window(title="GUI Example: Widget Gallery")
617618
window.show_view(GalleryView())

arcade/gui/surface.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(
3838
self._size = size
3939
self._pos = position
4040
self._pixel_ratio = pixel_ratio
41+
self._pixelated = False
4142

4243
self.texture = self.ctx.texture(self.size_scaled, components=4)
4344
self.fbo: Framebuffer = self.ctx.framebuffer(color_attachments=[self.texture])
@@ -142,7 +143,9 @@ def draw_texture(
142143

143144
tex.draw_rect(rect=LBWH(0, 0, width, height))
144145
else:
145-
arcade.draw_texture_rect(tex, LBWH(x, y, width, height), angle=angle, alpha=alpha)
146+
arcade.draw_texture_rect(
147+
tex, LBWH(x, y, width, height), angle=angle, alpha=alpha, pixelated=self._pixelated
148+
)
146149

147150
def draw_sprite(self, x: float, y: float, width: float, height: float, sprite: arcade.Sprite):
148151
"""Draw a sprite to the surface
@@ -157,7 +160,7 @@ def draw_sprite(self, x: float, y: float, width: float, height: float, sprite: a
157160
sprite.position = x + width // 2, y + height // 2
158161
sprite.width = width
159162
sprite.height = height
160-
arcade.draw_sprite(sprite)
163+
arcade.draw_sprite(sprite, pixelated=self._pixelated)
161164

162165
@contextmanager
163166
def activate(self) -> Generator[Self, None, None]:
@@ -226,11 +229,18 @@ def draw(
226229
Args:
227230
area: Limit the area in the surface we're drawing
228231
(l, b, w, h)
232+
pixelated: If True, the texture will be rendered pixelated
229233
"""
230234
# Set blend function
231235
blend_func = self.ctx.blend_func
232236
self.ctx.blend_func = self.blend_func_render
233237

238+
# Handle the pixelated shortcut if filter is not set
239+
if self._pixelated:
240+
self.texture.filter = self.ctx.NEAREST, self.ctx.NEAREST
241+
else:
242+
self.texture.filter = self.ctx.LINEAR, self.ctx.LINEAR
243+
234244
self.texture.use(0)
235245
self._program["pos"] = self._pos
236246
self._program["size"] = self._size

arcade/gui/ui_manager.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def on_draw():
8787
"""
8888

8989
_enabled = False
90+
_pixelated = False
91+
"""Experimental feature to pixelate the UI, all textures will be rendered pixelated,
92+
which will mostly influence scaled background images.
93+
This property has to be set right after the UIManager is created."""
9094

9195
DEFAULT_LAYER = 0
9296
OVERLAY_LAYER = 10
@@ -198,6 +202,7 @@ def _get_surface(self, layer: int) -> Surface:
198202
size=self.window.get_size(),
199203
pixel_ratio=self.window.get_pixel_ratio(),
200204
)
205+
self._surfaces[layer]._pixelated = self._pixelated
201206

202207
return self._surfaces[layer]
203208

@@ -316,7 +321,7 @@ def on_update(self, time_delta):
316321
"""Dispatches an update event to all widgets in the UIManager."""
317322
return self.dispatch_ui_event(UIOnUpdateEvent(self, time_delta))
318323

319-
def draw(self) -> None:
324+
def draw(self, pixelated=False) -> None:
320325
"""Will draw all widgets to the window.
321326
322327
UIManager caches all rendered widgets into a framebuffer (something like a

arcade/gui/widgets/text.py

+27
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,33 @@ def text(self, value):
194194
else:
195195
self.trigger_full_render()
196196

197+
@property
198+
def font_name(self) -> FontNameOrNames:
199+
"""Font name of the label. Use :py:meth:`~arcade.gui.UILabel.update_font` to change."""
200+
return self._label.font_name
201+
202+
@property
203+
def font_size(self) -> float:
204+
"""Font size of the label. Use :py:meth:`~arcade.gui.UILabel.update_font` to change."""
205+
return self._label.font_size
206+
207+
@property
208+
def font_color(self) -> Color:
209+
"""Font color of the label. Use :py:meth:`~arcade.gui.UILabel.update_font` to change."""
210+
return self._label.color
211+
212+
@property
213+
def bold(self) -> bool | str:
214+
"""Return if the label is in bold style.
215+
Use :py:meth:`~arcade.gui.UILabel.update_font` to change."""
216+
return self._label.bold
217+
218+
@property
219+
def italic(self) -> bool | str:
220+
"""Return if the label is in italic style.
221+
Use :py:meth:`~arcade.gui.UILabel.update_font` to change."""
222+
return self._label.italic
223+
197224
def _update_label(self):
198225
"""Update the position and size of the label.
199226

0 commit comments

Comments
 (0)