diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CGUIListBox.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CGUIListBox.cpp | 1820 |
1 files changed, 910 insertions, 910 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CGUIListBox.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CGUIListBox.cpp index adbc28d..8f7758f 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CGUIListBox.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CGUIListBox.cpp | |||
@@ -1,910 +1,910 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | 1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt |
2 | // This file is part of the "Irrlicht Engine". | 2 | // This file is part of the "Irrlicht Engine". |
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h |
4 | 4 | ||
5 | #include "CGUIListBox.h" | 5 | #include "CGUIListBox.h" |
6 | #ifdef _IRR_COMPILE_WITH_GUI_ | 6 | #ifdef _IRR_COMPILE_WITH_GUI_ |
7 | 7 | ||
8 | #include "CGUIListBox.h" | 8 | #include "CGUIListBox.h" |
9 | #include "IGUISkin.h" | 9 | #include "IGUISkin.h" |
10 | #include "IGUIEnvironment.h" | 10 | #include "IGUIEnvironment.h" |
11 | #include "IVideoDriver.h" | 11 | #include "IVideoDriver.h" |
12 | #include "IGUIFont.h" | 12 | #include "IGUIFont.h" |
13 | #include "IGUISpriteBank.h" | 13 | #include "IGUISpriteBank.h" |
14 | #include "CGUIScrollBar.h" | 14 | #include "CGUIScrollBar.h" |
15 | #include "os.h" | 15 | #include "os.h" |
16 | 16 | ||
17 | namespace irr | 17 | namespace irr |
18 | { | 18 | { |
19 | namespace gui | 19 | namespace gui |
20 | { | 20 | { |
21 | 21 | ||
22 | //! constructor | 22 | //! constructor |
23 | CGUIListBox::CGUIListBox(IGUIEnvironment* environment, IGUIElement* parent, | 23 | CGUIListBox::CGUIListBox(IGUIEnvironment* environment, IGUIElement* parent, |
24 | s32 id, core::rect<s32> rectangle, bool clip, | 24 | s32 id, core::rect<s32> rectangle, bool clip, |
25 | bool drawBack, bool moveOverSelect) | 25 | bool drawBack, bool moveOverSelect) |
26 | : IGUIListBox(environment, parent, id, rectangle), Selected(-1), | 26 | : IGUIListBox(environment, parent, id, rectangle), Selected(-1), |
27 | ItemHeight(0),ItemHeightOverride(0), | 27 | ItemHeight(0),ItemHeightOverride(0), |
28 | TotalItemHeight(0), ItemsIconWidth(0), Font(0), IconBank(0), | 28 | TotalItemHeight(0), ItemsIconWidth(0), Font(0), IconBank(0), |
29 | ScrollBar(0), selectTime(0), LastKeyTime(0), Selecting(false), DrawBack(drawBack), | 29 | ScrollBar(0), selectTime(0), LastKeyTime(0), Selecting(false), DrawBack(drawBack), |
30 | MoveOverSelect(moveOverSelect), AutoScroll(true), HighlightWhenNotFocused(true) | 30 | MoveOverSelect(moveOverSelect), AutoScroll(true), HighlightWhenNotFocused(true) |
31 | { | 31 | { |
32 | #ifdef _DEBUG | 32 | #ifdef _DEBUG |
33 | setDebugName("CGUIListBox"); | 33 | setDebugName("CGUIListBox"); |
34 | #endif | 34 | #endif |
35 | 35 | ||
36 | IGUISkin* skin = Environment->getSkin(); | 36 | IGUISkin* skin = Environment->getSkin(); |
37 | const s32 s = skin->getSize(EGDS_SCROLLBAR_SIZE); | 37 | const s32 s = skin->getSize(EGDS_SCROLLBAR_SIZE); |
38 | 38 | ||
39 | ScrollBar = new CGUIScrollBar(false, Environment, this, -1, | 39 | ScrollBar = new CGUIScrollBar(false, Environment, this, -1, |
40 | core::rect<s32>(RelativeRect.getWidth() - s, 0, RelativeRect.getWidth(), RelativeRect.getHeight()), | 40 | core::rect<s32>(RelativeRect.getWidth() - s, 0, RelativeRect.getWidth(), RelativeRect.getHeight()), |
41 | !clip); | 41 | !clip); |
42 | ScrollBar->setSubElement(true); | 42 | ScrollBar->setSubElement(true); |
43 | ScrollBar->setTabStop(false); | 43 | ScrollBar->setTabStop(false); |
44 | ScrollBar->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); | 44 | ScrollBar->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); |
45 | ScrollBar->setVisible(false); | 45 | ScrollBar->setVisible(false); |
46 | ScrollBar->setPos(0); | 46 | ScrollBar->setPos(0); |
47 | 47 | ||
48 | setNotClipped(!clip); | 48 | setNotClipped(!clip); |
49 | 49 | ||
50 | // this element can be tabbed to | 50 | // this element can be tabbed to |
51 | setTabStop(true); | 51 | setTabStop(true); |
52 | setTabOrder(-1); | 52 | setTabOrder(-1); |
53 | 53 | ||
54 | updateAbsolutePosition(); | 54 | updateAbsolutePosition(); |
55 | } | 55 | } |
56 | 56 | ||
57 | 57 | ||
58 | //! destructor | 58 | //! destructor |
59 | CGUIListBox::~CGUIListBox() | 59 | CGUIListBox::~CGUIListBox() |
60 | { | 60 | { |
61 | if (ScrollBar) | 61 | if (ScrollBar) |
62 | ScrollBar->drop(); | 62 | ScrollBar->drop(); |
63 | 63 | ||
64 | if (Font) | 64 | if (Font) |
65 | Font->drop(); | 65 | Font->drop(); |
66 | 66 | ||
67 | if (IconBank) | 67 | if (IconBank) |
68 | IconBank->drop(); | 68 | IconBank->drop(); |
69 | } | 69 | } |
70 | 70 | ||
71 | 71 | ||
72 | //! returns amount of list items | 72 | //! returns amount of list items |
73 | u32 CGUIListBox::getItemCount() const | 73 | u32 CGUIListBox::getItemCount() const |
74 | { | 74 | { |
75 | return Items.size(); | 75 | return Items.size(); |
76 | } | 76 | } |
77 | 77 | ||
78 | 78 | ||
79 | //! returns string of a list item. the may be a value from 0 to itemCount-1 | 79 | //! returns string of a list item. the may be a value from 0 to itemCount-1 |
80 | const wchar_t* CGUIListBox::getListItem(u32 id) const | 80 | const wchar_t* CGUIListBox::getListItem(u32 id) const |
81 | { | 81 | { |
82 | if (id>=Items.size()) | 82 | if (id>=Items.size()) |
83 | return 0; | 83 | return 0; |
84 | 84 | ||
85 | return Items[id].text.c_str(); | 85 | return Items[id].text.c_str(); |
86 | } | 86 | } |
87 | 87 | ||
88 | 88 | ||
89 | //! Returns the icon of an item | 89 | //! Returns the icon of an item |
90 | s32 CGUIListBox::getIcon(u32 id) const | 90 | s32 CGUIListBox::getIcon(u32 id) const |
91 | { | 91 | { |
92 | if (id>=Items.size()) | 92 | if (id>=Items.size()) |
93 | return -1; | 93 | return -1; |
94 | 94 | ||
95 | return Items[id].icon; | 95 | return Items[id].icon; |
96 | } | 96 | } |
97 | 97 | ||
98 | 98 | ||
99 | //! adds a list item, returns id of item | 99 | //! adds a list item, returns id of item |
100 | u32 CGUIListBox::addItem(const wchar_t* text) | 100 | u32 CGUIListBox::addItem(const wchar_t* text) |
101 | { | 101 | { |
102 | return addItem(text, -1); | 102 | return addItem(text, -1); |
103 | } | 103 | } |
104 | 104 | ||
105 | 105 | ||
106 | //! adds a list item, returns id of item | 106 | //! adds a list item, returns id of item |
107 | void CGUIListBox::removeItem(u32 id) | 107 | void CGUIListBox::removeItem(u32 id) |
108 | { | 108 | { |
109 | if (id >= Items.size()) | 109 | if (id >= Items.size()) |
110 | return; | 110 | return; |
111 | 111 | ||
112 | if ((u32)Selected==id) | 112 | if ((u32)Selected==id) |
113 | { | 113 | { |
114 | Selected = -1; | 114 | Selected = -1; |
115 | } | 115 | } |
116 | else if ((u32)Selected > id) | 116 | else if ((u32)Selected > id) |
117 | { | 117 | { |
118 | Selected -= 1; | 118 | Selected -= 1; |
119 | selectTime = os::Timer::getTime(); | 119 | selectTime = os::Timer::getTime(); |
120 | } | 120 | } |
121 | 121 | ||
122 | Items.erase(id); | 122 | Items.erase(id); |
123 | 123 | ||
124 | recalculateItemHeight(); | 124 | recalculateItemHeight(); |
125 | } | 125 | } |
126 | 126 | ||
127 | 127 | ||
128 | s32 CGUIListBox::getItemAt(s32 xpos, s32 ypos) const | 128 | s32 CGUIListBox::getItemAt(s32 xpos, s32 ypos) const |
129 | { | 129 | { |
130 | if ( xpos < AbsoluteRect.UpperLeftCorner.X || xpos >= AbsoluteRect.LowerRightCorner.X | 130 | if ( xpos < AbsoluteRect.UpperLeftCorner.X || xpos >= AbsoluteRect.LowerRightCorner.X |
131 | || ypos < AbsoluteRect.UpperLeftCorner.Y || ypos >= AbsoluteRect.LowerRightCorner.Y | 131 | || ypos < AbsoluteRect.UpperLeftCorner.Y || ypos >= AbsoluteRect.LowerRightCorner.Y |
132 | ) | 132 | ) |
133 | return -1; | 133 | return -1; |
134 | 134 | ||
135 | if ( ItemHeight == 0 ) | 135 | if ( ItemHeight == 0 ) |
136 | return -1; | 136 | return -1; |
137 | 137 | ||
138 | s32 item = ((ypos - AbsoluteRect.UpperLeftCorner.Y - 1) + ScrollBar->getPos()) / ItemHeight; | 138 | s32 item = ((ypos - AbsoluteRect.UpperLeftCorner.Y - 1) + ScrollBar->getPos()) / ItemHeight; |
139 | if ( item < 0 || item >= (s32)Items.size()) | 139 | if ( item < 0 || item >= (s32)Items.size()) |
140 | return -1; | 140 | return -1; |
141 | 141 | ||
142 | return item; | 142 | return item; |
143 | } | 143 | } |
144 | 144 | ||
145 | //! clears the list | 145 | //! clears the list |
146 | void CGUIListBox::clear() | 146 | void CGUIListBox::clear() |
147 | { | 147 | { |
148 | Items.clear(); | 148 | Items.clear(); |
149 | ItemsIconWidth = 0; | 149 | ItemsIconWidth = 0; |
150 | Selected = -1; | 150 | Selected = -1; |
151 | 151 | ||
152 | if (ScrollBar) | 152 | if (ScrollBar) |
153 | ScrollBar->setPos(0); | 153 | ScrollBar->setPos(0); |
154 | 154 | ||
155 | recalculateItemHeight(); | 155 | recalculateItemHeight(); |
156 | } | 156 | } |
157 | 157 | ||
158 | 158 | ||
159 | void CGUIListBox::recalculateItemHeight() | 159 | void CGUIListBox::recalculateItemHeight() |
160 | { | 160 | { |
161 | IGUISkin* skin = Environment->getSkin(); | 161 | IGUISkin* skin = Environment->getSkin(); |
162 | 162 | ||
163 | if (Font != skin->getFont()) | 163 | if (Font != skin->getFont()) |
164 | { | 164 | { |
165 | if (Font) | 165 | if (Font) |
166 | Font->drop(); | 166 | Font->drop(); |
167 | 167 | ||
168 | Font = skin->getFont(); | 168 | Font = skin->getFont(); |
169 | if ( 0 == ItemHeightOverride ) | 169 | if ( 0 == ItemHeightOverride ) |
170 | ItemHeight = 0; | 170 | ItemHeight = 0; |
171 | 171 | ||
172 | if (Font) | 172 | if (Font) |
173 | { | 173 | { |
174 | if ( 0 == ItemHeightOverride ) | 174 | if ( 0 == ItemHeightOverride ) |
175 | ItemHeight = Font->getDimension(L"A").Height + 4; | 175 | ItemHeight = Font->getDimension(L"A").Height + 4; |
176 | 176 | ||
177 | Font->grab(); | 177 | Font->grab(); |
178 | } | 178 | } |
179 | } | 179 | } |
180 | 180 | ||
181 | TotalItemHeight = ItemHeight * Items.size(); | 181 | TotalItemHeight = ItemHeight * Items.size(); |
182 | ScrollBar->setMax( core::max_(0, TotalItemHeight - AbsoluteRect.getHeight()) ); | 182 | ScrollBar->setMax( core::max_(0, TotalItemHeight - AbsoluteRect.getHeight()) ); |
183 | s32 minItemHeight = ItemHeight > 0 ? ItemHeight : 1; | 183 | s32 minItemHeight = ItemHeight > 0 ? ItemHeight : 1; |
184 | ScrollBar->setSmallStep ( minItemHeight ); | 184 | ScrollBar->setSmallStep ( minItemHeight ); |
185 | ScrollBar->setLargeStep ( 2*minItemHeight ); | 185 | ScrollBar->setLargeStep ( 2*minItemHeight ); |
186 | 186 | ||
187 | if ( TotalItemHeight <= AbsoluteRect.getHeight() ) | 187 | if ( TotalItemHeight <= AbsoluteRect.getHeight() ) |
188 | ScrollBar->setVisible(false); | 188 | ScrollBar->setVisible(false); |
189 | else | 189 | else |
190 | ScrollBar->setVisible(true); | 190 | ScrollBar->setVisible(true); |
191 | } | 191 | } |
192 | 192 | ||
193 | 193 | ||
194 | //! returns id of selected item. returns -1 if no item is selected. | 194 | //! returns id of selected item. returns -1 if no item is selected. |
195 | s32 CGUIListBox::getSelected() const | 195 | s32 CGUIListBox::getSelected() const |
196 | { | 196 | { |
197 | return Selected; | 197 | return Selected; |
198 | } | 198 | } |
199 | 199 | ||
200 | 200 | ||
201 | //! sets the selected item. Set this to -1 if no item should be selected | 201 | //! sets the selected item. Set this to -1 if no item should be selected |
202 | void CGUIListBox::setSelected(s32 id) | 202 | void CGUIListBox::setSelected(s32 id) |
203 | { | 203 | { |
204 | if ((u32)id>=Items.size()) | 204 | if ((u32)id>=Items.size()) |
205 | Selected = -1; | 205 | Selected = -1; |
206 | else | 206 | else |
207 | Selected = id; | 207 | Selected = id; |
208 | 208 | ||
209 | selectTime = os::Timer::getTime(); | 209 | selectTime = os::Timer::getTime(); |
210 | 210 | ||
211 | recalculateScrollPos(); | 211 | recalculateScrollPos(); |
212 | } | 212 | } |
213 | 213 | ||
214 | //! sets the selected item. Set this to -1 if no item should be selected | 214 | //! sets the selected item. Set this to -1 if no item should be selected |
215 | void CGUIListBox::setSelected(const wchar_t *item) | 215 | void CGUIListBox::setSelected(const wchar_t *item) |
216 | { | 216 | { |
217 | s32 index = -1; | 217 | s32 index = -1; |
218 | 218 | ||
219 | if ( item ) | 219 | if ( item ) |
220 | { | 220 | { |
221 | for ( index = 0; index < (s32) Items.size(); ++index ) | 221 | for ( index = 0; index < (s32) Items.size(); ++index ) |
222 | { | 222 | { |
223 | if ( Items[index].text == item ) | 223 | if ( Items[index].text == item ) |
224 | break; | 224 | break; |
225 | } | 225 | } |
226 | } | 226 | } |
227 | setSelected ( index ); | 227 | setSelected ( index ); |
228 | } | 228 | } |
229 | 229 | ||
230 | //! called if an event happened. | 230 | //! called if an event happened. |
231 | bool CGUIListBox::OnEvent(const SEvent& event) | 231 | bool CGUIListBox::OnEvent(const SEvent& event) |
232 | { | 232 | { |
233 | if (isEnabled()) | 233 | if (isEnabled()) |
234 | { | 234 | { |
235 | switch(event.EventType) | 235 | switch(event.EventType) |
236 | { | 236 | { |
237 | case EET_KEY_INPUT_EVENT: | 237 | case EET_KEY_INPUT_EVENT: |
238 | if (event.KeyInput.PressedDown && | 238 | if (event.KeyInput.PressedDown && |
239 | (event.KeyInput.Key == KEY_DOWN || | 239 | (event.KeyInput.Key == KEY_DOWN || |
240 | event.KeyInput.Key == KEY_UP || | 240 | event.KeyInput.Key == KEY_UP || |
241 | event.KeyInput.Key == KEY_HOME || | 241 | event.KeyInput.Key == KEY_HOME || |
242 | event.KeyInput.Key == KEY_END || | 242 | event.KeyInput.Key == KEY_END || |
243 | event.KeyInput.Key == KEY_NEXT || | 243 | event.KeyInput.Key == KEY_NEXT || |
244 | event.KeyInput.Key == KEY_PRIOR ) ) | 244 | event.KeyInput.Key == KEY_PRIOR ) ) |
245 | { | 245 | { |
246 | s32 oldSelected = Selected; | 246 | s32 oldSelected = Selected; |
247 | switch (event.KeyInput.Key) | 247 | switch (event.KeyInput.Key) |
248 | { | 248 | { |
249 | case KEY_DOWN: | 249 | case KEY_DOWN: |
250 | Selected += 1; | 250 | Selected += 1; |
251 | break; | 251 | break; |
252 | case KEY_UP: | 252 | case KEY_UP: |
253 | Selected -= 1; | 253 | Selected -= 1; |
254 | break; | 254 | break; |
255 | case KEY_HOME: | 255 | case KEY_HOME: |
256 | Selected = 0; | 256 | Selected = 0; |
257 | break; | 257 | break; |
258 | case KEY_END: | 258 | case KEY_END: |
259 | Selected = (s32)Items.size()-1; | 259 | Selected = (s32)Items.size()-1; |
260 | break; | 260 | break; |
261 | case KEY_NEXT: | 261 | case KEY_NEXT: |
262 | Selected += AbsoluteRect.getHeight() / ItemHeight; | 262 | Selected += AbsoluteRect.getHeight() / ItemHeight; |
263 | break; | 263 | break; |
264 | case KEY_PRIOR: | 264 | case KEY_PRIOR: |
265 | Selected -= AbsoluteRect.getHeight() / ItemHeight; | 265 | Selected -= AbsoluteRect.getHeight() / ItemHeight; |
266 | break; | 266 | break; |
267 | default: | 267 | default: |
268 | break; | 268 | break; |
269 | } | 269 | } |
270 | if (Selected >= (s32)Items.size()) | 270 | if (Selected >= (s32)Items.size()) |
271 | Selected = Items.size() - 1; | 271 | Selected = Items.size() - 1; |
272 | else | 272 | else |
273 | if (Selected<0) | 273 | if (Selected<0) |
274 | Selected = 0; | 274 | Selected = 0; |
275 | 275 | ||
276 | recalculateScrollPos(); | 276 | recalculateScrollPos(); |
277 | 277 | ||
278 | // post the news | 278 | // post the news |
279 | 279 | ||
280 | if (oldSelected != Selected && Parent && !Selecting && !MoveOverSelect) | 280 | if (oldSelected != Selected && Parent && !Selecting && !MoveOverSelect) |
281 | { | 281 | { |
282 | SEvent e; | 282 | SEvent e; |
283 | e.EventType = EET_GUI_EVENT; | 283 | e.EventType = EET_GUI_EVENT; |
284 | e.GUIEvent.Caller = this; | 284 | e.GUIEvent.Caller = this; |
285 | e.GUIEvent.Element = 0; | 285 | e.GUIEvent.Element = 0; |
286 | e.GUIEvent.EventType = EGET_LISTBOX_CHANGED; | 286 | e.GUIEvent.EventType = EGET_LISTBOX_CHANGED; |
287 | Parent->OnEvent(e); | 287 | Parent->OnEvent(e); |
288 | } | 288 | } |
289 | 289 | ||
290 | return true; | 290 | return true; |
291 | } | 291 | } |
292 | else | 292 | else |
293 | if (!event.KeyInput.PressedDown && ( event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE ) ) | 293 | if (!event.KeyInput.PressedDown && ( event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE ) ) |
294 | { | 294 | { |
295 | if (Parent) | 295 | if (Parent) |
296 | { | 296 | { |
297 | SEvent e; | 297 | SEvent e; |
298 | e.EventType = EET_GUI_EVENT; | 298 | e.EventType = EET_GUI_EVENT; |
299 | e.GUIEvent.Caller = this; | 299 | e.GUIEvent.Caller = this; |
300 | e.GUIEvent.Element = 0; | 300 | e.GUIEvent.Element = 0; |
301 | e.GUIEvent.EventType = EGET_LISTBOX_SELECTED_AGAIN; | 301 | e.GUIEvent.EventType = EGET_LISTBOX_SELECTED_AGAIN; |
302 | Parent->OnEvent(e); | 302 | Parent->OnEvent(e); |
303 | } | 303 | } |
304 | return true; | 304 | return true; |
305 | } | 305 | } |
306 | else if (event.KeyInput.PressedDown && event.KeyInput.Char) | 306 | else if (event.KeyInput.PressedDown && event.KeyInput.Char) |
307 | { | 307 | { |
308 | // change selection based on text as it is typed. | 308 | // change selection based on text as it is typed. |
309 | u32 now = os::Timer::getTime(); | 309 | u32 now = os::Timer::getTime(); |
310 | 310 | ||
311 | if (now - LastKeyTime < 500) | 311 | if (now - LastKeyTime < 500) |
312 | { | 312 | { |
313 | // add to key buffer if it isn't a key repeat | 313 | // add to key buffer if it isn't a key repeat |
314 | if (!(KeyBuffer.size() == 1 && KeyBuffer[0] == event.KeyInput.Char)) | 314 | if (!(KeyBuffer.size() == 1 && KeyBuffer[0] == event.KeyInput.Char)) |
315 | { | 315 | { |
316 | KeyBuffer += L" "; | 316 | KeyBuffer += L" "; |
317 | KeyBuffer[KeyBuffer.size()-1] = event.KeyInput.Char; | 317 | KeyBuffer[KeyBuffer.size()-1] = event.KeyInput.Char; |
318 | } | 318 | } |
319 | } | 319 | } |
320 | else | 320 | else |
321 | { | 321 | { |
322 | KeyBuffer = L" "; | 322 | KeyBuffer = L" "; |
323 | KeyBuffer[0] = event.KeyInput.Char; | 323 | KeyBuffer[0] = event.KeyInput.Char; |
324 | } | 324 | } |
325 | LastKeyTime = now; | 325 | LastKeyTime = now; |
326 | 326 | ||
327 | // find the selected item, starting at the current selection | 327 | // find the selected item, starting at the current selection |
328 | s32 start = Selected; | 328 | s32 start = Selected; |
329 | // dont change selection if the key buffer matches the current item | 329 | // dont change selection if the key buffer matches the current item |
330 | if (Selected > -1 && KeyBuffer.size() > 1) | 330 | if (Selected > -1 && KeyBuffer.size() > 1) |
331 | { | 331 | { |
332 | if (Items[Selected].text.size() >= KeyBuffer.size() && | 332 | if (Items[Selected].text.size() >= KeyBuffer.size() && |
333 | KeyBuffer.equals_ignore_case(Items[Selected].text.subString(0,KeyBuffer.size()))) | 333 | KeyBuffer.equals_ignore_case(Items[Selected].text.subString(0,KeyBuffer.size()))) |
334 | return true; | 334 | return true; |
335 | } | 335 | } |
336 | 336 | ||
337 | s32 current; | 337 | s32 current; |
338 | for (current = start+1; current < (s32)Items.size(); ++current) | 338 | for (current = start+1; current < (s32)Items.size(); ++current) |
339 | { | 339 | { |
340 | if (Items[current].text.size() >= KeyBuffer.size()) | 340 | if (Items[current].text.size() >= KeyBuffer.size()) |
341 | { | 341 | { |
342 | if (KeyBuffer.equals_ignore_case(Items[current].text.subString(0,KeyBuffer.size()))) | 342 | if (KeyBuffer.equals_ignore_case(Items[current].text.subString(0,KeyBuffer.size()))) |
343 | { | 343 | { |
344 | if (Parent && Selected != current && !Selecting && !MoveOverSelect) | 344 | if (Parent && Selected != current && !Selecting && !MoveOverSelect) |
345 | { | 345 | { |
346 | SEvent e; | 346 | SEvent e; |
347 | e.EventType = EET_GUI_EVENT; | 347 | e.EventType = EET_GUI_EVENT; |
348 | e.GUIEvent.Caller = this; | 348 | e.GUIEvent.Caller = this; |
349 | e.GUIEvent.Element = 0; | 349 | e.GUIEvent.Element = 0; |
350 | e.GUIEvent.EventType = EGET_LISTBOX_CHANGED; | 350 | e.GUIEvent.EventType = EGET_LISTBOX_CHANGED; |
351 | Parent->OnEvent(e); | 351 | Parent->OnEvent(e); |
352 | } | 352 | } |
353 | setSelected(current); | 353 | setSelected(current); |
354 | return true; | 354 | return true; |
355 | } | 355 | } |
356 | } | 356 | } |
357 | } | 357 | } |
358 | for (current = 0; current <= start; ++current) | 358 | for (current = 0; current <= start; ++current) |
359 | { | 359 | { |
360 | if (Items[current].text.size() >= KeyBuffer.size()) | 360 | if (Items[current].text.size() >= KeyBuffer.size()) |
361 | { | 361 | { |
362 | if (KeyBuffer.equals_ignore_case(Items[current].text.subString(0,KeyBuffer.size()))) | 362 | if (KeyBuffer.equals_ignore_case(Items[current].text.subString(0,KeyBuffer.size()))) |
363 | { | 363 | { |
364 | if (Parent && Selected != current && !Selecting && !MoveOverSelect) | 364 | if (Parent && Selected != current && !Selecting && !MoveOverSelect) |
365 | { | 365 | { |
366 | Selected = current; | 366 | Selected = current; |
367 | SEvent e; | 367 | SEvent e; |
368 | e.EventType = EET_GUI_EVENT; | 368 | e.EventType = EET_GUI_EVENT; |
369 | e.GUIEvent.Caller = this; | 369 | e.GUIEvent.Caller = this; |
370 | e.GUIEvent.Element = 0; | 370 | e.GUIEvent.Element = 0; |
371 | e.GUIEvent.EventType = EGET_LISTBOX_CHANGED; | 371 | e.GUIEvent.EventType = EGET_LISTBOX_CHANGED; |
372 | Parent->OnEvent(e); | 372 | Parent->OnEvent(e); |
373 | } | 373 | } |
374 | setSelected(current); | 374 | setSelected(current); |
375 | return true; | 375 | return true; |
376 | } | 376 | } |
377 | } | 377 | } |
378 | } | 378 | } |
379 | 379 | ||
380 | return true; | 380 | return true; |
381 | } | 381 | } |
382 | break; | 382 | break; |
383 | 383 | ||
384 | case EET_GUI_EVENT: | 384 | case EET_GUI_EVENT: |
385 | switch(event.GUIEvent.EventType) | 385 | switch(event.GUIEvent.EventType) |
386 | { | 386 | { |
387 | case gui::EGET_SCROLL_BAR_CHANGED: | 387 | case gui::EGET_SCROLL_BAR_CHANGED: |
388 | if (event.GUIEvent.Caller == ScrollBar) | 388 | if (event.GUIEvent.Caller == ScrollBar) |
389 | return true; | 389 | return true; |
390 | break; | 390 | break; |
391 | case gui::EGET_ELEMENT_FOCUS_LOST: | 391 | case gui::EGET_ELEMENT_FOCUS_LOST: |
392 | { | 392 | { |
393 | if (event.GUIEvent.Caller == this) | 393 | if (event.GUIEvent.Caller == this) |
394 | Selecting = false; | 394 | Selecting = false; |
395 | } | 395 | } |
396 | default: | 396 | default: |
397 | break; | 397 | break; |
398 | } | 398 | } |
399 | break; | 399 | break; |
400 | 400 | ||
401 | case EET_MOUSE_INPUT_EVENT: | 401 | case EET_MOUSE_INPUT_EVENT: |
402 | { | 402 | { |
403 | core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); | 403 | core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); |
404 | 404 | ||
405 | switch(event.MouseInput.Event) | 405 | switch(event.MouseInput.Event) |
406 | { | 406 | { |
407 | case EMIE_MOUSE_WHEEL: | 407 | case EMIE_MOUSE_WHEEL: |
408 | ScrollBar->setPos(ScrollBar->getPos() + (event.MouseInput.Wheel < 0 ? -1 : 1)*-ItemHeight/2); | 408 | ScrollBar->setPos(ScrollBar->getPos() + (event.MouseInput.Wheel < 0 ? -1 : 1)*-ItemHeight/2); |
409 | return true; | 409 | return true; |
410 | 410 | ||
411 | case EMIE_LMOUSE_PRESSED_DOWN: | 411 | case EMIE_LMOUSE_PRESSED_DOWN: |
412 | { | 412 | { |
413 | Selecting = true; | 413 | Selecting = true; |
414 | return true; | 414 | return true; |
415 | } | 415 | } |
416 | 416 | ||
417 | case EMIE_LMOUSE_LEFT_UP: | 417 | case EMIE_LMOUSE_LEFT_UP: |
418 | { | 418 | { |
419 | Selecting = false; | 419 | Selecting = false; |
420 | 420 | ||
421 | if (isPointInside(p)) | 421 | if (isPointInside(p)) |
422 | selectNew(event.MouseInput.Y); | 422 | selectNew(event.MouseInput.Y); |
423 | 423 | ||
424 | return true; | 424 | return true; |
425 | } | 425 | } |
426 | 426 | ||
427 | case EMIE_MOUSE_MOVED: | 427 | case EMIE_MOUSE_MOVED: |
428 | if (Selecting || MoveOverSelect) | 428 | if (Selecting || MoveOverSelect) |
429 | { | 429 | { |
430 | if (isPointInside(p)) | 430 | if (isPointInside(p)) |
431 | { | 431 | { |
432 | selectNew(event.MouseInput.Y, true); | 432 | selectNew(event.MouseInput.Y, true); |
433 | return true; | 433 | return true; |
434 | } | 434 | } |
435 | } | 435 | } |
436 | default: | 436 | default: |
437 | break; | 437 | break; |
438 | } | 438 | } |
439 | } | 439 | } |
440 | break; | 440 | break; |
441 | case EET_LOG_TEXT_EVENT: | 441 | case EET_LOG_TEXT_EVENT: |
442 | case EET_USER_EVENT: | 442 | case EET_USER_EVENT: |
443 | case EET_JOYSTICK_INPUT_EVENT: | 443 | case EET_JOYSTICK_INPUT_EVENT: |
444 | case EGUIET_FORCE_32_BIT: | 444 | case EGUIET_FORCE_32_BIT: |
445 | break; | 445 | break; |
446 | } | 446 | } |
447 | } | 447 | } |
448 | 448 | ||
449 | return IGUIElement::OnEvent(event); | 449 | return IGUIElement::OnEvent(event); |
450 | } | 450 | } |
451 | 451 | ||
452 | 452 | ||
453 | void CGUIListBox::selectNew(s32 ypos, bool onlyHover) | 453 | void CGUIListBox::selectNew(s32 ypos, bool onlyHover) |
454 | { | 454 | { |
455 | u32 now = os::Timer::getTime(); | 455 | u32 now = os::Timer::getTime(); |
456 | s32 oldSelected = Selected; | 456 | s32 oldSelected = Selected; |
457 | 457 | ||
458 | Selected = getItemAt(AbsoluteRect.UpperLeftCorner.X, ypos); | 458 | Selected = getItemAt(AbsoluteRect.UpperLeftCorner.X, ypos); |
459 | if (Selected<0 && !Items.empty()) | 459 | if (Selected<0 && !Items.empty()) |
460 | Selected = 0; | 460 | Selected = 0; |
461 | 461 | ||
462 | recalculateScrollPos(); | 462 | recalculateScrollPos(); |
463 | 463 | ||
464 | gui::EGUI_EVENT_TYPE eventType = (Selected == oldSelected && now < selectTime + 500) ? EGET_LISTBOX_SELECTED_AGAIN : EGET_LISTBOX_CHANGED; | 464 | gui::EGUI_EVENT_TYPE eventType = (Selected == oldSelected && now < selectTime + 500) ? EGET_LISTBOX_SELECTED_AGAIN : EGET_LISTBOX_CHANGED; |
465 | selectTime = now; | 465 | selectTime = now; |
466 | // post the news | 466 | // post the news |
467 | if (Parent && !onlyHover) | 467 | if (Parent && !onlyHover) |
468 | { | 468 | { |
469 | SEvent event; | 469 | SEvent event; |
470 | event.EventType = EET_GUI_EVENT; | 470 | event.EventType = EET_GUI_EVENT; |
471 | event.GUIEvent.Caller = this; | 471 | event.GUIEvent.Caller = this; |
472 | event.GUIEvent.Element = 0; | 472 | event.GUIEvent.Element = 0; |
473 | event.GUIEvent.EventType = eventType; | 473 | event.GUIEvent.EventType = eventType; |
474 | Parent->OnEvent(event); | 474 | Parent->OnEvent(event); |
475 | } | 475 | } |
476 | } | 476 | } |
477 | 477 | ||
478 | 478 | ||
479 | //! Update the position and size of the listbox, and update the scrollbar | 479 | //! Update the position and size of the listbox, and update the scrollbar |
480 | void CGUIListBox::updateAbsolutePosition() | 480 | void CGUIListBox::updateAbsolutePosition() |
481 | { | 481 | { |
482 | IGUIElement::updateAbsolutePosition(); | 482 | IGUIElement::updateAbsolutePosition(); |
483 | 483 | ||
484 | recalculateItemHeight(); | 484 | recalculateItemHeight(); |
485 | } | 485 | } |
486 | 486 | ||
487 | 487 | ||
488 | //! draws the element and its children | 488 | //! draws the element and its children |
489 | void CGUIListBox::draw() | 489 | void CGUIListBox::draw() |
490 | { | 490 | { |
491 | if (!IsVisible) | 491 | if (!IsVisible) |
492 | return; | 492 | return; |
493 | 493 | ||
494 | recalculateItemHeight(); // if the font changed | 494 | recalculateItemHeight(); // if the font changed |
495 | 495 | ||
496 | IGUISkin* skin = Environment->getSkin(); | 496 | IGUISkin* skin = Environment->getSkin(); |
497 | 497 | ||
498 | core::rect<s32>* clipRect = 0; | 498 | core::rect<s32>* clipRect = 0; |
499 | 499 | ||
500 | // draw background | 500 | // draw background |
501 | core::rect<s32> frameRect(AbsoluteRect); | 501 | core::rect<s32> frameRect(AbsoluteRect); |
502 | 502 | ||
503 | // draw items | 503 | // draw items |
504 | 504 | ||
505 | core::rect<s32> clientClip(AbsoluteRect); | 505 | core::rect<s32> clientClip(AbsoluteRect); |
506 | clientClip.UpperLeftCorner.Y += 1; | 506 | clientClip.UpperLeftCorner.Y += 1; |
507 | clientClip.UpperLeftCorner.X += 1; | 507 | clientClip.UpperLeftCorner.X += 1; |
508 | if (ScrollBar->isVisible()) | 508 | if (ScrollBar->isVisible()) |
509 | clientClip.LowerRightCorner.X = AbsoluteRect.LowerRightCorner.X - skin->getSize(EGDS_SCROLLBAR_SIZE); | 509 | clientClip.LowerRightCorner.X = AbsoluteRect.LowerRightCorner.X - skin->getSize(EGDS_SCROLLBAR_SIZE); |
510 | clientClip.LowerRightCorner.Y -= 1; | 510 | clientClip.LowerRightCorner.Y -= 1; |
511 | clientClip.clipAgainst(AbsoluteClippingRect); | 511 | clientClip.clipAgainst(AbsoluteClippingRect); |
512 | 512 | ||
513 | skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT), true, | 513 | skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT), true, |
514 | DrawBack, frameRect, &clientClip); | 514 | DrawBack, frameRect, &clientClip); |
515 | 515 | ||
516 | if (clipRect) | 516 | if (clipRect) |
517 | clientClip.clipAgainst(*clipRect); | 517 | clientClip.clipAgainst(*clipRect); |
518 | 518 | ||
519 | frameRect = AbsoluteRect; | 519 | frameRect = AbsoluteRect; |
520 | frameRect.UpperLeftCorner.X += 1; | 520 | frameRect.UpperLeftCorner.X += 1; |
521 | if (ScrollBar->isVisible()) | 521 | if (ScrollBar->isVisible()) |
522 | frameRect.LowerRightCorner.X = AbsoluteRect.LowerRightCorner.X - skin->getSize(EGDS_SCROLLBAR_SIZE); | 522 | frameRect.LowerRightCorner.X = AbsoluteRect.LowerRightCorner.X - skin->getSize(EGDS_SCROLLBAR_SIZE); |
523 | 523 | ||
524 | frameRect.LowerRightCorner.Y = AbsoluteRect.UpperLeftCorner.Y + ItemHeight; | 524 | frameRect.LowerRightCorner.Y = AbsoluteRect.UpperLeftCorner.Y + ItemHeight; |
525 | 525 | ||
526 | frameRect.UpperLeftCorner.Y -= ScrollBar->getPos(); | 526 | frameRect.UpperLeftCorner.Y -= ScrollBar->getPos(); |
527 | frameRect.LowerRightCorner.Y -= ScrollBar->getPos(); | 527 | frameRect.LowerRightCorner.Y -= ScrollBar->getPos(); |
528 | 528 | ||
529 | bool hl = (HighlightWhenNotFocused || Environment->hasFocus(this) || Environment->hasFocus(ScrollBar)); | 529 | bool hl = (HighlightWhenNotFocused || Environment->hasFocus(this) || Environment->hasFocus(ScrollBar)); |
530 | 530 | ||
531 | for (s32 i=0; i<(s32)Items.size(); ++i) | 531 | for (s32 i=0; i<(s32)Items.size(); ++i) |
532 | { | 532 | { |
533 | if (frameRect.LowerRightCorner.Y >= AbsoluteRect.UpperLeftCorner.Y && | 533 | if (frameRect.LowerRightCorner.Y >= AbsoluteRect.UpperLeftCorner.Y && |
534 | frameRect.UpperLeftCorner.Y <= AbsoluteRect.LowerRightCorner.Y) | 534 | frameRect.UpperLeftCorner.Y <= AbsoluteRect.LowerRightCorner.Y) |
535 | { | 535 | { |
536 | if (i == Selected && hl) | 536 | if (i == Selected && hl) |
537 | skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), frameRect, &clientClip); | 537 | skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), frameRect, &clientClip); |
538 | 538 | ||
539 | core::rect<s32> textRect = frameRect; | 539 | core::rect<s32> textRect = frameRect; |
540 | textRect.UpperLeftCorner.X += 3; | 540 | textRect.UpperLeftCorner.X += 3; |
541 | 541 | ||
542 | if (Font) | 542 | if (Font) |
543 | { | 543 | { |
544 | if (IconBank && (Items[i].icon > -1)) | 544 | if (IconBank && (Items[i].icon > -1)) |
545 | { | 545 | { |
546 | core::position2di iconPos = textRect.UpperLeftCorner; | 546 | core::position2di iconPos = textRect.UpperLeftCorner; |
547 | iconPos.Y += textRect.getHeight() / 2; | 547 | iconPos.Y += textRect.getHeight() / 2; |
548 | iconPos.X += ItemsIconWidth/2; | 548 | iconPos.X += ItemsIconWidth/2; |
549 | 549 | ||
550 | if ( i==Selected && hl ) | 550 | if ( i==Selected && hl ) |
551 | { | 551 | { |
552 | IconBank->draw2DSprite( (u32)Items[i].icon, iconPos, &clientClip, | 552 | IconBank->draw2DSprite( (u32)Items[i].icon, iconPos, &clientClip, |
553 | hasItemOverrideColor(i, EGUI_LBC_ICON_HIGHLIGHT) ? | 553 | hasItemOverrideColor(i, EGUI_LBC_ICON_HIGHLIGHT) ? |
554 | getItemOverrideColor(i, EGUI_LBC_ICON_HIGHLIGHT) : getItemDefaultColor(EGUI_LBC_ICON_HIGHLIGHT), | 554 | getItemOverrideColor(i, EGUI_LBC_ICON_HIGHLIGHT) : getItemDefaultColor(EGUI_LBC_ICON_HIGHLIGHT), |
555 | selectTime, os::Timer::getTime(), false, true); | 555 | selectTime, os::Timer::getTime(), false, true); |
556 | } | 556 | } |
557 | else | 557 | else |
558 | { | 558 | { |
559 | IconBank->draw2DSprite( (u32)Items[i].icon, iconPos, &clientClip, | 559 | IconBank->draw2DSprite( (u32)Items[i].icon, iconPos, &clientClip, |
560 | hasItemOverrideColor(i, EGUI_LBC_ICON) ? getItemOverrideColor(i, EGUI_LBC_ICON) : getItemDefaultColor(EGUI_LBC_ICON), | 560 | hasItemOverrideColor(i, EGUI_LBC_ICON) ? getItemOverrideColor(i, EGUI_LBC_ICON) : getItemDefaultColor(EGUI_LBC_ICON), |
561 | 0 , (i==Selected) ? os::Timer::getTime() : 0, false, true); | 561 | 0 , (i==Selected) ? os::Timer::getTime() : 0, false, true); |
562 | } | 562 | } |
563 | } | 563 | } |
564 | 564 | ||
565 | textRect.UpperLeftCorner.X += ItemsIconWidth+3; | 565 | textRect.UpperLeftCorner.X += ItemsIconWidth+3; |
566 | 566 | ||
567 | if ( i==Selected && hl ) | 567 | if ( i==Selected && hl ) |
568 | { | 568 | { |
569 | Font->draw(Items[i].text.c_str(), textRect, | 569 | Font->draw(Items[i].text.c_str(), textRect, |
570 | hasItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) ? | 570 | hasItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) ? |
571 | getItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) : getItemDefaultColor(EGUI_LBC_TEXT_HIGHLIGHT), | 571 | getItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) : getItemDefaultColor(EGUI_LBC_TEXT_HIGHLIGHT), |
572 | false, true, &clientClip); | 572 | false, true, &clientClip); |
573 | } | 573 | } |
574 | else | 574 | else |
575 | { | 575 | { |
576 | Font->draw(Items[i].text.c_str(), textRect, | 576 | Font->draw(Items[i].text.c_str(), textRect, |
577 | hasItemOverrideColor(i, EGUI_LBC_TEXT) ? getItemOverrideColor(i, EGUI_LBC_TEXT) : getItemDefaultColor(EGUI_LBC_TEXT), | 577 | hasItemOverrideColor(i, EGUI_LBC_TEXT) ? getItemOverrideColor(i, EGUI_LBC_TEXT) : getItemDefaultColor(EGUI_LBC_TEXT), |
578 | false, true, &clientClip); | 578 | false, true, &clientClip); |
579 | } | 579 | } |
580 | 580 | ||
581 | textRect.UpperLeftCorner.X -= ItemsIconWidth+3; | 581 | textRect.UpperLeftCorner.X -= ItemsIconWidth+3; |
582 | } | 582 | } |
583 | } | 583 | } |
584 | 584 | ||
585 | frameRect.UpperLeftCorner.Y += ItemHeight; | 585 | frameRect.UpperLeftCorner.Y += ItemHeight; |
586 | frameRect.LowerRightCorner.Y += ItemHeight; | 586 | frameRect.LowerRightCorner.Y += ItemHeight; |
587 | } | 587 | } |
588 | 588 | ||
589 | IGUIElement::draw(); | 589 | IGUIElement::draw(); |
590 | } | 590 | } |
591 | 591 | ||
592 | 592 | ||
593 | //! adds an list item with an icon | 593 | //! adds an list item with an icon |
594 | u32 CGUIListBox::addItem(const wchar_t* text, s32 icon) | 594 | u32 CGUIListBox::addItem(const wchar_t* text, s32 icon) |
595 | { | 595 | { |
596 | ListItem i; | 596 | ListItem i; |
597 | i.text = text; | 597 | i.text = text; |
598 | i.icon = icon; | 598 | i.icon = icon; |
599 | 599 | ||
600 | Items.push_back(i); | 600 | Items.push_back(i); |
601 | recalculateItemHeight(); | 601 | recalculateItemHeight(); |
602 | recalculateItemWidth(icon); | 602 | recalculateItemWidth(icon); |
603 | 603 | ||
604 | return Items.size() - 1; | 604 | return Items.size() - 1; |
605 | } | 605 | } |
606 | 606 | ||
607 | 607 | ||
608 | void CGUIListBox::setSpriteBank(IGUISpriteBank* bank) | 608 | void CGUIListBox::setSpriteBank(IGUISpriteBank* bank) |
609 | { | 609 | { |
610 | if ( bank == IconBank ) | 610 | if ( bank == IconBank ) |
611 | return; | 611 | return; |
612 | if (IconBank) | 612 | if (IconBank) |
613 | IconBank->drop(); | 613 | IconBank->drop(); |
614 | 614 | ||
615 | IconBank = bank; | 615 | IconBank = bank; |
616 | if (IconBank) | 616 | if (IconBank) |
617 | IconBank->grab(); | 617 | IconBank->grab(); |
618 | } | 618 | } |
619 | 619 | ||
620 | 620 | ||
621 | void CGUIListBox::recalculateScrollPos() | 621 | void CGUIListBox::recalculateScrollPos() |
622 | { | 622 | { |
623 | if (!AutoScroll) | 623 | if (!AutoScroll) |
624 | return; | 624 | return; |
625 | 625 | ||
626 | const s32 selPos = (Selected == -1 ? TotalItemHeight : Selected * ItemHeight) - ScrollBar->getPos(); | 626 | const s32 selPos = (Selected == -1 ? TotalItemHeight : Selected * ItemHeight) - ScrollBar->getPos(); |
627 | 627 | ||
628 | if (selPos < 0) | 628 | if (selPos < 0) |
629 | { | 629 | { |
630 | ScrollBar->setPos(ScrollBar->getPos() + selPos); | 630 | ScrollBar->setPos(ScrollBar->getPos() + selPos); |
631 | } | 631 | } |
632 | else | 632 | else |
633 | if (selPos > AbsoluteRect.getHeight() - ItemHeight) | 633 | if (selPos > AbsoluteRect.getHeight() - ItemHeight) |
634 | { | 634 | { |
635 | ScrollBar->setPos(ScrollBar->getPos() + selPos - AbsoluteRect.getHeight() + ItemHeight); | 635 | ScrollBar->setPos(ScrollBar->getPos() + selPos - AbsoluteRect.getHeight() + ItemHeight); |
636 | } | 636 | } |
637 | } | 637 | } |
638 | 638 | ||
639 | 639 | ||
640 | void CGUIListBox::setAutoScrollEnabled(bool scroll) | 640 | void CGUIListBox::setAutoScrollEnabled(bool scroll) |
641 | { | 641 | { |
642 | AutoScroll = scroll; | 642 | AutoScroll = scroll; |
643 | } | 643 | } |
644 | 644 | ||
645 | 645 | ||
646 | bool CGUIListBox::isAutoScrollEnabled() const | 646 | bool CGUIListBox::isAutoScrollEnabled() const |
647 | { | 647 | { |
648 | _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; | 648 | _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; |
649 | return AutoScroll; | 649 | return AutoScroll; |
650 | } | 650 | } |
651 | 651 | ||
652 | 652 | ||
653 | bool CGUIListBox::getSerializationLabels(EGUI_LISTBOX_COLOR colorType, core::stringc & useColorLabel, core::stringc & colorLabel) const | 653 | bool CGUIListBox::getSerializationLabels(EGUI_LISTBOX_COLOR colorType, core::stringc & useColorLabel, core::stringc & colorLabel) const |
654 | { | 654 | { |
655 | switch ( colorType ) | 655 | switch ( colorType ) |
656 | { | 656 | { |
657 | case EGUI_LBC_TEXT: | 657 | case EGUI_LBC_TEXT: |
658 | useColorLabel = "UseColText"; | 658 | useColorLabel = "UseColText"; |
659 | colorLabel = "ColText"; | 659 | colorLabel = "ColText"; |
660 | break; | 660 | break; |
661 | case EGUI_LBC_TEXT_HIGHLIGHT: | 661 | case EGUI_LBC_TEXT_HIGHLIGHT: |
662 | useColorLabel = "UseColTextHl"; | 662 | useColorLabel = "UseColTextHl"; |
663 | colorLabel = "ColTextHl"; | 663 | colorLabel = "ColTextHl"; |
664 | break; | 664 | break; |
665 | case EGUI_LBC_ICON: | 665 | case EGUI_LBC_ICON: |
666 | useColorLabel = "UseColIcon"; | 666 | useColorLabel = "UseColIcon"; |
667 | colorLabel = "ColIcon"; | 667 | colorLabel = "ColIcon"; |
668 | break; | 668 | break; |
669 | case EGUI_LBC_ICON_HIGHLIGHT: | 669 | case EGUI_LBC_ICON_HIGHLIGHT: |
670 | useColorLabel = "UseColIconHl"; | 670 | useColorLabel = "UseColIconHl"; |
671 | colorLabel = "ColIconHl"; | 671 | colorLabel = "ColIconHl"; |
672 | break; | 672 | break; |
673 | default: | 673 | default: |
674 | return false; | 674 | return false; |
675 | } | 675 | } |
676 | return true; | 676 | return true; |
677 | } | 677 | } |
678 | 678 | ||
679 | 679 | ||
680 | //! Writes attributes of the element. | 680 | //! Writes attributes of the element. |
681 | void CGUIListBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const | 681 | void CGUIListBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const |
682 | { | 682 | { |
683 | IGUIListBox::serializeAttributes(out,options); | 683 | IGUIListBox::serializeAttributes(out,options); |
684 | 684 | ||
685 | // todo: out->addString ("IconBank", IconBank->getName?); | 685 | // todo: out->addString ("IconBank", IconBank->getName?); |
686 | out->addBool ("DrawBack", DrawBack); | 686 | out->addBool ("DrawBack", DrawBack); |
687 | out->addBool ("MoveOverSelect", MoveOverSelect); | 687 | out->addBool ("MoveOverSelect", MoveOverSelect); |
688 | out->addBool ("AutoScroll", AutoScroll); | 688 | out->addBool ("AutoScroll", AutoScroll); |
689 | 689 | ||
690 | out->addInt("ItemCount", Items.size()); | 690 | out->addInt("ItemCount", Items.size()); |
691 | for (u32 i=0;i<Items.size(); ++i) | 691 | for (u32 i=0;i<Items.size(); ++i) |
692 | { | 692 | { |
693 | core::stringc label("text"); | 693 | core::stringc label("text"); |
694 | label += i; | 694 | label += i; |
695 | out->addString(label.c_str(), Items[i].text.c_str() ); | 695 | out->addString(label.c_str(), Items[i].text.c_str() ); |
696 | 696 | ||
697 | for ( s32 c=0; c < (s32)EGUI_LBC_COUNT; ++c ) | 697 | for ( s32 c=0; c < (s32)EGUI_LBC_COUNT; ++c ) |
698 | { | 698 | { |
699 | core::stringc useColorLabel, colorLabel; | 699 | core::stringc useColorLabel, colorLabel; |
700 | if ( !getSerializationLabels((EGUI_LISTBOX_COLOR)c, useColorLabel, colorLabel) ) | 700 | if ( !getSerializationLabels((EGUI_LISTBOX_COLOR)c, useColorLabel, colorLabel) ) |
701 | return; | 701 | return; |
702 | label = useColorLabel; label += i; | 702 | label = useColorLabel; label += i; |
703 | if ( Items[i].OverrideColors[c].Use ) | 703 | if ( Items[i].OverrideColors[c].Use ) |
704 | { | 704 | { |
705 | out->addBool(label.c_str(), true ); | 705 | out->addBool(label.c_str(), true ); |
706 | label = colorLabel; label += i; | 706 | label = colorLabel; label += i; |
707 | out->addColor(label.c_str(), Items[i].OverrideColors[c].Color); | 707 | out->addColor(label.c_str(), Items[i].OverrideColors[c].Color); |
708 | } | 708 | } |
709 | else | 709 | else |
710 | { | 710 | { |
711 | out->addBool(label.c_str(), false ); | 711 | out->addBool(label.c_str(), false ); |
712 | } | 712 | } |
713 | } | 713 | } |
714 | } | 714 | } |
715 | } | 715 | } |
716 | 716 | ||
717 | 717 | ||
718 | //! Reads attributes of the element | 718 | //! Reads attributes of the element |
719 | void CGUIListBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) | 719 | void CGUIListBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) |
720 | { | 720 | { |
721 | clear(); | 721 | clear(); |
722 | 722 | ||
723 | DrawBack = in->getAttributeAsBool("DrawBack"); | 723 | DrawBack = in->getAttributeAsBool("DrawBack"); |
724 | MoveOverSelect = in->getAttributeAsBool("MoveOverSelect"); | 724 | MoveOverSelect = in->getAttributeAsBool("MoveOverSelect"); |
725 | AutoScroll = in->getAttributeAsBool("AutoScroll"); | 725 | AutoScroll = in->getAttributeAsBool("AutoScroll"); |
726 | 726 | ||
727 | IGUIListBox::deserializeAttributes(in,options); | 727 | IGUIListBox::deserializeAttributes(in,options); |
728 | 728 | ||
729 | const s32 count = in->getAttributeAsInt("ItemCount"); | 729 | const s32 count = in->getAttributeAsInt("ItemCount"); |
730 | for (s32 i=0; i<count; ++i) | 730 | for (s32 i=0; i<count; ++i) |
731 | { | 731 | { |
732 | core::stringc label("text"); | 732 | core::stringc label("text"); |
733 | ListItem item; | 733 | ListItem item; |
734 | 734 | ||
735 | label += i; | 735 | label += i; |
736 | item.text = in->getAttributeAsStringW(label.c_str()); | 736 | item.text = in->getAttributeAsStringW(label.c_str()); |
737 | 737 | ||
738 | addItem(item.text.c_str(), item.icon); | 738 | addItem(item.text.c_str(), item.icon); |
739 | 739 | ||
740 | for ( u32 c=0; c < EGUI_LBC_COUNT; ++c ) | 740 | for ( u32 c=0; c < EGUI_LBC_COUNT; ++c ) |
741 | { | 741 | { |
742 | core::stringc useColorLabel, colorLabel; | 742 | core::stringc useColorLabel, colorLabel; |
743 | if ( !getSerializationLabels((EGUI_LISTBOX_COLOR)c, useColorLabel, colorLabel) ) | 743 | if ( !getSerializationLabels((EGUI_LISTBOX_COLOR)c, useColorLabel, colorLabel) ) |
744 | return; | 744 | return; |
745 | label = useColorLabel; label += i; | 745 | label = useColorLabel; label += i; |
746 | Items[i].OverrideColors[c].Use = in->getAttributeAsBool(label.c_str()); | 746 | Items[i].OverrideColors[c].Use = in->getAttributeAsBool(label.c_str()); |
747 | if ( Items[i].OverrideColors[c].Use ) | 747 | if ( Items[i].OverrideColors[c].Use ) |
748 | { | 748 | { |
749 | label = colorLabel; label += i; | 749 | label = colorLabel; label += i; |
750 | Items[i].OverrideColors[c].Color = in->getAttributeAsColor(label.c_str()); | 750 | Items[i].OverrideColors[c].Color = in->getAttributeAsColor(label.c_str()); |
751 | } | 751 | } |
752 | } | 752 | } |
753 | } | 753 | } |
754 | } | 754 | } |
755 | 755 | ||
756 | 756 | ||
757 | void CGUIListBox::recalculateItemWidth(s32 icon) | 757 | void CGUIListBox::recalculateItemWidth(s32 icon) |
758 | { | 758 | { |
759 | if (IconBank && icon > -1 && | 759 | if (IconBank && icon > -1 && |
760 | IconBank->getSprites().size() > (u32)icon && | 760 | IconBank->getSprites().size() > (u32)icon && |
761 | IconBank->getSprites()[(u32)icon].Frames.size()) | 761 | IconBank->getSprites()[(u32)icon].Frames.size()) |
762 | { | 762 | { |
763 | u32 rno = IconBank->getSprites()[(u32)icon].Frames[0].rectNumber; | 763 | u32 rno = IconBank->getSprites()[(u32)icon].Frames[0].rectNumber; |
764 | if (IconBank->getPositions().size() > rno) | 764 | if (IconBank->getPositions().size() > rno) |
765 | { | 765 | { |
766 | const s32 w = IconBank->getPositions()[rno].getWidth(); | 766 | const s32 w = IconBank->getPositions()[rno].getWidth(); |
767 | if (w > ItemsIconWidth) | 767 | if (w > ItemsIconWidth) |
768 | ItemsIconWidth = w; | 768 | ItemsIconWidth = w; |
769 | } | 769 | } |
770 | } | 770 | } |
771 | } | 771 | } |
772 | 772 | ||
773 | 773 | ||
774 | void CGUIListBox::setItem(u32 index, const wchar_t* text, s32 icon) | 774 | void CGUIListBox::setItem(u32 index, const wchar_t* text, s32 icon) |
775 | { | 775 | { |
776 | if ( index >= Items.size() ) | 776 | if ( index >= Items.size() ) |
777 | return; | 777 | return; |
778 | 778 | ||
779 | Items[index].text = text; | 779 | Items[index].text = text; |
780 | Items[index].icon = icon; | 780 | Items[index].icon = icon; |
781 | 781 | ||
782 | recalculateItemHeight(); | 782 | recalculateItemHeight(); |
783 | recalculateItemWidth(icon); | 783 | recalculateItemWidth(icon); |
784 | } | 784 | } |
785 | 785 | ||
786 | 786 | ||
787 | //! Insert the item at the given index | 787 | //! Insert the item at the given index |
788 | //! Return the index on success or -1 on failure. | 788 | //! Return the index on success or -1 on failure. |
789 | s32 CGUIListBox::insertItem(u32 index, const wchar_t* text, s32 icon) | 789 | s32 CGUIListBox::insertItem(u32 index, const wchar_t* text, s32 icon) |
790 | { | 790 | { |
791 | ListItem i; | 791 | ListItem i; |
792 | i.text = text; | 792 | i.text = text; |
793 | i.icon = icon; | 793 | i.icon = icon; |
794 | 794 | ||
795 | Items.insert(i, index); | 795 | Items.insert(i, index); |
796 | recalculateItemHeight(); | 796 | recalculateItemHeight(); |
797 | recalculateItemWidth(icon); | 797 | recalculateItemWidth(icon); |
798 | 798 | ||
799 | return index; | 799 | return index; |
800 | } | 800 | } |
801 | 801 | ||
802 | 802 | ||
803 | void CGUIListBox::swapItems(u32 index1, u32 index2) | 803 | void CGUIListBox::swapItems(u32 index1, u32 index2) |
804 | { | 804 | { |
805 | if ( index1 >= Items.size() || index2 >= Items.size() ) | 805 | if ( index1 >= Items.size() || index2 >= Items.size() ) |
806 | return; | 806 | return; |
807 | 807 | ||
808 | ListItem dummmy = Items[index1]; | 808 | ListItem dummmy = Items[index1]; |
809 | Items[index1] = Items[index2]; | 809 | Items[index1] = Items[index2]; |
810 | Items[index2] = dummmy; | 810 | Items[index2] = dummmy; |
811 | } | 811 | } |
812 | 812 | ||
813 | 813 | ||
814 | void CGUIListBox::setItemOverrideColor(u32 index, video::SColor color) | 814 | void CGUIListBox::setItemOverrideColor(u32 index, video::SColor color) |
815 | { | 815 | { |
816 | for ( u32 c=0; c < EGUI_LBC_COUNT; ++c ) | 816 | for ( u32 c=0; c < EGUI_LBC_COUNT; ++c ) |
817 | { | 817 | { |
818 | Items[index].OverrideColors[c].Use = true; | 818 | Items[index].OverrideColors[c].Use = true; |
819 | Items[index].OverrideColors[c].Color = color; | 819 | Items[index].OverrideColors[c].Color = color; |
820 | } | 820 | } |
821 | } | 821 | } |
822 | 822 | ||
823 | 823 | ||
824 | void CGUIListBox::setItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType, video::SColor color) | 824 | void CGUIListBox::setItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType, video::SColor color) |
825 | { | 825 | { |
826 | if ( index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) | 826 | if ( index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) |
827 | return; | 827 | return; |
828 | 828 | ||
829 | Items[index].OverrideColors[colorType].Use = true; | 829 | Items[index].OverrideColors[colorType].Use = true; |
830 | Items[index].OverrideColors[colorType].Color = color; | 830 | Items[index].OverrideColors[colorType].Color = color; |
831 | } | 831 | } |
832 | 832 | ||
833 | 833 | ||
834 | void CGUIListBox::clearItemOverrideColor(u32 index) | 834 | void CGUIListBox::clearItemOverrideColor(u32 index) |
835 | { | 835 | { |
836 | for (u32 c=0; c < (u32)EGUI_LBC_COUNT; ++c ) | 836 | for (u32 c=0; c < (u32)EGUI_LBC_COUNT; ++c ) |
837 | { | 837 | { |
838 | Items[index].OverrideColors[c].Use = false; | 838 | Items[index].OverrideColors[c].Use = false; |
839 | } | 839 | } |
840 | } | 840 | } |
841 | 841 | ||
842 | 842 | ||
843 | void CGUIListBox::clearItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) | 843 | void CGUIListBox::clearItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) |
844 | { | 844 | { |
845 | if ( index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) | 845 | if ( index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) |
846 | return; | 846 | return; |
847 | 847 | ||
848 | Items[index].OverrideColors[colorType].Use = false; | 848 | Items[index].OverrideColors[colorType].Use = false; |
849 | } | 849 | } |
850 | 850 | ||
851 | 851 | ||
852 | bool CGUIListBox::hasItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) const | 852 | bool CGUIListBox::hasItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) const |
853 | { | 853 | { |
854 | if ( index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) | 854 | if ( index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) |
855 | return false; | 855 | return false; |
856 | 856 | ||
857 | return Items[index].OverrideColors[colorType].Use; | 857 | return Items[index].OverrideColors[colorType].Use; |
858 | } | 858 | } |
859 | 859 | ||
860 | 860 | ||
861 | video::SColor CGUIListBox::getItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) const | 861 | video::SColor CGUIListBox::getItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) const |
862 | { | 862 | { |
863 | if ( (u32)index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) | 863 | if ( (u32)index >= Items.size() || colorType < 0 || colorType >= EGUI_LBC_COUNT ) |
864 | return video::SColor(); | 864 | return video::SColor(); |
865 | 865 | ||
866 | return Items[index].OverrideColors[colorType].Color; | 866 | return Items[index].OverrideColors[colorType].Color; |
867 | } | 867 | } |
868 | 868 | ||
869 | 869 | ||
870 | video::SColor CGUIListBox::getItemDefaultColor(EGUI_LISTBOX_COLOR colorType) const | 870 | video::SColor CGUIListBox::getItemDefaultColor(EGUI_LISTBOX_COLOR colorType) const |
871 | { | 871 | { |
872 | IGUISkin* skin = Environment->getSkin(); | 872 | IGUISkin* skin = Environment->getSkin(); |
873 | if ( !skin ) | 873 | if ( !skin ) |
874 | return video::SColor(); | 874 | return video::SColor(); |
875 | 875 | ||
876 | switch ( colorType ) | 876 | switch ( colorType ) |
877 | { | 877 | { |
878 | case EGUI_LBC_TEXT: | 878 | case EGUI_LBC_TEXT: |
879 | return skin->getColor(EGDC_BUTTON_TEXT); | 879 | return skin->getColor(EGDC_BUTTON_TEXT); |
880 | case EGUI_LBC_TEXT_HIGHLIGHT: | 880 | case EGUI_LBC_TEXT_HIGHLIGHT: |
881 | return skin->getColor(EGDC_HIGH_LIGHT_TEXT); | 881 | return skin->getColor(EGDC_HIGH_LIGHT_TEXT); |
882 | case EGUI_LBC_ICON: | 882 | case EGUI_LBC_ICON: |
883 | return skin->getColor(EGDC_ICON); | 883 | return skin->getColor(EGDC_ICON); |
884 | case EGUI_LBC_ICON_HIGHLIGHT: | 884 | case EGUI_LBC_ICON_HIGHLIGHT: |
885 | return skin->getColor(EGDC_ICON_HIGH_LIGHT); | 885 | return skin->getColor(EGDC_ICON_HIGH_LIGHT); |
886 | default: | 886 | default: |
887 | return video::SColor(); | 887 | return video::SColor(); |
888 | } | 888 | } |
889 | } | 889 | } |
890 | 890 | ||
891 | //! set global itemHeight | 891 | //! set global itemHeight |
892 | void CGUIListBox::setItemHeight( s32 height ) | 892 | void CGUIListBox::setItemHeight( s32 height ) |
893 | { | 893 | { |
894 | ItemHeight = height; | 894 | ItemHeight = height; |
895 | ItemHeightOverride = 1; | 895 | ItemHeightOverride = 1; |
896 | } | 896 | } |
897 | 897 | ||
898 | 898 | ||
899 | //! Sets whether to draw the background | 899 | //! Sets whether to draw the background |
900 | void CGUIListBox::setDrawBackground(bool draw) | 900 | void CGUIListBox::setDrawBackground(bool draw) |
901 | { | 901 | { |
902 | DrawBack = draw; | 902 | DrawBack = draw; |
903 | } | 903 | } |
904 | 904 | ||
905 | 905 | ||
906 | } // end namespace gui | 906 | } // end namespace gui |
907 | } // end namespace irr | 907 | } // end namespace irr |
908 | 908 | ||
909 | #endif // _IRR_COMPILE_WITH_GUI_ | 909 | #endif // _IRR_COMPILE_WITH_GUI_ |
910 | 910 | ||