summaryrefslogtreecommitdiff
path: root/plugins/Scale.cs
blob: ad6aa72db84d53b0aa2738820f111a2d72f27a6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using Compiz;
using PhysModel;

public class MscalePlugin : Plugin
{
	//not very clever or efficient, but ok for now
	protected Dictionary<IntPtr,Screen> screens = new Dictionary<IntPtr,Screen> ();
	protected Dictionary<IntPtr,Window> windowPtrs = new Dictionary<IntPtr,Window> ();
	protected List<Window> windows = new List<Window> ();
	protected Dictionary<Window,Particle> particles = new Dictionary<Window,Particle> ();

	Model model = new Model ();

	public override bool InitScreen (IntPtr plugin, IntPtr screen)
	{
		Console.WriteLine ("initScreen");

		Screen s = new Screen (screen);
		screens[screen] = s;

		s.PreparePaint += OnPreparePaintScreen;
		//s.Paint += OnPaintScreen;
		s.DonePaint += OnDonePaintScreen;

		InitModelConstants (model);

		//add the enclosing rectangle to the physics model
		//XRectangle s->workArea
		//Polygon bounds = CreateRectangle (true, 0, 0, 800, 600);
		//Polygon bounds = CreateRectangle (true, 0, 0, 640, 480);
		//Polygon bounds = CreateRectangle (false, 50, 50, 640, 480);
		Polygon bounds = CreateRectangle (true, 50, 50, 640, 480);
		bounds.Model = model;
		model.Polygons.Add (bounds);
		model.Constraints.Add (bounds);

		InitScale ();

		return true;
	}

	public static void InitModelConstants (Model model)
	{
		model.Elasticity = 0.7;
		model.Friction = 10;
		model.Gravity = new Vector (0, 80);
		//model.Gravity = new Vector (0, 0);
		model.K = 10;
		model.ConstrainIterations = 10;
	}

	Anchor anchor;

	public void InitScale ()
	{
		Particle anchorParticle = new Particle ();
		model.Particles.Add (anchorParticle);

		//Anchor anchor = new Anchor ();
		anchor = new Anchor ();
		anchor.Particle = anchorParticle;
		anchor.Position = new PhysModel.Point (320, 240);
		anchor.Particle.Position = anchor.Position;
		anchor.Particle.PreviousPosition = anchor.Position; //new
		model.Anchors.Add (anchor);
		model.Constraints.Add (anchor);
	}

	public Particle NewParticle (int x, int y)
	{
		int distance = 40;

		Particle current = new Particle ();
		//current.Position = new PhysModel.Point (200 + i * distance / 3, 40);
		//current.Position = new PhysModel.Point (10, 10);
		current.Position = new PhysModel.Point (x, y);
		current.PreviousPosition = current.Position;
		current.Mass = 2;
		model.Particles.Add (current);

		Spring s = new Spring ();
		s.A = anchor.Particle;
		s.B = current;
		s.Length = distance;
		model.Springs.Add (s);
		//model.Constraints.Add (s);

		foreach (Particle part in model.Particles) {
			//skip the anchor particle
			if (part == anchor.Particle || part == current)
				continue;
			Spacer spc = new Spacer ();
			spc.A = part;
			spc.B = current;
			spc.Length = distance;
			model.Spacers.Add (spc);
			model.Constraints.Add (spc);
		}

		return current;
	}

	//TODO: make this a library function
	public static Polygon CreateRectangle (bool enclosing, double x0, double y0, double x1, double y1)
	{
		Polygon p = new Polygon ();

		p.Enclosing = enclosing;

		p.Points.Add (new PhysModel.Point (x0, y0));
		p.Points.Add (new PhysModel.Point (x0, y1));
		p.Points.Add (new PhysModel.Point (x1, y1));
		p.Points.Add (new PhysModel.Point (x1, y0));

		//hack
		p.Init ();

		return p;
	}

	bool changed = false;

