00001 #include <sstream>
00002 #include "utility.h"
00003 #include "controls.h"
00004 #include "globals.h"
00005 #include "gui/calibrate.h"
00006 using namespace VGUI;
00007
00008 Calibrate::Calibrate( Widget* w )
00009 {
00010
00011 center = w->GetCenter();
00012 name = w->GetName();
00013 text = w->GetText();
00014 type = w->GetType();
00015 tip = w->GetTip();
00016 rel_width = w->GetRelWidth();
00017 rel_height = w->GetRelHeight();
00018 enabled = w->GetEnabled();
00019 selected = w->GetSelected();
00020 is_default = w->GetDefault();
00021 is_cancel = w->GetCancel();
00022 has_icon = w->GetHasIcon();
00023 width_auto = w->GetWidthAuto();
00024 height_auto = w->GetHeightAuto();
00025 font_size = w->GetFontSize();
00026 color_r = w->GetColorR();
00027 color_g = w->GetColorG();
00028 color_b = w->GetColorB();
00029
00030 int _w, _h;
00031 string skin_path = settings.GetSkinPath();
00032 joy_cal_box_tex.Load( skin_path + "/textures/widgets/joy_cal_box.png", false, _w, _h );
00033 axis_field_tex.Load( skin_path + "/textures/widgets/axis_field.png", false, _w, _h );
00034 joy_cursor_tex.Load( skin_path + "/textures/widgets/joy_cursor.png", false, _w, _h );
00035
00036 field_width = 256.0f / 1600.0f;
00037 field_height = 256.0f / 1200.0f;
00038 cursor_width = 64.0f / 1600.0f;
00039 cursor_height = 64.0f / 1200.0f;
00040 cur_joy_idx = 0;
00041
00042
00043
00044
00045
00046
00047 if( width_auto )
00048 {
00049 rel_width = field_width + ( 4.0 / 1600.0 );
00050 }
00051
00052
00053 if( height_auto )
00054 {
00055 rel_height = field_height + ( 4.0 / 1600.0 );
00056 }
00057 }
00058
00059 Calibrate::~Calibrate()
00060 {
00061 ResetValue();
00062 }
00063
00064 void Calibrate::Save()
00065 {
00066 map< int, map<int, CalSet*> >::iterator joy_idx_iter;
00067 map<int, CalSet*>::iterator joy_axis_iter;
00068 CalSet* joy_axis_cal;
00069 int joy_idx, joy_axis;
00070
00071 gamecontrols.MinimizeCalibration();
00072
00073 for( joy_idx_iter = calibrations.begin(); joy_idx_iter != calibrations.end(); ++joy_idx_iter )
00074 {
00075 joy_idx = joy_idx_iter->first;
00076
00077 for( joy_axis_iter = joy_idx_iter->second.begin(); joy_axis_iter != joy_idx_iter->second.end(); ++joy_axis_iter )
00078 {
00079 joy_axis = joy_axis_iter->first;
00080 joy_axis_cal = joy_axis_iter->second;
00081
00082 gamecontrols.SetCalibrationPoint( joy_idx, joy_axis, joy_axis_cal->GetUpperValue() );
00083 gamecontrols.SetCalibrationPoint( joy_idx, joy_axis, joy_axis_cal->GetLowerValue() );
00084 }
00085 }
00086 gamecontrols.WriteCalibration();
00087 }
00088
00089 void Calibrate::ResetValue()
00090 {
00091 map< int, map<int, CalSet*> >::iterator joy_idx_iter;
00092 map<int, CalSet*>::iterator joy_axis_iter;
00093
00094 for( joy_idx_iter = calibrations.begin(); joy_idx_iter != calibrations.end(); ++joy_idx_iter )
00095 {
00096 for( joy_axis_iter = joy_idx_iter->second.begin(); joy_axis_iter != joy_idx_iter->second.end(); ++joy_axis_iter )
00097 {
00098 delete joy_axis_iter->second;
00099 }
00100
00101 joy_idx_iter->second.clear();
00102 }
00103
00104 calibrations.clear();
00105
00106 cur_joy_idx = 0;
00107 }
00108
00109 void Calibrate::Draw()
00110 {
00111 Draw( 1.0 );
00112 }
00113
00114 void Calibrate::Draw( float opacity )
00115 {
00116
00117 map<int, CalSet*>::iterator joy_axis_iter;
00118 CalSet *tmp_cs;
00119 float center_x = center.GetXPercent();
00120 float center_y = center.GetYPercent();
00121 float rel_width_half = rel_width / 2.0f;
00122 float rel_height_half = rel_height / 2.0f;
00123 float curs_width_half = cursor_width / 2.0f;
00124 float curs_height_half = cursor_height / 2.0f;
00125 float opacity_txt = selected ? 0.9 : enabled ? 0.75 : 0.4;
00126 float text_width_half = ( font.Width( "Axis 0", 1, 5 ) / 2.0f );
00127 float upper_val_1, lower_val_1, cur_val_1, upper_val_2, lower_val_2, cur_val_2;
00128 float cursor_x, cursor_y;
00129 float total_width, total_width_half, field_offset, increment;
00130 float cal_box_l_offset, cal_box_r_offset, cal_box_t_offset, cal_box_b_offset;
00131 int num_axes, num_fields, i;
00132 string axis_name_1, axis_name_2, joy_text;
00133 ostringstream axis_namer;
00134
00135 joy_text = ( SDL_NumJoysticks() > 0 ) ? SDL_JoystickName( cur_joy_idx ) : "[No Joysticks...]";
00136 num_axes = calibrations[cur_joy_idx].size();
00137 num_fields = ( num_axes + 1 ) / 2;
00138 total_width = rel_width + ( (float)( 2 * ( num_fields - 1 ) ) * rel_width );
00139 total_width_half = total_width / 2.0f;
00140 increment = num_axes <= 2 ? 0 : ( total_width + rel_width ) / (float)num_fields;
00141 i = 0;
00142
00143 font.Print( center_x - ( font.Width( joy_text.c_str(), 1, font_size ) / 2.0f ),
00144 center_y - rel_height_half - ( 2.0f * cursor_height ),
00145 joy_text.c_str(), 1, font_size, color_r, color_g, color_b, opacity_txt * opacity );
00146
00147 for( joy_axis_iter = calibrations[cur_joy_idx].begin(); joy_axis_iter != calibrations[cur_joy_idx].end(); ++joy_axis_iter )
00148 {
00149 field_offset = (float)i * increment;
00150
00151 tmp_cs = joy_axis_iter->second;
00152 upper_val_1 = tmp_cs->GetUpperValue();
00153 lower_val_1 = tmp_cs->GetLowerValue();
00154 cur_val_1 = tmp_cs->GetCurrentValue();
00155
00156 axis_namer.str("");
00157 axis_namer << "Axis ";
00158 axis_namer << joy_axis_iter->first;
00159 axis_name_1 = axis_namer.str();
00160
00161 ++joy_axis_iter;
00162
00163 if( joy_axis_iter != calibrations[cur_joy_idx].end() )
00164 {
00165 tmp_cs = joy_axis_iter->second;
00166 upper_val_2 = tmp_cs->GetUpperValue();
00167 lower_val_2 = tmp_cs->GetLowerValue();
00168 cur_val_2 = tmp_cs->GetCurrentValue();
00169
00170 axis_namer.str("");
00171 axis_namer << "Axis ";
00172 axis_namer << joy_axis_iter->first;
00173 axis_name_2 = axis_namer.str();
00174 }
00175 else
00176 {
00177 upper_val_2 = lower_val_2 = cur_val_2 = 0.0;
00178 axis_name_2 = "";
00179 }
00180
00181
00182 utility.Draw2D( center_x - total_width_half + field_offset,
00183 center_y - rel_height_half,
00184 center_x - total_width_half + field_offset + rel_width,
00185 center_y + rel_height_half,
00186 &axis_field_tex );
00187
00188
00189 cal_box_l_offset = ( 1.0 + lower_val_1 ) * rel_width_half;
00190 cal_box_r_offset = ( 1.0 - upper_val_1 ) * rel_width_half;
00191 cal_box_t_offset = ( 1.0 + lower_val_2 ) * rel_height_half;
00192 cal_box_b_offset = ( 1.0 - upper_val_2 ) * rel_height_half;
00193
00194 utility.Draw2D( center_x - total_width_half + field_offset + cal_box_l_offset,
00195 center_y - rel_height_half + cal_box_t_offset,
00196 center_x - total_width_half + field_offset + rel_width - cal_box_r_offset,
00197 center_y + rel_height_half - cal_box_b_offset,
00198 &joy_cal_box_tex );
00199
00200 cursor_x = center_x - total_width_half + field_offset + rel_width_half + ( ( field_width / 2.0 ) * cur_val_1 );
00201 cursor_y = center_y + ( ( field_height / 2.0 ) * cur_val_2 );
00202
00203
00204 utility.Draw2D( cursor_x - curs_width_half,
00205 cursor_y - curs_height_half,
00206 cursor_x + curs_width_half,
00207 cursor_y + curs_height_half,
00208 &joy_cursor_tex );
00209
00210
00211 font.Print( center_x - total_width_half + field_offset + rel_width_half - text_width_half,
00212 center_y - rel_height_half - cursor_height,
00213 axis_name_1.c_str(), 1, font_size, color_r, color_g, color_b, opacity_txt * opacity );
00214
00215 if( joy_axis_iter == calibrations[cur_joy_idx].end() )
00216 {
00217 break;
00218 }
00219 else
00220 {
00221 font.Print( center_x - total_width_half + field_offset - ( text_width_half * 4.0f ),
00222 center_y - curs_height_half,
00223 axis_name_2.c_str(), 1, font_size, color_r, color_g, color_b, opacity_txt * opacity );
00224 }
00225
00226 i++;
00227 }
00228 }
00229
00230 void Calibrate::GrabJoyMove( Uint8 _joy_idx, Uint8 _joy_axis, bool joy_positive, float joy_value )
00231 {
00232 int joy_idx = (int)_joy_idx;
00233 int joy_axis = (int)_joy_axis;
00234 if( calibrations[joy_idx][joy_axis] != NULL )
00235 {
00236 if( calibrations[joy_idx][joy_axis]->GetUpperValue() < joy_value )
00237 {
00238 calibrations[joy_idx][joy_axis]->SetUpperValue( joy_value );
00239 }
00240 else if( calibrations[joy_idx][joy_axis]->GetLowerValue() > joy_value )
00241 {
00242 calibrations[joy_idx][joy_axis]->SetLowerValue( joy_value );
00243 }
00244 }
00245 else
00246 {
00247 CalSet *new_cal;
00248 new_cal = new CalSet( 0.0, 0.0 );
00249 calibrations[joy_idx][joy_axis] = new_cal;
00250 }
00251
00252 calibrations[joy_idx][joy_axis]->SetCurrentValue( joy_value );
00253 }
00254
00255 void Calibrate::HookIncValueRelease()
00256 {
00257 if( cur_joy_idx < SDL_NumJoysticks() - 1 )
00258 {
00259 cur_joy_idx++;
00260 }
00261 }
00262
00263 void Calibrate::HookDecValueRelease()
00264 {
00265 if( cur_joy_idx > 0 )
00266 {
00267 cur_joy_idx--;
00268 }
00269 }