ear/extract; extras; samples: Fixes
[sdk] / samples / guiAndGfx / pictureRotateAlpha / rotate1.ec
1 public import "ecere"
2
3 class SimplePlane : Object
4 {
5    void Create(DisplaySystem displaySystem)
6    {
7       PrimitiveGroup group;
8       InitializeMesh(displaySystem);
9
10       mesh.Allocate( { vertices = true, texCoords1 = true, normals = true }, 4, displaySystem);
11       group = mesh.AddPrimitiveGroup({ vertexRange = true, primitiveType = quads }, 0);
12       group.first = 0;
13       group.nVertices = 4;
14       mesh.LockPrimitiveGroup(group);
15       mesh.vertices[0] = { -0.5f, 0, -.5f };
16       mesh.vertices[1] = { -0.5f, 0, .5f };
17       mesh.vertices[2] = {  0.5f, 0, .5f };
18       mesh.vertices[3] = {  0.5f, 0, -.5f };
19       mesh.texCoords[3] = { 1, 1 };
20       mesh.texCoords[2] = { 1, 0 };
21       mesh.texCoords[1] = { 0, 0 };
22       mesh.texCoords[0] = { 0, 1 };
23       mesh.normals[0] = { 0, 1, 0 };
24       mesh.normals[1] = { 0, 1, 0 };
25       mesh.normals[2] = { 0, 1, 0 };
26       mesh.normals[3] = { 0, 1, 0 };
27       mesh.UnlockPrimitiveGroup(group);
28       mesh.Unlock(0);
29       SetMinMaxRadius(true);
30       UpdateTransform();
31    }
32 }
33
34 public class PictureRotate : Window
35 {
36    Degrees angle;
37    double zoom;
38    BitmapResource image;
39    opacity = 0;
40    zoom = 1;
41    public property BitmapResource image
42    {
43       set
44       {
45          delete image;
46          AddResource(value);
47          RemoveResource(image);
48          image = value;
49          material.baseMap = image ? image.bitmap : null;
50          material.opacity = 0.5f;
51          material.flags.translucent = true;
52          Update(null);
53       }
54    }
55    public property Degrees angle
56    {
57       set
58       {
59          angle = value;
60          plane.transform.orientation = Euler { angle, 0, 0 };
61          plane.UpdateTransform();
62          Update(null);
63       }
64       get { return angle; }
65    }
66
67    public property double zoom
68    {
69       set
70       {
71          zoom = value;
72          Update(null);
73          size = initSize;
74       }
75       get { return zoom; }
76    }
77
78    Camera camera
79    {
80       type = fixed;
81       position = { 0, -275, 0 };
82       zMin = 0.1f,
83       orientation = Euler { 0, 90, 0 };
84    };
85
86    bool OnLoadGraphics()
87    {
88       int w, h;
89       Mesh mesh;
90       material.baseMap = image.bitmap;
91       plane.Create(displaySystem);
92       plane.transform.scaling = { 1, 1, 1 };
93
94       // plane.transform.scaling = { image.bitmap.width, 1.5f, image.bitmap.height };
95       //plane.transform.scaling = { Max(image.bitmap.width, image.bitmap.height), 1.5f, Max(image.bitmap.width, image.bitmap.height) };
96       //camera.aspectRatio = 1.0f;
97       if(!image.bitmap) return false;
98       w = image.bitmap.width;
99       h = image.bitmap.height;
100       camera.position.y = -Max(image.bitmap.height, image.bitmap.height) / sqrt(2);
101
102       plane.UpdateTransform();
103
104       mesh = plane.mesh;
105       mesh.Lock(0);
106       mesh.vertices[0] = { -w/2.0f, 0, -h/2.0f };
107       mesh.vertices[1] = { -w/2.0f, 0, h/2.0f };
108       mesh.vertices[2] = {  w/2.0f, 0, h/2.0f };
109       mesh.vertices[3] = {  w/2.0f, 0, -h/2.0f };
110       mesh.Unlock(0);
111
112       plane.mesh.ApplyMaterial(material);
113       plane.mesh.ApplyTranslucency(plane);
114       size = initSize;
115       return true;
116    }
117
118    bool OnResizing(int * w, int * h)
119    {
120       Size size = initSize;
121       Anchor anchor = this.anchor;
122       if(image && image.bitmap)
123       {
124          Bitmap bitmap = image.bitmap;
125          double m = Max(bitmap.width, bitmap.height) * zoom * sqrt(2);
126          if(!size.w && (!anchor.left.type || !anchor.right.type))
127             *w = (int)m;
128          if(!size.h && (!anchor.top.type || !anchor.bottom.type))
129             *h = (int)m;
130       }
131       else
132       {
133          if(!size.w && (!anchor.left.type || !anchor.right.type))
134             *w = 80;
135          if(!size.h && (!anchor.top.type || !anchor.bottom.type))
136             *h = 80;
137       }
138       return true;
139    }
140
141    void OnResize(int width, int height)
142    {
143       camera.Setup(width, height, null);
144    }
145
146    void OnRedraw(Surface surface)
147    {
148       camera.Update();
149       surface.Clear(depthBuffer);
150       display.SetCamera(surface, camera);
151       display.DrawObject(plane);
152       display.SetCamera(surface, null);
153    }
154
155    Material material
156    {
157       emissive = white;
158       opacity = 1.0f;
159       flags = { translucent = true, noFog = true, doubleSided = true };
160    };
161    SimplePlane plane { };
162 }
163
164 static Degrees startAngle;
165 static int startY, startX;
166 static bool rotating;
167
168 PictureRotate pic
169 {
170    displayDriver = "OpenGL",
171    background = 0,
172    alphaBlend = true,
173    moveable = true,
174    image = { (((GuiApplication)__thisModule).argc > 1) ? ((GuiApplication)__thisModule).argv[1] : "image.png" };
175    angle = 20;
176
177    bool OnLeftButtonDown(int x, int y, Modifiers mods)
178    {
179       MenuWindowMove(null, mods);
180       return true;
181    }
182
183    bool OnRightButtonDown(int x, int y, Modifiers mods)
184    {
185       if(!rotating)
186       {
187          Capture();
188          startAngle = angle;
189          startY = y;
190          startX = x;
191          rotating = true;
192       }
193       return true;
194    }
195
196    bool OnRightButtonUp(int x, int y, Modifiers mods)
197    {
198       if(rotating)
199       {
200          rotating = false;
201          ReleaseCapture();
202       }
203       return true;
204    }
205
206    bool OnMouseMove(int x, int y, Modifiers mods)
207    {
208       if(rotating)
209       {
210          int dx = x-startX, dy = y-startY;
211          if(Abs(dx) > Abs(dy))
212             property::angle = startAngle + Degrees { dx/2.0 };
213          else
214             property::angle = startAngle + Degrees { dy/2.0 };
215       }
216       return true;
217    }
218
219       bool OnKeyHit(Key key, unichar ch)
220       {
221          if(key == escape) Destroy(0);
222          else if(key == wheelDown)
223             property::zoom /= 1.1;
224          else if(key == wheelUp)
225             property::zoom *= 1.1;
226          return true;
227       }
228 };