2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
28 /*****************************************************************************
33 * A 3D vector MFC control derived from CButton. *
35 * - Real-time light rendering on a 3D ball. *
36 * - Variable ball radius and position. *
37 * - Supports bitmap background (tiled). *
38 * - Supports vertical gradient color background (from color to color). *
39 * - Variable ball color (diffuse), light color and ambient color. *
40 * - Variable specular intensity. *
41 * - Supports attached controls (for automatic update). *
42 * - Variable mouse sensitivity. *
43 * - Supports front clipping (vector will not have negative Z values). *
44 * - Supports callback functions for the following events: *
45 * 1. The trackball has moved (vector is changing). *
46 * 2. The user dropped the trackball (released left mouse button) *
47 * i.e., the vector was changed. *
50 *****************************************************************************/
55 // Callback pointer prototype:
56 typedef void (*VectorCtlCallbackProc)( idQuat rotation );
58 // The callback should look like:
59 // void CALLBACK MyCallBack (double dVecX, double dVecY, double dVecZ);
61 // static void CALLBACK MyClass::MyCallBack (double dVecX, double dVecY, double dVecZ);
64 class CVectorCtl : public CButton
67 #define EPS 1.0e-6 // Epsilon
69 #define DEFAULT_VEC {0.00, 0.00, 1.00} // Default start vector
70 #define DEFAULT_DIFFUSE RGB( 30, 0, 200) // Default diffuse color
71 #define DEFAULT_AMBIENT RGB( 20, 20, 20) // Default ambient color
72 #define DEFAULT_LIGHT RGB(200, 200, 200) // Default light color
73 #define DEFAULT_START_BACKGROUND_COLOR RGB( 0, 0, 0) // Default gradient background start color
74 #define DEFAULT_END_BACKGROUND_COLOR RGB(140, 0, 120) // Default gradient background end color
75 #define DEFAULT_SPEC_EXP 25.0 // Default specular intensity
76 #define VAL_NOT_IN_USE -50000 // Internal use
82 virtual ~CVectorCtl ();
84 // Owner-drawn control support function
85 virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
87 // Sets / Gets diffuse (ball) color.
88 void SetDiffuseColor (COLORREF clr) { m_clrDiffuse = clr; Redraw (); }
89 COLORREF GetDiffuseColor () { return m_clrDiffuse; }
91 // Sets / Gets ambient (background) color.
92 void SetAmbientColor (COLORREF clr) { m_clrAmbient = clr; Redraw (); }
93 COLORREF GetAmbientColor () { return m_clrAmbient; }
95 // Sets / Gets light color.
96 void SetLightColor (COLORREF clr) { m_clrLight = clr; Redraw (); }
97 COLORREF GetLightColor () { return m_clrLight; }
99 // Sets background gradient color (from start to finish vertically)
100 void SetBackgroundColor (COLORREF clrStart, COLORREF clrEnd);
102 // Sets a background bitmap (resource ID)
103 BOOL SetBackgroundImage (UINT uBackgroundBitmapID);
105 // Sets / Gets specular intensity
106 BOOL SetSpecularExponent (double dExp);
107 double GetSpecularExponent () { return m_dSpecularExponent; }
109 // Enables auto-update of axis controls.
110 // Place the control's ID and the SetWindowText function will be called
111 // for each vector component to display the value in the control.
112 void SetAxisControl (int nXCtl, int nYCtl, int nZCtl);
114 // Sets / Gets ball radius (in pixels)
115 void SetRadius (UINT uRadius);
116 UINT GetRadius () { return UINT(m_iRadius); }
118 // Sets / Gets ball position (in pixels)
119 void SetCenter (UINT uHorizPos, UINT uVertPos);
120 UINT GetHorizCenter () { return UINT(m_iXCenter); }
121 UINT GetVertCenter () { return UINT(m_iYCenter); }
123 // Sets / Gets vector components
124 void SetX (double dx) { SetAxis (dx, 0); }
125 double GetX() { return m_dVec[0]; }
126 void SetY (double dy) { SetAxis (dy, 1); }
127 double GetY() { return m_dVec[1]; }
128 void SetZ (double dz) { SetAxis (dz, 2); }
129 double GetZ() { return m_dVec[2]; }
130 void SetVector (double dx, double dy, double dz);
131 void SetidAxis( const idMat3 &mat ) {
132 rotationMatrix = mat;
133 rotationQuat = mat.ToQuat();
137 // Sets / Gets mouse sensitivity
138 BOOL SetSensitivity (UINT uSens);
139 UINT GetSensitivity () { return UINT(m_dSensitivity); }
141 // Bounds / Unbounds vector to front (positive Z) only
142 void ClipToFront (BOOL bEnable) { m_bFrontVector = bEnable; }
144 // Set user-defined callback function to call whenever the vector has changed.
145 // Set to NULL to disable callback.
146 void SetVectorChangingCallback (VectorCtlCallbackProc proc)
147 { m_procVectorChanging = proc; }
149 // Set user-defined callback function to call whenever the vector has finished
150 // changing (user dropped track-ball).
151 // Set to NULL to disable callback.
152 void SetVectorChangedCallback (VectorCtlCallbackProc proc)
153 { m_procVectorChanged = proc; }
156 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
157 afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
158 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
160 // Mouse is being dragged
161 void OnMouseDrag (int , int);
162 // Create and measure off-screen buffer
163 void InitBitmap (LPDRAWITEMSTRUCT lpDrawItemStruct, CDC *pDC);
164 // Build image to BitBlt
165 void BuildImage (LPDRAWITEMSTRUCT lpDrawItemStruct);
166 // Free resources of background (non-ball) bitmap
167 void ClearBackgroundBitmap ();
170 // Calculate lightning effect for a pixel on the ball
171 COLORREF CalcLight (double dx, double dy, double dz);
172 // Rotate our vector by X and Y angles
173 void RotateByXandY (double XRot, double YRot);
174 // Create background image resource
175 void CreateBackground ();
176 // Force redraw of entire image
177 void Redraw (BOOL bErase = FALSE);
178 // Update user-defined vector components controls
179 void UpdateAxisControls ();
180 // Sets a specific vector component to a specific value
181 void SetAxis (double d, int nAxis);
183 CBitmap m_bmpBuffer, // Buffer bitmap for BitBlt
184 m_bmpBack; // Background image bitmap
185 CDC m_dcMem; // Memory DC
186 BOOL m_bBmpCreated, // Was the bitmap created ?
187 m_bBackgroundBitmapUsed,// Are we using a background bitmap ?
188 m_bImageChange, // Has the image changed ?
189 m_bFrontVector, // Is the vector constrained to be facing front (positive Z) ?
190 m_bHasFocus, // Does the control have the focus ?
191 m_bSelected; // Is the control selected ?
192 int m_iWidth, // Region width
193 m_iHeight, // Region height
194 m_iRadius, // Ball radius
195 m_iSqrRadius, // Ball radius to the power of two
196 m_iXCenter, // X center point
197 m_iYCenter; // Y center point
198 CBitmap *m_pOldBitmap; // Previously selected bitmap
199 COLORREF m_clrDiffuse, // Ball diffusion color (self color)
200 m_clrAmbient, // Ambient (background) color
201 m_clrLight, // Color of light
202 m_clrBackgroundStart, // Background color gradient start
203 m_clrBackgroundEnd; // Background color gradient end
204 CWnd *pCtl[3]; // Pointers to axis display controls
205 double m_dSpecularExponent, // Specularity effect intensity
206 m_dSensitivity; // The bigger the number the less sensitive the mouse gets
207 // Valid ranges are 1..MAX_UINT
208 idVec3 m_dVec; // Vector components
209 idMat3 rotationMatrix; //
216 VectorCtlCallbackProc m_procVectorChanging,
220 DECLARE_MESSAGE_MAP()