Commits

CSBerger  committed 2d11212

reverted to previous work

  • Participants
  • Parent commits c4c8d70

Comments (0)

Files changed (2)

File InputHandler.cpp

 #include <fstream>
 #include <iostream>
 #include <vector>
+#include <set>
+#include <algorithm>
 #include "InputHandler.h"
 #include "GL/glfw.h"
 #include "util.h"
+#include "boost/algorithm/string.hpp"
+#include "boost/algorithm/string/predicate.hpp"
+using namespace std;
 
-using namespace std;
+void InputHandler::bindAxisString(InputActions action, string value) {
+    vector<string> split_result;
+    boost::algorithm::split(split_result, value, boost::is_any_of(","));
+    for (auto i = split_result.begin(); i != split_result.end(); ++i) {
+        LOGMSG(TAG, "%s", (*i).c_str());
+    }
+    if (split_result.size() == 4) {
+        string controller_type = boost::algorithm::trim_copy(split_result[0]);
+        
+        string device_num = boost::algorithm::trim_copy(split_result[1]);
+        string input_type = boost::algorithm::trim_copy(split_result[2]);
+        string axis_num = boost::algorithm::trim_copy(split_result[3]);
+        int axis, device;
+        axis= std::stoi(axis_num);
+        device = std::stoi(device_num);
+        std::pair<int, int> p = std::make_pair<int, int>(device,axis);
+        inputAxisMapToDeviceAndDeviceAxis[action] = p;
+    }
+}
+
+const std::map<std::string, InputActions>& InputHandler::getActionBindings() {
+    if ( name_to_action_map.empty()) {
+        name_to_action_map[string("yaw_axis")] = InputActions(ANALOG_YAW);
+        name_to_action_map[string("roll_axis")] = InputActions(ANALOG_ROLL);
+        name_to_action_map[string("pitch_axis")] = InputActions(ANALOG_PITCH);
+        name_to_action_map[string("throttle_axis")] = InputActions(ANALOG_THROTTLE);
+        for (auto i = name_to_action_map.cbegin(); i != name_to_action_map.cend(); i++) {
+            LOGMSG(23, "%s -- %d", (*i).first.c_str(), (*i).second);
+        }
+    }
+    return name_to_action_map;
+
+}
+
 
 
 void InputHandler::loadKeyBindingsFromFile(const 
                                            string & filename) {
-    ifstream* nFile = new ifstream(filename.c_str());
 
+    std::map<string, InputActions> actionTitleMap = getActionBindings();
 //	ifstream file = ifstream(filename.c_str());
 	char line[100];
 	int i = 0;
 
+//Log contents of file
+    ifstream* nFile = new ifstream(filename.c_str());
+
 	while (nFile->good()) {
 		stringstream s;
-		s << i++;
 		nFile->getline(line, 99);
 		s << line;
-		LOGMSG(1, s.str().c_str());
+        size_t equal_pos = s.str().find('=');
+        if (equal_pos != string::npos) {
+		    LOGMSG(TAG, s.str().c_str());
+            vector<string> split_results;
+            boost::algorithm::split(split_results,s.str(), boost::is_any_of("="));
+            LOGMSG(TAG, "line: '%s'", split_results[0].c_str());
+            if (actionTitleMap.find(split_results[0]) != actionTitleMap.end()) {
+                LOGMSG(TAG, "action FOUND");
+                actionTitleMap.find(string(split_results[0]));
+                LOGMSG(TAG, "Action found! %s -- %d", split_results[0].c_str(), 
+                    actionTitleMap[split_results[0]]);
+                if (actionTitleMap[split_results[0]]) {
+                    
+                    bindAxisString(actionTitleMap[split_results[0]], split_results[1]);
+                }
+                LOGMSG(TAG, "ANALOG_THROTTLE = %d", ANALOG_THROTTLE);
+                string split_value(split_results[1]);
+                split_results.clear();
+                boost::algorithm::split(split_results, split_value, boost::is_any_of(","));
+                LOGMSG(TAG, "value = %s", split_value.c_str());
+            }
+        }
 	}
 }
 void InputHandler::initHandler() {
 	actionVec = vector<int>(actions, actions + sizeof(actions) / sizeof(int));
 }
 
