Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement resizing #18

Merged
merged 22 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ endif
collascii: frontend.out
mv frontend.out collascii

frontend.out: cursor.o fe_modes.o
frontend.out: LDLIBS +=-lncurses
frontend.out: cursor.o fe_modes.o canvas.o view.o

## PATTERNS

Expand Down
14 changes: 9 additions & 5 deletions src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
* TODO: add save/read from file options
* TODO: assert get/set positions are within canvas sizes?
*/
#include "canvas.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int num_cols, num_rows;
char **rows;
} Canvas;

/* Create a canvas object
*
* Returned pointer should be freed with free_canvas
Expand All @@ -27,6 +23,14 @@ Canvas *canvas_new(int rows, int cols) {
return canvas;
}

Canvas *canvas_new_blank(int rows, int cols) {
Canvas *canvas = canvas_new(rows, cols);
for (int i = 0; i < (rows * cols); i++) {
canvas_schari(canvas, i, ' ');
}
return canvas;
}

/* Create and return a deep copy of a canvas
*
* Returned pointer should be freed with free_canvas
Expand Down
2 changes: 2 additions & 0 deletions src/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ typedef struct {
} Canvas;

Canvas *canvas_new(int rows, int cols);
Canvas *canvas_new_blank(int rows, int cols);

Canvas *canvas_cpy(Canvas *orig);
void canvas_free(Canvas *canvas);
void canvas_scharyx(Canvas *canvas, int y, int x, char c);
Expand Down
37 changes: 21 additions & 16 deletions src/cursor.c
Original file line number Diff line number Diff line change
@@ -1,63 +1,68 @@
#include "cursor.h"
#include <ncurses.h>
#include <stdlib.h>
#include "frontend.h"

/* The Cursor struct helps with controls.
* It also maps the drawing area to the canvas nicely.
*/

