Skip to content

Commit

Permalink
EVENTS: Handle the notifications bundled in the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Mar 20, 2014
1 parent cd33eee commit 833af86
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 60 deletions.
5 changes: 0 additions & 5 deletions src/engines/nwn/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ class Area : public Aurora::NWScript::Object {
static Common::UString getName(const Common::UString &resRef);


protected:
/** Notify the area that the camera has been moved. */
void notifyCameraMoved();


private:
/** Tile orientation. */
enum Orientation {
Expand Down
25 changes: 16 additions & 9 deletions src/engines/nwn/gui/ingame/compass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Compass::Compass(float position) {
addWidget(_compass);


notifyResized(0, 0, GfxMan.getScreenWidth(), GfxMan.getScreenHeight());
notify(Events::kNotificationResized);
}

Compass::~Compass() {
Expand All @@ -96,16 +96,23 @@ Compass::~Compass() {
void Compass::callbackActive(Widget &widget) {
}

void Compass::notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
setPosition(newWidth / 2.0, - (newHeight / 2.0), -400.0);
}
void Compass::notify(Events::Notification notification) {
if (notification == Events::kNotificationResized) {
const float width = GfxMan.getScreenWidth();
const float height = GfxMan.getScreenHeight();

setPosition(width / 2.0, -(height / 2.0), -400.0);
return;
}

void Compass::notifyCameraMoved() {
float roll, pitch, yaw, x, y, z;
CameraMan.getOrientation(roll, pitch, yaw);
CameraMan.getDirection(x, y, z);
if (notification == Events::kNotificationCameraMoved) {
float roll, pitch, yaw, x, y, z;
CameraMan.getOrientation(roll, pitch, yaw);
CameraMan.getDirection(x, y, z);

_compass->setRotation(x, y, yaw);
_compass->setRotation(x, y, yaw);
return;
}
}

} // End of namespace NWN
Expand Down
3 changes: 1 addition & 2 deletions src/engines/nwn/gui/ingame/compass.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class Compass : public GUI, public Events::Notifyable {
private:
CompassWidget *_compass;

void notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight);
void notifyCameraMoved();
void notify(Events::Notification notification);
};

} // End of namespace NWN
Expand Down
14 changes: 10 additions & 4 deletions src/engines/nwn/gui/ingame/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ Dialog::Dialog(const Common::UString &conv, Creature &pc, Object &obj,
playSound(playHello);
playAnimation();

notifyResized(0, 0, GfxMan.getScreenWidth(), GfxMan.getScreenHeight());
notify(Events::kNotificationResized);
}

Dialog::~Dialog() {
Expand Down Expand Up @@ -687,9 +687,15 @@ void Dialog::checkPicked() {
mouseMove();
}

void Dialog::notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
const float x = -(newWidth / 2.0) + 10.0;
const float y = (newHeight / 2.0) - _dlgBox->getHeight() - 20.0;
void Dialog::notify(Events::Notification notification) {
if (notification != Events::kNotificationResized)
return;

const float width = GfxMan.getScreenWidth();
const float height = GfxMan.getScreenHeight();

const float x = -(width / 2.0) + 10.0;
const float y = (height / 2.0) - _dlgBox->getHeight() - 20.0;

_dlgBox->setPosition(x, y, 0.0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn/gui/ingame/dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Dialog : public Events::Notifyable {
int processEventQueue();

protected:
void notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight);
void notify(Events::Notification notification);

private:
Common::UString _conv; ///< The conversation file.
Expand Down
12 changes: 9 additions & 3 deletions src/engines/nwn/gui/ingame/partyleader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ PartyLeader::PartyLeader(Module &module) : _module(&module),


updatePortraitTooltip();
notifyResized(0, 0, GfxMan.getScreenWidth(), GfxMan.getScreenHeight());
notify(Events::kNotificationResized);
}

PartyLeader::~PartyLeader() {
Expand Down Expand Up @@ -183,8 +183,14 @@ void PartyLeader::updatePortraitTooltip() {
*/
}

void PartyLeader::notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
setPosition(newWidth / 2.0, newHeight / 2.0, -400.0);
void PartyLeader::notify(Events::Notification notification) {
if (notification != Events::kNotificationResized)
return;

const float width = GfxMan.getScreenWidth();
const float height = GfxMan.getScreenHeight();

setPosition(width / 2.0, height / 2.0, -400.0);
}

} // End of namespace NWN
Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn/gui/ingame/partyleader.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class PartyLeader : public CharacterInfo, public Events::Notifyable {

void updatePortraitTooltip();

void notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight);
void notify(Events::Notification notification);
};

} // End of namespace NWN
Expand Down
12 changes: 9 additions & 3 deletions src/engines/nwn/gui/ingame/quickbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Quickbar::Quickbar() {
topEdge->setPosition(0.0, _slotHeight, 0.0);
addWidget(topEdge);

notifyResized(0, 0, GfxMan.getScreenWidth(), GfxMan.getScreenHeight());
notify(Events::kNotificationResized);
}

Quickbar::~Quickbar() {
Expand Down Expand Up @@ -160,8 +160,14 @@ void Quickbar::getSlotSize() {
delete model;
}

void Quickbar::notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
setPosition(- ((12 * _slotWidth) / 2.0), -(newHeight / 2.0), -400.0);
void Quickbar::notify(Events::Notification notification) {
if (notification != Events::kNotificationResized)
return;

const float width = GfxMan.getScreenWidth();
const float height = GfxMan.getScreenHeight();

setPosition(- ((12 * _slotWidth) / 2.0), -(height / 2.0), -400.0);
}

} // End of namespace NWN
Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn/gui/ingame/quickbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Quickbar : public GUI, public Events::Notifyable {

void getSlotSize();

void notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight);
void notify(Events::Notification notification);
};

} // End of namespace NWN
Expand Down
12 changes: 9 additions & 3 deletions src/engines/nwn/gui/ingame/quickchat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Quickchat::Quickchat(float position) {
addWidget(modeButton);


notifyResized(0, 0, GfxMan.getScreenWidth(), GfxMan.getScreenHeight());
notify(Events::kNotificationResized);
}

