11"""
2- Create a slider using a custom texture subclass
2+ Create a slider using textures.
33
44The initial theme is a 90s sci-fi style, but you can replace the textures
55in this example to match the theme of your project.
66
77If arcade and Python are properly installed, you can run this example with:
88python -m arcade.gui.examples.textured_slider
99"""
10- from __future__ import annotations
11-
12- from typing import Union
1310
1411import arcade
15- from arcade import Texture
16- from arcade .gui import UIManager , Surface , UIAnchorLayout , NinePatchTexture
17- from arcade .gui .widgets .slider import UISlider , UISliderStyle
18-
19-
20- class UITextureSlider (UISlider ):
21- """
22- A custom slider subclass which supports textures.
23-
24- You can copy this as-is into your own project, or you can modify
25- the class to have more features as needed.
26- """
27-
28- def __init__ (
29- self ,
30- bar : Union [Texture , NinePatchTexture ],
31- thumb : Union [Texture , NinePatchTexture ],
32- style = None ,
33- ** kwargs
34- ):
35- self .bar = bar
36- self .thumb = thumb
37- style = style or UISlider .DEFAULT_STYLE
38-
39- super ().__init__ (style = style , ** kwargs )
40-
41- def do_render (self , surface : Surface ):
42- style : UISliderStyle = self .get_current_style () # type: ignore
43-
44- self .prepare_render (surface )
45-
46- surface .draw_texture (0 , 0 , self .width , self .height , self .bar )
47-
48- # TODO accept constructor params
49- slider_height = self .height // 4
50- slider_left_x = self ._x_for_value (self .vmin )
51- cursor_center_x = self .value_x
52-
53- slider_bottom = (self .height - slider_height ) // 2
54-
55- # slider
56- arcade .draw_xywh_rectangle_filled (
57- slider_left_x - self .x ,
58- slider_bottom ,
59- cursor_center_x - slider_left_x ,
60- slider_height ,
61- style .filled_bar ,
62- )
63-
64- # cursor
65- rel_cursor_x = cursor_center_x - self .x
66- surface .draw_texture (
67- x = rel_cursor_x - self .thumb .width // 4 + 2 ,
68- y = 0 ,
69- width = self .thumb .width // 2 ,
70- height = self .height ,
71- tex = self .thumb ,
72- )
12+ from arcade .gui import UIManager , UIAnchorLayout
13+ from arcade .gui .widgets .slider import UITextureSlider
7314
7415
7516class MyView (arcade .View ):
7617 def __init__ (self ):
7718 super ().__init__ ()
7819 self .ui = UIManager ()
7920
80- bar_tex = arcade .load_texture (":resources:gui_basic_assets/slider_bar .png" )
21+ track_tex = arcade .load_texture (":resources:gui_basic_assets/slider_track .png" )
8122 thumb_tex = arcade .load_texture (":resources:gui_basic_assets/slider_thumb.png" )
82- self .slider = UITextureSlider (bar_tex , thumb_tex )
23+ self .slider = UITextureSlider (track_tex , thumb_tex )
24+
25+ @self .slider .event
26+ def on_change (event ):
27+ print (f"Slider value: { event } " )
8328
8429 # Add button to UIManager, use UIAnchorWidget defaults to center on screen
8530 self .ui .add (UIAnchorLayout (children = [self .slider ]))
@@ -101,7 +46,7 @@ def on_draw(self):
10146 self .ui .draw ()
10247
10348
104- if __name__ == ' __main__' :
49+ if __name__ == " __main__" :
10550 window = arcade .Window (800 , 600 , "UIExample" , resizable = True )
10651 window .show_view (MyView ())
10752 window .run ()
0 commit comments