00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (C) 2010 Dan Leinir Turthra Jensen <admin@leinir.dk> 00004 * Copyright (c) 2010 Arjen Hiemstra <ahiemstra@heimr.nl> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library 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 GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include "mouseinputcomponent.h" 00022 #include "input/inputmanager.h" 00023 00024 #include <core/debughelper.h> 00025 00026 #include <QtCore/QEvent> 00027 #include <QtCore/QDebug> 00028 00029 REGISTER_OBJECTTYPE( GluonEngine, MouseInputComponent ); 00030 00031 using namespace GluonEngine; 00032 00033 class MouseInputComponent::MouseInputComponentPrivate 00034 { 00035 public: 00036 bool actionHeld; 00037 bool actionStarted; 00038 bool actionStopped; 00039 00040 MouseButton mouseButton; 00041 00042 GluonInput::Mouse* mouse; 00043 00044 int lastX; 00045 int lastY; 00046 int lastZ; 00047 00048 static const int mouseButtonOffset; 00049 }; 00050 00051 const int MouseInputComponent::MouseInputComponentPrivate::mouseButtonOffset = 271; 00052 00053 MouseInputComponent::MouseInputComponent( QObject* parent ) 00054 : Component( parent ) 00055 , d( new MouseInputComponentPrivate ) 00056 { 00057 d->actionHeld = false; 00058 d->actionStarted = false; 00059 d->actionStopped = false; 00060 00061 d->mouseButton = MouseInputComponent::MOUSE_BUTTON_UNKNOWN; 00062 00063 d->mouse = 0; 00064 } 00065 00066 QString 00067 MouseInputComponent::category() const 00068 { 00069 return QString( "Input" ); 00070 } 00071 00072 void MouseInputComponent::initialize() 00073 { 00074 if( !d->mouse ) 00075 { 00076 d->mouse = GluonInput::InputManager::instance()->mouse(); 00077 } 00078 } 00079 00080 void 00081 MouseInputComponent::start() 00082 { 00083 if( d->mouse ) 00084 { 00085 d->mouse->setEnabled( true ); 00086 } 00087 else 00088 { 00089 debug( "WARNING! No mouse found!" ); 00090 } 00091 } 00092 00093 void 00094 MouseInputComponent::update( int elapsedMilliseconds ) 00095 { 00096 DEBUG_BLOCK 00097 if( d->actionStarted ) 00098 d->actionStarted = false; 00099 00100 if( d->actionStopped ) 00101 d->actionStopped = false; 00102 00103 if( d->mouse && d->mouseButton && d->mouse->buttonPressed( d->mouseButton + d->mouseButtonOffset ) ) 00104 { 00105 if( !d->actionHeld ) 00106 { 00107 d->actionStarted = true; 00108 d->actionHeld = true; 00109 } 00110 } 00111 else 00112 { 00113 if( d->actionHeld ) 00114 { 00115 d->actionStopped = true; 00116 d->actionHeld = false; 00117 } 00118 } 00119 00120 d->lastX = d->mouse->position().x(); 00121 d->lastY = d->mouse->position().y(); 00122 d->lastZ = d->mouse->wheelPosition(); 00123 } 00124 00125 void MouseInputComponent::stop() 00126 { 00127 if( d->mouse ) 00128 { 00129 d->mouse->setEnabled( false ); 00130 } 00131 00132 d->actionStopped = false; 00133 d->actionStarted = false; 00134 d->actionHeld = false; 00135 } 00136 00137 bool 00138 MouseInputComponent::isActionStarted() 00139 { 00140 return d->actionStarted; 00141 } 00142 00143 bool 00144 MouseInputComponent::isActionHeld() 00145 { 00146 return d->actionHeld; 00147 } 00148 00149 bool 00150 MouseInputComponent::isActionStopped() 00151 { 00152 return d->actionStopped; 00153 } 00154 00155 MouseInputComponent::MouseButton MouseInputComponent::mouseButton() const 00156 { 00157 return d->mouseButton; 00158 } 00159 00160 void MouseInputComponent::setMouseButton( MouseInputComponent::MouseButton button ) 00161 { 00162 d->mouseButton = button; 00163 } 00164 00165 int MouseInputComponent::relativeXAxis() 00166 { 00167 return d->lastX - d->mouse->position().x(); 00168 } 00169 00170 int MouseInputComponent::relativeYAxis() 00171 { 00172 return d->lastY - d->mouse->position().y(); 00173 } 00174 00175 int MouseInputComponent::relativeZAxis() 00176 { 00177 return d->lastZ - d->mouse->wheelPosition(); 00178 } 00179 00180 int MouseInputComponent::xAxis() 00181 { 00182 return d->mouse->position().x(); 00183 } 00184 00185 int MouseInputComponent::yAxis() 00186 { 00187 return d->mouse->position().y(); 00188 } 00189 00190 int MouseInputComponent::zAxis() 00191 { 00192 return d->mouse->wheelPosition(); 00193 } 00194 00195 00196 Q_EXPORT_PLUGIN2( gluon_component_mouseinput, GluonEngine::MouseInputComponent ); 00197 00198 #include "mouseinputcomponent.moc"