Quickchat::~Quickchat() {
Expand All @@ -126,8 +126,14 @@ float Quickchat::getHeight() const {
void Quickchat::callbackActive(Widget &widget) {
}

void Quickchat::notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
setPosition(- (newWidth / 2.0), -(newHeight / 2.0), -400.0);
void Quickchat::notify(Events::Notification notification) {
if (notification != Events::kNotificationResized)
return;

const float width = GfxMan.getScreenWidth();
const float height = GfxMan.getScreenHeight();

setPosition(- (width / 2.0), -(height / 2.0), -400.0);
}

} // End of namespace NWN
Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn/gui/ingame/quickchat.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Quickchat : public GUI, public Events::Notifyable {
private:
WidgetPanel *_prompt;

void notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight);
void notify(Events::Notification notification);
};

} // End of namespace NWN
Expand Down
5 changes: 4 additions & 1 deletion src/engines/nwn/gui/widgets/tooltip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ void Tooltip::setAlign(float align) {
redoLayout();
}

void Tooltip::notifyCameraMoved() {
void Tooltip::notify(Events::Notification notification) {
if (notification != Events::kNotificationCameraMoved)
return;

updatePosition();
}

Expand Down
2 changes: 1 addition & 1 deletion src/engines/nwn/gui/widgets/tooltip.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Tooltip : public Events::Notifyable {


protected:
void notifyCameraMoved();
void notify(Events::Notification notification);


private:
Expand Down
3 changes: 3 additions & 0 deletions src/events/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ void EventsManager::runMainLoop() {

_queueProcessed.signal();

// Handle the notifications
NotificationMan.handle();

if (!_quitRequested)
// Render a frame
GfxMan.renderScene();
Expand Down
17 changes: 10 additions & 7 deletions src/events/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ NotificationManager::~NotificationManager() {
}

void NotificationManager::init() {
for (int i = 0; i < kNotificationMAX; i++)
_notifications[i].store(false);
}

std::list<Notifyable *>::iterator NotificationManager::registerNotifyable(Notifyable &notifyable) {
Expand All @@ -57,18 +59,19 @@ void NotificationManager::unregisterNotifyable(const std::list<Notifyable *>::it
_notifyables.erase(it);
}

void NotificationManager::resized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
Common::StackLock lock(_mutex);
void NotificationManager::notify(Notification notification) {
assert((notification >= 0) && (notification < kNotificationMAX));

for (std::list<Notifyable *>::iterator it = _notifyables.begin(); it != _notifyables.end(); ++it)
(*it)->notifyResized(oldWidth, oldHeight, newWidth, newHeight);
_notifications[notification].store(true);
}

void NotificationManager::cameraMoved() {
void NotificationManager::handle() {
Common::StackLock lock(_mutex);

for (std::list<Notifyable *>::iterator it = _notifyables.begin(); it != _notifyables.end(); ++it)
(*it)->notifyCameraMoved();
for (int i = 0; i < kNotificationMAX; i++)
if (_notifications[i].exchange(false))
for (std::list<Notifyable *>::iterator it = _notifyables.begin(); it != _notifyables.end(); ++it)
(*it)->notify((Notification) i);
}

} // End of namespace Events
20 changes: 16 additions & 4 deletions src/events/notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include <list>

#include <boost/atomic.hpp>

#include "common/types.h"
#include "common/singleton.h"
#include "common/mutex.h"
Expand All @@ -42,6 +44,13 @@ namespace Events {

class Notifyable;

/** Types of notifications that can be sent. */
enum Notification {
kNotificationResized = 0, ///< The main window was resized.
kNotificationCameraMoved , ///< The camera was moved.
kNotificationMAX ///< For range checks.
};

/** The notification manager, handling all notifications.
*
* Notifications are handed out to all classes of the Notifyable type for
Expand All @@ -58,20 +67,23 @@ class NotificationManager : public Common::Singleton<NotificationManager> {

void init();

/** Notify all Notifyables that the screen size changed. */
void resized(int oldWidth, int oldHeight, int newWidth, int newHeight);
/** Notify all Notifyables that the camera moved. */
void cameraMoved();
/** Notify all Notifyables of a Notification. */
void notify(Notification notification);

private:
Common::Mutex _mutex;

boost::atomic<bool> _notifications[kNotificationMAX];

std::list<Notifyable *> _notifyables;

std::list<Notifyable *>::iterator registerNotifyable(Notifyable &notifyable);
void unregisterNotifyable(const std::list<Notifyable *>::iterator &it);

friend class Notifyable;

public:
void handle(); ///< Called by the main thread.
};

} // End of namespace Events
Expand Down
4 changes: 1 addition & 3 deletions src/events/notifyable.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class Notifyable {
}

private:
virtual void notifyResized(int oldWidth, int oldHeight, int newWidth, int newHeight) {
}
virtual void notifyCameraMoved() {
virtual void notify(Notification notification) {
}

friend class NotificationManager;
Expand Down

0 comments on commit 833af86

Please sign in to comment.