Skip to content

Instantly share code, notes, and snippets.

@StonedXander
Created May 27, 2012 19:35
Show Gist options
  • Save StonedXander/2815614 to your computer and use it in GitHub Desktop.
Save StonedXander/2815614 to your computer and use it in GitHub Desktop.
Handler
class CameraHandler {
public:
CameraHandler();
Handler<CameraHandler> *getArcballHandler();
void handleKey(Handler<CameraHandler> *, int, int);
void handleMouseButton(Handler<CameraHandler> *, int, int);
void handleMousePosition(Handler<CameraHandler> *, int, int);
void handleMouseWheel(Handler<CameraHandler> *, int);
private:
Camera *camera;
FPSModifier *fpsModifier;
Arcball *arcball;
ArcballCameraModifier *arcModifier;
Handler<CameraHandler> *arcballHandler;
};
void CameraHandler::handleKey(Handler<CameraHandler> *handler, int key, int action) {
if(handler == arcballHandler) {
// Modify 'arcball' using 'key' and 'action'.
arcModifier.Update();
}
}
// Same with CameraHandler::handleMousePosition, etc.
void Game::handleKey(int key, int action, UserData *) {
Handler<CameraHandler> *handler = getCurrentHandler();
handler->handleKey(key, action);
}
template <typename T> class Handler {
public:
Handler();
void handleKey(int, int);
void handleMousePosition(int, int);
void handleMouseButton(int, int);
void handleMouseWheel(int);
private:
T *target;
};
template <typename T, typename E> Handler<T, E>::Handler(T *t) : target(t) {}
template <typename T, typename E> void Handler<T, E>::handleKey(int a, int b) {
target->handleKey(this, a, b);
}
template <typename T, typename E> void Handler<T, E>::handleMousePosition(int a, int b) {
target->handleMousePosition(this, a, b);
}
template <typename T, typename E> void Handler<T, E>::handleMouseButton(int a, int b) {
target->handleMouseButton(this, a, b);
}
template <typename T, typename E> void Handler<T, E>::handleMouseWheel(int a) {
target->handleWheel(this, a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment