summaryrefslogtreecommitdiffstats
path: root/src/timeman.cpp
blob: f043894f708b43b311d956f055d62cf3654c4276 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "timeman.hpp"
#include "keybind.hpp"
#include "window.hpp"
#include "game.hpp"

unit::Time TimeMan::m_time(0);
unit::Time TimeMan::m_step(unit::DAY_SECONDS);
bool TimeMan::m_auto;
bool TimeMan::m_changed;

void
TimeMan::init()
{
    KeyMan::registerBind('.', BIND_TIMEMAN_STEP, CTX_TIMEMAN, "Move time ahead by a step");
    KeyMan::registerBind('+', BIND_TIMEMAN_INCSTEP, CTX_TIMEMAN, "Increase the timestep");
    KeyMan::registerBind('-', BIND_TIMEMAN_DECSTEP, CTX_TIMEMAN, "Decrease the timestep");
    KeyMan::registerBind('a', BIND_TIMEMAN_TOGGLEAUTO, CTX_TIMEMAN, "Toggle if time will move automatically");
    m_changed = true;
}

void 
TimeMan::update(int c)
{
    WindowContext &context = Game::contexts();
    m_changed = false;
    if(!Game::paused()) {
        if(m_auto) {
            m_time += (m_step);
            m_changed = true;
        }
    }
    if(context.getFocusedString() != WINDOW_TIMEMAN_ID) return;
    if(c == KeyMan::binds[BIND_TIMEMAN_INCSTEP].code) m_step = unit::Time(std::max<long>(1, m_step() * 2));
    if(c == KeyMan::binds[BIND_TIMEMAN_DECSTEP].code) m_step = unit::Time(std::max<long>(1, m_step() / 2));
    if(c == KeyMan::binds[BIND_TIMEMAN_TOGGLEAUTO].code) m_auto = !m_auto;
    if(c == KeyMan::binds[BIND_TIMEMAN_STEP].code && !Game::paused()) { m_time += m_step; m_changed = true; }
}

void 
TimeMan::draw()
{
    WindowContext &context = Game::contexts();
    Window &timeWindow = context[WINDOW_TIMEMAN_ID];
    timeWindow << straw::clear(' ');

    timeWindow << straw::move(0, 0) << straw::resetcolor() << m_time.format("%S %D, %C \n%H:%m\n\n");
    timeWindow << m_step.format("Step:\n%Y Years, %M Months\n%D Days, %H Hours\n%m Minutes, %s Seconds\n\n");
    if(m_auto) timeWindow << "Auto";
}