1 module evael.graphics.gui.controls.ComboBox;
2 
3 import evael.graphics.gui.controls.Button;
4 import evael.graphics.gui.controls.ListBox;
5 import evael.graphics.gui.controls.TextBlock;
6 import evael.graphics.gui.controls.Container;
7 
8 import evael.utils.Math;
9 
10 import evael.utils.Size;
11 import evael.utils.Color;
12 
13 class ComboBox : Container
14 {
15     protected alias OnItemSelected = void delegate(ListBoxItem item);
16 	protected OnItemSelected m_onItemSelectedEvent;
17 
18     private ListBox m_listBox;
19 
20     /// Initial ComboBox size (when collapsed)
21     private Size!int m_initialSize;
22 
23     /// Text displayed in ComboBox input
24     private TextBlock m_text;
25 
26     /// Selected item
27     private ListBoxItem m_selectedItem;
28 
29 	public this(in vec2 position, in Size!int size, in int listHeight)
30 	{
31 		super(position, size);
32 
33         this.m_initialSize = size;
34         this.m_autoResize = false;
35 		this.m_name = "comboBox";
36 
37         this.m_listBox = new ListBox(ListBox.Type.List, 0, size.height, size.width, listHeight);
38         this.m_listBox.onItemSelectedEvent = &this.onItemSelected;
39         
40         this.m_listBox.hide();
41 
42         auto showListBoxButton = new Button(">");
43         showListBoxButton.type = Button.Type.Icon;
44         showListBoxButton.icon = Icon.DownOpenBig;
45         showListBoxButton.dock = Dock.Right;
46         showListBoxButton.onClickEvent = (sender) 
47         {
48             if(this.m_listBox.isVisible)
49             {
50                 this.m_listBox.hide();
51                 this.m_size.height = size.height;
52             }
53             else
54             {
55                 this.m_listBox.show();
56                 this.m_size.height = size.height + this.m_listBox.size.height;
57             }
58         };
59 
60         this.m_text = new TextBlock(5, 2, size.width - 40, size.height);
61 		this.m_text.textAlignement = TextBlock.TextAlignement.Left | TextBlock.TextAlignement.Top;
62 
63         this.addChild(showListBoxButton);
64         this.addChild(this.m_text);
65         this.addChild(this.m_listBox);
66 	}
67 
68 	public this(in float x, in float y, in int width = 0, in int height = 0, in int listHeight = 200)
69 	{
70 		this(vec2(x, y), Size!int(width, height), listHeight);
71 	}
72 
73 	public override void draw(in float deltaTime)
74 	{
75 		super.draw(deltaTime);
76 	}
77     
78     public override void initialize()
79     {
80         super.initialize();
81 
82         this.m_text.theme.background.type = Background.Type.Transparent;
83     }
84 
85     /**
86      * Adds item
87      * Params:
88      *      item : item to add
89      */
90     public void addItem(in string item)
91     {
92         this.m_listBox.addItem(item);
93     }
94 
95     /**
96      * Selects item by index
97      * Params:
98      *      index : item index in list
99      */
100     public void selectItem(in uint index)
101     {
102         this.onItemSelected(this.m_listBox.getItem(index));
103     }
104 
105     /**
106      * Event called when an item has been selected
107      */
108     private void onItemSelected(ListBoxItem item)
109     {
110         if(item is null)
111         {
112             return;
113         }
114 
115         this.m_listBox.hide();
116         this.m_size.height = this.m_initialSize.height;
117     
118         this.m_selectedItem = item;
119         this.m_text.text = item.text;
120 
121         this.m_onItemSelectedEvent(item);
122     }
123 
124     @property
125     {
126         public void onItemSelectedEvent(OnItemSelected callback) nothrow @nogc
127 		{
128 			this.m_onItemSelectedEvent = callback;
129 		}
130     }
131 }