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