Skip to content

Commit

Permalink
new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lamonato29 committed Aug 24, 2024
1 parent b69d445 commit 9b55af1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/menubar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<item name="Find Item on Selection" action="SEARCH_ON_SELECTION_ITEM" help="Find items on selected area."/>
<item name="Remove Item on Selection" action="REMOVE_ON_SELECTION_ITEM" help="Remove item on selected area."/>
<item name="Remove Monsters on Selection" action="REMOVE_ON_SELECTION_MONSTER" help="Remove monsters on selected area."/>
<item name="Count Monsters on Selection" action="COUNT_ON_SELECTION_MONSTER" help="Count monsters on selected area."/>
<item name="Remove Duplicated Items on Selection" action="REMOVE_ON_SELECTION_DUPLICATED_ITEMS" help="Removes all items duplicated selected area."/>
<separator/>
<menu name="$Find on Selection">
Expand Down
22 changes: 22 additions & 0 deletions source/main_menubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ MainMenuBar::MainMenuBar(MainFrame* frame) :
MAKE_ACTION(REPLACE_ON_SELECTION_ITEMS, wxITEM_NORMAL, OnReplaceItemsOnSelection);
MAKE_ACTION(REMOVE_ON_SELECTION_ITEM, wxITEM_NORMAL, OnRemoveItemOnSelection);
MAKE_ACTION(REMOVE_ON_SELECTION_MONSTER, wxITEM_NORMAL, OnRemoveMonstersOnSelection);
MAKE_ACTION(COUNT_ON_SELECTION_MONSTER, wxITEM_NORMAL, OnCountMonstersOnSelection);
MAKE_ACTION(SELECT_MODE_COMPENSATE, wxITEM_RADIO, OnSelectionTypeChange);
MAKE_ACTION(SELECT_MODE_LOWER, wxITEM_RADIO, OnSelectionTypeChange);
MAKE_ACTION(SELECT_MODE_CURRENT, wxITEM_RADIO, OnSelectionTypeChange);
Expand Down Expand Up @@ -363,6 +364,7 @@ void MainMenuBar::Update() {
EnableItem(REPLACE_ON_SELECTION_ITEMS, has_selection && is_host);
EnableItem(REMOVE_ON_SELECTION_ITEM, has_selection && is_host);
EnableItem(REMOVE_ON_SELECTION_MONSTER, has_selection && is_host);
EnableItem(COUNT_ON_SELECTION_MONSTER, has_selection && is_host);

EnableItem(CUT, has_map);
EnableItem(COPY, has_map);
Expand Down Expand Up @@ -1228,6 +1230,26 @@ void MainMenuBar::OnRemoveMonstersOnSelection(wxCommandEvent &WXUNUSED(event)) {
g_gui.RefreshView();
}

void MainMenuBar::OnCountMonstersOnSelection(wxCommandEvent &WXUNUSED(event)) {
if (!g_gui.IsEditorOpen()) {
return;
}

g_gui.CreateLoadBar("Counting monsters on selection...");
const auto result = CountMonstersOnMap(g_gui.GetCurrentMap(), true);
g_gui.DestroyLoadBar();

int64_t totalMonsters = result.first;
const std::unordered_map<std::string, int64_t> &monsterCounts = result.second;

wxString message = wxString::Format("There are %d monsters in total.\n\n", totalMonsters);
for (const auto &pair : monsterCounts) {
message += wxString::Format("%s: %d\n", pair.first, pair.second);
}

g_gui.PopupDialog("Count Monsters", message, wxOK);
}

void MainMenuBar::OnSelectionTypeChange(wxCommandEvent &WXUNUSED(event)) {
g_settings.setInteger(Config::COMPENSATED_SELECT, IsItemChecked(MenuBar::SELECT_MODE_COMPENSATE));

Expand Down
2 changes: 2 additions & 0 deletions source/main_menubar.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace MenuBar {
REPLACE_ON_SELECTION_ITEMS,
REMOVE_ON_SELECTION_ITEM,
REMOVE_ON_SELECTION_MONSTER,
COUNT_ON_SELECTION_MONSTER,
SELECT_MODE_COMPENSATE,
SELECT_MODE_CURRENT,
SELECT_MODE_LOWER,
Expand Down Expand Up @@ -257,6 +258,7 @@ class MainMenuBar : public wxEvtHandler {
void OnReplaceItemsOnSelection(wxCommandEvent &event);
void OnRemoveItemOnSelection(wxCommandEvent &event);
void OnRemoveMonstersOnSelection(wxCommandEvent &event);
void OnCountMonstersOnSelection(wxCommandEvent &event);

// Map menu
void OnMapEditTowns(wxCommandEvent &event);
Expand Down
27 changes: 27 additions & 0 deletions source/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,30 @@ int64_t RemoveMonstersOnMap(Map &map, bool selectedOnly) {
}
return removed;
}

std::pair<int64_t, std::unordered_map<std::string, int64_t>> CountMonstersOnMap(Map &map, bool selectedOnly) {
int64_t done = 0;
int64_t total = 0;
std::unordered_map<std::string, int64_t> monsterCount;

MapIterator it = map.begin();
MapIterator end = map.end();

while (it != end) {
++done;
Tile* tile = (*it)->get();
if (selectedOnly && !tile->isSelected()) {
++it;
continue;
}
if (tile->monster) {
++total;
std::string monsterName = tile->monster->getName();
++monsterCount[monsterName];
}

++it;
}

return std::make_pair(total, monsterCount);
}
1 change: 1 addition & 0 deletions source/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ inline int64_t RemoveItemOnMap(Map &map, RemoveIfType &condition, bool selectedO
}

int64_t RemoveMonstersOnMap(Map &map, bool selectedOnly);
std::pair<int64_t, std::unordered_map<std::string, int64_t>> CountMonstersOnMap(Map &map, bool selectedOnly);

template <typename RemoveIfType>
inline int64_t RemoveItemDuplicateOnMap(Map &map, RemoveIfType &condition, bool selectedOnly) {
Expand Down

0 comments on commit 9b55af1

Please sign in to comment.