Never start inside a really probable child box when cursor is moved Got
[dasher.git] / Src / DasherCore / DasherViewSquare.h
blobd526c7865391b398eadc5f4cb67f21a19fc0b698
1 // DasherViewSquare.h
2 //
3 // Copyright (c) 2001-2004 David Ward
5 #ifndef __DasherViewSquare_h__
6 #define __DasherViewSquare_h__
7 #include "DasherView.h"
8 #include <deque>
10 using namespace std;
12 namespace Dasher {
13 class CDasherViewSquare;
14 class CDasherView;
15 class CDasherModel;
16 class CDelayedDraw;
17 class CDasherNode;
20 class Dasher::CDasherViewSquare;
21 class Dasher::CDasherModel;
22 class Dasher::CDelayedDraw;
23 class Dasher::CDasherNode;
25 /// An implementation of the DasherView class
26 ///
27 /// This class renders Dasher in the vanilla style,
28 /// but with horizontal and vertical mappings
29 ///
30 /// Horizontal mapping - linear and log
31 /// Vertical mapping - linear with different gradient
33 class Dasher::CDasherViewSquare:public Dasher::CDasherView
35 public:
37 /// \param pEventHandler Event handler.
38 /// \param pSettingsStore Settings store.
39 /// \param DasherScreen Pointer to creen to which the view will render.
40 /// \param DasherModel The model which will be rendered.
41 /// \todo Don't cache screen and model locally - screen can be
42 /// passed as parameter to the drawing functions, and data structure
43 /// can be extracted from the model and passed too.
45 CDasherViewSquare(CEventHandler *pEventHandler, CSettingsStore *pSettingsStore, CDasherScreen *DasherScreen, CDasherModel *DasherModel);
46 ~CDasherViewSquare();
48 ///
49 /// Convert input coordinates to dasher coordinates and evolve the model
50 ///
52 void TapOnDisplay(screenint mousex, screenint mousey, unsigned long Time);
53 void TapOnDisplay(screenint mousex,screenint mousey, unsigned long Time, VECTOR_SYMBOL_PROB* pAdded = NULL, int* pNumDeleted = NULL);
55 ///
56 /// \todo Document this
57 ///
59 void GoTo(screenint mousex, screenint mousey);
61 ///
62 /// \todo Document this
63 ///
65 void ClickTo(int x, int y, int width, int height);
67 ///
68 /// \todo Document this
69 ///
71 void DrawGoTo(screenint mousex, screenint mousey);
73 ///
74 /// Render the current state of the model.
75 ///
77 virtual void RenderNodes();
79 ///
80 /// Supply a new screen to draw to
81 ///
83 void ChangeScreen(CDasherScreen * NewScreen);
85 ///
86 /// Draw the mouse pointer
87 ///
89 void DrawMouse(screenint mousex, screenint mousey);
91 ///
92 /// Draw the mouse line
93 ///
95 void DrawMouseLine(screenint mousex, screenint mousey);
97 ///
98 /// \todo Document this
99 ///
101 void DrawKeyboard();
104 /// Draw the game mode pointer
107 void DrawGameModePointer();
109 /// Get the scale factor for conversion between Dasher co-ordinates
110 /// and screen co-ordinates
112 void GetScaleFactor( int eOrientation, myint *iScaleFactorX, myint *iScaleFactorY );
114 /// Checks for start on mouse behaviour and updates which boxes are
115 /// displayed, triggers starts etc.
117 virtual bool HandleStartOnMouse(int iTime);
120 /// Event handler
123 virtual void HandleEvent(Dasher::CEvent * pEvent);
125 /// Render a single node
126 /// \param Character Symbol ID to be drawn
127 /// \param Color The colour to draw it
128 /// \param ColorScheme Which of the alternating colour schemes to use
129 /// \param y1 Upper extent.
130 /// \param y2 Lower extent
131 /// \param mostleft The left most position in which the text (l->r)
132 /// can be displayed in order to avoid overlap. This is updated by
133 /// the function to allow for the new text
134 /// \param displaytext Text to display.
135 /// \todo Character and displaytext are redundant. We shouldn't need
136 /// to know about alphabets here, so only use the latterr
138 int RenderNode(const symbol Character, const int Color, Opts::ColorSchemes ColorScheme, myint y1, myint y2, int &mostleft, const std::string & displaytext, bool bShove);
141 /// Recursively render all nodes in a tree. Responsible for all the Render_node calls
144 int RecursiveRender(CDasherNode * Render, myint y1, myint y2, int mostleft, std::vector<CDasherNode *> &vNodeList);
147 /// Displays some nodes inside one parent node. Used to group capital letters, accents, punctuation etc.
150 void RenderGroups(CDasherNode * Render, myint y1, myint y2, int mostleft);
152 ///
153 /// Get the automatic calibration offset
156 int GetAutoOffset() const;
157 bool CheckForNewRoot();
159 /// \todo Document this
162 virtual void ResetSum();
165 /// \todo Document this
168 virtual void ResetSumCounter();
171 /// Reset the automatic calibration offset
174 virtual void ResetYAutoOffset();
176 private:
179 /// Convert input device position to Dasher co-ordinates
182 void Input2Dasher(screenint iInputX, screenint iInputY, myint & iDasherX, myint & iDasherY, int iType, int iMode);
184 ///
185 /// Convert a screen co-ordinate to Dasher co-ordinates
188 void Screen2Dasher(screenint iInputX, screenint iInputY, myint & iDasherX, myint & iDasherY,bool b1D, bool bNonlinearity);
191 /// Convert Dasher co-ordinates to screen co-ordinates
194 void Dasher2Screen(myint iDasherX, myint iDasherY, screenint & iScreenX, screenint & iScreenY);
197 /// Applies the 1D mode transformation
200 void Dasher2OneD(myint & iDasherX, myint & iDasherY);
203 /// Applies the eyetracker mode transformation
206 void Dasher2Eyetracker(myint & iDasherX, myint & iDasherY);
209 /// Trunates co-ordinates to fit on screen
212 void TruncateToScreen(screenint & iX, screenint & iY);
215 /// Draw a polyline specified in Dasher co-ordinates
218 void DasherPolyline(myint * x, myint * y, int n, int iWidth, int iColour);
221 /// Draw a polygon specified in Dasher co-ordinates
224 void DasherPolygon(myint * x, myint * y, int n, int iColour);
227 /// Draw a rectangle specified in Dasher co-ordinates
230 void DasherDrawRectangle(myint iLeft, myint iTop, myint iRight, myint iBottom, const int Color, int iOutlineColour, Opts::ColorSchemes ColorScheme,bool bDrawOutline, bool bFill, int iThickness);
233 /// Draw a centred rectangle specified in Dasher co-ordinates (used for mouse cursor)
236 void DasherDrawCentredRectangle(myint iDasherX, myint iDasherY, screenint iSize, const int Color, Opts::ColorSchemes ColorScheme, bool bDrawOutline);
239 /// Draw text specified in Dasher co-ordinates
242 void DasherDrawText(myint iAnchorX1, myint iAnchorY1, myint iAnchorX2, myint iAnchorY2, const std::string & sDisplayText, int &mostleft, bool bShove);
245 /// Get minimum visible Dasher Y co-ordinate
246 /// \deprecated Use VisibleRegion.
249 myint DasherVisibleMinY();
252 /// Get maximum visible Dasher Y co-ordinate
253 /// \deprecated Use VisibleRegion.
256 myint DasherVisibleMaxY();
259 /// Get maximum visible Dasher X co-ordinate
260 /// \deprecated Use VisibleRegion.
263 myint DasherVisibleMaxX();
266 /// Get the bounding box of the visible region.
269 void VisibleRegion( myint &iDasherMinX, myint &iDasherMinY, myint &iDasherMaxX, myint &iDasherMaxY );
272 /// Unused
275 myint m_iDasherXCache;
278 /// Unused
281 myint m_iDasherYCache;
283 //Auto-speed control functions
286 /// AUTO-SPEED-CONTROL
287 /// This is the main speed control function and drives all of auto speed control.
288 /// \param non-linear Dasher x coord
289 /// \param non-linear Dasher y coord
292 void SpeedControl(myint iDasherX, myint iDasherY);
295 /// AUTO-SPEED-CONTROL
296 /// Calculates the running variance of the angle between the +ve x-axis and the line joining
297 /// the cross hair to the mouse position.
300 double Variance();
303 /// AUTO-SPEED-CONTROL
304 /// Updates the exclusion radius for auto speed control.
307 double UpdateMinRadius();
310 /// AUTO-SPEED-CONTROL
311 /// Applies changes to the max bit rate depending on the running variance
312 /// of the angle between the +ve x-axis and the line joining
313 /// the cross hair to the mouse position.
316 double UpdateBitrate();
319 /// AUTO-SPEED-CONTROL
320 /// Adapts the number of samples taken so that auto speed control
321 /// is invariant to clock rate and user ability (!!!).
324 int UpdateSampleSize();
327 /// AUTO-SPEED-CONTROL
328 /// Updates the *variances* of the two populations of mixture-of-2-Gaussians
329 /// distribution of radii. These are used to calculate the exclusion radius.
330 /// \param radius
333 void UpdateSigmas(double r);
336 #ifdef _WIN32
338 /// FIXME - couldn't find windows version of round(double) so here's one!
339 /// \param number to be rounded
341 double round(double d)
343 if(d - floor(d) < 0.5)
344 return floor(d);
345 else
346 return ceil(d);
349 #endif
350 // myint s_Y1,s_Y2,s_Y3;
352 // Variables for speed control
355 // AUTO-SPEED-CONTROL
356 double m_dBitrate; // stores max bit rate internally
357 double m_dSampleScale, m_dSampleOffset; // internal, control sample size
358 int m_nSpeedCounter; // keep track of how many samples
359 int m_nSpeedSamples; // upper limit on #samples
360 double m_dSpeedMax, m_dSpeedMin; // bit rate always within this range
361 double m_dTier1, m_dTier2, m_dTier3, m_dTier4; // variance tolerance tiers
362 double m_dChange1, m_dChange2, m_dChange3, m_dChange4; // fractional changes to bit rate
363 double m_dMinRRate; // controls rate at which min. r adapts HIGHER===SLOWER!
364 double m_dSensitivity; // not used, control sensitivity of auto speed control
365 typedef std::deque<double> DOUBLE_DEQUE;
366 DOUBLE_DEQUE m_dequeAngles; // store angles for statistics
368 //variables for adaptive radius calculations...
369 double m_dSigma1, m_dSigma2, m_dMinRadius;
371 // Class definitions
373 class Cymap {
374 public:
375 Cymap(myint iScale);
376 Cymap() {}
377 myint map(myint y) const;
378 myint unmap(myint y) const;
379 private:
380 myint m_Y1, m_Y2, m_Y3;
383 // the x and y non-linearities
386 /// \deprecated See Screen2Dasher
389 void screen2dasher(screenint mousex, screenint mousey, myint * dasherx, myint * dashery) const;
390 void AutoCalibrate(screenint * mousex, screenint * mousey);
393 /// \deprecated See Dasher2Screen
396 screenint dasherx2screen(myint sx) const;
399 /// \deprecated See Dasher2Screen
402 screenint dashery2screen(myint sy) const;
405 /// \deprecated See Dasher2Screen
408 Cint32 dashery2screen(myint y1, myint y2, screenint & s1, screenint & s2) const;
410 double eyetracker_get_x(double x, double y);
411 double eyetracker_get_y(double x, double y);
412 double xmax(double x, double y) const;
413 double xmap(double x) const;
414 double ixmap(double x) const;
417 inline void Crosshair(myint sx);
419 // Data
421 bool bInBox; // Whether we're in the mouseposstart box
422 int iBoxStart; // Time that the current box was drawn
423 int iBoxEntered; // Time when the user enttered the current box
425 CDelayedDraw *m_pDelayDraw;
427 double m_dXmpa, m_dXmpb, m_dXmpc, m_dXmpd;
428 screenint CanvasX, CanvasY, CanvasBorder;
430 int m_ySum, m_ySumCounter, m_yFilterTimescale, m_ySigBiasPixels, m_ySigBiasPercentage, m_yAutoOffset;
432 Cymap m_ymap;
434 // Called on screen size changes
435 void SetScaleFactor();
437 // Cached values for scaling
438 myint iLRScaleFactorX;
439 myint iLRScaleFactorY;
440 myint iTBScaleFactorX;
441 myint iTBScaleFactorY;
443 // The factor that scale factors are multipled by
444 myint m_iScalingFactor;
447 #include "DasherViewSquare.inl"
449 #endif /* #ifndef __DasherViewSquare_h__ */