samples/guiAndGfx: eC port of JFD's Mekano
[sdk] / samples / guiAndGfx / mekano / mekanownd.ec
1 import "mekanoobjectfixed"
2 import "mekanoobjectspring"
3 import "mekanopolygonalbox"
4 import "mekanoobjectgravity"
5
6 class MekanoWnd : Window
7 {
8 private:
9    Point mouse;
10    MekanoDisplay display { };
11    MekanoSimulation simulation { timeMultiplier = 4.0f };
12
13    MekanoObjectFixed fix;
14    fix = { simulation, position = { 300, 50 } };
15    MekanoPolygonalBox pbox;
16    pbox = { simulation, size = 20, mass = 10, inertiaMoment = 200 };
17    MekanoObjectSpring spring;
18    spring = { simulation, length = 60, restitution = 30 };
19
20    MekanoObjectGravity gravity { gravity = { 0, 10 } };
21
22    MekanoPolygonalBox lastbox;
23    bool selected, dragging, buttondown;
24
25 public:
26
27    caption = "Mekano";
28    hasMaximize = true, hasMinimize = true, hasClose = true;
29    borderStyle = sizable;
30    clientSize = { 640, 480 };
31    background = slateGray;
32
33    BitmapResource bg { ":ecere.bmp", window = this };
34
35    MekanoWnd()
36    {
37       RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
38       pbox.position = { GetRandom(0,500), 110 };
39       spring.link(MekanoPoint { fix, type = center }, pbox.getCertainPoint(topPoint));
40       lastbox = pbox;
41    }
42
43    ~MekanoWnd()
44    {
45       simulation.empty();
46    }
47
48    Timer timer
49    {
50       this, started = true;
51
52       bool DelayExpired()
53       {
54          simulation.resetForces();
55
56          simulation.exertForces();
57          simulation.stepDelta();
58
59          if(!buttondown) dragging = false;
60
61          for(object : simulation.objectList)
62          {
63             ObjectAttributes attributes=object.attributes;
64             if(attributes.selected)
65                if(dragging)
66                   object.position = { mouse.x, mouse.y };
67
68             attributes.selected = false;
69             attributes.steady = false;
70
71             if(object.isInside({ mouse.x, mouse.y }))
72             {
73                attributes.highlighted = true;
74                if(buttondown)
75                {
76                   dragging=true;
77                   attributes.selected = true;
78                   attributes.steady = true;
79                }
80             }
81             else
82             {
83                attributes.selected = false;
84                attributes.highlighted = false;
85             }
86             object.attributes = attributes;
87          }
88          Update(null);
89
90          return true;
91       }
92    };
93
94    void OnRedraw(Surface surface)
95    {
96       //surface.Tile(bg.bitmap, 0, 0, clientSize.w, clientSize.h);
97       display.surface = surface;
98       simulation.render(display);
99    }
100
101    bool OnRightButtonDown(int x, int y, Modifiers mods)
102    {
103       /*
104       fix = MekanoObjectFixed { simulation, position = { x, y } };
105       spring = { simulation, length = 30; restitution = 2; };
106
107       spring.link(MekanoPoint { fix, type = center }, pbox.centerPoint);
108       */
109
110       //int c; for(c = 0; c<400; c++) {
111       pbox =
112       {
113          simulation,
114          size = 20,
115          position = { x, y },
116          mass = 10
117       };
118
119       spring =
120       {
121          simulation,
122          length = 40,
123          restitution = 10
124       };
125
126       spring.link(lastbox.getCertainPoint(bottomPoint), pbox.getCertainPoint(topPoint));
127       lastbox=pbox;
128       //}
129       return true;
130    }
131
132    bool OnLeftButtonUp(int x, int y, Modifiers mods)
133    {
134       buttondown=false;
135       ReleaseCapture();
136       return true;
137    }
138
139    bool OnLeftButtonDown(int x, int y, Modifiers mods)
140    {
141       buttondown=true;
142       Capture();
143       return true;
144    }
145
146    bool OnMouseMove(int x, int y, Modifiers mods)
147    {
148       mouse = { x, y };
149       return true;
150    }
151
152    bool OnKeyDown(Key key, unichar ch)
153    {
154       switch(key)
155       {
156          case escape: Destroy(0); return false;
157          case f:      ((GuiApplication)__thisModule.application).fullScreen ^= true; return false;
158       }
159       return true;
160    }
161 }