bind,
help,
connect,
+ switch_to_schema,
exitsim,
} t_command;
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);
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
*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);
return (0);
if (c == connect && argc != 4)
return (0);
+ if (c == switch_to_schema && argc > 0)
+ return (0);
return (1);
}
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);
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);
}
--- /dev/null
+#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);
+}
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();