cryptic-clock/src/c/digit_layer.cpp

60 lines
2.2 KiB
C++

#include "digit_layer.hpp"
#include <@rebble/sdk-c-utils/macros.h>
#include "bitmaps.hpp"
using namespace Pebble;
namespace {
void draw_digit(const Pebble::GContext& ctx, const Pebble::GRect& bounds, const uint16_t digit) {
Pebble::GBitmapBase& bitmap = bitmaps::GetDigitBitmap(digit);
auto rect = bitmap.GetBounds();
rect.Align(bounds, GAlignCenter, false);
ctx.DrawBitmapInRect(bitmap, rect);
}
}
void DigitLayerInner::OnUpdate(const Pebble::GContext& ctx) {
auto bounds = this->GetBounds();
// LOG("Digit layer update. %d digits", this->digitCount);
ctx.SetCompositingMode(GCompOpSet);
for (int i = 0; i <= this->digitCount + 2 * this->extraDigits; i++) {
Pebble::GRect rect(0, i * this->digitHeight, bounds.size.w, this->digitHeight);
uint16_t digit = ((i - this->extraDigits) % this->digitCount + this->digitCount) % this->digitCount % 10;
// LOG("Digit %d -> %d", i - this->extraDigits, digit);
draw_digit(ctx, rect, digit);
}
}
DigitLayer::DigitLayer() : Pebble::SimpleLayer(Pebble::GRect()) {
this->scrollLayer.AddChild(this->innerLayer);
this->scrollLayer.SetShadowHidden(true);
this->AddChild(this->scrollLayer);
}
void DigitLayer::UpdateLayout() {
auto bounds = this->GetBounds();
this->scrollLayer.SetFrame(bounds);
bounds.size.h = (this->innerLayer.digitCount + 2 * this->innerLayer.extraDigits) * this->innerLayer.digitHeight;
this->innerLayer.SetFrame(bounds);
this->scrollLayer.SetContentSize(bounds.size);
}
int16_t DigitLayer::ComputeOffset(uint16_t digit) {
auto bounds = this->GetBounds();
return bounds.size.h / 2 - this->innerLayer.digitHeight / 2
- (digit + this->innerLayer.extraDigits) * this->innerLayer.digitHeight;
}
void DigitLayer::RotateTo(uint16_t digit, bool animate) {
// LOG("Digit %d, out of %d", digit, innerLayer.digitCount);
digit %= innerLayer.digitCount;
auto currentOffset = this->scrollLayer.GetContentOffset();
if (digit == 0 && currentOffset.y <= ComputeOffset(this->innerLayer.digitCount / 2)) {
currentOffset.y += this->innerLayer.digitCount * this->innerLayer.digitHeight;
this->scrollLayer.SetContentOffset(currentOffset, false);
}
this->scrollLayer.SetContentOffset(this->ComputeOffset(digit), animate);
}