samples/guiAndGfx: eC port of JFD's Mekano
[sdk] / samples / guiAndGfx / mekano / mekanodisplay.ec
1 import "mekanopolygon"
2
3 define COLORSPRING = orange;
4 define COLORPOLY   = magenta;
5 define COLORBOX    = blue;
6 define COLORCROSS  = lime;
7
8 class MekanoDisplay
9 {
10    Surface surface;
11
12 public:
13    void drawPolygon(Vector2D position, MekanoPolygon polygon)
14    {
15       List<MekanoPoint> points=polygon.points;
16       if(points.count > 1)
17       {
18          MekanoPoint point1, point2 = null, firstpoint = null;
19          Vector2D point_pos1, point_pos2;
20          bool first = true;
21
22          surface.foreground = COLORPOLY;
23
24          for(p : points)
25          {
26             if(first)
27             {
28                point2 = firstpoint = p;
29                first = false;
30             }
31             else
32             {
33                point1 = point2;
34                point2 = p;
35                point_pos1=point1.localPosition;
36                point_pos2=point2.localPosition;
37
38                surface.DrawLine((int)(position.x+point_pos1.x), (int)(position.y+point_pos1.y),
39                   (int)(position.x+point_pos2.x), (int)(position.y+point_pos2.y));
40             }
41          }
42          point_pos1=point2.localPosition;
43          point_pos2=firstpoint.localPosition;
44          surface.DrawLine((int)(position.x+point_pos1.x), (int)(position.y+point_pos1.y),
45             (int)(position.x+point_pos2.x), (int)(position.y+point_pos2.y));
46       }
47    }
48
49    void drawBox(Vector2D position, int width)
50    {
51       surface.foreground = COLORBOX;
52       surface.Rectangle(
53          (short)position.x-width/2, (short)position.y-width/2,
54          (short)position.x+width/2, (short)position.y+width/2);
55    }
56
57    void drawCrossHair(Vector2D position, int width)
58    {
59       surface.foreground = COLORCROSS;
60       surface.HLine((short)position.x-width/2, (short)position.x+width/2,
61          (short)position.y);
62       surface.VLine((short)position.y-width/2, (short)position.y+width/2,
63          (short)position.x);
64    }
65
66    void drawSpring(Vector2D pos1, Vector2D pos2, int zigs)
67    {
68       int t;
69       float lzig, h = 10.0;
70       Vector2D d=pos1;
71       Vector2D o;
72       Vector2D u, n;
73       Vector2D a1, a2;
74
75       surface.foreground = COLORSPRING;
76
77       o.subtract(pos2, pos1);
78
79       u=o.unit;
80       n=o.normal;
81       lzig=o.length/zigs;
82
83       // a1=u*(lzig*1/4)+n*h;
84       {
85          Vector2D t1, t2;
86          t1.scale(u, lzig*1/4);
87          t2.scale(n, h);
88          a1.add(t1, t2);
89       }
90
91       // a2=u*(lzig*2/4)-n*h*2.0;
92       {
93          Vector2D t1, t2;
94          t1.scale(u, lzig*2/4);
95          t2.scale(n, h*2.0f);
96          a2.subtract(t1, t2);
97       }
98
99       for (t=0; t<zigs; t++)
100       {
101          Vector2D end;
102          end.add(d, a1);
103          surface.DrawLine((int)d.x, (int)d.y, (int)end.x, (int)end.y);
104          d = end;
105
106          end.add(d, a2);
107          surface.DrawLine((int)d.x, (int)d.y, (int)end.x, (int)end.y);
108          d = end;
109
110          end.add(d, a1);
111          surface.DrawLine((int)d.x, (int)d.y, (int)end.x, (int)end.y);
112          d = end;
113       }
114    }
115 }