Commit a8535228 authored by Florian Oetke's avatar Florian Oetke
Browse files

tweaked debug console

parent 3ab74dd6
Subproject commit 26cc9aa8881064c6f43caac653d809d8717f4c4e
Subproject commit 047a45313c80155ade06f2a5f01e3dbc5ec826e3
......@@ -49,12 +49,13 @@ namespace mirrage::gui {
Gui& _gui;
util::Mailbox_collection _mailbox;
bool _show_console = false;
bool _focus_prompt = false;
bool _show_suggestions = false;
std::uint32_t _scroll_x = 0;
std::uint32_t _scroll_y = 0;
bool _scroll_lock = true;
bool _show_console = false;
bool _focus_prompt = false;
bool _show_suggestions = false;
int _selected_suggestion = -1;
std::uint32_t _scroll_x = 0;
std::uint32_t _scroll_y = 0;
bool _scroll_lock = true;
std::array<char, max_command_length> _command_input_buffer{};
int _command_input_length = 0;
util::Console_command_container _commands;
......
......@@ -58,7 +58,7 @@ namespace mirrage::gui {
struct Nk_renderer;
}
extern bool nk_interactive_text(struct nk_context* ctx, const char* str, int len);
extern auto nk_interactive_text(struct nk_context* ctx, const char* str, int len, nk_color color) -> int;
struct Gui_vertex {
......
......@@ -4,6 +4,7 @@
#include <mirrage/input/events.hpp>
#include <mirrage/utils/console_command.hpp>
#include <mirrage/utils/ranges.hpp>
namespace mirrage::gui {
......@@ -83,7 +84,7 @@ namespace mirrage::gui {
auto ctx = _gui.ctx();
if(nk_begin(ctx,
"debug_console",
nk_rect(50, 0, float(width), float(_show_suggestions ? 400 + 12 * 10 : 400)),
nk_rect(50, 0, float(width), float(_show_suggestions ? 400 + 12 * 5 : 400)),
NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_DYNAMIC)) {
nk_layout_row_dynamic(ctx, 360, 1);
......@@ -119,7 +120,9 @@ namespace mirrage::gui {
if(_focus_prompt) {
nk_edit_focus(ctx, 0);
_focus_prompt = false;
_focus_prompt = false;
ctx->text_edit.cursor = _command_input_length;
ctx->active->edit.cursor = _command_input_length;
}
nk_layout_row_dynamic(ctx, 30, 1);
......@@ -132,21 +135,64 @@ namespace mirrage::gui {
auto cmd = std::string_view(_command_input_buffer.data(), std::size_t(_command_input_length));
if(cmd_event & NK_EDIT_COMMITED) {
if(util::Console_command_container::call(cmd)) {
auto suggestions = util::Console_command_container::complete(cmd);
if(_selected_suggestion >= 0 && gsl::narrow<int>(suggestions.size()) > _selected_suggestion) {
auto s = suggestions[std::size_t(_selected_suggestion)];
auto name_sep = s->api().find(" ");
auto name_len = int(name_sep == std::string::npos ? s->api().size() : name_sep);
std::copy(s->api().begin(), s->api().begin() + name_len, _command_input_buffer.begin());
_command_input_length = name_len;
_focus_prompt = true;
_selected_suggestion = -1;
} else if(util::Console_command_container::call(cmd)) {
_command_input_length = 0;
}
} else if(cmd_event & NK_EDIT_ACTIVE && _selected_suggestion >= 0) {
for(auto i : util::range(static_cast<int>(NK_KEY_MAX))) {
if(i != NK_KEY_UP && i != NK_KEY_DOWN && i != NK_KEY_ENTER
&& nk_input_is_key_pressed(&ctx->input, static_cast<enum nk_keys>(i))) {
_selected_suggestion = -1;
break;
}
}
}
if(_show_suggestions) {
auto suggestions = util::Console_command_container::complete(cmd);
auto suggestions_size = gsl::narrow<int>(suggestions.size());
auto up_pressed = nk_input_is_key_pressed(&ctx->input, NK_KEY_UP);
auto down_pressed = nk_input_is_key_pressed(&ctx->input, NK_KEY_DOWN);
if(up_pressed == nk_true || down_pressed == nk_true || _selected_suggestion >= 0) {
if(up_pressed)
_selected_suggestion--;
else if(down_pressed)
_selected_suggestion++;
if(_selected_suggestion < 0)
_selected_suggestion = suggestions_size - 1;
else if(_selected_suggestion >= suggestions_size)
_selected_suggestion = 0;
}
nk_layout_row_dynamic(ctx, 12, 3);
for(auto& s : util::Console_command_container::complete(cmd)) {
auto any_clicked = false;
auto sep = s->api().find("|");
auto i = -1;
for(auto& s : suggestions) {
i++;
auto button_state = 0;
auto sep = s->api().find("|");
auto color = _selected_suggestion == i ? nk_color{255, 255, 255, 255}
: nk_color{180, 180, 180, 180};
auto name_sep = s->api().find(" ");
auto name_len = int(name_sep == std::string::npos ? s->api().size() : name_sep);
any_clicked |= nk_interactive_text(ctx, s->api().c_str(), name_len);
button_state |= nk_interactive_text(ctx, s->api().c_str(), name_len, color);
if(name_sep != std::string::npos) {
name_sep++;
......@@ -155,13 +201,13 @@ namespace mirrage::gui {
name_sep = space;
if(s->api()[name_sep] != '|')
any_clicked |= nk_interactive_text(
ctx, s->api().c_str() + name_sep, int(sep - name_sep));
button_state |= nk_interactive_text(
ctx, s->api().c_str() + name_sep, int(sep - name_sep), color);
else
any_clicked |= nk_interactive_text(ctx, "", 0);
button_state |= nk_interactive_text(ctx, "", 0, color);
} else {
any_clicked |= nk_interactive_text(ctx, "", 0);
button_state |= nk_interactive_text(ctx, "", 0, color);
}
if(sep != std::string::npos) {
......@@ -169,16 +215,18 @@ namespace mirrage::gui {
auto space = s->api().find_first_not_of(" ", sep);
if(space != std::string::npos)
sep = space;
any_clicked |=
nk_interactive_text(ctx, s->api().c_str() + sep, int(s->api().size() - sep));
button_state |= nk_interactive_text(
ctx, s->api().c_str() + sep, int(s->api().size() - sep), color);
} else
any_clicked |= nk_interactive_text(ctx, "", 0);
button_state |= nk_interactive_text(ctx, "", 0, color);
if(any_clicked && name_len > _command_input_length) {
if((button_state & NK_WIDGET_STATE_ACTIVE) && name_len > _command_input_length) {
std::copy(
s->api().begin(), s->api().begin() + name_len, _command_input_buffer.begin());
_command_input_length = name_len;
_focus_prompt = true;
} else if(button_state & NK_WIDGET_STATE_HOVERED) {
_selected_suggestion = i;
}
}
}
......
......@@ -20,10 +20,10 @@ extern void ref_embedded_assets_mirrage_gui();
namespace mirrage::gui {
bool nk_interactive_text(struct nk_context* ctx, const char* str, int len)
auto nk_interactive_text(struct nk_context* ctx, const char* str, int len, nk_color color) -> int
{
auto bounds = nk_widget_bounds(ctx);
nk_text(ctx, str, len, NK_TEXT_ALIGN_LEFT);
nk_text_colored(ctx, str, len, NK_TEXT_ALIGN_LEFT, color);
return nk_button_behavior(&ctx->last_widget_state, bounds, &ctx->input, NK_BUTTON_DEFAULT);
}
......@@ -134,6 +134,9 @@ namespace mirrage::gui {
case SDLK_RSHIFT:
case SDLK_LSHIFT: nk_input_key(_ctx, NK_KEY_SHIFT, down); return false;
case SDLK_RCTRL:
case SDLK_LCTRL: nk_input_key(_ctx, NK_KEY_CTRL, down); return false;
case SDLK_DELETE: nk_input_key(_ctx, NK_KEY_DEL, down); return false;
case SDLK_RETURN: nk_input_key(_ctx, NK_KEY_ENTER, down); return false;
......@@ -164,6 +167,8 @@ namespace mirrage::gui {
case SDLK_b: nk_input_key(_ctx, NK_KEY_TEXT_LINE_START, down && ctrl); return false;
case SDLK_e: nk_input_key(_ctx, NK_KEY_TEXT_LINE_END, down && ctrl); return false;
case SDLK_a: nk_input_key(_ctx, NK_KEY_TEXT_SELECT_ALL, down && ctrl); return false;
case SDLK_LEFT:
nk_input_key(_ctx, NK_KEY_TEXT_WORD_LEFT, down && ctrl);
nk_input_key(_ctx, NK_KEY_LEFT, down && !ctrl);
......@@ -174,6 +179,9 @@ namespace mirrage::gui {
nk_input_key(_ctx, NK_KEY_RIGHT, down && !ctrl);
return false;
case SDLK_UP: nk_input_key(_ctx, NK_KEY_UP, down); return false;
case SDLK_DOWN: nk_input_key(_ctx, NK_KEY_DOWN, down); return false;
default: return true;
}
}
......@@ -186,6 +194,9 @@ namespace mirrage::gui {
if(!_grab_inputs)
return true;
if(evt.type == SDL_KEYDOWN && evt.key.repeat != 0) {
handle_key(false, evt.key.keysym.sym);
}
handle_key(evt.type == SDL_KEYDOWN, evt.key.keysym.sym);
return evt.key.keysym.sym == SDLK_ESCAPE;
......
......@@ -10,6 +10,8 @@
#include <mirrage/utils/maybe.hpp>
#include <mirrage/utils/template_utils.hpp>
#include <sf2/sf2.hpp>
#include <algorithm>
#include <cctype>
#include <charconv>
......@@ -68,6 +70,12 @@ namespace mirrage::util {
else
return v;
} else if constexpr(sf2::is_annotated_struct<T>()) {
return sf2::deserialize_json<T>(std::stringstream(std::string(view)));
} else if constexpr(sf2::is_annotated_enum<T>()) {
return sf2::get_enum_info<T>().value_of(view);
} else {
(void) view;
static_assert(util::dependent_false<T>(), "No matching from_string() for type T.");
......
......@@ -49,6 +49,9 @@ namespace mirrage::util {
std::sort(
begin(matches), end(matches), [](auto& lhs, auto& rhs) { return lhs->name() < rhs->name(); });
if(matches.size() > 5)
matches.resize(5);
return matches;
}
} // namespace mirrage::util
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment