Skip to content

Commit b3efe1e

Browse files
authored
Merge pull request BalazsJako#36 from pthom/bundle_fixes
Some changes from Dear ImGui Bundle
2 parents 112097c + 39f72d8 commit b3efe1e

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

TextEditor.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ void TextEditor::render(const char* title, const ImVec2& size, bool border) {
170170
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
171171
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4(palette.get(Color::background)));
172172
ImGui::BeginChild(title, size, border, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_NoNavInputs);
173+
lastRenderOrigin = ImGui::GetCursorScreenPos();
173174

174175
// handle keyboard and mouse inputs
175176
handleKeyboardInputs();
@@ -1306,6 +1307,30 @@ std::string TextEditor::getCursorText(size_t cursor) const {
13061307
}
13071308

13081309

1310+
//
1311+
// TextEditor::GetWordAtScreenPos
1312+
//
1313+
1314+
std::string TextEditor::GetWordAtScreenPos(const ImVec2& screenPos) const {
1315+
// Convert screen position to local coordinates using the origin saved during last Render()
1316+
auto local = screenPos - lastRenderOrigin;
1317+
1318+
// Convert to text coordinates using the same logic as handleMouseInteractions()
1319+
Coordinate glyphCoordinate;
1320+
Coordinate cursorCoordinate;
1321+
document.normalizeCoordinate(
1322+
local.y / glyphSize.y,
1323+
(local.x - textOffset) / glyphSize.x,
1324+
glyphCoordinate,
1325+
cursorCoordinate);
1326+
1327+
// Find word boundaries and extract text
1328+
auto start = document.findWordStart(glyphCoordinate);
1329+
auto end = document.findWordEnd(glyphCoordinate);
1330+
return document.getSectionText(start, end);
1331+
}
1332+
1333+
13091334
//
13101335
// TextEditor::makeCursorVisible
13111336
//
@@ -2355,12 +2380,12 @@ TextEditor::Palette TextEditor::defaultPalette = TextEditor::GetDarkPalette();
23552380
//
23562381

23572382
TextEditor::Coordinate TextEditor::Cursor::adjustCoordinateForInsert(Coordinate coordinate, Coordinate insertStart, Coordinate insertEnd) {
2358-
coordinate.line += insertEnd.line - insertStart.line;
2359-
2360-
if (end.line == coordinate.line) {
2383+
if (insertStart.line == coordinate.line) {
23612384
coordinate.column += insertEnd.column - insertStart.column;
23622385
}
23632386

2387+
coordinate.line += insertEnd.line - insertStart.line;
2388+
23642389
return coordinate;
23652390
}
23662391

@@ -6220,7 +6245,7 @@ bool TextEditor::CodePoint::isWhiteSpace(ImWchar codepoint) {
62206245

62216246
bool TextEditor::CodePoint::isWord(ImWchar codepoint) {
62226247
if (codepoint < 0x7f) {
6223-
return (static_cast<unsigned>((codepoint | 32) - 'a') < 26) || (static_cast<unsigned>(codepoint - '0') < 10);
6248+
return (static_cast<unsigned>((codepoint | 32) - 'a') < 26) || (static_cast<unsigned>(codepoint - '0') < 10) || codepoint == '_';
62246249

62256250
#if defined(IMGUI_USE_WCHAR32)
62266251
} else if (codepoint >= 0x10000) {

TextEditor.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ class TextEditor {
148148
inline void GetMainCursor(int& line, int& column) const { return getCursor(line, column, cursors.getMainIndex()); }
149149
inline void GetCurrentCursor(int& line, int& column) const { return getCursor(line, column, cursors.getCurrentIndex()); }
150150

151+
// Alternative API for cursor and selection position using lightweight out struct (line and column are zero-based)
152+
struct CursorPosition { int line = 0; int column = 0; };
153+
struct CursorSelection { CursorPosition start; CursorPosition end; };
154+
inline CursorPosition GetMainCursorPosition() const { CursorPosition p; getCursor(p.line, p.column, cursors.getMainIndex()); return p; }
155+
inline CursorPosition GetCurrentCursorPosition() const { CursorPosition p; getCursor(p.line, p.column, cursors.getCurrentIndex()); return p; }
156+
inline CursorPosition GetCursorPosition(size_t cursor) const { CursorPosition p; getCursor(p.line, p.column, cursor); return p; }
157+
inline CursorSelection GetCursorSelection(size_t cursor) const { CursorSelection s; getCursor(s.start.line, s.start.column, s.end.line, s.end.column, cursor); return s; }
158+
inline CursorSelection GetMainCursorSelection() const { return GetCursorSelection(cursors.getMainIndex()); }
159+
160+
// get the word at a screen position (e.g. from ImGui::GetMousePos()) - uses the origin saved during the last Render() call
161+
std::string GetWordAtScreenPos(const ImVec2& screenPos) const;
162+
151163
// scrolling support
152164
enum class Scroll {
153165
alignTop,
@@ -1281,6 +1293,7 @@ class TextEditor {
12811293
ImFont* font;
12821294
float fontSize;
12831295
ImVec2 glyphSize;
1296+
ImVec2 lastRenderOrigin;
12841297
float lineNumberLeftOffset;
12851298
float lineNumberRightOffset;
12861299
float decorationOffset;

0 commit comments

Comments
 (0)