acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
lv_example_list_2.c (5400B)
1 #include <stdlib.h> 2 3 4 #include "../../lv_examples.h" 5 #if LV_USE_LIST && LV_BUILD_EXAMPLES 6 7 static lv_obj_t * list1; 8 static lv_obj_t * list2; 9 10 static lv_obj_t * currentButton = NULL; 11 12 static void event_handler(lv_event_t * e) 13 { 14 lv_event_code_t code = lv_event_get_code(e); 15 lv_obj_t * obj = lv_event_get_target(e); 16 if(code == LV_EVENT_CLICKED) { 17 LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list1, obj)); 18 19 if(currentButton == obj) { 20 currentButton = NULL; 21 } 22 else { 23 currentButton = obj; 24 } 25 lv_obj_t * parent = lv_obj_get_parent(obj); 26 uint32_t i; 27 for(i = 0; i < lv_obj_get_child_cnt(parent); i++) { 28 lv_obj_t * child = lv_obj_get_child(parent, i); 29 if(child == currentButton) { 30 lv_obj_add_state(child, LV_STATE_CHECKED); 31 } 32 else { 33 lv_obj_clear_state(child, LV_STATE_CHECKED); 34 } 35 } 36 } 37 } 38 39 static void event_handler_top(lv_event_t * e) 40 { 41 lv_event_code_t code = lv_event_get_code(e); 42 if(code == LV_EVENT_CLICKED) { 43 if(currentButton == NULL) return; 44 lv_obj_move_background(currentButton); 45 lv_obj_scroll_to_view(currentButton, LV_ANIM_ON); 46 } 47 } 48 49 static void event_handler_up(lv_event_t * e) 50 { 51 lv_event_code_t code = lv_event_get_code(e); 52 if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) { 53 if(currentButton == NULL) return; 54 uint32_t index = lv_obj_get_index(currentButton); 55 if(index <= 0) return; 56 lv_obj_move_to_index(currentButton, index - 1); 57 lv_obj_scroll_to_view(currentButton, LV_ANIM_ON); 58 } 59 } 60 61 static void event_handler_center(lv_event_t * e) 62 { 63 const lv_event_code_t code = lv_event_get_code(e); 64 if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) { 65 if(currentButton == NULL) return; 66 67 lv_obj_t * parent = lv_obj_get_parent(currentButton); 68 const uint32_t pos = lv_obj_get_child_cnt(parent) / 2; 69 70 lv_obj_move_to_index(currentButton, pos); 71 72 lv_obj_scroll_to_view(currentButton, LV_ANIM_ON); 73 } 74 } 75 76 static void event_handler_dn(lv_event_t * e) 77 { 78 const lv_event_code_t code = lv_event_get_code(e); 79 if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) { 80 if(currentButton == NULL) return; 81 const uint32_t index = lv_obj_get_index(currentButton); 82 83 lv_obj_move_to_index(currentButton, index + 1); 84 lv_obj_scroll_to_view(currentButton, LV_ANIM_ON); 85 } 86 } 87 88 static void event_handler_bottom(lv_event_t * e) 89 { 90 const lv_event_code_t code = lv_event_get_code(e); 91 if(code == LV_EVENT_CLICKED) { 92 if(currentButton == NULL) return; 93 lv_obj_move_foreground(currentButton); 94 lv_obj_scroll_to_view(currentButton, LV_ANIM_ON); 95 } 96 } 97 98 static void event_handler_swap(lv_event_t * e) 99 { 100 const lv_event_code_t code = lv_event_get_code(e); 101 // lv_obj_t* obj = lv_event_get_target(e); 102 if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) { 103 uint32_t cnt = lv_obj_get_child_cnt(list1); 104 for(int i = 0; i < 100; i++) 105 if(cnt > 1) { 106 lv_obj_t * obj = lv_obj_get_child(list1, rand() % cnt); 107 lv_obj_move_to_index(obj, rand() % cnt); 108 if(currentButton != NULL) { 109 lv_obj_scroll_to_view(currentButton, LV_ANIM_ON); 110 } 111 } 112 } 113 } 114 115 void lv_example_list_2(void) 116 { 117 /*Create a list*/ 118 list1 = lv_list_create(lv_scr_act()); 119 lv_obj_set_size(list1, lv_pct(60), lv_pct(100)); 120 lv_obj_set_style_pad_row(list1, 5, 0); 121 122 /*Add buttons to the list*/ 123 lv_obj_t * btn; 124 int i; 125 for(i = 0; i < 15; i++) { 126 btn = lv_btn_create(list1); 127 lv_obj_set_width(btn, lv_pct(50)); 128 lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL); 129 130 lv_obj_t * lab = lv_label_create(btn); 131 lv_label_set_text_fmt(lab, "Item %d", i); 132 } 133 134 /*Select the first button by default*/ 135 currentButton = lv_obj_get_child(list1, 0); 136 lv_obj_add_state(currentButton, LV_STATE_CHECKED); 137 138 /*Create a second list with up and down buttons*/ 139 list2 = lv_list_create(lv_scr_act()); 140 lv_obj_set_size(list2, lv_pct(40), lv_pct(100)); 141 lv_obj_align(list2, LV_ALIGN_TOP_RIGHT, 0, 0); 142 lv_obj_set_flex_flow(list2, LV_FLEX_FLOW_COLUMN); 143 144 btn = lv_list_add_btn(list2, NULL, "Top"); 145 lv_obj_add_event_cb(btn, event_handler_top, LV_EVENT_ALL, NULL); 146 lv_group_remove_obj(btn); 147 148 btn = lv_list_add_btn(list2, LV_SYMBOL_UP, "Up"); 149 lv_obj_add_event_cb(btn, event_handler_up, LV_EVENT_ALL, NULL); 150 lv_group_remove_obj(btn); 151 152 btn = lv_list_add_btn(list2, LV_SYMBOL_LEFT, "Center"); 153 lv_obj_add_event_cb(btn, event_handler_center, LV_EVENT_ALL, NULL); 154 lv_group_remove_obj(btn); 155 156 btn = lv_list_add_btn(list2, LV_SYMBOL_DOWN, "Down"); 157 lv_obj_add_event_cb(btn, event_handler_dn, LV_EVENT_ALL, NULL); 158 lv_group_remove_obj(btn); 159 160 btn = lv_list_add_btn(list2, NULL, "Bottom"); 161 lv_obj_add_event_cb(btn, event_handler_bottom, LV_EVENT_ALL, NULL); 162 lv_group_remove_obj(btn); 163 164 btn = lv_list_add_btn(list2, LV_SYMBOL_SHUFFLE, "Shuffle"); 165 lv_obj_add_event_cb(btn, event_handler_swap, LV_EVENT_ALL, NULL); 166 lv_group_remove_obj(btn); 167 } 168 169 #endif