summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/eve/screen/form.c
blob: d3707fc0de86cbec16a84baec4cc0b08d0223af1 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdlib.h>
#include <string.h>

#include "unicode.h"

#include "eve.h"
#include "eve_kbd.h"
#include "eve_font.h"

#include "window.h"
#include "page.h"
#include "form.h"

#include "widget/label.h"
#include "widget/widget.h"

#define MIN(X, Y)               (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)               (((X) > (Y)) ? (X) : (Y))

static void form_update_g(EVEForm *form, EVEWidget *_widget) {
    EVEWidget *widget = form->widget;
    int i;
    uint16_t w = 0;
    uint16_t h = 0;
    uint16_t l_h = 0;

    for (i=0; i<form->widget_size; i++) {
        if (widget->label) {
            h += l_h;
            w = widget->label->g.w;
            l_h = widget->label->g.h;
            widget->label->g.x = 0;
            widget->label->g.y = h;
        }
        if (w + widget->g.w > form->p.v.window->g.w) {
            h += l_h;
            w = 0;
            l_h = 0;
        }
        widget->g.x = w;
        widget->g.y = h;

        w += widget->g.w;
        l_h = MAX(l_h, widget->g.h);

        widget = eve_widget_next(widget);
    }
    form->h = h + l_h;
}

static int form_handle_evt(EVEForm *form, EVEWidget *widget, EVETouch *touch, uint16_t evt, uint8_t tag0) {
    int ret = 0;
    EVEPage *page = &form->p;

    if ((evt & EVE_TOUCH_ETYPE_POINT_UP) && !(touch->eevt & (EVE_TOUCH_EETYPE_TRACK_XY | EVE_TOUCH_EETYPE_ABORT))) {
        if (page->widget_f) eve_page_set_focus(page, NULL, NULL);
        ret = 1;
    }

    /* Scroll start */
    if ((evt & EVE_TOUCH_ETYPE_TRACK_START) && !(touch->eevt & EVE_TOUCH_EETYPE_ABORT)) {
        form->evt_lock = 1;
    }

    /* Scroll stop */
    if (((evt & EVE_TOUCH_ETYPE_TRACK_STOP) && !(evt & EVE_TOUCH_ETYPE_TRACK_ABORT)) ||
        ((evt & EVE_TOUCH_ETYPE_POINT_UP) && (touch->eevt & EVE_TOUCH_EETYPE_ABORT) && !(touch->eevt & EVE_TOUCH_EETYPE_TRACK_XY))) {
        int wmax_x, wmax_y;
        int lho_x, lho_y;
        EVERect vg;

        eve_window_visible_g(page->v.window, &vg);
        wmax_x = form->w > vg.w ? form->w - vg.w : 0;
        wmax_y = form->h > vg.h ? form->h - vg.h : 0;
        lho_x = page->win_x < 0 ? 0 : wmax_x;
        lho_y = page->win_y < 0 ? 0 : wmax_y;
        if ((page->win_x < 0) || (page->win_x > wmax_x) ||
            (page->win_y < 0) || (page->win_y > wmax_y)) {
            EVEPhyLHO *lho = &form->lho;
            eve_phy_lho_init(lho, lho_x, lho_y, 1000, 0.5, 0);
            eve_phy_lho_start(lho, page->win_x, page->win_y);
            form->lho_t0 = eve_time_get_tick();
            eve_touch_timer_start(tag0, 20);
        } else {
            form->evt_lock = 0;
            if (evt & EVE_TOUCH_ETYPE_TRACK_STOP) {
                if (touch->eevt & EVE_TOUCH_EETYPE_TRACK_RIGHT) {
                    eve_page_close((EVEPage *)form);
                    return 1;
                }
                if (touch->eevt & EVE_TOUCH_EETYPE_TRACK_LEFT) {
                    if (form->action) form->action(form);
                    return 1;
                }
            }
        }
    }

    if (evt & EVE_TOUCH_ETYPE_TRACK_START) {
        if (touch->eevt & EVE_TOUCH_EETYPE_TRACK_Y) {
            form->win_y0 = page->win_y;
        }
    }
    if (evt & EVE_TOUCH_ETYPE_TRACK) {
        if (touch->eevt & EVE_TOUCH_EETYPE_TRACK_Y) {
            page->win_y = form->win_y0 + touch->y0 - touch->y;
        }
        ret = 1;
    }
    if (evt & EVE_TOUCH_ETYPE_TIMER) {
        EVEPhyLHO *lho = &form->lho;
        int more = eve_phy_lho_tick(lho, eve_time_get_tick() - form->lho_t0, NULL, &page->win_y);

        if (!more) {
            form->evt_lock = 0;
            eve_touch_timer_stop();
        }
        ret = 1;
    }

    return ret;
}

