From ae81b99958b33ce457dec269fbc035c54940484b Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Mon, 16 Jan 2012 20:20:01 +0200 Subject: [PATCH] [Add] Event history box. --- src/Unuk/Game.cpp | 2 ++ src/Unuk/Game.h | 3 ++ src/libUnuk/UI/EventHistory.cpp | 51 +++++++++++++++++++++++++++++++++ src/libUnuk/UI/EventHistory.h | 28 ++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/libUnuk/UI/EventHistory.cpp create mode 100644 src/libUnuk/UI/EventHistory.h diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp index f5f9c3a..255bed6 100644 --- a/src/Unuk/Game.cpp +++ b/src/Unuk/Game.cpp @@ -211,6 +211,8 @@ void Game::Render(void) { _npcHealth.RenderLiteral(); } + _eventHistory.Render(); + } else { _ingameMenu.Render(); } diff --git a/src/Unuk/Game.h b/src/Unuk/Game.h index ce6daa6..68b9ab0 100644 --- a/src/Unuk/Game.h +++ b/src/Unuk/Game.h @@ -16,6 +16,7 @@ #include "../libUnuk/System/Debug.h" #include "../libUnuk/UI/Text.h" #include "../libUnuk/UI/Bar.h" +#include "../libUnuk/UI/EventHistory.h" #include "../libUnuk/Engine/MemClass.h" using namespace std; @@ -59,4 +60,6 @@ private: Text _playerHealth; Bar _playerHealthBar; + + EventHistory _eventHistory; }; diff --git a/src/libUnuk/UI/EventHistory.cpp b/src/libUnuk/UI/EventHistory.cpp new file mode 100644 index 0000000..0ced2df --- /dev/null +++ b/src/libUnuk/UI/EventHistory.cpp @@ -0,0 +1,51 @@ +#include "EventHistory.h" +#include "../../Unuk/Constants.h" + +const int EventHistory::BOX_WIDTH = 350; +const int EventHistory::BOX_HEIGHT = 130; + +EventHistory::EventHistory(void) { + _background.SetXY(SCREEN_WIDTH/2 - BOX_WIDTH/2, 10); + _background.SetWidthHeight(BOX_WIDTH, BOX_HEIGHT); + _background.SetRGB(0, 0, 0); + + _text.SetXY(_background.GetX() + 5, _background.GetY() + 5); + + _visible = false; +} + +EventHistory::~EventHistory(void) { +} + +void EventHistory::LogEvent(const std::string& evtText) { + _events.push_back(evtText); + + if(_events.size() > 5) { + _events.erase(_events.begin()); + } + + std::string textStr; + for(std::list::iterator evIt = _events.begin(); evIt != _events.end(); ++evIt) { + textStr.append(*evIt); + textStr.append("\n"); + } + + _text.SetTextBlended(textStr, small, 255, 255, 255); + + _timeToVanish.Start(); + _visible = true; +} + +void EventHistory::Render(void) { + if(_visible && (_timeToVanish.GetTicks() >= 2500)) { + _timeToVanish.Stop(); + _visible = false; + } + + if(!_visible) { + return; + } + + _background.DrawLiteral(); + _text.RenderLiteral(); +} \ No newline at end of file diff --git a/src/libUnuk/UI/EventHistory.h b/src/libUnuk/UI/EventHistory.h new file mode 100644 index 0000000..c237a95 --- /dev/null +++ b/src/libUnuk/UI/EventHistory.h @@ -0,0 +1,28 @@ +#pragma once +#include +#include + +#include "Text.h" +#include "../System/Rect.h" +#include "../System/Timer.h" + +class EventHistory { +public: + EventHistory(void); + ~EventHistory(void); + + void LogEvent(const std::string& evtText); + + void Render(void); + +private: + std::list _events; + Text _text; + Rect _background; + + Timer _timeToVanish; + bool _visible; + + static const int BOX_WIDTH; + static const int BOX_HEIGHT; +}; \ No newline at end of file