From 9a82b7002055cc769e30e934daa363eadcf050d9 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Tue, 11 Feb 2014 09:16:01 +0100 Subject: [PATCH] Misc - Button system working. (Logic) --- Code/Misc/EventHandler/EventButton.h | 273 +++++++++++++----- Code/Misc/EventHandler/EventButtonCircle.h | 64 ++++ .../EventHandler/EventButtonCollection.cpp | 16 +- .../Misc/EventHandler/EventButtonCollection.h | 25 +- Code/Misc/EventHandler/EventButtonRectangle.h | 60 ++++ Code/Misc/EventHandler/EventHandler.cpp | 17 +- Code/Misc/EventHandler/EventHandler.h | 10 +- Code/Misc/EventHandler/IEventButton.h | 32 +- Code/Misc/Misc.vcxproj | 2 + Code/Misc/Misc.vcxproj.filters | 6 + 10 files changed, 399 insertions(+), 106 deletions(-) create mode 100644 Code/Misc/EventHandler/EventButtonCircle.h create mode 100644 Code/Misc/EventHandler/EventButtonRectangle.h diff --git a/Code/Misc/EventHandler/EventButton.h b/Code/Misc/EventHandler/EventButton.h index e8fbc690..64215da2 100644 --- a/Code/Misc/EventHandler/EventButton.h +++ b/Code/Misc/EventHandler/EventButton.h @@ -4,125 +4,256 @@ #ifndef MISC_EVENT_BUTTON_H #define MISC_EVENT_BUTTON_H -#include "../../Input/L_inputClass.h" + +#include "IEventButton.h" namespace Oyster { namespace Event { - template - class EventButton + template + struct ButtonEvent { - public: - struct ButtonEvent - { - IEventButton::ButtonState state; - EventButton &sender; - owner owner; - }; + ButtonState state; + IEventButton* sender; + Owner owner; + void* userData; + }; + + template + class EventButton : public IEventButton + { + protected: + //typedef for callback function pointer + typedef void (*EventFunc)(Oyster::Event::ButtonEvent& e); struct PrivData { + PrivData() : ID(currID++){} + static unsigned int currID; - unsigned int ID; + const unsigned int ID; - owner owner; - void (*EventFunc)( ButtonEvent e ); + ButtonState previousState; + Owner owner; + EventFunc eventCallback; + void* userData; + bool enabled; }; - + + PrivData privData; + private: + //Implement this in the inherited classes for collision against that shape. + virtual bool Collision(InputClass *input) = 0; + public: EventButton(); - EventButton(owner owner); - EventButton(void (*EventFunc)( ButtonEvent e)); - EventButton(void (*EventFunc)( ButtonEvent e), owner owner); - + EventButton(Owner owner); + EventButton(EventFunc func); + EventButton(EventFunc func, Owner owner); + EventButton(EventFunc func, Owner owner, void* userData); ~EventButton(); - - void CheckCollision(InputClass *input); - void SendEvent(IEventButton::ButtonState state); + void Update(InputClass *input); - void SetEventFunc(void (*EventFunc)( ButtonEvent e )); //? + //Send event to callback function + void SendEvent(ButtonState state); + //Set + void SetUserData(void* data); + void SetEventFunc(EventFunc func); + void SetOwner(Owner owner); + + //Get + bool Enabled(); unsigned int GetID(); - + //EventFunc GetFunctionPointer(); + Owner GetOwner(); + + bool operator ==(const EventButton& obj); + }; - template - unsigned int EventButton::PrivData::currID = 0; + template + unsigned int EventButton::PrivData::currID = 0; - template - EventButton::EventButton() + template + EventButton::EventButton() { - this->privData.ID = privData.currID; - this->privData.currID += 1; - //this->privData.owner = NULL; - this->privData.EventFunc = NULL; + this->privData.eventCallback = NULL; + this->privData.userData = NULL; + this->privData.previousState = ButtonState_None; + this->privData.enabled = true; } - template - EventButton::EventButton(owner owner) + template + EventButton::EventButton(Owner owner) { - this->privData.ID = privData.currID; - this->privData.currID += 1; 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 - EventButton::EventButton(void (*EventFunc)( ButtonEvent e)) + template + EventButton::EventButton(EventFunc func) { - this->privData.ID = privData.currID; - this->privData.currID += 1; - //this->privData.owner = NULL; - this->privData.EventFunc = EventFunc; + this->privData.eventCallback = func; + this->privData.userData = NULL; + this->privData.previousState = ButtonState_None; + this->privData.enabled = true; } - template - EventButton::EventButton(void (*EventFunc)( ButtonEvent e), owner owner) + template + EventButton::EventButton(EventFunc func, Owner owner) { - this->privData.ID = privData.currID; - this->privData.currID += 1; this->privData.owner = owner; + this->privData.eventCallback = func; + this->privData.userData = NULL; + this->privData.previousState = ButtonState_None; + this->privData.enabled = true; + } + + template + EventButton::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 + EventButton::~EventButton() + {} + + //Checks for collision and + template + void EventButton::Update(InputClass *input) + { + if(this->privData.enabled) + { + 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; + } + } + } + + //Only call the callback function when the state has changed. + if(this->privData.previousState != currentState) + SendEvent(currentState); + + this->privData.previousState = currentState; + } + } + + template + void EventButton::SendEvent(ButtonState state) + { + if(privData.eventCallback != NULL) + { + Oyster::Event::ButtonEvent event; + event.state = state; + event.sender = this; + event.owner = privData.owner; + event.userData = privData.userData; + privData.eventCallback(event); + } + } + + template + void EventButton::SetUserData(void* data) + { + this->privData.userData = data; + } + + template + void EventButton::SetEventFunc(EventFunc func) + { this->privData.EventFunc = EventFunc; } - template - EventButton::~EventButton() + template + void EventButton::SetOwner(Owner owner) { - + this->privData.owner = owner; } - template - void EventButton::CheckCollision(InputClass *input) + template + bool EventButton::Enabled() { - //??????????????? TODO: everything - SendEvent(Button_Smashed); - } - - template - void EventButton::SendEvent(IEventButton::ButtonState state) - { - ButtonEvent event; - event.state = state; - event.sender = this; - event.owner = privData.owner; - privData.EventFunc(event); + return this->privData.enabled; } - template - void EventButton::SetEventFunc(void (*EventFunc)( ButtonEvent e )) - { - this->privData.EventFunc = EventFunc; - } - - template - unsigned int EventButton::GetID() + template + unsigned int EventButton::GetID() { return this->privData.ID; } + + /* Something is wrong, can't return EventFunc + template + EventFunc EventButton::GetFunctionPointer() + { + return this->privData.eventCallback; + }*/ + + template + Owner EventButton::GetOwner() + { + return this->privData.owner; + } + + template + bool EventButton::operator ==(const EventButton& obj) + { + return (this->privData.ID == obj.privData.ID); + } } } diff --git a/Code/Misc/EventHandler/EventButtonCircle.h b/Code/Misc/EventHandler/EventButtonCircle.h new file mode 100644 index 00000000..3a298acb --- /dev/null +++ b/Code/Misc/EventHandler/EventButtonCircle.h @@ -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 + class EventButtonCircle : public EventButton + { + 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& e), float xPos, float yPos, float radius) + : EventButton(EventFunc), xPos(xPos), yPos(yPos), radius(radius) + {} + EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent& e), Owner owner, float xPos, float yPos, float radius) + : EventButton(EventFunc, owner), xPos(xPos), yPos(yPos), radius(radius) + {} + EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent& 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 \ No newline at end of file diff --git a/Code/Misc/EventHandler/EventButtonCollection.cpp b/Code/Misc/EventHandler/EventButtonCollection.cpp index 7908816e..6a77c520 100644 --- a/Code/Misc/EventHandler/EventButtonCollection.cpp +++ b/Code/Misc/EventHandler/EventButtonCollection.cpp @@ -1,5 +1,11 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + #include "EventButtonCollection.h" +#include "../../Input/L_inputClass.h" + using namespace Oyster::Event; EventButtonCollection::EventButtonCollection() @@ -9,15 +15,21 @@ 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) { 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); } } } diff --git a/Code/Misc/EventHandler/EventButtonCollection.h b/Code/Misc/EventHandler/EventButtonCollection.h index 130d59fa..0cc77b42 100644 --- a/Code/Misc/EventHandler/EventButtonCollection.h +++ b/Code/Misc/EventHandler/EventButtonCollection.h @@ -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 "../DynamicArray.h" + #include "IEventButton.h" #include "EventButton.h" @@ -30,20 +36,9 @@ namespace Oyster void Update(InputClass* inputObject); template - void AddButton(EventButton& button) + void AddButton(EventButton* button) { - EventButton* b = new EventButton(); - buttons.size(); - buttons.push_back((IEventButton*)b); - //buttons.push_back((IEventButton*)&button); - } - - template - EventButton& CreateButton() - { - EventButton temp; - buttons.push_back(&temp); - return temp; + buttons.push_back(button); } EventCollectionState GetState() const; diff --git a/Code/Misc/EventHandler/EventButtonRectangle.h b/Code/Misc/EventHandler/EventButtonRectangle.h new file mode 100644 index 00000000..fc917437 --- /dev/null +++ b/Code/Misc/EventHandler/EventButtonRectangle.h @@ -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 + class EventButtonRectangle : public EventButton + { + 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& 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& 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& 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 \ No newline at end of file diff --git a/Code/Misc/EventHandler/EventHandler.cpp b/Code/Misc/EventHandler/EventHandler.cpp index 0d270997..a7347075 100644 --- a/Code/Misc/EventHandler/EventHandler.cpp +++ b/Code/Misc/EventHandler/EventHandler.cpp @@ -1,3 +1,7 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + #include "EventHandler.h" using namespace Oyster::Event; @@ -15,11 +19,16 @@ EventHandler::EventHandler() EventHandler::~EventHandler() { + int size = collections.size(); + for(int i = 0; i < size; i++) + { + delete collections[i]; + } } 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); } @@ -32,7 +41,7 @@ void EventHandler::AddCollection(EventButtonCollection& collection) EventButtonCollection& EventHandler::CreateCollection() { - EventButtonCollection temp; - collections.push_back(&temp); - return temp; + EventButtonCollection* temp = new EventButtonCollection; + collections.push_back(temp); + return *temp; } \ No newline at end of file diff --git a/Code/Misc/EventHandler/EventHandler.h b/Code/Misc/EventHandler/EventHandler.h index 7fb86b13..4ca1a9ab 100644 --- a/Code/Misc/EventHandler/EventHandler.h +++ b/Code/Misc/EventHandler/EventHandler.h @@ -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 "EventButtonCollection.h" #include "EventButton.h" +#include "EventButtonCircle.h" +#include "EventButtonRectangle.h" #include diff --git a/Code/Misc/EventHandler/IEventButton.h b/Code/Misc/EventHandler/IEventButton.h index 92c58c10..901a8265 100644 --- a/Code/Misc/EventHandler/IEventButton.h +++ b/Code/Misc/EventHandler/IEventButton.h @@ -1,30 +1,38 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + #ifndef MISC_IEVENT_BUTTON #define MISC_IEVENT_BUTTON -#include "../../Input/L_inputClass.h" + +class InputClass; namespace Oyster { namespace Event { + enum ButtonState + { + ButtonState_None, + ButtonState_Hover, + ButtonState_Pressed, + ButtonState_Down, + ButtonState_Released, + }; + class IEventButton { public: - enum ButtonState - { - Button_Clicked, - Button_Hover, - Button_Hold, - Button_Smashed, - }; + virtual ~IEventButton(){} - 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; - 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; } }; } diff --git a/Code/Misc/Misc.vcxproj b/Code/Misc/Misc.vcxproj index f3e58e56..e329e9cd 100644 --- a/Code/Misc/Misc.vcxproj +++ b/Code/Misc/Misc.vcxproj @@ -165,7 +165,9 @@ + + diff --git a/Code/Misc/Misc.vcxproj.filters b/Code/Misc/Misc.vcxproj.filters index edcf0e99..c50f2804 100644 --- a/Code/Misc/Misc.vcxproj.filters +++ b/Code/Misc/Misc.vcxproj.filters @@ -134,5 +134,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file