Skip to content

Commit 963127c

Browse files
committed
fixes
1 parent 2a2b882 commit 963127c

File tree

1 file changed

+61
-50
lines changed

1 file changed

+61
-50
lines changed

src/editor/JulGameEditor/Components/TextEditor/TextEditor.jl

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -516,19 +516,6 @@ function setText(editor::TextEditor, text::AbstractString)
516516
colorizeAll(editor)
517517
end
518518

519-
function getText(editor::TextEditor)
520-
io = IOBuffer()
521-
for (i, line) in enumerate(editor.mLines)
522-
for glyph in line
523-
print(io, glyph.mChar)
524-
end
525-
if i < length(editor.mLines)
526-
print(io, '\n')
527-
end
528-
end
529-
return String(take!(io))
530-
end
531-
532519
function getText(editor::TextEditor, startCoords::Coordinates, endCoords::Coordinates)
533520
startCoords = sanitizeCoordinates(editor, startCoords)
534521
endCoords = sanitizeCoordinates(editor, endCoords)
@@ -538,30 +525,32 @@ function getText(editor::TextEditor, startCoords::Coordinates, endCoords::Coordi
538525
end
539526

540527
io = IOBuffer()
541-
start_char_idx = getCharIndex(editor, startCoords)
542-
end_char_idx = getCharIndex(editor, endCoords)
528+
start_char_idx = getCharIndex(editor, startCoords) # 0-based char index
529+
end_char_idx = getCharIndex(editor, endCoords) # 0-based char index
543530

544531
for line_idx = startCoords.mLine:endCoords.mLine
545-
line = editor.mLines[line_idx]
546-
line_text = join([g.mChar for g in line])
547-
548-
local_start = (line_idx == startCoords.mLine) ? start_char_idx : 0
549-
local_end = (line_idx == endCoords.mLine) ? end_char_idx : length(line_text)
550-
551-
if local_start < length(line_text) && local_start <= local_end
552-
# Adjust indices for Julia's 1-based string indexing if needed
553-
start_str_idx = nextind(line_text, 0, local_start + 1)
554-
end_str_idx = nextind(line_text, 0, local_end + 1) - 1 # End index is inclusive in substring
555-
556-
# Handle potential edge cases with indices
557-
if start_str_idx <= end_str_idx && start_str_idx > 0 && end_str_idx <= lastindex(line_text)
558-
print(io, SubString(line_text, start_str_idx, end_str_idx))
559-
elseif start_str_idx == end_str_idx + 1 # When range is empty
560-
# Print nothing
561-
elseif start_str_idx <= lastindex(line_text) # If only start is valid
562-
print(io, SubString(line_text, start_str_idx))
532+
line = editor.mLines[line_idx] # Line is Vector{Glyph}
533+
num_glyphs = length(line)
534+
535+
# Determine glyph indices (1-based) for this line
536+
# Range is [start_char_idx, end_char_idx) -> Glyphs [start_char_idx + 1, end_char_idx]
537+
glyph_start_idx = (line_idx == startCoords.mLine) ? start_char_idx + 1 : 1
538+
glyph_end_idx = (line_idx == endCoords.mLine) ? end_char_idx : num_glyphs # Inclusive end glyph index in the range
539+
540+
# Ensure indices are valid and the range is sensible
541+
glyph_start_idx = max(1, glyph_start_idx)
542+
# We want glyphs *up to* end_char_idx, so max index is end_char_idx.
543+
# If end_char_idx is 0 (start of line), glyph_end_idx becomes 0.
544+
# If line_idx != endCoords.mLine, glyph_end_idx is num_glyphs.
545+
glyph_end_idx = min(num_glyphs, glyph_end_idx)
546+
547+
if glyph_start_idx <= glyph_end_idx # Check if there's anything to print on this line
548+
for i = glyph_start_idx:glyph_end_idx
549+
# Check bounds just in case, though should be correct now
550+
if i > 0 && i <= num_glyphs
551+
print(io, line[i].mChar)
552+
end
563553
end
564-
565554
end
566555

567556
if line_idx < endCoords.mLine
@@ -1273,22 +1262,44 @@ function handleKeyboardInputs(editor::TextEditor)
12731262
alt = unsafe_load(io.KeyAlt)
12741263

12751264
# Process typed characters
1276-
if !isempty(unsafe_wrap(Array, io.InputQueueCharacters, Int(unsafe_load(io.InputQueueCharacters).Size)))
1277-
input_chars = unsafe_wrap(Array, io.InputQueueCharacters, Int(unsafe_load(io.InputQueueCharacters).Size))
1278-
for i = 1:length(input_chars)
1279-
char_code = input_chars[i]
1280-
if char_code == 0 || char_code == CImGui.ImGuiKey_Tab # Ignore null and Tab (handle Tab separately)
1281-
continue
1282-
end
1283-
# Check for newline chars (might be platform specific)
1284-
if char_code == UInt('\n') || char_code == UInt('\r')
1285-
enterCharacter(editor, '\n', shift)
1286-
elseif char_code > 0 && char_code < 0x10000 # Basic check for printable char range
1287-
enterCharacter(editor, Char(char_code), shift)
1288-
end
1265+
input_vec_ptr = io.InputQueueCharacters
1266+
if input_vec_ptr != C_NULL
1267+
input_vec = unsafe_load(input_vec_ptr)
1268+
input_size = Int(input_vec.Size) # Convert to Int
1269+
1270+
if input_size > 0 # Check size directly
1271+
input_data_ptr = input_vec.Data
1272+
if input_data_ptr != C_NULL # Ensure data pointer is valid
1273+
input_chars = unsafe_wrap(Array, input_data_ptr, input_size)
1274+
for i = 1:input_size # Iterate up to input_size
1275+
char_code = input_chars[i] # This is ImWchar (UInt16)
1276+
1277+
if char_code == 0 || char_code == CImGui.ImGuiKey_Tab # Ignore null and Tab
1278+
continue
1279+
end
1280+
1281+
# Check for newline chars (compare UInt16)
1282+
if char_code == UInt16('\n') || char_code == UInt16('\r')
1283+
enterCharacter(editor, '\n', shift)
1284+
# Check if it's a printable character (basic validity check)
1285+
elseif char_code >= 32 # Basic check, could be refined
1286+
try
1287+
# Convert ImWchar (UInt16) to Julia Char (UTF-8)
1288+
# This might need more robust UTF handling for complex cases (surrogates)
1289+
char = Char(char_code)
1290+
if isvalid(char)
1291+
enterCharacter(editor, char, shift)
1292+
end
1293+
catch e
1294+
@warn "Failed to convert character code $(repr(char_code)): $e"
1295+
end
1296+
end
1297+
end
1298+
1299+
# Clear the input queue *after* processing
1300+
CImGui.ClearInputCharacters() # Use the dedicated CImGui function
1301+
end
12891302
end
1290-
# Clear the input queue manually? CImGui might do this.
1291-
unsafe_store!(unsafe_load(io.InputQueueCharacters).Size, 0)
12921303
end
12931304

12941305
# Handle special keys
@@ -1402,7 +1413,7 @@ end
14021413

14031414
# Convert screen position (relative to text area top-left) to Coordinates
14041415
function screenPosToCoordinates(editor::TextEditor, pos::ImVec2)::Coordinates
1405-
relativePos = pos + ImVec2(editor.mScrollX, editor.mScrollY) # Adjust for scroll
1416+
relativePos = ImVec2(pos.x + editor.mScrollX, pos.y + editor.mScrollY) # Adjust for scroll
14061417
line = max(1, floor(Int, relativePos.y / editor.mLineHeight) + 1)
14071418
line = min(line, getLineCount(editor))
14081419

0 commit comments

Comments
 (0)