1+ from unreal_engine import SWindow , SBox , SPythonComboBox , STextBlock , SBorder , SVerticalBox , SEditableTextBox , SHorizontalBox , SButton
2+ from unreal_engine .enums import EHorizontalAlignment , EVerticalAlignment
3+ from unreal_engine import FLinearColor
4+ from unreal_engine .structs import SlateColor , Margin
5+
6+ class DynamicComboBox :
7+
8+ def __init__ (self , items ):
9+ self .box = SBox (height_override = 100 , min_desired_width = 400 )
10+ self .items = items
11+ self .build_combo_box ()
12+ self .combo_box .set_selected_item (self .items [0 ])
13+
14+ def get_widget (self ):
15+ return self .box
16+
17+ def generate_combo_box_widget (self , item ):
18+ return STextBlock (text = item )
19+
20+ def append (self , item , committed ):
21+ if item and item not in self .items :
22+ self .items .append (item )
23+ self .build_combo_box ()
24+ self .combo_box .set_selected_item (item )
25+
26+ def get_current_item (self ):
27+ return self .combo_box .get_selected_item ()
28+
29+ def build_combo_box (self ):
30+ self .combo_box = SPythonComboBox (options_source = self .items , on_generate_widget = self .generate_combo_box_widget , content = STextBlock (text = self .get_current_item ))
31+ self .box .set_content (self .combo_box )
32+
33+
34+
35+ dynamic_combo_box = DynamicComboBox (['one' ,'two' , 'three' ])
36+
37+ # the final backslash is required for the 'pretty syntax'
38+ SWindow (client_size = (1024 , 576 ), title = 'DynamicComboBox' )\
39+ (
40+ SBorder (color_and_opacity = FLinearColor (0 , 1 , 0 , 1 ), border_background_color = SlateColor (SpecifiedColor = FLinearColor (1 , 0 , 0 , 1 )))
41+ (
42+ SBox (h_align = EHorizontalAlignment .HAlign_Center , v_align = EVerticalAlignment .VAlign_Center )
43+ (
44+ SBorder (color_and_opacity = FLinearColor (0 , 1 , 0 , 1 ), border_background_color = SlateColor (SpecifiedColor = FLinearColor (0 , 1 , 0 , 1 )))
45+ (
46+ SVerticalBox ()
47+ (
48+ STextBlock (text = 'Hello 1' , tool_tip_text = 'Test Tool Tip' )
49+ )
50+ (
51+ STextBlock (text = 'Hello 2' )
52+ )
53+ (
54+ dynamic_combo_box .get_widget ()
55+ )
56+ (
57+ SHorizontalBox ()
58+ (
59+ SEditableTextBox (text = lambda : '' , on_text_committed = dynamic_combo_box .append ),
60+ fill_width = 0.8
61+ )
62+ (
63+ SButton (text = 'Ok' ),
64+ fill_width = 0.2
65+ )
66+ )
67+ )
68+ )
69+ )
70+ )
0 commit comments