Skip to content

Commit

Permalink
Merge branch 'main' into fix_items_load_fluid
Browse files Browse the repository at this point in the history
  • Loading branch information
majestyotbr authored Aug 27, 2024
2 parents ebee8f7 + 7dd98f2 commit d158362
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 8 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
53 changes: 46 additions & 7 deletions source/palette_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ EVT_TOGGLEBUTTON(PALETTE_SPAWN_MONSTER_BRUSH_BUTTON, MonsterPalettePanel::OnClic

EVT_SPINCTRL(PALETTE_MONSTER_SPAWN_TIME, MonsterPalettePanel::OnChangeSpawnMonsterTime)
EVT_SPINCTRL(PALETTE_MONSTER_SPAWN_SIZE, MonsterPalettePanel::OnChangeSpawnMonsterSize)

EVT_TEXT_ENTER(PALETTE_MONSTER_SEARCH, MonsterPalettePanel::OnChangeMonsterName)
END_EVENT_TABLE()

MonsterPalettePanel::MonsterPalettePanel(wxWindow* parent, wxWindowID id) :
Expand All @@ -52,8 +50,18 @@ MonsterPalettePanel::MonsterPalettePanel(wxWindow* parent, wxWindowID id) :
tileset_choice = newd wxChoice(this, PALETTE_MONSTER_TILESET_CHOICE, wxDefaultPosition, wxDefaultSize, (int)0, (const wxString*)nullptr);
sidesizer->Add(tileset_choice, 0, wxEXPAND);

monster_name_text = newd wxTextCtrl(this, PALETTE_MONSTER_SEARCH, "Name");
sidesizer->Add(monster_name_text, 0, wxEXPAND);
wxSizer* monsterNameSizer = newd wxStaticBoxSizer(wxHORIZONTAL, this);
monster_name_text = newd wxTextCtrl(this, PALETTE_MONSTER_SEARCH, "Search name", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
monster_name_text->Bind(wxEVT_SET_FOCUS, &MonsterPalettePanel::OnSetFocus, this);
monster_name_text->Bind(wxEVT_KILL_FOCUS, &MonsterPalettePanel::OnKillFocus, this);
monster_name_text->Bind(wxEVT_TEXT_ENTER, &MonsterPalettePanel::OnChangeMonsterNameSearch, this);

monster_search_button = newd wxButton(this, wxID_ANY, "Search");
monster_search_button->Bind(wxEVT_BUTTON, &MonsterPalettePanel::OnChangeMonsterNameSearch, this);

monsterNameSizer->Add(monster_name_text, 2, wxEXPAND);
monsterNameSizer->Add(monster_search_button, 1, wxEXPAND);
sidesizer->Add(monsterNameSizer);

monster_list = newd SortableListBox(this, PALETTE_MONSTER_LISTBOX);
sidesizer->Add(monster_list, 1, wxEXPAND);
Expand Down Expand Up @@ -90,7 +98,9 @@ MonsterPalettePanel::MonsterPalettePanel(wxWindow* parent, wxWindowID id) :
}

MonsterPalettePanel::~MonsterPalettePanel() {
////
monster_name_text->Unbind(wxEVT_SET_FOCUS, &MonsterPalettePanel::OnSetFocus, this);
monster_name_text->Unbind(wxEVT_KILL_FOCUS, &MonsterPalettePanel::OnKillFocus, this);
monster_name_text->Unbind(wxEVT_TEXT_ENTER, &MonsterPalettePanel::OnChangeMonsterNameSearch, this);
}

PaletteType MonsterPalettePanel::GetType() const {
Expand Down Expand Up @@ -208,7 +218,6 @@ void MonsterPalettePanel::SelectTileset(size_t index) {
monster_brush_button->Enable(false);
} else {
const TilesetCategory* tsc = reinterpret_cast<const TilesetCategory*>(tileset_choice->GetClientData(index));
// Select first house
for (BrushVector::const_iterator iter = tsc->brushlist.begin();
iter != tsc->brushlist.end();
++iter) {
Expand Down Expand Up @@ -297,5 +306,35 @@ void MonsterPalettePanel::OnChangeSpawnMonsterSize(wxSpinEvent &event) {
}
}

void MonsterPalettePanel::OnChangeMonsterName(wxCommandEvent &event) {
void MonsterPalettePanel::OnSetFocus(wxFocusEvent &event) {
g_gui.DisableHotkeys();
event.Skip();
}

void MonsterPalettePanel::OnKillFocus(wxFocusEvent &event) {
g_gui.EnableHotkeys();
event.Skip();
}

void MonsterPalettePanel::OnChangeMonsterNameSearch(wxCommandEvent &event) {
const auto monsterNameSearch = as_lower_str(monster_name_text->GetValue().ToStdString());

const auto index = tileset_choice->GetSelection();

if (monsterNameSearch.empty()) {
SelectTileset(index);
return;
}

monster_list->Clear();
const auto tilesetCategory = reinterpret_cast<const TilesetCategory*>(tileset_choice->GetClientData(index));
for (auto it = tilesetCategory->brushlist.begin(); it != tilesetCategory->brushlist.end(); ++it) {
const auto monsterName = wxstr((*it)->getName());
const auto regexPattern = std::regex(monsterNameSearch);
if (std::regex_search(as_lower_str(monsterName.ToStdString()), regexPattern)) {
monster_list->Append(monsterName, *it);
}
}
monster_list->Sort();
Update();
}
5 changes: 4 additions & 1 deletion source/palette_monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ class MonsterPalettePanel : public PalettePanel {
void OnListBoxChange(wxCommandEvent &event);
void OnClickMonsterBrushButton(wxCommandEvent &event);
void OnClickSpawnMonsterBrushButton(wxCommandEvent &event);
void OnChangeMonsterName(wxCommandEvent &event);
void OnSetFocus(wxFocusEvent &event);
void OnKillFocus(wxFocusEvent &event);
void OnChangeMonsterNameSearch(wxCommandEvent &event);

protected:
void SelectMonsterBrush();
void SelectSpawnBrush();

wxChoice* tileset_choice;
wxTextCtrl* monster_name_text;
wxButton* monster_search_button;
SortableListBox* monster_list;
wxToggleButton* monster_brush_button;
wxToggleButton* spawn_monster_brush_button;
Expand Down

0 comments on commit d158362

Please sign in to comment.