]> git.lizzy.rs Git - nothing.git/commitdiff
Merge pull request #1216 from kolumb/scrollbar
authorAlexey Kutepov <reximkut@gmail.com>
Sat, 21 Dec 2019 17:17:16 +0000 (00:17 +0700)
committerGitHub <noreply@github.com>
Sat, 21 Dec 2019 17:17:16 +0000 (00:17 +0700)
Add scrollbar to level picker

CMakeLists.txt
README.md
assets/levels/level-02.txt
src/game.c
src/ui/console.c
src/ui/console_log.c
src/ui/console_log.h
src/ui/edit_field.c

index 4bf7a9ee155b21d18eeda3d5e001e0d7892336f3..218247375ba39f97ba8bc805a91ed6ca898df0f7 100644 (file)
@@ -165,8 +165,11 @@ add_executable(nothing
 )
 target_link_libraries(nothing ${SDL2_LIBRARIES})
 
-ADD_CUSTOM_TARGET(link_assets ALL
-                  COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/assets ${CMAKE_BINARY_DIR}/assets)
+if(WIN32)
+    ADD_CUSTOM_TARGET(link_assets ALL COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/assets ${CMAKE_BINARY_DIR}/assets)
+else()
+    ADD_CUSTOM_TARGET(link_assets ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/assets ${CMAKE_BINARY_DIR}/assets)
+endif()
 
 if(("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang"))
   set(CMAKE_C_FLAGS
index 6e4d274a7f3ca704ee7a897bbb15a73c7c34fa11..db1beccf5b46cbd26b8cfa6885cd9b57f7bb4c36 100644 (file)
--- a/README.md
+++ b/README.md
@@ -154,6 +154,10 @@ $ ./nothing
 | `ESC`     | Exit console             |
 | `Enter`   | Evaluate the expression  |
 | `Up/Down` | Traverse console history |
+| `CTRL+L`  | Clear                    |
+| `CTRL+W`  | Cut                      |
+| `ALT+W`  | Copy                     |
+| `CTRL+Y`  | Paste                    |
 
 ### Level Editor
 
index af6a0c344a228d7cbbd8c8655fa37e57cc6e42b5..356933e0bfad26d1e7f6daed3dd3cf9094b9dd32 100644 (file)
@@ -14,10 +14,11 @@ lava_0 182.002197 589.132202 4415.973633 688.541870 d42b2b
 2
 back_platform_0 -185.010040 442.315765 519.166626 221.875031 80926d
 back_platform_0 1640.383789 420.668457 519.166626 221.875031 80926d
-4
+5
 box_0 120.543991 -72.326294 112.500031 107.291695 9aa034
 box_1 714.640747 516.840698 112.500031 107.291695 9aa034
 box_3 977.140808 521.007324 112.500031 107.291695 9aa034
 box_4 1263.946411 515.729614 112.500000 105.902771 9aa034
+box_0 1478.200684 515.248352 112.500000 105.902771 9aa034
 0
 0
index f355297802ca4f7fc0e7deb107681a93f35aba81..43767df50fe8914ad06366b44111708f0659d9b7 100644 (file)
@@ -202,7 +202,7 @@ int game_update(Game *game, float delta_time)
     trace_assert(game);
     trace_assert(delta_time > 0.0f);
 
-    // TODO: effective scale recalculation should be probably done only when the size of the window is changed
+    // TODO(#1218): effective scale recalculation should be probably done only when the size of the window is changed
     SDL_Rect view_port;
     SDL_RenderGetViewport(game->camera.renderer, &view_port);
     game->camera.effective_scale = effective_scale(&view_port);
index d4c2c7014987a76238ebbaac6713775d8f7cd876..909189561b6b792f4b5d9bb94e8264fd110ba882 100644 (file)
@@ -93,7 +93,6 @@ struct Console
 };
 
 /* TODO(#356): Console does not support autocompletion */
-/* TODO(#358): Console does not support copy, cut, paste operations */
 
 Console *create_console(Game *game)
 {
@@ -211,6 +210,13 @@ int console_handle_event(Console *console,
             }
         } break;
 
