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