00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _CONTROLS_H
00026
00027 #include <stdio.h>
00028 #include <GL/gl.h>
00029 #include <GL/glu.h>
00030 #include <SDL/SDL.h>
00031 #ifdef __APPLE__
00032 #include <SDL_image/SDL_image.h>
00033 #else
00034 #include <SDL/SDL_image.h>
00035 #endif
00036 #include <string>
00037 #include <sstream>
00038 #include <iostream>
00039 #include <fstream>
00040 #include <cmath>
00041 #include <cassert>
00042
00043 #include <list>
00044
00045 #include "utility.h"
00046 #include "keyman.h"
00047 #include "settings.h"
00048
00049 #include "globals.h"
00050
00051 #define MAX_JOYSTICKS 32
00052 #define MAX_AXES 10
00053
00054 using namespace std;
00055
00056 enum ControllerEnum { Joy, Key, Mouse, Unknown };
00057 enum JoyTypeEnum { Button, Axis, UnknownType };
00058 enum JoyAxisTypeEnum { Positive, Negative, Both };
00059 enum MouseTypeEnum { MButton, Motion, UnknownMouse };
00060 enum MouseDirEnum { Up, Down, Left, Right };
00061
00062 class CONTROL
00063 {
00064 private:
00065 string controlname;
00066 ControllerEnum controltype;
00067
00068 int joynum;
00069 int joyaxis;
00070 JoyAxisTypeEnum joyaxistype;
00071 int joybutton;
00072 bool joybuttonlaststate;
00073 JoyTypeEnum joytype;
00074 bool joypushdown;
00075
00076 int keycode;
00077 bool onetime;
00078 bool keypushdown;
00079
00080 MouseTypeEnum mousetype;
00081 int mbutton;
00082 MouseDirEnum mdir;
00083 bool last_mouse_state;
00084 bool mouse_push_down;
00085
00086 public:
00087 void SetName(string cname) {controlname = cname;}
00088 void SetType(string type) {if (type == "joy") controltype = Joy; else if (type == "key") controltype = Key; else if( type == "mouse" ) controltype = Mouse; else {controltype = Unknown; cout << "Unknown control type: " << type << endl;}}
00089 ControllerEnum GetType() {return controltype;}
00090 string GetName() {return controlname;}
00091 void SetKeyCode(int nkey) {keycode = nkey;}
00092 int GetKeyCode() {return keycode;}
00093 void SetOneTime(bool ot) {onetime = ot;}
00094 bool GetOneTime() {return onetime;}
00095 void SetKeyPushDown(bool npd) {keypushdown = npd;}
00096 bool GetKeyPushDown() {return keypushdown;}
00097 void SetJoyNum(int jn) {joynum = jn;}
00098 int GetJoyNum() {return joynum;}
00099 void SetJoyAxis(int ja) {joyaxis = ja;}
00100 void SetJoyAxisType(JoyAxisTypeEnum ap) {joyaxistype = ap;}
00101 int GetJoyAxis() {return joyaxis;}
00102 JoyAxisTypeEnum GetJoyAxisType() {return joyaxistype;}
00103 void SetJoyButton(int jb) {joybutton = jb;}
00104 int GetJoyButton() {return joybutton;}
00105 void SetJoyType(string type) {if (type == "button") joytype = Button; else if (type == "axis") joytype = Axis; else {joytype = UnknownType; cout << "Unknown joystick input type: " << type << endl;}}
00106 JoyTypeEnum GetJoyType() {return joytype;}
00107 void Dump();
00108 void SetJoyButtonLastState(bool ls) {joybuttonlaststate = ls;}
00109 bool GetJoyButtonLastState() {return joybuttonlaststate;}
00110 void SetJoyPushDown(bool pd) {joypushdown = pd;}
00111 bool GetJoyPushDown() {return joypushdown;}
00112 MouseTypeEnum GetMouseType() { return mousetype; }
00113 void SetMouseType( string mt ) { if( mt == "button" ) mousetype = MButton; else if( mt == "motion" ) mousetype = Motion; else { mousetype = UnknownMouse; cout << "Unknown mouse input type: " << mt << endl; } }
00114 MouseDirEnum GetMouseDirection() { return mdir; }
00115 void SetMouseDirection( string md ) { if( md == "up" ) mdir = Up; else if( md == "down" ) mdir = Down; else if( md == "left" ) mdir = Left; else if( md == "right" ) mdir = Right; else { mdir = Left; cout << "mouse direction not found: " << md << endl; } }
00116 int GetMouseButton() { return mbutton; }
00117 void SetMouseButton( string mb ) { int mbut = 0; istringstream mbss; mbss.str( mb ); mbss >> mbut; mbutton = mbut; }
00118 void SetMouseButton( int mb ) { mbutton = mb; }
00119 bool GetLastMouseState() { return last_mouse_state; }
00120 void SetLastMouseState( bool new_state ) { last_mouse_state = new_state; }
00121 bool GetMousePushDown() { return mouse_push_down; }
00122 void SetMousePushDown( bool new_push_down ) { mouse_push_down = new_push_down; }
00123 };
00124
00125 class GAMECONTROLS
00126 {
00127 private:
00128
00129 list <CONTROL> controls;
00130
00131 void ControlParseError(int line, ifstream & ffrom);
00132 ofstream error_log;
00133 string joysticktype;
00134 string compensation;
00135 string deadzone;
00136 float calibration[MAX_JOYSTICKS][MAX_AXES][2];
00137 SDL_Joystick * js[MAX_JOYSTICKS];
00138
00139 list <CONTROL>::iterator control_iterator;
00140
00141 public:
00142 GAMECONTROLS();
00143 ~GAMECONTROLS();
00144 void LoadControls() {LoadControls(settings.GetSettingsDir() + "/controls");}
00145 void LoadControls(string fname);
00146
00147 void ControlIteratorReset() {control_iterator = controls.begin();}
00148 CONTROL * ControlIteratorGetControl() {if (control_iterator != controls.end()) return &(*control_iterator); else return NULL;}
00149 void ControlIteratorIncrement() {control_iterator++;}
00150 int GetNumControls();
00151 string GetJoystickType() {return joysticktype;}
00152 void SetJoystickType(string newtype) {joysticktype = newtype;}
00153 void SetControls(CONTROL * newcontrols, int newnum);
00154 void WriteControlFile();
00155 void WriteControlFile(string fname);
00156 string GetCompensation() {return compensation;}
00157 void SetCompensation(string newcomp) {compensation = newcomp;}
00158 string GetDeadzone() {return deadzone;}
00159 void SetDeadzone(string newdeadzone) {deadzone = newdeadzone;}
00160
00161 void DeleteControlsWithAction(string actionstr);
00162 void AddControl(CONTROL & other);
00163
00164 void LoadCalibration();
00165 void WriteCalibration();
00166 float GetCalibration(int joynum, int joyaxis, bool max);
00167 void MinimizeCalibration();
00168 void SetCalibrationPoint(int joynum, int joyaxis, float val);
00169
00170 SDL_Joystick * GetJoy(int joynum) {return js[joynum];}
00171 SDL_Joystick ** Get_js() {return js;}
00172 void InitJoy();
00173 void DeinitJoy();
00174 void UpdateSettings();
00175 };
00176
00177 #define _CONTROLS_H
00178 #endif