]> www.ljiriste.work Git - FET_sim.git/commitdiff
Extend schematics window with viewport position
authorLukáš Jiřiště <redacted>
Mon, 10 Aug 2026 18:13:48 +0000 (20:13 +0200)
committerLukáš Jiřiště <redacted>
Mon, 10 Aug 2026 18:13:48 +0000 (20:13 +0200)
This is the base for moving the viewport around.

inc/FET_sim.h
src/colors.c
src/main.c
src/schema_mode.c
src/terminal.c

index 973582dd6a74c3ca76139ac63c0c315f1c2b71a4..b284b9eeaf2695e19ede6736352437ed6cd6a494 100644 (file)
@@ -140,11 +140,17 @@ typedef struct s_input
        t_arg           argv[MAX_ARGS];
 }                              t_input;
 
+typedef struct s_schematics_window
+{
+       WINDOW          *win;
+       t_position      view_pos;
+}      t_schem_win;
+
 typedef struct s_windows
 {
-       WINDOW  *command_win;
-       WINDOW  *schematics_win;
-       WINDOW  *border_win;
+       WINDOW          *command_win;
+       t_schem_win     schematics_win;
+       WINDOW          *border_win;
 }                      t_windows;
 
 t_node         *add_node(t_vec *nodes, t_state set_state);
@@ -163,9 +169,9 @@ int                 get_input(WINDOW *command_win, t_input *input);
 
 const char     *state_color_escape(t_state state);
 void           draw_single(WINDOW *command_win, t_vec *nodes, const t_mosfet *mosfet);
-void           schema_draw_mosfet(WINDOW *schematics_win, t_vec *nodes, const t_mosfet *mosfet);
-void           schema_draw_node(WINDOW *schematics_win, const t_node *node);
-void           schema_draw_segment(WINDOW *schematics_win, const t_node_segment *segment);
+void           schema_draw_mosfet(t_schem_win schematics_win, t_vec *nodes, const t_mosfet *mosfet);
+void           schema_draw_node(t_schem_win schematics_win, const t_node *node);
+void           schema_draw_segment(t_schem_win schematics_win, const t_node_segment *segment);
 
 void           update_nodes(t_vec *nodes, t_vec *mosfets);
 int                    should_open(t_vec *nodes, const t_mosfet *mosfet);
@@ -197,8 +203,8 @@ int                 c_load(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets);
 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                    refresh_schema_win(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets);
-int                    schema_mode(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets);
+int                    refresh_schema_win(t_schem_win schematics_win, t_vec *nodes, t_vec *mosfets);
+int                    schema_mode(t_schem_win *schematics_win, t_vec *nodes, t_vec *mosfets);
 
 int    str_is_num(const char *str);
 
index 525c0db180bde31351e4e9b9c37597c5a4242864..2ce8aadaea5b38f62f3dd1636d05d0d300a084fd 100644 (file)
@@ -108,14 +108,14 @@ static void       draw_switch(WINDOW *win, t_vec *nodes, const t_mosfet *mosfet, t_ori
        return ;
 }
 
-void   schema_draw_mosfet(WINDOW *schematics_win, t_vec *nodes, const t_mosfet *mosfet)
+void   schema_draw_mosfet(t_schem_win schematics_win, t_vec *nodes, const t_mosfet *mosfet)
 {
        t_position      old_pos;
        t_position      work_pos;
 
-       getyx(schematics_win, old_pos.y, old_pos.x);
+       getyx(schematics_win.win, old_pos.y, old_pos.x);
        work_pos = mosfet->position;
-       mvwaddch(schematics_win, work_pos.y, work_pos.x, mosfet->type);
+       mvwaddch(schematics_win.win, work_pos.y, work_pos.x, mosfet->type);
        if (mosfet->orientation == UP)
                ++work_pos.y;
        else if (mosfet->orientation == DOWN)
@@ -124,9 +124,9 @@ void        schema_draw_mosfet(WINDOW *schematics_win, t_vec *nodes, const t_mosfet *mo
                ++work_pos.x;
        else if (mosfet->orientation == RIGHT)
                --work_pos.x;
-       wmove(schematics_win, work_pos.y, work_pos.x);
-       draw_switch(schematics_win, nodes, mosfet, mosfet->orientation);
-       wmove(schematics_win, old_pos.y, old_pos.x);
+       wmove(schematics_win.win, work_pos.y, work_pos.x);
+       draw_switch(schematics_win.win, nodes, mosfet, mosfet->orientation);
+       wmove(schematics_win.win, old_pos.y, old_pos.x);
        return ;
 }
 
@@ -153,22 +153,22 @@ t_symbol  seg_connects_to_symbol(t_bitflag connects)
        }
 }
 
