1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Merge pull request #3583 from Laserlicht/town_portal

town portal dialog adjustments
This commit is contained in:
Ivan Savenko 2024-02-01 11:30:51 +02:00 committed by GitHub
commit facdc9f993
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 78 additions and 14 deletions

View File

@ -43,6 +43,7 @@
#include "render/CAnimation.h"
#include "render/IImage.h"
#include "render/IRenderHandler.h"
#include "widgets/Buttons.h"
#include "widgets/CComponent.h"
@ -1068,6 +1069,19 @@ void CPlayerInterface::showMapObjectSelectDialog(QueryID askID, const Component
{
EVENT_HANDLER_CALLED_BY_CLIENT;
std::vector<ObjectInstanceID> tmpObjects;
if(objects.size() && dynamic_cast<const CGTownInstance *>(cb->getObj(objects[0])))
{
// sorting towns (like in client)
std::vector <const CGTownInstance*> Towns = LOCPLINT->localState->getOwnedTowns();
for(auto town : Towns)
for(auto item : objects)
if(town == cb->getObj(item))
tmpObjects.push_back(item);
}
else // other object list than town
tmpObjects = objects;
auto selectCallback = [=](int selection)
{
cb->sendQueryReply(selection, askID);
@ -1082,9 +1096,9 @@ void CPlayerInterface::showMapObjectSelectDialog(QueryID askID, const Component
const std::string localDescription = description.toString();
std::vector<int> tempList;
tempList.reserve(objects.size());
tempList.reserve(tmpObjects.size());
for(auto item : objects)
for(auto item : tmpObjects)
tempList.push_back(item.getNum());
CComponent localIconC(icon);
@ -1092,8 +1106,24 @@ void CPlayerInterface::showMapObjectSelectDialog(QueryID askID, const Component
std::shared_ptr<CIntObject> localIcon = localIconC.image;
localIconC.removeChild(localIcon.get(), false);
auto wnd = std::make_shared<CObjectListWindow>(tempList, localIcon, localTitle, localDescription, selectCallback);
std::vector<std::shared_ptr<IImage>> images;
for(auto & obj : tmpObjects)
{
if(!settings["general"]["enableUiEnhancements"].Bool())
break;
const CGTownInstance * t = dynamic_cast<const CGTownInstance *>(cb->getObj(obj));
if(t)
{
std::shared_ptr<CAnimation> a = GH.renderHandler().loadAnimation(AnimationPath::builtin("ITPA"));
a->preload();
images.push_back(a->getImage(t->town->clientInfo.icons[t->hasFort()][false] + 2)->scaleFast(Point(35, 23)));
}
}
auto wnd = std::make_shared<CObjectListWindow>(tempList, localIcon, localTitle, localDescription, selectCallback, 0, images);
wnd->onExit = cancelCallback;
wnd->onPopup = [this, tmpObjects](int index) { CRClickPopup::createAndPush(cb->getObj(tmpObjects[index]), GH.getCursorPosition()); };
wnd->onClicked = [this, tmpObjects](int index) { adventureInt->centerOnObject(cb->getObj(tmpObjects[index])); GH.windows().totalRedraw(); };
GH.windows().pushWindow(wnd);
}

View File

@ -34,6 +34,7 @@
#include "../render/Canvas.h"
#include "../render/IImage.h"
#include "../render/IRenderHandler.h"
#include "../render/CAnimation.h"
#include "../render/ColorFilter.h"
#include "../adventureMap/AdventureMapInterface.h"
#include "../adventureMap/CList.h"
@ -872,7 +873,7 @@ void CCastleBuildings::enterCastleGate()
return;//only visiting hero can use castle gates
}
std::vector <int> availableTowns;
std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
std::vector <const CGTownInstance*> Towns = LOCPLINT->localState->getOwnedTowns();
for(auto & Town : Towns)
{
const CGTownInstance *t = Town;
@ -883,9 +884,22 @@ void CCastleBuildings::enterCastleGate()
availableTowns.push_back(t->id.getNum());//add to the list
}
}
std::vector<std::shared_ptr<IImage>> images;
for(auto & t : Towns)
{
if(!settings["general"]["enableUiEnhancements"].Bool())
break;
std::shared_ptr<CAnimation> a = GH.renderHandler().loadAnimation(AnimationPath::builtin("ITPA"));
a->preload();
images.push_back(a->getImage(t->town->clientInfo.icons[t->hasFort()][false] + 2)->scaleFast(Point(35, 23)));
}
auto gateIcon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, BuildingID::CASTLE_GATE);//will be deleted by selection window
GH.windows().createAndPushWindow<CObjectListWindow>(availableTowns, gateIcon, CGI->generaltexth->jktexts[40],
CGI->generaltexth->jktexts[41], std::bind (&CCastleInterface::castleTeleport, LOCPLINT->castleInt, _1));
auto wnd = std::make_shared<CObjectListWindow>(availableTowns, gateIcon, CGI->generaltexth->jktexts[40],
CGI->generaltexth->jktexts[41], std::bind (&CCastleInterface::castleTeleport, LOCPLINT->castleInt, _1), 0, images);
wnd->onPopup = [Towns](int index) { CRClickPopup::createAndPush(Towns[index], GH.getCursorPosition()); };
GH.windows().pushWindow(wnd);
}
void CCastleBuildings::enterDwelling(int level)

