samples: Various fixes
[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 #if defined(__EMSCRIPTEN__) || defined(__ANDROID__)
29    anchor = { 0, 0, 0, 0 };
30 #else
31    clientSize = { 640, 480 };
32    hasMaximize = true, hasMinimize = true, hasClose = true;
33    borderStyle = sizable;
34 #endif
35    background = slateGray;
36
37    // BitmapResource bg { ":ecere.bmp", window = this };
38
39    MekanoWnd()
40    {
41       RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
42       pbox.position = { GetRandom(0,500), 110 };
43       spring.link(MekanoPoint { fix, type = center }, pbox.getCertainPoint(topPoint));
44       lastbox = pbox;
45    }
46
47    ~MekanoWnd()
48    {
49       simulation.empty();
50    }
51
52    Timer timer
53    {
54       this, started = true;
55
56       bool DelayExpired()
57       {
58          simulation.resetForces();
59
60          simulation.exertForces();
61          simulation.stepDelta();
62
63          if(!buttondown) dragging = false;
64
65          for(object : simulation.objectList)
66          {
67             ObjectAttributes attributes=object.attributes;
68             if(attributes.selected)
69                if(dragging)
70                   object.position = { mouse.x, mouse.y };
71
72             attributes.selected = false;
73             attributes.steady = false;
74
75             if(object.isInside({ mouse.x, mouse.y }))
76             {
77                attributes.highlighted = true;
78                if(buttondown)
79                {
80                   dragging=true;
81                   attributes.selected = true;
82                   attributes.steady = true;
83                }
84             }
85             else
86             {
87                attributes.selected = false;
88                attributes.highlighted = false;
89             }
90             object.attributes = attributes;
91          }
92          Update(null);
93
94          return true;
95       }
96    };
97
98    void OnRedraw(Surface surface)
99    {
100       //surface.Tile(bg.bitmap, 0, 0, clientSize.w, clientSize.h);
101       display.surface = surface;
102       simulation.render(display);
103    }
104
105    bool OnRightButtonDown(int x, int y, Modifiers mods)
106    {
107       /*
108       fix = MekanoObjectFixed { simulation, position = { x, y } };
109       spring = { simulation, length = 30; restitution = 2; };
110
111       spring.link(MekanoPoint { fix, type = center }, pbox.centerPoint);
112       */
113
114       //int c; for(c = 0; c<400; c++) {
115       pbox =
116       {
117          simulation,
118          size = 20,
119          position = { x, y },
120          mass = 10
121       };
122
123       spring =
124       {
125          simulation,
126          length = 40,
127          restitution = 10
128       };
129
130       spring.link(lastbox.getCertainPoint(bottomPoint), pbox.getCertainPoint(topPoint));
131       lastbox=pbox;
132       //}
133       return true;
134    }
135
136    bool OnLeftButtonUp(int x, int y, Modifiers mods)
137    {
138       buttondown=false;
139       ReleaseCapture();
140       return true;
141    }
142
143    bool OnLeftButtonDown(int x, int y, Modifiers mods)
144    {
145       buttondown=true;
146       Capture();
147       return true;
148    }
149
150    bool OnMouseMove(int x, int y, Modifiers mods)
151    {
152       mouse = { x, y };
153       return true;
154    }
155
156    bool OnKeyDown(Key key, unichar ch)
157    {
158       switch(key)
159       {
160          case escape: Destroy(0); return false;
161          case f:      ((GuiApplication)__thisModule.application).fullScreen ^= true; return false;
162       }
163       return true;
164    }
165 }