summaryrefslogtreecommitdiffstats
path: root/game/client/toggles.cc
blob: 7e208753ca494a79d4659e510357c26a336810e9 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "client/pch.hh"
#include "client/toggles.hh"

#include "core/config.hh"

#include "client/chat.hh"
#include "client/const.hh"
#include "client/gamepad.hh"
#include "client/glfw.hh"
#include "client/globals.hh"
#include "client/language.hh"

bool toggles::is_sequence_await = false;

bool toggles::draw_chunk_borders = false;
bool toggles::render_fullbright = false;
bool toggles::render_wireframe = false;

#if defined(NDEBUG)
bool toggles::draw_metrics = false;
#else
bool toggles::draw_metrics = true;
#endif

static void toggle_value(bool *value, const char *description)
{
    value[0] = !value[0];

    if(description) {
        if(value[0])
            client_chat::print(fmt::format("[debug] {} ON", description));
        else client_chat::print(fmt::format("[debug] {} OFF", description));
    }
}

static void toggle_value_config(ConfigBoolean &value, const char *description)
{
    value.set_value(!value.get_value());

    if(description) {
        if(value.get_value())
            client_chat::print(fmt::format("[debug] {} ON", description));
        else client_chat::print(fmt::format("[debug] {} OFF", description));
    }
}

static void on_glfw_key(const GlfwKeyEvent &event)
{
    if(globals::gui_keybind_ptr) {
        // The UI keybind subsystem has the authority
        // over debug toggles and it hogs the input keys
        return;
    }

    if(event.key == DEBUG_KEY) {
        if(event.action == GLFW_PRESS) {
            toggles::is_sequence_await = true;
            return;
        }

        if(event.action == GLFW_RELEASE) {
            toggles::is_sequence_await = false;
            return;
        }
    }

    if((event.action == GLFW_PRESS) && toggles::is_sequence_await) {
        switch(event.key) {
            case GLFW_KEY_G:
                toggle_value(&toggles::draw_chunk_borders, "chunk borders");
                return;
            case GLFW_KEY_V:
                toggle_value(&toggles::draw_metrics, nullptr);
                return;
            case GLFW_KEY_J:
                toggle_value(&toggles::render_fullbright, nullptr);
                return;
            case GLFW_KEY_Z:
                toggle_value(&toggles::render_wireframe, "wireframe");
                return;
            case GLFW_KEY_P:
                toggle_value_config(gamepad::active, "gamepad input");
                return;
            case GLFW_KEY_L:
                // This causes the language subsystem
                // to re-parse the JSON file essentially
                // causing the game to soft-reload language
                language::set(language::get_current());
                return;
        }
    }
}

void toggles::init(void)
{
    globals::dispatcher.sink<GlfwKeyEvent>().connect<&on_glfw_key>();
}