From a6cc5a5d44b06c7bb75b51feee04f17659ff6bed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Sun, 15 Dec 2024 23:42:04 +0100 Subject: [PATCH] Implement switching to schematic mode and movement --- Makefile | 2 ++ inc/FET_sim.h | 4 +++- src/main.c | 22 +++++++++++++-------- src/schema_mode.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/terminal.c | 3 +++ 5 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 src/schema_mode.c diff --git a/Makefile b/Makefile index f5ae798..acd4188 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,8 @@ SOURCES := main.c \ \ build_helper.c \ \ + schema_mode.c \ + \ c_addnode.c \ c_addfet.c \ c_setnode.c \ diff --git a/inc/FET_sim.h b/inc/FET_sim.h index f4cc3af..2163e36 100644 --- a/inc/FET_sim.h +++ b/inc/FET_sim.h @@ -61,6 +61,7 @@ typedef enum e_command bind, help, connect, + switch_to_schema, exitsim, } t_command; @@ -110,7 +111,7 @@ int transfer_mosfet(t_mosfet *mosfet, t_node *from, t_node *to); int merge_nodes(t_node *node1, t_node *node2); void free_node(void *node); -int process_input(WINDOW *command_win, t_vec *nodes, t_vec *mosfets); +int process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets); int get_input(WINDOW *command_win, t_input *input); const char *state_color_escape(t_state state); @@ -145,4 +146,5 @@ int c_help(WINDOW *command_win, t_input input); int c_next(t_input input, t_vec *nodes, t_vec *mosfets); int c_setnode(WINDOW *command_win, t_input input, t_vec *nodes); +int schema_mode(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets); #endif //FET_SIM_H diff --git a/src/main.c b/src/main.c index 3e54974..97c302b 100644 --- a/src/main.c +++ b/src/main.c @@ -60,6 +60,8 @@ int parse_command(const char *str, t_command *cmd) *cmd = connect; else if (!ft_strcmp(str, "e") || !ft_strcmp(str, "exit")) *cmd = exitsim; + else if (!ft_strcmp(str, "schema")) + *cmd = switch_to_schema; else return (0); return (1); @@ -129,6 +131,8 @@ int has_correct_argc(t_input input) return (0); if (c == connect && argc != 4) return (0); + if (c == switch_to_schema && argc > 0) + return (0); return (1); } @@ -213,30 +217,32 @@ int construct_input(t_input *input, char **split_inp) return (1); } -int process_input(WINDOW *command_win, t_vec *nodes, t_vec *mosfets) +int process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets) { int res; static t_input input = {.command = help, .argc = 0}; - if (!get_input(command_win, &input)) + if (!get_input(windows->command_win, &input)) return (1); res = 1; if (input.command == next) res = c_next(input, nodes, mosfets); else if (input.command == draw) - res = c_draw(command_win, input, mosfets); + res = c_draw(windows->command_win, input, mosfets); else if (input.command == setnode) - res = c_setnode(command_win, input, nodes); + res = c_setnode(windows->command_win, input, nodes); else if (input.command == addnode) - res = c_addnode(command_win, input, nodes); + res = c_addnode(windows->command_win, input, nodes); else if (input.command == addfet) - res = c_addfet(command_win, input, mosfets); + res = c_addfet(windows->command_win, input, mosfets); else if (input.command == bind) res = c_bind(input, nodes, mosfets); else if (input.command == connect) res = c_connect(input, nodes, mosfets); else if (input.command == help) - res = c_help(command_win, input); + res = c_help(windows->command_win, input); + else if (input.command == switch_to_schema) + res = schema_mode(windows->schematics_win, nodes, mosfets); else if (input.command == exitsim) res = 0; return (res); @@ -263,7 +269,7 @@ int main(void) print_start(windows.command_win); //if (argc > 1) // build_graph(argv[1], &nodes, &mosfets); - while (process_input(windows.command_win, &nodes, &mosfets)); + while (process_input(&windows, &nodes, &mosfets)); cleanup(&nodes, &mosfets, &windows); return (0); } diff --git a/src/schema_mode.c b/src/schema_mode.c new file mode 100644 index 0000000..fcddfba --- /dev/null +++ b/src/schema_mode.c @@ -0,0 +1,49 @@ +#include "FET_sim.h" +#include "libft.h" +#include + +int handle_key_press(int ch, WINDOW *schematics_win, __attribute__((unused)) t_vec *nodes, __attribute__((unused)) t_vec *mosfets) +{ + int y; + int x; + + getyx(schematics_win, y, x); + if (ch == 'h') + --x; + else if (ch == 'l') + ++x; + else if (ch == 'j') + ++y; + else if (ch == 'k') + --y; + wmove(schematics_win, y, x); + return (0); +} + +// 27 == '\e' but compiler flags '\e' as not an ISO escape sequence +int schema_loop(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets) +{ + int res; + int ch; + + wrefresh(schematics_win); + ch = wgetch(schematics_win); + while (ch != 27) + { + res = handle_key_press(ch, schematics_win, nodes, mosfets); + if (res) + return (res); + ch = wgetch(schematics_win); + } + return (1); +} + +int schema_mode(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets) +{ + int res; + + noecho(); + res = schema_loop(schematics_win, nodes, mosfets); + echo(); + return (res); +} diff --git a/src/terminal.c b/src/terminal.c index 14fe9c1..15ab1a4 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -13,7 +13,10 @@ void setup_terminal(t_windows *windows) windows->command_win = newwin(0, COMMAND_WIN_COLS, 0, COLS - COMMAND_WIN_COLS); windows->schematics_win = newwin(0, COLS - COMMAND_WIN_COLS - 2, 0, 0); windows->border_win = newwin(0, 1, 0, COLS - COMMAND_WIN_COLS - 1); + wmove(windows->schematics_win, LINES / 2, (COLS - COMMAND_WIN_COLS - 2) / 2); + set_escdelay(50); keypad(windows->command_win, TRUE); + keypad(windows->schematics_win, TRUE); nonl(); cbreak(); echo(); -- 2.30.2