Skip to content

Commit

Permalink
on(Un)hover callback for Spots (#195)
Browse files Browse the repository at this point in the history
* onHover callback for Spots

add a `onHover` function to Lua spots, accepting a function parameter to
be called when that Spot is hovered.

* add onUnhover with semantics symmetric to onHover
  • Loading branch information
bd339 committed May 24, 2017
1 parent fa2c388 commit 1dcf7c0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Language.h
Expand Up @@ -99,6 +99,7 @@
#define kString14013 "Syntax error"
#define kString14014 "Function expected as second parameter in register()"
#define kString14015 "Bad configuration file"
#define kString14016 "Expected function parameter"

// Font module
#define kString15001 "Initializing font manager..."
Expand Down
12 changes: 11 additions & 1 deletion src/Scene.cpp
Expand Up @@ -26,6 +26,7 @@
#include "RenderManager.h"
#include "Room.h"
#include "Scene.h"
#include "Script.h"
#include "Spot.h"
#include "Texture.h"
#include "VideoManager.h"
Expand All @@ -51,6 +52,7 @@ videoManager(VideoManager::instance())
_canDrawSpots = false;
_isCutsceneLoaded = false;
_isSplashLoaded = false;
_lastHoveredSpot = nullptr;
}

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -193,13 +195,21 @@ bool Scene::scanSpots() {
if (color == spot->color()) {
cursorManager.setAction(*spot->action());
foundAction = true;

if (_lastHoveredSpot != spot && spot->hasOnHoverCallback()) {
Script::instance().processCallback(spot->onHoverCallback(), spot->luaObject());
}
_lastHoveredSpot = spot;
break;
}
} while (currentNode->iterateSpots());
}

if (!foundAction) {
if (_lastHoveredSpot && _lastHoveredSpot->hasOnUnhoverCallback()) {
Script::instance().processCallback(_lastHoveredSpot->onUnhoverCallback(),
_lastHoveredSpot->luaObject());
}
_lastHoveredSpot = nullptr;
cursorManager.removeAction();

if (cameraManager.isPanning())
Expand Down
3 changes: 3 additions & 0 deletions src/Scene.h
Expand Up @@ -31,6 +31,7 @@ class Config;
class CursorManager;
class RenderManager;
class Room;
class Spot;
class State;
class Texture;
class VideoManager;
Expand All @@ -52,6 +53,8 @@ class Scene {
Room* _currentRoom;
Texture* _cutsceneTexture;
Texture* _splashTexture;

Spot* _lastHoveredSpot;

bool _canDrawSpots; // This bool is used to make checks faster
bool _isCutsceneLoaded;
Expand Down
31 changes: 31 additions & 0 deletions src/Spot.cpp
Expand Up @@ -16,6 +16,7 @@
////////////////////////////////////////////////////////////

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iterator>
#include <sstream>
Expand Down Expand Up @@ -49,6 +50,8 @@ Spot::Spot(std::vector<int> withArrayOfCoordinates,
_xOrigin = 0;
_yOrigin = 0;
_zOrder = 0; // For future use
_hasHoverCallback = false;
_hasUnhoverCallback = false;
this->setType(kObjectSpot);
}

Expand Down Expand Up @@ -97,6 +100,14 @@ bool Spot::isPlaying() {
return _isPlaying;
}

bool Spot::hasOnHoverCallback() const {
return _hasHoverCallback;
}

bool Spot::hasOnUnhoverCallback() const {
return _hasUnhoverCallback;
}

////////////////////////////////////////////////////////////
// Implementation - Gets
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -151,6 +162,16 @@ std::string Spot::stringifyCoords() const {
return coordStr.str() + std::to_string(_arrayOfCoordinates.back());
}

int Spot::onHoverCallback() const {
assert(_hasHoverCallback);
return _luaHoverCallback;
}

int Spot::onUnhoverCallback() const {
assert(_hasUnhoverCallback);
return _luaUnhoverCallback;
}

////////////////////////////////////////////////////////////
// Implementation - Sets
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -209,6 +230,16 @@ void Spot::setVolume(float theVolume) {
_volume = theVolume;
}

void Spot::setOnHoverCallback(int luaRegRef) {
_luaHoverCallback = luaRegRef;
_hasHoverCallback = true;
}

void Spot::setOnUnhoverCallback(int luaRegRef) {
_luaUnhoverCallback = luaRegRef;
_hasUnhoverCallback = true;
}

////////////////////////////////////////////////////////////
// Implementation - State changes
////////////////////////////////////////////////////////////
Expand Down
10 changes: 10 additions & 0 deletions src/Spot.h
Expand Up @@ -61,6 +61,8 @@ class Spot : public Object {
bool hasTexture();
bool hasVideo();
bool isPlaying();
bool hasOnHoverCallback() const;
bool hasOnUnhoverCallback() const;

// Gets
Action* action();
Expand All @@ -74,6 +76,8 @@ class Spot : public Object {
Video* video();
float volume();
std::string stringifyCoords() const;
int onHoverCallback() const;
int onUnhoverCallback() const;

// Sets
void setAction(Action* anAction);
Expand All @@ -83,6 +87,8 @@ class Spot : public Object {
void setTexture(Texture* aTexture);
void setVideo(Video* aVideo);
void setVolume(float theVolume);
void setOnHoverCallback(int luaRegRef);
void setOnUnhoverCallback(int luaRegRef);

// State changes
void play();
Expand Down Expand Up @@ -110,6 +116,10 @@ class Spot : public Object {
int _xOrigin;
int _yOrigin;
int _zOrder; // For future use
bool _hasHoverCallback;
int _luaHoverCallback;
bool _hasUnhoverCallback;
int _luaUnhoverCallback;

Spot(const Spot&);
void operator=(const Spot&);
Expand Down
34 changes: 34 additions & 0 deletions src/SpotProxy.h
Expand Up @@ -290,6 +290,38 @@ class SpotProxy : public ObjectProxy {
s->stop();
return 0;
}

int onHover(lua_State *L) {
if (!lua_isfunction(L, 1)) {
Log::instance().trace(kModScript, kString14016);
return 0;
}

if (s->hasOnHoverCallback())
luaL_unref(L, LUA_REGISTRYINDEX, s->onHoverCallback());

lua_pushvalue(L, 1); // push the callback on the stack
int ref = luaL_ref(L, LUA_REGISTRYINDEX); // pop callback and return a registry reference to it
s->setOnHoverCallback(ref);

return 0;
}

int onUnhover(lua_State *L) {
if (!lua_isfunction(L, 1)) {
Log::instance().trace(kModScript, kString14016);
return 0;
}

if (s->hasOnUnhoverCallback())
luaL_unref(L, LUA_REGISTRYINDEX, s->onUnhoverCallback());

lua_pushvalue(L, 1); // push the callback on the stack
int ref = luaL_ref(L, LUA_REGISTRYINDEX); // pop callback and return a registry reference to it
s->setOnUnhoverCallback(ref);

return 0;
}

// Destructor
~SpotProxy() { delete s; }
Expand All @@ -313,6 +345,8 @@ Luna<SpotProxy>::RegType SpotProxy::methods[] = {
method(SpotProxy, isPlaying),
method(SpotProxy, play),
method(SpotProxy, stop),
method(SpotProxy, onHover),
method(SpotProxy, onUnhover),
{0,0}
};

Expand Down

0 comments on commit 1dcf7c0

Please sign in to comment.