Update sample

This commit is contained in:
Dawid Drozd 2018-07-24 13:01:37 +02:00
parent 11a146bb91
commit 1e7500607b

View File

@ -2,30 +2,33 @@
* @brief Sample code to play with!
*/
#include <memory>
#include <iostream>
#include <string>
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <eventbus/EventBus.h>
#include <eventbus/EventCollector.h>
namespace Event // Example namespace for events
{
struct Gold { int value = 0; }; //Event that will be proceed when our gold changes
struct Gold // Event that will be proceed when our gold changes
{
int value = 0;
};
}
enum class Monster
{
Frog, Tux, Shark
Frog,
Tux,
Shark
};
class CharacterController // some kind of character controller
class Character
{
public:
CharacterController(const std::shared_ptr<Dexode::EventBus>& eventBus)
Character(const std::shared_ptr<Dexode::EventBus>& eventBus)
: _bus{eventBus}
{
}
@ -48,7 +51,7 @@ public:
}
private:
int _gold = 0;//Controller stores how much gold we have
int _gold = 0;
std::shared_ptr<Dexode::EventBus> _bus;
};
@ -65,12 +68,11 @@ public:
std::cout << "Gold:" << _gold << std::endl;
}
void onEnter()// We could also do such things in ctor and dtor but a lot of UI has something like this
void onEnter() // We could also do such things in ctor and dtor but a lot of UI has something
// like this
{
_listener.listen<Event::Gold>([this](const auto& event)
{
_gold = std::to_string(event.value);
});
_listener.listen<Event::Gold>(
[this](const auto& event) { _gold = std::to_string(event.value); });
}
void onExit()
@ -83,23 +85,17 @@ private:
Dexode::EventCollector _listener;
};
class ShopButton // Shop button is only enabled when we have some gold (odd decision but for sample good :P)
class ShopButton // Shop button is only enabled when we have some gold (odd decision but for sample
// good :P)
{
public:
ShopButton(const std::shared_ptr<Dexode::EventBus>& eventBus)
: _listener{eventBus}
{
}
void onEnter()
{
// We can use lambda or bind your choice
_listener.listen<Event::Gold>(std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
}
void onExit()
{
_listener.unlistenAll();//unlistenAll is also called when listener is destroyed
_listener.listen<Event::Gold>(
std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
// Also we use RAII idiom to handle unlisten
}
bool isEnabled() const
@ -120,19 +116,21 @@ private:
int main(int argc, char* argv[])
{
std::shared_ptr<Dexode::EventBus> eventBus = std::shared_ptr<Dexode::EventBus>(new Dexode::EventBus{});
std::shared_ptr<Dexode::EventBus> eventBus = std::make_shared<Dexode::EventBus>();
CharacterController characterController{eventBus};
Character characterController{eventBus};
UIWallet wallet{eventBus};//UIWallet doesn't know anything about character or even who store gold
UIWallet wallet{eventBus}; // UIWallet doesn't know anything about character
// or even who store gold
ShopButton shopButton{eventBus}; // ShopButton doesn't know anything about character
{
wallet.onEnter();
shopButton.onEnter();
}
wallet.onDraw();
{
characterController.kill(Monster::Tux);
}
wallet.onDraw();
// It is easy to test UI eg.
@ -141,5 +139,7 @@ int main(int argc, char* argv[])
eventBus->notify(Event::Gold{0});
assert(shopButton.isEnabled() == false);
wallet.onExit();
return 0;
}