Project Alice
Loading...
Searching...
No Matches
gui_console.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "fif.hpp"
5
6namespace ui {
7
9protected:
10 // Vector list of last commands
11 std::vector<std::string> command_history;
12 // Index of the current command in the history
14
15public:
16 void render(sys::state& state, int32_t x, int32_t y) noexcept override;
17 void edit_box_update(sys::state& state, std::string_view s) noexcept override;
18 void edit_box_tab(sys::state& state, std::string_view s) noexcept override;
19 void edit_box_enter(sys::state& state, std::string_view s) noexcept override;
20 void edit_box_esc(sys::state& state) noexcept override;
21 void edit_box_backtick(sys::state& state) noexcept override;
22 void edit_box_back_slash(sys::state& state) noexcept override;
23 void edit_box_up(sys::state& state) noexcept override;
24 void edit_box_down(sys::state& state) noexcept override;
25 void add_to_history(sys::state& state, std::string s) noexcept {
26 // Add the command to the history, starting with the most recent command
27 command_history.insert(command_history.begin(), s);
28 // Reset the history index
29 history_index = 0;
30 }
31 std::string navigate_history(int16_t step) noexcept {
32 // Start with most recent command.
33 // If user presses up/down, go to the older/newer command, etc.
34 int16_t size = static_cast<int16_t>(command_history.size());
35 history_index += step;
36 if(history_index < 0) {
37 history_index = 0;
38 } else if(history_index >= size) {
40 }
41 if(history_index > 0 && history_index <= size) {
43 }
44 return "";
45 }
46 std::string down_history() noexcept {
47 return navigate_history(-1);
48 }
49 std::string up_history() noexcept {
50 return navigate_history(1);
51 }
52};
53
55public:
56 std::string raw_text;
57 bool text_pending = false;
58
59 void on_update(sys::state& state) noexcept override {
60 std::string new_content;
61 {
62 std::lock_guard lg{ state.lock_console_strings };
63 new_content = state.console_command_result;
64 state.console_command_result.clear();
65 }
66 if(new_content.size() > 0) {
67 raw_text += new_content;
68 text_pending = true;
69 }
70 if(text_pending) {
71 text_pending = false;
73 text::layout_parameters{ 10, 10, int16_t(base_data.size.x), int16_t(base_data.size.y),
74 base_data.data.text.font_handle, 0, text::alignment::left,
75 text::is_black_from_font_id(base_data.data.text.font_handle) ? text::text_color::black : text::text_color::white, false });
76 auto box = text::open_layout_box(contents);
78 text::close_layout_box(contents, box);
80 }
81 }
82};
83
85private:
86 console_list* console_output_list = nullptr;
87 console_edit* edit_box = nullptr;
88public:
89 void on_create(sys::state& state) noexcept override {
91 set_visible(state, false);
92 }
93
94 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
95 if(name == "console_list") {
96 auto ptr = make_element_by_type<console_list>(state, id);
97 console_output_list = ptr.get();
98 return ptr;
99 } else if(name == "console_edit") {
100 auto ptr = make_element_by_type<console_edit>(state, id);
101 edit_box = ptr.get();
102 return ptr;
103 } else {
104 return nullptr;
105 }
106 }
107
108 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
109 if(payload.holds_type<std::string>()) {
110 auto entry = any_cast<std::string>(payload);
111 console_output_list->raw_text += entry + "\\n";
112 console_output_list->text_pending = true;
113 console_output_list->impl_on_update(state);
115 } else if(payload.holds_type<console_edit*>()) {
116 //console_output_list->scroll_to_bottom(state);
118 } else {
120 }
121 }
122
123 void clear_list(sys::state& state) noexcept {
124 console_output_list->raw_text.clear();
125 console_output_list->impl_on_update(state);
126 }
127
128 static void show_toggle(sys::state& state);
129
130 void on_visible(sys::state& state) noexcept override {
131 //console_output_list->scroll_to_bottom(state);
132 state.ui_state.edit_target = edit_box;
133 }
134 void on_hide(sys::state& state) noexcept override {
135 state.ui_state.edit_target = nullptr;
136 }
137};
138
140std::string format_fif_value(sys::state& state, int64_t data, int32_t type);
141
142} // namespace ui
std::string down_history() noexcept
Definition: gui_console.hpp:46
void edit_box_update(sys::state &state, std::string_view s) noexcept override
Definition: gui_console.cpp:24
void edit_box_tab(sys::state &state, std::string_view s) noexcept override
Definition: gui_console.cpp:27
void edit_box_enter(sys::state &state, std::string_view s) noexcept override
void edit_box_down(sys::state &state) noexcept override
Definition: gui_console.cpp:38
std::string navigate_history(int16_t step) noexcept
Definition: gui_console.hpp:31
void edit_box_up(sys::state &state) noexcept override
Definition: gui_console.cpp:30
std::vector< std::string > command_history
Definition: gui_console.hpp:11
void add_to_history(sys::state &state, std::string s) noexcept
Definition: gui_console.hpp:25
void edit_box_back_slash(sys::state &state) noexcept override
std::string up_history() noexcept
Definition: gui_console.hpp:49
void edit_box_esc(sys::state &state) noexcept override
void edit_box_backtick(sys::state &state) noexcept override
void render(sys::state &state, int32_t x, int32_t y) noexcept override
Definition: gui_console.cpp:20
std::string raw_text
Definition: gui_console.hpp:56
void on_update(sys::state &state) noexcept override
Definition: gui_console.hpp:59
static void show_toggle(sys::state &state)
void on_visible(sys::state &state) noexcept override
void clear_list(sys::state &state) noexcept
void on_create(sys::state &state) noexcept override
Definition: gui_console.hpp:89
void on_hide(sys::state &state) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
Definition: gui_console.hpp:94
void impl_on_update(sys::state &state) noexcept override
virtual message_result get(sys::state &state, Cyto::Any &payload) noexcept
void set_visible(sys::state &state, bool vis)
void calibrate_scrollbar(sys::state &state) noexcept
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
multiline_text_element_base * delegate
void on_create(sys::state &state) noexcept override
layout_box open_layout_box(layout_base &dest, int32_t indent)
Definition: text.cpp:1799
void add_unparsed_text_to_layout_box(sys::state &state, layout_base &dest, layout_box &box, std::string_view sv, substitution_map const &mp)
Definition: text.cpp:1601
endless_layout create_endless_layout(sys::state &state, layout &dest, layout_parameters const &params)
Definition: text.cpp:1100
void close_layout_box(columnar_layout &dest, layout_box &box)
Definition: text.cpp:1807
std::string format_fif_value(sys::state &state, int64_t data, int32_t type)
void initialize_console_fif_environment(sys::state &state)
message_result
element_base * edit_target