tagging release
[dasher.git] / trunk / Src / DasherCore / UserLocation.cpp
blobb0e485565b179a2217340e608940978c7c24432c
2 #include "../Common/Common.h"
4 #include "UserLocation.h"
6 #include <sys/timeb.h>
8 // Track memory leaks on Windows to the line that new'd the memory
9 #ifdef _WIN32
10 #ifdef _DEBUG
11 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
12 #define new DEBUG_NEW
13 #undef THIS_FILE
14 static char THIS_FILE[] = __FILE__;
15 #endif
16 #endif
18 // Construct a new location at the current point in time and at the specified coordinates.
19 // This version only stores the integer coordinate data.
20 CUserLocation::CUserLocation(int iX, int iY, float dNats)
22 //CFunctionLogger f1("CUserLocation::CUserLocation(1)", gLogger);
24 InitMemeberVars();
26 m_strTime = CTimeSpan::GetTimeStamp();
27 m_iLocationX = iX;
28 m_iLocationY = iY;
29 m_bHasInteger = true;
30 m_dNats = dNats;
33 // Stores only the normalized floating point data.
34 CUserLocation::CUserLocation(float iX, float iY, float dNats)
36 //CFunctionLogger f1("CUserLocation::CUserLocation(2)", gLogger);
38 InitMemeberVars();
40 m_strTime = CTimeSpan::GetTimeStamp();
41 m_dNormalizedLocationX = iX;
42 m_dNormalizedLocationY = iY;
43 m_bHasNormalized = true;
44 m_dNats = dNats;
47 // Stores only the normalized floating point data.
48 // This version calculates the normalization itself.
49 CUserLocation::CUserLocation(int iX,
50 int iY,
51 int iTop,
52 int iLeft,
53 int iBottom,
54 int iRight,
55 bool bStoreIntegerRep,
56 float dNats)
58 //CFunctionLogger f1("CUserLocation::CUserLocation(3)", gLogger);
60 InitMemeberVars();
62 m_strTime = CTimeSpan::GetTimeStamp();
63 m_dNormalizedLocationX = (float) ComputeNormalizedX(iX, iLeft, iRight);
64 m_dNormalizedLocationY = (float) ComputeNormalizedY(iY, iTop, iBottom);
65 m_bHasNormalized = true;
66 m_dNats = dNats;
68 if (bStoreIntegerRep)
70 m_bHasInteger = true;
71 m_iLocationX = iX;
72 m_iLocationY = iY;
74 else
75 m_bHasInteger = false;
78 // We want both the integer representation and the normalized.
79 CUserLocation::CUserLocation(int iX1, int iY1, float iX2, float iY2, float dNats)
81 //CFunctionLogger f1("CUserLocation::CUserLocation(4)", gLogger);
83 InitMemeberVars();
85 m_strTime = CTimeSpan::GetTimeStamp();
86 m_iLocationX = iX1;
87 m_iLocationY = iY1;
88 m_dNormalizedLocationX = iX2;
89 m_dNormalizedLocationY = iY2;
90 m_bHasNormalized = true;
91 m_bHasInteger = true;
92 m_dNats = dNats;
95 CUserLocation::~CUserLocation()
97 //CFunctionLogger f1("CUserLocation::~CUserLocation", gLogger);
100 string CUserLocation::GetXML(const string& strPrefix)
102 //CFunctionLogger f1("CUserLocation::GetXML", gLogger);
104 string strResult = "";
106 strResult += strPrefix;
107 strResult += "<Pos>\n";
109 strResult += strPrefix;
110 strResult += "\t<Time>";
111 strResult += m_strTime;
112 strResult += "</Time>\n";
114 char strNum[256];
115 if (m_bHasInteger)
117 strResult += strPrefix;
118 strResult += "\t<X>";
119 sprintf(strNum, "%d", m_iLocationX);
120 strResult += strNum;
121 strResult += "</X>\n";
123 strResult += strPrefix;
124 strResult += "\t<Y>";
125 sprintf(strNum, "%d", m_iLocationY);
126 strResult += strNum;
127 strResult += "</Y>\n";
129 if (m_bHasNormalized)
131 strResult += strPrefix;
132 strResult += "\t<XNorm>";
133 sprintf(strNum, "%0.4f", m_dNormalizedLocationX);
134 strResult += strNum;
135 strResult += "</XNorm>\n";
137 strResult += strPrefix;
138 strResult += "\t<YNorm>";
139 sprintf(strNum, "%0.4f", m_dNormalizedLocationY);
140 strResult += strNum;
141 strResult += "</YNorm>\n";
144 strResult += strPrefix;
145 strResult += "\t<Bits>";
146 sprintf(strNum, "%0.3f", m_dNats / log(2.0));
147 strResult += strNum;
148 strResult += "</Bits>\n";
150 strResult += strPrefix;
151 strResult += "</Pos>\n";
153 return strResult;
156 // Static helper method for computing normalized X coordinate
157 double CUserLocation::ComputeNormalizedX(int iX, int iLeft, int iRight)
159 //CFunctionLogger f1("CUserLocation::ComputeNormalizedX", gLogger);
161 return (double) (iX - iLeft) / (double) abs(iRight - iLeft);
164 // Static helper method for computing normalized Y coordinate
165 double CUserLocation::ComputeNormalizedY(int iY, int iTop, int iBottom)
167 //CFunctionLogger f1("CUserLocation::ComputeNormalizedY", gLogger);
169 return (double) (iY - iTop) / (double) abs(iBottom - iTop);
172 void CUserLocation::InitMemeberVars()
174 //CFunctionLogger f1("CUserLocation::InitMemeberVars", gLogger);
176 m_strTime = "";
177 m_iLocationX = 0;
178 m_iLocationY = 0;
179 m_dNormalizedLocationX = 0.0;
180 m_dNormalizedLocationY = 0.0;
181 m_bHasNormalized = false;
182 m_bHasInteger = false;
183 m_dNats = 0.0;
186 // Construct based on some XML like:
187 // <Pos>
188 // <Time>15:49:10.203</Time>
189 // <X>807</X>
190 // <Y>382</Y>
191 // <XNorm>0.7274</XNorm>
192 // <YNorm>0.1853</YNorm>
193 // <Bits>0.555</Bits>
194 // </Pos>
195 CUserLocation::CUserLocation(const string& strXML)
197 //CFunctionLogger f1("CUserLocation::CUserLocation(XML)", gLogger);
199 InitMemeberVars();
201 bool bFoundNormX = false;
202 bool bFoundNormY = false;
203 bool bFoundX = false;
204 bool bFoundY = false;
206 m_strTime = XMLUtil::GetElementString("Time", strXML, true);
207 m_iLocationX = XMLUtil::GetElementInt("X", strXML, &bFoundX);
208 m_iLocationY = XMLUtil::GetElementInt("Y", strXML, &bFoundY);
209 m_dNormalizedLocationX = (float) XMLUtil::GetElementFloat("XNorm", strXML, &bFoundNormX);
210 m_dNormalizedLocationY = (float) XMLUtil::GetElementFloat("YNorm", strXML, &bFoundNormY);
212 // Convert the bits back to dNats
213 m_dNats = (float) ((double) XMLUtil::GetElementFloat("Bits", strXML) * (double) log(2.0));
215 // If there weren't X, Y elements, we want them set to 0 and mark
216 // ourselves as not having them.
217 if ((!bFoundX) && (!bFoundY))
219 m_iLocationX = 0;
220 m_iLocationY = 0;
221 m_bHasInteger = false;
223 else
224 m_bHasInteger = true;
226 // Require that we find both XNorm and YNorm in order to count
227 if ((!bFoundNormX) || (!bFoundNormY))
229 m_dNormalizedLocationX = 0.0;
230 m_dNormalizedLocationY = 0.0;
231 m_bHasNormalized = false;
233 else
234 m_bHasNormalized = true;
238 // Returns a tab delimited version of this location's X & Y coordinate
239 string CUserLocation::GetTabMouseXY(bool bReturnNormalized)
241 //CFunctionLogger f1("CUserLocation::GetTabMouseXY", gLogger);
243 string strResult = "";
244 char szNum[256];
246 if (bReturnNormalized)
247 sprintf(szNum, "%0.4f\t%0.4f\n", m_dNormalizedLocationX, m_dNormalizedLocationY);
248 else
249 sprintf(szNum, "%0.4f\t%0.4f\n", (double)m_iLocationX, (double)m_iLocationY);
251 strResult += szNum;
253 return strResult;
256 // Figure out what grid location this normalized mouse coordinate should go.
257 void CUserLocation::GetMouseGridLocation(int iGridSize, int* pRow, int* pCol)
259 //CFunctionLogger f1("CUserLocation::GetMouseGridLocation", gLogger);
261 if ((pRow == NULL) || (pCol == NULL))
262 return;
264 *pRow = (int) (m_dNormalizedLocationX * (double) iGridSize);
265 *pCol = (int) (m_dNormalizedLocationY * (double) iGridSize);