Changeset 1672 for code/branches/gui/src/core
- Timestamp:
- Aug 27, 2008, 10:21:39 PM (16 years ago)
- Location:
- code/branches/gui/src/core
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/CMakeLists.txt
r1665 r1672 10 10 OutputBuffer.cc 11 11 OutputHandler.cc 12 RootGameState.cc 12 13 Script.cc 13 14 SignalHandler.cc -
code/branches/gui/src/core/CorePrereqs.h
r1663 r1672 161 161 class XMLPortParamContainer; 162 162 163 // game states 164 class BaseGameState; 165 class GameState; 166 class RootGameState; 167 163 168 // input 164 169 class BaseCommand; -
code/branches/gui/src/core/GameState.cc
r1670 r1672 46 46 GameState::GameState(const std::string& name) 47 47 : name_(name) 48 , bPauseParent_(false)49 //, bActive_(false)50 //, bSuspended_(false)51 //, bRunning_(false)52 , scheduledTransition_(0)53 48 , parent_(0) 54 49 , activeChild_(0) 50 //, bPauseParent_(false) 55 51 { 56 52 Operations temp = {false, false, false, false, false}; … … 64 60 GameState::~GameState() 65 61 { 66 if (this->operation_.active) 67 { 68 if (this->parent_) 69 this->requestState(this->parent_->getName()); 70 else 71 this->requestState(""); 72 } 62 OrxAssert(!isInSubtree(getCurrentState()), "Deleting an active GameState is a very bad idea.."); 73 63 } 74 64 … … 88 78 it != state->allChildren_.end(); ++it) 89 79 { 90 if (this-> checkState(it->second->getName()))80 if (this->getState(it->second->getName())) 91 81 { 92 82 ThrowException(GameState, "Cannot add a GameState to the hierarchy twice."); … … 94 84 } 95 85 } 96 if (this-> checkState(state->name_))86 if (this->getState(state->name_)) 97 87 { 98 88 ThrowException(GameState, "Cannot add a GameState to the hierarchy twice."); … … 109 99 for (std::map<std::string, GameState*>::const_iterator it = state->allChildren_.begin(); 110 100 it != state->allChildren_.end(); ++it) 111 { 112 this->allChildren_[it->second->getName()] = it->second; 113 this->grandchildrenToChildren_[it->second] = state; 114 if (this->parent_) 115 this->parent_->grandchildAdded(this, it->second); 116 } 101 this->grandchildAdded(state, it->second); 117 102 // merge 'state' into this tree 118 this->allChildren_[state->name_] = state; 119 this->grandchildrenToChildren_[state] = state; 120 if (this->parent_) 121 this->parent_->grandchildAdded(this, state); 103 this->grandchildAdded(state, state); 122 104 123 105 // mark us as parent … … 137 119 if (it != this->grandchildrenToChildren_.end()) 138 120 { 139 if (state-> getOperation().active)121 if (state->isInSubtree(getCurrentState())) 140 122 { 141 ThrowException(GameState, "Cannot remove a ctive game state child '"123 ThrowException(GameState, "Cannot remove an active game state child '" 142 124 + state->getName() + "' from '" + name_ + "'."); 143 //COUT(2) << "Warning: Cannot remove a ctive game state child '" << state->getName()125 //COUT(2) << "Warning: Cannot remove an active game state child '" << state->getName() 144 126 // << "' from '" << name_ << "'." << std::endl; 145 127 } … … 148 130 for (std::map<GameState*, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin(); 149 131 it != state->grandchildrenToChildren_.end(); ++it) 150 {151 132 this->grandchildRemoved(it->first); 152 }153 133 this->grandchildRemoved(state); 154 134 } … … 157 137 { 158 138 ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '" 159 + state->getName() + "'. Removal skipped.");139 + state->getName() + "'."); 160 140 //COUT(2) << "Warning: Game state '" << name_ << "' doesn't have a child named '" 161 141 // << state->getName() << "'. Removal skipped." << std::endl; … … 173 153 void GameState::removeChild(const std::string& name) 174 154 { 175 GameState* state = checkState(name);155 GameState* state = getState(name); 176 156 if (state) 177 157 { … … 194 174 The child that has been added. 195 175 */ 196 void GameState::grandchildAdded(GameState* child, GameState* grandchild)176 inline void GameState::grandchildAdded(GameState* child, GameState* grandchild) 197 177 { 198 178 // fill the two maps correctly. … … 212 192 The child that has been removed. 213 193 */ 214 void GameState::grandchildRemoved(GameState* grandchild)194 inline void GameState::grandchildRemoved(GameState* grandchild) 215 195 { 216 196 // adjust the two maps correctly. … … 227 207 Remember that the every node has a map with all its child nodes. 228 208 */ 229 GameState* GameState:: checkState(const std::string& name)209 GameState* GameState::getState(const std::string& name) 230 210 { 231 211 if (this->parent_) 232 return this->parent_-> checkState(name);212 return this->parent_->getState(name); 233 213 else 234 214 { … … 244 224 /** 245 225 @brief 226 Returns the root node of the tree. 227 */ 228 GameState* GameState::getRoot() 229 { 230 if (this->parent_) 231 return this->parent_->getRoot(); 232 else 233 return this; 234 } 235 236 /** 237 @brief 246 238 Returns the current active state. 247 239 @remarks … … 260 252 else 261 253 { 262 if (this-> parent_)263 return this-> parent_->getCurrentState();254 if (this->getParent()) 255 return this->getParent()->getCurrentState(); 264 256 else 265 257 return 0; … … 269 261 /** 270 262 @brief 271 Returns the root node of the tree. 272 */ 273 GameState* GameState::getRootNode() 274 { 275 if (this->parent_) 276 return this->parent_->getRootNode(); 277 else 278 return this; 263 Determines whether 'state' is in this subtree, including this node. 264 */ 265 bool GameState::isInSubtree(GameState* state) const 266 { 267 return (grandchildrenToChildren_.find(state) != grandchildrenToChildren_.end() 268 || state == this); 279 269 } 280 270 … … 288 278 void GameState::requestState(const std::string& name) 289 279 { 290 GameState* current = getCurrentState(); 291 if (current != 0 && (current->getOperation().entering || current->getOperation().leaving)) 292 { 293 ThrowException(GameState, "Making state transitions during enter()/leave() is forbidden."); 294 } 295 //if (name == "") 296 //{ 297 // // user would like to leave every state. 298 // if (current) 299 // { 300 // // Deactivate all states but root 301 // GameState* root = getRootNode(); 302 // current->makeTransition(root); 303 // //// Kick root too 304 // //assert(!(root->getOperation().entering || root->getOperation().leaving)); 305 // //if (root->operation_.running) 306 // // root->scheduledTransition_ = 0; 307 // //else 308 // // root->deactivate(); 309 // } 310 //} 311 else 312 { 313 GameState* request = checkState(name); 314 if (request) 315 { 316 if (current) 317 { 318 // There is already an active state 319 current->makeTransition(request); 320 } 321 else 322 { 323 // no active state --> we have to activate the root node first. 324 GameState* root = getRootNode(); 325 root->activate(); 326 root->makeTransition(request); 327 } 328 } 329 else 330 { 331 COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; 332 } 333 } 280 assert(getRoot()); 281 getRoot()->requestState(name); 334 282 } 335 283 … … 339 287 the method can assume certain things to be granted (like 'this' is always active). 340 288 */ 341 void GameState::makeTransition(GameState* state) 342 { 343 // we're always already active 344 assert(this->operation_.active); 345 346 if (state == this) 289 void GameState::makeTransition(GameState* source, GameState* destination) 290 { 291 if (source == this->getParent()) 292 { 293 // call is from the parent 294 this->activate(); 295 } 296 else if (source == 0) 297 { 298 // call was just started by root 299 // don't do anyting yet 300 } 301 else 302 { 303 // call is from a child 304 this->activeChild_ = 0; 305 } 306 307 if (destination == this) 347 308 return; 348 309 349 // Check for 'state' in the children map first 350 std::map<GameState*, GameState*>::const_iterator it = this->grandchildrenToChildren_.find(state); 310 // Check for 'destination' in the children map first 311 std::map<GameState*, GameState*>::const_iterator it 312 = this->grandchildrenToChildren_.find(destination); 351 313 if (it != this->grandchildrenToChildren_.end()) 352 314 { 353 315 // child state. Don't use 'state', might be a grandchild! 354 it->second->activate();355 it->second->makeTransition( state);316 this->activeChild_ = it->second; 317 it->second->makeTransition(this, destination); 356 318 } 357 319 else 358 320 { 359 321 // parent. We can be sure of this. 360 assert(this->parent_ != 0); 361 362 // only do the transition if we're not currently running 363 if (this->operation_.running) 364 { 365 //this->bDeactivationScheduled_ = true; 366 this->scheduledTransition_ = state; 367 } 368 else 369 { 370 this->deactivate(); 371 this->parent_->makeTransition(state); 372 } 373 322 assert(this->getParent() != 0); 323 324 this->deactivate(); 325 this->getParent()->makeTransition(this, destination); 374 326 } 375 327 } … … 381 333 void GameState::activate() 382 334 { 383 if (this->parent_)384 this->parent_->activeChild_ = this;385 335 this->operation_.active = true; 386 336 this->operation_.entering = true; … … 398 348 this->operation_.leaving = false; 399 349 this->operation_.active = false; 400 if (this->parent_)401 this->parent_->activeChild_ = 0;402 350 } 403 351 … … 411 359 This method is not virtual! You cannot override it therefore. 412 360 */ 413 void GameState::tick(float dt )361 void GameState::tick(float dt, uint64_t time) 414 362 { 415 363 this->operation_.running = true; 416 this->ticked(dt );364 this->ticked(dt, time); 417 365 this->operation_.running = false; 418 419 if (this->scheduledTransition_) 420 { 421 // state was requested to be deactivated when ticked. 422 this->makeTransition(this->scheduledTransition_); 423 this->scheduledTransition_ = 0; 424 this->deactivate(); 425 } 426 } 427 366 } 428 367 } -
code/branches/gui/src/core/GameState.h
r1670 r1672 41 41 #include <vector> 42 42 #include <map> 43 #include "util/Integers.h" 43 44 44 45 namespace orxonox … … 60 61 class _CoreExport GameState 61 62 { 63 friend class RootGameState; 64 62 65 public: 66 /** 67 @brief 68 Gives information about what the GameState is currently doing 69 */ 63 70 struct Operations 64 71 { … … 70 77 }; 71 78 79 public: 72 80 GameState(const std::string& name); 73 81 virtual ~GameState(); 74 82 75 83 const std::string& getName() const { return name_; } 84 const Operations getOperation() const { return this->operation_; } 85 bool isInSubtree(GameState* state) const; 86 87 GameState* getState(const std::string& name); 88 GameState* getRoot(); 89 GameState* getParent() const { return this->parent_; } 90 //! Returns the currently active game state 91 virtual GameState* getCurrentState(); 92 93 virtual void requestState(const std::string& name); 76 94 77 95 void addChild(GameState* state); 78 96 void removeChild(GameState* state); 79 97 void removeChild(const std::string& name); 80 void requestState(const std::string& name);81 82 ////! Determines whether the state is active.83 //bool isActive() { return this->bActive_; }84 ////! Determines whether the state is suspended.85 //bool isSuspended() { return this->bSuspended_; }86 ////! Determines whether the state is the current87 //bool isCurrentState() { return this->bActive_ && !this->activeChild_; }88 const Operations getOperation() { return this->operation_; }89 90 void tick(float dt);91 void tickChild(float dt) { if (this->activeChild_) this->activeChild_->tick(dt); }92 98 93 99 protected: 94 100 virtual void enter() = 0; 95 101 virtual void leave() = 0; 96 virtual void ticked(float dt) = 0; 97 //virtual void enter() { } 98 //virtual void leave() { } 99 //virtual void ticked(float dt) { } 102 virtual void ticked(float dt, uint64_t time) = 0; 100 103 101 104 GameState* getActiveChild() { return this->activeChild_; } 102 bool hasScheduledTransition() { return this->scheduledTransition_; } 105 106 void tickChild(float dt, uint64_t time) { if (this->getActiveChild()) this->getActiveChild()->tick(dt, time); } 103 107 104 108 private: 105 GameState* checkState(const std::string& name);106 GameState* getCurrentState();107 GameState* getRootNode(); 109 //! Performs a transition to 'destination' 110 virtual void makeTransition(GameState* source, GameState* destination); 111 108 112 void grandchildAdded(GameState* child, GameState* grandchild); 109 113 void grandchildRemoved(GameState* grandchild); 110 void makeTransition(GameState* state); 114 115 void tick(float dt, uint64_t time); 111 116 void activate(); 112 117 void deactivate(); 113 118 114 const std::string name_; 115 bool bPauseParent_; 116 117 Operations operation_; 118 119 GameState* parent_; 120 GameState* activeChild_; 121 GameState* scheduledTransition_; 122 std::map<std::string, GameState*> allChildren_; 123 std::map<GameState*, GameState*> grandchildrenToChildren_; 119 const std::string name_; 120 Operations operation_; 121 GameState* parent_; 122 GameState* activeChild_; 123 //bool bPauseParent_; 124 std::map<std::string, GameState*> allChildren_; 125 std::map<GameState*, GameState*> grandchildrenToChildren_; 124 126 }; 125 127 } -
code/branches/gui/src/core/input/InputManager.cc
r1670 r1672 640 640 641 641 // check for states to leave 642 for (std::set<InputState*>::reverse_iterator it = stateLeaveRequests_.rbegin();643 it != stateLeaveRequests_.rend(); ++it)644 { 645 (* it)->onLeave();642 for (std::set<InputState*>::reverse_iterator rit = stateLeaveRequests_.rbegin(); 643 rit != stateLeaveRequests_.rend(); ++rit) 644 { 645 (*rit)->onLeave(); 646 646 // just to be sure that the state actually is registered 647 assert(inputStatesByName_.find((* it)->getName()) != inputStatesByName_.end());648 649 activeStates_.erase((* it)->getPriority());647 assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end()); 648 649 activeStates_.erase((*rit)->getPriority()); 650 650 _updateActiveStates(); 651 651 } … … 653 653 654 654 // check for states to enter 655 for (std::set<InputState*>::reverse_iterator it = stateEnterRequests_.rbegin();656 it != stateEnterRequests_.rend(); ++it)655 for (std::set<InputState*>::reverse_iterator rit = stateEnterRequests_.rbegin(); 656 rit != stateEnterRequests_.rend(); ++rit) 657 657 { 658 658 // just to be sure that the state actually is registered 659 assert(inputStatesByName_.find((* it)->getName()) != inputStatesByName_.end());660 661 activeStates_[(* it)->getPriority()] = (*it);659 assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end()); 660 661 activeStates_[(*rit)->getPriority()] = (*rit); 662 662 _updateActiveStates(); 663 (* it)->onEnter();663 (*rit)->onEnter(); 664 664 } 665 665 stateEnterRequests_.clear(); 666 666 667 667 // check for states to destroy 668 for (std::set<InputState*>::reverse_iterator it = stateDestroyRequests_.rbegin();669 it != stateDestroyRequests_.rend(); ++it)670 { 671 _destroyState((* it));668 for (std::set<InputState*>::reverse_iterator rit = stateDestroyRequests_.rbegin(); 669 rit != stateDestroyRequests_.rend(); ++rit) 670 { 671 _destroyState((*rit)); 672 672 } 673 673
Note: See TracChangeset
for help on using the changeset viewer.