From 35db4371e920641167f5b85d46bb7fb491ee91b5 Mon Sep 17 00:00:00 2001 From: Jenny Diomidova Date: Sat, 13 Dec 2025 19:35:47 +0100 Subject: [PATCH] improve RotateTo logic --- src/c/digit_layer.cpp | 31 +++++++++++++++++++------------ src/c/digit_layer.hpp | 11 +---------- src/c/main.cpp | 2 +- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/c/digit_layer.cpp b/src/c/digit_layer.cpp index 126b89f..0b1ddd4 100644 --- a/src/c/digit_layer.cpp +++ b/src/c/digit_layer.cpp @@ -5,6 +5,10 @@ using namespace Pebble; namespace { + static constexpr int16_t extraDigits = 6; + static constexpr int16_t digitHeight = 56; + static constexpr int16_t digitCount = 10; + 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(); @@ -15,13 +19,13 @@ namespace { void DigitLayerInner::OnUpdate(const Pebble::GContext& ctx) { auto bounds = this->GetBounds(); - // LOG("Digit layer update. %d digits", this->digitCount); + // LOG("Digit layer update. %d digits", 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); + for (int i = 0; i <= digitCount + 2 * extraDigits; i++) { + Pebble::GRect rect(0, i * digitHeight, bounds.size.w, digitHeight); + uint16_t digit = ((i - extraDigits) % digitCount + digitCount) % digitCount; + // LOG("Digit %d -> %d", i - extraDigits, digit); draw_digit(ctx, rect, digit); } } @@ -35,7 +39,7 @@ DigitLayer::DigitLayer() : Pebble::SimpleLayer(Pebble::GRect()) { void DigitLayer::UpdateLayout() { auto bounds = this->GetBounds(); this->scrollLayer.SetFrame(bounds); - bounds.size.h = (this->innerLayer.digitCount + 2 * this->innerLayer.extraDigits) * this->innerLayer.digitHeight; + bounds.size.h = (digitCount + 2 * extraDigits) * digitHeight; this->innerLayer.SetFrame(bounds); this->scrollLayer.SetContentSize(bounds.size); } @@ -43,18 +47,21 @@ void DigitLayer::UpdateLayout() { 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; + return bounds.size.h / 2 - digitHeight / 2 - (digit + extraDigits) * digitHeight; } void DigitLayer::RotateTo(uint16_t digit, bool animate) { - // LOG("Digit %d, out of %d", digit, innerLayer.digitCount); - digit %= innerLayer.digitCount; + // LOG("Digit %d, out of %d", digit, digitCount); + digit %= digitCount; auto currentOffset = this->scrollLayer.GetContentOffset(); - if (digit == 0 && currentOffset.y <= ComputeOffset(this->innerLayer.digitCount / 2)) { - currentOffset.y += this->innerLayer.digitCount * this->innerLayer.digitHeight; + if (digit > this->currentDigit + digitCount / 2) { + currentOffset.y -= digitCount * digitHeight; + this->scrollLayer.SetContentOffset(currentOffset, false); + } else if (digit < this->currentDigit - digitCount / 2) { + currentOffset.y += digitCount * digitHeight; this->scrollLayer.SetContentOffset(currentOffset, false); } this->scrollLayer.SetContentOffset(this->ComputeOffset(digit), animate); + this->currentDigit = digit; } \ No newline at end of file diff --git a/src/c/digit_layer.hpp b/src/c/digit_layer.hpp index 82c68bd..7e66cae 100644 --- a/src/c/digit_layer.hpp +++ b/src/c/digit_layer.hpp @@ -3,10 +3,6 @@ #include class DigitLayerInner : public Pebble::CustomLayer { - static constexpr int16_t extraDigits = 2; - static constexpr int16_t digitHeight = 56; - int16_t digitCount = 10; - DigitLayerInner() : Pebble::CustomLayer(Pebble::GRect()) {} static void DrawDigit(const Pebble::GContext& ctx, const Pebble::GRect& bounds, const uint16_t digit); @@ -20,15 +16,10 @@ protected: class DigitLayer : public Pebble::SimpleLayer { DigitLayerInner innerLayer; Pebble::ScrollLayer scrollLayer; + int16_t currentDigit = 0; public: DigitLayer(); - void SetDigitCount(uint16_t cnt = 10) { - if (this->innerLayer.digitCount != cnt) { - this->innerLayer.digitCount = cnt; - this->UpdateLayout(); - } - } void UpdateLayout(); void RotateTo(uint16_t digit, bool animate=true); private: diff --git a/src/c/main.cpp b/src/c/main.cpp index 89e8045..48f7cad 100644 --- a/src/c/main.cpp +++ b/src/c/main.cpp @@ -8,7 +8,7 @@ using namespace Pebble; // For debugging -const bool USE_SECONDS_INSTEAD_OF_MINUTES = false; +constexpr bool USE_SECONDS_INSTEAD_OF_MINUTES = false; class MainWindow : public AppWindow, public TickTimer { DigitLayer digitLayers[4];