diff --git a/ascii.cpp b/ascii.cpp index fa3d599..d9114c8 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -254,9 +254,9 @@ int main(int argc,char*argv[]) { } else { debug_cursor_blink--; if (debug_cursor_blink == 0) { - debug_cursor_blink = 30; + debug_cursor_blink = 60; const int pos = cursor_x+cursor_y*screen_width; - char_screen[pos] = char_screen[pos]==32 ? 95 : 32; + //char_screen[pos] = char_screen[pos]==32 ? 95 : 32; } loop(); } @@ -266,7 +266,10 @@ int main(int argc,char*argv[]) { case 0: for (int y=0; y>4)+((chr_color&0x0f)<<4); + } const uint32_t ink_color = palette[chr_color & 0x0f]; const uint32_t paper_color = palette[chr_color >> 4]; const uint8_t chr = CHRSCR(x,y); @@ -581,6 +584,11 @@ void prev_cmd() { get_cmd_by_index(); } +void debug_set_cursor(uint8_t x, uint8_t y) { + cursor_x = x; + cursor_y = y; +} + uint8_t ascii(const char *str, uint8_t index) { return str[index]; } diff --git a/ascii.h b/ascii.h index d1a055d..7539a4f 100644 --- a/ascii.h +++ b/ascii.h @@ -183,6 +183,7 @@ void debug(const char *str, const bool newline=true); void debug_get_cmd(); void next_cmd(); void prev_cmd(); +void debug_set_cursor(uint8_t x, uint8_t y); uint8_t ascii(const char *str, uint8_t index); const char* chr(uint8_t ascii); diff --git a/main.cpp b/main.cpp index a3a0008..7222c0d 100644 --- a/main.cpp +++ b/main.cpp @@ -3,15 +3,41 @@ #include #include +std::list code; + +int current_editor = 0; + void init_terminal(); void do_terminal(); +void init_code_editor(); +void do_code_editor(); +void save_code(); + void loop() { - do_terminal(); + if (btnp(KEY_TAB)) { + current_editor = (++current_editor)%2; + switch(current_editor) { + case 0: + init_terminal(); + break; + case 1: + init_code_editor(); + break; + } + } + switch(current_editor) { + case 0: + do_terminal(); + break; + case 1: + do_code_editor(); + break; + } } void execute_run() { - //if (current_editor == 1) save_code(); + if (current_editor == 1) save_code(); } uint8_t get_char(uint8_t key) { @@ -31,7 +57,7 @@ uint8_t get_char(uint8_t key) { else if (key == KEY_4) { if (mods & KMOD_SHIFT) { return '$'; } else if (mods & KMOD_RALT) { return '~'; } else { return '4'; } } else if (key == KEY_5) { if (mods & KMOD_SHIFT) { return '%'; } else if (mods & KMOD_RALT) { return 180; } else { return '5'; } } else if (key == KEY_6) { if (mods & KMOD_SHIFT) { return '&'; } else if (mods & KMOD_RALT) { return 173; } else { return '6'; } } - else if (key == KEY_7) { if (mods & KMOD_SHIFT) { return '/'; } else { return '7'; } } + else if (key ==KEY_7) { if (mods & KMOD_SHIFT) { return '/'; } else { return '7'; } } else if (key == KEY_8) { if (mods & KMOD_SHIFT) { return '('; } else { return '8'; } } else if (key == KEY_9) { if (mods & KMOD_SHIFT) { return ')'; } else { return '9'; } } else if (key == KEY_SPACE) return 32; @@ -71,3 +97,186 @@ void do_terminal() { } } } + +void save_code() { + const char* file = get_filename(); + FILE *f = fopen(file, "w"); + for (std::list::iterator it = code.begin(); it != code.end(); it++) { + fprintf(f, "%s\n", (*it).c_str()); + } + fclose(f); +} + +void load_code() { + const char* file = get_filename(); + FILE *f = fopen(file, "rb"); + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); /* same as rewind(f); */ + char *buffer = (char*)malloc(fsize + 1); + fread(buffer, 1, fsize, f); + fclose(f); + buffer[fsize] = 0; + + int start = 0; + for (int pos=0;pos::iterator ls[28]; +static int col = 0; +static int line = 0; + +void refresh_code_editor() { + color(COLOR_WHITE, COLOR_BLUE); + cls(); + //print(" File Edit Run ",0,0); + //print(" ",0,29); + std::list::iterator it = code.begin(); + int i=0; + while (it != code.end() && i<28) { + ls[i] = it; + print((*it).c_str(), 0, i); + ++i; ++it; + } +} + +void init_code_editor() { + mode(0); + code.clear(); + load_code(); + refresh_code_editor(); +} + +std::string get_current_line() { + return *ls[line]; +} + +void move_cursor_up() { + if (line > 0) { + line--; + if (get_current_line().size() < col) col = get_current_line().size(); + } +} + +void move_cursor_down() { + if (line < code.size()-1) { + line++; + if (get_current_line().size() < col) col = get_current_line().size(); + } +} + +void move_cursor_right() { + if ( col < get_current_line().size() ) { + col++; + } else { + if (line < code.size()-1) { + col = 0; + move_cursor_down(); + } + } +} + +void move_cursor_left() { + if (col > 0) { + col--; + } else { + if (line > 0) { + move_cursor_up(); + col = get_current_line().size(); + } + } +} + +void move_line_end() { + col = get_current_line().size(); +} + +void move_line_home() { + col = 0; +} + +void split_line() { + std::string str = get_current_line(); + *ls[line] = str.substr(col); + std::list::iterator newline = ls[line]++; + code.insert(newline, str.substr(0, col)); + + line++; col=0; + refresh_code_editor(); +} + +void join_lines() { + std::string str = get_current_line(); + //std::list::iterator prevline = ls[line]--; + code.erase(ls[line]); + line--; + const int line_size = (*ls[line]).size(); + *ls[line] += str; + col = line_size; + refresh_code_editor(); +} + +void delete_char() { + if (col == 0) { + if (line > 0) join_lines(); + } else { + std::string str = get_current_line(); + std::string a = str.substr(0,col-1); + std::string b = str.substr(col); + *ls[line] = a+b; + col--; + refresh_code_editor(); + } +} + +void supr_char() { + if ((col == (*ls[line]).size()) && (line == code.size()-1)) return; + move_cursor_right(); + delete_char(); +} + +void add_char(uint8_t chr) { + std::string str = get_current_line(); + std::string a = str.substr(0,col); + std::string b = str.substr(col); + std::string c = " "; c[0] = chr; + *ls[line] = a+c+b; + col++; + refresh_code_editor(); +} + +void do_code_editor() { + const uint8_t key = whichbtn(); + if (key != KEY_UNKNOWN) { + if (key == KEY_RETURN or key == KEY_KP_ENTER) { + split_line(); + } + else if (key == KEY_UP) move_cursor_up(); + else if (key == KEY_DOWN) move_cursor_down(); + else if (key == KEY_LEFT) move_cursor_left(); + else if (key == KEY_RIGHT) move_cursor_right(); + else if (key == KEY_BACKSPACE) delete_char(); + else if (key == KEY_DELETE) supr_char(); + else if (key == KEY_END) move_line_end(); + else if (key == KEY_HOME) move_line_home(); + else { + uint8_t chr = get_char(key); + if (chr != 0) add_char(chr); + } + } + + debug_set_cursor(col, line); +} \ No newline at end of file