Misc - Button system working. (Logic)

This commit is contained in:
Pontus Fransson 2014-02-11 09:16:01 +01:00
parent 50d28a5784
commit 9a82b70020
10 changed files with 399 additions and 106 deletions

View File

@ -4,125 +4,256 @@
#ifndef MISC_EVENT_BUTTON_H #ifndef MISC_EVENT_BUTTON_H
#define MISC_EVENT_BUTTON_H #define MISC_EVENT_BUTTON_H
#include "../../Input/L_inputClass.h"
#include "IEventButton.h"
namespace Oyster namespace Oyster
{ {
namespace Event namespace Event
{ {
template <typename owner> template <typename Owner>
class EventButton
{
public:
struct ButtonEvent struct ButtonEvent
{ {
IEventButton::ButtonState state; ButtonState state;
EventButton<owner> &sender; IEventButton* sender;
owner owner; 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);
struct PrivData struct PrivData
{ {
static unsigned int currID; PrivData() : ID(currID++){}
unsigned int ID;
owner owner; static unsigned int currID;
void (*EventFunc)( ButtonEvent e ); const unsigned int ID;
ButtonState previousState;
Owner owner;
EventFunc eventCallback;
void* userData;
bool enabled;
}; };
PrivData privData; PrivData privData;
private:
//Implement this in the inherited classes for collision against that shape.
virtual bool Collision(InputClass *input) = 0;
public: public:
EventButton(); EventButton();
EventButton(owner owner); EventButton(Owner owner);
EventButton(void (*EventFunc)( ButtonEvent e)); EventButton(EventFunc func);
EventButton(void (*EventFunc)( ButtonEvent e), owner owner); EventButton(EventFunc func, Owner owner);
EventButton(EventFunc func, Owner owner, void* userData);
~EventButton(); ~EventButton();
void CheckCollision(InputClass *input); void Update(InputClass *input);
void SendEvent(IEventButton::ButtonState state); //Send event to callback function
void SendEvent(ButtonState state);
void SetEventFunc(void (*EventFunc)( ButtonEvent e )); //? //Set
void SetUserData(void* data);
void SetEventFunc(EventFunc func);
void SetOwner(Owner owner);
//Get
bool Enabled();
unsigned int GetID(); unsigned int GetID();
//EventFunc GetFunctionPointer();
Owner GetOwner();
bool operator ==(const EventButton<Owner>& obj);
}; };
template <typename owner> template <typename Owner>
unsigned int EventButton<owner>::PrivData::currID = 0; unsigned int EventButton<Owner>::PrivData::currID = 0;
template <typename owner> template <typename Owner>
EventButton<owner>::EventButton() EventButton<Owner>::EventButton()
{ {
this->privData.ID = privData.currID; this->privData.eventCallback = NULL;
this->privData.currID += 1; this->privData.userData = NULL;
//this->privData.owner = NULL; this->privData.previousState = ButtonState_None;
this->privData.EventFunc = NULL; this->privData.enabled = true;
} }
template <typename owner> template <typename Owner>
EventButton<owner>::EventButton(owner owner) EventButton<Owner>::EventButton(Owner owner)
{ {
this->privData.ID = privData.currID;
this->privData.currID += 1;
this->privData.owner = owner; this->privData.owner = owner;
this->privData.EventFunc = NULL; this->privData.eventCallback = NULL;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
} }
template <typename owner> template <typename Owner>
EventButton<owner>::EventButton(void (*EventFunc)( ButtonEvent e)) EventButton<Owner>::EventButton(EventFunc func)
{ {
this->privData.ID = privData.currID; this->privData.eventCallback = func;
this->privData.currID += 1; this->privData.userData = NULL;
//this->privData.owner = NULL; this->privData.previousState = ButtonState_None;
this->privData.EventFunc = EventFunc; this->privData.enabled = true;
} }
template <typename owner> template <typename Owner>
EventButton<owner>::EventButton(void (*EventFunc)( ButtonEvent e), owner owner) EventButton<Owner>::EventButton(EventFunc func, Owner owner)
{ {
this->privData.ID = privData.currID;
this->privData.currID += 1;
this->privData.owner = owner; this->privData.owner = owner;
this->privData.EventFunc = EventFunc; this->privData.eventCallback = func;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
} }
template <typename owner> template <typename Owner>
EventButton<owner>::~EventButton() 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;
} }
template <typename owner> template <typename Owner>
void EventButton<owner>::CheckCollision(InputClass *input) EventButton<Owner>::~EventButton()
{}
//Checks for collision and
template <typename Owner>
void EventButton<Owner>::Update(InputClass *input)
{ {
//??????????????? TODO: everything if(this->privData.enabled)
SendEvent(Button_Smashed); {
ButtonState currentState = ButtonState_None;
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;
}
}
} }
template <typename owner> //Only call the callback function when the state has changed.
void EventButton<owner>::SendEvent(IEventButton::ButtonState state) if(this->privData.previousState != currentState)
SendEvent(currentState);
this->privData.previousState = currentState;
}
}
template <typename Owner>
void EventButton<Owner>::SendEvent(ButtonState state)
{ {
ButtonEvent event; if(privData.eventCallback != NULL)
{
Oyster::Event::ButtonEvent<Owner> event;
event.state = state; event.state = state;
event.sender = this; event.sender = this;
event.owner = privData.owner; event.owner = privData.owner;
privData.EventFunc(event); event.userData = privData.userData;
privData.eventCallback(event);
}
} }
template <typename owner> template <typename Owner>
void EventButton<owner>::SetEventFunc(void (*EventFunc)( ButtonEvent e )) void EventButton<Owner>::SetUserData(void* data)
{
this->privData.userData = data;
}
template <typename Owner>
void EventButton<Owner>::SetEventFunc(EventFunc func)
{ {
this->privData.EventFunc = EventFunc; this->privData.EventFunc = EventFunc;
} }
template <typename owner> template <typename Owner>
unsigned int EventButton<owner>::GetID() void EventButton<Owner>::SetOwner(Owner owner)
{
this->privData.owner = owner;
}
template <typename Owner>
bool EventButton<Owner>::Enabled()
{
return this->privData.enabled;
}
template <typename Owner>
unsigned int EventButton<Owner>::GetID()
{ {
return this->privData.ID; return this->privData.ID;
} }
/* 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);
}
} }
} }

View File

@ -0,0 +1,64 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_BUTTON_CIRCLE_H
#define MISC_EVENT_BUTTON_CIRCLE_H
#include "EventButton.h"
#include "../../Input/L_inputClass.h"
namespace Oyster
{
namespace Event
{
template <typename Owner>
class EventButtonCircle : public EventButton<Owner>
{
public:
EventButtonCircle()
: EventButton(), xPos(0), yPos(0), radius(0)
{}
EventButtonCircle(Owner owner, float xPos, float yPos, float radius)
: EventButton(owner), xPos(xPos), yPos(yPos), radius(radius)
{}
EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), float xPos, float yPos, float radius)
: EventButton(EventFunc), xPos(xPos), yPos(yPos), radius(radius)
{}
EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, float xPos, float yPos, float radius)
: EventButton(EventFunc, owner), xPos(xPos), yPos(yPos), radius(radius)
{}
EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, void* userData, float xPos, float yPos, float radius)
: EventButton(EventFunc, owner, userData), xPos(xPos), yPos(yPos), radius(radius)
{}
~EventButtonCircle()
{}
//Circle vs point collision
bool Collision(InputClass* inputObject)
{
//Should come from the InputClass
float xMouse = 2, yMouse = 2;
float xDiff = xMouse - xPos;
float yDiff = yMouse - yPos;
float length = (xDiff * xDiff) + (yDiff * yDiff);
if(length <= radius*radius)
{
return true;
}
return false;
}
private:
float xPos, yPos;
float radius;
};
}
}
#endif

View File

@ -1,5 +1,11 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#include "EventButtonCollection.h" #include "EventButtonCollection.h"
#include "../../Input/L_inputClass.h"
using namespace Oyster::Event; using namespace Oyster::Event;
EventButtonCollection::EventButtonCollection() EventButtonCollection::EventButtonCollection()
@ -9,15 +15,21 @@ EventButtonCollection::EventButtonCollection()
EventButtonCollection::~EventButtonCollection() EventButtonCollection::~EventButtonCollection()
{ {
int size = buttons.size();
for(int i = 0; i < size; i++)
{
delete buttons[i];
buttons[i] = NULL;
}
} }
void EventButtonCollection::Update(InputClass* inputObject) void EventButtonCollection::Update(InputClass* inputObject)
{ {
if(this->collectionState == EventCollectionState_Enabled) if(this->collectionState == EventCollectionState_Enabled)
{ {
for(int i = 0; i < buttons.size(); i++) for(int i = 0; i < (int)buttons.size(); i++)
{ {
buttons.at(i)->CheckCollision(inputObject); buttons[i]->Update(inputObject);
} }
} }
} }

View File

@ -1,8 +1,14 @@
#ifndef EVENT_BUTTON_COLLECTION_H //////////////////////////////////////
#define EVENT_BUTTON_COLLECTION_H // Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_BUTTON_COLLECTION_H
#define MISC_EVENT_BUTTON_COLLECTION_H
#include "../../Input/L_inputClass.h" #include "../../Input/L_inputClass.h"
#include "../DynamicArray.h"
#include "IEventButton.h" #include "IEventButton.h"
#include "EventButton.h" #include "EventButton.h"
@ -30,20 +36,9 @@ namespace Oyster
void Update(InputClass* inputObject); void Update(InputClass* inputObject);
template <typename Owner> template <typename Owner>
void AddButton(EventButton<Owner>& button) void AddButton(EventButton<Owner>* button)
{ {
EventButton<Owner>* b = new EventButton<Owner>(); buttons.push_back(button);
buttons.size();
buttons.push_back((IEventButton*)b);
//buttons.push_back((IEventButton*)&button);
}
template <typename Owner>
EventButton<Owner>& CreateButton()
{
EventButton<Owner> temp;
buttons.push_back(&temp);
return temp;
} }
EventCollectionState GetState() const; EventCollectionState GetState() const;

View File

@ -0,0 +1,60 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_BUTTON_RECTANGLE_H
#define MISC_EVENT_BUTTON_RECTANGLE_H
#include "EventButton.h"
#include "../../Input/L_inputClass.h"
namespace Oyster
{
namespace Event
{
template <typename Owner>
class EventButtonRectangle : public EventButton<Owner>
{
public:
EventButtonRectangle()
: EventButton(), xPos(0), yPos(0), halfWidth(0), halfHeight(0)
{}
EventButtonRectangle(Owner owner, float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(owner), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
EventButtonRectangle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(EventFunc), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
EventButtonRectangle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(EventFunc, owner), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
EventButtonRectangle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, void* userData, float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(EventFunc, owner, userData), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
~EventButtonRectangle()
{}
//Circle vs point collision
bool Collision(InputClass* inputObject)
{
//Should come from the InputClass
float xMouse = 1, yMouse = 0;
if(xMouse >= xPos - halfWidth && xMouse <= xPos + halfWidth
&& yMouse >= yPos - halfHeight && yMouse <= yPos + halfHeight)
{
return true;
}
return false;
}
private:
float xPos, yPos;
float halfWidth, halfHeight;
};
}
}
#endif

View File

@ -1,3 +1,7 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#include "EventHandler.h" #include "EventHandler.h"
using namespace Oyster::Event; using namespace Oyster::Event;
@ -15,11 +19,16 @@ EventHandler::EventHandler()
EventHandler::~EventHandler() EventHandler::~EventHandler()
{ {
int size = collections.size();
for(int i = 0; i < size; i++)
{
delete collections[i];
}
} }
void EventHandler::Update(InputClass* inputObject) void EventHandler::Update(InputClass* inputObject)
{ {
for(int i = 0; i < collections.size(); i++) for(int i = 0; i < (int)collections.size(); i++)
{ {
collections.at(i)->Update(inputObject); collections.at(i)->Update(inputObject);
} }
@ -32,7 +41,7 @@ void EventHandler::AddCollection(EventButtonCollection& collection)
EventButtonCollection& EventHandler::CreateCollection() EventButtonCollection& EventHandler::CreateCollection()
{ {
EventButtonCollection temp; EventButtonCollection* temp = new EventButtonCollection;
collections.push_back(&temp); collections.push_back(temp);
return temp; return *temp;
} }

View File

@ -1,10 +1,16 @@
#ifndef EVENT_HANDLER_H //////////////////////////////////////
#define EVENT_HANDLER_H // Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_HANDLER_H
#define MISC_EVENT_HANDLER_H
#include "../../Input/L_inputClass.h" #include "../../Input/L_inputClass.h"
#include "EventButtonCollection.h" #include "EventButtonCollection.h"
#include "EventButton.h" #include "EventButton.h"
#include "EventButtonCircle.h"
#include "EventButtonRectangle.h"
#include <vector> #include <vector>

View File

@ -1,30 +1,38 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_IEVENT_BUTTON #ifndef MISC_IEVENT_BUTTON
#define MISC_IEVENT_BUTTON #define MISC_IEVENT_BUTTON
#include "../../Input/L_inputClass.h"
class InputClass;
namespace Oyster namespace Oyster
{ {
namespace Event namespace Event
{ {
enum ButtonState
{
ButtonState_None,
ButtonState_Hover,
ButtonState_Pressed,
ButtonState_Down,
ButtonState_Released,
};
class IEventButton class IEventButton
{ {
public: public:
enum ButtonState virtual ~IEventButton(){}
{
Button_Clicked,
Button_Hover,
Button_Hold,
Button_Smashed,
};
virtual void CheckCollision(InputClass *input) = 0; virtual void Update(InputClass *input){}
virtual void SendEvent(IEventButton::ButtonState state) = 0; virtual void SendEvent(ButtonState state){}
struct ButtonEvent; struct ButtonEvent;
virtual void SetEventFunc(void (*EventFunc)( ButtonEvent e )) = 0; virtual void SetEventFunc(void (*EventFunc)( ButtonEvent e )){}
virtual unsigned int GetID() = 0; virtual unsigned int GetID(){ return -1; }
}; };
} }

View File

@ -165,7 +165,9 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="DynamicArray.h" /> <ClInclude Include="DynamicArray.h" />
<ClInclude Include="EventHandler\EventButton.h" /> <ClInclude Include="EventHandler\EventButton.h" />
<ClInclude Include="EventHandler\EventButtonCircle.h" />
<ClInclude Include="EventHandler\EventButtonCollection.h" /> <ClInclude Include="EventHandler\EventButtonCollection.h" />
<ClInclude Include="EventHandler\EventButtonRectangle.h" />
<ClInclude Include="EventHandler\EventHandler.h" /> <ClInclude Include="EventHandler\EventHandler.h" />
<ClInclude Include="EventHandler\IEventButton.h" /> <ClInclude Include="EventHandler\IEventButton.h" />
<ClInclude Include="GID.h" /> <ClInclude Include="GID.h" />

View File

@ -134,5 +134,11 @@
<ClInclude Include="EventHandler\IEventButton.h"> <ClInclude Include="EventHandler\IEventButton.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="EventHandler\EventButtonCircle.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EventHandler\EventButtonRectangle.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>