15
15
16
16
#define _apps_spline_pixmap (spline ) ((spline)->widget.window->pixmap)
17
17
18
- #define NPT 4
19
18
20
19
typedef struct _apps_spline {
21
20
twin_widget_t widget ;
22
- twin_point_t points [NPT ];
21
+ int n_points ;
22
+ twin_point_t * points ;
23
23
int which ;
24
24
twin_fixed_t line_width ;
25
25
twin_cap_t cap_style ;
26
26
} apps_spline_t ;
27
27
28
+ static void _draw_aux_line (twin_path_t * path ,
29
+ apps_spline_t * spline ,
30
+ int idx1 ,
31
+ int idx2 )
32
+ {
33
+ twin_path_move (path , spline -> points [idx1 ].x , spline -> points [idx1 ].y );
34
+ twin_path_draw (path , spline -> points [idx2 ].x , spline -> points [idx2 ].y );
35
+ twin_paint_stroke (_apps_spline_pixmap (spline ), 0xc08000c0 , path ,
36
+ twin_int_to_fixed (2 ));
37
+ twin_path_empty (path );
38
+ }
39
+
28
40
static void _apps_spline_paint (apps_spline_t * spline )
29
41
{
30
42
twin_path_t * path ;
31
- int i ;
32
-
33
43
path = twin_path_create ();
34
44
twin_path_set_cap_style (path , spline -> cap_style );
45
+
35
46
twin_path_move (path , spline -> points [0 ].x , spline -> points [0 ].y );
36
- twin_path_curve (path , spline -> points [1 ].x , spline -> points [1 ].y ,
37
- spline -> points [2 ].x , spline -> points [2 ].y ,
38
- spline -> points [3 ].x , spline -> points [3 ].y );
47
+ if (spline -> n_points == 4 ) {
48
+ twin_path_curve (path , spline -> points [1 ].x , spline -> points [1 ].y ,
49
+ spline -> points [2 ].x , spline -> points [2 ].y ,
50
+ spline -> points [3 ].x , spline -> points [3 ].y );
51
+ } else if (spline -> n_points == 3 ) {
52
+ twin_path_quadratic_curve (path , spline -> points [1 ].x ,
53
+ spline -> points [1 ].y , spline -> points [2 ].x ,
54
+ spline -> points [2 ].y );
55
+ }
39
56
twin_paint_stroke (_apps_spline_pixmap (spline ), 0xff404040 , path ,
40
57
spline -> line_width );
41
58
twin_path_set_cap_style (path , TwinCapButt );
42
59
twin_paint_stroke (_apps_spline_pixmap (spline ), 0xffffff00 , path ,
43
60
twin_int_to_fixed (2 ));
44
-
45
61
twin_path_empty (path );
46
- twin_path_move (path , spline -> points [0 ].x , spline -> points [0 ].y );
47
- twin_path_draw (path , spline -> points [1 ].x , spline -> points [1 ].y );
48
- twin_paint_stroke (_apps_spline_pixmap (spline ), 0xc08000c0 , path ,
49
- twin_int_to_fixed (2 ));
50
- twin_path_empty (path );
51
- twin_path_move (path , spline -> points [3 ].x , spline -> points [3 ].y );
52
- twin_path_draw (path , spline -> points [2 ].x , spline -> points [2 ].y );
53
- twin_paint_stroke (_apps_spline_pixmap (spline ), 0xc08000c0 , path ,
54
- twin_int_to_fixed (2 ));
55
- twin_path_empty (path );
56
- for (i = 0 ; i < NPT ; i ++ ) {
62
+ if (spline -> n_points == 4 ) {
63
+ _draw_aux_line (path , spline , 0 , 1 );
64
+ _draw_aux_line (path , spline , 3 , 2 );
65
+ } else if (spline -> n_points == 3 ) {
66
+ _draw_aux_line (path , spline , 0 , 1 );
67
+ _draw_aux_line (path , spline , 1 , 2 );
68
+ }
69
+
70
+ for (int i = 0 ; i < spline -> n_points ; i ++ ) {
57
71
twin_path_empty (path );
58
72
twin_path_circle (path , spline -> points [i ].x , spline -> points [i ].y ,
59
73
twin_int_to_fixed (10 ));
@@ -81,7 +95,7 @@ static int _apps_spline_hit(apps_spline_t *spline,
81
95
{
82
96
int i ;
83
97
84
- for (i = 0 ; i < NPT ; i ++ )
98
+ for (i = 0 ; i < spline -> n_points ; i ++ )
85
99
if (twin_fixed_abs (x - spline -> points [i ].x ) < spline -> line_width / 2 &&
86
100
twin_fixed_abs (y - spline -> points [i ].y ) < spline -> line_width / 2 )
87
101
return i ;
@@ -123,28 +137,40 @@ static twin_dispatch_result_t _apps_spline_dispatch(twin_widget_t *widget,
123
137
124
138
static void _apps_spline_init (apps_spline_t * spline ,
125
139
twin_box_t * parent ,
126
- twin_dispatch_proc_t dispatch )
140
+ twin_dispatch_proc_t dispatch ,
141
+ int n_points )
127
142
{
128
143
static const twin_widget_layout_t preferred = {0 , 0 , 1 , 1 };
129
144
_twin_widget_init (& spline -> widget , parent , 0 , preferred , dispatch );
130
145
twin_widget_set (& spline -> widget , 0xffffffff );
131
146
spline -> line_width = twin_int_to_fixed (100 );
132
147
spline -> cap_style = TwinCapRound ;
133
- spline -> points [0 ].x = twin_int_to_fixed (100 );
134
- spline -> points [0 ].y = twin_int_to_fixed (100 );
135
- spline -> points [1 ].x = twin_int_to_fixed (300 );
136
- spline -> points [1 ].y = twin_int_to_fixed (300 );
137
- spline -> points [2 ].x = twin_int_to_fixed (100 );
138
- spline -> points [2 ].y = twin_int_to_fixed (300 );
139
- spline -> points [3 ].x = twin_int_to_fixed (300 );
140
- spline -> points [3 ].y = twin_int_to_fixed (100 );
148
+ spline -> points = calloc (n_points , sizeof (twin_point_t ));
149
+ spline -> n_points = n_points ;
150
+ if (n_points == 4 ) {
151
+ spline -> points [0 ].x = twin_int_to_fixed (100 );
152
+ spline -> points [0 ].y = twin_int_to_fixed (100 );
153
+ spline -> points [1 ].x = twin_int_to_fixed (300 );
154
+ spline -> points [1 ].y = twin_int_to_fixed (300 );
155
+ spline -> points [2 ].x = twin_int_to_fixed (100 );
156
+ spline -> points [2 ].y = twin_int_to_fixed (300 );
157
+ spline -> points [3 ].x = twin_int_to_fixed (300 );
158
+ spline -> points [3 ].y = twin_int_to_fixed (100 );
159
+ } else if (n_points == 3 ) {
160
+ spline -> points [0 ].x = twin_int_to_fixed (100 );
161
+ spline -> points [0 ].y = twin_int_to_fixed (100 );
162
+ spline -> points [1 ].x = twin_int_to_fixed (200 );
163
+ spline -> points [1 ].y = twin_int_to_fixed (300 );
164
+ spline -> points [2 ].x = twin_int_to_fixed (300 );
165
+ spline -> points [2 ].y = twin_int_to_fixed (100 );
166
+ }
141
167
}
142
168
143
- static apps_spline_t * apps_spline_create (twin_box_t * parent )
169
+ static apps_spline_t * apps_spline_create (twin_box_t * parent , int n_points )
144
170
{
145
171
apps_spline_t * spline = malloc (sizeof (apps_spline_t ));
146
172
147
- _apps_spline_init (spline , parent , _apps_spline_dispatch );
173
+ _apps_spline_init (spline , parent , _apps_spline_dispatch , n_points );
148
174
return spline ;
149
175
}
150
176
@@ -157,7 +183,12 @@ void apps_spline_start(twin_screen_t *screen,
157
183
{
158
184
twin_toplevel_t * toplevel = twin_toplevel_create (
159
185
screen , TWIN_ARGB32 , TwinWindowApplication , x , y , w , h , name );
160
- apps_spline_t * spline = apps_spline_create (& toplevel -> box );
161
- (void ) spline ;
186
+ apps_spline_t * spline_cubic = apps_spline_create (& toplevel -> box , 4 );
187
+ twin_toplevel_t * toplevel2 = twin_toplevel_create (
188
+ screen , TWIN_ARGB32 , TwinWindowApplication , x + 20 , y + 20 , w , h , name );
189
+ apps_spline_t * spline_quad = apps_spline_create (& toplevel2 -> box , 3 );
190
+ (void ) spline_cubic ;
191
+ (void ) spline_quad ;
162
192
twin_toplevel_show (toplevel );
193
+ twin_toplevel_show (toplevel2 );
163
194
}
0 commit comments