-vector<int> list_input_devices() {
+ vector<int> InputHandler::list_input_devices()  {
 	vector<int> joysticks;
-	for (int i = 1; i <= 16; ++i) {
-		if (glfwGetJoystickParam(i, GLFW_PRESENT) == GL_TRUE) {
+	for (int i = 0; i < 16; ++i) {
+		if (glfwGetJoystickParam(i, GLFW_PRESENT)) {
 			joysticks.push_back(i);
+            LOGMSG(TAG, "Joystick #%d is present", i);
 		}
-		stringstream s;
-		s << "Joystick #" << i  << "==" << glfwGetJoystickParam(i-1, GLFW_PRESENT);
-		LOGMSG(0,s.str().c_str());
 	}
 	return joysticks;
 }
 
-int get_input_joystick() {
+ int InputHandler::get_input_joystick() {
 	vector<int> joysticks = list_input_devices();
 	for (auto i = joysticks.begin(); i != joysticks.end(); ++i) {
 		return (*i);
 	}
-	return 0;
+	return -1;
 }
 
 void InputHandler::loadKeyBindingsFromDefault() {
 	actionsToKeyBindings[FIRE_GUN] = GLFW_KEY_SPACE;
 }
 
+std::map<int, vector<pair<int, InputActions> > > InputHandler::deviceToInputActions(map<InputActions, std::pair<int, int> > axis_map) {
+    std::map<int, vector<pair<int, InputActions> > > devicesToInputAndActions;
+    for ( auto i = axis_map.cbegin(); i != axis_map.cend(); ++i) {
+        InputActions action = (*i).first;
+        int device = (*i).second.first;
+        int device_axis = (*i).second.second;
+        devicesToInputAndActions[device].push_back(make_pair(device_axis, action));
+//        devicesToInputAndActions
+    }
+    return devicesToInputAndActions;
+
+
+}
 void InputHandler::translateInputToActions(map<int, bool> & actionButtonsHeldDown, map<int, float> &steeringAxes) {
-
 	//Get Joystick button input
 	unsigned char joy_buttons[10];
+    
 	glfwGetJoystickButtons(joystick_num, joy_buttons, 10);
-
-
 	//Get Keyboard Input
 	for (auto i = actionVec.begin(); i != actionVec.end(); ++i) {
 		if(actionsToKeyBindings[*i] < JOY_MASK) {		
 		}
 	}
 	//Get Joystick axes input
-	float pos[6];
-	int n =  glfwGetJoystickPos(joystick_num, pos, 6);
-	for (int i = 0; i < n; i++) {
-		steeringAxes[i] = clamp(pos[i], -1.0, 1.0);
-	}
+    std::set<int> device_ids;
+//    map<int, vector<pair<int, InputActions> > > action_map;
+//    map<int, vector<pair<int, InputActions> > > device_to_action_bindings = deviceToInputActions();
 
+    //compile list of devices that are used for input
+    for (auto i = inputAxisMapToDeviceAndDeviceAxis.cbegin(); i != inputAxisMapToDeviceAndDeviceAxis.cend(); ++i) {
+        device_ids.insert((*i).second.first);
+    }
+//    action_map = deviceToInputActions(inputAxisMapToDeviceAndDeviceAxis);
+    auto action_map = deviceToInputActions(inputAxisMapToDeviceAndDeviceAxis);
+    map<InputActions, float> steering_axes;
+    for(auto i = device_ids.cbegin(); i != device_ids.cend(); ++i) {
+        joystick_num = *i;
+        
+        vector<pair<int, InputActions> > bound_axes_on_this_device = action_map[joystick_num];
+        float pos[6];
+        glfwGetJoystickPos(joystick_num, pos, 6);
+        for (auto axis_it = bound_axes_on_this_device.cbegin(); axis_it != bound_axes_on_this_device.cend(); ++axis_it) {
+            steering_axes[axis_it->second] = pos[(*axis_it).first];
+            if ((*axis_it).first < 0) {
+                steering_axes[axis_it->second] *= -1;
+            }
+        }
+    }
+    steeringAxes[0] = clamp(steering_axes[ANALOG_YAW], -1.0, 1.0);
+    steeringAxes[1] = clamp(steering_axes[ANALOG_PITCH], -1.0, 1.0);
+    steeringAxes[2] = clamp(steering_axes[ANALOG_THROTTLE], -1.0, 1.0);
+    steeringAxes[3] = clamp(steering_axes[ANALOG_ROLL], -1.0, 1.0);
+/*
+    if (getPrimaryGameController() == InputType(JOYSTICK)) {
+        float pos[6];
+        int axis_count =  glfwGetJoystickPos(joystick_num, pos, 6);
+        for (int i = 0; i < axis_count; i++) {
+            steeringAxes[i] = clamp(pos[i], -1.0, 1.0);
+        }
+    }
+  */  
 }
 
+ enum InputType InputHandler::getPrimaryGameController() {
+    int joy = get_input_joystick();
+    if (joy != -1) {
+        return InputType(JOYSTICK);
+    }
+    else {
+        return InputType(KEYBOARD);
+    }
+}
 InputHandler::~InputHandler(void)
 {
 }

File nightspace.suo

Binary file modified.