diff --git a/src/gui/Window.cc b/src/gui/Window.cc index f35760b..47a1e1c 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -6,6 +6,8 @@ #define INITIAL_HEIGHT 500 #define FONT_SIZE 16 +bool Key_Statuses[SDL_NUM_SCANCODES]; + /** * Initialize SDL. * @@ -21,7 +23,7 @@ static bool Initialize_SDL() return true; } - if (SDL_Init(SDL_INIT_VIDEO) != 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { return false; } @@ -133,6 +135,26 @@ void Window::run_event_loop() } } +Uint32 Key_Repeat(Uint32 interval, void * param) +{ + if (Key_Statuses[(uintptr_t)param]) + { + SDL_Event event; + + event.user.code = 0; + event.user.data1 = param; + event.type = SDL_USEREVENT; + + SDL_PushEvent(&event); + + return 25u; + } + else + { + return 0u; + } +} + /** * Handle a SDL event. */ @@ -145,28 +167,33 @@ void Window::handle_event(SDL_Event & event) break; case SDL_KEYDOWN: - switch (event.key.keysym.sym) + Key_Statuses[event.key.keysym.scancode] = true; + switch (event.key.keysym.scancode) { - case SDLK_ESCAPE: + case SDL_SCANCODE_ESCAPE: m_exit_requested = true; break; - case SDLK_j: - if (m_line < m_buffer->get_num_lines() - 1) + case SDL_SCANCODE_J: + if (event.key.repeat == 0u) { - m_line++; - redraw(); + SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode); + scroll_down(); } break; - case SDLK_k: - if (m_line > 0) + case SDL_SCANCODE_K: + if (event.key.repeat == 0u) { - m_line--; - redraw(); + SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode); + scroll_up(); } break; } break; + case SDL_KEYUP: + Key_Statuses[event.key.keysym.scancode] = false; + break; + case SDL_WINDOWEVENT: switch (event.window.event) { @@ -179,6 +206,36 @@ void Window::handle_event(SDL_Event & event) break; } break; + + case SDL_USEREVENT: + switch ((uintptr_t)event.user.data1) + { + case SDL_SCANCODE_J: + scroll_down(); + break; + case SDL_SCANCODE_K: + scroll_up(); + break; + } + break; + } +} + +void Window::scroll_down() +{ + if (m_line < m_buffer->get_num_lines() - 1) + { + m_line++; + redraw(); + } +} + +void Window::scroll_up() +{ + if (m_line > 0) + { + m_line--; + redraw(); } } diff --git a/src/gui/Window.h b/src/gui/Window.h index fe20f0e..48ea124 100644 --- a/src/gui/Window.h +++ b/src/gui/Window.h @@ -16,6 +16,8 @@ protected: void resize(); void redraw(); void handle_event(SDL_Event & event); + void scroll_down(); + void scroll_up(); SDL_Window * m_window; bool m_exit_requested; diff --git a/wscript b/wscript index 5c409e9..d293d99 100644 --- a/wscript +++ b/wscript @@ -24,7 +24,7 @@ def build(bld): source = bld.path.ant_glob(["src/**/*.cc", "src/**/*.c", "libs/glcxx/src/glcxx/*"]), includes = includes, defines = ['GLCXX_GL_INCLUDE="gl3w.h"'] + defines, - cxxflags = ["-Wall", "-std=gnu++14", "-O2"], + cxxflags = ["-Wall", "-std=gnu++14", "-O2", "-Wno-switch"], lib = ["dl", "GL"], uselib = ["SDL2", "FreeType2"])