Add 'type', 'label', rename 'icon' to 'img' in widget

This commit is contained in:
Pim van Pelt
2017-11-26 18:41:17 +01:00
parent a11336703e
commit fda8b332db
5 changed files with 108 additions and 12 deletions

View File

@ -13,7 +13,8 @@
"w": 48,
"h": 48,
"label": "One",
"type": 0
"type": 2,
"screen": "screen_one.json"
},
{
"name": "two",
@ -22,7 +23,17 @@
"w": 48,
"h": 48,
"label": "Two",
"type": 0
"type": 2,
"screen": "screen_two.json"
},
{
"name": "logo",
"x": 88,
"y": 48,
"w": 144,
"h": 144,
"type": 1,
"img": "/mongoose-os.jpg"
}
]
}

30
fs/screen_one.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "One",
"x": 20,
"y": 0,
"w": 320,
"h": 220,
"widgets": [
{
"name": "back",
"x": 16,
"y": 16,
"w": 48,
"h": 48,
"label": "Back",
"type": 2,
"screen": "screen_main.json"
},
{
"name": "one",
"x": 256,
"y": 16,
"w": 48,
"h": 48,
"label": "One",
"type": 1,
"img": "/flower.jpg"
}
]
}

30
fs/screen_two.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "Two",
"x": 20,
"y": 0,
"w": 320,
"h": 220,
"widgets": [
{
"name": "back",
"x": 16,
"y": 16,
"w": 48,
"h": 48,
"label": "Back",
"type": 2,
"screen": "screen_main.json"
},
{
"name": "two",
"x": 256,
"y": 16,
"w": 48,
"h": 48,
"label": "Two",
"type": 2,
"screen": "screen_two.json"
}
]
}

View File

@ -14,12 +14,22 @@ struct widget_t;
#define EV_WIDGET_TOUCH_UP 6
#define EV_WIDGET_TOUCH_DOWN 7
enum widget_type_t {
WIDGET_TYPE_NONE =0,
WIDGET_TYPE_IMAGE =1,
WIDGET_TYPE_LOADSCREEN =2,
};
typedef void (*widget_event_fn)(int ev, struct widget_t *w, void *ev_data);
struct widget_t {
char *name;
uint16_t x, y, w, h;
enum widget_type_t type;
char *label;
char *img;
uint32_t timer_msec; // 0 to disable
widget_event_fn handler; // Event callback for this widget
void *user_data; // User supplied data
@ -34,11 +44,6 @@ struct widget_list_t {
SLIST_ENTRY(widget_list_t) entries;
};
/*
struct widget_t *widget_find(uint16_t x, uint16_t y);
void widget_remove(struct widget_t *widget);
*/
struct widget_t *widget_create(char *name, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
void widget_set_handler(struct widget_t *w, widget_event_fn handler, void *user_data);
void widget_delete_handler(struct widget_t *w);

View File

@ -19,8 +19,19 @@ void widget_destroy(struct widget_t **widget) {
if ((*widget)->_timer_id)
mgos_clear_timer((*widget)->_timer_id);
if ((*widget)->user_data)
free((*widget)->user_data);
if ((*widget)->name)
free((*widget)->name);
if ((*widget)->label)
free((*widget)->label);
if ((*widget)->img)
free((*widget)->img);
free(*widget);
*widget=NULL;
}
@ -31,12 +42,16 @@ struct widget_t *widget_create(char *name, uint16_t x, uint16_t y, uint16_t w, u
widget = (struct widget_t *) calloc(1, sizeof(*widget));
if (!widget)
return NULL;
if (name)
widget->name=strdup(name);
widget->name=strdup(name);
widget->x=x;
widget->y=y;
widget->w=w;
widget->h=h;
widget->type=WIDGET_TYPE_NONE;
widget->label=NULL;
widget->img=NULL;
widget->user_data = NULL;
widget->handler = NULL;
widget->timer_msec = 0;
@ -52,15 +67,20 @@ struct widget_t *widget_create_from_json(const char *json) {
int type = 0;
char *name = NULL;
char *label = NULL;
char *icon = NULL;
char *img = NULL;
if (json_scanf(json, strlen(json), "{name:%Q,x:%d,y:%d,w:%d,h:%d}", &name, &x, &y, &w, &h) != 5) {
LOG(LL_ERROR, ("Incomplete JSON: require 'x', 'y', 'w', 'h' and 'name' fields"));
return NULL;
}
json_scanf(json, strlen(json), "{type:%d,label:%Q,icon:%Q}", &type, &label, &icon);
widget = widget_create(name, x, y, w, h);
if (name) free(name);
free(name);
json_scanf(json, strlen(json), "{type:%d,label:%Q,img:%Q}", &type, &label, &img);
widget->type=type;
widget->label=label;
widget->img=img;
return widget;
}