1 //------------------------------------------------------------------------------
2 // emViewAnimator.h
3 //
4 // Copyright (C) 2014-2017 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20 
21 #ifndef emViewAnimator_h
22 #define emViewAnimator_h
23 
24 #ifndef emView_h
25 #include <emCore/emView.h>
26 #endif
27 
28 
29 //==============================================================================
30 //=============================== emViewAnimator ===============================
31 //==============================================================================
32 
33 class emViewAnimator : public emEngine {
34 
35 public:
36 
37 	emViewAnimator(emView & view);
38 	virtual ~emViewAnimator();
39 
40 	emView & GetView() const;
41 
42 	void SetMaster(emViewAnimator * master);
43 	emViewAnimator * GetMaster() const;
44 	emViewAnimator * GetActiveSlave() const;
45 
46 	bool IsActive() const;
47 	virtual void Activate();
48 	virtual void Deactivate();
49 
50 	bool IsDeactivatingWhenIdle() const;
51 	void SetDeactivateWhenIdle(bool deactivateWhenIdle=true);
52 
53 	virtual void Input(emInputEvent & event, const emInputState & state);
54 	virtual void Paint(const emPainter & painter) const;
55 
56 protected:
57 
58 	virtual bool Cycle();
59 
60 	virtual bool CycleAnimation(double dt) = 0;
61 
62 	void InvalidatePainting();
63 
64 private:
65 
66 	emView & View;
67 	emViewAnimator * Master;
68 	emViewAnimator * ActiveSlave;
69 	emViewAnimator * * UpperActivePtr;
70 	emUInt64 LastTSC;
71 	emUInt64 LastClk;
72 	bool DeactivateWhenIdle;
73 };
74 
GetView()75 inline emView & emViewAnimator::GetView() const
76 {
77 	return View;
78 }
79 
GetMaster()80 inline emViewAnimator * emViewAnimator::GetMaster() const
81 {
82 	return Master;
83 }
84 
GetActiveSlave()85 inline emViewAnimator * emViewAnimator::GetActiveSlave() const
86 {
87 	return ActiveSlave;
88 }
89 
IsActive()90 inline bool emViewAnimator::IsActive() const
91 {
92 	return *UpperActivePtr==this;
93 }
94 
IsDeactivatingWhenIdle()95 inline bool emViewAnimator::IsDeactivatingWhenIdle() const
96 {
97 	return DeactivateWhenIdle;
98 }
99 
InvalidatePainting()100 inline void emViewAnimator::InvalidatePainting()
101 {
102 	View.InvalidatePainting();
103 }
104 
105 
106 //==============================================================================
107 //=========================== emKineticViewAnimator ============================
108 //==============================================================================
109 
110 class emKineticViewAnimator : public emViewAnimator {
111 
112 public:
113 
114 	emKineticViewAnimator(emView & view);
115 	virtual ~emKineticViewAnimator();
116 
117 	virtual void Activate();
118 	virtual void Deactivate();
119 
120 	double GetVelocity(int dimension) const;
121 	double GetAbsVelocity() const;
122 	void SetVelocity(int dimension, double velocity);
123 
124 	void CenterZoomFixPoint();
125 	void SetZoomFixPoint(double zoomFixX, double zoomFixY);
126 
127 	void SetFrictionEnabled(bool enabled);
128 	bool IsFrictionEnabled() const;
129 	double GetFriction() const;
130 	void SetFriction(double friction);
131 
132 protected:
133 
134 	virtual bool CycleAnimation(double dt);
135 
136 private:
137 
138 	void UpdateBusyState();
139 	void UpdateZoomFixPoint();
140 
141 	double Velocity[3];
142 	bool ZoomFixPointCentered;
143 	double ZoomFixX,ZoomFixY;
144 	bool FrictionEnabled;
145 	double Friction;
146 	bool Busy;
147 };
148 
GetVelocity(int dimension)149 inline double emKineticViewAnimator::GetVelocity(int dimension) const
150 {
151 	return Velocity[dimension];
152 }
153 
IsFrictionEnabled()154 inline bool emKineticViewAnimator::IsFrictionEnabled() const
155 {
156 	return FrictionEnabled;
157 }
158 
GetFriction()159 inline double emKineticViewAnimator::GetFriction() const
160 {
161 	return Friction;
162 }
163 
164 
165 
166 
167 //==============================================================================
168 //=========================== emSpeedingViewAnimator ===========================
169 //==============================================================================
170 
171 class emSpeedingViewAnimator : public emKineticViewAnimator {
172 
173 public:
174 
175 	emSpeedingViewAnimator(emView & view);
176 
177 	virtual ~emSpeedingViewAnimator();
178 
179 	virtual void Activate();
180 	virtual void Deactivate();
181 
182 	double GetTargetVelocity(int dimension) const;
183 	double GetAbsTargetVelocity() const;
184 	void SetTargetVelocity(int dimension, double targetVelocity);
185 
186 	double GetAcceleration() const;
187 	void SetAcceleration(double acceleration);
188 
189 	double GetReverseAcceleration() const;
190 	void SetReverseAcceleration(double reverseAcceleration);
191 
192 protected:
193 
194 	virtual bool CycleAnimation(double dt);
195 
196 private:
197 
198 	void UpdateBusyState();
199 
200 	double TargetVelocity[3];
201 	double Acceleration;
202 	double ReverseAcceleration;
203 	bool Busy;
204 };
205 
GetTargetVelocity(int dimension)206 inline double emSpeedingViewAnimator::GetTargetVelocity(int dimension) const
207 {
208 	return TargetVelocity[dimension];
209 }
210 
GetAcceleration()211 inline double emSpeedingViewAnimator::GetAcceleration() const
212 {
213 	return Acceleration;
214 }
215 
GetReverseAcceleration()216 inline double emSpeedingViewAnimator::GetReverseAcceleration() const
217 {
218 	return ReverseAcceleration;
219 }
220 
221 
222 //==============================================================================
223 //=========================== emSwipingViewAnimator ============================
224 //==============================================================================
225 
226 class emSwipingViewAnimator : public emKineticViewAnimator {
227 
228 public:
229 
230 	emSwipingViewAnimator(emView & view);
231 	virtual ~emSwipingViewAnimator();
232 
233 	virtual void Activate();
234 	virtual void Deactivate();
235 
236 	bool IsGripped() const;
237 	void SetGripped(bool gripped);
238 
239 	void MoveGrip(int dimension, double distance);
240 
241 	double GetSpringConstant() const;
242 	void SetSpringConstant(double springConstant);
243 
244 	double GetAbsSpringExtension() const;
245 
246 protected:
247 
248 	virtual bool CycleAnimation(double dt);
249 
250 private:
251 
252 	void UpdateBusyState();
253 
254 	bool Gripped;
255 	double SpringExtension[3];
256 	double InstantaneousVelocity[3];
257 	double SpringConstant;
258 	bool Busy;
259 };
260 
IsGripped()261 inline bool emSwipingViewAnimator::IsGripped() const
262 {
263 	return Gripped;
264 }
265 
GetSpringConstant()266 inline double emSwipingViewAnimator::GetSpringConstant() const
267 {
268 	return SpringConstant;
269 }
270 
271 
272 //==============================================================================
273 //=========================== emMagneticViewAnimator ===========================
274 //==============================================================================
275 
276 class emMagneticViewAnimator : public emKineticViewAnimator {
277 
278 public:
279 
280 	emMagneticViewAnimator(emView & view);
281 	virtual ~emMagneticViewAnimator();
282 
283 	virtual void Activate();
284 	virtual void Deactivate();
285 
286 protected:
287 
288 	virtual bool CycleAnimation(double dt);
289 
290 private:
291 
292 	double CalculateDistance(double * pDX, double * pDY, double * pDZ) const;
293 
294 	void GetViewRect(double * pX, double * pY,
295 	                 double * pW, double * pH) const;
296 
297 	emRef<emCoreConfig> CoreConfig;
298 	bool MagnetismActive;
299 };
300 
301 
302 //==============================================================================
303 //=========================== emVisitingViewAnimator ===========================
304 //==============================================================================
305 
306 class emVisitingViewAnimator : public emViewAnimator {
307 
308 public:
309 
310 	emVisitingViewAnimator(emView & view);
311 	virtual ~emVisitingViewAnimator();
312 
313 	bool IsAnimated() const;
314 	void SetAnimated(bool animated=true);
315 
316 	double GetAcceleration() const;
317 	void SetAcceleration(double acceleration);
318 
319 	double GetMaxCuspSpeed() const;
320 	void SetMaxCuspSpeed(double maxCuspSpeed);
321 
322 	double GetMaxAbsoluteSpeed() const;
323 	void SetMaxAbsoluteSpeed(double maxAbsoluteSpeed);
324 
325 	void SetAnimParamsByCoreConfig(const emCoreConfig & coreConfig);
326 
327 	void SetGoal(const char * identity, bool adherent,
328 	             const char * subject=NULL);
329 	void SetGoal(const char * identity, double relX, double relY, double relA,
330 	             bool adherent, const char * subject=NULL);
331 	void SetGoalFullsized(const char * identity, bool adherent,
332 	                      bool utilizeView=false, const char * subject=NULL);
333 	void ClearGoal();
334 
335 	bool HasGoal() const;
336 	bool HasReachedGoal() const;
337 	bool HasGivenUp() const;
338 
339 	virtual void Activate();
340 	virtual void Deactivate();
341 	virtual void Input(emInputEvent & event, const emInputState & state);
342 	virtual void Paint(const emPainter & painter) const;
343 
344 protected:
345 
346 	virtual bool CycleAnimation(double dt);
347 
348 private:
349 
350 	enum StateEnum {
351 		ST_NO_GOAL,
352 		ST_CURVE,
353 		ST_DIRECT,
354 		ST_SEEK,
355 		ST_GIVING_UP,
356 		ST_GIVEN_UP,
357 		ST_GOAL_REACHED
358 	};
359 
360 	enum VisitTypeEnum {
361 		VT_VISIT,
362 		VT_VISIT_REL,
363 		VT_VISIT_FULLSIZED
364 	};
365 
366 	struct CurvePoint {
367 		double X;
368 		double Z;
369 	};
370 
371 	void SetGoal(
372 		VisitTypeEnum visitType, const char * identity, double relX, double relY,
373 		double relA, bool adherent, bool utilizeView, const char * subject
374 	);
375 
376 	void UpdateSpeed(
377 		double pos, double dist, int panelsAfter, double distFinal, double dt
378 	);
379 
380 	emPanel * GetNearestExistingPanel(
381 		double * pRelX, double * pRelY, double * pRelA, bool * pAdherent,
382 		int * pDepth, int * pPanelsAfter, double * pDistFinal
383 	) const;
384 
385 	emPanel * GetNearestViewedPanel(emPanel * nearestExistingPanel) const;
386 
387 	void GetDistanceTo(
388 		emPanel * panel, double relX, double relY, double relA,
389 		double * pDirX, double * pDirY, double * pDistXY, double * pDistZ
390 	) const;
391 
392 	void GetViewRect(
393 		double * pX, double * pY, double * pW, double * pH
394 	) const;
395 
396 	static double GetDirectDist(double x, double z);
397 
398 	static void GetDirectPoint(
399 		double x, double z, double d,
400 		double * pX, double * pZ
401 	);
402 
403 	static void GetCurvePosDist(
404 		double x, double z, double * pCurvePos, double * pCurveDist
405 	);
406 
407 	static CurvePoint GetCurvePoint(double d);
408 
409 	bool Animated;
410 	double Acceleration;
411 	double MaxCuspSpeed;
412 	double MaxAbsoluteSpeed;
413 	StateEnum State;
414 	VisitTypeEnum VisitType;
415 	emString Identity;
416 	double RelX,RelY,RelA;
417 	bool Adherent;
418 	bool UtilizeView;
419 	emString Subject;
420 	emArray<emString> Names;
421 	int MaxDepthSeen;
422 	double Speed;
423 	int TimeSlicesWithoutHope;
424 	emUInt64 GiveUpClock;
425 
426 	static const double CurveDeltaDist;
427 	static const CurvePoint CurvePoints[];
428 	static const int CurveMaxIndex;
429 };
430 
IsAnimated()431 inline bool emVisitingViewAnimator::IsAnimated() const
432 {
433 	return Animated;
434 }
435 
GetAcceleration()436 inline double emVisitingViewAnimator::GetAcceleration() const
437 {
438 	return Acceleration;
439 }
440 
GetMaxCuspSpeed()441 inline double emVisitingViewAnimator::GetMaxCuspSpeed() const
442 {
443 	return MaxCuspSpeed;
444 }
445 
GetMaxAbsoluteSpeed()446 inline double emVisitingViewAnimator::GetMaxAbsoluteSpeed() const
447 {
448 	return MaxAbsoluteSpeed;
449 }
450 
HasGoal()451 inline bool emVisitingViewAnimator::HasGoal() const
452 {
453 	return State!=ST_NO_GOAL;
454 }
455 
HasReachedGoal()456 inline bool emVisitingViewAnimator::HasReachedGoal() const
457 {
458 	return State==ST_GOAL_REACHED;
459 }
460 
HasGivenUp()461 inline bool emVisitingViewAnimator::HasGivenUp() const
462 {
463 	return State==ST_GIVEN_UP;
464 }
465 
466 
467 #endif
468