From 7b137f718c6cbadcfdda9955377ad4a45ab8d56c Mon Sep 17 00:00:00 2001 From: Pedro Cruz Date: Fri, 16 Aug 2024 20:37:08 -0300 Subject: [PATCH] feat: item overlay order customization in browser field (#110) This commit implements a feature that enables the ability to customize the order of items except from ground in browse field. --- source/browse_tile_window.cpp | 132 ++++++++++++++++++++++++++-------- source/browse_tile_window.h | 25 ++++++- 2 files changed, 125 insertions(+), 32 deletions(-) diff --git a/source/browse_tile_window.cpp b/source/browse_tile_window.cpp index 1c3f23ae..59386ccd 100644 --- a/source/browse_tile_window.cpp +++ b/source/browse_tile_window.cpp @@ -141,6 +141,8 @@ EVT_BUTTON(wxID_FIND, BrowseTileWindow::OnClickSelectRaw) EVT_BUTTON(wxID_ABOUT, BrowseTileWindow::OnClickProperties) EVT_BUTTON(wxID_OK, BrowseTileWindow::OnClickOK) EVT_BUTTON(wxID_CANCEL, BrowseTileWindow::OnClickCancel) +EVT_BUTTON(wxID_UP, BrowseTileWindow::OnButtonUpClick) +EVT_BUTTON(wxID_DOWN, BrowseTileWindow::OnButtonDownClick) END_EVENT_TABLE() BrowseTileWindow::BrowseTileWindow(wxWindow* parent, Tile* tile, wxPoint position /* = wxDefaultPosition */) : @@ -149,39 +151,17 @@ BrowseTileWindow::BrowseTileWindow(wxWindow* parent, Tile* tile, wxPoint positio itemList = newd BrowseTileListBox(this, wxID_ANY, tile); sizer->Add(itemList, wxSizerFlags(1).Expand()); - const auto positionString = wxString::Format("x=%i, y=%i, z=%i", tile->getX(), tile->getY(), tile->getZ()); - const auto infoSizer = newd wxBoxSizer(wxVERTICAL); - const auto buttons = newd wxBoxSizer(wxHORIZONTAL); - - deleteButton = newd wxButton(this, wxID_REMOVE, "Delete"); - deleteButton->Enable(false); - buttons->Add(deleteButton); - - buttons->AddSpacer(5); - - selectRawButton = newd wxButton(this, wxID_FIND, "Select RAW"); - selectRawButton->Enable(false); - buttons->Add(selectRawButton); - - buttons->AddSpacer(5); - - propertiesButton = newd wxButton(this, wxID_ABOUT, "Properties"); - propertiesButton->Enable(false); - buttons->Add(propertiesButton); + AddTopOrderButtons(infoSizer); + infoSizer->AddSpacer(5); - infoSizer->Add(buttons); + AddActionButtons(infoSizer); infoSizer->AddSpacer(5); - infoSizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("Position: %s", positionString)), wxSizerFlags(0).Left()); - infoSizer->Add(itemCountText = newd wxStaticText(this, wxID_ANY, wxString::Format("Item count: %i", itemList->GetItemCount())), wxSizerFlags(0).Left()); - infoSizer->Add(newd wxStaticText(this, wxID_ANY, "Protection zone: " + b2yn(tile->isPZ())), wxSizerFlags(0).Left()); - infoSizer->Add(newd wxStaticText(this, wxID_ANY, "No PvP: " + b2yn(tile->getMapFlags() & TILESTATE_NOPVP)), wxSizerFlags(0).Left()); - infoSizer->Add(newd wxStaticText(this, wxID_ANY, "No logout: " + b2yn(tile->getMapFlags() & TILESTATE_NOLOGOUT)), wxSizerFlags(0).Left()); - infoSizer->Add(newd wxStaticText(this, wxID_ANY, "PvP zone: " + b2yn(tile->getMapFlags() & TILESTATE_PVPZONE)), wxSizerFlags(0).Left()); - infoSizer->Add(newd wxStaticText(this, wxID_ANY, "House: " + b2yn(tile->isHouseTile())), wxSizerFlags(0).Left()); - sizer->Add(infoSizer, wxSizerFlags(0).Left().DoubleBorder()); + AddInformations(infoSizer); + + sizer->Add(infoSizer, wxSizerFlags(0).Center().DoubleBorder()); // OK/Cancel buttons const auto btnSizer = newd wxBoxSizer(wxHORIZONTAL); @@ -200,11 +180,75 @@ BrowseTileWindow::~BrowseTileWindow() { itemList->Unbind(wxEVT_COMMAND_LISTBOX_SELECTED, &BrowseTileWindow::OnItemSelected, this); } -void BrowseTileWindow::OnItemSelected(wxCommandEvent &WXUNUSED(event)) { +void BrowseTileWindow::AddTopOrderButtons(wxSizer* sizer) { + const auto topOrderButtons = newd wxBoxSizer(wxHORIZONTAL); + + upButton = newd wxButton(this, wxID_UP, "Up /\\"); + upButton->Enable(false); + topOrderButtons->Add(upButton); + + topOrderButtons->AddSpacer(5); + + downButton = newd wxButton(this, wxID_DOWN, "Down \\/"); + downButton->Enable(false); + topOrderButtons->Add(downButton); + + sizer->Add(topOrderButtons, wxSizerFlags(0).Center()); +} + +void BrowseTileWindow::AddActionButtons(wxSizer* sizer) { + const auto actionButtons = newd wxBoxSizer(wxHORIZONTAL); + + deleteButton = newd wxButton(this, wxID_REMOVE, "Delete"); + deleteButton->Enable(false); + actionButtons->Add(deleteButton); + + actionButtons->AddSpacer(5); + + selectRawButton = newd wxButton(this, wxID_FIND, "Select RAW"); + selectRawButton->Enable(false); + actionButtons->Add(selectRawButton); + + actionButtons->AddSpacer(5); + + propertiesButton = newd wxButton(this, wxID_ABOUT, "Properties"); + propertiesButton->Enable(false); + + actionButtons->Add(propertiesButton); + + sizer->Add(actionButtons); +} + +void BrowseTileWindow::AddInformations(wxSizer* sizer) { + const auto tile = itemList->GetTile(); + const auto positionString = wxString::Format("x=%i, y=%i, z=%i", tile->getX(), tile->getY(), tile->getZ()); + + sizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("Position: %s", positionString)), wxSizerFlags(0).Left()); + sizer->Add(itemCountText = newd wxStaticText(this, wxID_ANY, wxString::Format("Item count: %i", itemList->GetItemCount())), wxSizerFlags(0).Left()); + sizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("Protection zone: %s", b2yn(tile->isPZ()))), wxSizerFlags(0).Left()); + sizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("No PvP: %s", b2yn(tile->getMapFlags() & TILESTATE_NOPVP))), wxSizerFlags(0).Left()); + sizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("No logout: %s", b2yn(tile->getMapFlags() & TILESTATE_NOLOGOUT))), wxSizerFlags(0).Left()); + sizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("PvP zone: %s", b2yn(tile->getMapFlags() & TILESTATE_PVPZONE))), wxSizerFlags(0).Left()); + sizer->Add(newd wxStaticText(this, wxID_ANY, wxString::Format("House: %s", b2yn(tile->isHouseTile()))), wxSizerFlags(0).Left()); +} + +void BrowseTileWindow::UpdateButtons(int selection) { const auto count = itemList->GetSelectedCount(); + const auto tile = itemList->GetTile(); + const auto itemsAmount = itemList->GetItemCount(); + const auto items = itemList->GetItems(); + const auto nextItemIsGround = itemsAmount - 1 == selection ? false : items.at(selection + 1) == tile->ground; + deleteButton->Enable(count != 0); selectRawButton->Enable(count == 1); - propertiesButton->Enable(count != 0); + propertiesButton->Enable(count == 1); + upButton->Enable(count == 1 && selection != 0 && items.at(selection) != tile->ground); + downButton->Enable(count == 1 && selection != itemsAmount - 1 && !nextItemIsGround); +} + +void BrowseTileWindow::OnItemSelected(wxCommandEvent &evt) { + UpdateButtons(evt.GetSelection()); + const auto count = itemList->GetSelectedCount(); } void BrowseTileWindow::OnClickDelete(wxCommandEvent &WXUNUSED(event)) { @@ -237,3 +281,31 @@ void BrowseTileWindow::OnClickOK(wxCommandEvent &WXUNUSED(event)) { void BrowseTileWindow::OnClickCancel(wxCommandEvent &WXUNUSED(event)) { EndModal(0); } + +void BrowseTileWindow::ChangeItemIndex(bool up /* = true*/) { + const auto i = up ? 1 : -1; + const auto tile = itemList->GetTile(); + + const auto selectedItemIndex = itemList->GetSelection(); + const auto tileItemsSize = tile->items.size() - 1; + auto index = tileItemsSize - selectedItemIndex; + + const auto tmpItem = tile->items[index]; + tile->items[index] = tile->items[index + i]; + tile->items[index + i] = tmpItem; + + itemList->UpdateItems(); + + itemList->DeselectAll(); + itemList->SetSelection(selectedItemIndex - i); + UpdateButtons(selectedItemIndex - i); + Refresh(); +} + +void BrowseTileWindow::OnButtonUpClick(wxCommandEvent &evt) { + ChangeItemIndex(); +} + +void BrowseTileWindow::OnButtonDownClick(wxCommandEvent &evt) { + ChangeItemIndex(false); +} diff --git a/source/browse_tile_window.h b/source/browse_tile_window.h index 1c9508b8..61403506 100644 --- a/source/browse_tile_window.h +++ b/source/browse_tile_window.h @@ -22,6 +22,8 @@ #include "map.h" #include "tile.h" +using ItemsMap = std::map; + class BrowseTileListBox : public wxVListBox { public: BrowseTileListBox(wxWindow* parent, wxWindowID id, Tile* tile); @@ -33,12 +35,19 @@ class BrowseTileListBox : public wxVListBox { void RemoveSelected(); void OnItemDoubleClick(wxCommandEvent &); + Tile* GetTile() const noexcept { + return editTile; + } + + ItemsMap GetItems() const noexcept { + return items; + } + void OpenPropertiesWindow(int index); -protected: void UpdateItems(); - using ItemsMap = std::map; +protected: ItemsMap items; Tile* editTile = nullptr; @@ -56,13 +65,25 @@ class BrowseTileWindow : public wxDialog { void OnClickProperties(wxCommandEvent &); void OnClickOK(wxCommandEvent &); void OnClickCancel(wxCommandEvent &); + void OnButtonUpClick(wxCommandEvent &); + void OnButtonDownClick(wxCommandEvent &); + + void ChangeItemIndex(bool up = true); + void UpdateButtons(int selection); protected: + void AddTopOrderButtons(wxSizer* sizer); + void AddActionButtons(wxSizer* sizer); + void AddInformations(wxSizer* sizer); + + friend class BrowseTileListBox; BrowseTileListBox* itemList = nullptr; wxStaticText* itemCountText = nullptr; wxButton* deleteButton = nullptr; wxButton* selectRawButton = nullptr; wxButton* propertiesButton = nullptr; + wxButton* upButton = nullptr; + wxButton* downButton = nullptr; DECLARE_EVENT_TABLE(); };