1 module Engine.Camera;
2 
3 import Engine.Component;
4 import Engine.Texture;
5 import Engine.Core;
6 import Engine.math;
7 import Engine.Input;
8 
9 class Camera  {
10 	mixin ComponentBase;
11 	// This will identify our vertex buffer
12 
13 	package mat4 projection;
14 
15 	float size = 1;
16 	recti rect;
17 	recti realRect;
18 	vec2i  center;
19 
20 	this() {
21 		UpdateResolution();
22 	}
23 
24 	mat4 Projection() {
25 		return projection;
26 	}
27 
28 	//Mouse world position
29 	vec2 MouseWorldPosition() {
30 		auto v = MouseLocalPosition();
31 		return ScreenToWorld(v.x, v.y);
32 	}
33 
34 	//Takes a point on the screen and turns it into point on world
35 	vec2 ScreenToWorld(float x, float y)  {
36 		auto p = transform.position;
37 		return vec2(p.x + x,p.y + y);
38 		//return (vec4(x,y,0,1) * transform.Matrix()).xy;
39 	}
40 
41 	//Mouse local position
42 	vec2 MouseLocalPosition() {
43 		auto p = Input.MousePosition();
44 		p *= size;
45 		p.x += realRect.min.x;
46 		p.y = realRect.max.y-p.y;
47 		return p;
48 	}
49 
50 	recti bounds() {
51 		auto r = realRect;
52 		r.add(vec2i(cast(int)transform.position.x,cast(int)transform.position.y));
53 		return r;
54 	}
55 
56 	//Updates the Projection
57 	void UpdateResolution() {
58 		rect.min.x = -(Core.width) / 2;
59 		rect.max.x = (Core.width) / 2;
60 		rect.min.y = -(Core.height) / 2;
61 		rect.max.y = (Core.height) / 2;
62 
63 		realRect.min.x = center.x - (center.x - rect.min.x);
64 		realRect.max.x = center.x - (center.x - rect.max.x);
65 		realRect.min.y = center.y - (center.y - rect.min.y);
66 		realRect.max.y = center.y - (center.y - rect.max.y);
67 
68 		realRect.min.x = cast(int)(center.x - (center.x-rect.min.x)*size);
69 		realRect.max.x = cast(int)(center.x - (center.x-rect.max.x)*size);
70 		realRect.min.y = cast(int)(center.y - (center.y-rect.min.y)*size);
71 		realRect.max.y = cast(int)(center.y - (center.y-rect.max.y)*size);
72 
73 		projection = mat4().orthographic(realRect.min.x, realRect.max.x, realRect.min.y, realRect.max.y, -100000, 100000);
74 	}
75 
76 	mat4 View() {
77 		return transform.InvertedMatrix();
78 	}
79 }
80