Update
This commit is contained in:
@@ -17,8 +17,8 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
|
||||
editor->cursor_line_offset = 0;
|
||||
editor->cursor_pos = 0;
|
||||
editor->readOnly = 0;
|
||||
editor->rect.x = 0;
|
||||
editor->rect.y = 0;
|
||||
editor->rect.x = 2;
|
||||
editor->rect.y = 2;
|
||||
editor->rect.w = MAX_LINE_WIDTH * (font->size + 1);
|
||||
editor->rect.h = MAX_LINES_DISPLAY * (font->size + 4);
|
||||
editor->outRect = editor->rect;
|
||||
@@ -30,7 +30,7 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
|
||||
}
|
||||
|
||||
// Insert a new line at a specific position
|
||||
void insert_line(TextEditor *editor, int position, const char *text) {
|
||||
void insert_line(TextEditor *editor, int position, const char *text, SDL_Renderer *renderer) {
|
||||
if (editor->line_count >= MAX_LINES_ASM || position < 0 || position > editor->line_count) {
|
||||
printf("Invalid position or max lines reached!\n");
|
||||
return;
|
||||
@@ -47,11 +47,12 @@ void insert_line(TextEditor *editor, int position, const char *text) {
|
||||
editor->lines[position].active = 1;
|
||||
|
||||
editor->line_count++;
|
||||
generate_string_display(editor, renderer);
|
||||
}
|
||||
|
||||
void insert_line_rel(TextEditor *editor) {
|
||||
void insert_line_rel(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
editor->cursor_pos = 0;
|
||||
insert_line(editor, editor->cursor_line + 1, "");
|
||||
insert_line(editor, editor->cursor_line + 1, "", renderer);
|
||||
editor->cursor_line++;
|
||||
}
|
||||
|
||||
@@ -80,6 +81,46 @@ void insert_character(TextEditor *editor, char ch, SDL_Renderer *renderer) {
|
||||
generate_string_display(editor, renderer);
|
||||
}
|
||||
|
||||
void remove_character(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
if (editor->cursor_line < 0 || editor->cursor_line >= editor->line_count) {
|
||||
printf("Invalid cursor position!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (editor->cursor_pos == 0 && editor->line_count > 1) {
|
||||
// Remove the current line and shift lines up
|
||||
for (int i = editor->cursor_line; i < editor->line_count - 1; i++) {
|
||||
strcpy(editor->lines[i].text, editor->lines[i + 1].text);
|
||||
editor->lines[i].active = editor->lines[i + 1].active;
|
||||
}
|
||||
editor->lines[editor->line_count - 1].text[0] = '\0';
|
||||
editor->lines[editor->line_count - 1].active = 0;
|
||||
editor->line_count--;
|
||||
if (editor->cursor_line >= editor->line_count) {
|
||||
editor->cursor_line = editor->line_count - 1;
|
||||
}
|
||||
editor->cursor_pos = strlen(editor->lines[editor->cursor_line].text);
|
||||
} else {
|
||||
Line *line = &editor->lines[editor->cursor_line];
|
||||
int len = strlen(line->text);
|
||||
|
||||
if (editor->cursor_pos <= 0 || editor->cursor_pos > len) {
|
||||
printf("Position out of bounds!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Shift characters to the left to remove the character
|
||||
for (int i = editor->cursor_pos - 1; i < len; i++) {
|
||||
line->text[i] = line->text[i + 1];
|
||||
}
|
||||
|
||||
editor->cursor_pos--;
|
||||
}
|
||||
|
||||
generate_string_display(editor, renderer);
|
||||
}
|
||||
|
||||
|
||||
// Move cursor
|
||||
void move_cursor_relative(TextEditor *editor, int line_offset, int pos_offset) {
|
||||
int new_line = editor->cursor_line + line_offset;
|
||||
@@ -127,27 +168,41 @@ void generate_string_display(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
charRect.w = editor->font->size;
|
||||
charRect.h = editor->font->size;
|
||||
SDL_Rect outRect = charRect;
|
||||
outRect.x = 0;
|
||||
outRect.y = 0;
|
||||
outRect.x = 3;
|
||||
outRect.y = 2;
|
||||
|
||||
for (int i = editor->cursor_line_offset; i < end_line; i++) {
|
||||
if (editor->lines[i].active) {
|
||||
strcat(editor->displayString, editor->lines[i].text);
|
||||
char *linePTR = editor->lines[i].text;
|
||||
SDL_Rect cursorRect;
|
||||
cursorRect.x = 0;
|
||||
cursorRect.y = 0;
|
||||
cursorRect.w = 1;
|
||||
cursorRect.h = editor->font->size;
|
||||
|
||||
for (int line = editor->cursor_line_offset; line < end_line; line++) {
|
||||
if (editor->lines[line].active) {
|
||||
strcat(editor->displayString, editor->lines[line].text);
|
||||
char *linePTR = editor->lines[line].text;
|
||||
int charIndex = 0;
|
||||
while (*linePTR) {
|
||||
SDL_RenderCopy(renderer, editor->font->texture[*linePTR], &charRect, &outRect);
|
||||
outRect.x += charRect.w + 1;
|
||||
if (line == editor->cursor_line && charIndex == editor->cursor_pos - 1) {
|
||||
cursorRect.x = outRect.x;
|
||||
cursorRect.y = outRect.y;
|
||||
}
|
||||
charIndex++;
|
||||
linePTR++;
|
||||
}
|
||||
strcat(editor->displayString, "\n");
|
||||
outRect.x = 0;
|
||||
outRect.y += charRect.h + 4;
|
||||
outRect.x = 3;
|
||||
outRect.y += charRect.h + 1;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderFillRect(renderer, &cursorRect);
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
}
|
||||
|
||||
void editor_render(TextEditor *editor, SDL_Renderer * renderer) {
|
||||
void editor_render(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
SDL_RenderCopy(renderer, editor->texture, &editor->rect, &editor->outRect);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user