Sirikata
|
00001 /* Sirikata Input Plugin -- plugins/input 00002 * ButtonToAxis.hpp 00003 * 00004 * Copyright (c) 2009, Patrick Reiter Horn 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifndef SIRIKATA_INPUT_ButtonToAxis_HPP__ 00034 #define SIRIKATA_INPUT_ButtonToAxis_HPP__ 00035 00036 #include <sirikata/ogre/input/InputDevice.hpp> 00037 00038 namespace Sirikata { 00039 namespace Input { 00040 00042 class SIRIKATA_OGRE_EXPORT AxisToButton : public InputDevice { 00043 struct HatButton { 00044 AxisValue mLow; 00045 AxisValue mHigh; 00046 }; 00047 std::vector <HatButton> mButtons; 00048 00049 InputDevicePtr mParentDevice; 00050 int mParentAxis; 00051 00052 public: 00053 AxisToButton (const std::string &name, const InputDevicePtr &parent, int parentAxis) 00054 : mParentDevice(parent), mParentAxis(parentAxis) { 00055 setName(name); 00056 } 00057 00058 virtual std::string getButtonName(unsigned int num) const { 00059 std::ostringstream os ("[" + mParentDevice->getAxisName(mParentAxis) + " "); 00060 const HatButton & button = mButtons[num]; 00061 if (button.mLow.isNegative() && button.mHigh.isPositive()) { 00062 os << "Centered"; 00063 } else { 00064 bool lessZero = button.mHigh.isNegative(); 00065 AxisValue extreme = button.mHigh; 00066 if (lessZero) { 00067 extreme = button.mLow; 00068 } 00069 os << ((int)(extreme.getCentered()*100)) << "%"; 00070 } 00071 os << "]"; 00072 return os.str(); 00073 } 00074 00075 virtual int getNumButtons() const { 00076 return mButtons.size(); 00077 } 00078 virtual unsigned int getNumAxes() const { 00079 return 0; 00080 } 00081 virtual std::string getNumAxisName(unsigned int i) const { 00082 return std::string(); 00083 } 00084 00085 void addButton(AxisValue low, AxisValue high) { 00086 HatButton hb = {low, high}; 00087 mButtons.push_back(hb); 00088 } 00089 00090 }; 00091 typedef std::tr1::shared_ptr<AxisToButton> AxisToButtonPtr; 00092 00094 class SIRIKATA_OGRE_EXPORT ButtonToAxis : public InputDevice { 00095 typedef std::map <int, float> AxisMap; 00096 AxisMap mAxisPoints; 00097 InputDevicePtr mParentDevice; 00098 public: 00099 ButtonToAxis (const std::string &name, const InputDevicePtr &parent) 00100 : mParentDevice(parent) { 00101 setName(name); 00102 } 00103 00104 void addButton(int button, float value) { 00105 mAxisPoints.insert(AxisMap::value_type(button, value)); 00106 } 00107 00108 virtual std::string getAxisName(unsigned int axis) { 00109 std::string os = "[" + mParentDevice->getName() + " "; 00110 bool first = true; 00111 for (AxisMap::const_iterator iter = mAxisPoints.begin(); iter != mAxisPoints.end(); ++iter) { 00112 if (!first) { 00113 os += '/'; 00114 } 00115 first = false; 00116 os += mParentDevice->getButtonName((*iter).first); 00117 } 00118 os += "]"; 00119 return os; 00120 } 00121 virtual unsigned int getNumAxes() const { 00122 return 1; 00123 } 00124 virtual int getNumButtons() const { 00125 return 0; 00126 } 00127 virtual std::string getButtonName(unsigned int button) const { 00128 return std::string(); 00129 } 00130 00131 }; 00132 typedef std::tr1::shared_ptr<ButtonToAxis> ButtonToAxisPtr; 00133 00134 #endif