improve RotateTo logic

This commit is contained in:
Jenny Diomidova 2025-12-13 19:35:47 +01:00
parent 517f332ed2
commit 35db4371e9
3 changed files with 21 additions and 23 deletions

View File

@ -5,6 +5,10 @@
using namespace Pebble; using namespace Pebble;
namespace { 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) { void draw_digit(const Pebble::GContext& ctx, const Pebble::GRect& bounds, const uint16_t digit) {
Pebble::GBitmapBase& bitmap = bitmaps::GetDigitBitmap(digit); Pebble::GBitmapBase& bitmap = bitmaps::GetDigitBitmap(digit);
auto rect = bitmap.GetBounds(); auto rect = bitmap.GetBounds();
@ -15,13 +19,13 @@ namespace {
void DigitLayerInner::OnUpdate(const Pebble::GContext& ctx) { void DigitLayerInner::OnUpdate(const Pebble::GContext& ctx) {
auto bounds = this->GetBounds(); auto bounds = this->GetBounds();
// LOG("Digit layer update. %d digits", this->digitCount); // LOG("Digit layer update. %d digits", digitCount);
ctx.SetCompositingMode(GCompOpSet); ctx.SetCompositingMode(GCompOpSet);
for (int i = 0; i <= this->digitCount + 2 * this->extraDigits; i++) { for (int i = 0; i <= digitCount + 2 * extraDigits; i++) {
Pebble::GRect rect(0, i * this->digitHeight, bounds.size.w, this->digitHeight); Pebble::GRect rect(0, i * digitHeight, bounds.size.w, digitHeight);
uint16_t digit = ((i - this->extraDigits) % this->digitCount + this->digitCount) % this->digitCount % 10; uint16_t digit = ((i - extraDigits) % digitCount + digitCount) % digitCount;
// LOG("Digit %d -> %d", i - this->extraDigits, digit); // LOG("Digit %d -> %d", i - extraDigits, digit);
draw_digit(ctx, rect, digit); draw_digit(ctx, rect, digit);
} }
} }
@ -35,7 +39,7 @@ DigitLayer::DigitLayer() : Pebble::SimpleLayer(Pebble::GRect()) {
void DigitLayer::UpdateLayout() { void DigitLayer::UpdateLayout() {
auto bounds = this->GetBounds(); auto bounds = this->GetBounds();
this->scrollLayer.SetFrame(bounds); 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->innerLayer.SetFrame(bounds);
this->scrollLayer.SetContentSize(bounds.size); this->scrollLayer.SetContentSize(bounds.size);
} }
@ -43,18 +47,21 @@ void DigitLayer::UpdateLayout() {
int16_t DigitLayer::ComputeOffset(uint16_t digit) { int16_t DigitLayer::ComputeOffset(uint16_t digit) {
auto bounds = this->GetBounds(); auto bounds = this->GetBounds();
return bounds.size.h / 2 - this->innerLayer.digitHeight / 2 return bounds.size.h / 2 - digitHeight / 2 - (digit + extraDigits) * digitHeight;
- (digit + this->innerLayer.extraDigits) * this->innerLayer.digitHeight;
} }
void DigitLayer::RotateTo(uint16_t digit, bool animate) { void DigitLayer::RotateTo(uint16_t digit, bool animate) {
// LOG("Digit %d, out of %d", digit, innerLayer.digitCount); // LOG("Digit %d, out of %d", digit, digitCount);
digit %= innerLayer.digitCount; digit %= digitCount;
auto currentOffset = this->scrollLayer.GetContentOffset(); auto currentOffset = this->scrollLayer.GetContentOffset();
if (digit == 0 && currentOffset.y <= ComputeOffset(this->innerLayer.digitCount / 2)) { if (digit > this->currentDigit + digitCount / 2) {
currentOffset.y += this->innerLayer.digitCount * this->innerLayer.digitHeight; 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(currentOffset, false);
} }
this->scrollLayer.SetContentOffset(this->ComputeOffset(digit), animate); this->scrollLayer.SetContentOffset(this->ComputeOffset(digit), animate);
this->currentDigit = digit;
} }

View File

@ -3,10 +3,6 @@
#include <pebble-cpp/pebble.hpp> #include <pebble-cpp/pebble.hpp>
class DigitLayerInner : public Pebble::CustomLayer { 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()) {} DigitLayerInner() : Pebble::CustomLayer(Pebble::GRect()) {}
static void DrawDigit(const Pebble::GContext& ctx, const Pebble::GRect& bounds, const uint16_t digit); 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 { class DigitLayer : public Pebble::SimpleLayer {
DigitLayerInner innerLayer; DigitLayerInner innerLayer;
Pebble::ScrollLayer scrollLayer; Pebble::ScrollLayer scrollLayer;
int16_t currentDigit = 0;
public: public:
DigitLayer(); DigitLayer();
void SetDigitCount(uint16_t cnt = 10) {
if (this->innerLayer.digitCount != cnt) {
this->innerLayer.digitCount = cnt;
this->UpdateLayout();
}
}
void UpdateLayout(); void UpdateLayout();
void RotateTo(uint16_t digit, bool animate=true); void RotateTo(uint16_t digit, bool animate=true);
private: private:

View File

@ -8,7 +8,7 @@
using namespace Pebble; using namespace Pebble;
// For debugging // For debugging
const bool USE_SECONDS_INSTEAD_OF_MINUTES = false; constexpr bool USE_SECONDS_INSTEAD_OF_MINUTES = false;
class MainWindow : public AppWindow, public TickTimer { class MainWindow : public AppWindow, public TickTimer {
DigitLayer digitLayers[4]; DigitLayer digitLayers[4];