diff --git a/src/ppui/Tools.cpp b/src/ppui/Tools.cpp index b9b644b2..674114ba 100644 --- a/src/ppui/Tools.cpp +++ b/src/ppui/Tools.cpp @@ -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)); +} + diff --git a/src/ppui/Tools.h b/src/ppui/Tools.h index bf353a48..67d41c7f 100644 --- a/src/ppui/Tools.h +++ b/src/ppui/Tools.h @@ -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 diff --git a/src/tracker/PatternEditorControl.cpp b/src/tracker/PatternEditorControl.cpp index d7cbd9ad..6ff63541 100644 --- a/src/tracker/PatternEditorControl.cpp +++ b/src/tracker/PatternEditorControl.cpp @@ -22,6 +22,7 @@ #include "PatternEditorControl.h" #include "GraphicsAbstract.h" +#include "Tools.h" #include "Screen.h" #include "Control.h" #include "Font.h" @@ -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); diff --git a/src/tracker/PatternEditorControlEventListener.cpp b/src/tracker/PatternEditorControlEventListener.cpp index 3bdcebb8..bfedace4 100644 --- a/src/tracker/PatternEditorControlEventListener.cpp +++ b/src/tracker/PatternEditorControlEventListener.cpp @@ -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) @@ -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; + } } } @@ -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) { @@ -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); }