diff --git a/package.json b/package.json index e608fd8..04b9bfc 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,9 @@ "watchface": true }, "messageKeys": [ - "dummy" + "BACKGROUND_COLOR", + "DIGIT_COLOR", + "FRAME_COLOR" ], "capabilities": [ "configurable" ], "resources": { diff --git a/src/c/bitmaps.cpp b/src/c/bitmaps.cpp index 5e2b4aa..bf060f4 100644 --- a/src/c/bitmaps.cpp +++ b/src/c/bitmaps.cpp @@ -4,95 +4,97 @@ using namespace Pebble; // TODO move this to pebble-cpp -bool operator==(const GColor8& lhs, const GColor8& rhs) { return lhs.argb == rhs.argb; } -bool operator!=(const GColor8& lhs, const GColor8& rhs) { return lhs.argb != rhs.argb; } +bool operator==(const GColor& lhs, const GColor& rhs) { return gcolor_equal(lhs, rhs); } +bool operator!=(const GColor& lhs, const GColor& rhs) { return !gcolor_equal(lhs, rhs); } -namespace { - GOwnedBitmap digitsBitmap; - GOwnedBitmap digitBitmaps[10]; - GOwnedBitmap frameBitmap; +namespace bitmaps { + namespace { + GOwnedBitmap digitsBitmap; + GOwnedBitmap digitBitmaps[10]; + GOwnedBitmap frameBitmap; - GColor* digitColor; - GColor* frameColor; + GColor* digitColor; + GColor* frameColor; - int get_palette_size(GBitmapBase& bitmap){ - switch (bitmap.GetFormat()) { - case GBitmapFormat1BitPalette: return 2; - case GBitmapFormat2BitPalette: return 4; - case GBitmapFormat4BitPalette: return 16; - default: return 0; - } - } - - GColor* find_color(GBitmapBase& bitmap, GColor color) { - GColor* palette = bitmap.GetPalette(); - if (palette == nullptr) { - ERROR("Palette is null"); - return nullptr; - } - - int paletteSize = get_palette_size(bitmap); - if (paletteSize == 0) { - ERROR("Expected GBitmapFormat[124]BitPalette, got: %d", digitsBitmap.GetFormat()); - return nullptr; - } - for (int i = 0; i < paletteSize; i++) { - if (palette[i] == color) { - DEBUG("Found color 0x%02X at index %d", color, i); - return &palette[i]; + int GetPaletteSize(GBitmapBase& bitmap){ + switch (bitmap.GetFormat()) { + case GBitmapFormat1BitPalette: return 2; + case GBitmapFormat2BitPalette: return 4; + case GBitmapFormat4BitPalette: return 16; + default: return 0; } } - ERROR("Color 0x%02X not found in the palette", color); - return nullptr; - } -} -void init_bitmaps() { - digitsBitmap = GOwnedBitmap::CreateWithResource(RESOURCE_ID_DIGITS); - auto size = digitsBitmap.GetBounds().size; - uint16_t w = size.w / 10; - uint16_t h = size.h; - for (int i = 0; i < 10; i++) { - digitBitmaps[i] = GOwnedBitmap::CreateSubBitmap(digitsBitmap, Pebble::GRect(i * w, 0, w, h)); + GColor* FindColor(GBitmapBase& bitmap, GColor color) { + GColor* palette = bitmap.GetPalette(); + if (palette == nullptr) { + ERROR("Palette is null"); + return nullptr; + } + + int paletteSize = GetPaletteSize(bitmap); + if (paletteSize == 0) { + ERROR("Expected GBitmapFormat[124]BitPalette, got: %d", digitsBitmap.GetFormat()); + return nullptr; + } + for (int i = 0; i < paletteSize; i++) { + if (palette[i] == color) { + DEBUG("Found color 0x%02X at index %d", color, i); + return &palette[i]; + } + } + ERROR("Color 0x%02X not found in the palette", color); + return nullptr; + } } - frameBitmap = GOwnedBitmap::CreateWithResource(RESOURCE_ID_FRAME); + void Init() { + digitsBitmap = GOwnedBitmap::CreateWithResource(RESOURCE_ID_DIGITS); + auto size = digitsBitmap.GetBounds().size; + uint16_t w = size.w / 10; + uint16_t h = size.h; + for (int i = 0; i < 10; i++) { + digitBitmaps[i] = GOwnedBitmap::CreateSubBitmap(digitsBitmap, Pebble::GRect(i * w, 0, w, h)); + } - digitColor = find_color(digitsBitmap, GColorBlack); - frameColor = find_color(frameBitmap, GColorBlack); -} + frameBitmap = GOwnedBitmap::CreateWithResource(RESOURCE_ID_FRAME); -void deinit_bitmaps() { - for (int i = 0; i < 10; i++) { - digitBitmaps[i] = GOwnedBitmap(); + digitColor = FindColor(digitsBitmap, GColorBlack); + frameColor = FindColor(frameBitmap, GColorBlack); } - digitsBitmap = GOwnedBitmap(); - frameBitmap = GOwnedBitmap(); - digitColor = nullptr; - frameColor = nullptr; -} -GBitmapBase& get_digit_bitmap(uint16_t digit) {return digitBitmaps[digit];} -GBitmapBase& get_frame_bitmap() {return frameBitmap;} - - -void set_digit_color(GColor color) { - if (digitColor != nullptr) *digitColor = color; -} -void set_frame_color(GColor color) { - if (frameColor != nullptr) *frameColor = color; -} - -void print_palette(GBitmapBase& bitmap) { - int paletteSize = get_palette_size(bitmap); - if (paletteSize == 0) { - ERROR("Expected GBitmapFormat[124]BitPalette, got: %d", digitsBitmap.GetFormat()); - return; + void DeInit() { + for (int i = 0; i < 10; i++) { + digitBitmaps[i] = GOwnedBitmap(); + } + digitsBitmap = GOwnedBitmap(); + frameBitmap = GOwnedBitmap(); + digitColor = nullptr; + frameColor = nullptr; } - DEBUG("%d colors:", paletteSize); - GColor8* palette = bitmap.GetPalette(); - for (int i = 0; i < paletteSize; i++) { - DEBUG("* 0x%02X = argb(%d,%d,%d,%d)", palette[i].argb, palette[i].a, palette[i].r, palette[i].g, palette[i].b); + + GBitmapBase& GetDigitBitmap(uint16_t digit) {return digitBitmaps[digit];} + GBitmapBase& GetFrameBitmap() {return frameBitmap;} + + + void SetDigitColor(GColor color) { + if (digitColor != nullptr) *digitColor = color; + } + void SetFrameColor(GColor color) { + if (frameColor != nullptr) *frameColor = color; + } + + void PrintPalette(GBitmapBase& bitmap) { + int paletteSize = GetPaletteSize(bitmap); + if (paletteSize == 0) { + ERROR("Expected GBitmapFormat[124]BitPalette, got: %d", digitsBitmap.GetFormat()); + return; + } + DEBUG("%d colors:", paletteSize); + GColor* palette = bitmap.GetPalette(); + for (int i = 0; i < paletteSize; i++) { + DEBUG("* 0x%02X = argb(%d,%d,%d,%d)", palette[i].argb, palette[i].a, palette[i].r, palette[i].g, palette[i].b); + } } } \ No newline at end of file diff --git a/src/c/bitmaps.hpp b/src/c/bitmaps.hpp index a4a7c31..c41ed74 100644 --- a/src/c/bitmaps.hpp +++ b/src/c/bitmaps.hpp @@ -2,10 +2,12 @@ #include -void init_bitmaps(); -void deinit_bitmaps(); -Pebble::GBitmapBase& get_digit_bitmap(uint16_t digit); -Pebble::GBitmapBase& get_frame_bitmap(); -void set_digit_color(GColor color); -void set_frame_color(GColor color); -void print_palette(Pebble::GBitmapBase& bitmap); \ No newline at end of file +namespace bitmaps { + void Init(); + void DeInit(); + Pebble::GBitmapBase& GetDigitBitmap(uint16_t digit); + Pebble::GBitmapBase& GetFrameBitmap(); + void SetDigitColor(GColor color); + void SetFrameColor(GColor color); + void PrintPalette(Pebble::GBitmapBase& bitmap); +} \ No newline at end of file diff --git a/src/c/digit_layer.cpp b/src/c/digit_layer.cpp index bc2af76..b8addca 100644 --- a/src/c/digit_layer.cpp +++ b/src/c/digit_layer.cpp @@ -1,12 +1,12 @@ #include "digit_layer.hpp" -#include "bitmaps.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 = get_digit_bitmap(digit); + Pebble::GBitmapBase& bitmap = bitmaps::GetDigitBitmap(digit); auto rect = bitmap.GetBounds(); rect.Align(bounds, GAlignCenter, false); ctx.DrawBitmapInRect(bitmap, rect); diff --git a/src/c/cryptic_clock.cpp b/src/c/main.cpp similarity index 74% rename from src/c/cryptic_clock.cpp rename to src/c/main.cpp index 9e5ce68..e55c761 100644 --- a/src/c/cryptic_clock.cpp +++ b/src/c/main.cpp @@ -3,6 +3,7 @@ #include "pebble_bitmap_layer.hpp" #include "digit_layer.hpp" #include "bitmaps.hpp" +#include "settings.hpp" using namespace Pebble; @@ -13,8 +14,6 @@ class MainWindow : public AppWindow, public TickTimer { Pebble::BitmapLayer frameLayer; protected: void OnLoad() { - init_bitmaps(); - auto bounds = this->GetBounds(); int16_t w2 = bounds.size.w / 2; int16_t h = bounds.size.h; @@ -32,12 +31,7 @@ protected: this->AddChild(this->digitLayers[i]); } - // print_palette(get_digit_bitmap(0)); - // print_palette(get_frame_bitmap()); - // set_frame_color(GColorPictonBlue); - // print_palette(get_digit_bitmap(0)); - // print_palette(get_frame_bitmap()); - this->frameLayer.SetBitmap(get_frame_bitmap()); + this->frameLayer.SetBitmap(bitmaps::GetFrameBitmap()); this->frameLayer.SetFrame(bounds); this->frameLayer.SetCompositingMode(GCompOpSet); this->AddChild(this->frameLayer); @@ -48,14 +42,6 @@ protected: TickTimerStart(MINUTE_UNIT); #endif UpdateTime(false); - - this->SetBackgroundColor(GColorBlack); - set_digit_color(GColorVividCerulean); - set_frame_color(GColorPictonBlue); - } - - void OnUnload() { - deinit_bitmaps(); } void UpdateTime(bool animate=true) { @@ -78,9 +64,35 @@ protected: } }; +namespace { + void ApplySettings() { + + Pebble::Window window = window_stack_get_top_window(); + window.SetBackgroundColor(settings::GetBackgroundColor()); + window.MarkDirty(); + + bitmaps::SetDigitColor(settings::GetDigitColor()); + bitmaps::SetFrameColor(settings::GetFrameColor()); + } + + void HandleMsg(DictionaryIterator *iter, void *context) { + settings::HandleMsg(iter); + ApplySettings(); + } +} + MAIN { + bitmaps::Init(); + settings::Load(); + app_message_register_inbox_received(HandleMsg); + app_message_open(64, 64); // inbox, outbox + MainWindow window; window.StackPush(true); + ApplySettings(); + app_event_loop(); + + bitmaps::DeInit(); return 0; } \ No newline at end of file diff --git a/src/c/settings.cpp b/src/c/settings.cpp new file mode 100644 index 0000000..08eabb1 --- /dev/null +++ b/src/c/settings.cpp @@ -0,0 +1,44 @@ +#include "settings.hpp" + +// Built-in GColorFromRGBA doesn't seem to work in C++11 :( +#undef GColorFromRGBA +GColor8 GColorFromRGBA(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) { + GColor8 res; + res.a = alpha >> 6; + res.r = red >> 6; + res.g = green >> 6; + res.b = blue >> 6; + return res; +} + +namespace settings { + namespace { + GColor backgroundColor = GColorWhite; + GColor digitColor = GColorBlack; + GColor frameColor = GColorBlack; + } + + void Save() { + persist_write_data(MESSAGE_KEY_BACKGROUND_COLOR, &backgroundColor, sizeof(GColor)); + persist_write_data(MESSAGE_KEY_DIGIT_COLOR, &digitColor, sizeof(GColor)); + persist_write_data(MESSAGE_KEY_FRAME_COLOR, &frameColor, sizeof(GColor)); + } + + void Load() { + persist_read_data(MESSAGE_KEY_BACKGROUND_COLOR, &backgroundColor, sizeof(GColor)); + persist_read_data(MESSAGE_KEY_DIGIT_COLOR, &digitColor, sizeof(GColor)); + persist_read_data(MESSAGE_KEY_FRAME_COLOR, &frameColor, sizeof(GColor)); + } + + void HandleMsg(DictionaryIterator *iter) { + + if (auto tuple = dict_find(iter, MESSAGE_KEY_BACKGROUND_COLOR)) backgroundColor = GColorFromHEX(tuple->value->int32); + if (auto tuple = dict_find(iter, MESSAGE_KEY_DIGIT_COLOR)) digitColor = GColorFromHEX(tuple->value->int32); + if (auto tuple = dict_find(iter, MESSAGE_KEY_FRAME_COLOR)) frameColor = GColorFromHEX(tuple->value->int32); + Save(); + } + + GColor GetBackgroundColor() {return backgroundColor; } + GColor GetDigitColor() {return digitColor; } + GColor GetFrameColor() {return frameColor; } +} \ No newline at end of file diff --git a/src/c/settings.hpp b/src/c/settings.hpp new file mode 100644 index 0000000..3b62e9c --- /dev/null +++ b/src/c/settings.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace settings { + void Save(); + void Load(); + void HandleMsg(DictionaryIterator *iter); + GColor GetBackgroundColor(); + GColor GetDigitColor(); + GColor GetFrameColor(); +} diff --git a/src/pkjs/config.js b/src/pkjs/config.js index e26008f..0a5a879 100644 --- a/src/pkjs/config.js +++ b/src/pkjs/config.js @@ -1,10 +1,36 @@ module.exports = [ { - "type": "heading", - "defaultValue": "Example Header Item" - }, - { - "type": "text", - "defaultValue": "Example text item." - } + type: "section", + items: [ + { + type: "heading", + defaultValue: "Colors", + }, + { + label: "Background Color", + messageKey: "BACKGROUND_COLOR", + type: "color", + defaultValue: "#FFFFFF", + allowGray: true, + }, + { + label: "Digit Color", + messageKey: "DIGIT_COLOR", + type: "color", + defaultValue: "#000000", + allowGray: true, + }, + { + label: "Frame Color", + messageKey: "FRAME_COLOR", + type: "color", + defaultValue: "#00000", + allowGray: true, + }, + ] + }, + { + type: "submit", + defaultValue: "Save Settings", + }, ]; \ No newline at end of file diff --git a/src/pkjs/index.js b/src/pkjs/index.js index df6395a..9a8e20b 100644 --- a/src/pkjs/index.js +++ b/src/pkjs/index.js @@ -1,4 +1,3 @@ var Clay = require('pebble-clay'); var clayConfig = require('./config'); -var clay = new Clay(clayConfig); -console.log('Loaded clay!'); \ No newline at end of file +var clay = new Clay(clayConfig); \ No newline at end of file