	public void OnPreparePaintScreen (PreparePaintScreenHandler inner, IntPtr s, int msSinceLastPaint)
	{
		Screen screen = screens[s];

		Console.Error.WriteLine ("PreparePaintScreen (managed) " + msSinceLastPaint);

		foreach (Window win in windows)
		{

				//win.SyncPosition ();
			int x, y;
			win.GetPosition (out x, out y);
			if (x < 1 | y < 1)
				continue;
			UpdateWindow (win);

			/*
			//if (x > 800 | y > 600)
			//	continue;


			//particles[win].PreviousPosition = particles[win].Position;
			particles[win].Position = new PhysModel.Point (x, y);
			//particles[win].PreviousPosition = new PhysModel.Point (x, y);
			*/
		}

		model.Step (msSinceLastPaint / 200);

		changed = false;

		//foreach (Window win in windows)
		//	UpdateWindow (win);

		//if (false)
		foreach (Window win in windows)
		{
			//win.Move (dx, dy, false, false);
			int x, y;

			win.GetPosition (out x, out y);

		int width, height;
		win.GetSize (out width, out height);
		x += width/2;
		y += height/2;

			if (x < 1 | y < 1)
				continue;

			//if (x > 800 | y > 600)
			//	continue;

			PhysModel.Point oldpoint = new PhysModel.Point (x, y);
			//PhysModel.Point oldpoint = particles[win].PreviousPosition;
			Console.WriteLine ("old: " + oldpoint.X + ", " + oldpoint.Y);

			PhysModel.Point point = particles[win].Position;
			Console.WriteLine ("new: " + point.X + ", " + point.Y);

			PhysModel.Vector vec = point - oldpoint;
			//PhysModel.Vector vec = oldpoint - point;
			Console.WriteLine ("delta: " + vec.X + ", " + vec.Y);

			if ((int)vec.X != 0 | (int)vec.Y != 0) {
				changed = true;
				win.Move ((int)vec.X, (int)vec.Y, false, false);
				win.SyncPosition ();
			}
		}

		inner (s, msSinceLastPaint);
	}

	public bool OnPaintScreen (PaintScreenHandler inner, IntPtr s, ref ScreenPaintAttrib sa, IntPtr region, ScreenPaint mask)
	{
		Screen screen = screens[s];

		Console.Error.WriteLine ("PaintScreen (managed) " + " " +  s + " " + mask);

		Console.Error.WriteLine ("xRotate: " + sa.xRotate);
		Console.Error.WriteLine ("yRotate: " + sa.yRotate);
		Console.Error.WriteLine ("vRotate: " + sa.vRotate);
		//Console.Error.WriteLine (": " + my_screen.);

		//restrict rotation on x
		if (sa.xRotate > 6)
			sa.xRotate = 6f;
		else if (sa.xRotate < -6)
			sa.xRotate = -6f;

		//restrict rotation on v
		if (sa.vRotate > 6)
			sa.vRotate = 6f;
		else if (sa.vRotate < -6)
			sa.vRotate = -6f;

		//zoom out
		//sa.zCamera = -1f;

		//mask &= ScreenPaint.Region;
		//mask |= ScreenPaint.Transformed;

		bool ret = inner (s, ref sa, region, mask);

		return ret;
	}

	protected int grabIndex;

	public void OnDonePaintScreen (DonePaintScreenHandler inner, IntPtr s)
	{
		Screen screen = screens[s];

		Console.Error.WriteLine ("DonePaintScreen (managed)");

		if (false)
			screen.RemoveGrab (grabIndex);

		//if (false)
		if (changed) {
			screen.Damage ();
			changed = false;
		}

		inner (s);
	}

	void UpdateWindow (Window win)
	{
		Particle p = particles[win];

		int x, y;
		win.GetPosition (out x, out y);
		int width, height;
		win.GetSize (out width, out height);
		x += width/2;
		y += height/2;
		p.Position = new PhysModel.Point (x, y);
	}

	public override bool InitWindow (IntPtr plugin, IntPtr window)
	{
		Console.WriteLine ("initWindow");

		Window w = new Window (window);
		windowPtrs[window] = w;
		windows.Add (w);

		int x, y;
		w.GetPosition (out x, out y);
		int width, height;
		w.GetSize (out width, out height);
		x += width/2;
		y += height/2;
		particles[w] = NewParticle (x, y);


		//Console.WriteLine (window.Id);
		//Console.WriteLine (window.Frame);

		//Console.WriteLine (win.UserTime);
		//Console.WriteLine (win.State);
		//win.Maximize ();

		return true;
	}

	public override void FiniWindow (IntPtr plugin, IntPtr window)
	{
		Console.WriteLine ("finiWindow");

		model.Particles.Remove (particles[windowPtrs[window]]);
		particles.Remove (windowPtrs[window]);
		windows.Remove (windowPtrs[window]);
		windowPtrs.Remove (window);
	}
}