1 | #include "dxstdafx.h"
|
---|
2 | #include "Parameters.h"
|
---|
3 |
|
---|
4 | float noconvert(float a) {
|
---|
5 | return a;
|
---|
6 | }
|
---|
7 |
|
---|
8 | float convertLightScale(float a)
|
---|
9 | {
|
---|
10 | return a * a * a * a * a * 99.0f + 1.0f;
|
---|
11 | }
|
---|
12 |
|
---|
13 | float convertTorchDistance(float a)
|
---|
14 | {
|
---|
15 | return a * 10.0;
|
---|
16 | }
|
---|
17 |
|
---|
18 | float convertInt4(float a)
|
---|
19 | {
|
---|
20 | return a * 3.0;
|
---|
21 | }
|
---|
22 |
|
---|
23 | void OnChange() {
|
---|
24 | }
|
---|
25 |
|
---|
26 | void Parameters::Setup( CDXUTDialog* g_HUD ) {
|
---|
27 | Setup(g_HUD, OnChange, OnChange, OnChange);
|
---|
28 | }
|
---|
29 |
|
---|
30 | void Parameters::Setup( CDXUTDialog* g_HUD, ONCHANGE_CALLBACK OnReset, ONCHANGE_CALLBACK OnSave, ONCHANGE_CALLBACK OnLoad )
|
---|
31 | {
|
---|
32 | assert( g_HUD != NULL );
|
---|
33 | this->g_HUD = g_HUD;
|
---|
34 | g_HUD->GetDefaultElement( DXUT_CONTROL_STATIC, 0 )->dwTextFormat = DT_VCENTER | DT_LEFT;
|
---|
35 |
|
---|
36 | for (int i=0; i<LAST_NUMBER; i++) {
|
---|
37 | ffunc[i] = noconvert;
|
---|
38 | chfunc[i] = OnChange;
|
---|
39 | }
|
---|
40 |
|
---|
41 | int posX = 380;
|
---|
42 | int posY = 50 + LAST_BOOL*16;
|
---|
43 | g_HUD->AddButton( IDC_RESET_BUTTON, L"[R] Reset", posX, posY += 50, 60, 16, 'R' );
|
---|
44 | g_HUD->AddButton( IDC_SAVE_BUTTON, L"[X] Save", posX + 70, posY, 60, 16, 'X' );
|
---|
45 | g_HUD->AddButton( IDC_LOAD_BUTTON, L"[L] Load", posX + 140, posY, 60, 16, 'L' );
|
---|
46 | // g_HUD->AddButton( IDC_GEN_BUTTON, L"[G] Regenerate", posX - 250, posY, 100, 16, 'L' );
|
---|
47 | chfunc[ LAST_NUMBER ] = OnReset;
|
---|
48 | chfunc[ LAST_NUMBER+1 ] = OnSave;
|
---|
49 | chfunc[ LAST_NUMBER+2 ] = OnLoad;
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool Parameters::Get( bool_t i ) {
|
---|
53 | return bparam[i];
|
---|
54 | }
|
---|
55 |
|
---|
56 | float Parameters::Get( number_t i ) {
|
---|
57 | //assert( ffunc[i] != NULL );
|
---|
58 | return ffunc[i]( (float)param[i] / numsteps[i] ); // 0..1
|
---|
59 | }
|
---|
60 |
|
---|
61 | int Parameters::GetInt( number_t i ) {
|
---|
62 | //assert( ffunc[i] != NULL );
|
---|
63 | float r = ffunc[i]( (float)param[i] / numsteps[i] );
|
---|
64 | return (int)(r+0.5); // 0..numsteps[i]
|
---|
65 | }
|
---|
66 |
|
---|
67 | void Parameters::Add( bool_t ID, char* label, char cHotKey ) {
|
---|
68 |
|
---|
69 | assert( cHotKey != 'R' && cHotKey != 'S' && cHotKey != 'L' );
|
---|
70 | mbstowcs( bname[ID], label, CHARBUFFER_SIZE );
|
---|
71 |
|
---|
72 | int posX = 380;
|
---|
73 | int posY = 50;
|
---|
74 | CDXUTCheckBox* c;
|
---|
75 | g_HUD->AddCheckBox( ID+checkboxID0, bname[ID], posX, posY + ID*16, 150, 16, false, cHotKey, 0 );
|
---|
76 | // checked=false
|
---|
77 | }
|
---|
78 |
|
---|
79 | void Parameters::SetBool( bool_t ID, bool b ) {
|
---|
80 | bparam[ID] = b;
|
---|
81 |
|
---|
82 | bSilent = true;
|
---|
83 | if( g_HUD->GetCheckBox( ID+checkboxID0 ) != NULL )
|
---|
84 | g_HUD->GetCheckBox( ID+checkboxID0 )->SetChecked( bparam[ID] );
|
---|
85 | bSilent = false;
|
---|
86 | }
|
---|
87 |
|
---|
88 | void Parameters::SetFloat( number_t ID, float v ) {
|
---|
89 | SetInt( ID, (int)( v*numsteps[ID] + 0.5 ));
|
---|
90 | }
|
---|
91 |
|
---|
92 | void Parameters::SetInt( number_t ID, int v ) {
|
---|
93 | param[ID] = v;
|
---|
94 | if ( param[ID] < 0 ) param[ID] = 0;
|
---|
95 | else if ( param[ID] > numsteps[ID] ) param[ID] = numsteps[ID];
|
---|
96 |
|
---|
97 | bSilent = true;
|
---|
98 |
|
---|
99 | // minden frissül
|
---|
100 | if( g_HUD->GetSlider( ID+sliderID0 ) != NULL )
|
---|
101 | g_HUD->GetSlider( ID+sliderID0 )->SetValue( v );
|
---|
102 |
|
---|
103 | wchar_t text[30];
|
---|
104 | _sntprintf( text, 30, L"%.2f", Get( (number_t)ID ) );
|
---|
105 |
|
---|
106 | if( g_HUD->GetStatic( ID+staticID0 ) != NULL )
|
---|
107 | g_HUD->GetStatic( ID+staticID0 )->SetText( text );
|
---|
108 |
|
---|
109 | // user-defined callback
|
---|
110 | //assert( chfunc[ID] != NULL );
|
---|
111 | chfunc[ID]();
|
---|
112 |
|
---|
113 | bSilent = false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void Parameters::Add( number_t ID, char* label, int num_steps) {
|
---|
117 | Add( ID, label, num_steps, noconvert );
|
---|
118 | }
|
---|
119 |
|
---|
120 | void Parameters::Add( number_t ID, char* label, int num_steps, CONVERTER ff, ONCHANGE_CALLBACK chf ) {
|
---|
121 | char cKeyDown = 0;
|
---|
122 | char cKeyUp = 0;
|
---|
123 | Add( ID, label, num_steps, cKeyDown, cKeyUp, ff, chf);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void Parameters::Add( number_t ID, char* label, int num_steps, char cKeyDecr, char cKeyIncr, CONVERTER ff, ONCHANGE_CALLBACK chf ) {
|
---|
127 |
|
---|
128 | assert( cKeyDecr != 'R' && cKeyDecr != 'S' && cKeyDecr != 'L' );
|
---|
129 | assert( cKeyIncr != 'R' && cKeyIncr != 'S' && cKeyIncr != 'L' );
|
---|
130 |
|
---|
131 | mbstowcs( name[ID], label, CHARBUFFER_SIZE );
|
---|
132 | /*wchar_t val[30];
|
---|
133 | _sntprintf( val, 30, L"%.2f", param[ID] / 100.0f ); // nem kell */
|
---|
134 |
|
---|
135 | //cHotKey
|
---|
136 | ffunc[ID] = ff;
|
---|
137 | chfunc[ID] = chf;
|
---|
138 | numsteps[ID] = num_steps;
|
---|
139 |
|
---|
140 | int posX = 210;
|
---|
141 | int posY = 50;
|
---|
142 | g_HUD->AddStatic( 0, name[ID], posX-180, posY + ID*16, 120, 14 );
|
---|
143 | g_HUD->AddStatic( ID+staticID0, L"", posX-50, posY + ID*16, 120, 14 );
|
---|
144 | g_HUD->AddSlider( ID+sliderID0, posX, posY + ID*16, 120, 14, 0, num_steps );
|
---|
145 | //UpdateFromHUD( ID+sliderID0 );
|
---|
146 |
|
---|
147 | // hogy ne kelljen azt eltárolni, hogy melyik hotkey melyik sliderhez tartozik,
|
---|
148 | // 2 láthatatlan checkboxot hozok létre ezekkel a hotkeyekkel.
|
---|
149 | // a checkbox id alapján azonosíthatom az érintett slidert.
|
---|
150 | CDXUTCheckBox* c;
|
---|
151 | g_HUD->AddCheckBox( ID+downID0, L"d", posX-20, posY + ID*16, 200, 16, 0, cKeyDecr, 0, &c );
|
---|
152 | c->m_bVisible = false;
|
---|
153 | g_HUD->AddCheckBox( ID+upID0, L"u", posX-20, posY + ID*16, 200, 16, 0, cKeyIncr, 0, &c );
|
---|
154 | c->m_bVisible = false;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void Parameters::UpdateFromHUD( int controlID ) {
|
---|
158 |
|
---|
159 | //if (bSilent) return;
|
---|
160 |
|
---|
161 | // Reset, Save & Load buttons
|
---|
162 | if (controlID == IDC_RESET_BUTTON ) {
|
---|
163 | LoadFromFile( ".params0" );
|
---|
164 | chfunc[ LAST_NUMBER ]();
|
---|
165 | }
|
---|
166 | else if (controlID == IDC_SAVE_BUTTON ) {
|
---|
167 | SaveToFile( ".params" );
|
---|
168 | chfunc[ LAST_NUMBER+1 ]();
|
---|
169 | }
|
---|
170 | else if (controlID == IDC_LOAD_BUTTON ) {
|
---|
171 | LoadFromFile( ".params" );
|
---|
172 | chfunc[ LAST_NUMBER+2 ]();
|
---|
173 | }
|
---|
174 |
|
---|
175 | // a checkbox
|
---|
176 | else if (controlID >= checkboxID0 && controlID < checkboxID0+LAST_BOOL) {
|
---|
177 | bool b = g_HUD->GetCheckBox( controlID )->GetChecked();
|
---|
178 | int ID = controlID - checkboxID0;
|
---|
179 | SetBool( (bool_t)ID, b);
|
---|
180 | }
|
---|
181 |
|
---|
182 | // a slider
|
---|
183 | else if (controlID >= sliderID0 && controlID < sliderID0+LAST_NUMBER) {
|
---|
184 | int v = g_HUD->GetSlider( controlID )->GetValue();
|
---|
185 | int ID = controlID - sliderID0;
|
---|
186 | SetInt( (number_t)ID, v);
|
---|
187 | }
|
---|
188 |
|
---|
189 | // a hotkey (bound to an invisible checkbox) that decrements a slider
|
---|
190 | else if (controlID >= downID0 && controlID < downID0+LAST_NUMBER) { // down
|
---|
191 | int ID = controlID-downID0;
|
---|
192 | SetInt( (number_t)ID, param[ID]-1 );
|
---|
193 | }
|
---|
194 |
|
---|
195 | // a hotkey (bound to an invisible checkbox) that increments a slider
|
---|
196 | else if (controlID >= upID0 && controlID < upID0+LAST_NUMBER) { // up
|
---|
197 | int ID = controlID-upID0;
|
---|
198 | SetInt( (number_t)ID, param[ID]+1 );
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | void Parameters::SaveToFile( char* fileName ) {
|
---|
203 | FILE* fileS;
|
---|
204 | if( ( fileS = fopen( fileName, "wt" ) ) == NULL )
|
---|
205 | {
|
---|
206 | wchar_t wbuf[ CHARBUF ];
|
---|
207 | mbstowcs( wbuf, fileName, CHARBUF );
|
---|
208 | MessageBox( NULL, wbuf, L"File creation failed!", MB_ICONERROR ); // show error message
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | fwprintf( fileS, L"----- RayTraceEffects: Saved parameters of the algorithm -----\n" );
|
---|
213 |
|
---|
214 | fwprintf( fileS, L"Bool values:\n" );
|
---|
215 | for ( int i=0; i<LAST_BOOL; i++)
|
---|
216 | fwprintf( fileS, L"%i", (int)bparam[i] );
|
---|
217 | fwprintf( fileS, L"\n" );
|
---|
218 |
|
---|
219 | fwprintf( fileS, L"(" );
|
---|
220 | for ( int i=0; i<LAST_BOOL; i++)
|
---|
221 | fwprintf( fileS, L"%s; ", bname[i] );
|
---|
222 | fwprintf( fileS, L")\n" );
|
---|
223 |
|
---|
224 | fwprintf( fileS, L"Float values:\n" );
|
---|
225 |
|
---|
226 | for ( int i=0; i<LAST_NUMBER; i++)
|
---|
227 | fwprintf( fileS, L"%i \t(%s, 0..%i)\n", param[i], name[i], numsteps[i] );
|
---|
228 |
|
---|
229 | fclose(fileS);
|
---|
230 | }
|
---|
231 |
|
---|
232 | void Parameters::LoadFromFile( char* fileName ) {
|
---|
233 | FILE* fileL;
|
---|
234 | if( ( fileL = fopen( fileName, "rt" ) ) == NULL )
|
---|
235 | {
|
---|
236 | wchar_t wbuf[ CHARBUF ];
|
---|
237 | mbstowcs( wbuf, fileName, CHARBUF );
|
---|
238 | MessageBox( NULL, wbuf, L"File not found!", MB_ICONERROR ); // show error message
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | char buf[ CHARBUF ];
|
---|
243 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
244 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
245 |
|
---|
246 | for ( int i=0; i<LAST_BOOL; i++)
|
---|
247 | {
|
---|
248 | int num = fgetc( fileL ) - '0';
|
---|
249 | SetBool( (bool_t)i, num > 0 );
|
---|
250 | }
|
---|
251 |
|
---|
252 | fgets( buf, CHARBUF, fileL ); // skip line
|
---|
253 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
254 | fgets( buf, CHARBUF, fileL ); // skip comment
|
---|
255 |
|
---|
256 | for ( int i=0; i<LAST_NUMBER; i++)
|
---|
257 | {
|
---|
258 | fgets( buf, CHARBUF, fileL );
|
---|
259 | int v;
|
---|
260 | sscanf( buf, "%i", &v );
|
---|
261 | SetInt( (number_t)i, v );
|
---|
262 | }
|
---|
263 |
|
---|
264 | fclose(fileL);
|
---|
265 | } |
---|