source: GTP/trunk/App/Demos/Illum/Standalone/Glow [DirectX]/Parameters.cpp @ 846

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