-void   schema_draw_segment(WINDOW *schematics_win, const t_node_segment *segment)
+void   schema_draw_segment(t_schem_win schematics_win, const t_node_segment *segment)
 {
        t_symbol                                symbol;
 
        symbol = seg_connects_to_symbol(segment->connects);
        if (symbol != '\0')
-               mvwaddch(schematics_win, segment->position.y, segment->position.x, symbol);
+               mvwaddch(schematics_win.win, segment->position.y, segment->position.x, symbol);
        return ;
 }
 
-void   schema_draw_node(WINDOW *schematics_win, const t_node *node)
+void   schema_draw_node(t_schem_win schematics_win, const t_node *node)
 {
        const t_node_segment    *seg;
        size_t                                  i;
 
-       wattron(schematics_win, COLOR_PAIR(get_state_color(node->state)));
+       wattron(schematics_win.win, COLOR_PAIR(get_state_color(node->state)));
        i = 0;
        while (i < node->segments.size)
        {
@@ -176,7 +176,7 @@ void        schema_draw_node(WINDOW *schematics_win, const t_node *node)
                schema_draw_segment(schematics_win, seg);
                ++i;
        }
-       wattroff(schematics_win, COLOR_PAIR(get_state_color(node->state)));
+       wattroff(schematics_win.win, COLOR_PAIR(get_state_color(node->state)));
        return ;
 }
 
@@ -233,13 +233,13 @@ void      draw_single(WINDOW *command_win, t_vec *nodes, const t_mosfet *mosfet)
        return ;
 }
 
-int    refresh_schema_win(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets)
+int    refresh_schema_win(t_schem_win schematics_win, t_vec *nodes, t_vec *mosfets)
 {
        size_t                  i;
        const t_mosfet  *mosfet;
        const t_node    *node;
 
-       werase(schematics_win);
+       werase(schematics_win.win);
        i = 0;
        while (i < mosfets->size)
        {
@@ -254,6 +254,6 @@ int refresh_schema_win(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets)
                schema_draw_node(schematics_win, node);
                ++i;
        }
-       wrefresh(schematics_win);
+       wrefresh(schematics_win.win);
        return (1);
 }
index 1f22ba12f9d75c5533a3e0164d4e467676d4eb72..4d044abec2459c1aab183485a6a20b2f0277da8f 100644 (file)
@@ -270,7 +270,7 @@ int process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets)
        else if(input.command == refresh_schema)
                res = refresh_schema_win(windows->schematics_win, nodes, mosfets);
        else if (input.command == switch_to_schema)
-               res = schema_mode(windows->schematics_win, nodes, mosfets);
+               res = schema_mode(&windows->schematics_win, nodes, mosfets);
        else if (input.command == exitsim)
                res = 0;
        return (res);
index 93465a743ee7a78816515b9866f6c7560055bf51..4a677c9f6b4f7de47d09dcac69742c43033dbf3b 100644 (file)
@@ -85,7 +85,7 @@ void  add_terminal_segment(t_node *node, t_mosfet *mosfet, t_terminal term)
 }
 
 
