Skip to content

Commit

Permalink
Allow selections by clicking outside the pattern bounds
Browse files Browse the repository at this point in the history
When clicking above the first row, or beyond the rightmost channel, instead of ignoring the input, selection will be properly clamped.

Selections that would exceed the bottom of the pattern are now also clamped.
  • Loading branch information
exelotl authored and Deltafire committed May 23, 2020
1 parent 268da18 commit 4630751
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
5 changes: 5 additions & 0 deletions src/ppui/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,8 @@ bool PPTools::decodeByteArray(pp_uint8* array, pp_uint32 size, const PPString& s
return true;
}

pp_int32 PPTools::clamp(pp_int32 a, pp_int32 min, pp_int32 max)
{
return (a < min ? min : (a >= max ? max-1 : a));
}

2 changes: 2 additions & 0 deletions src/ppui/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class PPTools

static PPString encodeByteArray(const pp_uint8* array, pp_uint32 size);
static bool decodeByteArray(pp_uint8* array, pp_uint32 size, const PPString& str);

static pp_int32 clamp(pp_int32 a, pp_int32 min, pp_int32 max);
};

#endif
13 changes: 5 additions & 8 deletions src/tracker/PatternEditorControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "PatternEditorControl.h"
#include "GraphicsAbstract.h"
#include "Tools.h"
#include "Screen.h"
#include "Control.h"
#include "Font.h"
Expand Down Expand Up @@ -828,14 +829,10 @@ void PatternEditorControl::paint(PPGraphicsAbstract* g)

if (i2 >= 0 && j2 >= 0 && i1 < pattern->rows && j1 < numVisibleChannels)
{
#define CLAMP(a, min, max) ((a) < (min) ? (min) : ((a) >= (max) ? (max-1) : (a)))

i1 = CLAMP(i1, 0, pattern->rows);
i2 = CLAMP(i2, 0, pattern->rows);
j1 = CLAMP(j1, 0, numVisibleChannels);
j2 = CLAMP(j2, 0, numVisibleChannels);

#undef CLAMP
i1 = PPTools::clamp(i1, 0, pattern->rows);
i2 = PPTools::clamp(i2, 0, pattern->rows);
j1 = PPTools::clamp(j1, 0, numVisibleChannels);
j2 = PPTools::clamp(j2, 0, numVisibleChannels);

pp_int32 x1 = (location.x + (j1-startPos) * slotSize + SCROLLBARWIDTH) + cursorPositions[selectionStart.inner] + (getRowCountWidth() + 4);
pp_int32 y1 = (location.y + (i1-startIndex) * font->getCharHeight() + SCROLLBARWIDTH) + (font->getCharHeight() + 4);
Expand Down
77 changes: 48 additions & 29 deletions src/tracker/PatternEditorControlEventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,18 @@ pp_int32 PatternEditorControl::dispatchEvent(PPEvent* event)
if (newStartIndex < visibleRows && newStartIndex >= 0)
{
if (newStartIndex + startIndex < 0)
break;

preCursor.row = newStartIndex + startIndex;
{
patternEditor->resetSelection();
preCursor.row = 0;
}
else if (newStartIndex + startIndex >= patternEditor->getNumRows())
{
preCursor.row = patternEditor->getNumRows()-1;
}
else
{
preCursor.row = newStartIndex + startIndex;
}
}

if (newStartPos < visibleHeight && newStartPos >= 0)
Expand All @@ -380,20 +389,27 @@ pp_int32 PatternEditorControl::dispatchEvent(PPEvent* event)
break;

preCursor.channel = newStartPos + startPos;
preCursor.inner = 0;

if (preCursor.channel >= patternEditor->getNumChannels())
break;

preCursor.inner = 0;

pp_int32 innerPos = cp.x % slotSize;
for (pp_uint32 i = 0; i < sizeof(cursorPositions) - 1; i++)
{
if (innerPos >= cursorPositions[i] &&
innerPos < cursorPositions[i+1])
// clicked beyond rightmost channel, start selection from the edge
patternEditor->resetSelection();
preCursor.channel = patternEditor->getNumChannels() - 1;
preCursor.inner = 7;
}
else
{
// find which column in the channel was clicked
pp_int32 innerPos = cp.x % slotSize;
for (pp_uint32 i = 0; i < sizeof(cursorPositions) - 1; i++)
{
preCursor.inner = i;
break;
if (innerPos >= cursorPositions[i] &&
innerPos < cursorPositions[i+1])
{
preCursor.inner = i;
break;
}
}
}

Expand Down Expand Up @@ -651,6 +667,7 @@ pp_int32 PatternEditorControl::dispatchEvent(PPEvent* event)

mp_sint32 cursorPositionRow = newStartIndex + startIndex;
mp_sint32 cursorPositionChannel = newStartPos + startPos;
mp_sint32 cursorPositionInner;

if (moveSelection)
{
Expand All @@ -659,38 +676,40 @@ pp_int32 PatternEditorControl::dispatchEvent(PPEvent* event)
}
else
{
if (cursorPositionRow < 0) cursorPositionRow = 0;
if (cursorPositionChannel < 0) cursorPositionChannel = 0;

//if (cursorPositionRow < 0 || cursorPositionChannel < 0)
// break;

if (cursorPositionChannel >= patternEditor->getNumChannels())
if (cursorPositionRow < 0)
cursorPositionRow = 0;
else if (cursorPositionRow >= patternEditor->getNumRows())
cursorPositionRow = patternEditor->getNumRows()-1;

if (cursorPositionChannel < 0)
{
cursorPositionChannel = 0;
cursorPositionInner = 0;
}
else if (cursorPositionChannel >= patternEditor->getNumChannels())
{
patternEditor->getSelection().end.channel = patternEditor->getNumChannels()-1;
patternEditor->getSelection().end.inner = 7;
cursorPositionChannel = patternEditor->getNumChannels()-1;
cursorPositionInner = 7;
}
else
{

// start selecting row
patternEditor->getSelection().end.channel = cursorPositionChannel;
patternEditor->getSelection().end.row = cursorPositionRow;

pp_int32 innerPos = cp.x % slotSize;

//selectionEnd.inner = 7;
for (pp_uint32 i = 0; i < sizeof(cursorPositions) - 1; i++)
{
if (innerPos >= cursorPositions[i] &&
innerPos < cursorPositions[i+1])
{
patternEditor->getSelection().end.inner = i;
cursorPositionInner = i;
break;
}
}
}

patternEditor->getSelection().end.row = cursorPositionRow;
patternEditor->getSelection().end.channel = cursorPositionChannel;
patternEditor->getSelection().end.inner = cursorPositionInner;

setScrollbarPositions(startIndex, startPos);

}
Expand Down

0 comments on commit 4630751

Please sign in to comment.