Danbias/Code/Misc/Utilities/EventHandler/EventButton.h

293 lines
6.5 KiB
C
Raw Normal View History

2014-01-31 14:29:29 +01:00
///////////////////////
// Sam Svensson 2013 //
///////////////////////
2014-01-31 15:38:12 +01:00
#ifndef MISC_EVENT_BUTTON_H
#define MISC_EVENT_BUTTON_H
2014-02-11 09:16:01 +01:00
#include "IEventButton.h"
2014-01-31 14:29:29 +01:00
namespace Oyster
{
namespace Event
{
2014-02-11 09:16:01 +01:00
template <typename Owner>
struct ButtonEvent
2014-01-31 15:06:30 +01:00
{
2014-02-11 09:16:01 +01:00
ButtonState state;
IEventButton* sender;
Owner owner;
void* userData;
};
template <typename Owner>
class EventButton : public IEventButton
{
protected:
//typedef for callback function pointer
typedef void (*EventFunc)(Oyster::Event::ButtonEvent<Owner>& e);
2014-01-31 15:38:12 +01:00
2014-01-31 14:29:29 +01:00
struct PrivData
{
2014-02-11 09:16:01 +01:00
PrivData() : ID(currID++){}
2014-01-31 14:29:29 +01:00
static unsigned int currID;
2014-02-11 09:16:01 +01:00
const unsigned int ID;
2014-01-31 14:29:29 +01:00
2014-02-11 09:16:01 +01:00
ButtonState previousState;
Owner owner;
EventFunc eventCallback;
void* userData;
bool enabled;
2014-01-31 14:29:29 +01:00
};
2014-02-11 09:16:01 +01:00
2014-01-31 14:29:29 +01:00
PrivData privData;
2014-02-11 09:16:01 +01:00
private:
//Implement this in the inherited classes for collision against that shape.
virtual bool Collision(MouseInput& input) = 0;
2014-02-11 09:16:01 +01:00
2014-01-31 14:29:29 +01:00
public:
EventButton();
2014-02-11 09:16:01 +01:00
EventButton(Owner owner);
EventButton(EventFunc func);
EventButton(EventFunc func, Owner owner);
EventButton(EventFunc func, Owner owner, void* userData);
2014-02-12 09:08:38 +01:00
virtual ~EventButton();
2014-01-31 14:29:29 +01:00
void Update(MouseInput& input);
2014-02-11 09:16:01 +01:00
//Send event to callback function
void SendEvent(ButtonState state);
2014-01-31 14:29:29 +01:00
2014-02-11 09:16:01 +01:00
//Set
void SetEnabled(bool enable);
2014-02-11 09:16:01 +01:00
void SetUserData(void* data);
void SetEventFunc(EventFunc func);
void SetOwner(Owner owner);
//Get
bool Enabled();
2014-01-31 14:29:29 +01:00
unsigned int GetID();
2014-02-11 09:16:01 +01:00
//EventFunc GetFunctionPointer();
Owner GetOwner();
2014-02-12 09:08:38 +01:00
ButtonState GetState();
2014-02-11 09:16:01 +01:00
bool operator ==(const EventButton<Owner>& obj);
2014-01-31 14:29:29 +01:00
};
2014-02-11 09:16:01 +01:00
template <typename Owner>
unsigned int EventButton<Owner>::PrivData::currID = 0;
2014-01-31 14:29:29 +01:00
2014-02-11 09:16:01 +01:00
template <typename Owner>
EventButton<Owner>::EventButton()
2014-01-31 14:29:29 +01:00
{
2014-02-11 09:16:01 +01:00
this->privData.eventCallback = NULL;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
2014-01-31 14:29:29 +01:00
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
EventButton<Owner>::EventButton(Owner owner)
2014-01-31 14:29:29 +01:00
{
this->privData.owner = owner;
2014-02-11 09:16:01 +01:00
this->privData.eventCallback = NULL;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
2014-01-31 14:29:29 +01:00
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
EventButton<Owner>::EventButton(EventFunc func)
2014-01-31 14:29:29 +01:00
{
2014-02-11 09:16:01 +01:00
this->privData.eventCallback = func;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
2014-01-31 14:29:29 +01:00
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
EventButton<Owner>::EventButton(EventFunc func, Owner owner)
2014-01-31 14:29:29 +01:00
{
this->privData.owner = owner;
2014-02-11 09:16:01 +01:00
this->privData.eventCallback = func;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
}
template <typename Owner>
EventButton<Owner>::EventButton(EventFunc func, Owner owner, void* userData)
{
this->privData.owner = owner;
this->privData.eventCallback = func;
this->privData.userData = userData;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
2014-01-31 14:29:29 +01:00
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
EventButton<Owner>::~EventButton()
{}
//Checks for collision and
template <typename Owner>
void EventButton<Owner>::Update(MouseInput& input)
2014-01-31 14:29:29 +01:00
{
2014-02-11 09:16:01 +01:00
if(this->privData.enabled)
{
ButtonState currentState = ButtonState_None;
2014-02-12 09:08:38 +01:00
static bool outside = false;
static bool clicked = false;
//Check for collision against the button.
2014-02-11 09:16:01 +01:00
if(Collision(input))
{
if(input.mouseButtonPressed)
2014-02-11 09:16:01 +01:00
{
//Change state when the mouse button is pressed
switch(this->privData.previousState)
{
case ButtonState_None:
2014-02-12 09:08:38 +01:00
outside = true;
2014-02-11 09:16:01 +01:00
currentState = ButtonState_Hover;
break;
case ButtonState_Hover:
2014-02-12 09:08:38 +01:00
if(outside == false)
{
clicked = true;
currentState = ButtonState_Pressed;
}
else
{
currentState = ButtonState_Hover;
}
2014-02-11 09:16:01 +01:00
break;
case ButtonState_Released:
currentState = ButtonState_Hover;
break;
2014-02-11 09:16:01 +01:00
case ButtonState_Pressed:
case ButtonState_Down:
currentState = ButtonState_Down;
break;
default:
break;
}
}
else
{
2014-02-12 09:08:38 +01:00
outside = false;
2014-02-11 09:16:01 +01:00
//Change state when the mouse button is NOT pressed
switch(this->privData.previousState)
{
case ButtonState_None:
case ButtonState_Hover:
case ButtonState_Released:
currentState = ButtonState_Hover;
2014-02-12 09:08:38 +01:00
clicked = false;
2014-02-11 09:16:01 +01:00
break;
case ButtonState_Pressed:
case ButtonState_Down:
currentState = ButtonState_Released;
break;
default:
break;
}
}
}
//Only call the callback function when the state has changed.
if(this->privData.previousState != currentState)
SendEvent(currentState);
this->privData.previousState = currentState;
}
2014-01-31 14:29:29 +01:00
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
void EventButton<Owner>::SendEvent(ButtonState state)
2014-01-31 14:29:29 +01:00
{
2014-02-11 09:16:01 +01:00
if(privData.eventCallback != NULL)
{
Oyster::Event::ButtonEvent<Owner> event;
event.state = state;
event.sender = this;
event.owner = privData.owner;
event.userData = privData.userData;
privData.eventCallback(event);
}
}
//Set if the button should be updated and collided with.
template <typename Owner>
void EventButton<Owner>::SetEnabled(bool enable)
{
this->privData.enabled = enable;
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
void EventButton<Owner>::SetUserData(void* data)
{
this->privData.userData = data;
2014-01-31 14:29:29 +01:00
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
void EventButton<Owner>::SetEventFunc(EventFunc func)
2014-01-31 14:29:29 +01:00
{
this->privData.EventFunc = EventFunc;
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
void EventButton<Owner>::SetOwner(Owner owner)
{
this->privData.owner = owner;
}
2014-01-31 14:29:29 +01:00
2014-02-11 09:16:01 +01:00
template <typename Owner>
bool EventButton<Owner>::Enabled()
{
return this->privData.enabled;
}
template <typename Owner>
unsigned int EventButton<Owner>::GetID()
2014-01-31 14:29:29 +01:00
{
return this->privData.ID;
}
2014-02-11 09:16:01 +01:00
/* Something is wrong, can't return EventFunc
template <typename Owner>
EventFunc EventButton<Owner>::GetFunctionPointer()
{
return this->privData.eventCallback;
}*/
template <typename Owner>
Owner EventButton<Owner>::GetOwner()
{
return this->privData.owner;
}
2014-02-12 09:08:38 +01:00
template <typename Owner>
ButtonState EventButton<Owner>::GetState()
{
return this->privData.previousState;
}
2014-02-11 09:16:01 +01:00
template <typename Owner>
bool EventButton<Owner>::operator ==(const EventButton<Owner>& obj)
{
return (this->privData.ID == obj.privData.ID);
}
2014-01-31 14:29:29 +01:00
}
2014-01-31 15:38:12 +01:00
}
2014-01-31 15:38:12 +01:00
#endif