Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

inputcontrol.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002    this file is part of rcssserver3D
00003    Fri May 9 2003
00004    Copyright (C) 2003 Koblenz University
00005    $Id: inputcontrol.cpp,v 1.4 2004/12/31 11:02:13 rollmark Exp $
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; version 2 of the License.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 #include "inputcontrol.h"
00021 #include "inputitem.h"
00022 #include <oxygen/simulationserver/simulationserver.h>
00023 #include <oxygen/sceneserver/fpscontroller.h>
00024 #include <zeitgeist/logserver/logserver.h>
00025 #include <zeitgeist/scriptserver/scriptserver.h>
00026 
00027 using namespace kerosin;
00028 using namespace oxygen;
00029 using namespace zeitgeist;
00030 using namespace boost;
00031 using namespace std;
00032 
00033 InputControl::InputControl()
00034 {
00035     mDeltaTime = 0;
00036     mHorSens = 0.3;
00037     mVertSens = 0.3;
00038     mAdvanceTime = true;
00039     mMouseLook = false;
00040 }
00041 
00042 InputControl::~InputControl()
00043 {
00044 }
00045 
00046 bool InputControl::SetFPSController(const std::string& path)
00047 {
00048     if (path.empty())
00049         {
00050             mFPSController.reset();
00051             return true;
00052         }
00053 
00054     shared_ptr<Leaf> leaf = GetCore()->Get(path);
00055 
00056     if (leaf.get() == 0)
00057         {
00058             GetLog()->Error()
00059                 << "(InputControl) ERROR: invalid path "
00060                 << path << "'\n";
00061             return false;
00062         }
00063 
00064     mFPSController = shared_dynamic_cast<FPSController>
00065         (GetCore()->Get(path));
00066 
00067     if (mFPSController.get() == 0)
00068         {
00069             // the path is valid but doesn't point to an FPSController;
00070             // for convenience search below for a controller
00071             mFPSController  =
00072                 leaf->FindChildSupportingClass<FPSController>(true);
00073         }
00074 
00075     if (mFPSController.get() == 0)
00076         {
00077             GetLog()->Error()
00078                 << "(InputControl) ERROR: no FPSController found at '"
00079                 << path << "'\n";
00080             return false;
00081         }
00082 
00083     return true;
00084 }
00085 
00086 void InputControl::SetFPSController(shared_ptr<FPSController> controller)
00087 {
00088     mFPSController = controller;
00089 }
00090 
00091 void InputControl::OnLink()
00092 {
00093     shared_ptr<ScriptServer> scriptServer = GetCore()->GetScriptServer();
00094 
00095     // publish common command constants to the scripts
00096     scriptServer->CreateVariable("Command.Quit",     CmdQuit);
00097     scriptServer->CreateVariable("Command.Timer",    CmdTimer);
00098     scriptServer->CreateVariable("Command.MouseX",   CmdMouseX);
00099     scriptServer->CreateVariable("Command.MouseY",   CmdMouseY);
00100     scriptServer->CreateVariable("Command.Left",     CmdLeft);
00101     scriptServer->CreateVariable("Command.Right",    CmdRight);
00102     scriptServer->CreateVariable("Command.Forward",  CmdForward);
00103     scriptServer->CreateVariable("Command.Backward", CmdBackward);
00104     scriptServer->CreateVariable("Command.Up",       CmdUp);
00105     scriptServer->CreateVariable("Command.Down",     CmdDown);
00106     scriptServer->CreateVariable("Command.Mouselook", CmdMouseLook);
00107 
00108     mInputServer = shared_dynamic_cast<InputServer>
00109         (GetCore()->Get("/sys/server/input"));
00110 
00111     if (mInputServer.get() == 0)
00112         {
00113             GetLog()->Error()
00114                 << "(InputControl) ERROR: InputServer not found\n";
00115         }
00116 }
00117 
00118 void InputControl::OnUnlink()
00119 {
00120     // we have to make sure, the inputServer is shut down before the
00121     // opengl server, as the opengl server shuts down SDL ... this
00122     // will conflict with the input server
00123     mInputServer->Unlink();
00124     mInputServer.reset();
00125     mFPSController.reset();
00126 }
00127 
00128 void InputControl::SetHorizontalSensitivity(float s)
00129 {
00130     mHorSens = s;
00131 }
00132 
00133 void InputControl::SetVerticalSensitivity(float s)
00134 {
00135     mVertSens = s;
00136 }
00137 
00138 float InputControl::GetHorizontalSensitivity()
00139 {
00140     return mHorSens;
00141 }
00142 
00143 float InputControl::GetVerticalSensitivity()
00144 {
00145     return mVertSens;
00146 }
00147 
00148 void InputControl::InitSimulation()
00149 {
00150     if (mAdvanceTime)
00151         {
00152             // this node will step the simulation time
00153             GetSimulationServer()->SetAutoTimeMode(false);
00154         }
00155 }
00156 
00157 void InputControl::StartCycle()
00158 {
00159     // Process incoming input
00160     mDeltaTime = 0.0f;
00161     static InputServer::Input input;
00162 
00163     while (mInputServer->GetInput(input))
00164         {
00165             switch (input.id)
00166                 {
00167                 case CmdQuit:
00168                     GetSimulationServer()->Quit();
00169                     break;
00170 
00171                 case CmdMouseLook:
00172                     mMouseLook = (input.data.l == 1);
00173                     break;
00174 
00175                 case CmdTimer:
00176                     mDeltaTime = (float) input.data.l/1000.0f;
00177                     break;
00178 
00179                 case CmdMouseX:
00180                     if (
00181                         (mMouseLook) &&
00182                         (mFPSController.get() != 0)
00183                         )
00184                         {
00185                             mFPSController->AdjustHAngle(mHorSens*(float)input.data.l);
00186                         }
00187                     break;
00188 
00189                 case CmdMouseY:
00190                     if (
00191                         (mMouseLook) &&
00192                         (mFPSController.get() != 0)
00193                         )
00194                         {
00195                             mFPSController->AdjustVAngle(mVertSens*(float)input.data.l);
00196                         }
00197                     break;
00198 
00199                 case CmdUp:
00200                     if (mFPSController.get() != 0)
00201                         {
00202                             mFPSController->Up(input.data.l!=0);
00203                         }
00204                     break;
00205 
00206                 case CmdDown:
00207                     if (mFPSController.get() != 0)
00208                         {
00209                             mFPSController->Down(input.data.l!=0);
00210                         }
00211                     break;
00212 
00213                 case CmdLeft:
00214                     if (mFPSController.get() != 0)
00215                         {
00216                             mFPSController->StrafeLeft(input.data.l!=0);
00217                         }
00218                     break;
00219 
00220                 case CmdRight:
00221                     if (mFPSController.get() != 0)
00222                         {
00223                             mFPSController->StrafeRight(input.data.l!=0);
00224                         }
00225                     break;
00226 
00227                 case CmdForward:
00228                     if (mFPSController.get() != 0)
00229                         {
00230                             mFPSController->Forward(input.data.l!=0);
00231                         }
00232                     break;
00233 
00234                 case CmdBackward:
00235                     if (mFPSController.get() != 0)
00236                         {
00237                             mFPSController->Backward(input.data.l!=0);
00238                         }
00239                     break;
00240 
00241                 default:
00242                     // pass unknown events on to the registered InputItems
00243                     TLeafList items;
00244                     ListChildrenSupportingClass<InputItem>(items);
00245 
00246                     for (
00247                          TLeafList::iterator iter = items.begin();
00248                          iter != items.end();
00249                          ++iter
00250                          )
00251                         {
00252                             shared_static_cast<InputItem>(*iter)
00253                                 ->ProcessInput(input);
00254                         }
00255                     break;
00256                 }
00257         }
00258 
00259     if (mAdvanceTime)
00260         {
00261             // pass the delta time on to the SimulationServer
00262             GetSimulationServer()->AdvanceTime(mDeltaTime);
00263         }
00264 }
00265 
00266 float InputControl::GetDeltaTime()
00267 {
00268     return mDeltaTime;
00269 }
00270 
00271 void InputControl::SetAdvanceTime(bool advance)
00272 {
00273     mAdvanceTime = advance;
00274 }
00275 
00276 bool InputControl::GetAdvanceTime()
00277 {
00278     return mAdvanceTime;
00279 }
00280 
00281 bool InputControl::RegisterInputItem(const string& inputItemName, const string& name)
00282 {
00283     // check if a input item of the requested type was already created
00284     shared_ptr<InputItem> inputItem =
00285         shared_dynamic_cast<InputItem>(GetChildOfClass(inputItemName));
00286 
00287     if (inputItem.get() != 0)
00288     {
00289         return true;
00290     }
00291 
00292     // create the input item
00293     inputItem = shared_dynamic_cast<InputItem>(GetCore()->New(inputItemName));
00294 
00295     if (inputItem.get() == 0)
00296     {
00297         GetLog()->Error() << "ERROR: (InputControl) Cannot create input item '"
00298                           << inputItemName << "'" << std::endl;
00299         return false;
00300     }
00301 
00302     // link the input item in the hierarchy
00303     inputItem->SetName(name);
00304 
00305     if (! AddChildReference(inputItem))
00306         {
00307             GetLog()->Error() << "ERROR: (InputControl) Cannot link the input item '"
00308                               << inputItemName << "' to the hierarchy\n";
00309             return false;
00310         }
00311 
00312     GetLog()->Debug() << "(InputControl) Registered input item '"
00313                       << inputItemName << "'\n";
00314 
00315     return true;
00316 }

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4