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(InputClass *input) = 0;
|
|
|
|
|
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-01-31 14:29:29 +01:00
|
|
|
~EventButton();
|
|
|
|
|
2014-02-11 09:16:01 +01:00
|
|
|
void Update(InputClass *input);
|
2014-02-03 10:41:54 +01:00
|
|
|
|
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
|
2014-02-11 14:13:35 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
bool operator ==(const EventButton<Owner>& obj);
|
|
|
|
|
2014-01-31 14:29:29 +01:00
|
|
|
};
|
2014-02-03 10:41:54 +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(InputClass *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-01-31 14:29:29 +01:00
|
|
|
|
2014-02-11 09:16:01 +01:00
|
|
|
if(Collision(input))
|
|
|
|
{
|
|
|
|
if(input->IsMousePressed())
|
|
|
|
{
|
|
|
|
//Change state when the mouse button is pressed
|
|
|
|
switch(this->privData.previousState)
|
|
|
|
{
|
|
|
|
case ButtonState_None:
|
|
|
|
currentState = ButtonState_Hover;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ButtonState_Hover:
|
|
|
|
case ButtonState_Released:
|
|
|
|
currentState = ButtonState_Pressed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ButtonState_Pressed:
|
|
|
|
case ButtonState_Down:
|
|
|
|
currentState = ButtonState_Down;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//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;
|
|
|
|
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);
|
|
|
|
}
|
2014-02-03 10:41:54 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 14:13:35 +01:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-03 10:41:54 +01:00
|
|
|
|
2014-01-31 15:38:12 +01:00
|
|
|
#endif
|