Archived
1
Fork 0

Add new console debug window, handle text input under sdl

This commit is contained in:
redstrate 2021-10-07 17:46:28 -04:00
parent 529bc27702
commit 9c8c257c43
12 changed files with 1165 additions and 526 deletions

View file

@ -2,6 +2,7 @@
#include <string_view>
void draw_console();
void draw_debug_ui();
void load_debug_options();

View file

@ -210,6 +210,12 @@ namespace prism {
*/
void process_mouse_down(int button, prism::Offset offset);
/**
* Called when text has been inputted. This is only emitted when text input is enabled thru input system
* @param string
*/
void process_text_input(const std::string_view string);
/** Adds a timer to the list of timers.
@param timer The timer to add.
@note The timer instance is passed by reference. Use this to keep track of your timers without having to query it's state back.
@ -282,6 +288,8 @@ namespace prism {
bool debug_enabled = false;
#endif
bool console_enabled = true;
private:
void setup_scene(Scene& scene);

View file

@ -1,5 +1,7 @@
#pragma once
#include <string_view>
namespace prism {
class imgui_backend {
public:
@ -14,6 +16,8 @@ namespace prism {
void process_key_down(unsigned int key_code);
void process_key_up(unsigned int key_code);
void process_text_input(const std::string_view string);
private:
bool mouse_buttons[3] = {};
};

View file

@ -81,8 +81,16 @@ namespace prism {
/// Returns all of the bindings registered with this Input system.
[[nodiscard]] std::vector<input_binding> get_bindings() const;
void begin_text_input();
void end_text_input();
bool is_text_input() const;
bool get_allowable_text_button(unsigned int keycode) const;
private:
std::vector<input_binding> _input_bindings;
std::tuple<int, int> _last_cursor_position;
bool _in_text_input = false;
};
}

View file

@ -12,6 +12,7 @@
#include "scene.hpp"
#include "renderer.hpp"
#include "file.hpp"
#include "console.hpp"
struct Options {
std::string shader_source_path;
@ -244,3 +245,15 @@ void save_debug_options() {
std::string_view get_shader_source_directory() {
return options.shader_source_path;
}
void draw_console() {
ImGui::Text("%s", prism::log_output.c_str());
static std::string console_input;
ImGui::InputText("##console_input", &console_input);
if(ImGui::Button("Input")) {
prism::console::parse_and_invoke_command(console_input);
console_input.clear();
}
}

View file

@ -425,10 +425,16 @@ void engine::resize(const int identifier, const prism::Extent extent) {
void engine::process_key_down(const unsigned int keyCode) {
Expects(keyCode >= 0);
if(input->is_text_input() && !input->get_allowable_text_button(keyCode))
return;
imgui->process_key_down(keyCode);
if(keyCode == platform::get_keycode(debug_button) && !ImGui::GetIO().WantTextInput)
debug_enabled = !debug_enabled;
if(keyCode == platform::get_keycode(InputButton::C))
console_enabled = !console_enabled;
}
void engine::process_key_up(const unsigned int keyCode) {
@ -604,6 +610,9 @@ void engine::begin_frame(const float delta_time) {
if(debug_enabled)
draw_debug_ui();
if(console_enabled)
draw_console();
if(app != nullptr)
app->begin_frame();
}
@ -798,3 +807,7 @@ void engine::setup_scene(Scene& scene) {
scene.reset_shadows();
scene.reset_environment();
}
void prism::engine::process_text_input(const std::string_view string) {
imgui->process_text_input(string);
}

View file

@ -6,6 +6,7 @@
#include "engine.hpp"
#include "platform.hpp"
#include "assertions.hpp"
#include "input.hpp"
using prism::imgui_backend;
@ -160,6 +161,17 @@ imgui_backend::imgui_backend() {
void imgui_backend::begin_frame(const float delta_time) {
ImGuiIO& io = ImGui::GetIO();
const bool imgui_wants_text = io.WantTextInput;
const bool input_is_text = ::engine->get_input()->is_text_input();
if(imgui_wants_text != input_is_text) {
if(imgui_wants_text) {
::engine->get_input()->begin_text_input();
} else {
::engine->get_input()->end_text_input();
}
}
const auto [width, height] = platform::get_window_size(0);
const auto [dw, dh] = platform::get_window_drawable_size(0);
@ -207,7 +219,6 @@ void imgui_backend::process_key_down(unsigned int key_code) {
Expects(key_code >= 0);
ImGuiIO& io = ImGui::GetIO();
io.AddInputCharactersUTF8(platform::translate_keycode(key_code));
io.KeysDown[key_code] = true;
}
@ -223,3 +234,8 @@ void imgui_backend::process_key_up(unsigned int key_code) {
void imgui_backend::process_mouse_down(int button) {
mouse_buttons[button] = true;
}
void prism::imgui_backend::process_text_input(const std::string_view string) {
ImGuiIO& io = ImGui::GetIO();
io.AddInputCharactersUTF8(string.data());
}

View file

@ -155,3 +155,24 @@ bool input_system::is_repeating(const std::string& name) {
std::vector<prism::input_binding> input_system::get_bindings() const {
return _input_bindings;
}
void input_system::begin_text_input() {
_in_text_input = true;
platform::begin_text_input();
}
void input_system::end_text_input() {
_in_text_input = false;
platform::end_text_input();
}
bool input_system::is_text_input() const {
return _in_text_input;
}
bool input_system::get_allowable_text_button(unsigned int keycode) const {
if(platform::get_keycode(InputButton::Backspace) == keycode)
return true;
return false;
}

View file

@ -3,8 +3,12 @@
#include <fmt/format.h>
namespace prism {
inline std::string log_output;
inline void vlog(fmt::string_view format, fmt::format_args args) {
fmt::vprint(format, args);
auto str = fmt::vformat(format, args);
fmt::print("{}", str);
log_output += str + "\n";
}
template <typename S, typename... Args>

View file

@ -159,6 +159,10 @@ namespace platform {
/// On platforms that support moue capture, this will lock the mouse cursor to the window and hide it.
void capture_mouse(const bool capture);
// TODO: right now the OS intercepting and saying "We dont want text input anymore" ala software keyboards is NOT supported yet
void begin_text_input();
void end_text_input();
/**Opens a file dialog to select a file. This will not block.
@param existing Whether or not to limit to existing files.
@param returnFunction The callback function when a file is selected or the dialog is cancelled. An empy string is returned when cancelled.

1467
extern/magic_enum/include/magic_enum.hpp vendored Executable file → Normal file

File diff suppressed because it is too large Load diff

View file

@ -264,7 +264,15 @@ void platform::save_dialog(std::function<void(std::string)> returnFunction) {
}
char* platform::translate_keycode(const unsigned int keycode) {
return const_cast<char*>(SDL_GetKeyName(keycode));
return const_cast<char*>(SDL_GetKeyName(SDL_GetKeyFromScancode((SDL_Scancode)keycode)));
}
void platform::begin_text_input() {
SDL_StartTextInput();
}
void platform::end_text_input() {
SDL_StopTextInput();
}
void platform::mute_output() {}
@ -320,7 +328,13 @@ int main(int argc, char* argv[]) {
engine->process_key_down(event.key.keysym.scancode);
}
break;
case SDL_KEYUP:
{
engine->process_key_up(event.key.keysym.scancode);
}
break;
case SDL_WINDOWEVENT:
{
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
auto window = get_window_by_sdl_id(event.window.windowID);
if(window != nullptr)
@ -328,7 +342,13 @@ int main(int argc, char* argv[]) {
} else if(event.window.event == SDL_WINDOWEVENT_CLOSE) {
engine->quit();
}
}
break;
case SDL_TEXTINPUT:
{
engine->process_text_input(event.text.text);
}
break;
}
}