| 1 |
#include <ClanLib/core.h> // for CL_DomElement |
| 2 |
#include <ClanLib/display.h> |
| 3 |
#include <ClanLib/gl.h> |
| 4 |
|
| 5 |
#include "listbox_zz.h" |
| 6 |
#include "stylemanager_zz.h" |
| 7 |
#include "scrollbar_zz.h" |
| 8 |
|
| 9 |
CL_ListBox_ZZ::CL_ListBox_ZZ( |
| 10 |
CL_ListBox * listbox, |
| 11 |
CL_StyleManager_ZZ * style) |
| 12 |
: |
| 13 |
CL_ComponentStyle(listbox), |
| 14 |
listbox_(listbox), |
| 15 |
style_(style), |
| 16 |
resources_(style->get_resources()), |
| 17 |
first_paint_(true), |
| 18 |
listbox_type_(type_normal) |
| 19 |
{ |
| 20 |
font_ = new CL_Font("ListBox/font", resources_); |
| 21 |
font_->set_color(152.0f / 255, 211.0f / 255, 101.0f / 255); |
| 22 |
|
| 23 |
// Create a vertical scrollbar |
| 24 |
scrollbar_ = new CL_ScrollBar(false, listbox); |
| 25 |
{ |
| 26 |
CL_ScrollBar_ZZ * sb_type = (CL_ScrollBar_ZZ*)scrollbar_->get_style(); |
| 27 |
if (sb_type) |
| 28 |
sb_type->set_type(CL_ScrollBar_ZZ::type_listbox); |
| 29 |
} |
| 30 |
visible_scrollbar_ = true; |
| 31 |
|
| 32 |
int font_height = font_->get_height(); |
| 33 |
listbox_->set_item_height(font_height); |
| 34 |
|
| 35 |
CL_Component *client_area = listbox_->get_client_area(); |
| 36 |
|
| 37 |
slots_.connect(listbox_->sig_set_options(), this, &CL_ListBox_ZZ::on_set_options); |
| 38 |
slots_.connect(listbox_->sig_paint(), this, &CL_ListBox_ZZ::on_paint_background); |
| 39 |
slots_.connect(client_area->sig_mouse_down(), this, &CL_ListBox_ZZ::on_mouse_down); |
| 40 |
slots_.connect(client_area->sig_paint(), this, &CL_ListBox_ZZ::on_paint_listbox); |
| 41 |
slots_.connect(listbox_->sig_resize(), this, &CL_ListBox_ZZ::on_resize); |
| 42 |
slots_.connect(listbox_->sig_item_added(), this, &CL_ListBox_ZZ::on_item_added); |
| 43 |
slots_.connect(listbox_->sig_item_removed(), this, &CL_ListBox_ZZ::on_item_removed); |
| 44 |
slots_.connect(listbox_->sig_clear(), this, &CL_ListBox_ZZ::on_clear); |
| 45 |
slots_.connect(scrollbar_->sig_value_changed(), this, &CL_ListBox_ZZ::on_scroll_change); |
| 46 |
|
| 47 |
on_resize(0,0); |
| 48 |
} |
| 49 |
|
| 50 |
CL_ListBox_ZZ::~CL_ListBox_ZZ() |
| 51 |
{ |
| 52 |
delete font_; |
| 53 |
delete scrollbar_; |
| 54 |
} |
| 55 |
|
| 56 |
void CL_ListBox_ZZ::on_paint_background() |
| 57 |
{ |
| 58 |
if (first_paint_) |
| 59 |
{ |
| 60 |
if (listbox_->get_count() > 0) |
| 61 |
{ |
| 62 |
if (listbox_->get_current_item() == -1) |
| 63 |
{ |
| 64 |
listbox_->set_current_item(0); |
| 65 |
} |
| 66 |
listbox_->insert_item("hack"); |
| 67 |
listbox_->remove_item(listbox_->get_count() - 1); |
| 68 |
} |
| 69 |
first_paint_ = false; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
int width = listbox_->get_width(); |
| 74 |
int height = listbox_->get_height(); |
| 75 |
|
| 76 |
// main |
| 77 |
CL_Color const clr_background(0, 40, 0, 60); |
| 78 |
CL_Rect rc_main = listbox_->get_screen_rect(); |
| 79 |
style_->fill_rounded_rect(rc_main, clr_background); |
| 80 |
|
| 81 |
// Outline |
| 82 |
CL_Color clr_outline(175, 205, 148); |
| 83 |
CL_Rect rc_outline = rc_main; |
| 84 |
style_->draw_rounded_rect(rc_outline, clr_outline); |
| 85 |
} |
| 86 |
|
| 87 |
void CL_ListBox_ZZ::on_paint_listbox() |
| 88 |
{ |
| 89 |
int width = listbox_->get_client_area()->get_width(); |
| 90 |
int height = listbox_->get_client_area()->get_height(); |
| 91 |
|
| 92 |
int offset_x = listbox_->get_client_area()->get_screen_x(); |
| 93 |
int offset_y = listbox_->get_client_area()->get_screen_y(); |
| 94 |
|
| 95 |
if (listbox_type_ == type_normal) |
| 96 |
{ |
| 97 |
CL_Display::push_cliprect(listbox_->get_client_area()->get_screen_rect()); |
| 98 |
int pos = 0; |
| 99 |
int offset = listbox_->get_top_item(); |
| 100 |
std::vector<CL_ListItem *> items = listbox_->get_items(); |
| 101 |
std::vector<CL_ListItem *>::iterator it; |
| 102 |
|
| 103 |
font_->set_color(152.0f / 255, 211.0f / 255, 101.0f / 255); |
| 104 |
for (it = items.begin(); it != items.end(); ++it, ++pos) |
| 105 |
{ |
| 106 |
if (pos < offset) |
| 107 |
continue; |
| 108 |
|
| 109 |
if (pos == listbox_->get_current_item()) |
| 110 |
{ |
| 111 |
CL_Display::fill_rect( |
| 112 |
CL_Rect(offset_x, offset_y + (pos - offset) * font_->get_height(), |
| 113 |
offset_x + width, offset_y + (pos + 1 - offset) * font_->get_height()), |
| 114 |
CL_Color(93, 156, 39)); |
| 115 |
font_->set_color(255.0f / 255, 255.0f / 255, 255.0f / 255); |
| 116 |
font_->draw( |
| 117 |
offset_x + 4, |
| 118 |
offset_y + (pos - offset) * font_->get_height(), |
| 119 |
(*it)->str); |
| 120 |
font_->set_color(152.0f / 255, 211.0f / 255, 101.0f / 255); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
font_->draw( |
| 125 |
offset_x + 4, |
| 126 |
offset_y + (pos - offset) * font_->get_height(), |
| 127 |
(*it)->str); |
| 128 |
} |
| 129 |
|
| 130 |
if((pos - offset) * font_->get_height() >= height) |
| 131 |
break; |
| 132 |
} |
| 133 |
CL_Display::pop_cliprect(); |
| 134 |
} |
| 135 |
else |
| 136 |
if (listbox_type_ == type_levels_list) |
| 137 |
{ |
| 138 |
CL_Display::push_cliprect(listbox_->get_client_area()->get_screen_rect()); |
| 139 |
int pos = 0; |
| 140 |
int offset = listbox_->get_top_item(); |
| 141 |
std::vector<CL_ListItem *> items = listbox_->get_items(); |
| 142 |
std::vector<CL_ListItem *>::iterator it; |
| 143 |
|
| 144 |
for (it = items.begin(); it != items.end(); ++it, ++pos) |
| 145 |
{ |
| 146 |
if (pos < offset) |
| 147 |
continue; |
| 148 |
|
| 149 |
CL_ListBoxItem_ZZ * item = (CL_ListBoxItem_ZZ *)(*it); |
| 150 |
std::string text; |
| 151 |
CL_Color text_color; |
| 152 |
if (item->get_solved()) |
| 153 |
{ |
| 154 |
text = "(+) " + item->get_levelname(); |
| 155 |
text_color = CL_Color(211, 211, 101); |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
text = "(-) " + item->get_levelname(); |
| 160 |
text_color = CL_Color(152, 211, 101); |
| 161 |
} |
| 162 |
|
| 163 |
if (pos == listbox_->get_current_item()) |
| 164 |
{ |
| 165 |
CL_Display::fill_rect( |
| 166 |
CL_Rect(offset_x, offset_y + (pos - offset) * font_->get_height(), |
| 167 |
offset_x + width, offset_y + (pos + 1 - offset) * font_->get_height()), |
| 168 |
CL_Color(93, 156, 39)); |
| 169 |
font_->set_color(255.0f / 255, 255.0f / 255, 255.0f / 255); |
| 170 |
font_->draw( |
| 171 |
offset_x + 4, |
| 172 |
offset_y + (pos - offset) * font_->get_height(), |
| 173 |
text); |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
font_->set_color(text_color); |
| 178 |
font_->draw( |
| 179 |
offset_x + 4, |
| 180 |
offset_y + (pos - offset) * font_->get_height(), |
| 181 |
text); |
| 182 |
} |
| 183 |
|
| 184 |
if((pos - offset) * font_->get_height() >= height) |
| 185 |
break; |
| 186 |
} |
| 187 |
CL_Display::pop_cliprect(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void CL_ListBox_ZZ::on_item_added(int index) |
| 192 |
{ |
| 193 |
update_scrollbar(true); |
| 194 |
} |
| 195 |
|
| 196 |
void CL_ListBox_ZZ::on_item_removed(int index) |
| 197 |
{ |
| 198 |
update_scrollbar(true); |
| 199 |
} |
| 200 |
|
| 201 |
void CL_ListBox_ZZ::on_clear() |
| 202 |
{ |
| 203 |
update_scrollbar(true); |
| 204 |
} |
| 205 |
|
| 206 |
void CL_ListBox_ZZ::on_set_options(const CL_DomElement &options) |
| 207 |
{ |
| 208 |
if (options.get_attribute("type") == "levels_list") |
| 209 |
{ |
| 210 |
listbox_type_ = type_levels_list; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void CL_ListBox_ZZ::on_scroll_change(int new_offset) |
| 215 |
{ |
| 216 |
listbox_->set_top_item(new_offset); |
| 217 |
} |
| 218 |
|
| 219 |
void CL_ListBox_ZZ::on_mouse_down(const CL_InputEvent &key) |
| 220 |
{ |
| 221 |
int height_of_all_items = listbox_->get_item_height() * listbox_->get_count(); |
| 222 |
int height_of_client_area = listbox_->get_client_area()->get_height(); |
| 223 |
|
| 224 |
if ( key.id == CL_MOUSE_WHEEL_UP |
| 225 |
&& height_of_all_items > height_of_client_area ) |
| 226 |
{ |
| 227 |
listbox_->set_top_item(listbox_->get_top_item() - 5); |
| 228 |
update_scrollbar(true); |
| 229 |
return; |
| 230 |
} |
| 231 |
if ( key.id == CL_MOUSE_WHEEL_DOWN |
| 232 |
&& height_of_all_items > height_of_client_area ) |
| 233 |
{ |
| 234 |
listbox_->set_top_item(listbox_->get_top_item() + 5); |
| 235 |
update_scrollbar(true); |
| 236 |
return; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void CL_ListBox_ZZ::on_resize(int old_width, int old_height) |
| 241 |
{ |
| 242 |
// Position the listbox item-box |
| 243 |
CL_Rect rect(4, 4, 4 + listbox_->get_width() - 8 , 4 + listbox_->get_height() - 8); |
| 244 |
listbox_->get_client_area()->set_position(rect); |
| 245 |
|
| 246 |
update_scrollbar(false); |
| 247 |
|
| 248 |
// Scrollbar was added, shrink clientarea |
| 249 |
if(visible_scrollbar_) |
| 250 |
{ |
| 251 |
listbox_->get_client_area()->set_width(listbox_->get_client_area()->get_width() - 16); |
| 252 |
|
| 253 |
// Position the scrollbar |
| 254 |
rect = CL_Rect(listbox_->get_width() - 19, 0, listbox_->get_width(), listbox_->get_height()); |
| 255 |
scrollbar_->set_position(rect); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
///////////////////////////////////////////////////////////////////////////// |
| 261 |
// Implementation: |
| 262 |
|
| 263 |
void CL_ListBox_ZZ::update_scrollbar(bool resize) |
| 264 |
{ |
| 265 |
int max_visible_items = (listbox_->get_client_area()->get_height() - 4) / listbox_->get_item_height(); |
| 266 |
|
| 267 |
if(listbox_->get_count() > max_visible_items) |
| 268 |
{ |
| 269 |
if(visible_scrollbar_ == false) |
| 270 |
{ |
| 271 |
visible_scrollbar_ = true; |
| 272 |
listbox_->add_child(scrollbar_, false); |
| 273 |
|
| 274 |
if(resize) |
| 275 |
on_resize(0,0); |
| 276 |
} |
| 277 |
|
| 278 |
scrollbar_->set_max_value(listbox_->get_count() - max_visible_items); |
| 279 |
scrollbar_->set_value(listbox_->get_top_item()); |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
if(visible_scrollbar_) |
| 284 |
{ |
| 285 |
visible_scrollbar_ = false; |
| 286 |
scrollbar_->set_value(0); |
| 287 |
listbox_->remove_child(scrollbar_); |
| 288 |
|
| 289 |
if(resize) |
| 290 |
on_resize(0,0); |
| 291 |
} |
| 292 |
} |
| 293 |
} |