separate input actions with modes
parent
73bb0035ec
commit
be84eaba64
|
@ -1,6 +1,7 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import "core:runtime"
|
import "core:runtime"
|
||||||
|
import "core:reflect"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "vendor:sdl2"
|
import "vendor:sdl2"
|
||||||
import lua "vendor:lua/5.4"
|
import lua "vendor:lua/5.4"
|
||||||
|
@ -118,7 +119,13 @@ new_input_map :: proc() -> InputMap {
|
||||||
input_map := InputMap {
|
input_map := InputMap {
|
||||||
mode = make(map[Mode]InputActions),
|
mode = make(map[Mode]InputActions),
|
||||||
}
|
}
|
||||||
input_map.mode[.Normal] = new_input_actions();
|
|
||||||
|
ti := runtime.type_info_base(type_info_of(Mode));
|
||||||
|
if v, ok := ti.variant.(runtime.Type_Info_Enum); ok {
|
||||||
|
for i in &v.values {
|
||||||
|
input_map.mode[(cast(^Mode)(&i))^] = new_input_actions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return input_map;
|
return input_map;
|
||||||
}
|
}
|
||||||
|
|
|
@ -778,7 +778,7 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, sho
|
||||||
cursor_y -= begin * state.source_font_height;
|
cursor_y -= begin * state.source_font_height;
|
||||||
|
|
||||||
// draw cursor
|
// draw cursor
|
||||||
if state.mode == .Normal {
|
if state.mode == .Normal || state.mode == .Visual {
|
||||||
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Background4);
|
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Background4);
|
||||||
} else if state.mode == .Insert {
|
} else if state.mode == .Insert {
|
||||||
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Green);
|
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Green);
|
||||||
|
|
|
@ -84,18 +84,18 @@ do_visual_mode :: proc(state: ^State, buffer: ^FileBuffer) {
|
||||||
|
|
||||||
register_default_leader_actions :: proc(input_map: ^core.InputActions) {
|
register_default_leader_actions :: proc(input_map: ^core.InputActions) {
|
||||||
core.register_key_action(input_map, .Q, proc(state: ^State) {
|
core.register_key_action(input_map, .Q, proc(state: ^State) {
|
||||||
state.current_input_map = &state.input_map.mode[.Normal];
|
state.current_input_map = &state.input_map.mode[state.mode];
|
||||||
}, "close this help");
|
}, "close this help");
|
||||||
}
|
}
|
||||||
|
|
||||||
register_default_go_actions :: proc(input_map: ^core.InputActions) {
|
register_default_go_actions :: proc(input_map: ^core.InputActions) {
|
||||||
core.register_key_action(input_map, .H, proc(state: ^State) {
|
core.register_key_action(input_map, .H, proc(state: ^State) {
|
||||||
core.move_cursor_start_of_line(&state.buffers[state.current_buffer]);
|
core.move_cursor_start_of_line(&state.buffers[state.current_buffer]);
|
||||||
state.current_input_map = &state.input_map.mode[.Normal];
|
state.current_input_map = &state.input_map.mode[state.mode];
|
||||||
}, "move to beginning of line");
|
}, "move to beginning of line");
|
||||||
core.register_key_action(input_map, .L, proc(state: ^State) {
|
core.register_key_action(input_map, .L, proc(state: ^State) {
|
||||||
core.move_cursor_end_of_line(&state.buffers[state.current_buffer]);
|
core.move_cursor_end_of_line(&state.buffers[state.current_buffer]);
|
||||||
state.current_input_map = &state.input_map.mode[.Normal];
|
state.current_input_map = &state.input_map.mode[state.mode];
|
||||||
}, "move to end of line");
|
}, "move to end of line");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,8 +153,14 @@ register_default_input_actions :: proc(input_map: ^core.InputActions) {
|
||||||
}, "decrease font size");
|
}, "decrease font size");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inserting Text
|
core.register_key_action(input_map, .SPACE, core.new_input_actions(), "leader commands");
|
||||||
{
|
register_default_leader_actions(&(&input_map.key_actions[.SPACE]).action.(core.InputActions));
|
||||||
|
|
||||||
|
core.register_key_action(input_map, .G, core.new_input_actions(), "Go commands");
|
||||||
|
register_default_go_actions(&(&input_map.key_actions[.G]).action.(core.InputActions));
|
||||||
|
}
|
||||||
|
|
||||||
|
register_default_text_input_actions :: proc(input_map: ^core.InputActions) {
|
||||||
core.register_key_action(input_map, .I, proc(state: ^State) {
|
core.register_key_action(input_map, .I, proc(state: ^State) {
|
||||||
state.mode = .Insert;
|
state.mode = .Insert;
|
||||||
sdl2.StartTextInput();
|
sdl2.StartTextInput();
|
||||||
|
@ -177,13 +183,6 @@ register_default_input_actions :: proc(input_map: ^core.InputActions) {
|
||||||
}, "insert mode on newline");
|
}, "insert mode on newline");
|
||||||
}
|
}
|
||||||
|
|
||||||
core.register_key_action(input_map, .SPACE, core.new_input_actions(), "leader commands");
|
|
||||||
register_default_leader_actions(&(&input_map.key_actions[.SPACE]).action.(core.InputActions));
|
|
||||||
|
|
||||||
core.register_key_action(input_map, .G, core.new_input_actions(), "Go commands");
|
|
||||||
register_default_go_actions(&(&input_map.key_actions[.G]).action.(core.InputActions));
|
|
||||||
}
|
|
||||||
|
|
||||||
load_plugin :: proc(info: os.File_Info, in_err: os.Errno, state: rawptr) -> (err: os.Errno, skip_dir: bool) {
|
load_plugin :: proc(info: os.File_Info, in_err: os.Errno, state: rawptr) -> (err: os.Errno, skip_dir: bool) {
|
||||||
state := cast(^State)state;
|
state := cast(^State)state;
|
||||||
|
|
||||||
|
@ -229,7 +228,7 @@ draw :: proc(state_with_ui: ^StateWithUi) {
|
||||||
ui.compute_layout(state_with_ui.ui_context, { state_with_ui.state.screen_width, state_with_ui.state.screen_height }, state_with_ui.state.source_font_width, state_with_ui.state.source_font_height, state_with_ui.ui_context.root);
|
ui.compute_layout(state_with_ui.ui_context, { state_with_ui.state.screen_width, state_with_ui.state.screen_height }, state_with_ui.state.source_font_width, state_with_ui.state.source_font_height, state_with_ui.ui_context.root);
|
||||||
ui.draw(state_with_ui.ui_context, state_with_ui.state, state_with_ui.state.source_font_width, state_with_ui.state.source_font_height, state_with_ui.ui_context.root);
|
ui.draw(state_with_ui.ui_context, state_with_ui.state, state_with_ui.state.source_font_width, state_with_ui.state.source_font_height, state_with_ui.ui_context.root);
|
||||||
|
|
||||||
if state_with_ui.state.current_input_map != &state_with_ui.state.input_map.mode[.Normal] {
|
if state_with_ui.state.mode != .Insert && state_with_ui.state.current_input_map != &state_with_ui.state.input_map.mode[state_with_ui.state.mode] {
|
||||||
longest_description := 0;
|
longest_description := 0;
|
||||||
for key, action in state_with_ui.state.current_input_map.key_actions {
|
for key, action in state_with_ui.state.current_input_map.key_actions {
|
||||||
if len(action.description) > longest_description {
|
if len(action.description) > longest_description {
|
||||||
|
@ -365,6 +364,7 @@ init_plugin_vtable :: proc(ui_context: ^ui.Context) -> plugin.Plugin {
|
||||||
to_be_edited_map = state.current_input_map;
|
to_be_edited_map = state.current_input_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: change this to use the given mode
|
||||||
if action, exists := to_be_edited_map.key_actions[key]; exists {
|
if action, exists := to_be_edited_map.key_actions[key]; exists {
|
||||||
switch value in action.action {
|
switch value in action.action {
|
||||||
case core.LuaEditorAction:
|
case core.LuaEditorAction:
|
||||||
|
@ -958,6 +958,13 @@ main :: proc() {
|
||||||
|
|
||||||
state.current_input_map = &state.input_map.mode[.Normal];
|
state.current_input_map = &state.input_map.mode[.Normal];
|
||||||
register_default_input_actions(&state.input_map.mode[.Normal]);
|
register_default_input_actions(&state.input_map.mode[.Normal]);
|
||||||
|
register_default_input_actions(&state.input_map.mode[.Visual]);
|
||||||
|
core.register_key_action(&state.input_map.mode[.Normal], .V, proc(state: ^State) {
|
||||||
|
state.mode = .Visual;
|
||||||
|
state.current_input_map = &state.input_map.mode[.Visual];
|
||||||
|
}, "enter visual mode");
|
||||||
|
|
||||||
|
register_default_text_input_actions(&state.input_map.mode[.Normal]);
|
||||||
|
|
||||||
for arg in os.args[1:] {
|
for arg in os.args[1:] {
|
||||||
buffer, err := core.new_file_buffer(context.allocator, arg, state.directory);
|
buffer, err := core.new_file_buffer(context.allocator, arg, state.directory);
|
||||||
|
@ -1842,11 +1849,14 @@ main :: proc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch state.mode {
|
switch state.mode {
|
||||||
|
case .Visual: fallthrough
|
||||||
case .Normal: {
|
case .Normal: {
|
||||||
if sdl_event.type == .KEYDOWN {
|
if sdl_event.type == .KEYDOWN {
|
||||||
key := plugin.Key(sdl_event.key.keysym.sym);
|
key := plugin.Key(sdl_event.key.keysym.sym);
|
||||||
if key == .ESCAPE {
|
if key == .ESCAPE {
|
||||||
core.request_window_close(&state);
|
core.request_window_close(&state);
|
||||||
|
state.mode = .Normal;
|
||||||
|
state.current_input_map = &state.input_map.mode[.Normal];
|
||||||
}
|
}
|
||||||
|
|
||||||
if key == .LCTRL {
|
if key == .LCTRL {
|
||||||
|
@ -1985,26 +1995,6 @@ main :: proc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .Visual: {
|
|
||||||
buffer: ^FileBuffer;
|
|
||||||
|
|
||||||
if state.window != nil && state.window.get_buffer != nil {
|
|
||||||
buffer = transmute(^core.FileBuffer)(state.window.get_buffer(state.plugin_vtable, state.window.user_data));
|
|
||||||
} else {
|
|
||||||
buffer = &state.buffers[state.current_buffer];
|
|
||||||
}
|
|
||||||
|
|
||||||
if sdl_event.type == .KEYDOWN {
|
|
||||||
key := plugin.Key(sdl_event.key.keysym.sym);
|
|
||||||
|
|
||||||
#partial switch key {
|
|
||||||
case .ESCAPE: {
|
|
||||||
state.mode = .Normal;
|
|
||||||
// TODO: visual mode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue