summaryrefslogtreecommitdiff
path: root/src/corelib/render/software/ctrl/agg_spline_ctrl.pas
blob: 71e05a1ace49df91ac4f0ddacfd6dffeb96466bc (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4 (Public License)
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Anti-Grain Geometry - Version 2.4 Release Milano 3 (AggPas 2.4 RM3)
// Pascal Port By: Milan Marusinec alias Milano
//                 milan@marusinec.sk
//                 http://www.aggpas.org
// Copyright (c) 2005-2006
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//
//----------------------------------------------------------------------------
//
// classes spline_ctrl_impl, spline_ctrl
//
// Class that can be used to create an interactive control to set up
// gamma arrays.
//
// [Pascal Port History] -----------------------------------------------------
//
// 23.02.2006-Milano: Unit port establishment
//
{ agg_spline_ctrl.pas }
unit
 agg_spline_ctrl ;

INTERFACE

{$I agg_mode.inc }

uses
 agg_basics ,
 agg_ellipse ,
 agg_bspline ,
 agg_conv_stroke ,
 agg_path_storage ,
 agg_trans_affine ,
 agg_color ,
 agg_ctrl ,
 agg_math ;

{ TYPES DEFINITION }
type
 spline_ctrl_impl = object(ctrl )
   m_num_pnt : unsigned;

   m_xp ,
   m_yp : array[0..31 ] of double;

   m_spline : bspline;

   m_spline_values  : array[0..255 ] of double;
   m_spline_values8 : array[0..255 ] of int8u;
   m_border_width   ,
   m_border_extra   ,
   m_curve_width    ,
   m_point_size     ,

   m_xs1 ,
   m_ys1 ,
   m_xs2 ,
   m_ys2 : double;

   m_curve_pnt  : path_storage;
   m_curve_poly : conv_stroke;
   m_ellipse    : ellipse;

   m_idx    ,
   m_vertex : unsigned;

   m_vx ,
   m_vy : array[0..31 ] of double;

   m_active_pnt ,
   m_move_pnt   : int;

   m_pdx ,
   m_pdy : double;

   constructor Construct(x1 ,y1 ,x2 ,y2 : double; num_pnt : unsigned; flip_y : boolean = false );
   destructor  Destruct; virtual;

  // Set other parameters
   procedure border_width_(t : double; extra : double = 0.0 );
   procedure curve_width_ (t : double );
   procedure point_size_  (s : double );

  // Event handlers. Just call them if the respective events
  // in your system occure. The functions return true if redrawing
  // is required.
   function  in_rect(x ,y : double ) : boolean; virtual;

   function  on_mouse_button_down(x ,y : double ) : boolean; virtual;
   function  on_mouse_button_up  (x ,y : double ) : boolean; virtual;

   function  on_mouse_move(x ,y : double; button_flag : boolean ) : boolean; virtual;
   function  on_arrow_keys(left ,right ,down ,up : boolean ) : boolean; virtual;

  // Spline
   procedure active_point_(i : int );

   function  _spline : double_ptr;
   function  _spline8 : int8u_ptr;

   function  _value(x : double ) : double;
   procedure value_(idx : unsigned; y : double );
   procedure point_(idx : unsigned; x ,y : double );

   procedure x_(idx : unsigned; x : double );
   procedure y_(idx : unsigned; y : double );
   function  _x(idx : unsigned ) : double;
   function  _y(idx : unsigned ) : double;

   procedure update_spline;

  // Vertex soutce interface
   function  num_paths : unsigned; virtual;
   procedure rewind(path_id : unsigned ); virtual;
   function  vertex(x ,y : double_ptr ) : unsigned; virtual;

  // Private
   procedure calc_spline_box;
   procedure calc_curve;
   function  calc_xp(idx : unsigned ) : double;
   function  calc_yp(idx : unsigned ) : double;
   procedure set_xp (idx : unsigned; val : double );
   procedure set_yp (idx : unsigned; val : double );

  end;

 spline_ctrl = object(spline_ctrl_impl )
   m_background_color   ,
   m_border_color       ,
   m_curve_color        ,
   m_inactive_pnt_color ,
   m_active_pnt_color   : aggclr;

   m_colors : array[0..4 ] of aggclr_ptr;

   constructor Construct(x1 ,y1 ,x2 ,y2 : double; num_pnt : unsigned; flip_y : boolean = false );

   procedure background_color_  (c : aggclr_ptr );
   procedure border_color_      (c : aggclr_ptr );
   procedure curve_color_       (c : aggclr_ptr );
   procedure inactive_pnt_color_(c : aggclr_ptr );
   procedure active_pnt_color_  (c : aggclr_ptr );

   function  _color(i : unsigned ) : aggclr_ptr; virtual;

  end;

{ GLOBAL PROCEDURES }


IMPLEMENTATION
{ LOCAL VARIABLES & CONSTANTS }
{ UNIT IMPLEMENTATION }
{ CONSTRUCT }
constructor spline_ctrl_impl.Construct;
var
 i : unsigned;

begin
 inherited Construct(x1 ,y1 ,x2 ,y2 ,flip_y );

 m_num_pnt:=num_pnt;

 m_border_width:=1.0;
 m_border_extra:=0.0;
 m_curve_width :=1.0;
 m_point_size  :=3.0;

 m_spline.Construct;
 m_curve_pnt.Construct;
 m_curve_poly.Construct(@m_curve_pnt );
 m_ellipse.Construct;

 m_idx       :=0;
 m_vertex    :=0;
 m_active_pnt:=-1;
 m_move_pnt  :=-1;

 m_pdx:=0.0;
 m_pdy:=0.0;

 if m_num_pnt < 4 then
  m_num_pnt:=4;

 if m_num_pnt > 32 then
  m_num_pnt:=32;

 for i:=0 to m_num_pnt - 1 do
  begin
   m_xp[i ]:=i / (m_num_pnt - 1 );
   m_yp[i ]:=0.5;

  end;

 calc_spline_box;
 update_spline;

end;

{ DESTRUCT }
destructor spline_ctrl_impl.Destruct;
begin
 m_spline.Destruct;
 m_curve_pnt.Destruct;
 m_curve_poly.Destruct;

end;

{ BORDER_WIDTH_ }
procedure spline_ctrl_impl.border_width_;
begin
 m_border_width:=t;
 m_border_extra:=extra;

 calc_spline_box;

end;

{ CURVE_WIDTH_ }
procedure spline_ctrl_impl.curve_width_;
begin
 m_curve_width:=t;

end;

{ POINT_SIZE_ }
procedure spline_ctrl_impl.point_size_;
begin
 m_point_size:=s;

end;

{ IN_RECT }
function spline_ctrl_impl.in_rect;
begin
 inverse_transform_xy(@x ,@y );

 result:=
  (x >= m_x1 ) and
  (x <= m_x2 ) and
  (y >= m_y1 ) and
  (y <= m_y2 );

end;

{ ON_MOUSE_BUTTON_DOWN }
function spline_ctrl_impl.on_mouse_button_down;
var
 i : unsigned;

 xp ,yp : double;

begin
 inverse_transform_xy(@x ,@y );

 for i:=0 to m_num_pnt - 1 do
  begin
   xp:=calc_xp(i );
   yp:=calc_yp(i );

   if calc_distance(x ,y ,xp ,yp ) <= m_point_size + 1 then
    begin
     m_pdx:=xp - x;
     m_pdy:=yp - y;

     m_active_pnt:=i;
     m_move_pnt  :=i;

     result:=true;

     exit;

    end;

  end;

 result:=false;

end;

{ ON_MOUSE_BUTTON_UP }
function spline_ctrl_impl.on_mouse_button_up;
begin
 if m_move_pnt >= 0 then
  begin
   m_move_pnt:=-1;

   result:=true;

  end
 else
  result:=false;

end;

{ ON_MOUSE_MOVE }
function spline_ctrl_impl.on_mouse_move;
var
 xp ,yp : double;

begin
 inverse_transform_xy(@x ,@y );

 if not button_flag then
  begin
   result:=on_mouse_button_up(x ,y );

   exit;

  end;

 if m_move_pnt >= 0 then
  begin
   xp:=x + m_pdx;
   yp:=y + m_pdy;

   set_xp(m_move_pnt ,(xp - m_xs1 ) / (m_xs2 - m_xs1 ) );
   set_yp(m_move_pnt ,(yp - m_ys1 ) / (m_ys2 - m_ys1 ) );

   update_spline;

   result:=true;

  end
 else
  result:=false;

end;

{ ON_ARROW_KEYS }
function spline_ctrl_impl.on_arrow_keys;
var
 kx ,ky : double;

 ret : boolean;

begin
 kx :=0.0;
 ky :=0.0;
 ret:=false;

 if m_active_pnt >= 0 then
  begin
   kx:=m_xp[m_active_pnt ];
   ky:=m_yp[m_active_pnt ];

   if left then
    begin
     kx :=kx - 0.001;
     ret:=true;

    end;

   if right then
    begin
     kx :=kx + 0.001;
     ret:=true;

    end;

   if down then
    begin
     ky :=ky - 0.001;
     ret:=true;

    end;

   if up then
    begin
     ky :=ky + 0.001;
     ret:=true;

    end;

  end;

 if ret then
  begin
   set_xp(m_active_pnt ,kx );
   set_yp(m_active_pnt ,ky );

   update_spline;

  end;

 result:=ret;

end;

{ ACTIVE_POINT_ }
procedure spline_ctrl_impl.active_point_;
begin
 m_active_pnt:=i;

end;

{ _SPLINE }
function spline_ctrl_impl._spline;
begin
 result:=@m_spline_values;

end;

{ _SPLINE8 }
function spline_ctrl_impl._spline8;
begin
 result:=@m_spline_values8;

end;

{ _VALUE }
function spline_ctrl_impl._value;
begin
 x:=m_spline.get(x );

 if x < 0.0 then
  x:=0.0;

 if x > 1.0 then
  x:=1.0;

 result:=x;

end;

{ VALUE_ }
procedure spline_ctrl_impl.value_;
begin
 if idx < m_num_pnt then
  set_yp(idx ,y );

end;

{ POINT_ }
procedure spline_ctrl_impl.point_;
begin
 if idx < m_num_pnt then
  begin
   set_xp(idx ,x );
   set_yp(idx ,y );

  end;

end;

{ X_ }
procedure spline_ctrl_impl.x_;
begin
 m_xp[idx ]:=x;

end;

{ Y_ }
procedure spline_ctrl_impl.y_;
begin
 m_yp[idx ]:=y;

end;

{ _X }
function spline_ctrl_impl._x;
begin
 result:=m_xp[idx ];

end;

{ _Y }
function spline_ctrl_impl._y;
begin
 result:=m_yp[idx ];

end;

{ UPDATE_SPLINE }
procedure spline_ctrl_impl.update_spline;
var
 i : int;

begin
 m_spline.init(m_num_pnt ,@m_xp ,@m_yp );

 for i:=0 to 255 do
  begin
   m_spline_values[i ]:=m_spline.get(i / 255.0 );

   if m_spline_values[i ] < 0.0 then
    m_spline_values[i ]:=0.0;

   if m_spline_values[i ] > 1.0 then
    m_spline_values[i ]:=1.0;

   m_spline_values8[i ]:=int8u(trunc(m_spline_values[i ] * 255.0 ) );

  end;

end;

{ NUM_PATHS }
function spline_ctrl_impl.num_paths;
begin
 result:=5;

end;

{ REWIND }
procedure spline_ctrl_impl.rewind;
var
 i : unsigned;

begin
 m_idx:=path_id;

 case path_id of
  0 : // Background
   begin
    m_vertex:=0;

    m_vx[0 ]:=m_x1 - m_border_extra;
    m_vy[0 ]:=m_y1 - m_border_extra;
    m_vx[1 ]:=m_x2 + m_border_extra;
    m_vy[1 ]:=m_y1 - m_border_extra;
    m_vx[2 ]:=m_x2 + m_border_extra;
    m_vy[2 ]:=m_y2 + m_border_extra;
    m_vx[3 ]:=m_x1 - m_border_extra;
    m_vy[3 ]:=m_y2 + m_border_extra;

   end;

  1 : // Border
   begin
    m_vertex:=0;

    m_vx[0 ]:=m_x1;
    m_vy[0 ]:=m_y1;
    m_vx[1 ]:=m_x2;
    m_vy[1 ]:=m_y1;
    m_vx[2 ]:=m_x2;
    m_vy[2 ]:=m_y2;
    m_vx[3 ]:=m_x1;
    m_vy[3 ]:=m_y2;
    m_vx[4 ]:=m_x1 + m_border_width;
    m_vy[4 ]:=m_y1 + m_border_width;
    m_vx[5 ]:=m_x1 + m_border_width;
    m_vy[5 ]:=m_y2 - m_border_width;
    m_vx[6 ]:=m_x2 - m_border_width;
    m_vy[6 ]:=m_y2 - m_border_width;
    m_vx[7 ]:=m_x2 - m_border_width;
    m_vy[7 ]:=m_y1 + m_border_width;

   end;

  2 : // Curve
   begin
    calc_curve;

    m_curve_poly.width_(m_curve_width );
    m_curve_poly.rewind(0 );

   end;

  3 : // Inactive points
   begin
    m_curve_pnt.remove_all;

    for i:=0 to m_num_pnt - 1 do
     if i <> m_active_pnt then
      begin
       m_ellipse.init(
        calc_xp(i ) ,calc_yp(i ) ,
        m_point_size ,m_point_size ,32 );

       m_curve_pnt.add_path(@m_ellipse ,0 ,false );

      end;

    m_curve_poly.rewind(0 );

   end;

  4 : // Active point
   begin
    m_curve_pnt.remove_all;

    if m_active_pnt >= 0 then
     begin
      m_ellipse.init(
       calc_xp(m_active_pnt ) ,calc_yp(m_active_pnt ) ,
       m_point_size ,m_point_size ,32 );

      m_curve_pnt.add_path(@m_ellipse );

     end;

    m_curve_poly.rewind(0 );

   end;

 end;

end;

{ VERTEX }
function spline_ctrl_impl.vertex;
var
 cmd : unsigned;

begin
 cmd:=path_cmd_line_to;

 case m_idx of
  0 :
   begin
    if m_vertex = 0 then
     cmd:=path_cmd_move_to;

    if m_vertex >= 4 then
     cmd:=path_cmd_stop;

    x^:=m_vx[m_vertex ];
    y^:=m_vy[m_vertex ];

    inc(m_vertex );

   end;

  1 :
   begin
    if (m_vertex = 0 ) or
       (m_vertex = 4 ) then
     cmd:=path_cmd_move_to;

    if m_vertex >= 8 then
     cmd:=path_cmd_stop;

    x^:=m_vx[m_vertex ];
    y^:=m_vy[m_vertex ];

    inc(m_vertex );

   end;

  2 :
   cmd:=m_curve_poly.vertex(x ,y );

  3 ,4 :
   cmd:=m_curve_pnt.vertex(x ,y );

  else
   cmd:=path_cmd_stop;

 end;

 if not is_stop(cmd ) then
  transform_xy(x ,y );

 result:=cmd;

end;

{ CALC_SPLINE_BOX }
procedure spline_ctrl_impl.calc_spline_box;
begin
 m_xs1:=m_x1 + m_border_width;
 m_ys1:=m_y1 + m_border_width;
 m_xs2:=m_x2 - m_border_width;
 m_ys2:=m_y2 - m_border_width;

end;

{ CALC_CURVE }
procedure spline_ctrl_impl.calc_curve;
var
 i : int;

begin
 m_curve_pnt.remove_all;
 m_curve_pnt.move_to(m_xs1 ,m_ys1 + (m_ys2 - m_ys1 ) * m_spline_values[0 ] );

 for i:=1 to 255 do
  m_curve_pnt.line_to(
   m_xs1 + (m_xs2 - m_xs1 ) * i / 255.0 ,
   m_ys1 + (m_ys2 - m_ys1 ) * m_spline_values[i ] );

end;

{ CALC_XP }
function spline_ctrl_impl.calc_xp;
begin
 result:=m_xs1 + (m_xs2 - m_xs1 ) * m_xp[idx ];

end;

{ CALC_YP }
function spline_ctrl_impl.calc_yp;
begin
 result:=m_ys1 + (m_ys2 - m_ys1 ) * m_yp[idx ];

end;

{ SET_XP }
procedure spline_ctrl_impl.set_xp;
begin
 if val < 0.0 then
  val:=0.0;

 if val > 1.0 then
  val:=1.0;

 if idx = 0 then
  val:=0.0
 else
  if idx = m_num_pnt - 1 then
   val:=1.0
  else
   begin
    if val < m_xp[idx - 1 ] + 0.001 then
     val:=m_xp[idx - 1 ] + 0.001;

    if val > m_xp[idx + 1 ] - 0.001 then
     val:=m_xp[idx + 1 ] - 0.001;

   end;

 m_xp[idx ]:=val;

end;

{ SET_YP }
procedure spline_ctrl_impl.set_yp;
begin
 if val < 0.0 then
  val:=0.0;

 if val > 1.0 then
  val:=1.0;

 m_yp[idx ]:=val;

end;

{ CONSTRUCT }
constructor spline_ctrl.Construct;
begin
 inherited Construct(x1 ,y1 ,x2 ,y2 ,num_pnt ,flip_y );

 m_background_color.ConstrDbl  (1.0 ,1.0 ,0.9 );
 m_border_color.ConstrDbl      (0.0 ,0.0 ,0.0 );
 m_curve_color.ConstrDbl       (0.0 ,0.0 ,0.0 );
 m_inactive_pnt_color.ConstrDbl(0.0 ,0.0 ,0.0 );
 m_active_pnt_color.ConstrDbl  (1.0 ,0.0 ,0.0 );

 m_colors[0 ]:=@m_background_color;
 m_colors[1 ]:=@m_border_color;
 m_colors[2 ]:=@m_curve_color;
 m_colors[3 ]:=@m_inactive_pnt_color;
 m_colors[4 ]:=@m_active_pnt_color;

end;

{ BACKGROUND_COLOR_ }
procedure spline_ctrl.background_color_;
begin
 m_background_color:=c^;

end;

{ BORDER_COLOR_ }
procedure spline_ctrl.border_color_;
begin
 m_border_color:=c^;

end;

{ CURVE_COLOR_ }
procedure spline_ctrl.curve_color_;
begin
 m_curve_color:=c^;

end;

{ INACTIVE_PNT_COLOR_ }
procedure spline_ctrl.inactive_pnt_color_;
begin
 m_inactive_pnt_color:=c^;

end;

{ ACTIVE_PNT_COLOR_ }
procedure spline_ctrl.active_pnt_color_;
begin
 m_active_pnt_color:=c^;

end;

{ _COLOR }
function spline_ctrl._color;
begin
 result:=m_colors[i ];

end;

END.