Cursor* cursor_new() {
Cursor* cursor = malloc(sizeof(Cursor));
Cursor *cursor_new() {
Cursor *cursor = malloc(sizeof(Cursor));
cursor->x = 0;
cursor->y = 0;
return cursor;
}

void cursor_move_up(Cursor* cursor) {
void cursor_move_up(Cursor *cursor, View *view) {
if (cursor->y == 0) {
view_move_up(view);
return;
}
cursor->y--;
}

void cursor_move_down(Cursor* cursor) {
if (cursor->y == (LINES - 5)) { // Take box lines into account
void cursor_move_down(Cursor *cursor, View *view) {
if (cursor->y == view_max_y) {
view_move_down(view);
return;
}
cursor->y++;
}

void cursor_move_left(Cursor* cursor) {
void cursor_move_left(Cursor *cursor, View *view) {
if (cursor->x == 0) {
view_move_left(view);
return;
}
cursor->x--;
}

void cursor_move_right(Cursor* cursor) {
if (cursor->x == (COLS - 3)) { // Take box lines into account
void cursor_move_right(Cursor *cursor, View *view) {
if (cursor->x == view_max_x) {
view_move_right(view);
return;
}
cursor->x++;
}

int cursor_x_to_canvas(Cursor* cursor) { return cursor->x + 1; }
int cursor_x_to_canvas(Cursor *cursor) { return cursor->x + 1; }

int cursor_y_to_canvas(Cursor* cursor) { return cursor->y + 1; }
int cursor_y_to_canvas(Cursor *cursor) { return cursor->y + 1; }

void cursor_key_to_move(int arrow, Cursor* cursor) {
void cursor_key_to_move(int arrow, Cursor *cursor, View *view) {
switch (arrow) {
case KEY_LEFT:
cursor_move_left(cursor);
cursor_move_left(cursor, view);
break;
case KEY_RIGHT:
cursor_move_right(cursor);
cursor_move_right(cursor, view);
break;
case KEY_UP:
cursor_move_up(cursor);
cursor_move_up(cursor, view);
break;
case KEY_DOWN:
cursor_move_down(cursor);
cursor_move_down(cursor, view);
break;
}
}
Expand All @@ -76,4 +81,4 @@ int cursor_opposite_dir(int arrow) {
return -1;
}

void cursor_free(Cursor* cursor) { free(cursor); }
void cursor_free(Cursor *cursor) { free(cursor); }
23 changes: 14 additions & 9 deletions src/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@
* It also maps the drawing area to the canvas nicely.
*/

#include "view.h"

#include <ncurses.h>
#include <stdlib.h>

typedef struct CURSOR {
int x;
int y;
} Cursor;

Cursor* cursor_new();
void cursor_free(Cursor* cursor);
Cursor *cursor_new();
void cursor_free(Cursor *cursor);

void cursor_move_up(Cursor* cursor);
void cursor_move_down(Cursor* cursor);
void cursor_move_left(Cursor* cursor);
void cursor_move_right(Cursor* cursor);
void cursor_move_up(Cursor *cursor, View *view);
void cursor_move_down(Cursor *cursor, View *view);
void cursor_move_left(Cursor *cursor, View *view);
void cursor_move_right(Cursor *cursor, View *view);

int cursor_x_to_canvas(Cursor* cursor);
int cursor_y_to_canvas(Cursor* cursor);
void cursor_key_to_move(int arrow, Cursor* cursor);
int cursor_x_to_canvas(Cursor *cursor);
int cursor_y_to_canvas(Cursor *cursor);
void cursor_key_to_move(int arrow, Cursor *cursor, View *view);
int cursor_opposite_dir(int arrow);

#endif
40 changes: 31 additions & 9 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
int (*mode_functions[])(State *, WINDOW *, WINDOW *) = {
mode_picker,
mode_insert,
mode_pan,
};

//////////////////////////////
Expand Down Expand Up @@ -49,6 +50,7 @@ int mode_picker(State *state, WINDOW *canvas_win, WINDOW *status_win) {
// Mode Switch - Enter to canvas,
if (state->ch_in == KEY_ENTER) {
state->current_mode = state->last_canvas_mode;
return 0;
}

// LR Arrows navigation
Expand All @@ -73,22 +75,20 @@ int mode_insert(State *state, WINDOW *canvas_win, WINDOW *status_win) {
// insert mode behavior
if ((state->ch_in == KEY_LEFT) || (state->ch_in == KEY_RIGHT) ||
(state->ch_in == KEY_UP) || (state->ch_in == KEY_DOWN)) {
cursor_key_to_move(state->ch_in, state->cursor);
cursor_key_to_move(state->ch_in, state->cursor, state->view);
state->last_arrow_direction = state->ch_in;
} else {
if (' ' <= state->ch_in &&
state->ch_in <= '~') { // check if ch is printable
mvwaddch(canvas_win, cursor_y_to_canvas(state->cursor),
cursor_x_to_canvas(state->cursor), state->ch_in);
cursor_key_to_move(state->last_arrow_direction, state->cursor);
front_setcharcursor(state->ch_in);
cursor_key_to_move(state->last_arrow_direction, state->cursor,
state->view);
} else if (state->ch_in == KEY_BACKSPACE) {
cursor_key_to_move(cursor_opposite_dir(state->last_arrow_direction),
state->cursor);
mvwaddch(canvas_win, cursor_y_to_canvas(state->cursor),
cursor_x_to_canvas(state->cursor), ' ');
state->cursor, state->view);
front_setcharcursor(' ');
} else if (state->ch_in == KEY_DC) {
mvwaddch(canvas_win, cursor_y_to_canvas(state->cursor),
cursor_x_to_canvas(state->cursor), ' ');
front_setcharcursor(' ');
} else {
// Print non-print characters to bottom left in status_win bar
mvwaddch(status_win, 1, COLS - 3, state->ch_in);
Expand All @@ -101,6 +101,28 @@ int mode_insert(State *state, WINDOW *canvas_win, WINDOW *status_win) {
return 0;
}

/* Mode Pan

* Pans the View with arrow keys
*/
int mode_pan(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_PAN;

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);
}

return 0;
}

////////////////////////////
// SPECIFC MODE FUNCTIONS //
////////////////////////////
1 change: 1 addition & 0 deletions src/fe_modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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);

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

Expand Down