diff -r e3a32a79a10a -r 4355e72ffd47 src/messagelog.cpp --- a/src/messagelog.cpp Wed Sep 25 11:02:44 2013 +0300 +++ b/src/messagelog.cpp Wed Oct 23 12:46:10 2013 +0300 @@ -21,16 +21,17 @@ #include "messagelog.h" #include "gldraw.h" #include "gui.h" +#include "moc_messagelog.cpp" -static const unsigned int g_maxMessages = 5; +static const int g_maxMessages = 5; static const int g_expiry = 5; static const int g_fadeTime = 500; // msecs // ============================================================================= // ----------------------------------------------------------------------------- MessageManager::MessageManager (QObject* parent) : - QObject (parent) { - m_ticker = new QTimer; + QObject (parent) +{ m_ticker = new QTimer; m_ticker->start (100); connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick())); } @@ -38,45 +39,45 @@ // ============================================================================= // ----------------------------------------------------------------------------- MessageManager::Line::Line (str text) : - text (text), - alpha (1.0f), - expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {} + text (text), + alpha (1.0f), + expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {} // ============================================================================= // Check this line's expiry and update alpha accordingly. Returns true if the // line is to still stick around, false if it expired. 'changed' is updated to // whether the line has somehow changed since the last update. // ----------------------------------------------------------------------------- -bool MessageManager::Line::update (bool& changed) { - changed = false; +bool MessageManager::Line::update (bool& changed) +{ changed = false; QDateTime now = QDateTime::currentDateTime(); int msec = now.msecsTo (expiry); - - if (now >= expiry) { - // Message line has expired + + if (now >= expiry) + { // Message line has expired changed = true; return false; } - - if (msec <= g_fadeTime) { - // Message line has not expired but is fading out - alpha = ((float) msec) / g_fadeTime; + + if (msec <= g_fadeTime) + { // Message line has not expired but is fading out + alpha = ( (float) msec) / g_fadeTime; changed = true; } - + return true; } // ============================================================================= // Add a line to the message manager. // ----------------------------------------------------------------------------- -void MessageManager::addLine (str line) { - // If there's too many entries, pop the excess out +void MessageManager::addLine (str line) +{ // If there's too many entries, pop the excess out while (m_lines.size() >= g_maxMessages) - m_lines.erase (0); - + m_lines.removeFirst(); + m_lines << Line (line); - + // Update the renderer view if (renderer()) renderer()->update(); @@ -86,40 +87,43 @@ // Ticks the message manager. All lines are ticked and the renderer scene is // redrawn if something changed. // ----------------------------------------------------------------------------- -void MessageManager::tick() { - if (m_lines.size() == 0) +void MessageManager::tick() +{ if (m_lines.isEmpty()) return; - + bool changed = false; - - for (uint i = 0; i < m_lines.size(); ++i) { - bool lineChanged; - + + for (int i = 0; i < m_lines.size(); ++i) + { bool lineChanged; + if (!m_lines[i].update (lineChanged)) - m_lines.erase (i--); - + m_lines.removeAt (i--); + changed |= lineChanged; } - + if (changed && renderer()) renderer()->update(); } // ============================================================================= // ----------------------------------------------------------------------------- -const List& MessageManager::getLines() const { - return m_lines; +const QList& MessageManager::getLines() const +{ return m_lines; } // ============================================================================= // log() interface - format the argument list and add the resulting string to // the main message manager. // ----------------------------------------------------------------------------- -void DoLog (std::initializer_list args) { - const str msg = DoFormat (args); - g_win->addMessage (msg); - - // Also print it to stdout - print ("%1\n", msg); +void DoLog (std::initializer_list args) +{ const str msg = DoFormat (args); + + for (str& a : msg.split ("\n", QString::SkipEmptyParts)) + { if (g_win) + g_win->addMessage (a); + + // Also print it to stdout + fprint (stdout, "%1\n", a); + } } -#include "moc_messagelog.cpp"