1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-23 00:28:08 +02:00

sort town feature

This commit is contained in:
Laserlicht
2023-10-15 14:31:33 +02:00
committed by GitHub
parent e052a96637
commit 3b853bff08
11 changed files with 62 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -42,6 +42,9 @@
"vcmi.radialWheel.moveUnit" : "Move creatures to another army", "vcmi.radialWheel.moveUnit" : "Move creatures to another army",
"vcmi.radialWheel.splitUnit" : "Split creature to another slot", "vcmi.radialWheel.splitUnit" : "Split creature to another slot",
"vcmi.radialWheel.townUp" : "Move town up",
"vcmi.radialWheel.townDown" : "Move town down",
"vcmi.mainMenu.serverConnecting" : "Connecting...", "vcmi.mainMenu.serverConnecting" : "Connecting...",
"vcmi.mainMenu.serverAddressEnter" : "Enter address:", "vcmi.mainMenu.serverAddressEnter" : "Enter address:",
"vcmi.mainMenu.serverConnectionFailed" : "Failed to connect", "vcmi.mainMenu.serverConnectionFailed" : "Failed to connect",

View File

@ -269,3 +269,9 @@ void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
if (currentSelection == nullptr && !ownedTowns.empty()) if (currentSelection == nullptr && !ownedTowns.empty())
setSelection(ownedTowns.front()); setSelection(ownedTowns.front());
} }
void PlayerLocalState::swapOwnedTowns(int pos1, int pos2)
{
assert(ownedTowns[pos1] && ownedTowns[pos2]);
std::swap(ownedTowns[pos1], ownedTowns[pos2]);
}

View File

@ -66,6 +66,7 @@ public:
const CGTownInstance * getOwnedTown(size_t index); const CGTownInstance * getOwnedTown(size_t index);
void addOwnedTown(const CGTownInstance * hero); void addOwnedTown(const CGTownInstance * hero);
void removeOwnedTown(const CGTownInstance * hero); void removeOwnedTown(const CGTownInstance * hero);
void swapOwnedTowns(int pos1, int pos2);
const std::vector<const CGHeroInstance *> & getWanderingHeroes(); const std::vector<const CGHeroInstance *> & getWanderingHeroes();
const CGHeroInstance * getWanderingHero(size_t index); const CGHeroInstance * getWanderingHero(size_t index);

View File

