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
|
/*
* File : plot.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-09-03 Grissiom first version
*/
#include <rtgui/rtgui_system.h>
#include <rtgui/dc.h>
#include <rtgui/widgets/plot.h>
static void _rtgui_plot_constructor(struct rtgui_plot *plot)
{
plot->base_point.x = plot->base_point.y = 0;
plot->curve_container.curve = RT_NULL;
plot->curve_container.next = RT_NULL;
/* init widget and set event handler */
rtgui_object_set_event_handler(RTGUI_OBJECT(plot), rtgui_plot_event_handler);
}
static void _free_curve_container_recursive(struct rtgui_plot_curve_container *cnt)
{
if (!cnt)
return;
_free_curve_container_recursive(cnt->next);
rtgui_free(cnt);
}
static void _rtgui_plot_destructor(struct rtgui_plot *plot)
{
_free_curve_container_recursive(plot->curve_container.next);
}
DEFINE_CLASS_TYPE(plot, "plot",
RTGUI_WIDGET_TYPE,
_rtgui_plot_constructor,
_rtgui_plot_destructor,
sizeof(struct rtgui_plot));
struct rtgui_plot *rtgui_plot_create(struct rtgui_plot_curve *curve)
{
struct rtgui_plot *plot;
plot = (struct rtgui_plot *)rtgui_widget_create(RTGUI_PLOT_TYPE);
plot->curve_container.curve = curve;
return plot;
}
RTM_EXPORT(rtgui_plot_create);
void rtgui_plot_destroy(struct rtgui_plot *plot)
{
rtgui_widget_destroy(RTGUI_WIDGET(plot));
}
RTM_EXPORT(rtgui_plot_destroy);
void rtgui_plot_set_base_point(struct rtgui_plot *plot, rt_uint16_t x, rt_uint16_t y)
{
plot->base_point.x = x;
plot->base_point.y = y;
}
RTM_EXPORT(rtgui_plot_set_base_point);
void rtgui_plot_append_curve(struct rtgui_plot *plot, struct rtgui_plot_curve *curve)
{
struct rtgui_plot_curve_container *cur_cnt, *next_cnt;
RT_ASSERT(plot);
next_cnt = rtgui_malloc(sizeof(*next_cnt));
next_cnt->curve = curve;
next_cnt->next = RT_NULL;
cur_cnt = &plot->curve_container;
while (cur_cnt->next)
{
cur_cnt = cur_cnt->next;
}
cur_cnt->next = next_cnt;
}
RTM_EXPORT(rtgui_plot_append_curve);
static void _rtgui_plot_curve_onpaint(
struct rtgui_dc *dc,
struct rtgui_plot_curve *curve,
struct rtgui_point base)
{
struct rtgui_rect rect;
rt_uint16_t height;
int last_x, last_y;
rtgui_color_t old_color;
rtgui_dc_get_rect(dc, &rect);
height = rtgui_rect_height(rect);
old_color = RTGUI_DC_FC(dc);
RTGUI_DC_FC(dc) = curve->color;
if (curve->x_data)
{
rt_size_t i;
last_x = curve->x_data[0] + base.x;
last_y = height - curve->y_data[0] - base.y;
for (i = 1; i < curve->length; i++)
{
int cur_x = curve->x_data[i] + base.x;
int cur_y = height - curve->y_data[i] - base.y;
rtgui_dc_draw_line(dc,
last_x, last_y,
cur_x, cur_y);
last_x = cur_x;
last_y = cur_y;
}
}
else
{
rt_size_t i;
last_x = 0 + base.x;
last_y = height - curve->y_data[0] - base.y;
for (i = 1; i < curve->length; i++)
{
int cur_x = i + base.x;
int cur_y = height - curve->y_data[i] - base.y;
rtgui_dc_draw_line(dc,
last_x, last_y,
cur_x, cur_y);
last_x = cur_x;
last_y = cur_y;
}
}
RTGUI_DC_FC(dc) = old_color;
}
static void _rtgui_plot_onpaint(struct rtgui_object *object, struct rtgui_event *event)
{
struct rtgui_dc *dc;
struct rtgui_rect rect;
struct rtgui_plot *plot = RTGUI_PLOT(object);
struct rtgui_plot_curve_container *cnt;
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot));
if (dc == RT_NULL)
return;
rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
rtgui_dc_fill_rect(dc, &rect);
for (cnt = &plot->curve_container; cnt; cnt = cnt->next)
{
_rtgui_plot_curve_onpaint(dc, cnt->curve, plot->base_point);
}
rtgui_dc_end_drawing(dc);
}
rt_bool_t rtgui_plot_event_handler(struct rtgui_object *object, struct rtgui_event *event)
{
struct rtgui_plot *plot;
RTGUI_WIDGET_EVENT_HANDLER_PREPARE;
plot = RTGUI_PLOT(object);
switch (event->type)
{
case RTGUI_EVENT_PAINT:
_rtgui_plot_onpaint(object, event);
break;
default:
return rtgui_widget_event_handler(object, event);
}
return RT_FALSE;
}
RTM_EXPORT(rtgui_plot_event_handler);
|