1 #include "../../lv_examples.h"
2 #if LV_USE_OBSERVER && LV_USE_SLIDER && LV_USE_LABEL && LV_USE_ROLLER && LV_USE_DROPDOWN && LV_FONT_MONTSERRAT_30 && LV_BUILD_EXAMPLES
3
4 static void cont_observer_cb(lv_observer_t * observer, lv_subject_t * subject);
5 static void btn_create(lv_obj_t * parent, const char * text);
6 static void btn_click_event_cb(lv_event_t * e);
7 static void btn_observer_cb(lv_observer_t * observer, lv_subject_t * subject);
8 static void indicator_observer_cb(lv_observer_t * observer, lv_subject_t * subject);
9
10 static lv_subject_t current_tab_subject;
11 static lv_subject_t slider_subject[4];
12 static lv_subject_t dropdown_subject[3];
13 static lv_subject_t roller_subject[2];
14
lv_example_observer_4(void)15 void lv_example_observer_4(void)
16 {
17 lv_subject_init_int(¤t_tab_subject, 0);
18 lv_subject_init_int(&slider_subject[0], 0);
19 lv_subject_init_int(&slider_subject[1], 0);
20 lv_subject_init_int(&slider_subject[2], 0);
21 lv_subject_init_int(&slider_subject[3], 0);
22 lv_subject_init_int(&dropdown_subject[0], 0);
23 lv_subject_init_int(&dropdown_subject[1], 0);
24 lv_subject_init_int(&dropdown_subject[2], 0);
25 lv_subject_init_int(&roller_subject[0], 0);
26 lv_subject_init_int(&roller_subject[1], 0);
27
28 lv_obj_t * main_cont = lv_obj_create(lv_screen_active());
29 lv_obj_remove_style_all(main_cont);
30 lv_obj_set_size(main_cont, lv_pct(100), lv_pct(100));
31 lv_obj_set_style_pad_all(main_cont, 0, 0);
32 lv_obj_set_flex_flow(main_cont, LV_FLEX_FLOW_COLUMN);
33
34 lv_obj_t * cont = lv_obj_create(main_cont);
35 lv_obj_remove_style_all(cont);
36 lv_obj_set_flex_grow(cont, 1);
37 lv_obj_set_style_pad_all(cont, 8, 0);
38 lv_obj_set_width(cont, lv_pct(100));
39 lv_subject_add_observer_obj(¤t_tab_subject, cont_observer_cb, cont, NULL);
40 lv_obj_set_scroll_dir(cont, LV_DIR_VER);
41
42 lv_obj_t * footer = lv_obj_create(main_cont);
43 lv_obj_remove_style_all(footer);
44 lv_obj_set_style_pad_column(footer, 8, 0);
45 lv_obj_set_style_pad_all(footer, 8, 0);
46 lv_obj_set_flex_flow(footer, LV_FLEX_FLOW_ROW);
47 lv_obj_set_size(footer, lv_pct(100), 60);
48 lv_obj_align(footer, LV_ALIGN_BOTTOM_MID, 0, 0);
49
50 btn_create(footer, "First");
51 btn_create(footer, "Second");
52 btn_create(footer, "Third");
53
54 lv_obj_t * indicator = lv_obj_create(footer);
55 lv_obj_remove_style(indicator, NULL, 0);
56 lv_obj_set_style_bg_opa(indicator, LV_OPA_40, 0);
57 lv_subject_add_observer_obj(¤t_tab_subject, indicator_observer_cb, indicator, NULL);
58 lv_obj_set_height(indicator, 10);
59 lv_obj_align(indicator, LV_ALIGN_BOTTOM_LEFT, 0, 0);
60 lv_obj_add_flag(indicator, LV_OBJ_FLAG_IGNORE_LAYOUT);
61
62 /*Be sure the indicator has the correct size*/
63 lv_obj_update_layout(indicator);
64 lv_subject_notify(¤t_tab_subject);
65 }
66
anim_get_x_cb(lv_anim_t * a)67 static int32_t anim_get_x_cb(lv_anim_t * a)
68 {
69 return lv_obj_get_x_aligned(a->var);
70 }
71
anim_set_x_cb(void * obj,int32_t v)72 static void anim_set_x_cb(void * obj, int32_t v)
73 {
74 lv_obj_set_x(obj, v);
75 }
76
cont_observer_cb(lv_observer_t * observer,lv_subject_t * subject)77 static void cont_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
78 {
79 int32_t prev_v = lv_subject_get_previous_int(subject);
80 int32_t cur_v = lv_subject_get_int(subject);
81 lv_obj_t * cont = lv_observer_get_target(observer);
82
83 /*Animate out the previous content*/
84 lv_anim_t a;
85 lv_anim_init(&a);
86 lv_anim_set_duration(&a, 300);
87 lv_anim_set_path_cb(&a, lv_anim_path_ease_in_out);
88 lv_anim_set_exec_cb(&a, anim_set_x_cb);
89 lv_anim_set_get_value_cb(&a, anim_get_x_cb);
90 lv_anim_set_completed_cb(&a, lv_obj_delete_anim_completed_cb);
91
92 uint32_t i;
93 uint32_t delay = 0;
94 uint32_t child_cnt_prev = lv_obj_get_child_count(cont);
95 for(i = 0; i < child_cnt_prev; i++) {
96 lv_obj_t * child = lv_obj_get_child(cont, i);
97 lv_anim_set_var(&a, child);
98 if(prev_v < cur_v) {
99 lv_anim_set_values(&a, 0, -20);
100 }
101 else {
102 lv_anim_set_values(&a, 0, 20);
103 }
104 lv_anim_set_delay(&a, delay);
105 lv_anim_start(&a);
106 lv_obj_fade_out(child, 200, delay);
107 delay += 50;
108 }
109
110 /*Create the widgets according to the current value*/
111 if(cur_v == 0) {
112 for(i = 0; i < 4; i++) {
113 lv_obj_t * slider = lv_slider_create(cont);
114 lv_slider_bind_value(slider, &slider_subject[i]);
115 lv_obj_align(slider, LV_ALIGN_TOP_MID, 0, 10 + i * 30);
116 }
117 }
118 if(cur_v == 1) {
119 for(i = 0; i < 3; i++) {
120 lv_obj_t * dropdown = lv_dropdown_create(cont);
121 lv_dropdown_bind_value(dropdown, &dropdown_subject[i]);
122 lv_obj_align(dropdown, LV_ALIGN_TOP_MID, 0, i * 50);
123 }
124 }
125 if(cur_v == 2) {
126 for(i = 0; i < 2; i++) {
127 lv_obj_t * roller = lv_roller_create(cont);
128 lv_roller_bind_value(roller, &roller_subject[i]);
129 lv_obj_align(roller, LV_ALIGN_CENTER, - 80 + i * 160, 0);
130 }
131 }
132
133 /*Animate in the new widgets*/
134 lv_anim_set_completed_cb(&a, NULL);
135 for(i = child_cnt_prev; i < lv_obj_get_child_count(cont); i++) {
136 lv_obj_t * child = lv_obj_get_child(cont, i);
137 lv_anim_set_var(&a, child);
138 if(prev_v < cur_v) {
139 lv_anim_set_values(&a, 20, 0);
140 }
141 else {
142 lv_anim_set_values(&a, -20, -0);
143 }
144 lv_anim_set_delay(&a, delay);
145 lv_anim_start(&a);
146 lv_obj_fade_in(child, 200, delay);
147 delay += 50;
148 }
149
150 }
151
btn_create(lv_obj_t * parent,const char * text)152 static void btn_create(lv_obj_t * parent, const char * text)
153 {
154 lv_obj_t * btn = lv_button_create(parent);
155 lv_obj_set_flex_grow(btn, 1);
156 lv_obj_set_height(btn, lv_pct(100));
157 lv_obj_set_style_radius(btn, 0, 0);
158 lv_subject_add_observer_obj(¤t_tab_subject, btn_observer_cb, btn, NULL);
159 lv_obj_add_event_cb(btn, btn_click_event_cb, LV_EVENT_CLICKED, NULL);
160
161 lv_obj_t * label = lv_label_create(btn);
162 lv_label_set_text(label, text);
163 lv_obj_center(label);
164 }
165
btn_click_event_cb(lv_event_t * e)166 static void btn_click_event_cb(lv_event_t * e)
167 {
168 lv_obj_t * btn = lv_event_get_target(e);
169 uint32_t idx = lv_obj_get_index(btn);
170 lv_subject_set_int(¤t_tab_subject, idx);
171 }
172
btn_observer_cb(lv_observer_t * observer,lv_subject_t * subject)173 static void btn_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
174 {
175 int32_t prev_v = lv_subject_get_previous_int(subject);
176 int32_t cur_v = lv_subject_get_int(subject);
177
178 lv_obj_t * btn = lv_observer_get_target(observer);
179 int32_t idx = (int32_t)lv_obj_get_index(btn);
180
181 if(idx == prev_v) lv_obj_remove_state(btn, LV_STATE_CHECKED);
182 if(idx == cur_v) lv_obj_add_state(btn, LV_STATE_CHECKED);
183 }
184
indicator_observer_cb(lv_observer_t * observer,lv_subject_t * subject)185 static void indicator_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
186 {
187 int32_t cur_v = lv_subject_get_int(subject);
188 lv_obj_t * indicator = lv_observer_get_target(observer);
189
190 lv_obj_t * footer = lv_obj_get_parent(indicator);
191 lv_obj_t * btn_act = lv_obj_get_child(footer, cur_v);
192 lv_obj_set_width(indicator, lv_obj_get_width(btn_act));
193
194 lv_anim_t a;
195 lv_anim_init(&a);
196 lv_anim_set_exec_cb(&a, anim_set_x_cb);
197 lv_anim_set_duration(&a, 300);
198 lv_anim_set_path_cb(&a, lv_anim_path_ease_in_out);
199 lv_anim_set_var(&a, indicator);
200 lv_anim_set_values(&a, lv_obj_get_x(indicator), lv_obj_get_x(btn_act));
201 lv_anim_start(&a);
202
203 }
204
205 #endif
206