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 "keyboardinputcomponent.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, KeyboardInputComponent ); 00030 00031 using namespace GluonEngine; 00032 00033 KeyboardInputComponent::KeyboardInputComponent( QObject* parent ) 00034 : Component( parent ) 00035 , m_actionHeld( false ) 00036 , m_actionStarted( false ) 00037 , m_actionStopped( false ) 00038 , m_keyCode( KeyboardInputComponent::UNKNOWN ) 00039 , m_keyboard( 0 ) 00040 { 00041 } 00042 00043 QString 00044 KeyboardInputComponent::category() const 00045 { 00046 return QString( "Input" ); 00047 } 00048 00049 void KeyboardInputComponent::initialize() 00050 { 00051 if( !m_keyboard ) 00052 m_keyboard = GluonInput::InputManager::instance()->keyboard(); 00053 } 00054 00055 void 00056 KeyboardInputComponent::start() 00057 { 00058 if( m_keyboard ) 00059 { 00060 m_keyboard->setEnabled( true ); 00061 } 00062 else 00063 { 00064 debug( "WARNING! No keyboard found!" ); 00065 } 00066 } 00067 00068 void 00069 KeyboardInputComponent::update( int elapsedMilliseconds ) 00070 { 00071 DEBUG_BLOCK 00072 if( m_actionStarted ) 00073 m_actionStarted = false; 00074 00075 if( m_actionStopped ) 00076 m_actionStopped = false; 00077 00078 if( m_keyboard && m_keyboard->buttonPressed( m_keyCode ) ) 00079 { 00080 if( !m_actionHeld ) 00081 { 00082 m_actionStarted = true; 00083 m_actionHeld = true; 00084 } 00085 } 00086 else 00087 { 00088 if( m_actionHeld ) 00089 { 00090 m_actionStopped = true; 00091 m_actionHeld = false; 00092 } 00093 } 00094 } 00095 00096 void KeyboardInputComponent::stop() 00097 { 00098 if( m_keyboard ) 00099 { 00100 m_keyboard->setEnabled( false ); 00101 } 00102 00103 m_actionStopped = false; 00104 m_actionStarted = false; 00105 m_actionHeld = false; 00106 } 00107 00108 bool 00109 KeyboardInputComponent::isActionStarted() 00110 { 00111 return m_actionStarted; 00112 } 00113 00114 bool 00115 KeyboardInputComponent::isActionHeld() 00116 { 00117 return m_actionHeld; 00118 } 00119 00120 bool 00121 KeyboardInputComponent::isActionStopped() 00122 { 00123 return m_actionStopped; 00124 } 00125 00126 KeyboardInputComponent::KeyName 00127 KeyboardInputComponent::keyCode() const 00128 { 00129 return m_keyCode; 00130 } 00131 00132 void 00133 KeyboardInputComponent::setKeyCode( KeyboardInputComponent::KeyName newKeyCode ) 00134 { 00135 m_keyCode = newKeyCode; 00136 } 00137 00138 Q_EXPORT_PLUGIN2( gluon_component_keyboardinput, GluonEngine::KeyboardInputComponent ); 00139 00140 #include "keyboardinputcomponent.moc"