From fff867846d9bc7844c331dd4f0bc6e611cc7bf49 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 22 Feb 2020 22:25:27 +0100 Subject: [PATCH] Add support to drag icons into empty spots --- src/gui/GuiIconGrid.cpp | 67 +++++++++++++++++++++++++++++++++-------- src/gui/GuiIconGrid.h | 2 ++ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/gui/GuiIconGrid.cpp b/src/gui/GuiIconGrid.cpp index d50916c..4ef9861 100644 --- a/src/gui/GuiIconGrid.cpp +++ b/src/gui/GuiIconGrid.cpp @@ -115,6 +115,14 @@ GuiIconGrid::GuiIconGrid(int32_t w, int32_t h, uint64_t GameIndex,bool sortByNam for(int i = 0; i< MAX_COLS * MAX_ROWS *2; i++) { GameIcon * image = new GameIcon(&emptyIcon); emptyIcons.push_back(image); + GuiButton * button = new GuiButton(emptyIcon.getWidth(), emptyIcon.getHeight()); + button->setImage(image); + button->setPosition(0, 0); + //button->setEffectGrow(); + button->setHoldable(true); + button->setTrigger(&touchTrigger); + button->held.connect(this, &GuiIconGrid::OnGameButtonHeld); + emptyButtons.push_back(button); } dragListener.setTrigger(&touchTrigger); @@ -139,10 +147,15 @@ GuiIconGrid::~GuiIconGrid() { gameInfoContainers.clear(); containerMutex.unlock(); - for (auto const& x : emptyIcons) { - AsyncExecutor::pushForDelete(x); + for (auto const& x : emptyButtons) { + delete x; } + for (auto const& x : emptyIcons) { + delete x; + } + + emptyButtons.clear(); emptyIcons.clear(); } @@ -374,7 +387,17 @@ void GuiIconGrid::OnRightArrowReleased(GuiButton *button, const GuiController *c void GuiIconGrid::OnGameButtonHeld(GuiButton *button, const GuiController *controller, GuiTrigger *trigger) { if(currentlyHeld == NULL) { - currentlyHeld = button; + bool found = false; + // We don't want to drag empty buttons + for(auto const & x : emptyButtons) { + if(x == button) { + found = true; + break; + } + } + if(!found) { + currentlyHeld = button; + } } if(currentlyHeld != NULL && currentlyHeld != button) { dragTarget = button; @@ -491,14 +514,20 @@ void GuiIconGrid::process() { for (auto const& x : vec) { if(x.second->button == dragTarget) { targetTitleId = x.first; + break; } } - if(targetTitleId > 0) { - for(uint32_t i = 0; i< position.size(); i++) { - if(position[i] == targetTitleId) { + for(uint32_t i = 0; i< positionButtons.size(); i++) { + if(positionButtons[i] == dragTarget) { + if(i < position.size() && i != currentlyHeldPosition) { position[i] = currentlyHeldTitleId; + DEBUG_FUNCTION_LINE("Set position to title id to %d\n", i, currentlyHeldPosition); + } else { + targetTitleId = currentlyHeldTitleId; } + + break; } } @@ -576,7 +605,7 @@ void GuiIconGrid::updateButtonPositions() { remove(x.second->button); } - for (auto const& x : emptyIcons) { + for (auto const& x : emptyButtons) { remove(x); } @@ -590,7 +619,6 @@ void GuiIconGrid::updateButtonPositions() { }); } - // TODO somehow be able to adjust the positions. //position.clear(); @@ -603,6 +631,9 @@ void GuiIconGrid::updateButtonPositions() { uint32_t elementSize = position.size(); uint32_t pages = (elementSize / (MAX_COLS * MAX_ROWS)) +1; + if(elementSize % (MAX_COLS * MAX_ROWS) == 0){ + pages--; + } uint32_t emptyIconUse = 0; @@ -637,15 +668,21 @@ void GuiIconGrid::updateButtonPositions() { if(endPage > pages) { endPage = pages; } - }else{ + } else { for (auto const& x : vec) { x.second->button->setHoldable(true); } } + uint32_t startValue = startPage * (MAX_COLS * MAX_ROWS); + + positionButtons.clear(); + for(uint32_t i = 0; isetPosition(posX, posY); append(element); @@ -686,6 +724,11 @@ void GuiIconGrid::updateButtonPositions() { if(currentlyHeld != NULL) { append(currentlyHeld); } + if(positionButtons.size() > position.size()) { + for(uint32_t i = 0; i < positionButtons.size() - position.size(); i++) { + position.push_back(0); + } + } } void GuiIconGrid::draw(CVideo *pVideo) { diff --git a/src/gui/GuiIconGrid.h b/src/gui/GuiIconGrid.h index 4d13dfa..4631a47 100644 --- a/src/gui/GuiIconGrid.h +++ b/src/gui/GuiIconGrid.h @@ -148,8 +148,10 @@ private: std::recursive_mutex containerMutex; std::map gameInfoContainers; std::vector position; + std::vector positionButtons; std::vector emptyIcons; + std::vector emptyButtons; };