Improve entry with cutting off text, vertical scroll, text selection, caret movement, copy, etc

This commit is contained in:
dec05eba
2025-08-06 02:03:48 +02:00
parent 8ed1fe4799
commit a9e118ea8f
6 changed files with 369 additions and 42 deletions

View File

@@ -5,9 +5,17 @@
#include <mglpp/graphics/Color.hpp>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Rectangle.hpp>
namespace gsr {
using EntryValidateHandler = std::function<bool(std::string &str)>;
class Entry;
enum class EntryValidateHandlerResult {
DENY,
ALLOW,
REPLACED
};
using EntryValidateHandler = std::function<EntryValidateHandlerResult(Entry &entry, const std::string &str)>;
class Entry : public Widget {
public:
@@ -20,19 +28,39 @@ namespace gsr {
mgl::vec2f get_size() override;
void set_text(std::string str);
EntryValidateHandlerResult set_text(std::string str);
const std::string& get_text() const;
// Also updates the cursor position
void replace_text(size_t index, size_t size, const std::string &replacement);
// Return false to specify that the string should not be accepted. This reverts the string back to its previous value.
// The input can be changed by changing the input parameter and returning true.
EntryValidateHandler validate_handler;
std::function<void(const std::string &text)> on_changed;
private:
EntryValidateHandlerResult set_text_internal(std::string str);
void draw_caret(mgl::Window &window, mgl::vec2f draw_pos, mgl::vec2f caret_size);
void draw_caret_selection(mgl::Window &window, mgl::vec2f draw_pos, mgl::vec2f caret_size);
mgl_index_codepoint_pair find_closest_caret_index_by_position(mgl::vec2f position);
private:
struct Caret {
float offset_x = 0.0f;
int utf8_index = 0;
int byte_index = 0;
};
mgl::Rectangle background;
mgl::Text text;
float max_width;
bool selected = false;
float caret_offset_x = 0.0f;
bool selecting_text = false;
bool selecting_with_keyboard = false;
bool show_selection = false;
Caret caret;
Caret selection_start_caret;
float text_overflow = 0.0f;
};
EntryValidateHandler create_entry_validator_integer_in_range(int min, int max);

View File

@@ -2,6 +2,7 @@
#include <mglpp/system/vec.hpp>
#include <mglpp/graphics/Color.hpp>
#include <mglpp/window/Window.hpp>
namespace mgl {
class Window;
@@ -14,4 +15,5 @@ namespace gsr {
void set_frame_delta_seconds(double frame_delta);
mgl::vec2f scale_keep_aspect_ratio(mgl::vec2f from, mgl::vec2f to);
mgl::vec2f clamp_keep_aspect_ratio(mgl::vec2f from, mgl::vec2f to);
mgl::Scissor scissor_get_sub_area(mgl::Scissor parent, mgl::Scissor child);
}