Added variable scrollbar length, max item count to 24, fixed an NVM bug

This commit is contained in:
Your Name
2026-09-19 17:33:59 +03:00
parent 717dbc3890
commit 67e456fdeb
15 changed files with 92 additions and 74 deletions
Binary file not shown.
+17 -17
View File
@@ -23,18 +23,6 @@
"projectType": "native",
"resources": {
"media": [
{
"file": "images/scroll_bar_half.png",
"name": "IMAGE_SCROLL_BAR_HALF",
"targetPlatforms": null,
"type": "bitmap"
},
{
"file": "images/scroll_bar_full.png",
"name": "IMAGE_SCROLL_BAR_FULL",
"targetPlatforms": null,
"type": "bitmap"
},
{
"file": "images/item_background.png",
"name": "IMAGE_ITEM_BACKGROUND",
@@ -87,15 +75,27 @@
"type": "font"
},
{
"file": "images/scroll_bar_third.png",
"name": "IMAGE_SCROLL_BAR_THIRD",
"file": "images/menu_icon.png",
"menuIcon": true,
"name": "IMAGE_MENU_ICON",
"targetPlatforms": null,
"type": "bitmap"
},
{
"file": "images/menu_icon.png",
"menuIcon": true,
"name": "IMAGE_MENU_ICON",
"file": "images/scroll_bar_top.png",
"name": "IMAGE_SCROLL_BAR_TOP",
"targetPlatforms": null,
"type": "bitmap"
},
{
"file": "images/scroll_bar_middle.png",
"name": "IMAGE_SCROLL_BAR_MIDDLE",
"targetPlatforms": null,
"type": "bitmap"
},
{
"file": "images/scroll_bar_bottom.png",
"name": "IMAGE_SCROLL_BAR_BOTTOM",
"targetPlatforms": null,
"type": "bitmap"
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 97 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 B

+23 -7
View File
@@ -5,21 +5,37 @@
#include "src/c/scrolllayer.h"
static const uint32_t ITEMS_NVM_KEYS[NUM_KEYS] = {1, 2, 3, 4, 5, 6};
item_list_t s_items;
// ------------ NVM ------------
void items_nvm_load() {
if (persist_exists(ITEMS_NVM_KEY)) {
persist_read_data(ITEMS_NVM_KEY, s_items.items, sizeof(s_items.items));
}
else {
memset(s_items.items, 0, sizeof(s_items.items));
}
memset(s_items.items, 0, sizeof(s_items.items));
for (int chunk = 0; chunk < NUM_KEYS; chunk++) {
int start = chunk * ITEMS_PER_KEY;
persist_read_data(
ITEMS_NVM_KEYS[chunk],
&s_items.items[start],
ITEMS_PER_KEY * sizeof(item_t)
);
}
}
void items_nvm_save() {
persist_write_data(ITEMS_NVM_KEY, s_items.items, sizeof(s_items.items));
for (int chunk = 0; chunk < NUM_KEYS; chunk++) {
int start = chunk * ITEMS_PER_KEY;
int count = ITEMS_PER_KEY;
persist_write_data(
ITEMS_NVM_KEYS[chunk],
&s_items.items[start],
count * sizeof(item_t)
);
}
}
// ------------ ITEMS ------------
+3 -2
View File
@@ -2,8 +2,9 @@
// NVM
#define ITEMS_NVM_KEY 1
#define MAX_ITEMS 9
#define ITEMS_PER_KEY 4
#define NUM_KEYS 6
#define MAX_ITEMS 24
#define MAX_CHARACTERS 41
+47 -47
View File
@@ -12,10 +12,9 @@ static GBitmap *s_plus_bitmap;
static GBitmap *s_minus_bitmap;
static GBitmap *s_background_plus_pressed_bitmap;
static GBitmap *s_background_minus_pressed_bitmap;
static GBitmap *s_scroll_bar_current_bitmap;
static GBitmap *s_scroll_bar_full_bitmap;
static GBitmap *s_scroll_bar_half_bitmap;
static GBitmap *s_scroll_bar_third_bitmap;
static GBitmap *s_scroll_bar_top_bitmap;
static GBitmap *s_scroll_bar_middle_bitmap;
static GBitmap *s_scroll_bar_bottom_bitmap;
// TOUCH BUTTONS
static GRect item_1_plus_button;
@@ -47,7 +46,7 @@ static void scroll_up(ClickRecognizerRef recognizer, void *context) {
// Move scroll bar
GRect scroll_frame = layer_get_frame(s_scroll_bar_layer);
int scroll_frame_y = scroll_frame.origin.y - scroll_frame.size.h;
if (scroll_frame_y < SCROLL_BAR_ORIGIN_HEIGHT) {
if ((scroll_frame_y < SCROLL_BAR_ORIGIN_HEIGHT) || (s_scroll_page == 0)) {
scroll_frame_y = SCROLL_BAR_ORIGIN_HEIGHT;
}
scroll_frame.origin.y = scroll_frame_y;
@@ -74,10 +73,12 @@ static void scroll_down(ClickRecognizerRef recognizer, void *context) {
s_scroll_page = ((MAX_ITEMS / MAX_UI_ITEMS) - 1);
}
APP_LOG(APP_LOG_LEVEL_DEBUG, "Page %d of %d", s_scroll_page, (s_scroll_items.page_count - 1));
// Move scroll bar
GRect scroll_frame = layer_get_frame(s_scroll_bar_layer);
int scroll_frame_y = scroll_frame.origin.y + scroll_frame.size.h;
if (scroll_frame_y > (SCROLL_BAR_ORIGIN_HEIGHT_END - scroll_frame.size.h)) {
if ((scroll_frame_y > (SCROLL_BAR_ORIGIN_HEIGHT_END - scroll_frame.size.h)) || (s_scroll_page == (s_scroll_items.page_count - 1))) {
scroll_frame_y = (SCROLL_BAR_ORIGIN_HEIGHT_END - scroll_frame.size.h);
}
scroll_frame.origin.y = scroll_frame_y;
@@ -123,8 +124,22 @@ static void bar_draw(Layer *layer, GContext *ctx) {
}
static void scroll_bar_draw(Layer *layer, GContext *ctx) {
GRect bounds = gbitmap_get_bounds(s_scroll_bar_current_bitmap);
graphics_draw_bitmap_in_rect(ctx, s_scroll_bar_current_bitmap, GRect(0, 0, bounds.size.w, bounds.size.h));
GRect bounds = layer_get_bounds(s_scroll_bar_layer);
// TOP
GRect top_bounds = bounds;
top_bounds.size.h = 2;
graphics_draw_bitmap_in_rect(ctx, s_scroll_bar_top_bitmap, GRect(0, 0, top_bounds.size.w, top_bounds.size.h));
// MIDDLE
GRect middle_bounds = bounds;
middle_bounds.size.h -= 4;
graphics_draw_bitmap_in_rect(ctx, s_scroll_bar_middle_bitmap, GRect(0, 2, middle_bounds.size.w, middle_bounds.size.h));
// BOTTOM
GRect bottom_bounds = bounds;
bottom_bounds.size.h = 2;
graphics_draw_bitmap_in_rect(ctx, s_scroll_bar_bottom_bitmap, GRect(0, (bounds.size.h - 2), bottom_bounds.size.w, bottom_bounds.size.h));
}
static void fill_percentage_buffer(scroll_items_t *items, int element, int total, float percentage) {
@@ -243,26 +258,25 @@ void scroll_layer_remove_items() {
void scroll_layer_update_scrollbar() {
GRect scroll_box = layer_get_bounds(s_scroll_bar_layer);
GRect scroll_frame = layer_get_frame(s_scroll_bar_layer);
if (s_scroll_items.page_count == 2) {
scroll_box.size.h = 82;
scroll_frame.size.h = 82;
s_scroll_bar_current_bitmap = s_scroll_bar_half_bitmap;
}
else if (s_scroll_items.page_count == 3) {
scroll_box.size.h = 52;
scroll_frame.size.h = 52;
s_scroll_bar_current_bitmap = s_scroll_bar_third_bitmap;
}
else {
scroll_box.size.h = 160;
scroll_frame.size.h = 160;
s_scroll_bar_current_bitmap = s_scroll_bar_full_bitmap;
}
scroll_box.size.h = SCROLL_BAR_HEAIGHT / s_scroll_items.page_count;
scroll_frame.size.h = SCROLL_BAR_HEAIGHT / s_scroll_items.page_count;
layer_set_bounds(s_scroll_bar_layer, scroll_box);
// Scroll position update
s_scroll_y = 0;
s_scroll_page = 0;
// Move scroll bar
int scroll_frame_y = SCROLL_BAR_ORIGIN_HEIGHT;
scroll_frame.origin.y = scroll_frame_y;
layer_set_frame(s_scroll_bar_layer, scroll_frame);
layer_mark_dirty(s_scroll_bar_layer);
scroll_layer_set_content_offset(
s_scroll_layer,
GPoint(0, -s_scroll_y),
true
);
}
static void touch_update_element(scroll_items_t *items, int element, int *inc_value_ptr) {
@@ -436,9 +450,9 @@ void scrolllayer_init(Layer *parent, Window *window) {
s_minus_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_MINUS_BUTTON);
s_background_plus_pressed_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND_PLUS_PRESSED);
s_background_minus_pressed_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND_MINUS_PRESSED);
s_scroll_bar_full_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SCROLL_BAR_FULL);
s_scroll_bar_half_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SCROLL_BAR_HALF);
s_scroll_bar_third_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SCROLL_BAR_THIRD);
s_scroll_bar_top_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SCROLL_BAR_TOP);
s_scroll_bar_middle_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SCROLL_BAR_MIDDLE);
s_scroll_bar_bottom_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SCROLL_BAR_BOTTOM);
// Create ScrollLayer
s_scroll_layer = scroll_layer_create(bounds);
@@ -463,22 +477,8 @@ void scrolllayer_init(Layer *parent, Window *window) {
// Add scroll bar
GRect scroll_box = SCROLL_BAR_FRAME_RECT;
if (s_scroll_items.page_count == 2) {
scroll_box.size.h = 82;
s_scroll_bar_layer = layer_create(scroll_box);
s_scroll_bar_current_bitmap = s_scroll_bar_half_bitmap;
}
else if (s_scroll_items.page_count == 3) {
scroll_box.size.h = 52;
s_scroll_bar_layer = layer_create(scroll_box);
s_scroll_bar_current_bitmap = s_scroll_bar_third_bitmap;
}
else {
scroll_box.size.h = 160;
s_scroll_bar_layer = layer_create(scroll_box);
s_scroll_bar_current_bitmap = s_scroll_bar_full_bitmap;
}
scroll_box.size.h = SCROLL_BAR_HEAIGHT / s_scroll_items.page_count;
s_scroll_bar_layer = layer_create(scroll_box);
layer_set_update_proc(s_scroll_bar_layer, scroll_bar_draw);
layer_add_child(parent, s_scroll_bar_layer);
@@ -503,9 +503,9 @@ void scrolllayer_deinit() {
gbitmap_destroy(s_minus_bitmap);
gbitmap_destroy(s_background_plus_pressed_bitmap);
gbitmap_destroy(s_background_minus_pressed_bitmap);
gbitmap_destroy(s_scroll_bar_full_bitmap);
gbitmap_destroy(s_scroll_bar_half_bitmap);
gbitmap_destroy(s_scroll_bar_third_bitmap);
gbitmap_destroy(s_scroll_bar_top_bitmap);
gbitmap_destroy(s_scroll_bar_middle_bitmap);
gbitmap_destroy(s_scroll_bar_bottom_bitmap);
// Destroy scroll bar
layer_destroy(s_scroll_bar_layer);
+1
View File
@@ -9,6 +9,7 @@
// SCROLLLAYER POSITION
#define SCROLLLAYER_FRAME_RECT GRect(5, 30, 174, 168)
#define SCROLL_BAR_FRAME_RECT GRect(180, 34, 10, 160)
#define SCROLL_BAR_HEAIGHT 160
#define SCROLL_BAR_ORIGIN_HEIGHT 34
#define SCROLL_BAR_ORIGIN_HEIGHT_END 34 + 160
#define PERCENTAGE_BAR_WIDTH 112
+1 -1
View File
@@ -5,7 +5,7 @@ module.exports = [
},
{
"type": "text",
"defaultValue": "Add/remove/update up to 9 items. Name character limit is 40. If more than 10 characters, font goes smaller. If numbers go higher than 1000, use percentage."
"defaultValue": "Add/remove/update up to 24 items. Name character limit is 40. If more than 10 characters, font goes smaller. If numbers go higher than 1000, use percentage."
},
{