Advertisement
Guest User

Untitled

a guest
Jan 31st, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <lvgl.h>
  4. #include <Arduino.h>
  5.  
  6. struct CounterData {
  7.     lv_obj_t* counter_label;
  8.     int count;
  9. };
  10.  
  11. static void lv_spinbox_increment_event_cb(lv_event_t * e)
  12. {
  13.     lv_event_code_t code = lv_event_get_code(e);
  14.     lv_obj_t* button = lv_event_get_target(e);
  15.     auto counterData = (CounterData*)lv_obj_get_user_data(button);
  16.  
  17.     if(code == LV_EVENT_SHORT_CLICKED || code  == LV_EVENT_LONG_PRESSED_REPEAT) {
  18.         auto label = counterData->counter_label;
  19.         counterData->count++;
  20.         lv_label_set_text(label, String(counterData->count).c_str());
  21.     }
  22. }
  23.  
  24. static void lv_spinbox_decrement_event_cb(lv_event_t * e)
  25. {
  26.     lv_event_code_t code = lv_event_get_code(e);
  27.     lv_obj_t* button = lv_event_get_target(e);
  28.     auto counterData = (CounterData*)lv_obj_get_user_data(button);
  29.  
  30.     if(code == LV_EVENT_SHORT_CLICKED || code == LV_EVENT_LONG_PRESSED_REPEAT) {
  31.         auto label = counterData->counter_label;
  32.         counterData->count--;
  33.         lv_label_set_text(label, String(counterData->count).c_str());
  34.     }
  35. }
  36.  
  37. lv_obj_t* createCounter(int initialValue, const char* playerName, lv_obj_t* parent)
  38. {
  39.     auto root = lv_obj_create(parent);
  40.  
  41.     static lv_coord_t col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
  42.     static lv_coord_t row_dsc[] = {LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
  43.  
  44.     lv_obj_set_grid_dsc_array(root, col_dsc, row_dsc);
  45.  
  46.     //Center content with life counter and player name
  47.     auto centerContent = lv_obj_create(root);
  48.     lv_obj_clear_flag(centerContent, LV_OBJ_FLAG_SCROLLABLE);
  49.     lv_obj_set_grid_cell(centerContent, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 0, 1); //grid 1, 0
  50.     lv_obj_set_style_border_width(centerContent, 0, LV_PART_MAIN);
  51.  
  52.     //Life counter label
  53.     auto label = lv_label_create(centerContent);
  54.     lv_label_set_text(label, String(initialValue).c_str());
  55.     lv_obj_set_style_text_font(label, &lv_font_montserrat_48, 0);
  56.     lv_obj_clear_flag(label, LV_OBJ_FLAG_CLICKABLE);
  57.     lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
  58.     lv_obj_center(label);
  59.  
  60.     //Associated counter data
  61.     CounterData* counterData = new CounterData;
  62.     counterData->count = initialValue;
  63.     counterData->counter_label = label;
  64.  
  65.     //Player name label
  66.     label = lv_label_create(centerContent);
  67.     lv_label_set_text(label, playerName);
  68.     lv_obj_set_style_text_font(label, &lv_font_montserrat_18, 0);
  69.     lv_obj_clear_flag(label, LV_OBJ_FLAG_CLICKABLE);
  70.     lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
  71.     lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0);
  72.  
  73.     //Right increment button
  74.     lv_obj_t * btn = lv_btn_create(root);
  75.     lv_obj_set_style_bg_img_src(btn, LV_SYMBOL_PLUS, 0);
  76.     lv_obj_add_event_cb(btn, lv_spinbox_increment_event_cb, LV_EVENT_ALL,  NULL);
  77.     lv_obj_set_user_data(btn, counterData);
  78.     lv_obj_set_grid_cell(btn, LV_GRID_ALIGN_STRETCH, 2, 1, LV_GRID_ALIGN_STRETCH, 0, 1); //grid 2, 0
  79.  
  80.     //Left decrement button
  81.     btn = lv_btn_create(root);
  82.     lv_obj_set_style_bg_img_src(btn, LV_SYMBOL_MINUS, 0);
  83.     lv_obj_add_event_cb(btn, lv_spinbox_decrement_event_cb, LV_EVENT_ALL, NULL);
  84.     lv_obj_set_user_data(btn, counterData);
  85.     lv_obj_set_grid_cell(btn, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 0, 1); //grid 0, 0
  86.  
  87.     return root;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement