- Treballant en el editor de codi
This commit is contained in:
14
ascii.cpp
14
ascii.cpp
@@ -254,9 +254,9 @@ int main(int argc,char*argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
debug_cursor_blink--;
|
debug_cursor_blink--;
|
||||||
if (debug_cursor_blink == 0) {
|
if (debug_cursor_blink == 0) {
|
||||||
debug_cursor_blink = 30;
|
debug_cursor_blink = 60;
|
||||||
const int pos = cursor_x+cursor_y*screen_width;
|
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();
|
loop();
|
||||||
}
|
}
|
||||||
@@ -266,7 +266,10 @@ int main(int argc,char*argv[]) {
|
|||||||
case 0:
|
case 0:
|
||||||
for (int y=0; y<screen_height; ++y) {
|
for (int y=0; y<screen_height; ++y) {
|
||||||
for (int x=0; x<screen_width; ++x) {
|
for (int x=0; x<screen_width; ++x) {
|
||||||
const uint8_t chr_color = current_color; //COLSCR(x,y);
|
uint8_t chr_color = current_color; //COLSCR(x,y);
|
||||||
|
if ((debug_cursor_blink<30)&&(cursor_x==x)&&(cursor_y==y)) {
|
||||||
|
chr_color = (chr_color>>4)+((chr_color&0x0f)<<4);
|
||||||
|
}
|
||||||
const uint32_t ink_color = palette[chr_color & 0x0f];
|
const uint32_t ink_color = palette[chr_color & 0x0f];
|
||||||
const uint32_t paper_color = palette[chr_color >> 4];
|
const uint32_t paper_color = palette[chr_color >> 4];
|
||||||
const uint8_t chr = CHRSCR(x,y);
|
const uint8_t chr = CHRSCR(x,y);
|
||||||
@@ -581,6 +584,11 @@ void prev_cmd() {
|
|||||||
get_cmd_by_index();
|
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) {
|
uint8_t ascii(const char *str, uint8_t index) {
|
||||||
return str[index];
|
return str[index];
|
||||||
}
|
}
|
||||||
|
|||||||
1
ascii.h
1
ascii.h
@@ -183,6 +183,7 @@ void debug(const char *str, const bool newline=true);
|
|||||||
void debug_get_cmd();
|
void debug_get_cmd();
|
||||||
void next_cmd();
|
void next_cmd();
|
||||||
void prev_cmd();
|
void prev_cmd();
|
||||||
|
void debug_set_cursor(uint8_t x, uint8_t y);
|
||||||
|
|
||||||
uint8_t ascii(const char *str, uint8_t index);
|
uint8_t ascii(const char *str, uint8_t index);
|
||||||
const char* chr(uint8_t ascii);
|
const char* chr(uint8_t ascii);
|
||||||
|
|||||||
211
main.cpp
211
main.cpp
@@ -3,15 +3,41 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
|
std::list<std::string> code;
|
||||||
|
|
||||||
|
int current_editor = 0;
|
||||||
|
|
||||||
void init_terminal();
|
void init_terminal();
|
||||||
void do_terminal();
|
void do_terminal();
|
||||||
|
|
||||||
|
void init_code_editor();
|
||||||
|
void do_code_editor();
|
||||||
|
void save_code();
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
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();
|
do_terminal();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
do_code_editor();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute_run() {
|
void execute_run() {
|
||||||
//if (current_editor == 1) save_code();
|
if (current_editor == 1) save_code();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t get_char(uint8_t key) {
|
uint8_t get_char(uint8_t key) {
|
||||||
@@ -71,3 +97,186 @@ void do_terminal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void save_code() {
|
||||||
|
const char* file = get_filename();
|
||||||
|
FILE *f = fopen(file, "w");
|
||||||
|
for (std::list<std::string>::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<fsize;++pos) {
|
||||||
|
if (buffer[pos] == '\n') {
|
||||||
|
buffer[pos]='\0';
|
||||||
|
code.push_back(&buffer[start]);
|
||||||
|
start=pos+1;
|
||||||
|
} else if (buffer[pos] == '\r') {
|
||||||
|
buffer[pos]='\0';
|
||||||
|
code.push_back(&buffer[start]);
|
||||||
|
start=pos+1;
|
||||||
|
if (buffer[start] == '\n') {pos++; start++;}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::list<std::string>::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<std::string>::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<std::string>::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<std::string>::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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user