Skip to content

Commit

Permalink
fix: select brush in page on jump to and searches
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Sep 13, 2024
1 parent 2d63ae7 commit 7ce2b9f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
52 changes: 48 additions & 4 deletions source/palette_brushlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ BrushPalettePanel::BrushPalettePanel(wxWindow* parent, const TilesetContainer &t
PalettePanel(parent, id),
paletteType(category) {

sizer = newd wxBoxSizer(wxVERTICAL);
pageInfoSizer = newd wxFlexGridSizer(7, 1, 1);

// Create the tileset panel
const auto tsSizer = newd wxStaticBoxSizer(wxVERTICAL, this, "Tileset");
choicebook = newd wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxSize(180, 250));
Expand Down Expand Up @@ -154,6 +151,15 @@ PaletteType BrushPalettePanel::GetType() const {
return paletteType;
}

BrushListType BrushPalettePanel::GetListType() const {
if (!choicebook) {
return BRUSHLIST_LISTBOX;
}

const auto panel = dynamic_cast<BrushPanel*>(choicebook->GetPage(0));
return panel->GetListType();
}

void BrushPalettePanel::SetListType(BrushListType newListType) {
if (!choicebook) {
return;
Expand Down Expand Up @@ -433,6 +439,10 @@ void BrushPanel::AssignTileset(const TilesetCategory* newTileset) {
}
}

BrushListType BrushPanel::GetListType() const {
return listType;
}

void BrushPanel::SetListType(BrushListType newListType) {
if (listType != newListType) {
InvalidateContents();
Expand Down Expand Up @@ -632,14 +642,44 @@ Brush* BrushIconBox::GetSelectedBrush() const {
return selectedButton ? selectedButton->brush : nullptr;
}

bool BrushIconBox::SelectPaginatedBrush(const Brush* whatBrush, BrushPalettePanel* brushPalettePanel) {
const auto index = std::ranges::find(tileset->brushlist.begin(), tileset->brushlist.end(), whatBrush) - tileset->brushlist.begin();

if (index < tileset->brushlist.size()) {
const auto page = std::ceil(index / (width * height)) + 1;
if (currentPage != page) {
brushPalettePanel->OnPageUpdate(this, page);
}

const auto it = std::ranges::find_if(brushButtons, [&](const auto &brushButton) {
return brushButton->brush == whatBrush;
});

if (it != brushButtons.end()) {
Select(*it);
return true;
}

return false;
}

return false;
}

bool BrushIconBox::SelectBrush(const Brush* whatBrush) {
Deselect();

if (!whatBrush) {
return false;
}

const auto it = std::ranges::find_if(brushButtons.begin(), brushButtons.end(), [&](const auto &brushButton) {
const auto &brushPalettePanel = g_gui.GetParentWindowByType<BrushPalettePanel*>(GetSelfWindow());
const auto listType = brushPalettePanel->GetListType();
if (listType == BRUSHLIST_LARGE_ICONS || listType == BRUSHLIST_SMALL_ICONS) {
return SelectPaginatedBrush(whatBrush, brushPalettePanel);
}

const auto it = std::ranges::find_if(brushButtons, [&](const auto &brushButton) {
return brushButton->brush == whatBrush;
});

Expand Down Expand Up @@ -747,6 +787,10 @@ Brush* BrushListBox::GetSelectedBrush() const {
return nullptr;
}

bool BrushListBox::SelectPaginatedBrush(const Brush* whatBrush, BrushPalettePanel* brushPalettePanel) noexcept {
return false;
}

bool BrushListBox::SelectBrush(const Brush* whatBrush) {
for (auto index = 0; index < tileset->brushlist.size(); ++index) {
if (tileset->brushlist[index] == whatBrush) {
Expand Down
10 changes: 8 additions & 2 deletions source/palette_brushlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class BrushBoxInterface {
// Returns the currently selected brush (First brush if panel is not loaded)
virtual Brush* GetSelectedBrush() const = 0;
// Select the brush in the parameter, this only changes the look of the panel
virtual bool SelectPaginatedBrush(const Brush* brush, BrushPalettePanel* brushPalettePanel) = 0;
virtual bool SelectBrush(const Brush* brush) = 0;

virtual bool NextPage() = 0;
Expand Down Expand Up @@ -85,6 +86,7 @@ class BrushListBox : public wxVListBox, public BrushBoxInterface {
// Returns the currently selected brush (First brush if panel is not loaded)
Brush* GetSelectedBrush() const;
// Select the brush in the parameter, this only changes the look of the panel
bool SelectPaginatedBrush(const Brush* whatBrush, BrushPalettePanel* brushPalettePanel) noexcept override;
bool SelectBrush(const Brush* whatBrush) override;

bool NextPage() override {
Expand Down Expand Up @@ -127,6 +129,7 @@ class BrushIconBox : public wxScrolledWindow, public BrushBoxInterface {
// Returns the currently selected brush (First brush if panel is not loaded)
Brush* GetSelectedBrush() const;
// Select the brush in the parameter, this only changes the look of the panel
bool SelectPaginatedBrush(const Brush* whatBrush, BrushPalettePanel* brushPalettePanel) override;
bool SelectBrush(const Brush* whatBrush) override;

bool NextPage() override;
Expand Down Expand Up @@ -172,6 +175,8 @@ class BrushPanel : public wxPanel {
// Loads the content (This must be called before the panel is displayed, else it will appear empty
void LoadContents();

BrushListType GetListType() const;

// Sets the display type (list or icons)
void SetListType(BrushListType newListType);
void SetListType(const wxString &newListType);
Expand Down Expand Up @@ -223,6 +228,7 @@ class BrushPalettePanel : public PalettePanel {
void LoadAllContents();

PaletteType GetType() const;
BrushListType GetListType() const;

// Sets the display type (list or icons)
void SetListType(BrushListType newListType);
Expand Down Expand Up @@ -258,8 +264,8 @@ class BrushPalettePanel : public PalettePanel {
void SetCurrentPage(const wxString &text);

protected:
wxSizer* sizer = nullptr;
wxSizer* pageInfoSizer = nullptr;
wxSizer* sizer = newd wxBoxSizer(wxVERTICAL);
wxSizer* pageInfoSizer = newd wxFlexGridSizer(7, 1, 1);
PaletteType paletteType;
wxChoicebook* choicebook = nullptr;
wxButton* nextPageButton = nullptr;
Expand Down
6 changes: 2 additions & 4 deletions source/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,11 @@ void PreferencesWindow::Apply() {
if (g_settings.getBoolean(Config::USE_MEMCACHED_SPRITES) != use_memcached_chkbox->GetValue()) {
must_restart = true;
}
int iconsColSize;
if (palette_icons_col_size->GetValue().ToInt(&iconsColSize) && g_settings.getInteger(Config::PALETTE_COL_COUNT) != iconsColSize) {
if (int iconsColSize; palette_icons_col_size->GetValue().ToInt(&iconsColSize) && g_settings.getInteger(Config::PALETTE_COL_COUNT) != iconsColSize) {
g_settings.setInteger(Config::PALETTE_COL_COUNT, iconsColSize);
must_restart = true;
}
int iconsRowSize;
if (palette_icons_row_size->GetValue().ToInt(&iconsRowSize) && g_settings.getInteger(Config::PALETTE_ROW_COUNT) != iconsRowSize) {
if (int iconsRowSize; palette_icons_row_size->GetValue().ToInt(&iconsRowSize) && g_settings.getInteger(Config::PALETTE_ROW_COUNT) != iconsRowSize) {
g_settings.setInteger(Config::PALETTE_ROW_COUNT, iconsRowSize);
must_restart = true;
}
Expand Down

0 comments on commit 7ce2b9f

Please sign in to comment.