00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General License as published by the Free Software 00012 Foundation either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General License for more details. 00018 00019 You should have received a copy of the GNU Lesser General License along with 00020 this program if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #include "OgreStableHeaders.h" 00026 00027 #include "OgreException.h" 00028 #include "OgreEventMulticaster.h" 00029 00030 00031 namespace Ogre { 00032 00033 EventMulticaster::EventMulticaster(EventListener* a, EventListener* b) 00034 { 00035 mA = a; 00036 mB = b; 00037 } 00038 MouseListener* EventMulticaster::add(MouseListener* a, MouseListener* b) 00039 { 00040 return static_cast<MouseListener*>(addInternal(a, b)); 00041 } 00042 KeyListener* EventMulticaster::add(KeyListener* a, KeyListener* b) 00043 { 00044 return static_cast<KeyListener*>(addInternal(a, b)); 00045 } 00046 MouseMotionListener* EventMulticaster::add(MouseMotionListener* a, MouseMotionListener* b) 00047 { 00048 return static_cast<MouseMotionListener*>(addInternal(a, b)); 00049 } 00050 00051 ActionListener* EventMulticaster::add(ActionListener* a, ActionListener* b) 00052 { 00053 return static_cast<ActionListener*>(addInternal(a, b)); 00054 } 00055 00056 ScrollListener* EventMulticaster::add(ScrollListener* a, ScrollListener* b) 00057 { 00058 return static_cast<ScrollListener*>(addInternal(a, b)); 00059 } 00060 00061 ListSelectionListener* EventMulticaster::add(ListSelectionListener* a, ListSelectionListener* b) 00062 { 00063 return static_cast<ListSelectionListener*>(addInternal(a, b)); 00064 } 00065 00066 EventListener* EventMulticaster::addInternal(EventListener* a, EventListener* b) 00067 { 00068 if (a == NULL) return b; 00069 if (b == NULL) return a; 00070 return convertMultiToListener(new EventMulticaster(a, b)); 00071 } 00072 00073 void EventMulticaster::listSelected(ListSelectionEvent* e) 00074 { 00075 (static_cast<ListSelectionListener*>(mA))->listSelected(e); 00076 (static_cast<ListSelectionListener*>(mB))->listSelected(e); 00077 } 00078 00079 void EventMulticaster::actionPerformed(ActionEvent* e) 00080 { 00081 (static_cast<ActionListener*>(mA))->actionPerformed(e); 00082 (static_cast<ActionListener*>(mB))->actionPerformed(e); 00083 } 00084 00085 void EventMulticaster::scrollPerformed(ScrollEvent* e) 00086 { 00087 (static_cast<ScrollListener*>(mA))->scrollPerformed(e); 00088 (static_cast<ScrollListener*>(mB))->scrollPerformed(e); 00089 } 00090 00091 void EventMulticaster::mouseClicked(MouseEvent* e) 00092 { 00093 (static_cast<MouseListener*>(mA))->mouseClicked(e); 00094 (static_cast<MouseListener*>(mB))->mouseClicked(e); 00095 } 00096 00097 void EventMulticaster::keyClicked(KeyEvent* e) 00098 { 00099 (static_cast<KeyListener*>(mA))->keyClicked(e); 00100 (static_cast<KeyListener*>(mB))->keyClicked(e); 00101 } 00102 00103 void EventMulticaster::mouseEntered(MouseEvent* e) 00104 { 00105 (static_cast<MouseListener*>(mA))->mouseEntered(e); 00106 (static_cast<MouseListener*>(mB))->mouseEntered(e); 00107 } 00108 00109 void EventMulticaster::mouseExited(MouseEvent* e) 00110 { 00111 (static_cast<MouseListener*>(mA))->mouseExited(e); 00112 (static_cast<MouseListener*>(mB))->mouseExited(e); 00113 } 00114 00115 void EventMulticaster::mousePressed(MouseEvent* e) 00116 { 00117 (static_cast<MouseListener*>(mA))->mousePressed(e); 00118 (static_cast<MouseListener*>(mB))->mousePressed(e); 00119 } 00120 00121 void EventMulticaster::mouseReleased(MouseEvent* e) 00122 { 00123 (static_cast<MouseListener*>(mA))->mouseReleased(e); 00124 (static_cast<MouseListener*>(mB))->mouseReleased(e); 00125 } 00126 void EventMulticaster::keyPressed(KeyEvent* e) 00127 { 00128 (static_cast<KeyListener*>(mA))->keyPressed(e); 00129 (static_cast<KeyListener*>(mB))->keyPressed(e); 00130 } 00131 00132 void EventMulticaster::keyReleased(KeyEvent* e) 00133 { 00134 (static_cast<KeyListener*>(mA))->keyReleased(e); 00135 (static_cast<KeyListener*>(mB))->keyReleased(e); 00136 } 00137 00138 ListSelectionListener* EventMulticaster::remove(ListSelectionListener* l, ListSelectionListener* oldl) 00139 { 00140 return static_cast<ListSelectionListener*>( removeInternal(l, oldl)); 00141 } 00142 00143 00144 ActionListener* EventMulticaster::remove(ActionListener* l, ActionListener* oldl) 00145 { 00146 return static_cast<ActionListener*>( removeInternal(l, oldl)); 00147 } 00148 00149 ScrollListener* EventMulticaster::remove(ScrollListener* l, ScrollListener* oldl) 00150 { 00151 return static_cast<ScrollListener*>( removeInternal(l, oldl)); 00152 } 00153 00154 MouseListener* EventMulticaster::remove(MouseListener* l, MouseListener* oldl) 00155 { 00156 return static_cast<MouseListener*> (removeInternal(l, oldl)); 00157 } 00158 KeyListener* EventMulticaster::remove(KeyListener* l, KeyListener* oldl) 00159 { 00160 return static_cast<KeyListener*> (removeInternal(l, oldl)); 00161 } 00162 00163 MouseMotionListener* EventMulticaster::remove(MouseMotionListener* l, MouseMotionListener* oldl) 00164 { 00165 return static_cast<MouseMotionListener*> (removeInternal(l, oldl)); 00166 } 00167 00168 EventListener* EventMulticaster::remove(EventListener* oldl, bool& deleteSelf) 00169 { 00170 if (oldl == mA) 00171 { 00172 delete mA; 00173 deleteSelf = true; // the multicaster is now not needed, so delete this at a higher level 00174 return mB; 00175 } 00176 if (oldl == mB) 00177 { 00178 delete mB; 00179 deleteSelf = true; // the multicaster is now not needed, so delete this at a higher level 00180 return mA; 00181 } 00182 EventListener* a2 = removeInternal(mA, oldl); 00183 EventListener* b2 = removeInternal(mB, oldl); 00184 if (a2 == mA && b2 == mB) 00185 { 00186 return convertMultiToListener(this); // it's not here 00187 } 00188 return addInternal(a2, b2); // the oldl has been removed so join the rest and return it. 00189 } 00190 00191 EventListener* EventMulticaster::removeInternal(EventListener* l, EventListener* oldl) 00192 { 00193 if (l == oldl || l == NULL) 00194 { 00195 return NULL; 00196 } 00197 else if (l->isMulticaster()) 00198 { 00199 bool deleteSelf = false; 00200 EventListener* newl = convertListenerToMulti(l)->remove(oldl, deleteSelf); 00201 00202 if (deleteSelf) 00203 { 00204 delete l; 00205 } 00206 00207 00208 return newl; 00209 } 00210 else 00211 { 00212 return l; // it's not here 00213 } 00214 } 00215 00216 bool EventMulticaster::isMulticaster() const 00217 { 00218 return true; 00219 } 00220 00221 00222 EventListener* EventMulticaster::convertMultiToListener(EventMulticaster* m) 00223 { 00224 // messy, but C++ uses 2 copies of Base class for multiple inheritance with a common parent, 00225 // so ActionListener is used to resolve ambiguity. 00226 // Since Multicaster has many listeners parents, it chooses just one (actionListener arbitary) 00227 // to get to the EventListener parent. 00228 //http://citeseer.nj.nec.com/cache/papers/cs/15957/ 00229 //http:zSzzSzwww.cs.colorado.eduzSz~diwanzSz5535-00zSzmi.pdf/stroustrup99multiple.pdf 00230 return static_cast <ActionListener*> (m); 00231 } 00232 00233 EventMulticaster* EventMulticaster::convertListenerToMulti(EventListener* l) 00234 { 00235 // messy, but C++ uses 2 copies of Base class for multiple inheritance with a common parent, 00236 // so ActionListener is used to resolve ambiguity. 00237 // Since Multicaster has many listeners parents, it chooses just one (actionListener arbitary) 00238 // to get to the EventListener parent. 00239 //http://citeseer.nj.nec.com/cache/papers/cs/15957/ 00240 //http:zSzzSzwww.cs.colorado.eduzSz~diwanzSz5535-00zSzmi.pdf/stroustrup99multiple.pdf 00241 if (!l->isMulticaster()) 00242 { 00243 Except(Exception::ERR_INVALIDPARAMS, "Illegal convertion, listener is not a multicaster" 00244 , "EventMulticaster::convertListenerToMulti"); 00245 00246 } 00247 return (static_cast <EventMulticaster*>(static_cast <ActionListener*> (l))); 00248 } 00249 } 00250
Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:10 2004