milligram
Revision | 4fb7499263137cdefba517717336221767b7307e (tree) |
---|---|
Zeit | 2011-05-03 07:12:30 |
Autor | berupon <berupon@gmai...> |
Commiter | berupon |
rewrite in C language.
@@ -0,0 +1,101 @@ | ||
1 | +#include "mg.h" | |
2 | + | |
3 | +void mg_offset(mgRect* p, mgPoint pt) { | |
4 | + p->pos.x += pt.x; | |
5 | + p->pos.y += pt.y; | |
6 | +} | |
7 | + | |
8 | +bool_t mg_in(const mgRect* p, mgPoint pt) { | |
9 | + return | |
10 | + 1 | |
11 | + && mg_left(p) <= pt.x | |
12 | + && pt.x < mg_right(p) | |
13 | + && mg_top(p) <= pt.y | |
14 | + && pt.y < mg_bottom(p) | |
15 | + ; | |
16 | +} | |
17 | + | |
18 | +bool_t mg_contains(const mgRect* p, const mgRect* pCheck) { | |
19 | + return | |
20 | + 1 | |
21 | + && mg_left(p) <= mg_left(pCheck) | |
22 | + && mg_right(p) >= mg_right(pCheck) | |
23 | + && mg_top(p) <= mg_top(pCheck) | |
24 | + && mg_bottom(p) >= mg_bottom(pCheck) | |
25 | + ; | |
26 | +} | |
27 | + | |
28 | +void mg_intersect(const mgRect* p1, const mgRect* p2, mgRect* pResult) { | |
29 | + int16_t left = max(mg_left(p1), mg_left(p2)); | |
30 | + int16_t right = min(mg_right(p1), mg_right(p2)); | |
31 | + | |
32 | + int16_t top = max(mg_top(p1), mg_top(p2)); | |
33 | + int16_t bottom = min(mg_bottom(p1), mg_bottom(p2)); | |
34 | + | |
35 | + pResult->x = left; | |
36 | + pResult->w = max(0, right - left); | |
37 | + | |
38 | + pResult->y = top; | |
39 | + pResult->h = max(0, bottom - top); | |
40 | +} | |
41 | + | |
42 | +void mg_unionPoint(const mgRect* pRect, mgPoint pt, mgRect* pResult) { | |
43 | + mgPoint rp = pRect->pt; | |
44 | + pResult->pt.x = min(rp.x, pt.x); | |
45 | + pResult->pt.y = min(rp.y, pt.y); | |
46 | + pResult->w = max(pRect->w, pt.x - rp.x + 1); | |
47 | + pResult->h = max(pRect->h, pt.y - rp.y + 1); | |
48 | +} | |
49 | + | |
50 | +void mg_unionRect(const mgRect* p1, const mgRect* p2, mgRect* pResult) { | |
51 | + pResult->x = min(p1->pt.x, p2->pt.x); | |
52 | + pResult->y = min(p1->pt.y, p2->pt.y); | |
53 | + pResult->w = max(mg_right(p1), mg_right(p2)) - mg_left(pResult); | |
54 | + pResult->h = max(mg_bottom(p1), mg_bottom(p2)) - mg_top(pResult); | |
55 | +} | |
56 | + | |
57 | +static mgElement* s_pElements[256]; | |
58 | +static mgElement* s_pBehaviors[256]; | |
59 | + | |
60 | +void mg_registerElement(mgElement* p) { | |
61 | + mgEnum i; | |
62 | + for (i=0; i<256; ++i) { | |
63 | + if (!s_pElements[i]) { | |
64 | + s_pElements[i] = p; | |
65 | + p->id = i; | |
66 | + break; | |
67 | + } | |
68 | + } | |
69 | +} | |
70 | + | |
71 | +void mg_unregisterElement(mgElement* p) { | |
72 | + s_pElements[i] = 0; | |
73 | + p->id = 0; | |
74 | +} | |
75 | + | |
76 | +void mg_registerBehavior(mgBehavior* p) { | |
77 | + mgEnum i; | |
78 | + for (i=0; i<256; ++i) { | |
79 | + if (!s_pBehaviors[i]) { | |
80 | + s_pBehaviors[i] = p; | |
81 | + p->id = i; | |
82 | + break; | |
83 | + } | |
84 | + } | |
85 | +} | |
86 | + | |
87 | +void mg_unresiterBehavior(mgBehavior* p) { | |
88 | + s_pBehaviors[i] = 0; | |
89 | + p->id = 0; | |
90 | +} | |
91 | + | |
92 | +void mg_notifyEvent(id_t parent, mgEventInfo* pEvent) { | |
93 | + | |
94 | +} | |
95 | + | |
96 | +void mg_initialize() { | |
97 | + | |
98 | + | |
99 | + | |
100 | +} | |
101 | + |
@@ -0,0 +1,94 @@ | ||
1 | +#ifndef MG_H_INCLUDED__ | |
2 | +#define MG_H_INCLUDED__ | |
3 | + | |
4 | +#ifdef __cplusplus | |
5 | +extern "C" { | |
6 | +#endif /* __cplusplus */ | |
7 | + | |
8 | +// type definitions | |
9 | + | |
10 | +typedef struct mgPoint_t { | |
11 | + int16_t x; | |
12 | + int16_t y; | |
13 | +} mgPoint; | |
14 | + | |
15 | +typedef struct mgRect_t { | |
16 | + mgPoint pos; | |
17 | + uint16_t w; | |
18 | + uint16_t h; | |
19 | +} mgRect; | |
20 | + | |
21 | +typedef uint8_t mgEnum; | |
22 | + | |
23 | +typedef struct mgElement_t { | |
24 | + mgEnum behavior; | |
25 | + mgEnum id; | |
26 | + mgEnum parent; | |
27 | + | |
28 | + mgRect rect; | |
29 | + bool_t bEnable; | |
30 | + bool_t bVisible; | |
31 | + bool_t bNeedsToDraw; | |
32 | + | |
33 | +} mgElement; | |
34 | + | |
35 | +typedef struct mgBehavior_t { | |
36 | + mgEventListerFunc pDraw; | |
37 | + mgEventListerFunc pHitTest; | |
38 | + mgEventListerFunc pOnMouseDown; | |
39 | + mgEventListerFunc pOnMouseUp; | |
40 | + mgEventListerFunc pOnMouseMove; | |
41 | +} mgBehavior; | |
42 | + | |
43 | +typedef enum mgEventType_t { | |
44 | + mgEventType_MouseDown, | |
45 | + mgEventType_MouseUp, | |
46 | + mgEventType_MouseMove, | |
47 | + | |
48 | +} mgEventType; | |
49 | + | |
50 | +typedef struct mgEventInfo_t { | |
51 | + mgElement* pTarget; | |
52 | + mgEventType type; | |
53 | + mgPoint pos; | |
54 | +} mgEventInfo; | |
55 | + | |
56 | +typedef enum mgElementType_t { | |
57 | + mgElementType_Canvas, | |
58 | + mgElementType_Label, | |
59 | + mgElementType_Button, | |
60 | + | |
61 | +} mgElementType; | |
62 | + | |
63 | +typedef bool (*mgEventListenerFunc) (mgEventInfo* pInfo); | |
64 | + | |
65 | +// function | |
66 | + | |
67 | +int16_t mg_left(const mgRect* p) const { return p->pos.x; } | |
68 | +int16_t mg_right(const mgRect* p) const { return mg_left(p) + p->w; } | |
69 | +int16_t mg_top(const mgRect* p) const { return p->pos.y; } | |
70 | +int16_t mg_bottom(const mgRect* p) const { return mg_top(p) + p->h; } | |
71 | + | |
72 | +void mg_offset(mgRect* p, mgPoint pt); | |
73 | +bool_t mg_in(const mgRect* p, mgPoint pt); | |
74 | + | |
75 | +bool_t mg_contains(const mgRect* p, const mgRect* pCheck); | |
76 | +void mg_intersect(const mgRect* p1, const mgRect* p2, mgRect* pResult); | |
77 | + | |
78 | +void mg_unionPoint(const mgRect* pRect, mgPoint pt, mgRect* pResult); | |
79 | +void mg_unionRect(const mgRect* p1, const mgRect* p2, mgRect* pResult); | |
80 | + | |
81 | +void mg_setEventListener(mgEnum id, mgEventType type, mgEventListenerFunc func); | |
82 | + | |
83 | +void mg_initialize(); | |
84 | + | |
85 | +void mg_registerElement(mgElement* p); | |
86 | +void mg_unregisterElement(mgElement* p); | |
87 | + | |
88 | +void mg_notifyEvent(id_t parent, mgEventInfo* pEvent); | |
89 | + | |
90 | +#ifdef __cplusplus | |
91 | +}; /* extern "C" */ | |
92 | +#endif /* __cplusplus */ | |
93 | + | |
94 | +#endif // #ifndef MG_H_INCLUDED__ |