Skip to content

Commit

Permalink
Restore ReorderFunctions.h and add a note in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
getdunne committed Apr 1, 2019
1 parent fccdcf5 commit 6bd7f75
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ To implement a JUCE list-box with drag-to-reorder capability:
5. Set your *DraggableListBox*'s model to be your *DraggableListBoxModel* object (by calling the former's *setModel()* member).

See this repo's *MainContentComponent.h* and *MainContentComponent.cpp*. The file *Main.cpp* is auto-generated by the Projucer and completely generic.

**Note** I have used a *juce::OwnedArray* as my app-specific data container. If you prefer to use a *std::vector* (or any of several other container classes in the C++ STL), you can use the function templates found in *ReorderFunctions.h* (from Charles's original code, but not used in mine) to implement the *moveBefore()/moveAfter()* operations.
46 changes: 46 additions & 0 deletions Source/ReorderFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once
#include "JuceHeader.h"

template<typename C>
void MoveItemBefore(C& container, size_t currentIndex, size_t indexOfItemToPlaceBefore)
{
if (currentIndex == indexOfItemToPlaceBefore) return;

jassert(isPositiveAndBelow((int)currentIndex, (int)container.size()));
jassert(isPositiveAndBelow((int)indexOfItemToPlaceBefore, (int)container.size()));

if (currentIndex < indexOfItemToPlaceBefore)
{
std::rotate(container.begin() + currentIndex,
container.begin() + currentIndex + 1,
container.begin() + indexOfItemToPlaceBefore);
}
else
{
std::rotate(container.begin() + indexOfItemToPlaceBefore,
container.begin() + currentIndex,
container.begin() + currentIndex + 1);
}
}

template<typename C>
void MoveItemAfter(C& container, size_t currentIndex, size_t indexOfItemToPlaceAfter)
{
if (currentIndex == indexOfItemToPlaceAfter) return;

jassert(isPositiveAndBelow((int)currentIndex, (int)container.size()));
jassert(isPositiveAndBelow((int)indexOfItemToPlaceAfter, (int)container.size()));

if (currentIndex < indexOfItemToPlaceAfter)
{
std::rotate(container.begin() + currentIndex,
container.begin() + currentIndex + 1,
container.begin() + indexOfItemToPlaceAfter + 1);
}
else
{
std::rotate(container.begin() + indexOfItemToPlaceAfter + 1,
container.begin() + currentIndex,
container.begin() + currentIndex + 1);
}
}

0 comments on commit 6bd7f75

Please sign in to comment.