From 1e7500607b42bff3632250f623888b95a503dfd0 Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Tue, 24 Jul 2018 13:01:37 +0200 Subject: [PATCH] Update sample --- sample/src/main.cpp | 88 ++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/sample/src/main.cpp b/sample/src/main.cpp index fe7ac5b..96dcf8b 100644 --- a/sample/src/main.cpp +++ b/sample/src/main.cpp @@ -2,45 +2,48 @@ * @brief Sample code to play with! */ -#include -#include -#include #include +#include +#include +#include #include #include -namespace Event //Example namespace for events +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& eventBus) - : _bus{eventBus} + Character(const std::shared_ptr& eventBus) + : _bus{eventBus} { } void kill(Monster monsterType) { - if (Monster::Frog == monsterType) + if(Monster::Frog == monsterType) { _gold += 1; } - else if (Monster::Tux == monsterType) + else if(Monster::Tux == monsterType) { _gold += 100; } - else if (Monster::Shark == monsterType) + else if(Monster::Shark == monsterType) { _gold += 25; } @@ -48,7 +51,7 @@ public: } private: - int _gold = 0;//Controller stores how much gold we have + int _gold = 0; std::shared_ptr _bus; }; @@ -56,21 +59,20 @@ class UIWallet { public: UIWallet(const std::shared_ptr& eventBus) - : _listener{eventBus} + : _listener{eventBus} { } - void onDraw()//Example "draw" of UI + void onDraw() // Example "draw" of UI { 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([this](const auto& event) - { - _gold = std::to_string(event.value); - }); + _listener.listen( + [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& eventBus) - : _listener{eventBus} + : _listener{eventBus} { - } - - void onEnter() - { - //We can use lambda or bind your choice - _listener.listen(std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1)); - } - - void onExit() - { - _listener.unlistenAll();//unlistenAll is also called when listener is destroyed + // We can use lambda or bind your choice + _listener.listen( + std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1)); + // Also we use RAII idiom to handle unlisten } bool isEnabled() const @@ -114,32 +110,36 @@ private: void onGoldUpdated(const Event::Gold& event) { _isEnabled = event.value > 0; - std::cout << "Shop button is:" << _isEnabled << std::endl;//some kind of logs + std::cout << "Shop button is:" << _isEnabled << std::endl; // some kind of logs } }; int main(int argc, char* argv[]) { - std::shared_ptr eventBus = std::shared_ptr(new Dexode::EventBus{}); + std::shared_ptr eventBus = std::make_shared(); - CharacterController characterController{eventBus}; + Character characterController{eventBus}; - 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 + 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(); } - characterController.kill(Monster::Tux); - + wallet.onDraw(); + { + characterController.kill(Monster::Tux); + } wallet.onDraw(); - //It is easy to test UI eg. + // It is easy to test UI eg. eventBus->notify(Event::Gold{1}); assert(shopButton.isEnabled() == true); eventBus->notify(Event::Gold{0}); assert(shopButton.isEnabled() == false); + wallet.onExit(); + return 0; }