Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/InputDevice.h @ 8858

Last change on this file since 8858 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 9.4 KB
RevLine 
[3274]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
[3327]31@brief
32    Implementation of InputDevice and InputDeviceTemplated
[3274]33*/
34
35#ifndef _InputDevice_H__
36#define _InputDevice_H__
37
38#include "InputPrereqs.h"
39
40#include <vector>
41#include <ois/OISInputManager.h>
[7174]42#include <ois/OISException.h>
[3274]43
[5929]44#include "util/Clock.h"
[8858]45#include "util/Output.h"
[5747]46#include "util/Exception.h"
[3274]47#include "InputState.h"
48
49namespace orxonox
50{
51    /**
52    @brief
[3327]53        Abstract base class for all input devices (mouse, keyboard and joy sticks).
54
55        It provides common virtual functions to be used by the InputManager.
[3274]56    */
57    class InputDevice
58    {
59    public:
[3327]60        //! Only resets the members
61        InputDevice(unsigned int id) : bCalibrating_(false), deviceID_(id) { }
[3274]62        virtual ~InputDevice() { }
[3327]63        //! Returns the device class (derived) name as string
64        virtual std::string getClassName() const = 0;
65        //! Updates the device which should in turn distribute events
[3274]66        virtual void update(const Clock& time) = 0;
[3327]67        //! Clear all button related buffers
[3274]68        virtual void clearBuffers() = 0;
69
[3327]70        //! Start calibrating (only useful for joy sticks)
[3274]71        void startCalibration()
72        {
73            bCalibrating_ = true;
74            this->calibrationStarted();
75        }
76
[3327]77        //! Stop calibrating and evaluate the data (only useful for joy sticks)
[3274]78        void stopCalibration()
79        {
80            this->calibrationStopped();
81            bCalibrating_ = false;
82        }
83
[3327]84        //! Returns a reference to the internal input state vector. Use with care!
85        std::vector<InputState*>& getStateListRef() { return this->inputStates_; }
86        //! Returns the ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard)
87        unsigned int getDeviceID() const { return this->deviceID_; }
88        //! Tells whether the device is in calibration mode
89        bool isCalibrating() const { return bCalibrating_; }
[3274]90
91    protected:
[3327]92        //! To be ovrridden by the subclass
[3274]93        virtual void calibrationStarted() { }
[3327]94        //! To be ovrridden by the subclass
[3274]95        virtual void calibrationStopped() { }
96
[3327]97        //! List of all input states that receive events from this device
[3274]98        std::vector<InputState*> inputStates_;
99
100    private:
[3327]101        InputDevice(const InputDevice& rhs); //!< Don't use!
[3274]102
[3327]103        bool bCalibrating_;                  //!< Whether the device is in calibration mode
104        const unsigned int deviceID_;        //!< ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard)
[3274]105    };
106
107    /**
108    @brief
[3327]109        Heavily templated base class for all three input devices.
110
111        The purpose of this class is not to provide an interface but rather
112        to reduce code redundancy. This concerns device creation and destruction
113        as well as common code for button events (press, release, hold).
114
115        In order to derive from this class you have to supply it with a struct
116        as template parameter that contains the necessary type traits.
[3274]117    */
118    template <class Traits>
119    class InputDeviceTemplated : public InputDevice
120    {
121        typedef typename Traits::DeviceClass DeviceClass;
122        typedef typename Traits::OISDeviceClass OISDeviceClass;
123        typedef typename Traits::ButtonType ButtonType;
124        typedef typename Traits::ButtonTypeParam ButtonTypeParam;
125        static const OIS::Type OISDeviceValue = Traits::OISDeviceValue;
126
127    public:
[3327]128        //! Creates the OIS device
129        InputDeviceTemplated(unsigned int id, OIS::InputManager* oisInputManager)
[3274]130            : InputDevice(id)
[3327]131            , oisInputManager_(oisInputManager)
[3274]132        {
[3327]133            oisDevice_ = static_cast<OISDeviceClass*>(oisInputManager_->createInputObject(OISDeviceValue, true));
[7271]134            // Note: after the static_cast here, the cast this pointer becomes
[3327]135            //       invalid right until the subclass has been constructed!
[3274]136            oisDevice_->setEventCallback(static_cast<DeviceClass*>(this));
[8858]137            orxout(verbose, context::input) << "Instantiated a " << this->getClassName() << endl;
[3274]138        }
139
[3327]140        //! Destroys the OIS device
[3274]141        virtual ~InputDeviceTemplated()
142        {
143            try
144            {
[3327]145                oisInputManager_->destroyInputObject(oisDevice_);
[3274]146            }
[7174]147            catch (const OIS::Exception& ex)
[3274]148            {
[8858]149                orxout(internal_error, context::input) << this->getClassName() << " destruction failed: " << ex.eText << '\n'
150                                                       << "Potential resource leak!" << endl;
[3274]151            }
152        }
153
[3327]154        //! Captures OIS events (which then get distributed to the derived class) and creates the button held events
[3274]155        void update(const Clock& time)
156        {
157            oisDevice_->capture();
158
[3327]159            // Call all the states with the held button event
160            for (unsigned int iB = 0; iB < pressedButtons_.size(); ++iB)
161                for (unsigned int iS = 0; iS < inputStates_.size(); ++iS)
[6746]162                    inputStates_[iS]->buttonEvent<ButtonEvent::THold, typename Traits::ButtonTypeParam>(
[3327]163                        this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(pressedButtons_[iB]));
[3274]164
[3327]165            // Call states with device update events
166            for (unsigned int i = 0; i < inputStates_.size(); ++i)
167                inputStates_[i]->update(time.getDeltaTime(), this->getDeviceID());
[3274]168
169            static_cast<DeviceClass*>(this)->updateImpl(time);
170        }
171
[3327]172        //! Clears the list of pressed buttons and calls the derived class's method
[3274]173        void clearBuffers()
174        {
175            pressedButtons_.clear();
176            static_cast<DeviceClass*>(this)->clearBuffersImpl();
177        }
178
[3327]179        // Returns a pointer to the OIS device
180        OISDeviceClass* getOISDevice()   { return this->oisDevice_; }
181        // Returns the name of the derived class as string
182        std::string getClassName() const { return DeviceClass::getClassNameImpl(); }
183
[3274]184    protected:
[3327]185        //! Common code for all button pressed events (updates pressed buttons list and calls the input states)
[8351]186        ORX_FORCEINLINE void buttonPressed(ButtonTypeParam button)
[3274]187        {
188            // check whether the button already is in the list (can happen when focus was lost)
189            unsigned int iButton = 0;
190            while (iButton < pressedButtons_.size() && pressedButtons_[iButton] != button)
191                iButton++;
192            if (iButton == pressedButtons_.size())
193                pressedButtons_.push_back(button);
194            else
195                return; // Button already pressed
196
197            // Call states
198            for (unsigned int i = 0; i < inputStates_.size(); ++i)
[6746]199                inputStates_[i]->buttonEvent<ButtonEvent::TPress, typename Traits::ButtonTypeParam>(this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(button));
[3274]200        }
201
[3327]202        //! Common code for all button released events (updates pressed buttons list and calls the input states)
[8351]203        ORX_FORCEINLINE void buttonReleased(ButtonTypeParam button)
[3274]204        {
205            // remove the button from the pressedButtons_ list
[6417]206            bool found = false;
[3274]207            for (unsigned int iButton = 0; iButton < pressedButtons_.size(); iButton++)
208            {
209                if (pressedButtons_[iButton] == button)
210                {
211                    pressedButtons_.erase(pressedButtons_.begin() + iButton);
[6417]212                    found = true;
[3274]213                    break;
214                }
215            }
[6417]216            if (!found)
217                return; // We ignore release strokes when the press was not captured
[3274]218
219            // Call states
220            for (unsigned int i = 0; i < inputStates_.size(); ++i)
[6746]221                inputStates_[i]->buttonEvent<ButtonEvent::TRelease, typename Traits::ButtonTypeParam>(this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(button));
[3274]222        }
223
[3327]224        //! Managed pointer to the OIS device
[3274]225        OISDeviceClass* oisDevice_;
226
227    private:
228        //!< Fallback dummy function for static polymorphism
[3327]229        void clearBuffersImpl() { }
230        //!< Fallback dummy function for static polymorphism
231        void updateImpl(const Clock& time) { }
232        //!< Fallback dummy function for static polymorphism
[3274]233        ButtonType& getButtonEventArg(ButtonType& button) { return button; }
234
[3327]235        std::vector<ButtonType> pressedButtons_; //!< List of all buttons that are currently pressed down
236        OIS::InputManager* oisInputManager_;     //!< Pointer to the OIS InputManager that can create and destroy devices
[3274]237    };
238}
239
240#endif /* _InputDevice_H__ */
Note: See TracBrowser for help on using the repository browser.