@ -16,11 +16,13 @@
#include "../widgets/Images.h" #include "../widgets/Images.h"
#include "../widgets/Buttons.h" #include "../widgets/Buttons.h"
#include "../widgets/ObjectLists.h" #include "../widgets/ObjectLists.h"
#include "../widgets/RadialMenu.h"
#include "../windows/InfoWindows.h" #include "../windows/InfoWindows.h"
#include "../CGameInfo.h" #include "../CGameInfo.h"
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../PlayerLocalState.h" #include "../PlayerLocalState.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../render/Canvas.h" #include "../render/Canvas.h"
#include "../render/Colors.h" #include "../render/Colors.h"
@ -324,12 +326,15 @@ std::shared_ptr<CIntObject> CTownList::createItem(size_t index)
CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town): CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
CListItem(parent), CListItem(parent),
parentList(parent),
town(Town) town(Town)
{ {
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0); picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), 0);
pos = picture->pos; pos = picture->pos;
update(); update();
addUsedEvents(GESTURE);
} }
std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection() std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
@ -361,6 +366,31 @@ void CTownList::CTownItem::showTooltip()
CRClickPopup::createAndPush(town, GH.getCursorPosition()); CRClickPopup::createAndPush(town, GH.getCursorPosition());
} }
void CTownList::CTownItem::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
{
if(!on)
return;
if(!town)
return;
const std::vector<const CGTownInstance *> towns = LOCPLINT->localState->getOwnedTowns();
if(towns.size() < 2)
return;
int listPos = std::distance(towns.begin(), std::find(towns.begin(), towns.end(), town));
int townUpperPos = (listPos < 1) ? -1 : listPos - 1;
int townLowerPos = (listPos > towns.size() - 2) ? -1 : listPos + 1;
std::vector<RadialMenuConfig> menuElements = {
{ RadialMenuConfig::ITEM_ALT_NW, townUpperPos > -1, "altUp", "vcmi.radialWheel.townUp", [this, listPos, townUpperPos](){LOCPLINT->localState->swapOwnedTowns(listPos, townUpperPos); parentList->updateWidget(); } },
{ RadialMenuConfig::ITEM_ALT_SW, townLowerPos > -1, "altDown", "vcmi.radialWheel.townDown", [this, listPos, townLowerPos](){ LOCPLINT->localState->swapOwnedTowns(listPos, townLowerPos); parentList->updateWidget(); } },
};
GH.windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements, true);
}
std::string CTownList::CTownItem::getHoverText() std::string CTownList::CTownItem::getHoverText()
{ {
return town->getObjectName(); return town->getObjectName();

View File

@ -150,6 +150,7 @@ class CTownList : public CList
class CTownItem : public CListItem class CTownItem : public CListItem
{ {
std::shared_ptr<CAnimImage> picture; std::shared_ptr<CAnimImage> picture;
CTownList *parentList;
public: public:
const CGTownInstance * const town; const CGTownInstance * const town;
@ -160,6 +161,7 @@ class CTownList : public CList
void select(bool on) override; void select(bool on) override;
void open() override; void open() override;
void showTooltip() override; void showTooltip() override;
void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
std::string getHoverText() override; std::string getHoverText() override;
}; };

View File

@ -21,14 +21,14 @@
#include "../../lib/CGeneralTextHandler.h" #include "../../lib/CGeneralTextHandler.h"
RadialMenuItem::RadialMenuItem(const std::string & imageName, const std::string & hoverText, const std::function<void()> & callback) RadialMenuItem::RadialMenuItem(const std::string & imageName, const std::string & hoverText, const std::function<void()> & callback, bool altLayout)
: callback(callback) : callback(callback)
, hoverText(hoverText) , hoverText(hoverText)
{ {
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
inactiveImage = std::make_shared<CPicture>(ImagePath::builtin("radialMenu/itemInactive"), Point(0, 0)); inactiveImage = std::make_shared<CPicture>(ImagePath::builtin(altLayout ? "radialMenu/itemInactiveAlt" : "radialMenu/itemInactive"), Point(0, 0));
selectedImage = std::make_shared<CPicture>(ImagePath::builtin("radialMenu/itemEmpty"), Point(0, 0)); selectedImage = std::make_shared<CPicture>(ImagePath::builtin(altLayout ? "radialMenu/itemEmptyAlt" : "radialMenu/itemEmpty"), Point(0, 0));
iconImage = std::make_shared<CPicture>(ImagePath::builtin("radialMenu/" + imageName), Point(0, 0)); iconImage = std::make_shared<CPicture>(ImagePath::builtin("radialMenu/" + imageName), Point(0, 0));
@ -42,13 +42,13 @@ void RadialMenuItem::setSelected(bool selected)
inactiveImage->setEnabled(!selected); inactiveImage->setEnabled(!selected);
} }
RadialMenu::RadialMenu(const Point & positionToCenter, const std::vector<RadialMenuConfig> & menuConfig): RadialMenu::RadialMenu(const Point & positionToCenter, const std::vector<RadialMenuConfig> & menuConfig, bool altLayout):
centerPosition(positionToCenter) centerPosition(positionToCenter), altLayout(altLayout)
{ {
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos += positionToCenter; pos += positionToCenter;
Point itemSize = Point(70, 80); Point itemSize = altLayout ? Point(80, 70) : Point(70, 80);
moveBy(-itemSize / 2); moveBy(-itemSize / 2);
pos.w = itemSize.x; pos.w = itemSize.x;
pos.h = itemSize.y; pos.h = itemSize.y;
@ -60,6 +60,7 @@ RadialMenu::RadialMenu(const Point & positionToCenter, const std::vector<RadialM
for(const auto & item : items) for(const auto & item : items)
pos = pos.include(item->pos); pos = pos.include(item->pos);
pos = pos.include(statusBar->pos);
fitToScreen(10); fitToScreen(10);
@ -71,7 +72,7 @@ void RadialMenu::addItem(const Point & offset, bool enabled, const std::string &
if (!enabled) if (!enabled)
return; return;
auto item = std::make_shared<RadialMenuItem>(path, CGI->generaltexth->translate(hoverText), callback); auto item = std::make_shared<RadialMenuItem>(path, CGI->generaltexth->translate(hoverText), callback, altLayout);
item->moveBy(offset); item->moveBy(offset);

View File

@ -26,6 +26,13 @@ struct RadialMenuConfig
static constexpr Point ITEM_SW = Point(-40, +70); static constexpr Point ITEM_SW = Point(-40, +70);
static constexpr Point ITEM_SE = Point(+40, +70); static constexpr Point ITEM_SE = Point(+40, +70);
static constexpr Point ITEM_ALT_NN = Point(0, -80);
static constexpr Point ITEM_ALT_SS = Point(0, +80);
static constexpr Point ITEM_ALT_NW = Point(-70, -40);
static constexpr Point ITEM_ALT_SW = Point(-70, +40);
static constexpr Point ITEM_ALT_NE = Point(+70, -40);
static constexpr Point ITEM_ALT_SE = Point(+70, +40);
Point itemPosition; Point itemPosition;
bool enabled; bool enabled;
std::string imageName; std::string imageName;
@ -44,7 +51,7 @@ class RadialMenuItem : public CIntObject
std::string hoverText; std::string hoverText;
public: public:
RadialMenuItem(const std::string & imageName, const std::string & hoverText, const std::function<void()> & callback); RadialMenuItem(const std::string & imageName, const std::string & hoverText, const std::function<void()> & callback, bool altLayout);
void setSelected(bool selected); void setSelected(bool selected);
}; };
@ -60,8 +67,10 @@ class RadialMenu : public CIntObject
void addItem(const Point & offset, bool enabled, const std::string & path, const std::string & hoverText, const std::function<void()> & callback); void addItem(const Point & offset, bool enabled, const std::string & path, const std::string & hoverText, const std::function<void()> & callback);
std::shared_ptr<RadialMenuItem> findNearestItem(const Point & cursorPosition) const; std::shared_ptr<RadialMenuItem> findNearestItem(const Point & cursorPosition) const;
bool altLayout;
public: public:
RadialMenu(const Point & positionToCenter, const std::vector<RadialMenuConfig> & menuConfig); RadialMenu(const Point & positionToCenter, const std::vector<RadialMenuConfig> & menuConfig, bool altLayout = false);
void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override; void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override; void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;