summaryrefslogtreecommitdiffstats
path: root/src/game/client/toggles.cc
blob: 1b4696025b11fe84dba7f185207f5d7538b8bd4a (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2025 Kirill Dmitrievich
// File: toggles.cc
// Description: Debug and not-so-debug toggles

#include "client/pch.hh"

#include "client/toggles.hh"

#include "core/io/config_map.hh"

#include "client/gui/chat.hh"
#include "client/gui/language.hh"

#include "client/io/gamepad.hh"
#include "client/io/keyboard.hh"

#include "client/const.hh"
#include "client/globals.hh"

struct ToggleInfo final {
    std::string_view description;
    int glfw_keycode;
    bool is_enabled;
};

bool toggles::is_sequence_await = false;

static ToggleInfo toggle_infos[TOGGLE_COUNT];

static void print_toggle_state(const ToggleInfo& info)
{
    if(info.description.size()) {
        if(info.is_enabled) {
            client_chat::print(std::format("[toggles] {} ON", info.description));
        }
        else {
            client_chat::print(std::format("[toggles] {} OFF", info.description));
        }
    }
}

static void toggle_value(ToggleInfo& info, toggle_type type)
{
    if(info.is_enabled) {
        info.is_enabled = false;
        globals::dispatcher.trigger(ToggleDisabledEvent { type });
    }
    else {
        info.is_enabled = true;
        globals::dispatcher.trigger(ToggleEnabledEvent { type });
    }

    print_toggle_state(info);
}

static void on_key(const KeyEvent& 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.is_keycode(DEBUG_KEY)) {
        if(event.is_action(GLFW_PRESS)) {
            toggles::is_sequence_await = true;
            ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard;
            return;
        }

        if(event.is_action(GLFW_RELEASE)) {
            toggles::is_sequence_await = false;
            ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
            return;
        }
    }

    if((event.is_action(GLFW_PRESS)) && toggles::is_sequence_await) {
        if(event.is_keycode(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;
        }

        for(toggle_type i = 0; i < TOGGLE_COUNT; ++i) {
            if(event.is_keycode(toggle_infos[i].glfw_keycode)) {
                toggle_value(toggle_infos[i], i);
                return;
            }
        }
    }
}

void toggles::init(void)
{
    toggle_infos[TOGGLE_WIREFRAME].description = "wireframe";
    toggle_infos[TOGGLE_WIREFRAME].glfw_keycode = GLFW_KEY_Z;
    toggle_infos[TOGGLE_WIREFRAME].is_enabled = false;

    toggle_infos[TOGGLE_FULLBRIGHT].description = "fullbright";
    toggle_infos[TOGGLE_FULLBRIGHT].glfw_keycode = GLFW_KEY_J;
    toggle_infos[TOGGLE_FULLBRIGHT].is_enabled = false;

    toggle_infos[TOGGLE_CHUNK_AABB].description = "chunk Borders";
    toggle_infos[TOGGLE_CHUNK_AABB].glfw_keycode = GLFW_KEY_G;
    toggle_infos[TOGGLE_CHUNK_AABB].is_enabled = true;

    toggle_infos[TOGGLE_METRICS_UI].description = std::string_view();
    toggle_infos[TOGGLE_METRICS_UI].glfw_keycode = GLFW_KEY_V;
    toggle_infos[TOGGLE_METRICS_UI].is_enabled = false;

    toggle_infos[TOGGLE_USE_GAMEPAD].description = "gamepad input";
    toggle_infos[TOGGLE_USE_GAMEPAD].glfw_keycode = GLFW_KEY_P;
    toggle_infos[TOGGLE_USE_GAMEPAD].is_enabled = false;

    toggle_infos[TOGGLE_PM_FLIGHT].description = "flight mode";
    toggle_infos[TOGGLE_PM_FLIGHT].glfw_keycode = GLFW_KEY_F;
    toggle_infos[TOGGLE_PM_FLIGHT].is_enabled = false;

    globals::dispatcher.sink<KeyEvent>().connect<&on_key>();
}

void toggles::init_late(void)
{
    for(toggle_type i = 0; i < TOGGLE_COUNT; ++i) {
        if(toggle_infos[i].is_enabled) {
            globals::dispatcher.trigger(ToggleEnabledEvent { i });
        }
        else {
            globals::dispatcher.trigger(ToggleDisabledEvent { i });
        }
    }
}

bool toggles::get(toggle_type type)
{
    if(type < TOGGLE_COUNT) {
        return toggle_infos[type].is_enabled;
    }
    else {
        return false;
    }
}

void toggles::set(toggle_type type, bool value)
{
    if(type < TOGGLE_COUNT) {
        if(value) {
            toggle_infos[type].is_enabled = true;
            globals::dispatcher.trigger(ToggleEnabledEvent { type });
        }
        else {
            toggle_infos[type].is_enabled = false;
            globals::dispatcher.trigger(ToggleDisabledEvent { type });
        }

        print_toggle_state(toggle_infos[type]);
    }
}