add lv_components as src/extra and minor fixes
This commit is contained in:
16
examples/widgets/btn/index.rst
Normal file
16
examples/widgets/btn/index.rst
Normal file
@@ -0,0 +1,16 @@
|
||||
C
|
||||
^
|
||||
|
||||
Simple Buttons
|
||||
""""""""""""""""
|
||||
|
||||
.. lv_example:: lv_ex_widgets/lv_ex_btn/lv_ex_btn_1
|
||||
:language: c
|
||||
|
||||
.. lv_example:: lv_ex_widgets/lv_ex_btn/lv_ex_btn_2
|
||||
:language: c
|
||||
|
||||
MicroPython
|
||||
^^^^^^^^^^^
|
||||
|
||||
No examples yet.
|
||||
35
examples/widgets/btn/lv_btn_example_1.c
Normal file
35
examples/widgets/btn/lv_btn_example_1.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "../../../lvgl.h"
|
||||
#include <stdio.h>
|
||||
#if LV_USE_BTN
|
||||
|
||||
static void event_handler(lv_obj_t * obj, lv_event_t event)
|
||||
{
|
||||
if(event == LV_EVENT_CLICKED) {
|
||||
printf("Clicked\n");
|
||||
}
|
||||
else if(event == LV_EVENT_VALUE_CHANGED) {
|
||||
printf("Toggled\n");
|
||||
}
|
||||
}
|
||||
|
||||
void lv_ex_btn_1(void)
|
||||
{
|
||||
lv_obj_t * label;
|
||||
|
||||
lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL);
|
||||
lv_obj_add_event_cb(btn1, event_handler, NULL);
|
||||
lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40);
|
||||
|
||||
label = lv_label_create(btn1, NULL);
|
||||
lv_label_set_text(label, "Button");
|
||||
|
||||
lv_obj_t * btn2 = lv_btn_create(lv_scr_act(), NULL);
|
||||
lv_obj_add_event_cb(btn2, event_handler, NULL);
|
||||
lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40);
|
||||
lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
|
||||
lv_obj_set_height(btn2, LV_SIZE_AUTO);
|
||||
|
||||
label = lv_label_create(btn2, NULL);
|
||||
lv_label_set_text(label, "Toggle");
|
||||
}
|
||||
#endif
|
||||
21
examples/widgets/btn/lv_btn_example_1.py
Normal file
21
examples/widgets/btn/lv_btn_example_1.py
Normal file
@@ -0,0 +1,21 @@
|
||||
def event_handler(obj, event):
|
||||
if event == lv.EVENT.CLICKED:
|
||||
print("Clicked")
|
||||
|
||||
btn1 = lv.btn(lv.scr_act())
|
||||
btn1.set_event_cb(event_handler)
|
||||
btn1.align(None, lv.ALIGN.CENTER, 0, -40)
|
||||
|
||||
label = lv.label(btn1)
|
||||
label.set_text("Button")
|
||||
|
||||
btn2 = lv.btn(lv.scr_act())
|
||||
# callback can be lambda:
|
||||
btn2.set_event_cb(lambda obj, event: print("Toggled") if event == lv.EVENT.VALUE_CHANGED else None)
|
||||
btn2.align(None, lv.ALIGN.CENTER, 0, 40)
|
||||
btn2.set_toggle(True)
|
||||
btn2.toggle()
|
||||
btn2.set_fit2(lv.FIT.NONE, lv.FIT.TIGHT)
|
||||
|
||||
label = lv.label(btn2)
|
||||
label.set_text("Toggled")
|
||||
52
examples/widgets/btn/lv_btn_example_2.c
Normal file
52
examples/widgets/btn/lv_btn_example_2.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "../../../lvgl.h"
|
||||
#include <stdio.h>
|
||||
#if LV_USE_BTN
|
||||
|
||||
/**
|
||||
* Style a button from scratch
|
||||
*/
|
||||
void lv_ex_btn_2(void)
|
||||
{
|
||||
|
||||
static lv_style_t style;
|
||||
static lv_style_t style_pr;
|
||||
lv_style_init(&style);
|
||||
lv_style_init(&style_pr);
|
||||
|
||||
/*Init the default style*/
|
||||
lv_style_set_radius(&style, 3);
|
||||
|
||||
lv_style_set_bg_opa(&style, LV_OPA_70);
|
||||
lv_style_set_bg_color(&style, LV_COLOR_BLUE);
|
||||
lv_style_set_bg_grad_color(&style, LV_COLOR_AQUA);
|
||||
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_COLOR_GRAY);
|
||||
|
||||
lv_style_set_shadow_width(&style, 8);
|
||||
lv_style_set_shadow_color(&style, LV_COLOR_GRAY);
|
||||
lv_style_set_shadow_ofs_x(&style, 8);
|
||||
lv_style_set_shadow_ofs_y(&style, 8);
|
||||
|
||||
lv_style_set_text_color(&style, LV_COLOR_WHITE);
|
||||
|
||||
/*Init the pressed style*/
|
||||
lv_style_set_shadow_ofs_x(&style_pr, 4);
|
||||
lv_style_set_shadow_ofs_y(&style_pr, 4);
|
||||
lv_style_set_color_filter_cb(&style_pr, lv_color_darken); /*Darken every color*/
|
||||
lv_style_set_color_filter_opa(&style_pr, LV_OPA_30);
|
||||
|
||||
lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL);
|
||||
lv_obj_remove_style(btn1, LV_PART_ANY, LV_STATE_ANY, NULL);
|
||||
lv_obj_add_style(btn1, LV_PART_MAIN, LV_STATE_DEFAULT, &style);
|
||||
lv_obj_add_style(btn1, LV_PART_MAIN, LV_STATE_PRESSED, &style_pr);
|
||||
lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
lv_obj_t * label = lv_label_create(btn1, NULL);
|
||||
lv_label_set_text(label, "Button");
|
||||
lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
}
|
||||
#endif
|
||||
57
examples/widgets/btn/lv_btn_example_3.c
Normal file
57
examples/widgets/btn/lv_btn_example_3.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "../../../lvgl.h"
|
||||
#include <stdio.h>
|
||||
#if LV_USE_BTN
|
||||
|
||||
/**
|
||||
* Create a style transition on a button to act like a gum when clicked
|
||||
*/
|
||||
void lv_ex_btn_3(void)
|
||||
{
|
||||
/*Properties to transition*/
|
||||
static lv_style_prop_t props[] = {
|
||||
LV_STYLE_TRANSFORM_WIDTH, LV_STYLE_TRANSFORM_HEIGHT, LV_STYLE_CONTENT_LETTER_SPACE, 0
|
||||
};
|
||||
|
||||
/*Define animation paths*/
|
||||
static lv_anim_path_t path_ease_in_out;
|
||||
lv_anim_path_init(&path_ease_in_out);
|
||||
lv_anim_path_set_cb(&path_ease_in_out, lv_anim_path_ease_in_out);
|
||||
|
||||
static lv_anim_path_t path_overshoot;
|
||||
lv_anim_path_init(&path_overshoot);
|
||||
lv_anim_path_set_cb(&path_overshoot, lv_anim_path_overshoot);
|
||||
|
||||
|
||||
/* 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, &path_overshoot, 250, 100);
|
||||
|
||||
/* 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, &path_ease_in_out, 250, 0);
|
||||
|
||||
/*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_content_letter_space(&style_pr, 10);
|
||||
lv_style_set_transition(&style_pr, &transition_dsc_pr);
|
||||
|
||||
|
||||
lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL);
|
||||
lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -80);
|
||||
lv_obj_add_style(btn1, LV_PART_MAIN, LV_STATE_PRESSED, &style_pr);
|
||||
lv_obj_add_style(btn1, LV_PART_MAIN, LV_STATE_DEFAULT, &style_def);
|
||||
|
||||
/*Instead of creating a label add a values string*/
|
||||
lv_obj_set_style_content_text(btn1, LV_PART_MAIN, LV_STATE_DEFAULT, "Gum");
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user