[escher] Improve switch view

Change-Id: I832cc4733c0611d79d5ac79e3867413e5af0ac2d
This commit is contained in:
Émilie Feral
2017-04-17 10:05:14 +02:00
parent 257e90f108
commit 91ce605f65
2 changed files with 44 additions and 22 deletions

View File

@@ -10,14 +10,12 @@ public:
void setState(bool state);
void drawRect(KDContext * ctx, KDRect rect) const override;
KDSize minimalSizeForOptimalDisplay() const override;
private:
/* k_switchHeight and k_switchWidth are the dimensions of the switch
* (including the outline of the switch). The outline thickness is
* k_separatorThickness. The k_switchMargin is the margin between the right
* extremity of the view and the right extremity of the switch. */
constexpr static KDCoordinate k_switchHeight = 16;
constexpr static KDCoordinate k_switchWidth = 20;
constexpr static KDCoordinate k_separatorThickness = 1;
* (including the outline of the switch). */
constexpr static KDCoordinate k_onOffSize = 12;
constexpr static KDCoordinate k_switchHeight = 12;
constexpr static KDCoordinate k_switchWidth = 22;
private:
bool m_state;
};

View File

@@ -1,6 +1,37 @@
#include <escher/switch_view.h>
#include <escher/palette.h>
const uint8_t switchMask[SwitchView::k_switchHeight][SwitchView::k_switchWidth] = {
{0xFF, 0xFF, 0xE1, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xE1, 0xFF, 0xFF},
{0xFF, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0xFF},
{0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1},
{0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45},
{0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C},
{0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45},
{0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1},
{0xFF, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0xFF},
{0xFF, 0xFF, 0xE1, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xE1, 0xFF, 0xFF},
};
const uint8_t onOffMask[SwitchView::k_onOffSize][SwitchView::k_onOffSize] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xE1, 0x0C, 0x00, 0x00, 0x0C, 0xE1, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0xFF, 0xFF},
{0xFF, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xFF},
{0xFF, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xFF},
{0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF},
{0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF},
{0xFF, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xFF},
{0xFF, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xFF},
{0xFF, 0xFF, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xE1, 0x0C, 0x00, 0x00, 0x0C, 0xE1, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
};
SwitchView::SwitchView() :
View(),
m_state(true)
@@ -16,6 +47,8 @@ void SwitchView::setState(bool state) {
markRectAsDirty(bounds());
}
KDColor s_switchWorkingBuffer[SwitchView::k_switchWidth*SwitchView::k_switchHeight];
void SwitchView::drawRect(KDContext * ctx, KDRect rect) const {
/* Draw the switch aligned on the right of the view and vertically centered.
* The heightCenter is the coordinate of the vertical middle of the view. That
@@ -23,24 +56,15 @@ void SwitchView::drawRect(KDContext * ctx, KDRect rect) const {
KDCoordinate width = bounds().width();
KDCoordinate heightCenter = bounds().height()/2;
KDCoordinate switchHalfHeight = k_switchHeight/2;
KDCoordinate switchHalfWidth = k_switchWidth/2;
KDColor mainColor = m_state ? Palette::YellowDark : Palette::GreyDark;
// These 4 lines draw the outline of the switch.
ctx->fillRect(KDRect(width - 2*k_separatorThickness - k_switchWidth, heightCenter-switchHalfHeight - k_separatorThickness, k_switchWidth + 2*k_separatorThickness, k_separatorThickness), mainColor);
ctx->fillRect(KDRect(width - 2*k_separatorThickness - k_switchWidth, heightCenter-switchHalfHeight, k_separatorThickness, k_switchHeight+k_separatorThickness), mainColor);
ctx->fillRect(KDRect(width - k_separatorThickness - k_switchWidth, heightCenter+switchHalfHeight, k_switchWidth+k_separatorThickness, k_separatorThickness), mainColor);
ctx->fillRect(KDRect(width - k_separatorThickness, heightCenter-switchHalfHeight, k_separatorThickness, k_switchHeight), mainColor);
// These next lines fill the switch with black and green/red.
if (m_state) {
ctx->fillRect(KDRect(width - k_switchWidth - k_separatorThickness, heightCenter-switchHalfHeight, switchHalfWidth, k_switchHeight), KDColorWhite);
ctx->fillRect(KDRect(width - switchHalfWidth - k_separatorThickness, heightCenter-switchHalfHeight, switchHalfWidth, k_switchHeight), mainColor);
} else {
ctx->fillRect(KDRect(width - k_switchWidth - k_separatorThickness, heightCenter-switchHalfHeight, switchHalfWidth, k_switchHeight), mainColor);
ctx->fillRect(KDRect(width - switchHalfWidth - k_separatorThickness, heightCenter-switchHalfHeight, switchHalfWidth, k_switchHeight), KDColorWhite);
}
KDRect frame(width - k_switchWidth, heightCenter -switchHalfHeight, k_switchWidth, k_switchHeight);
ctx->blendRectWithMask(frame, mainColor, (const uint8_t *)switchMask, s_switchWorkingBuffer);
KDCoordinate onOffX = width - (m_state ? k_switchWidth : k_onOffSize);
KDRect onOffFrame(onOffX, heightCenter -switchHalfHeight, k_onOffSize, k_onOffSize);
ctx->blendRectWithMask(onOffFrame, KDColorWhite, (const uint8_t *)onOffMask, s_switchWorkingBuffer);
}
KDSize SwitchView::minimalSizeForOptimalDisplay() const {
return KDSize(2*k_separatorThickness + k_switchWidth, k_switchHeight);
return KDSize(k_switchWidth, k_switchHeight);
}