+       case SDLK_l: {
+            if (event->key.keysym.mod & KMOD_CTRL) {
+                console_log_clear(console->console_log);
+                return 0;
+            }
+        } break;
+
         case SDLK_DOWN:
             edit_field_replace(
                 console->edit_field,
index e5ee8efc1ccc6b86aa16a94a0c246b7606f778f7..d4dc7d998d30883bc7429fa120e4b2145b7f5e16 100644 (file)
@@ -107,3 +107,15 @@ int console_log_push_line(Console_Log *console_log,
 
     return 0;
 }
+
+void console_log_clear(Console_Log *console_log)
+{
+    trace_assert(console_log);
+    console_log->cursor = 0;
+    for (size_t i = 0; i < console_log->capacity; ++i) {
+        if (console_log->buffer[i]) {
+            free(console_log->buffer[i]);
+            console_log->buffer[i] = 0;
+        }
+    }
+}
index 705e93c45749e5041fe7f6c32058de493e2a6726..0c14cdb713d301203d44116a11c99c93aaf462fe 100644 (file)
@@ -19,4 +19,6 @@ int console_log_push_line(Console_Log *console_log,
                           const char *line_end,
                           Color color);
 
+void console_log_clear(Console_Log *console_log);
+
 #endif  // CONSOLE_LOG_H_
index ff193b3151db5256164ce67b9a49c73921ddb187..fa77390784938420cd9f09ddce95f640569f3f4c 100644 (file)
@@ -38,6 +38,9 @@ static void delete_backward_char(Edit_field *edit_field);
 static void kill_word(Edit_field *edit_field);
 static void backward_kill_word(Edit_field *edit_field);
 static void kill_to_end_of_line(Edit_field *edit_field);
+static void field_buffer_cut(Edit_field *edit_field);
+static void field_buffer_copy(Edit_field *edit_field);
+static void field_buffer_paste(Edit_field *edit_field);
 
 static void handle_keydown(Edit_field *edit_field, const SDL_Event *event);
 static void handle_keydown_alt(Edit_field *edit_field, const SDL_Event *event);
@@ -56,6 +59,15 @@ static void edit_field_insert_char(Edit_field *edit_field, char c)
     edit_field->buffer[++edit_field->buffer_size] = 0;
 }
 
+static inline
+void edit_field_insert_string(Edit_field *edit_field, const char *text)
+{
+    size_t n = strlen(text);
+    for (size_t i = 0; i < n; ++i) {
+        edit_field_insert_char(edit_field, text[i]);
+    }
+}
+
 // See: https://www.gnu.org/software/emacs/manual/html_node/emacs/Moving-Point.html
 // For an explanation of the naming terminology for these helper methods
 
@@ -201,6 +213,23 @@ static void kill_to_end_of_line(Edit_field *edit_field) {
                                 edit_field->buffer_size);
 }
 
+static void field_buffer_cut(Edit_field *edit_field) {
+    // "C-w"
+    SDL_SetClipboardText(edit_field_as_text(edit_field));
+    edit_field_clean(edit_field);
+}
+
+static void field_buffer_copy(Edit_field *edit_field) {
+    // "M-w"
+    SDL_SetClipboardText(edit_field_as_text(edit_field));
+}
+
+static void field_buffer_paste(Edit_field *edit_field) {
+    // "C-y"
+    char *text = SDL_GetClipboardText();
+    edit_field_insert_string(edit_field, text);
+}
+
 static void handle_keydown(Edit_field *edit_field, const SDL_Event *event)
 {
     switch (event->key.keysym.sym) {
@@ -251,6 +280,13 @@ static void handle_keydown_alt(Edit_field *edit_field, const SDL_Event *event)
     case SDLK_d: {
         kill_word(edit_field);
     } break;
+
+    // TODO(#1219): edit_field should also support more conventional copy/paste/cut keys like Ctrl+C,Ctrl+V,Ctrl+X
+    //   Emacs keybindings support is cool and all, but we also need to be more reflex inclusive.
+    // TODO(#1220): edit_field doesn't support selections for copy/cut operations
+    case SDLK_w: {
+        field_buffer_copy(edit_field);
+    } break;
     }
 }
 
@@ -296,6 +332,14 @@ static void handle_keydown_ctrl(Edit_field *edit_field, const SDL_Event *event)
     case SDLK_k: {
         kill_to_end_of_line(edit_field);
     } break;
+
+    case SDLK_w: {
+        field_buffer_cut(edit_field);
+    } break;
+
+    case SDLK_y: {
+        field_buffer_paste(edit_field);
+    } break;
     }
 }