浏览代码

Implement text manipulations under the cursor

- Insert before the cursor
- Delete character before and after the cursor
rexim 4 年之前
父节点
当前提交
a5a9d72dfb
共有 1 个文件被更改,包括 55 次插入23 次删除
  1. 55 23
      main.c

+ 55 - 23
main.c

@@ -106,9 +106,11 @@ void render_char(SDL_Renderer *renderer, const Font *font, char c, Vec2f pos, fl
         .h = (int) floorf(FONT_CHAR_HEIGHT * scale),
         .h = (int) floorf(FONT_CHAR_HEIGHT * scale),
     };
     };
 
 
-    assert(c >= ASCII_DISPLAY_LOW);
-    assert(c <= ASCII_DISPLAY_HIGH);
-    const size_t index = c - ASCII_DISPLAY_LOW;
+    size_t index = '?' - ASCII_DISPLAY_LOW;
+    if (ASCII_DISPLAY_LOW <= c && c <= ASCII_DISPLAY_HIGH) {
+        index = c - ASCII_DISPLAY_LOW;
+    }
+
     scc(SDL_RenderCopy(renderer, font->spritesheet, &font->glyph_table[index], &dst));
     scc(SDL_RenderCopy(renderer, font->spritesheet, &font->glyph_table[index], &dst));
 }
 }
 
 
@@ -134,17 +136,48 @@ void render_text_sized(SDL_Renderer *renderer, Font *font, const char *text, siz
     }
     }
 }
 }
 
 
-void render_text(SDL_Renderer *renderer, Font *font, const char *text, Vec2f pos, Uint32 color, float scale)
-{
-    render_text_sized(renderer, font, text, strlen(text), pos, color, scale);
-}
-
 #define BUFFER_CAPACITY 1024
 #define BUFFER_CAPACITY 1024
 
 
 char buffer[BUFFER_CAPACITY];
 char buffer[BUFFER_CAPACITY];
 size_t buffer_cursor = 0;
 size_t buffer_cursor = 0;
 size_t buffer_size = 0;
 size_t buffer_size = 0;
 
 
+void buffer_insert_text_before_cursor(const char *text)
+{
+    size_t text_size = strlen(text);
+    const size_t free_space = BUFFER_CAPACITY - buffer_size;
+    if (text_size > free_space) {
+        text_size = free_space;
+    }
+    memmove(buffer + buffer_cursor + text_size,
+            buffer + buffer_cursor,
+            buffer_size - buffer_cursor);
+    memcpy(buffer + buffer_cursor, text, text_size);
+    buffer_size += text_size;
+    buffer_cursor += text_size;
+}
+
+void buffer_backspace(void)
+{
+    if (buffer_cursor > 0 && buffer_size > 0) {
+        memmove(buffer + buffer_cursor - 1,
+                buffer + buffer_cursor,
+                buffer_size - buffer_cursor);
+        buffer_size -= 1;
+        buffer_cursor -= 1;
+    }
+}
+
+void buffer_delete(void)
+{
+    if (buffer_cursor < buffer_size && buffer_size > 0) {
+        memmove(buffer + buffer_cursor,
+                buffer + buffer_cursor + 1,
+                buffer_size - buffer_cursor);
+        buffer_size -= 1;
+    }
+}
+
 #define UNHEX(color) \
 #define UNHEX(color) \
     ((color) >> (8 * 0)) & 0xFF, \
     ((color) >> (8 * 0)) & 0xFF, \
     ((color) >> (8 * 1)) & 0xFF, \
     ((color) >> (8 * 1)) & 0xFF, \
@@ -172,7 +205,8 @@ void render_cursor(SDL_Renderer *renderer, const Font *font)
 }
 }
 
 
 
 
-// TODO: move the cursor around
+// TODO: Jump forward/backward by a word
+// TODO: Delete a word
 // TODO: Blinking cursor
 // TODO: Blinking cursor
 // TODO: Multiple lines
 // TODO: Multiple lines
 // TODO: Save/Load file
 // TODO: Save/Load file
@@ -182,6 +216,8 @@ int main(int argc, char **argv)
     (void) argc;
     (void) argc;
     (void) argv;
     (void) argv;
 
 
+    buffer_insert_text_before_cursor("Hello, World");
+
     scc(SDL_Init(SDL_INIT_VIDEO));
     scc(SDL_Init(SDL_INIT_VIDEO));
 
 
     SDL_Window *window =
     SDL_Window *window =
@@ -207,37 +243,33 @@ int main(int argc, char **argv)
             case SDL_KEYDOWN: {
             case SDL_KEYDOWN: {
                 switch (event.key.keysym.sym) {
                 switch (event.key.keysym.sym) {
                 case SDLK_BACKSPACE: {
                 case SDLK_BACKSPACE: {
-                    if (buffer_size > 0) {
-                        buffer_size -= 1;
-                        buffer_cursor = buffer_size;
-                    }
+                    buffer_backspace();
                 }
                 }
                 break;
                 break;
 
 
+                case SDLK_DELETE: {
+                    buffer_delete();
+                } break;
+
                 case SDLK_LEFT: {
                 case SDLK_LEFT: {
                     if (buffer_cursor > 0) {
                     if (buffer_cursor > 0) {
                         buffer_cursor -= 1;
                         buffer_cursor -= 1;
                     }
                     }
-                } break;
+                }
+                break;
 
 
                 case SDLK_RIGHT: {
                 case SDLK_RIGHT: {
                     if (buffer_cursor < buffer_size) {
                     if (buffer_cursor < buffer_size) {
                         buffer_cursor += 1;
                         buffer_cursor += 1;
                     }
                     }
-                } break;
+                }
+                break;
                 }
                 }
             }
             }
             break;
             break;
 
 
             case SDL_TEXTINPUT: {
             case SDL_TEXTINPUT: {
-                size_t text_size = strlen(event.text.text);
-                const size_t free_space = BUFFER_CAPACITY - buffer_size;
-                if (text_size > free_space) {
-                    text_size = free_space;
-                }
-                memcpy(buffer + buffer_size, event.text.text, text_size);
-                buffer_size += text_size;
-                buffer_cursor = buffer_size;
+                buffer_insert_text_before_cursor(event.text.text);
             }
             }
             break;
             break;
             }
             }