Skip to content

Commit

Permalink
Merge pull request #33 from olin/free-line
Browse files Browse the repository at this point in the history
Free line
  • Loading branch information
newsch authored May 3, 2019
2 parents 7a81e74 + 13a7ed7 commit bc41358
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
88 changes: 84 additions & 4 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ int (*mode_functions[])(State *, WINDOW *, WINDOW *) = {
mode_picker,
mode_insert,
mode_pan,
mode_free_line,
};

//////////////////////////////
Expand All @@ -38,6 +39,50 @@ Mode_ID return_to_canvas(int input_ch) { // State?
return LAST;
}

////////////////////////////
// SPECIFC MODE FUNCTIONS //
////////////////////////////

/* free_line_arrows_to_char
*
* Takes the current and previous arrow directions, and returns the
* appropriate character, including diagonals.
*
* NOTE: Assumes that the input character is an arrow key
*
* Reference table:
* ^ v > < (current)
*
* ^ | | / \
* v | | \ /
* > \ / - -
* < / \ - -
* (last)
*/
int free_line_arrows_to_char(int last_arrow, int current_arrow) {
char horizontal = '-';
char vertical = '|';
char diag_up = '/';
char diag_down = '\\';

if ((last_arrow == KEY_UP || current_arrow == KEY_DOWN) &&
(last_arrow == KEY_DOWN || current_arrow == KEY_UP)) {
// arrows are vertically parallel (top left quarter of truth table)
return vertical;
} else if ((last_arrow == KEY_LEFT || current_arrow == KEY_RIGHT) &&
(last_arrow == KEY_RIGHT || current_arrow == KEY_LEFT)) {
// arrows are horizontally parallel (bottom right quarter of truth table)
return horizontal;
} else if ((last_arrow == KEY_UP && current_arrow == KEY_RIGHT) ||
(last_arrow == KEY_DOWN && current_arrow == KEY_LEFT) ||
(last_arrow == KEY_LEFT && current_arrow == KEY_DOWN) ||
(last_arrow == KEY_RIGHT && current_arrow == KEY_UP)) {
return diag_up;
} else {
return diag_down;
}
}

////////////////////
// MODE FUNCTIONS //
////////////////////
Expand Down Expand Up @@ -114,7 +159,6 @@ int mode_pan(State *state, WINDOW *canvas_win, WINDOW *status_win) {
state->current_mode = MODE_PICKER;
return 0;
}

if ((state->ch_in == KEY_LEFT) || (state->ch_in == KEY_RIGHT) ||
(state->ch_in == KEY_UP) || (state->ch_in == KEY_DOWN)) {
view_pan_ch(state->ch_in, state->view);
Expand All @@ -123,6 +167,42 @@ int mode_pan(State *state, WINDOW *canvas_win, WINDOW *status_win) {
return 0;
}

////////////////////////////
// SPECIFC MODE FUNCTIONS //
////////////////////////////
/* mode_free_line
*
* Move with arrows and insert character with keyboard.
*/
int mode_free_line(State *state, WINDOW *canvas_win, WINDOW *status_win) {
// handle mode changing
if (state->ch_in == KEY_TAB) {
// Clean up code
state->last_canvas_mode = MODE_INSERT;

state->current_mode = MODE_PICKER;
return 0;
}

// free line behavior
if ((state->ch_in == KEY_LEFT) || (state->ch_in == KEY_RIGHT) ||
(state->ch_in == KEY_UP) || (state->ch_in == KEY_DOWN)) {
int current_arrow = state->ch_in;
int last_arrow = state->last_arrow_direction;

front_setcharcursor(free_line_arrows_to_char(last_arrow, current_arrow));

state->last_cursor->x = state->cursor->x;
state->last_cursor->y = state->cursor->y;

cursor_key_to_move(current_arrow, state->cursor, state->view);

state->last_arrow_direction = state->ch_in;
} else if (state->ch_in == KEY_BACKSPACE) {
cursor_key_to_move(cursor_opposite_dir(state->last_arrow_direction),
state->cursor, state->view);
front_setcharcursor(' ');
}

wmove(canvas_win, cursor_y_to_canvas(state->cursor),
cursor_x_to_canvas(state->cursor));

return 0;
}
1 change: 1 addition & 0 deletions src/fe_modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
int mode_picker(State *state, WINDOW *canvas_win, WINDOW *status_win);
int mode_insert(State *state, WINDOW *canvas_win, WINDOW *status_win);
int mode_pan(State *state, WINDOW *canvas_win, WINDOW *status_win);
int mode_free_line(State *state, WINDOW *canvas_win, WINDOW *status_win);

extern int (*mode_functions[])(State *, WINDOW *, WINDOW *);

Expand Down
9 changes: 7 additions & 2 deletions src/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
(void)noecho(); /* don't print on getch() */
curs_set(2);

define_key("\r", KEY_ENTER); // Bind the <Enter> key properly
define_key("\r", KEY_ENTER); // Bind the <Enter> key properly

if (has_colors()) {
setup_colors();
Expand All @@ -51,6 +51,7 @@ int main(int argc, char *argv[]) {
status_win = create_status_win();

cursor = cursor_new();
Cursor *last_cursor = cursor_new();
Canvas *canvas = canvas_new_blank(1000, 1000);

view = view_new_startpos(canvas, 300, 300);
Expand All @@ -72,9 +73,12 @@ int main(int argc, char *argv[]) {
.ch_in = 0,
.cursor = cursor,
.current_mode = MODE_INSERT,
// .current_mode = MODE_FREE_LINE,

.last_arrow_direction = KEY_RIGHT,
.last_canvas_mode = MODE_INSERT,
.view = view,
.last_cursor = last_cursor,
};
State *state = &new_state;

Expand All @@ -83,8 +87,9 @@ int main(int argc, char *argv[]) {

mode_functions[state->current_mode](state, canvas_win, status_win);

update_screen_size(canvas_win, status_win, cursor);
// *(state->last_cursor) = *(state->cursor);

update_screen_size(canvas_win, status_win, cursor);
refresh_screen();
}

Expand Down
1 change: 1 addition & 0 deletions src/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#define KEY_TAB '\t'
#define KEY_SHIFT_TAB KEY_BTAB
// TODO: Understand delete/backspace on mac

void finish(int sig);
void setup_colors();
Expand Down
1 change: 1 addition & 0 deletions src/mode_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef enum {
MODE_PICKER,
MODE_INSERT,
MODE_PAN,
MODE_FREE_LINE,

// ^ add your mode above
LAST, // used to get number of elements
Expand Down
1 change: 1 addition & 0 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct {
Mode_ID last_canvas_mode;
int last_arrow_direction;
View *view;
Cursor *last_cursor;
} State;

#endif

0 comments on commit bc41358

Please sign in to comment.