Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/png2/src/modules/notifications/NotificationDispatcher.cc @ 7368

Last change on this file since 7368 was 7193, checked in by dafrick, 14 years ago

Added NotificationDispatcher, class that can, upon being triggered, dispatch (send/display) a specified Notification.
Additionally a NotificationDispatcher called CommandNotification, that, upon being triggered, displays a Notification with a message and the name of the key that the command is mapped to. It is envisioned to be used in the, soon to be, tutorial level.
Also: Small bugfix in Notification.

File size: 3.4 KB
Line 
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/**
30    @file
31    @brief Implementation of the NotificationDispatcher class.
32*/
33
34#include "NotificationDispatcher.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "core/EventIncludes.h"
39#include "Notification.h"
40#include "NotificationManager.h"
41
42namespace orxonox
43{
44
45    CreateUnloadableFactory(NotificationDispatcher);
46
47    /**
48    @brief
49        Default constructor. Initializes the object.
50    */
51    NotificationDispatcher::NotificationDispatcher(BaseObject* creator) : BaseObject(creator)
52    {
53        RegisterObject(NotificationDispatcher);
54
55        this->sender_ = NotificationManager::NONE;
56    }
57
58    /**
59    @brief
60        Destructor.
61    */
62    NotificationDispatcher::~NotificationDispatcher()
63    {
64
65    }
66
67    /**
68    @brief
69        Method for creating a NotificationDispatcher object through XML.
70    */
71    void NotificationDispatcher::XMLPort(Element& xmlelement, XMLPort::Mode mode)
72    {
73        SUPER(NotificationDispatcher, XMLPort, xmlelement, mode);
74
75        XMLPortEventSink(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
76    }
77
78    void NotificationDispatcher::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
79    {
80        SUPER(NotificationDispatcher, XMLEventPort, xmlelement, mode);
81
82        XMLPortEventState(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
83    }
84
85    /**
86    @brief
87        Dispatches a Notification with a message supplied by the createNotificationMessage() method, which can be overloaded.
88    */
89    void NotificationDispatcher::dispatch(void)
90    {
91        const std::string message = this->createNotificationMessage();
92        Notification* notification = new Notification(this, message);
93
94        notification->send(this->getSender());
95    }
96
97    /**
98    @brief
99        Is called when the NotificationDispatcher is triggered.
100    @param triggered
101        Whether it has been triggered or untriggered. The NotificationDispatcher only reacts to the first kind of events.
102    @return
103        Returns true if the NotificationDispatcher was successfully triggered.
104    */
105    bool NotificationDispatcher::trigger(bool triggered)
106    {
107        if(!triggered || !this->isActive()) // If the NotificationDispatcher is inactive it cannot be executed.
108            return false;
109
110        COUT(4) << "NotificationDispatcher (&" << this << ") triggered." << std::endl;
111
112        this->dispatch();
113
114        return true;
115    }
116
117}
Note: See TracBrowser for help on using the repository browser.