-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started working on the game menu, needed to make gui, cascaded into n…
…eeding to make an event system.
- Loading branch information
Showing
11 changed files
with
213 additions
and
68 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <memory> | ||
|
||
namespace graphics { | ||
|
||
class AbstractGuiObject | ||
{ | ||
|
||
protected: | ||
|
||
std::shared_ptr<AbstractGuiObject> parent_; | ||
|
||
std::vector<std::shared_ptr<AbstractGuiObject>> children_; | ||
|
||
AbstractGuiObject() = default; | ||
|
||
public: | ||
|
||
virtual ~AbstractGuiObject() = default; | ||
|
||
virtual void Initialize() = 0; | ||
|
||
virtual void Draw() = 0; | ||
|
||
virtual void Destroy() = 0; | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <memory> | ||
|
||
namespace util { | ||
|
||
template <typename EventData> class EventSubscriber | ||
{ | ||
|
||
public: | ||
|
||
EventSubscriber() = default; | ||
|
||
virtual ~EventSubscriber() = default; | ||
|
||
virtual void Update(const EventData &data) = 0; | ||
|
||
}; | ||
|
||
template <typename EventData> class Event | ||
{ | ||
|
||
std::vector<std::weak_ptr<EventSubscriber<EventData>>> subscribers_; | ||
|
||
public: | ||
|
||
Event() = default; | ||
|
||
~Event() = default; | ||
|
||
void Publish(const EventData &data) | ||
{ | ||
|
||
for (auto it = subscribers_.begin(); it != subscribers_.end(); ++it) | ||
{ | ||
if (auto shared = it->lock()) | ||
{ | ||
shared->Update(data); | ||
} | ||
else | ||
{ | ||
it = subscribers_.erase(it); | ||
} | ||
} | ||
} | ||
|
||
void Subscribe(const std::shared_ptr<EventSubscriber<EventData>> &subscriber) | ||
{ | ||
subscribers_.push_back(subscriber); | ||
} | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include <iostream> | ||
|
||
#include "event.h" | ||
|
||
namespace util { | ||
|
||
struct EventTestData | ||
{ | ||
float a; | ||
uint32_t b; | ||
std::string c; | ||
}; | ||
|
||
class EventTestSubscriber : public EventSubscriber<EventTestData> | ||
{ | ||
std::string name_; | ||
|
||
public: | ||
|
||
EventTestSubscriber(const std::string &name) : name_(name) {} | ||
|
||
virtual ~EventTestSubscriber() = default; | ||
|
||
virtual void Update(const EventTestData &data) override | ||
{ | ||
std::cout << "Published to " << name_ << ": " << data.a << " " << data.b << " " << data.c << "." << std::endl; | ||
} | ||
}; | ||
|
||
class EventTest | ||
{ | ||
|
||
public: | ||
|
||
EventTest() = default; | ||
|
||
~EventTest() = default; | ||
|
||
void Run() | ||
{ | ||
auto sub_0 = std::make_shared<EventTestSubscriber>("Olaf"); | ||
auto sub_1 = std::make_shared<EventTestSubscriber>("Jens"); | ||
auto sub_2 = std::make_shared<EventTestSubscriber>("Urban"); | ||
auto sub_3 = std::make_shared<EventTestSubscriber>("Ronja"); | ||
|
||
EventTestData data_0; | ||
data_0.a = 2.0f; | ||
data_0.b = 5; | ||
data_0.c = "Urgent Message"; | ||
|
||
Event<EventTestData> e; | ||
|
||
e.Subscribe(sub_0); | ||
e.Subscribe(sub_1); | ||
|
||
e.Publish(data_0); | ||
|
||
e.Subscribe(sub_2); | ||
e.Subscribe(sub_3); | ||
|
||
data_0.a = 1.0; | ||
data_0.b = 1000; | ||
data_0.c = "Unimportant Message"; | ||
|
||
e.Publish(data_0); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include "glm/glm.hpp" | ||
|
||
#include "abstract_gui_object.h" | ||
|
||
namespace graphics { | ||
|
||
class GuiCanvas : public AbstractGuiObject | ||
{ | ||
public: | ||
|
||
GuiCanvas(const glm::ivec2 &offset, const glm::ivec2 &size); | ||
|
||
~GuiCanvas(); | ||
|
||
}; | ||
} |
Oops, something went wrong.