aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/tileview/lv_tileview.c
blob: 86a5db3c36ad6bf94f0fafc34bfbbb0d60a458ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
 * @file lv_tileview.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "../../core/lv_obj_class_private.h"
#include "lv_tileview_private.h"
#include "../../indev/lv_indev.h"
#include "../../indev/lv_indev_private.h"
#if LV_USE_TILEVIEW

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void lv_tileview_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static void lv_tileview_tile_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static void tileview_event_cb(lv_event_t * e);

/**********************
 *  STATIC VARIABLES
 **********************/

const lv_obj_class_t lv_tileview_class = {
    .constructor_cb = lv_tileview_constructor,
    .base_class = &lv_obj_class,
    .instance_size = sizeof(lv_tileview_t),
    .name = "tileview",
};

const lv_obj_class_t lv_tileview_tile_class = {
    .constructor_cb = lv_tileview_tile_constructor,
    .base_class = &lv_obj_class,
    .instance_size = sizeof(lv_tileview_tile_t),
    .name = "tile",
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

lv_obj_t * lv_tileview_create(lv_obj_t * parent)
{
    LV_LOG_INFO("begin");
    lv_obj_t * obj = lv_obj_class_create_obj(&lv_tileview_class, parent);
    lv_obj_class_init_obj(obj);
    return obj;
}

/*======================
 * Add/remove functions
 *=====================*/

lv_obj_t * lv_tileview_add_tile(lv_obj_t * tv, uint8_t col_id, uint8_t row_id, lv_dir_t dir)
{
    LV_LOG_INFO("begin");

    lv_obj_t * obj = lv_obj_class_create_obj(&lv_tileview_tile_class, tv);
    lv_obj_class_init_obj(obj);
    lv_obj_set_pos(obj, lv_pct(col_id * 100), lv_pct(row_id * 100));

    lv_tileview_tile_t * tile = (lv_tileview_tile_t *)obj;
    tile->dir = dir;

    if(col_id == 0 && row_id == 0) {
        lv_obj_set_scroll_dir(tv, dir);
    }
    return obj;
}

void lv_tileview_set_tile(lv_obj_t * obj, lv_obj_t * tile_obj, lv_anim_enable_t anim_en)
{
    int32_t tx = lv_obj_get_x(tile_obj);
    int32_t ty = lv_obj_get_y(tile_obj);

    lv_tileview_tile_t * tile = (lv_tileview_tile_t *)tile_obj;
    lv_tileview_t * tv = (lv_tileview_t *) obj;
    tv->tile_act = (lv_obj_t *)tile;

    lv_obj_set_scroll_dir(obj, tile->dir);
    lv_obj_scroll_to(obj, tx, ty, anim_en);
}

void lv_tileview_set_tile_by_index(lv_obj_t * tv, uint32_t col_id, uint32_t row_id, lv_anim_enable_t anim_en)
{
    lv_obj_update_layout(tv);

    int32_t w = lv_obj_get_content_width(tv);
    int32_t h = lv_obj_get_content_height(tv);

    int32_t tx = col_id * w;
    int32_t ty = row_id * h;

    uint32_t i;
    for(i = 0; i < lv_obj_get_child_count(tv); i++) {
        lv_obj_t * tile_obj = lv_obj_get_child(tv, i);
        int32_t x = lv_obj_get_x(tile_obj);
        int32_t y = lv_obj_get_y(tile_obj);
        if(x == tx && y == ty) {
            lv_tileview_set_tile(tv, tile_obj, anim_en);
            return;
        }
    }

    LV_LOG_WARN("No tile found with at (%d,%d) index", (int)col_id, (int)row_id);
}

lv_obj_t * lv_tileview_get_tile_active(lv_obj_t * obj)
{
    lv_tileview_t * tv = (lv_tileview_t *) obj;
    return tv->tile_act;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void lv_tileview_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
{
    LV_UNUSED(class_p);
    lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
    lv_obj_add_event_cb(obj, tileview_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ONE);
    lv_obj_set_scroll_snap_x(obj, LV_SCROLL_SNAP_CENTER);
    lv_obj_set_scroll_snap_y(obj, LV_SCROLL_SNAP_CENTER);

}

static void lv_tileview_tile_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
{

    LV_UNUSED(class_p);
    lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
    lv_obj_update_layout(obj);  /*Be sure the size is correct*/
}

static void tileview_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_current_target(e);
    lv_tileview_t * tv = (lv_tileview_t *) obj;

    if(code == LV_EVENT_SCROLL_END) {
        lv_indev_t * indev = lv_indev_active();
        if(indev && indev->state == LV_INDEV_STATE_PRESSED) {
            return;
        }

        int32_t w = lv_obj_get_content_width(obj);
        int32_t h = lv_obj_get_content_height(obj);

        lv_point_t scroll_end;
        lv_obj_get_scroll_end(obj, &scroll_end);
        int32_t left = scroll_end.x;
        int32_t top = scroll_end.y;

        int32_t tx = ((left + (w / 2)) / w) * w;
        int32_t ty = ((top + (h / 2)) / h) * h;

        lv_dir_t dir = LV_DIR_ALL;
        uint32_t i;
        for(i = 0; i < lv_obj_get_child_count(obj); i++) {
            lv_obj_t * tile_obj = lv_obj_get_child(obj, i);
            int32_t x = lv_obj_get_x(tile_obj);
            int32_t y = lv_obj_get_y(tile_obj);
            if(x == tx && y == ty) {
                lv_tileview_tile_t * tile = (lv_tileview_tile_t *)tile_obj;
                tv->tile_act = (lv_obj_t *)tile;
                dir = tile->dir;
                lv_obj_send_event(obj, LV_EVENT_VALUE_CHANGED, NULL);
                break;
            }
        }
        lv_obj_set_scroll_dir(obj, dir);
    }
}
#endif /*LV_USE_TILEVIEW*/