Files
Upsilon/apps/shared/round_cursor_view.cpp
Léa Saviot f037df0ab5 [apps/round_cursor_view] Fix color change
When changing the color, erase the cursor first, otherwise there is a
drawing glitch

Scenario:
f(x)=1
g(x)=2
Cursor on f(x), go to g(x), go right -> the previous cursor left a dot
2019-09-23 17:31:35 +02:00

77 lines
2.4 KiB
C++

#include "round_cursor_view.h"
namespace Shared {
static constexpr KDCoordinate cursorSize = 10;
static const uint8_t cursorMask[cursorSize][cursorSize] = {
{0xFF, 0xFF, 0xFF, 0xED, 0xB6, 0xB6, 0xED, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0x7C, 0x06, 0x00, 0x00, 0x06, 0x7C, 0xFF, 0xFF},
{0xFF, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xFF},
{0xED, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE5},
{0xB6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB6},
{0xB6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB6},
{0xED, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE5},
{0xFF, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xFF},
{0xFF, 0xFF, 0x7C, 0x06, 0x00, 0x00, 0x06, 0x7C, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xED, 0xB6, 0xB6, 0xED, 0xFF, 0xFF, 0xFF},
};
static KDColor s_cursorWorkingBuffer[cursorSize*cursorSize];
void RoundCursorView::drawRect(KDContext * ctx, KDRect rect) const {
KDRect r = bounds();
ctx->getPixels(r, m_underneathPixelBuffer);
m_underneathPixelBufferLoaded = true;
ctx->blendRectWithMask(r, m_color, (const uint8_t *)cursorMask, s_cursorWorkingBuffer);
}
KDSize RoundCursorView::minimalSizeForOptimalDisplay() const {
return KDSize(cursorSize, cursorSize);
}
void RoundCursorView::setColor(KDColor color) {
m_color = color;
eraseCursorIfPossible();
markRectAsDirty(bounds());
}
void RoundCursorView::setCursorFrame(KDRect f) {
#if GRAPH_CURSOR_SPEEDUP
/* TODO This is quite dirty (we are out of the dirty tracking and we assume
* the cursor is the upmost view) but it works well. */
if (m_frame == f) {
return;
}
/* We want to avoid drawing the curve just because the cursor has been
* repositioned, as it is very slow for non cartesian curves.*/
if (eraseCursorIfPossible()) {
// Set the frame
m_frame = f;
markRectAsDirty(bounds());
return;
}
#endif
CursorView::setCursorFrame(f);
}
#ifdef GRAPH_CURSOR_SPEEDUP
bool RoundCursorView::eraseCursorIfPossible() {
if (!m_underneathPixelBufferLoaded) {
return false;
}
const KDRect currentFrame = absoluteVisibleFrame();
if (currentFrame.isEmpty()) {
return false;
}
// Erase the cursor
KDContext * ctx = KDIonContext::sharedContext();
ctx->setOrigin(currentFrame.origin());
ctx->setClippingRect(currentFrame);
ctx->fillRectWithPixels(KDRect(0,0,k_cursorSize, k_cursorSize), m_underneathPixelBuffer, s_cursorWorkingBuffer);
// TODO Restore the context to previous values?
return true;
}
#endif
}