diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 74d5f049..ccb80d85 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -17,6 +17,9 @@ #include "vld.h" #include "GameClientRecieverFunc.h" +#include "../Misc/EventHandler/EventHandler.h" +using namespace Oyster::Event; + namespace DanBias { @@ -138,8 +141,6 @@ namespace DanBias HRESULT DanBiasGame::Update(float deltaTime) { - - m_data->inputObj->Update(); if(m_data->serverOwner) diff --git a/Code/Misc/EventHandler/EventButton.h b/Code/Misc/EventHandler/EventButton.h new file mode 100644 index 00000000..64215da2 --- /dev/null +++ b/Code/Misc/EventHandler/EventButton.h @@ -0,0 +1,261 @@ +/////////////////////// +// Sam Svensson 2013 // +/////////////////////// + +#ifndef MISC_EVENT_BUTTON_H +#define MISC_EVENT_BUTTON_H + +#include "IEventButton.h" + +namespace Oyster +{ + namespace Event + { + template + struct ButtonEvent + { + 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; + const unsigned int ID; + + 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(EventFunc func); + EventButton(EventFunc func, Owner owner); + EventButton(EventFunc func, Owner owner, void* userData); + ~EventButton(); + + void Update(InputClass *input); + + //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 + EventButton::EventButton() + { + this->privData.eventCallback = NULL; + this->privData.userData = NULL; + this->privData.previousState = ButtonState_None; + this->privData.enabled = true; + } + + template + EventButton::EventButton(Owner owner) + { + this->privData.owner = owner; + this->privData.eventCallback = NULL; + this->privData.userData = NULL; + this->privData.previousState = ButtonState_None; + this->privData.enabled = true; + } + + template + EventButton::EventButton(EventFunc func) + { + this->privData.eventCallback = func; + this->privData.userData = NULL; + this->privData.previousState = ButtonState_None; + this->privData.enabled = true; + } + + template + EventButton::EventButton(EventFunc func, Owner owner) + { + 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 + void EventButton::SetOwner(Owner owner) + { + this->privData.owner = owner; + } + + template + bool EventButton::Enabled() + { + return this->privData.enabled; + } + + 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); + } + } +} + + +#endif \ No newline at end of file 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 new file mode 100644 index 00000000..6a77c520 --- /dev/null +++ b/Code/Misc/EventHandler/EventButtonCollection.cpp @@ -0,0 +1,51 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + +#include "EventButtonCollection.h" + +#include "../../Input/L_inputClass.h" + +using namespace Oyster::Event; + +EventButtonCollection::EventButtonCollection() + : collectionState(EventCollectionState_Enabled) +{ +} + +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 < (int)buttons.size(); i++) + { + buttons[i]->Update(inputObject); + } + } +} + +EventCollectionState EventButtonCollection::GetState() const +{ + return collectionState; +} + +void EventButtonCollection::SetState(const EventCollectionState state) +{ + collectionState = state; +} + +void EventButtonCollection::Clear() +{ + buttons.clear(); + collectionState = EventCollectionState_Enabled; +} \ No newline at end of file diff --git a/Code/Misc/EventHandler/EventButtonCollection.h b/Code/Misc/EventHandler/EventButtonCollection.h new file mode 100644 index 00000000..0cc77b42 --- /dev/null +++ b/Code/Misc/EventHandler/EventButtonCollection.h @@ -0,0 +1,58 @@ +////////////////////////////////////// +// 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" + +#include + +namespace Oyster +{ + namespace Event + { + enum EventCollectionState + { + EventCollectionState_Disabled, + EventCollectionState_Enabled, + + EventCollectionState_Count, + EventCollectionState_Unknown = -1, + }; + + class EventButtonCollection + { + public: + EventButtonCollection(); + ~EventButtonCollection(); + + void Update(InputClass* inputObject); + + template + void AddButton(EventButton* button) + { + buttons.push_back(button); + } + + EventCollectionState GetState() const; + void SetState(const EventCollectionState state); + + //Clear all buttons and reset the state. + void Clear(); + + private: + std::vector buttons; + EventCollectionState collectionState; + + }; + } +} + +#endif \ No newline at end of file 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 new file mode 100644 index 00000000..a7347075 --- /dev/null +++ b/Code/Misc/EventHandler/EventHandler.cpp @@ -0,0 +1,47 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + +#include "EventHandler.h" + +using namespace Oyster::Event; + +Oyster::Event::EventHandler EvtHandler; + +EventHandler& EventHandler::Instance() +{ + return EvtHandler; +} + +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 < (int)collections.size(); i++) + { + collections.at(i)->Update(inputObject); + } +} + +void EventHandler::AddCollection(EventButtonCollection& collection) +{ + collections.push_back(&collection); +} + +EventButtonCollection& EventHandler::CreateCollection() +{ + 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 new file mode 100644 index 00000000..4ca1a9ab --- /dev/null +++ b/Code/Misc/EventHandler/EventHandler.h @@ -0,0 +1,41 @@ +////////////////////////////////////// +// 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 + +namespace Oyster +{ + namespace Event + { + class EventHandler + { + public: + EventHandler(); + ~EventHandler(); + + static EventHandler& Instance(); + + void Update(InputClass* inputObject); + + void AddCollection(EventButtonCollection& collection); + EventButtonCollection& CreateCollection(); + + private: + std::vector collections; + + }; + } +} + +#endif \ No newline at end of file diff --git a/Code/Misc/EventHandler/IEventButton.h b/Code/Misc/EventHandler/IEventButton.h new file mode 100644 index 00000000..901a8265 --- /dev/null +++ b/Code/Misc/EventHandler/IEventButton.h @@ -0,0 +1,41 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + +#ifndef MISC_IEVENT_BUTTON +#define MISC_IEVENT_BUTTON + +class InputClass; + +namespace Oyster +{ + namespace Event + { + enum ButtonState + { + ButtonState_None, + ButtonState_Hover, + ButtonState_Pressed, + ButtonState_Down, + ButtonState_Released, + }; + + class IEventButton + { + public: + virtual ~IEventButton(){} + + virtual void Update(InputClass *input){} + + virtual void SendEvent(ButtonState state){} + + struct ButtonEvent; + virtual void SetEventFunc(void (*EventFunc)( ButtonEvent e )){} + + virtual unsigned int GetID(){ return -1; } + + }; + } +} + +#endif \ No newline at end of file diff --git a/Code/Misc/Misc.vcxproj b/Code/Misc/Misc.vcxproj index 2d9392a3..593de858 100644 --- a/Code/Misc/Misc.vcxproj +++ b/Code/Misc/Misc.vcxproj @@ -146,6 +146,8 @@ + + @@ -164,6 +166,12 @@ + + + + + + diff --git a/Code/Misc/Misc.vcxproj.filters b/Code/Misc/Misc.vcxproj.filters index 8413642a..c50f2804 100644 --- a/Code/Misc/Misc.vcxproj.filters +++ b/Code/Misc/Misc.vcxproj.filters @@ -51,6 +51,12 @@ Source Files + + Source Files + + + Source Files + @@ -116,5 +122,23 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file