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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
/*
This file is part of Warzone 2100.
Copyright (C) 2020 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Functions for dropdown.
*/
#include "dropdown.h"
#include "label.h"
#include "widgbase.h"
#include "form.h"
#include "lib/ivis_opengl/pieblitfunc.h"
#include "lib/framework/input.h"
class DropdownItemWrapper;
class DropdownOverlay: public WIDGET
{
public:
DropdownOverlay(std::function<void ()> onClickedFunc)
: onClickedFunc(onClickedFunc)
{
setCalcLayout(LAMBDA_CALCLAYOUT_SIMPLE({
psWidget->setGeometry(0, 0, screenWidth - 1, screenHeight - 1);
}));
}
void clicked(W_CONTEXT *, WIDGET_KEY = WKEY_PRIMARY) override
{
onClickedFunc();
}
void display(int xOffset, int yOffset) override
{
displayChildDropShadows(this, xOffset, yOffset);
}
private:
std::function<void ()> onClickedFunc;
};
void DropdownItemWrapper::initialize(const std::shared_ptr<DropdownWidget>& newParent, const std::shared_ptr<WIDGET> &newItem, DropdownOnSelectHandler newOnSelect)
{
item = newItem;
attach(item);
onSelect = newOnSelect;
parent = newParent;
}
std::shared_ptr<WIDGET> DropdownItemWrapper::findMouseTargetRecursive(W_CONTEXT *psContext, WIDGET_KEY key, bool wasPressed)
{
W_CONTEXT inputContext(psContext);
auto result = WIDGET::findMouseTargetRecursive(psContext, key, wasPressed);
if (key != WKEY_NONE && this->hitTest(inputContext.mx, inputContext.my))
{
if (auto psStrongParent = parent.lock())
{
psStrongParent->setMouseClickOnItem(std::dynamic_pointer_cast<DropdownItemWrapper>(shared_from_this()), key, wasPressed);
}
if (wasPressed)
{
mouseDownOnWrapper = true;
}
else
{
// if mouse down + mouse up on the item
if (mouseDownOnWrapper)
{
mouseDownOnWrapper = false;
// Instead of calling the onSelect handler immediately, schedule a task.
//
// (The onSelect handler may call close(), and we want that to be delayed by an event processing cycle
// so the mouse event gets properly attributed to the overlay screen by other code - such as the game
// event processing loop, which currently usese isMouseOverScreenOverlayChild)
auto weakSelf = std::weak_ptr<DropdownItemWrapper>(std::static_pointer_cast<DropdownItemWrapper>(shared_from_this()));
widgScheduleTask([weakSelf]() {
auto strongSelf = weakSelf.lock();
ASSERT_OR_RETURN(, strongSelf != nullptr, "DropdownItemWrapper disappeared?");
strongSelf->onSelect(strongSelf);
});
}
}
}
return result;
}
void DropdownItemWrapper::clearMouseDownState()
{
mouseDownOnWrapper = false;
}
void DropdownItemWrapper::geometryChanged()
{
item->setGeometry(0, 0, width(), height());
}
void DropdownItemWrapper::display(int xOffset, int yOffset)
{
if (selected)
{
auto x0 = xOffset + x();
auto y0 = yOffset + y();
pie_UniTransBoxFill(x0, y0, x0 + width(), y0 + height(), WZCOL_MENU_SCORE_BUILT);
}
}
DropdownWidget::DropdownWidget()
{
itemsList = ScrollableListWidget::make();
itemsList->setBackgroundColor(WZCOL_MENU_BACKGROUND);
setListHeight(100);
}
void DropdownWidget::run(W_CONTEXT *psContext)
{
if (overlayScreen)
{
itemsList->setGeometry(calculateDropdownListScreenPosX(), calculateDropdownListScreenPosY(), itemsList->width(), itemsList->height());
if (keyPressed(KEY_ESC))
{
inputLoseFocus(); // clear the input buffer.
close();
}
}
else
{
callRunOnItems();
}
}
void DropdownWidget::callRunOnItems()
{
// Some child widgets might use run() to clear cached textures (etc), so call run regularly to allow them
for (const auto& item : items)
{
auto ctx = W_CONTEXT::ZeroContext();
item->item->runRecursive(&ctx);
item->item->manuallyCallRun(&ctx);
}
}
void DropdownWidget::geometryChanged()
{
int w = width();
int h = height();
if (w == 0 || h == 0)
{
return;
}
int itemWidth = getItemDisplayWidth();
itemsList->setGeometry(itemsList->x(), itemsList->y(), calculateDropdownListDisplayWidth(), itemsList->height());
switch (menuStyle)
{
case DropdownMenuStyle::InPlace:
for(auto& item : items)
{
item->setGeometry(0, 0, itemWidth, h);
}
break;
case DropdownMenuStyle::Separate:
break;
}
}
void DropdownWidget::clicked(W_CONTEXT *psContext, WIDGET_KEY key)
{
open();
}
int DropdownWidget::calculateDropdownListScreenPosX() const
{
return screenPosX();
}
int DropdownWidget::calculateDropdownListDisplayWidth() const
{
switch (menuStyle)
{
case DropdownMenuStyle::InPlace:
return getItemDisplayWidth();
case DropdownMenuStyle::Separate:
return itemsList->idealWidth();
}
return 0; // silence warning
}
void DropdownWidget::setListBackgroundColor(const PIELIGHT& color)
{
itemsList->setBackgroundColor(color);
}
const Padding& DropdownWidget::getDropdownMenuOuterPadding() const
{
return itemsList->getPadding();
}
int DropdownWidget::calculateDropdownListScreenPosY() const
{
int yPosOffset = 0;
switch (menuStyle)
{
case DropdownMenuStyle::InPlace:
yPosOffset = -overlayYPosOffset;
break;
case DropdownMenuStyle::Separate:
yPosOffset = height();
break;
}
int dropDownOverlayPosY = screenPosY() + yPosOffset;
if (dropDownOverlayPosY + itemsList->height() > screenHeight)
{
// Positioning the dropdown below would cause it to appear partially or fully offscreen
// So, instead, position it above
dropDownOverlayPosY = screenPosY() - itemsList->height();
if (dropDownOverlayPosY < 0)
{
// Well, this would be off-screen too...
// For now: Just position it at the top
dropDownOverlayPosY = 0;
}
}
return dropDownOverlayPosY;
}
void DropdownWidget::open()
{
if (overlayScreen != nullptr) { return; }
mouseDownItem.reset();
std::weak_ptr<DropdownWidget> pWeakThis(std::dynamic_pointer_cast<DropdownWidget>(shared_from_this()));
widgScheduleTask([pWeakThis]() {
if (auto dropdownWidget = pWeakThis.lock())
{
if (dropdownWidget->onOpen)
{
dropdownWidget->onOpen(*dropdownWidget);
}
dropdownWidget->overlayScreen = W_SCREEN::make();
switch (dropdownWidget->menuStyle)
{
case DropdownMenuStyle::InPlace:
{
// calculate the ideal position so that the dropdown appears *overtop* of the currently-selected item (in-place)
size_t selectedItemIndex = dropdownWidget->getSelectedIndex().value_or(0);
size_t listViewTopIndex = 0;
if (dropdownWidget->itemsList->idealHeight() > dropdownWidget->itemsList->height())
{
size_t maxOffset = std::min<size_t>(2, dropdownWidget->itemsList->numItems());
if (selectedItemIndex > maxOffset)
{
listViewTopIndex = selectedItemIndex - maxOffset;
}
dropdownWidget->itemsList->scrollToItem(listViewTopIndex);
}
dropdownWidget->overlayYPosOffset = dropdownWidget->itemsList->getCurrentYPosOfItem(selectedItemIndex);
}
break;
case DropdownMenuStyle::Separate:
break;
}
auto newRootFrm = std::make_shared<DropdownOverlay>(
[pWeakThis]() { if (auto dropdownWidget = pWeakThis.lock()) { dropdownWidget->close(); } }
);
dropdownWidget->overlayScreen->psForm->attach(newRootFrm);
dropdownWidget->itemsList->setGeometry(dropdownWidget->calculateDropdownListScreenPosX(), dropdownWidget->calculateDropdownListScreenPosY(), dropdownWidget->calculateDropdownListDisplayWidth(), dropdownWidget->itemsList->height());
newRootFrm->attach(dropdownWidget->itemsList);
widgRegisterOverlayScreenOnTopOfScreen(dropdownWidget->overlayScreen, dropdownWidget->screenPointer.lock());
}
});
}
bool DropdownWidget::isOpen() const
{
return overlayScreen != nullptr;
}
void DropdownWidget::close()
{
ASSERT_OR_RETURN(, isOpen(), "Dropdown isn't open");
mouseDownItem.reset();
std::weak_ptr<DropdownWidget> pWeakThis(std::dynamic_pointer_cast<DropdownWidget>(shared_from_this()));
widgScheduleTask([pWeakThis]() {
if (auto dropdownWidget = pWeakThis.lock())
{
if (dropdownWidget->overlayScreen != nullptr)
{
if (dropdownWidget->onClose)
{
dropdownWidget->onClose(*dropdownWidget);
}
widgRemoveOverlayScreen(dropdownWidget->overlayScreen);
if (dropdownWidget->overlayScreen->psForm)
{
dropdownWidget->overlayScreen->psForm->detach(dropdownWidget->itemsList);
}
dropdownWidget->overlayScreen = nullptr;
}
}
});
}
int32_t DropdownWidget::getItemDisplayWidth() const
{
return std::max(width() - getCaretImageUsedWidth(), 0);
}
int32_t DropdownWidget::getCaretImageUsedWidth() const
{
return dropdownCaretImageSize.width() + dropdownCaretImagePadding.left + dropdownCaretImagePadding.right;
}
void DropdownWidget::display(int xOffset, int yOffset)
{
auto x0 = xOffset + x();
auto y0 = yOffset + y();
if (isOpen())
{
drawOpenedHighlight(xOffset, yOffset);
}
auto itemWidth = getItemDisplayWidth();
drawDropdownCaretImage(xOffset, yOffset, WZCOL_TEXT_MEDIUM);
if (selectedItem)
{
WzRect displayArea(x0, y0, itemWidth, height());
drawSelectedItem(selectedItem->getItem(), displayArea);
}
}
void DropdownWidget::drawDropdownCaretImage(int xOffset, int yOffset, PIELIGHT color)
{
if (!dropdownCaretImage.has_value())
{
return;
}
auto x0 = xOffset + x();
auto y0 = yOffset + y();
int caretX0 = x0 + getItemDisplayWidth() + dropdownCaretImagePadding.left;
int caretY0 = y0 + ((height() - dropdownCaretImageSize.height()) / 2);
iV_DrawImageFileAnisotropicTint(dropdownCaretImage.value().images, dropdownCaretImage.value().id, caretX0, caretY0, Vector2f{dropdownCaretImageSize.width(), dropdownCaretImageSize.height()}, color);
}
void DropdownWidget::drawOpenedHighlight(int xOffset, int yOffset)
{
int x0 = std::max<int>(0, (xOffset + x()) - 8);
int y0 = yOffset + y();
int w = width();
int h = height();
pie_UniTransBoxFill(x0, y0, x0 + w + 16, y0 + h, WZCOL_MENU_SCORE_BUILT);
}
void DropdownWidget::drawSelectedItem(const std::shared_ptr<WIDGET>& item, const WzRect& displayArea)
{
WidgetGraphicsContext context;
context = context.translatedBy(displayArea.x(), displayArea.y());
item->displayRecursive(context);
}
void DropdownWidget::addItem(const std::shared_ptr<WIDGET> &item)
{
std::weak_ptr<DropdownWidget> pWeakThis(std::dynamic_pointer_cast<DropdownWidget>(shared_from_this()));
size_t newItemIndex = items.size();
auto itemOnSelect = [newItemIndex, pWeakThis](std::shared_ptr<DropdownItemWrapper> selected) {
auto psStrongDropdown = pWeakThis.lock();
ASSERT_OR_RETURN(, psStrongDropdown != nullptr, "DropdownWidget no longer exists?");
if (!psStrongDropdown->overlayScreen) {
psStrongDropdown->open();
return;
}
if (psStrongDropdown->select(selected, newItemIndex))
{
psStrongDropdown->close();
}
};
auto wrapper = DropdownItemWrapper::make(std::dynamic_pointer_cast<DropdownWidget>(shared_from_this()), item, itemOnSelect);
switch (menuStyle)
{
case DropdownMenuStyle::InPlace:
wrapper->setGeometry(0, 0, std::max(getItemDisplayWidth(), 0), height());
break;
case DropdownMenuStyle::Separate:
wrapper->setGeometry(0, 0, std::max(item->width(), 0), item->height());
break;
}
items.push_back(wrapper);
itemsList->addItem(wrapper);
}
void DropdownWidget::clear()
{
items.clear();
itemsList->clear();
selectedItem.reset();
mouseOverItem.reset();
mouseDownItem.reset();
}
std::shared_ptr<WIDGET> DropdownWidget::findMouseTargetRecursive(W_CONTEXT *psContext, WIDGET_KEY key, bool wasPressed)
{
if (!overlayScreen && selectedItem && !isDisabled && key == WKEY_NONE) // only forward highlighting events
{
W_CONTEXT shiftedContext(psContext);
auto deltaX = x() - selectedItem->x();
auto deltaY = y() - selectedItem->y();
shiftedContext.mx = psContext->mx - deltaX;
shiftedContext.my = psContext->my - deltaY;
shiftedContext.xOffset = psContext->xOffset + deltaX;
shiftedContext.yOffset = psContext->yOffset + deltaY;
auto highlightedSelectedItemChildWidget = selectedItem->findMouseTargetRecursive(&shiftedContext, key, wasPressed);
if (highlightedSelectedItemChildWidget)
{
*psContext = shiftedContext; // bubble-up the matching hit's context
return highlightedSelectedItemChildWidget;
}
}
return WIDGET::findMouseTargetRecursive(psContext, key, wasPressed);
}
void DropdownWidget::setMouseClickOnItem(std::shared_ptr<DropdownItemWrapper> item, WIDGET_KEY key, bool wasPressed)
{
if (item == mouseDownItem) { return; }
if (mouseDownItem)
{
mouseDownItem->clearMouseDownState();
}
if (wasPressed)
{
mouseDownItem = item;
}
else
{
mouseDownItem.reset();
}
}
void DropdownWidget::setDropdownCaretImage(optional<AtlasImage> image, const WzSize& displaySize, const Padding& padding)
{
dropdownCaretImage = image;
dropdownCaretImageSize = WzSize(0, 0);
dropdownCaretImagePadding = Padding{};
if (dropdownCaretImage.has_value())
{
if (!dropdownCaretImage.value().images)
{
dropdownCaretImage.reset();
return;
}
if (dropdownCaretImage.value().id >= dropdownCaretImage.value().images->numImages())
{
dropdownCaretImage.reset();
return;
}
dropdownCaretImageSize = displaySize;
dropdownCaretImagePadding = padding;
}
}
void DropdownWidget::setDisabled(bool _isDisabled)
{
isDisabled = _isDisabled;
}
bool DropdownWidget::getIsDisabled() const
{
return isDisabled;
}
void DropdownWidget::setStyle(DropdownMenuStyle _menuStyle)
{
menuStyle = _menuStyle;
switch (menuStyle)
{
case DropdownMenuStyle::InPlace:
itemsList->setPadding(Padding{});
break;
case DropdownMenuStyle::Separate:
itemsList->setPadding(Padding{5,0,5,0});
break;
}
}
int32_t DropdownWidget::idealWidth()
{
int32_t result = itemsList->idealWidth();
result += getCaretImageUsedWidth();
return result;
}
int32_t DropdownWidget::idealHeight()
{
auto max = 0;
for (auto const &item: items)
{
max = std::max(max, item->idealHeight());
}
return max;
}
bool DropdownWidget::select(const std::shared_ptr<DropdownItemWrapper> &selected, size_t selectedIndex)
{
if (selectedItem == selected)
{
return true;
}
if (canChange)
{
if (!canChange(*this, selectedIndex, (selected) ? selected->getItem() : nullptr))
{
// abort change
return false;
}
}
if (selectedItem)
{
selectedItem->setSelected(false);
}
selectedItem = selected;
if (menuStyle == DropdownMenuStyle::InPlace)
{
selectedItem->setSelected(true);
}
onSelectedItemChanged();
if (onChange)
{
onChange(*this);
}
return true;
}
void DropdownWidget::onSelectedItemChanged()
{
// currently, no-op - intended for subclasses to override
}
|