Implement switching to schematic mode and movement
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 22:42:04 +0000 (23:42 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 22:42:04 +0000 (23:42 +0100)
Makefile
inc/FET_sim.h
src/main.c
src/schema_mode.c [new file with mode: 0644]
src/terminal.c

index f5ae798eb1e60960e1debba7afc1e9d23c669c68..acd41888d9b4ac93422f14bcfe2bf7023063a8f9 100644 (file)
--- 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             \
index f4cc3afaac979338bd0a18615da26eb839b3d0a8..2163e36a7d6cdf3398f20ba619a534b2f3349648 100644 (file)
@@ -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
index 3e54974a1f5511ed504c906d3622f44c2afed6b8..97c302b49e8094887eb8989e88b46316bb3e8252 100644 (file)
@@ -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 (file)
index 0000000..fcddfba
--- /dev/null
@@ -0,0 +1,49 @@
+#include "FET_sim.h"
+#include "libft.h"
+#include <ncurses.h>
+
+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);
+}
index 14fe9c1edb7412c33a26cc3f5fb83ec70fc05f13..15ab1a42dd33e8e234bb72e89762d8010787ce77 100644 (file)
@@ -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();