t_position old_pos;
t_position work_pos;
- getyx(schematics_win.win, old_pos.y, old_pos.x);
+ old_pos = get_cursor_pos(schematics_win);
work_pos = mosfet->position;
- mvwaddch(schematics_win.win, work_pos.y, work_pos.x, mosfet->type);
+ draw_symbol(schematics_win, work_pos, mosfet->type);
if (mosfet->orientation == UP)
++work_pos.y;
else if (mosfet->orientation == DOWN)
++work_pos.x;
else if (mosfet->orientation == RIGHT)
--work_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);
+ if (!cursor_move_unprivileged(schematics_win, work_pos.y, work_pos.x))
+ {
+ draw_switch(schematics_win.win, nodes, mosfet, mosfet->orientation);
+ }
+ cursor_move_unprivileged(schematics_win, old_pos.y, old_pos.x);
return ;
}
}
}
+int is_in_viewport(t_schem_win schematics_win, t_position position)
+{
+ return (!
+ ( position.x < schematics_win.view_pos.x
+ || position.x > schematics_win.view_pos.x + schematics_win.view_size.x - 1
+ || position.y < schematics_win.view_pos.y
+ || position.y > schematics_win.view_pos.y + schematics_win.view_size.y - 1));
+}
+
+void draw_symbol(t_schem_win schematics_win, t_position position, t_symbol symbol)
+{
+ if (is_in_viewport(schematics_win, position))
+ {
+ mvwaddch(schematics_win.win,
+ position.y - schematics_win.view_pos.y,
+ position.x - schematics_win.view_pos.x,
+ symbol);
+ }
+ return ;
+}
+
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.win, segment->position.y, segment->position.x, symbol);
+ draw_symbol(schematics_win, segment->position, symbol);
return ;
}
size_t i;
const t_mosfet *mosfet;
const t_node *node;
+ t_position pos;
+ pos = get_cursor_pos(schematics_win);
werase(schematics_win.win);
i = 0;
while (i < mosfets->size)
schema_draw_node(schematics_win, node);
++i;
}
+ cursor_move_unprivileged(schematics_win, pos.y, pos.x);
wrefresh(schematics_win.win);
return (1);
}
if (ch == 'r' || ch == 'R')
{
gate_pos = pos_add(mosfet->position, mosfet_channel_rel_pos(mosfet));
- mvwaddch(schematics_win.win, gate_pos.y, gate_pos.x, ' ');
- wmove(schematics_win.win, mosfet->position.y, mosfet->position.x);
+ draw_symbol(schematics_win, gate_pos, ' ');
+ cursor_move_unprivileged(schematics_win, mosfet->position.y, mosfet->position.x);
}
if (ch == 't' && mosfet->type == p)
mosfet->type = n;
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.win, mosfet->position.y, mosfet->position.x);
+ cursor_move_unprivileged(schematics_win, mosfet->position.y, mosfet->position.x);
ungetch(ch);
return ;
}
char ch;
t_node_segment tmp;
- wmove(schematics_win->win, prediction->position.y, prediction->position.x);
+ cursor_move(schematics_win, prediction->position.y, prediction->position.x);
ch = wgetch(schematics_win->win);
tmp = *prediction;
if (ch == 'l')
ft_vec_find_index(&chosen_node->segments, &seg, &index, cmp_seg);
ft_vec_erase(&chosen_node->segments, index, NULL);
}
- while (!place_segment(schematics_win, chosen_node, &seg));
+ while (!place_segment(schematics_win, chosen_node, &seg))
+ {
+ refresh_schema_win(*schematics_win, nodes, mosfets);
+ }
if (chosen_node->segments.size == 0)
{
remove_node(nodes, chosen_node);
}
merge_overlapping_segments(chosen_node);
schema_draw_node(*schematics_win, chosen_node);
- wmove(schematics_win->win, seg.position.y, seg.position.x);
+ cursor_move_unprivileged(*schematics_win, seg.position.y, seg.position.x);
return ;
}
++chosen_node->set_state;
update_nodes(nodes, mosfets);
refresh_schema_win(schematics_win, nodes, mosfets);
- wmove(schematics_win.win, pos.y, pos.x);
return ;
}
--chosen_node->set_state;
update_nodes(nodes, mosfets);
refresh_schema_win(schematics_win, nodes, mosfets);
- wmove(schematics_win.win, pos.y, pos.x);
return ;
}
-int schema_next(t_schem_win 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)
{
if (sim_step(nodes, mosfets))
return (1);
refresh_schema_win(schematics_win, nodes, mosfets);
- wmove(schematics_win.win, pos.y, pos.x);
return (0);
}
+t_position get_cursor_pos(t_schem_win schematics_win)
+{
+ t_position pos;
+
+ getyx(schematics_win.win, pos.y, pos.x);
+ pos.y += schematics_win.view_pos.y;
+ pos.x += schematics_win.view_pos.x;
+ return (pos);
+}
+
+int cursor_move(t_schem_win *schematics_win, long y, long x)
+{
+ t_position cursor_pos;
+
+ if (is_in_viewport(*schematics_win, (t_position){.x = x, .y = y}))
+ {
+ y -= schematics_win->view_pos.y;
+ x -= schematics_win->view_pos.x;
+ wmove(schematics_win->win, y, x);
+ return (0);
+ }
+ else
+ {
+ cursor_pos = get_cursor_pos(*schematics_win);
+ schematics_win->view_pos.y += y - cursor_pos.y;
+ schematics_win->view_pos.x += x - cursor_pos.x;
+ return (1);
+ }
+}
+
+int cursor_move_unprivileged(t_schem_win schematics_win, long y, long x)
+{
+ if (is_in_viewport(schematics_win, (t_position){.x = x, .y = y}))
+ {
+ y -= schematics_win.view_pos.y;
+ x -= schematics_win.view_pos.x;
+ wmove(schematics_win.win, y, x);
+ return (0);
+ }
+ return (1);;
+}
+
int handle_key_press(int ch, t_schem_win *schematics_win, t_vec *nodes, t_vec *mosfets)
{
t_position pos;
+ int viewport_moved;
- getyx(schematics_win->win, pos.y, pos.x);
+ viewport_moved = 0;
+ pos = get_cursor_pos(*schematics_win);
if (ch == 'h')
- wmove(schematics_win->win, pos.y, --pos.x);
+ viewport_moved = cursor_move(schematics_win, pos.y, --pos.x);
else if (ch == 'l')
- wmove(schematics_win->win, pos.y, ++pos.x);
+ viewport_moved = cursor_move(schematics_win, pos.y, ++pos.x);
else if (ch == 'j')
- wmove(schematics_win->win, ++pos.y, pos.x);
+ viewport_moved = cursor_move(schematics_win, ++pos.y, pos.x);
else if (ch == 'k')
- wmove(schematics_win->win, --pos.y, pos.x);
+ viewport_moved = cursor_move(schematics_win, --pos.y, pos.x);
else if (ch == 't')
schema_add_mosfet(*schematics_win, nodes, mosfets, pos);
else if (ch == 'c')
else if (ch == 'S')
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);
+ if (viewport_moved)
+ {
+ refresh_schema_win(*schematics_win, nodes, mosfets);
+ }
return (0);
}
ch = wgetch(schematics_win->win);
while (ch != 27)
{
+ get_viewport_size(schematics_win);
res = handle_key_press(ch, schematics_win, nodes, mosfets);
if (res)
return (res);