-void   add_terminal_node(WINDOW *schematics_win, t_vec *nodes, t_mosfet *mosfet, t_terminal terminal)
+void   add_terminal_node(t_schem_win schematics_win, t_vec *nodes, t_mosfet *mosfet, t_terminal terminal)
 {
        t_node  *node;
 
@@ -96,7 +96,7 @@ void  add_terminal_node(WINDOW *schematics_win, t_vec *nodes, t_mosfet *mosfet, t
        return ;
 }
 
-void   schema_add_mosfet(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
+void   schema_add_mosfet(t_schem_win schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
 {
        t_mosfet        *mosfet;
        size_t          i;
@@ -123,12 +123,12 @@ void      schema_add_mosfet(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_p
        while (1)
        {
                schema_draw_mosfet(schematics_win, nodes, mosfet);
-               ch = wgetch(schematics_win);
+               ch = wgetch(schematics_win.win);
                if (ch == 'r' || ch == 'R')
                {
                        gate_pos = pos_add(mosfet->position, mosfet_channel_rel_pos(mosfet));
-                       mvwaddch(schematics_win, gate_pos.y, gate_pos.x, ' ');
-                       wmove(schematics_win, mosfet->position.y, mosfet->position.x);
+                       mvwaddch(schematics_win.win, gate_pos.y, gate_pos.x, ' ');
+                       wmove(schematics_win.win, mosfet->position.y, mosfet->position.x);
                }
                if (ch == 't' && mosfet->type == p)
                        mosfet->type = n;
@@ -143,7 +143,7 @@ void        schema_add_mosfet(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_p
                        add_terminal_node(schematics_win, nodes, mosfet, source);
                        add_terminal_node(schematics_win, nodes, mosfet, gate);
                        add_terminal_node(schematics_win, nodes, mosfet, drain);
-                       wmove(schematics_win, mosfet->position.y, mosfet->position.x);
+                       wmove(schematics_win.win, mosfet->position.y, mosfet->position.x);
                        ungetch(ch);
                        return ;
                }
@@ -187,13 +187,13 @@ t_bitflag merge_connect(t_bitflag connects1, t_bitflag connects2)
        return (connects1 | connects2);
 }
 
-int    place_segment(WINDOW *schematics_win, t_node *node, t_node_segment *prediction)
+int    place_segment(t_schem_win *schematics_win, t_node *node, t_node_segment *prediction)
 {
        char                    ch;
        t_node_segment  tmp;
 
-       wmove(schematics_win, prediction->position.y, prediction->position.x);
-       ch = wgetch(schematics_win);
+       wmove(schematics_win->win, prediction->position.y, prediction->position.x);
+       ch = wgetch(schematics_win->win);
        tmp = *prediction;
        if (ch == 'l')
        {
@@ -222,7 +222,7 @@ int place_segment(WINDOW *schematics_win, t_node *node, t_node_segment *predicti
        else
                return (1);     // The choice not to ungetch is deliberate, because it felt weird using it
        ft_vec_append(&node->segments, prediction);
-       schema_draw_segment(schematics_win, prediction);
+       schema_draw_segment(*schematics_win, prediction);
        *prediction = tmp;
        return (0);
 }
@@ -269,7 +269,7 @@ static void merge_overlapping_segments(t_node *node)
        return ;
 }
 
-void   schema_user_draw_node(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
+void   schema_user_draw_node(t_schem_win *schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
 {
        t_node                  *chosen_node;
        t_node                  *end_node;
@@ -308,20 +308,20 @@ void      schema_user_draw_node(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets,
                        merge_nodes(nodes, mosfets, chosen_node, end_node);
                        chosen_node = get_node_by_id(nodes, index);
                }
-               schema_draw_segment(schematics_win, merge_seg);
+               schema_draw_segment(*schematics_win, merge_seg);
        }
        else
        {
                ft_vec_append(&chosen_node->segments, &seg);
-               schema_draw_segment(schematics_win, &seg);
+               schema_draw_segment(*schematics_win, &seg);
        }
        merge_overlapping_segments(chosen_node);
-       schema_draw_node(schematics_win, chosen_node);
-       wmove(schematics_win, seg.position.y, seg.position.x);
+       schema_draw_node(*schematics_win, chosen_node);
+       wmove(schematics_win->win, seg.position.y, seg.position.x);
        return ;
 }
 
-void schema_switch_node(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
+void schema_switch_node(t_schem_win schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
 {
        t_node  *chosen_node;
 
@@ -334,11 +334,11 @@ void schema_switch_node(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_
                ++chosen_node->set_state;
        update_nodes(nodes, mosfets);
        refresh_schema_win(schematics_win, nodes, mosfets);
-       wmove(schematics_win, pos.y, pos.x);
+       wmove(schematics_win.win, pos.y, pos.x);
        return ;
 }
 
-void schema_reverse_switch_node(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
+void schema_reverse_switch_node(t_schem_win schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
 {
        t_node  *chosen_node;
 
@@ -351,64 +351,64 @@ void schema_reverse_switch_node(WINDOW *schematics_win, t_vec *nodes, t_vec *mos
                --chosen_node->set_state;
        update_nodes(nodes, mosfets);
        refresh_schema_win(schematics_win, nodes, mosfets);
-       wmove(schematics_win, pos.y, pos.x);
+       wmove(schematics_win.win, pos.y, pos.x);
        return ;
 }
 
-int    schema_next(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
+int    schema_next(t_schem_win schematics_win, t_vec *nodes, t_vec *mosfets, t_position pos)
 {
        if (sim_step(nodes, mosfets))
                return (1);
        refresh_schema_win(schematics_win, nodes, mosfets);
-       wmove(schematics_win, pos.y, pos.x);
+       wmove(schematics_win.win, pos.y, pos.x);
        return (0);
 }
 
-int    handle_key_press(int ch, WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets)
+int    handle_key_press(int ch, t_schem_win *schematics_win, t_vec *nodes, t_vec *mosfets)
 {
        t_position      pos;
 
-       getyx(schematics_win, pos.y, pos.x);
+       getyx(schematics_win->win, pos.y, pos.x);
        if (ch == 'h')
-               wmove(schematics_win, pos.y, --pos.x);
+               wmove(schematics_win->win, pos.y, --pos.x);
        else if (ch == 'l')
-               wmove(schematics_win, pos.y, ++pos.x);
+               wmove(schematics_win->win, pos.y, ++pos.x);
        else if (ch == 'j')
-               wmove(schematics_win, ++pos.y, pos.x);
+               wmove(schematics_win->win, ++pos.y, pos.x);
        else if (ch == 'k')
-               wmove(schematics_win, --pos.y, pos.x);
+               wmove(schematics_win->win, --pos.y, pos.x);
        else if (ch == 't')
-               schema_add_mosfet(schematics_win, nodes, mosfets, pos);
+               schema_add_mosfet(*schematics_win, nodes, mosfets, pos);
        else if (ch == 'c')
                schema_user_draw_node(schematics_win, nodes, mosfets, pos);
        else if (ch == 's')
-               schema_switch_node(schematics_win, nodes, mosfets, pos);
+               schema_switch_node(*schematics_win, nodes, mosfets, pos);
        else if (ch == 'S')
-               schema_reverse_switch_node(schematics_win, nodes, mosfets, pos);
+               schema_reverse_switch_node(*schematics_win, nodes, mosfets, pos);
        else if (ch == 'n')
-               schema_next(schematics_win, nodes, mosfets, pos);
+               schema_next(*schematics_win, nodes, mosfets, pos);
        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    schema_loop(t_schem_win *schematics_win, t_vec *nodes, t_vec *mosfets)
 {
        int     res;
        int     ch;
 
-       wrefresh(schematics_win);
-       ch = wgetch(schematics_win);
+       wrefresh(schematics_win->win);
+       ch = wgetch(schematics_win->win);
        while (ch != 27)
        {
                res = handle_key_press(ch, schematics_win, nodes, mosfets);
                if (res)
                        return (res);
-               ch = wgetch(schematics_win);
+               ch = wgetch(schematics_win->win);
        }
        return (1);
 }
 
-int    schema_mode(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets)
+int    schema_mode(t_schem_win *schematics_win, t_vec *nodes, t_vec *mosfets)
 {
        int     res;
 
index 15ab1a42dd33e8e234bb72e89762d8010787ce77..c34f350b539ae9a341955f58e5e64cffafd534de 100644 (file)
@@ -11,12 +11,13 @@ void        setup_terminal(t_windows *windows)
        initscr();
        init_colors();
        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->schematics_win.win = newwin(0, COLS - COMMAND_WIN_COLS - 2, 0, 0);
+       windows->schematics_win.view_pos = (t_position){.x = 0, .y = 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);
+       wmove(windows->schematics_win.win, LINES / 2, (COLS - COMMAND_WIN_COLS - 2) / 2);
        set_escdelay(50);
        keypad(windows->command_win, TRUE);
-       keypad(windows->schematics_win, TRUE);
+       keypad(windows->schematics_win.win, TRUE);
        nonl();
        cbreak();
        echo();
@@ -71,7 +72,7 @@ int   get_input(WINDOW *command_win, t_input *input)
 void   clean_terminal(t_windows *windows)
 {
        delwin(windows->command_win);
-       delwin(windows->schematics_win);
+       delwin(windows->schematics_win.win);
        delwin(windows->border_win);
        endwin();
        return ;