int eve_form_init(EVEForm *form, EVEWindow *window, EVEViewStack *stack, EVEWidget *widget, uint16_t widget_size, eve_form_action_t action, eve_form_destructor_t destructor) {
    memset(form, 0, sizeof(EVEForm));
    eve_page_init(&form->p, window, stack, eve_form_draw, eve_form_touch, (eve_page_destructor_t)destructor);

    form->widget = widget;
    form->widget_size = widget_size;
    form->action = action;
    form_update_g(form, NULL);
}

uint8_t eve_form_draw(EVEView *view, uint8_t tag0) {
    EVEForm *form = (EVEForm *)view;
    EVEWidget *widget = form->widget;
    int i;
    uint8_t tagN = tag0;
    uint8_t tag_opt = EVE_TOUCH_OPT_TRACK | EVE_TOUCH_OPT_TRACK_XY | EVE_TOUCH_OPT_TRACK_EXT_Y;

    tagN = eve_view_clear(view, tagN);

    eve_cmd_dl(SAVE_CONTEXT());
    eve_cmd_dl(VERTEX_FORMAT(0));
    eve_cmd_dl(VERTEX_TRANSLATE_X(eve_page_scr_x(&form->p, 0) * 16));
    eve_cmd_dl(VERTEX_TRANSLATE_Y(eve_page_scr_y(&form->p, 0) * 16));
    for (i=0; i<form->widget_size; i++) {
        if (widget->label && eve_page_rect_visible(&form->p, &widget->label->g)) {
            eve_cmd_dl(TAG_MASK(0));
            eve_label_draw(widget->label);
            eve_cmd_dl(TAG_MASK(1));
        }
        if (eve_page_rect_visible(&form->p, &widget->g)) {
            uint16_t h = widget->g.h;
            tagN = widget->draw(widget, &form->p, tagN);
            if (h != widget->g.h) form_update_g(form, widget);
        }
        widget = eve_widget_next(widget);
    }

    eve_cmd_dl(RESTORE_CONTEXT());

    for (i=tag0; i<tagN; i++) {
        eve_touch_set_opt(i, eve_touch_get_opt(i) | tag_opt);
    }
    if (view->tag != EVE_TAG_NOTAG) eve_touch_set_opt(view->tag, eve_touch_get_opt(view->tag) | tag_opt);

    return tagN;
}

int eve_form_touch(EVEView *view, EVETouch *touch, uint16_t evt, uint8_t tag0) {
    EVEForm *form = (EVEForm *)view;
    EVEWidget *widget = form->widget;
    int8_t touch_idx = eve_touch_get_idx(touch);
    uint16_t _evt;
    int i, ret;

    if (touch_idx > 0) return 0;

    _evt = eve_touch_evt(touch, evt, tag0, form->p.v.tag, 1);
    if (_evt) {
        ret = form_handle_evt(form, NULL, touch, _evt, tag0);
        if (ret) return 1;
    }
    for (i=0; i<form->widget_size; i++) {
        _evt = eve_touch_evt(touch, evt, tag0, widget->tag0, widget->tagN - widget->tag0);
        if (_evt) {
            if (!form->evt_lock) {
                ret = widget->touch(widget, &form->p, touch, _evt);
                if (ret) return 1;
            }
            ret = form_handle_evt(form, widget, touch, _evt, tag0);
            if (ret) return 1;
        }
        widget = eve_widget_next(widget);
    }

    return 0;
}

EVEWidget *eve_form_widget(EVEForm *form, uint16_t idx) {
    EVEWidget *w = form->widget;
    int i;

    if (idx >= form->widget_size) return NULL;

    for (i=0; i<idx; i++) {
        w = eve_widget_next(w);
    }
    return w;
}