Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

BehaviorSwitchControl.cc

Go to the documentation of this file.
00001 #include "BehaviorSwitchControl.h"
00002 #include "Events/TextMsgEvent.h"
00003 
00004 ControlBase * BehaviorSwitchControlBase::takeInput(const std::string& msg) {
00005   if(options.size()>0)
00006     return ControlBase::takeInput(msg);
00007   if(!isRunning())
00008     startmine();
00009   mybeh->processEvent(TextMsgEvent(msg,1));
00010   return NULL;
00011 }
00012 
00013 void BehaviorSwitchControlBase::setGroup(BehaviorGroup* bg) {
00014   if(behgrp!=NULL) {
00015     behgrp->members.erase(this);
00016     behgrp->removeReference();
00017   }
00018   behgrp=bg;
00019   if(behgrp!=NULL) {
00020     behgrp->addReference();
00021     behgrp->members.insert(this);
00022     if(mybeh!=NULL && mybeh->isActive()) {
00023       if(behgrp->curBehavior!=NULL) {
00024         behgrp->curBehavior->stop();
00025         notifyGroupMembers();
00026       }
00027       behgrp->curBehavior=mybeh;
00028     }
00029   }
00030 }
00031 
00032 ControlBase * BehaviorSwitchControlBase::activate(MC_ID display, Socket * gui) {
00033   if(slotsSize()==0) {
00034     toggle();
00035     return NULL;
00036   } else
00037     return ControlBase::activate(display,gui);
00038 }
00039 
00040 std::string BehaviorSwitchControlBase::getName() const {
00041   if(mybeh==NULL)
00042     return ControlBase::getName();
00043   return (mybeh->isActive()?'#':'-')+mybeh->getName();
00044 }
00045 
00046 std::string BehaviorSwitchControlBase::getDescription() const {
00047   if(mybeh==NULL)
00048     return ControlBase::getDescription();
00049   return "Class "+mybeh->getClassName()+": "+mybeh->getDescription();
00050 }
00051 
00052 bool BehaviorSwitchControlBase::isRunning() const {
00053   if(mybeh==NULL) //not created or has been destroyed, definitely not running
00054     return false;
00055   // so, beh has been created (but may have been stopped by another in the group)
00056   return mybeh->isActive(); //just check active flag (is valid object, we would have set it to NULL if we stopped it ourselves)
00057 }
00058 
00059 void BehaviorSwitchControlBase::stopother() {
00060   if(behgrp==NULL) {
00061     if(mybeh!=NULL) {
00062       if(mybeh->isActive())
00063         mybeh->stop();
00064       behaviorStopped();
00065     }
00066   } else if(behgrp->curBehavior!=NULL) {
00067     if(behgrp->curBehavior->isActive())
00068       behgrp->curBehavior->stop();
00069     notifyGroupMembers();
00070     behgrp->curBehavior=NULL;
00071   }
00072 }
00073 
00074 void BehaviorSwitchControlBase::startmine() {
00075   if(behgrp!=NULL)
00076     behgrp->curBehavior=mybeh;
00077   mybeh->start();
00078 }
00079 
00080 void BehaviorSwitchControlBase::notifyGroupMembers() {
00081   for(std::set<BehaviorSwitchControlBase*>::iterator it=behgrp->members.begin(); it!=behgrp->members.end(); ++it)
00082     if((*it)->mybeh==behgrp->curBehavior)
00083       (*it)->behaviorStopped();
00084 }
00085 
00086 /*! @file
00087  * @brief Implements BehaviorSwitchControlBase - a control for turning behaviors on and off
00088  * @author ejt (Creator)
00089  */
00090 

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:36 2016 by Doxygen 1.6.3