diff options
| author | jacqueline <me@jacqueline.id.au> | 2024-06-12 17:54:40 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2024-06-12 17:54:40 +1000 |
| commit | 64bd9053a25297f7a442ca831c7da5b44bd33f84 (patch) | |
| tree | a90c6cad25a12028302ab1a5334510fba0229bae /lib/lvgl/examples/widgets/button | |
| parent | 611176ed667c4ed7ee9f609e958f9404f4aee91d (diff) | |
| download | tangara-fw-64bd9053a25297f7a442ca831c7da5b44bd33f84.tar.gz | |
Update LVGL to v9.1.0
Diffstat (limited to 'lib/lvgl/examples/widgets/button')
| -rw-r--r-- | lib/lvgl/examples/widgets/button/index.rst | 20 | ||||
| -rw-r--r-- | lib/lvgl/examples/widgets/button/lv_example_button_1.c | 40 | ||||
| -rw-r--r-- | lib/lvgl/examples/widgets/button/lv_example_button_2.c | 65 | ||||
| -rw-r--r-- | lib/lvgl/examples/widgets/button/lv_example_button_3.c | 45 |
4 files changed, 170 insertions, 0 deletions
diff --git a/lib/lvgl/examples/widgets/button/index.rst b/lib/lvgl/examples/widgets/button/index.rst new file mode 100644 index 00000000..c8b9cda4 --- /dev/null +++ b/lib/lvgl/examples/widgets/button/index.rst @@ -0,0 +1,20 @@ + +Simple Buttons +-------------- + +.. lv_example:: widgets/button/lv_example_button_1 + :language: c + + +Styling buttons +--------------- + +.. lv_example:: widgets/button/lv_example_button_2 + :language: c + +Gummy button +------------ + +.. lv_example:: widgets/button/lv_example_button_3 + :language: c + diff --git a/lib/lvgl/examples/widgets/button/lv_example_button_1.c b/lib/lvgl/examples/widgets/button/lv_example_button_1.c new file mode 100644 index 00000000..eae9c9da --- /dev/null +++ b/lib/lvgl/examples/widgets/button/lv_example_button_1.c @@ -0,0 +1,40 @@ +#include "../../lv_examples.h" +#if LV_USE_BUTTON && LV_BUILD_EXAMPLES + +static void event_handler(lv_event_t * e) +{ + lv_event_code_t code = lv_event_get_code(e); + + if(code == LV_EVENT_CLICKED) { + LV_LOG_USER("Clicked"); + } + else if(code == LV_EVENT_VALUE_CHANGED) { + LV_LOG_USER("Toggled"); + } +} + +void lv_example_button_1(void) +{ + lv_obj_t * label; + + lv_obj_t * btn1 = lv_button_create(lv_screen_active()); + lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL); + lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40); + lv_obj_remove_flag(btn1, LV_OBJ_FLAG_PRESS_LOCK); + + label = lv_label_create(btn1); + lv_label_set_text(label, "Button"); + lv_obj_center(label); + + lv_obj_t * btn2 = lv_button_create(lv_screen_active()); + lv_obj_add_event_cb(btn2, event_handler, LV_EVENT_ALL, NULL); + lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40); + lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE); + lv_obj_set_height(btn2, LV_SIZE_CONTENT); + + label = lv_label_create(btn2); + lv_label_set_text(label, "Toggle"); + lv_obj_center(label); + +} +#endif diff --git a/lib/lvgl/examples/widgets/button/lv_example_button_2.c b/lib/lvgl/examples/widgets/button/lv_example_button_2.c new file mode 100644 index 00000000..441901e5 --- /dev/null +++ b/lib/lvgl/examples/widgets/button/lv_example_button_2.c @@ -0,0 +1,65 @@ +#include "../../lv_examples.h" +#if LV_USE_BUTTON && LV_BUILD_EXAMPLES + +/** + * Style a button from scratch + */ +void lv_example_button_2(void) +{ + /*Init the style for the default state*/ + static lv_style_t style; + lv_style_init(&style); + + lv_style_set_radius(&style, 3); + + lv_style_set_bg_opa(&style, LV_OPA_100); + lv_style_set_bg_color(&style, lv_palette_main(LV_PALETTE_BLUE)); + lv_style_set_bg_grad_color(&style, lv_palette_darken(LV_PALETTE_BLUE, 2)); + lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER); + + lv_style_set_border_opa(&style, LV_OPA_40); + lv_style_set_border_width(&style, 2); + lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_GREY)); + + lv_style_set_shadow_width(&style, 8); + lv_style_set_shadow_color(&style, lv_palette_main(LV_PALETTE_GREY)); + lv_style_set_shadow_offset_y(&style, 8); + + lv_style_set_outline_opa(&style, LV_OPA_COVER); + lv_style_set_outline_color(&style, lv_palette_main(LV_PALETTE_BLUE)); + + lv_style_set_text_color(&style, lv_color_white()); + lv_style_set_pad_all(&style, 10); + + /*Init the pressed style*/ + static lv_style_t style_pr; + lv_style_init(&style_pr); + + /*Add a large outline when pressed*/ + lv_style_set_outline_width(&style_pr, 30); + lv_style_set_outline_opa(&style_pr, LV_OPA_TRANSP); + + lv_style_set_translate_y(&style_pr, 5); + lv_style_set_shadow_offset_y(&style_pr, 3); + lv_style_set_bg_color(&style_pr, lv_palette_darken(LV_PALETTE_BLUE, 2)); + lv_style_set_bg_grad_color(&style_pr, lv_palette_darken(LV_PALETTE_BLUE, 4)); + + /*Add a transition to the outline*/ + static lv_style_transition_dsc_t trans; + static lv_style_prop_t props[] = {LV_STYLE_OUTLINE_WIDTH, LV_STYLE_OUTLINE_OPA, 0}; + lv_style_transition_dsc_init(&trans, props, lv_anim_path_linear, 300, 0, NULL); + + lv_style_set_transition(&style_pr, &trans); + + lv_obj_t * btn1 = lv_button_create(lv_screen_active()); + lv_obj_remove_style_all(btn1); /*Remove the style coming from the theme*/ + lv_obj_add_style(btn1, &style, 0); + lv_obj_add_style(btn1, &style_pr, LV_STATE_PRESSED); + lv_obj_set_size(btn1, LV_SIZE_CONTENT, LV_SIZE_CONTENT); + lv_obj_center(btn1); + + lv_obj_t * label = lv_label_create(btn1); + lv_label_set_text(label, "Button"); + lv_obj_center(label); +} +#endif diff --git a/lib/lvgl/examples/widgets/button/lv_example_button_3.c b/lib/lvgl/examples/widgets/button/lv_example_button_3.c new file mode 100644 index 00000000..d6803856 --- /dev/null +++ b/lib/lvgl/examples/widgets/button/lv_example_button_3.c @@ -0,0 +1,45 @@ +#include "../../lv_examples.h" +#if LV_BUILD_EXAMPLES && LV_USE_BUTTON + +/** + * Create a style transition on a button to act like a gum when clicked + */ +void lv_example_button_3(void) +{ + /*Properties to transition*/ + static lv_style_prop_t props[] = { + LV_STYLE_TRANSFORM_WIDTH, LV_STYLE_TRANSFORM_HEIGHT, LV_STYLE_TEXT_LETTER_SPACE, 0 + }; + + /*Transition descriptor when going back to the default state. + *Add some delay to be sure the press transition is visible even if the press was very short*/ + static lv_style_transition_dsc_t transition_dsc_def; + lv_style_transition_dsc_init(&transition_dsc_def, props, lv_anim_path_overshoot, 250, 100, NULL); + + /*Transition descriptor when going to pressed state. + *No delay, go to presses state immediately*/ + static lv_style_transition_dsc_t transition_dsc_pr; + lv_style_transition_dsc_init(&transition_dsc_pr, props, lv_anim_path_ease_in_out, 250, 0, NULL); + + /*Add only the new transition to he default state*/ + static lv_style_t style_def; + lv_style_init(&style_def); + lv_style_set_transition(&style_def, &transition_dsc_def); + + /*Add the transition and some transformation to the presses state.*/ + static lv_style_t style_pr; + lv_style_init(&style_pr); + lv_style_set_transform_width(&style_pr, 10); + lv_style_set_transform_height(&style_pr, -10); + lv_style_set_text_letter_space(&style_pr, 10); + lv_style_set_transition(&style_pr, &transition_dsc_pr); + + lv_obj_t * btn1 = lv_button_create(lv_screen_active()); + lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -80); + lv_obj_add_style(btn1, &style_pr, LV_STATE_PRESSED); + lv_obj_add_style(btn1, &style_def, 0); + + lv_obj_t * label = lv_label_create(btn1); + lv_label_set_text(label, "Gum"); +} +#endif |
