![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cGuiInteractive.cppGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2003 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #include "cGuiInteractive.h" 00026 00027 #include "n2l/dynVars.h" 00028 #include "n2l/resourceManagement.h" 00029 00030 namespace n2l 00031 { 00032 00033 /**************************************************************************/ 00034 cGuiInteractive::cGuiInteractive() : 00035 mmMouseIsOver(false), 00036 mmLastClickButton( MouseButton_Unknown ) 00037 { 00038 } 00039 00040 /**************************************************************************/ 00041 cGuiInteractive::cGuiInteractive( const cDynVar &iDef ) : 00042 mmMouseIsOver(false), 00043 mmLastClickButton( MouseButton_Unknown ) 00044 { 00045 load( iDef ); 00046 } 00047 00048 /**************************************************************************/ 00049 cGuiInteractive::~cGuiInteractive() 00050 { 00051 } 00052 00053 /**************************************************************************/ 00054 void cGuiInteractive::load( const cDynVar &iDef ) 00055 { 00056 cGuiElement::load( iDef ); 00057 } 00058 00059 /**************************************************************************/ 00060 void cGuiInteractive::systemEvent( const cAutoPtr<const cEventInterface> & i_iEvent ) 00061 { 00062 // In this class we can be lazy and call the original methods 00063 // that used clickAt an mouseMove. The events are so close that 00064 // they're almost instantly source compatible. 00065 switch (i_iEvent->type()) 00066 { 00067 case EventType_MouseMotion: 00068 mouseMove( cAutoPtr<const cMouseMotionEvent>(i_iEvent)->pos() ); 00069 break; 00070 00071 case EventType_MouseButton: 00072 mouseClickAt( *(cAutoPtr<const cMouseButtonEvent>(i_iEvent)) ); 00073 break; 00074 00075 case EventType_Key: 00076 keystroke( *(cAutoPtr<const cKeyEvent>(i_iEvent)) ); 00077 break; 00078 00079 // Nothing to do 00080 default: 00081 break; 00082 00083 } // switch 00084 } 00085 00086 /**************************************************************************/ 00087 void cGuiInteractive::systemEventOutside( const cAutoPtr<const cEventInterface> & i_iEvent ) 00088 { 00089 // In this class we can be lazy and call the original methods 00090 // that used clickAt an mouseMove. The events are so close that 00091 // they're almost instantly source compatible. 00092 switch (i_iEvent->type()) 00093 { 00094 case EventType_MouseMotion: 00095 mouseMoveOutside( cAutoPtr<const cMouseMotionEvent>(i_iEvent)->pos() ); 00096 break; 00097 00098 case EventType_MouseButton: 00099 mouseClickOutside( *(cAutoPtr<const cMouseButtonEvent>(i_iEvent)) ); 00100 break; 00101 00102 // Nothing to do 00103 default: 00104 break; 00105 00106 } // switch 00107 } 00108 00109 /**************************************************************************/ 00110 const tBool cGuiInteractive::focus() const 00111 { 00112 return cGuiElement::focus(); 00113 } 00114 00115 /**************************************************************************/ 00116 void cGuiInteractive::focus( const tBool iFocus ) 00117 { 00118 if (cGuiElement::focus()==iFocus) { 00119 // Sending this along is a might pointless since we already 00120 // know that the focus didn't change. BUT! There's nothing 00121 // to say that our parent doesn't want redundent 00122 // focus calls anyway. So we'll do it anyway. 00123 cGuiElement::focus(iFocus); 00124 return; 00125 } 00126 cGuiElement::focus(iFocus); 00127 tActionData actionData( tActionData::Array ); 00128 actionData.insert("focus",iFocus); 00129 addExtraFocusData(actionData); 00130 broadcastAction( GuiActionType_FocusChanged, actionData ); 00131 } 00132 00133 /**************************************************************************/ 00134 void cGuiInteractive::mouseClickAt( const cMouseButtonEvent &iClick ) 00135 { 00136 if (collides(iClick.pos())) { 00137 if (iClick.pressed()) { 00138 // Button down. 00139 if (mmLastClickButton == MouseButton_Unknown) { 00140 mmLastClickButton = iClick.button(); 00141 tActionData actionData( tActionData::Array ); 00142 actionData.insert("x",iClick.pos().x()); 00143 actionData.insert("y",iClick.pos().y()); 00144 actionData.insert("button",iClick.button()); 00145 addExtraPressedData(actionData); 00146 broadcastAction( GuiActionType_Pressed, actionData ); 00147 } 00148 } else { 00149 // Mouse button up. 00150 if (iClick.button()==mmLastClickButton) { 00151 tActionData actionData( tActionData::Array ); 00152 actionData.insert("x",iClick.pos().x()); 00153 actionData.insert("y",iClick.pos().y()); 00154 actionData.insert("button",iClick.button()); 00155 mmLastClickButton = MouseButton_Unknown; 00156 tActionData releasedData( actionData ); 00157 addExtraReleasedData(releasedData); 00158 broadcastAction( GuiActionType_Released, releasedData ); 00159 addExtraClickedData(actionData); 00160 broadcastAction( GuiActionType_Clicked, actionData ); 00161 } 00162 } 00163 } else { // of if collides 00164 if (iClick.pressed()) { 00165 // Mouse button down -- who cares? 00166 00167 } else { 00168 // Mouse button up 00169 if (iClick.button()==mmLastClickButton) { 00170 tActionData actionData( tActionData::Array ); 00171 actionData.insert("x",iClick.pos().x()); 00172 actionData.insert("y",iClick.pos().y()); 00173 actionData.insert("button",iClick.button()); 00174 addExtraReleasedData(actionData); 00175 mmLastClickButton = MouseButton_Unknown; 00176 broadcastAction( GuiActionType_Released, actionData ); 00177 } 00178 } 00179 } 00180 00181 } // void cGuiInteractive::mouseClickAt 00182 00183 00184 /**************************************************************************/ 00185 void cGuiInteractive::mouseClickOutside( const cMouseButtonEvent & iClick ) 00186 { 00187 if (iClick.button()==mmLastClickButton) { 00188 tActionData actionData( tActionData::Array ); 00189 actionData.insert("x",iClick.pos().x()); 00190 actionData.insert("y",iClick.pos().y()); 00191 actionData.insert("button",iClick.button()); 00192 addExtraReleasedData(actionData); 00193 mmLastClickButton = MouseButton_Unknown; 00194 broadcastAction( GuiActionType_Released, actionData ); 00195 } 00196 } // void cGuiInteractive::mouseClickOutside 00197 00198 00199 /**************************************************************************/ 00200 void cGuiInteractive::mouseMove( const tGuiPos & iPosition ) 00201 { 00202 if (collides(iPosition)) { 00203 if (!mmMouseIsOver) { 00204 tActionData overData( tActionData::Array ); 00205 overData.insert("x",iPosition.x()); 00206 overData.insert("y",iPosition.y()); 00207 addExtraOverData(overData); 00208 mmMouseIsOver = true; 00209 broadcastAction( GuiActionType_Over, overData ); 00210 } 00211 } else { 00212 if (mmMouseIsOver) { 00213 tActionData outData( tActionData::Array ); 00214 outData.insert("x",iPosition.x()); 00215 outData.insert("y",iPosition.y()); 00216 addExtraOutData(outData); 00217 mmMouseIsOver = false; 00218 broadcastAction( GuiActionType_Out, outData ); 00219 } 00220 } 00221 } 00222 00223 /**************************************************************************/ 00224 void cGuiInteractive::keystroke( const cKeyEvent &iEvent ) 00225 { 00226 if (cGuiElement::focus()) { 00227 tActionData data( tActionData::Array ); 00228 data.insert("key", tSint(iEvent.key()) ); 00229 data.insert("mods",tSint(iEvent.mods()) ); 00230 data.insert("pressed",iEvent.pressed() ); 00231 addExtraKeyData(data); 00232 broadcastAction( GuiActionType_Keystroke, data ); 00233 } else { 00234 tActionData data( tActionData::Array ); 00235 data.insert("key", tSint(iEvent.key()) ); 00236 data.insert("mods",tSint(iEvent.mods()) ); 00237 data.insert("pressed",iEvent.pressed() ); 00238 addExtraKeyData(data); 00239 broadcastAction( GuiActionType_KeystrokeOutside, data ); 00240 } 00241 } 00242 00243 /**************************************************************************/ 00244 void cGuiInteractive::mouseMoveOutside( const tGuiPos & iPosition ) 00245 { 00246 if (mmMouseIsOver) { 00247 tActionData outData( tActionData::Array ); 00248 outData.insert("x",iPosition.x()); 00249 outData.insert("y",iPosition.y()); 00250 addExtraOutData(outData); 00251 mmMouseIsOver = false; 00252 broadcastAction( GuiActionType_Out, outData ); 00253 } 00254 } 00255 00256 00257 /**************************************************************************/ 00258 const cAutoPtr<cGuiElement> cGuiInteractive::clone() const 00259 { 00260 cAutoPtr<cGuiElement> newElement( new cGuiInteractive ); 00261 cloneInto( newElement ); 00262 return newElement; 00263 } 00264 00265 /**************************************************************************/ 00266 void cGuiInteractive::cloneInto( 00267 const cAutoPtr<cGuiInteractive> &i_ioElement ) const 00268 { 00269 // Parent Properties first 00270 cGuiElement::cloneInto( i_ioElement ); 00271 00272 // Now our properties. 00273 00274 } 00275 00276 00277 /**************************************************************************/ 00278 const tBool cGuiInteractive::collides( const tGuiPos & iPosition ) const 00279 { 00280 return (iPosition.x()>=mPos.x() && iPosition.x()<mPos.x()+mSize.x() && 00281 iPosition.y()>=mPos.y() && iPosition.y()<mPos.y()+mSize.y()); 00282 } 00283 00284 /**************************************************************************/ 00285 void cGuiInteractive::addExtraOverData( tActionData & oData ) 00286 { 00287 oData.insert("name",mName); 00288 } 00289 00290 /**************************************************************************/ 00291 void cGuiInteractive::addExtraOutData( tActionData & oData ) 00292 { 00293 oData.insert("name",mName); 00294 } 00295 00296 /**************************************************************************/ 00297 void cGuiInteractive::addExtraPressedData( tActionData & oData ) 00298 { 00299 oData.insert("name",mName); 00300 } 00301 00302 /**************************************************************************/ 00303 void cGuiInteractive::addExtraReleasedData( tActionData & oData ) 00304 { 00305 oData.insert("name",mName); 00306 } 00307 00308 /**************************************************************************/ 00309 void cGuiInteractive::addExtraClickedData( tActionData & oData ) 00310 { 00311 oData.insert("name",mName); 00312 } 00313 00314 /**************************************************************************/ 00315 void cGuiInteractive::addExtraKeyData( tActionData & oData ) 00316 { 00317 oData.insert("name",mName); 00318 } 00319 00320 /**************************************************************************/ 00321 void cGuiInteractive::addExtraFocusData( tActionData & oData ) 00322 { 00323 oData.insert("name",mName); 00324 } 00325 00326 } // namespace n2l |