Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel2/src/modules/notifications/NotificationDispatcher.cc @ 8474

Last change on this file since 8474 was 8445, checked in by dafrick, 14 years ago

NotificationListener is new an entity which is informed of new notifications. The NotificationManager is, so far, the only NotificationListener. This means that Notifications can now be sent from within orxonox (though not libraries or external).
Also introduced notification commands to affect the NotificationQueues in more ways than just have them display messages (e.g. clearing them).
Added a message type which allows to send Notifications of different importance, allowing the NotificationQueus to display them differently.

  • Property svn:eol-style set to native
File size: 5.5 KB
RevLine 
[7193]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See thes
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
[7456]30    @file NotificationDispatcher.cc
[7193]31    @brief Implementation of the NotificationDispatcher class.
32*/
33
34#include "NotificationDispatcher.h"
35
36#include "core/CoreIncludes.h"
[7474]37#include "core/EventIncludes.h"
[7193]38#include "core/XMLPort.h"
[7474]39#include "network/NetworkFunction.h"
40#include "network/Host.h"
41
42#include "infos/PlayerInfo.h"
[8374]43#include "interfaces/NotificationListener.h"
[7403]44#include "interfaces/PlayerTrigger.h"
45#include "worldentities/pawns/Pawn.h"
[7193]46
47namespace orxonox
48{
49
50    CreateUnloadableFactory(NotificationDispatcher);
51
[7474]52    registerMemberNetworkFunction(NotificationDispatcher, dispatch);
53
[7193]54    /**
55    @brief
56        Default constructor. Initializes the object.
57    */
[7474]58    NotificationDispatcher::NotificationDispatcher(BaseObject* creator) : BaseObject(creator), Synchronisable(creator)
[7193]59    {
60        RegisterObject(NotificationDispatcher);
61
[8374]62        this->sender_ = NotificationListener::NONE;
[7474]63        this->registerVariables();
[7193]64    }
65
66    /**
67    @brief
68        Destructor.
69    */
70    NotificationDispatcher::~NotificationDispatcher()
71    {
72
73    }
74
75    /**
76    @brief
77        Method for creating a NotificationDispatcher object through XML.
78    */
[7407]79    void NotificationDispatcher::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[7193]80    {
[7407]81        SUPER(NotificationDispatcher, XMLPort, xmlelement, mode);
[7193]82
[7488]83        XMLPortParam(NotificationDispatcher, "sender", getSender, setSender, xmlelement, mode);
84       
[7407]85        XMLPortEventSink(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
[7193]86    }
87
[7407]88    void NotificationDispatcher::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[7193]89    {
[7407]90        SUPER(NotificationDispatcher, XMLEventPort, xmlelement, mode);
[7193]91
[7407]92        XMLPortEventState(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
[7193]93    }
94
[7484]95    /**
96    @brief
97        Registers variables for synchronisation.
98    */
[7474]99    void NotificationDispatcher::registerVariables(void)
100    {
101        registerVariable(this->sender_, VariableDirection::ToClient);
102    }
103
[7193]104    /**
105    @brief
106        Dispatches a Notification with a message supplied by the createNotificationMessage() method, which can be overloaded.
[7403]107    @param clientId
108        The id of the client the notification should be dispatched to.
[7193]109    */
[7403]110    void NotificationDispatcher::dispatch(unsigned int clientId)
[7193]111    {
[7474]112        if(GameMode::isStandalone() || Host::getPlayerID() == clientId || this->getSyncMode() == 0x0)
113        {
114            const std::string message = this->createNotificationMessage();
[8445]115            // TODO: Make the type configurable.
116            NotificationListener::sendNotification(message, this->getSender(), notificationMessageType::info, notificationSendMode::network, clientId);
[7474]117        }
118        else if(GameMode::isServer())
119        {
120            callMemberNetworkFunction(NotificationDispatcher, dispatch, this->getObjectID(), clientId, clientId);
121        }
[7193]122    }
123
124    /**
125    @brief
126        Is called when the NotificationDispatcher is triggered.
127    @param triggered
128        Whether it has been triggered or untriggered. The NotificationDispatcher only reacts to the first kind of events.
[7408]129    @param trigger
130        The object that caused the event to be fired.
[7193]131    @return
132        Returns true if the NotificationDispatcher was successfully triggered.
133    */
[7403]134    bool NotificationDispatcher::trigger(bool triggered, BaseObject* trigger)
[7193]135    {
136        if(!triggered || !this->isActive()) // If the NotificationDispatcher is inactive it cannot be executed.
137            return false;
138
139        COUT(4) << "NotificationDispatcher (&" << this << ") triggered." << std::endl;
140
[7403]141        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
142        Pawn* pawn = NULL;
[7193]143
[7403]144        // If the trigger is a PlayerTrigger.
145        if(pTrigger != NULL)
146        {
[7474]147            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[7403]148                return false;
149            else
150                pawn = pTrigger->getTriggeringPlayer();
151        }
152        else
153            return false;
154
155        if(pawn == NULL)
156        {
[7474]157            COUT(4) << "The NotificationDispatcher was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
[7403]158            return false;
159        }
160
[7474]161        // Extract the PlayerInfo from the Pawn.
[7403]162        PlayerInfo* player = pawn->getPlayer();
163
164        if(player == NULL)
165        {
166            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
167            return false;
168        }
169
170        this->dispatch(player->getClientID());
171
[7193]172        return true;
173    }
174
175}
Note: See TracBrowser for help on using the repository browser.