1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "List.h"
#include <SDL_mouse.h>
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GL/myGL.h"
#include "Game/GlobalUnsynced.h"
#include "Gui.h"
#include "System/StringUtil.h"
namespace agui
{
const float itemFontScale = 1.0f;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
List::List(GuiElement* parent) :
GuiElement(parent),
cancelPlace(-1),
tooltip("No tooltip defined"),
clickedTime(spring_now()),
place(0),
activeMousePress(false),
activeScrollbar(false),
scrollbarGrabPos(0.0f),
filteredItems(&items)
{
borderSpacing = 0.005f;
itemSpacing = 0.000f;
itemHeight = 0.04f;
hasFocus = true;
topIndex = 0;
mx = my = 0;
}
List::~List()
{
}
void List::SetFocus(bool focus)
{
hasFocus = focus;
}
void List::RemoveAllItems() {
items.clear();
}
void List::RefreshQuery() {
if (query != "") {
int t = topIndex;
std::string q = query;
Filter(true);
query = q;
Filter(false);
topIndex = t;
}
}
void List::AddItem(const std::string& name, const std::string& description)
{
items.push_back(name);
// calculate width of text and resize box if necessary
float w = itemFontScale * font->GetSize() * font->GetTextWidth(name) * screensize[0] + 2 * itemSpacing;
if (w > (size[0]))
{
//box.x1 = 0.5f - 0.5f * w;
//box.x2 = 0.5f + 0.5f * w;
}
}
bool List::MousePress(int x, int y, int button)
{
switch (button)
{
case SDL_BUTTON_LEFT:
{
activeMousePress = true;
MouseUpdate(x, y); // make sure place is up to date
break;
}
}
return false;
}
int List::NumDisplay()
{
return std::max(1.0f, (size[1] - 2.0f * borderSpacing) / (itemHeight + itemSpacing));
}
void List::ScrollUpOne()
{
if(topIndex > 0)
--topIndex;
}
void List::ScrollDownOne()
{
if(topIndex + NumDisplay() < filteredItems->size())
++topIndex;
}
void List::MouseMove(int x, int y, int dx,int dy, int button)
{
mx = PixelToGlX(x);
my = PixelToGlY(y);
}
void List::MouseRelease(int x, int y, int button)
{
if(button & SDL_BUTTON_LEFT) {
activeMousePress = false;
activeScrollbar = false;
}
}
float List::ScaleFactor() {
return (float)std::max(1, globalRendering->winSizeY) * (size[1] - 2.0f * borderSpacing);
}
void List::UpdateTopIndex()
{
itemHeight = 18.0f / ScaleFactor();
const int numDisplay = NumDisplay();
if(topIndex + numDisplay > filteredItems->size())
topIndex = std::max(0, (int)filteredItems->size() - numDisplay);
}
bool List::MouseUpdate(int x, int y)
{
int nCurIndex = 0; // The item we're on
int nDrawOffset = 0; // The offset to the first draw item
GuiElement b;
b.SetPos(pos[0] + borderSpacing, pos[1] + size[1] - borderSpacing - itemHeight);
b.SetSize(size[0] - 2.0f * borderSpacing - ((scrollbar.GetSize()[0] < 0) ? 0 : (itemHeight + itemSpacing)), itemHeight);
// Get list started up here
std::vector<std::string>::iterator ii = filteredItems->begin();
UpdateTopIndex();
while (nCurIndex < topIndex) { ++ii; ++nCurIndex; }
const int numDisplay = NumDisplay();
float sbX = b.GetPos()[0];
float sbY1 = b.GetPos()[1] + (itemHeight + itemSpacing);
for (/*ii = items.begin()*/; ii != filteredItems->end() && nDrawOffset < numDisplay; ++ii)
{
if (b.MouseOver(mx, my))
{
if (nCurIndex == place && (clickedTime + spring_msecs(250)) > spring_now())
{
FinishSelection.emit();
}
clickedTime = spring_now();
place = nCurIndex;
return true;
}
// Up our index's
nCurIndex++; nDrawOffset++;
b.Move(0.0, - (itemHeight + itemSpacing));
}
if(nDrawOffset < filteredItems->size()) {
if (scrollbar.MouseOver(mx, my)) {
activeScrollbar = true;
scrollbarGrabPos = my - scrollbar.GetPos()[1];
}
else {
float sbY2 = b.GetPos()[1] + (itemHeight + itemSpacing);
float sbHeight = sbY1 - sbY2;
b.SetPos(sbX + (size[0] - 2.0f * borderSpacing) - (itemHeight + itemSpacing), sbY2);
b.SetSize(itemHeight + itemSpacing, sbHeight);
if (b.MouseOver(mx, my)) {
if(my > scrollbar.GetPos()[1] + scrollbar.GetSize()[1])
topIndex = std::max(0, std::min(topIndex - NumDisplay(), (int)filteredItems->size() - NumDisplay()));
else if(my < scrollbar.GetPos()[1])
topIndex = std::max(0, std::min(topIndex + NumDisplay(), (int)filteredItems->size() - NumDisplay()));
}
}
}
return false;
}
void List::DrawSelf()
{
gui->SetDrawMode(Gui::DrawMode::COLOR);
const float opacity = Opacity();
const float hf = font->GetSize() / ScaleFactor();
font->SetTextColor(1.0f, 1.0f, 0.4f, opacity);
int nCurIndex = 0; // The item we're on
int nDrawOffset = 0; // The offset to the first draw item
GuiElement b;
b.SetPos(pos[0] + borderSpacing, pos[1] + size[1] - borderSpacing - itemHeight);
b.SetSize(size[0] - 2.0f * borderSpacing - ((scrollbar.GetSize()[0] < 0) ? 0 : (itemHeight + itemSpacing)), itemHeight);
// Get list started up here
std::vector<std::string>::iterator ii = filteredItems->begin();
// Skip to current selection - 3; ie: scroll
UpdateTopIndex();
ii += std::max(0, topIndex - nCurIndex);
nCurIndex = topIndex;
const int numDisplay = NumDisplay();
font->SetTextColor(1.0f, 1.0f, 1.0f, opacity); //default
font->SetOutlineColor(0.0f, 0.0f, 0.0f, opacity);
font->SetTextDepth(depth);
float sbX = b.GetPos()[0];
float sbY1 = b.GetPos()[1] + (itemHeight + itemSpacing);
for (/*ii = items.begin()*/; ii != filteredItems->end() && nDrawOffset < numDisplay; ++ii) {
gui->SetColor(1.0f, 1.0f, 1.0f, opacity * 0.25f);
b.DrawOutline();
if (nCurIndex == place) {
glAttribStatePtr->BlendFunc(GL_ONE, GL_ONE); // additive blending
gui->SetColor(0.2f, 0.0f, 0.0f, opacity);
b.DrawBox();
glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gui->SetColor(1.0f, 0.0f, 0.0f, opacity / 2.0f);
b.DrawOutline();
} else if (b.MouseOver(mx, my)) {
glAttribStatePtr->BlendFunc(GL_ONE, GL_ONE); // additive blending
gui->SetColor(0.0f, 0.0f, 0.2f, opacity);
b.DrawBox();
glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gui->SetColor(1.0f, 1.0f, 1.0f, opacity / 2.0f);
b.DrawOutline();
}
font->glPrint(pos[0] + borderSpacing + 0.002f, b.GetMidY() - hf * 0.15f, itemFontScale, FONT_BASELINE | FONT_SHADOW | FONT_SCALE | FONT_NORM | FONT_BUFFERED, *ii);
nCurIndex++;
nDrawOffset++;
b.Move(0.0, - (itemHeight + itemSpacing));
}
gui->SetDrawMode(Gui::DrawMode::FONT);
font->DrawBufferedGL4(gui->GetShader());
gui->SetDrawMode(Gui::DrawMode::COLOR);
// scrollbar
if (nDrawOffset < filteredItems->size()) {
const float sbY2 = b.GetPos()[1] + (itemHeight + itemSpacing);
const float sbHeight = sbY1 - sbY2;
const float sbSize = (nDrawOffset * 1.0f / filteredItems->size()) * sbHeight;
if (activeScrollbar) {
const float dify = my - std::min(scrollbarGrabPos, sbSize);
const float rely = ((sbY1 - sbSize) - dify) / sbHeight;
const int minIndex = (filteredItems->size() * rely) + 0.5f;
const int endIndex = filteredItems->size() - numDisplay;
topIndex = std::min(minIndex, endIndex);
topIndex = std::max(0, topIndex);
}
scrollbar.SetPos(
sbX + (size[0] - 2.0f * borderSpacing) - (itemHeight + itemSpacing),
sbY1 - sbSize - ((float)topIndex / (float)filteredItems->size()) * sbHeight
);
scrollbar.SetSize((itemHeight + itemSpacing) , sbSize);
b.SetPos(scrollbar.GetPos()[0], sbY2);
b.SetSize(itemHeight + itemSpacing, sbHeight);
gui->SetColor(1.0f, 1.0f, 1.0f, opacity / 4.0f);
b.DrawOutline();
glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gui->SetColor(0.8f, 0.8f, 0.8f, opacity);
scrollbar.DrawBox();
glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gui->SetColor(1.0f, 1.0f, 1.0f, opacity / 2.0f);
scrollbar.DrawOutline();
return;
}
scrollbar.SetSize(-1, -1);
}
bool List::HandleEventSelf(const SDL_Event& ev)
{
switch (ev.type) {
case SDL_MOUSEBUTTONDOWN: {
hasFocus = gui->MouseOverElement(GetRoot(), ev.button.x, ev.button.y);
if(MouseOver(ev.button.x, ev.button.y)) {
if(hasFocus) {
MousePress(ev.button.x, ev.button.y, ev.button.button);
return true;
}
}
break;
}
case SDL_MOUSEBUTTONUP: {
if (!hasFocus)
break;
if (MouseOver(ev.button.x, ev.button.y) || activeScrollbar)
{
MouseRelease(ev.button.x, ev.button.y, ev.button.button);
return true;
}
break;
}
case SDL_MOUSEWHEEL: {
int mousex, mousey;
SDL_GetMouseState(&mousex, &mousey);
if(hasFocus && MouseOver(mousex, mousey)) {
if (ev.wheel.y > 0) {
ScrollUpOne();
} else {
ScrollDownOne();
}
return true;
}
} break;
case SDL_MOUSEMOTION: {
if (!hasFocus)
break;
if (MouseOver(ev.motion.x, ev.motion.y) || activeScrollbar)
{
MouseMove(ev.motion.x, ev.motion.y, ev.motion.xrel, ev.motion.yrel, ev.motion.state);
return true;
}
break;
}
case SDL_KEYDOWN: {
if (!hasFocus)
break;
if(ev.key.keysym.sym == SDLK_ESCAPE)
{
hasFocus = false;
break;
}
return KeyPressed(ev.key.keysym.sym, false);
}
}
return false;
}
void List::UpOne()
{
place = std::max(0, place - 1);
}
void List::DownOne()
{
place = std::max(0, std::min(place + 1, int(filteredItems->size()) - 1));
}
void List::UpPage()
{
place = std::max(0, place - NumDisplay());
}
void List::DownPage()
{
place = std::max(0, std::min(place + NumDisplay(), int(filteredItems->size()) - 1));
}
std::string List::GetCurrentItem() const
{
if (place < filteredItems->size()) {
return ((*filteredItems)[place]);
}
return "";
}
bool List::SetCurrentItem(const std::string& newCurrent)
{
for (unsigned i = 0; i < items.size(); ++i)
{
if (newCurrent == items[i])
{
place = i;
CenterSelected();
return true;
}
}
return false;
}
void List::CenterSelected()
{
topIndex = std::max(0, place - NumDisplay()/2);
}
bool List::KeyPressed(int k, bool isRepeat)
{
if (k == SDLK_ESCAPE) {
if (cancelPlace >= 0) {
place = cancelPlace;
return true;
}
} else if (k == SDLK_UP) {
UpOne();
CenterSelected();
return true;
} else if (k == SDLK_DOWN) {
DownOne();
CenterSelected();
return true;
} else if (k == SDLK_PAGEUP) {
UpPage();
CenterSelected();
return true;
} else if (k == SDLK_PAGEDOWN) {
DownPage();
CenterSelected();
return true;
} else if (k == SDLK_BACKSPACE) {
query = query.substr(0, query.length() - 1);
return Filter(true);
} else if (k == SDLK_RETURN) {
FinishSelection.emit();
return true;
} else if ((k & ~0xFF) != 0) {
// This prevents isalnum from asserting on msvc debug crt
// We don't actually need to process the key tho;)
} else if (isalnum(k)) {
query += tolower(k);
return Filter(false);
}
return false;
}
bool List::Filter(bool reset)
{
std::string current = GetCurrentItem();
std::vector<std::string>* destination = filteredItems == &temp1 ? &temp2 : &temp1;
destination->clear();
if (reset)
filteredItems = &items; // reset filter
for (std::vector<std::string>::const_iterator it = filteredItems->begin(); it != filteredItems->end(); ++it) {
std::string lcitem(*it, 0, query.length());
StringToLowerInPlace(lcitem);
if (lcitem == query) {
if (*it == current)
place = destination->size();
destination->push_back(*it);
}
}
if (destination->empty()) {
query = query.substr(0, query.length() - 1);
return false;
} else {
filteredItems = destination;
place = std::max(0, std::min(place, int(filteredItems->size()) - 1));
topIndex = 0;
return true;
}
}
}
|