d2259a2a255d59e9eed5e2ffe659897cc7a764ee
[sdk] / ide / src / documents / ModelView.ec
1 #ifdef ECERE_STATIC
2 import static "ecere"
3 #else
4 import "ecere"
5 #endif
6
7 class ModelView : Window
8 {
9    isActiveClient = true;
10    background = blue;
11    anchor = { left = 0, top = 0, right = 0, bottom = 0 };
12
13    FillModeValue fillMode;
14    bool moving, lightMoving;
15    char fileName[MAX_LOCATION];
16    Camera camera
17    {
18       attached,
19       fovDirection = vertical,
20       fov = 53,
21       position = { 0, 0, -10 },
22       eulerOrientation = Euler { 30, 0, 0 },
23       zMin = 1;
24       zMax = 10000;
25    };
26    Object cameraTarget { };
27    Object model {};
28    Point startPosition;
29    Euler startOrientation;
30    Light light
31    {
32       orientation = Euler { pitch = 50, yaw = 45 };
33       diffuse = white, specular = white;
34    }; // light.diffuse.r = light.diffuse.g = light.diffuse.b = 1; light.specular.r = light.specular.g = light.specular.b = 1;
35    Window help;
36
37    Timer timer
38    {
39       userData = this, delay = 0.05;
40       bool DelayExpired()
41       {
42          if(model)
43             model.frame++; //Animate(model.frame + 1);
44
45          Update(null);
46          return true;
47       }
48    };
49
50    property char * modelFile
51    {
52       set
53       {
54          strcpy(fileName, value);
55       }
56    }
57
58    void OnUnloadGraphics()
59    {
60       model.Free(displaySystem);
61       delete model;  // At the moment the reload preserving hierarchy position is not working, causing things to be misplaced in some models
62       model = { };
63    }
64
65    void OnDestroy()
66    {
67       DisplaySystem displaySystem = parent.displaySystem;
68       /*delete model;
69       model = { };*/
70       if(_class.count == 1 && parent.displaySystem)
71       {
72          displaySystem.ClearMaterials();
73          displaySystem.ClearTextures();
74          displaySystem.ClearMeshes();
75       }
76    }
77
78    void OnResize(int w, int h)
79    {
80       camera.Setup(w, h, null);
81       Update(null);
82    }
83
84    bool OnLeftButtonDown(int x, int y, Modifiers mods)
85    {
86       if(!moving && !lightMoving)
87       {
88          startPosition.x = x;
89          startPosition.y = y;
90          startOrientation = camera.eulerOrientation;
91          Capture();
92          moving = true;
93       }
94       return true;
95    }
96
97    bool OnLeftButtonUp(int x, int y, Modifiers mods)
98    {
99       if(moving)
100       {
101          ReleaseCapture();
102          moving = false;
103       }
104       return true;
105    }
106
107    bool OnRightButtonDown(int x, int y, Modifiers mods)
108    {
109       if(!moving && !lightMoving)
110       {
111          startPosition.x = x;
112          startPosition.y = y;
113          startOrientation = light.orientation;
114          Capture();
115          lightMoving = true;
116       }
117       return true;
118    }
119
120    bool OnRightButtonUp(int x, int y, Modifiers mods)
121    {
122       if(lightMoving)
123       {
124          ReleaseCapture();
125          lightMoving = false;
126       }
127       return true;
128    }
129
130    bool OnMouseMove(int x, int y, Modifiers mods)
131    {
132       if(moving)
133       {
134          Euler euler
135          {
136             startOrientation.yaw   - (x - startPosition.x),
137             startOrientation.pitch + (y - startPosition.y),
138             0
139          };
140
141          camera.eulerOrientation = euler;
142
143          Update(null);
144       }
145       else if(lightMoving)
146       {
147          light.orientation = Euler
148          {
149             startOrientation.yaw   - (x - startPosition.x),
150             startOrientation.pitch + (y - startPosition.y),
151             0
152          };
153
154          Update(null);
155       }
156       return true;
157    }
158
159    bool OnKeyDown(Key key, unichar ch)
160    {
161       switch(key)
162       {
163          case g:
164             fillMode = (fillMode == wireframe) ? solid : wireframe;
165             break;
166       }
167       return true;
168    }
169
170    bool OnKeyHit(Key key, unichar ch)
171    {
172       switch((SmartKey) key)
173       {
174          case wheelDown: case minus: camera.position.z *= 1.1111111f; Update(null); break;
175          case wheelUp: case plus: camera.position.z *= 0.9f; Update(null); break;
176          case up:    camera.position.y += camera.position.z / 20; Update(null); break;
177          case down:  camera.position.y -= camera.position.z / 20; Update(null); break;
178          case left:    camera.position.x += camera.position.z / 20; Update(null); break;
179          case right:  camera.position.x -= camera.position.z / 20; Update(null); break;
180          /*
181             case pageDown: model.Animate(model.frame + 1); break;
182             case pageUp: model.Animate(model.frame - 1); break;
183             case home: model.Animate(model.startFrame); break;
184             case end: model.Animate(model.endFrame); break;
185          */
186       }
187       return true;
188    }
189
190    bool OnLoadGraphics()
191    {
192       if(model.Load(fileName, null, displaySystem))
193       {
194          float r = model.radius;
195          cameraTarget.transform.position =
196          {
197             (model.max.x + model.min.x) / 2,
198             (model.max.y + model.min.y) / 2,
199             (model.max.z + model.min.z) / 2
200          };
201
202          camera.zMin = 1;
203          camera.zMax = 10000;
204          camera.position = { 0, 0, -r * 2 };
205          camera.eulerOrientation = Euler { 30, 0, 0 };
206          if(r * 2 < camera.zMax / 10)
207          {
208             while(r * 2 < camera.zMax / 100)
209             {
210                camera.zMax /= 10;
211                camera.zMin /= 10;
212             }
213          }
214          else
215             while(r * 2 > camera.zMax / 10)
216             {
217                camera.zMax *= 10;
218                camera.zMin *= 10;
219             }
220
221          camera.target = cameraTarget;
222          return true;
223       }
224       return false;
225    }
226
227    void OnRedraw(Surface surface)
228    {
229       //display.fogColor = blue;
230       display.fogDensity = 0; //0.000001f;
231
232       surface.SetBackground(lightBlue);
233       surface.Clear(colorAndDepth);
234
235       camera.Update();
236
237       display.SetCamera(surface, camera);
238
239       display.SetLight(0, light);
240
241       display.fillMode = fillMode;
242       display.DrawObject(model);
243       display.fillMode = solid;
244
245       display.SetCamera(surface, null);
246
247       surface.SetForeground(black);
248    }
249
250    ModelView()
251    {
252       fillMode = solid;
253       light.orientation.Yaw(0);
254       // timer.Start();
255    }
256
257    void SetFormat(char * format)
258    {
259       OnLoadGraphics();
260    }
261
262 }