View File

@ -39,6 +39,7 @@
#include "../render/Canvas.h"
#include "../render/CAnimation.h"
#include "../render/IRenderHandler.h"
#include "../render/IImage.h"
#include "../../CCallback.h"
@ -1674,11 +1675,13 @@ CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
}
CObjectListWindow::CItem::CItem(CObjectListWindow * _parent, size_t _id, std::string _text)
: CIntObject(LCLICK | DOUBLECLICK),
: CIntObject(LCLICK | DOUBLECLICK | RCLICK_POPUP),
parent(_parent),
index(_id)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
if(parent->images.size() > index)
icon = std::make_shared<CPicture>(parent->images[index], Point(1, 1));
border = std::make_shared<CPicture>(ImagePath::builtin("TPGATES"));
pos = border->pos;
@ -1701,6 +1704,9 @@ void CObjectListWindow::CItem::select(bool on)
void CObjectListWindow::CItem::clickPressed(const Point & cursorPosition)
{
parent->changeSelection(index);
if(parent->onClicked)
parent->onClicked(index);
}
void CObjectListWindow::CItem::clickDouble(const Point & cursorPosition)
@ -1708,10 +1714,17 @@ void CObjectListWindow::CItem::clickDouble(const Point & cursorPosition)
parent->elementSelected();
}
CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection)
void CObjectListWindow::CItem::showPopupWindow(const Point & cursorPosition)
{
if(parent->onPopup)
parent->onPopup(index);
}
CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection, std::vector<std::shared_ptr<IImage>> images)
: CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
onSelect(Callback),
selected(initialSelection)
selected(initialSelection),
images(images)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
items.reserve(_items.size());
@ -1724,10 +1737,11 @@ CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::share
init(titleWidget_, _title, _descr);
}
CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection)
CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection, std::vector<std::shared_ptr<IImage>> images)
: CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
onSelect(Callback),
selected(initialSelection)
selected(initialSelection),
images(images)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
items.reserve(_items.size());
@ -1805,7 +1819,7 @@ void CObjectListWindow::changeSelection(size_t which)
selected = which;
}
void CObjectListWindow::keyPressed (EShortcut key)
void CObjectListWindow::keyPressed(EShortcut key)
{
int sel = static_cast<int>(selected);

View File

@ -38,6 +38,7 @@ class CGarrisonSlot;
class CHeroArea;
class CAnimImage;
class CFilledTexture;
class IImage;
enum class EUserEvent;
@ -157,6 +158,7 @@ class CObjectListWindow : public CWindowObject
CObjectListWindow * parent;
std::shared_ptr<CLabel> text;
std::shared_ptr<CPicture> border;
std::shared_ptr<CPicture> icon;
public:
const size_t index;
CItem(CObjectListWindow * parent, size_t id, std::string text);
@ -164,12 +166,14 @@ class CObjectListWindow : public CWindowObject
void select(bool on);
void clickPressed(const Point & cursorPosition) override;
void clickDouble(const Point & cursorPosition) override;
void showPopupWindow(const Point & cursorPosition) override;
};
std::function<void(int)> onSelect;//called when OK button is pressed, returns id of selected item.
std::shared_ptr<CIntObject> titleWidget;
std::shared_ptr<CLabel> title;
std::shared_ptr<CLabel> descr;
std::vector<std::shared_ptr<IImage>> images;
std::shared_ptr<CListBox> list;
std::shared_ptr<CButton> ok;
@ -183,12 +187,14 @@ public:
size_t selected;//index of currently selected item
std::function<void()> onExit;//optional exit callback
std::function<void(int)> onPopup;//optional popup callback
std::function<void(int)> onClicked;//optional if clicked on item callback
/// Callback will be called when OK button is pressed, returns id of selected item. initState = initially selected item
/// Image can be nullptr
///item names will be taken from map objects
CObjectListWindow(const std::vector<int> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0);
CObjectListWindow(const std::vector<std::string> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0);
CObjectListWindow(const std::vector<int> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {});
CObjectListWindow(const std::vector<std::string> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {});
std::shared_ptr<CIntObject> genItem(size_t index);
void elementSelected();//call callback and close this window