2 #include "../Common/Common.h"
4 #include "UserLocation.h"
8 // Track memory leaks on Windows to the line that new'd the memory
11 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
14 static char THIS_FILE
[] = __FILE__
;
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);
26 m_strTime
= CTimeSpan::GetTimeStamp();
33 // Stores only the normalized floating point data.
34 CUserLocation::CUserLocation(float iX
, float iY
, float dNats
)
36 //CFunctionLogger f1("CUserLocation::CUserLocation(2)", gLogger);
40 m_strTime
= CTimeSpan::GetTimeStamp();
41 m_dNormalizedLocationX
= iX
;
42 m_dNormalizedLocationY
= iY
;
43 m_bHasNormalized
= true;
47 // Stores only the normalized floating point data.
48 // This version calculates the normalization itself.
49 CUserLocation::CUserLocation(int iX
,
55 bool bStoreIntegerRep
,
58 //CFunctionLogger f1("CUserLocation::CUserLocation(3)", gLogger);
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;
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);
85 m_strTime
= CTimeSpan::GetTimeStamp();
88 m_dNormalizedLocationX
= iX2
;
89 m_dNormalizedLocationY
= iY2
;
90 m_bHasNormalized
= true;
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";
117 strResult
+= strPrefix
;
118 strResult
+= "\t<X>";
119 sprintf(strNum
, "%d", m_iLocationX
);
121 strResult
+= "</X>\n";
123 strResult
+= strPrefix
;
124 strResult
+= "\t<Y>";
125 sprintf(strNum
, "%d", m_iLocationY
);
127 strResult
+= "</Y>\n";
129 if (m_bHasNormalized
)
131 strResult
+= strPrefix
;
132 strResult
+= "\t<XNorm>";
133 sprintf(strNum
, "%0.4f", m_dNormalizedLocationX
);
135 strResult
+= "</XNorm>\n";
137 strResult
+= strPrefix
;
138 strResult
+= "\t<YNorm>";
139 sprintf(strNum
, "%0.4f", m_dNormalizedLocationY
);
141 strResult
+= "</YNorm>\n";
144 strResult
+= strPrefix
;
145 strResult
+= "\t<Bits>";
146 sprintf(strNum
, "%0.3f", m_dNats
/ log(2.0));
148 strResult
+= "</Bits>\n";
150 strResult
+= strPrefix
;
151 strResult
+= "</Pos>\n";
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);
179 m_dNormalizedLocationX
= 0.0;
180 m_dNormalizedLocationY
= 0.0;
181 m_bHasNormalized
= false;
182 m_bHasInteger
= false;
186 // Construct based on some XML like:
188 // <Time>15:49:10.203</Time>
191 // <XNorm>0.7274</XNorm>
192 // <YNorm>0.1853</YNorm>
193 // <Bits>0.555</Bits>
195 CUserLocation::CUserLocation(const string
& strXML
)
197 //CFunctionLogger f1("CUserLocation::CUserLocation(XML)", gLogger);
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
))
221 m_bHasInteger
= false;
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;
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
= "";
246 if (bReturnNormalized
)
247 sprintf(szNum
, "%0.4f\t%0.4f\n", m_dNormalizedLocationX
, m_dNormalizedLocationY
);
249 sprintf(szNum
, "%0.4f\t%0.4f\n", (double)m_iLocationX
, (double)m_iLocationY
);
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
))
264 *pRow
= (int) (m_dNormalizedLocationX
* (double) iGridSize
);
265 *pCol
= (int) (m_dNormalizedLocationY
* (double) iGridSize
);