edit.js: completely new method of cursor moving thus everything is broken now :)

This commit is contained in:
minjaesong
2021-04-17 18:03:08 +09:00
parent 9465482fe2
commit 28d2fb6181

View File

@@ -235,13 +235,9 @@ function appendText(code) {
else { else {
let s = textbuffer[cursorRow].substring(0); let s = textbuffer[cursorRow].substring(0);
textbuffer[cursorRow] = s.substring(0, cursorCol) + String.fromCharCode(code) + s.substring(cursorCol); textbuffer[cursorRow] = s.substring(0, cursorCol) + String.fromCharCode(code) + s.substring(cursorCol);
//textbuffer[cursorRow] += String.fromCharCode(code);
} }
nextCol(); cursorMoveRelative(1,0);
drawTextLine(cursorRow - scroll);
drawLnCol();
gotoText();
} }
function appendLine() { function appendLine() {
@@ -267,69 +263,106 @@ function appendLine() {
drawTextbuffer(); drawTextbuffer();
} }
function nextCol() { function backspaceOnce() {
if (cursorCol < textbuffer[cursorRow].length) { // delete a linebreak
cursorCol += 1; cursoringCol = cursorCol; // do NOT inc/dec on cursoringCol if (cursorCol == 0 && cursorRow > 0) {
updateScrollState(true, false); let s1 = textbuffer[cursorRow - 1];
let s2 = textbuffer[cursorRow];
textbuffer.splice(cursorRow - 1, 2, s1+s2);
cursorMoveRelative(0,-1); cursorMoveRelative(Number.MAX_SAFE_INTEGER, 0); cursorHorAbsolute(s1.length);
}
// delete a character
else if (cursorCol > 0) {
let s = textbuffer[cursorRow].substring(0);
textbuffer[cursorRow] = s.substring(0, cursorCol - 1) + s.substring(cursorCol);
cursorMoveRelative(-1,0);
} }
} }
function prevCol() { // this one actually cares about the current scrolling stats
if (cursorCol > 0) { function cursorMoveRelative(odx, ody) {
cursorCol -= 1; cursoringCol = cursorCol; // do NOT inc/dec on cursoringCol //gotoText(); // update cursor pos
updateScrollState(true, false);
}
}
function nextRow() {
if (cursorRow < textbuffer.length - 1) {
cursorRow += 1;
updateScrollState(false, true);
}
}
function prevRow() {
if (cursorRow > 0) {
cursorRow -= 1;
updateScrollState(false, true);
}
}
function updateScrollState(hor, vert) {
gotoText(); // update cursor pos
let cursorPos = con.getyx(); let cursorPos = con.getyx();
let cx = cursorPos[1]; let cy = cursorPos[0];
let dx = odx; let dy = ody;
let px = cursorPos[1]; let py = cursorPos[0];
let nx = px + dx; let ny = py + dy;
let oldScroll = scroll;
let oldScrollHor = scrollHor;
// clamp dx/dy
if (cursorCol + dx > Math.min(cursoringCol, textbuffer[cursorRow].length))
dx = Math.min(cursoringCol, textbuffer[cursorRow].length) - cursorCol + 1;
else if (cursorCol + dx < 0)
dx = -cursorCol;
if (cursorRow + dy > textbuffer.length)
dy = textbuffer.length - cursorRow;
else if (cursorCol + dy < 0)
dy = -cursorRow;
// move editor cursor
cursorRow += dy;
if (cursorRow < 0) cursorRow = 0;
else if (cursorRow >= textbuffer.length) cursorRow = textbuffer.length - 1;
let tlen = textbuffer[cursorRow].length; let tlen = textbuffer[cursorRow].length;
serial.println(`paintSize: ${paintWidth}x${paintHeight}, cursYX:${cursorPos} cursCol: ${cursorCol} scrH: ${scrollHor}`); cursorCol += dx; cursoringCol = cursorCol;
if (cursorCol < 0) cursorCol = 0;
else if (cursorCol >= tlen + 1) cursorCol = tlen + 1;
// update horizontal scroll stats // update horizontal scroll stats
if (hor) { if (dx != 0) {
if (cursorCol >= paintWidth - 1 && cx == paintWidth - 1 + PAINT_START_X) { if (nx > paintWidth) {
scrollHor += 1; scrollHor += (nx - paintWidth);
drawTextLine(cursorRow - scroll); nx = paintWidth;
} }
else if (scrollHor > 0 && cursorCol == scrollHor && cx < PAINT_START_X + 1) { else if (nx < 0) {
scrollHor -= 1; scrollHor += nx; // plus-assigning because nx is less than zero
drawTextLine(cursorRow - scroll); nx = 0;
} }
} }
// update vertical scroll stats // update vertical scroll stats
if (vert) { if (dy != 0) {
cursorCol = Math.min(tlen, cursoringCol); // move the column pointer intelligently, just like any decent text editor would do if (ny > paintHeight) {
scroll += (ny - paintWidth - scrollPeek);
if (cursorRow >= paintHeight - 1 && cy == paintHeight - 1 + PAINT_START_Y) { ny = paintWidth - scrollPeek
scroll += 1;
drawTextbuffer();
drawLineNumbers();
} }
else if (scroll > 0 && cursorRow == scroll && cy < PAINT_START_Y + 1) { else if (ny < 0) {
scroll -= 1; let scrollToTop = (scroll - dy <= 0);
drawTextbuffer(); if (scrollToTop) {
drawLineNumbers(); scroll = 0;
}
else {
scroll += ny + paintHeight - scrollPeek; // ny is less than zero
}
ny = 0;
} }
} }
// update screendraw
if (oldScroll != scroll) {
drawTextbuffer(); drawLineNumbers();
}
else if (oldScrollHor != scrollHor) {
drawTextLine(ny);
}
gotoText();
}
// will try to put the cursor at the right end of the screen as long as the text length is longer than the window width
function cursorHorAbsolute(pos) {
let position = pos
if (position > textbuffer[cursorRow].length) textbuffer[cursorRow].length;
cursorCol = position;
scrollHor = position - paintWidth + 2;
if (scrollHor < 0) scrollHor = 0;
drawTextLine(cursorRow - scroll);
} }
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -357,49 +390,37 @@ while (!exit) {
displayBulletin(`Wrote ${textbuffer.length} lines`); displayBulletin(`Wrote ${textbuffer.length} lines`);
} }
else if (key == con.KEY_BACKSPACE) { // Bksp else if (key == con.KEY_BACKSPACE) { // Bksp
backspaceOnce(); drawLnCol(); gotoText();
} }
else if (key == con.KEY_RETURN) { // Return else if (key == con.KEY_RETURN) { // Return
appendLine(); drawLineNumbers(); drawLnCol(); gotoText(); appendLine(); drawLineNumbers(); drawLnCol(); gotoText();
} }
else if (key == con.KEY_LEFT) { else if (key == con.KEY_LEFT) {
prevCol(); drawLnCol(); gotoText(); cursorMoveRelative(-1,0);
} }
else if (key == con.KEY_RIGHT) { else if (key == con.KEY_RIGHT) {
nextCol(); drawLnCol(); gotoText(); cursorMoveRelative(1,0);
} }
else if (key == con.KEY_UP) { else if (key == con.KEY_UP) {
prevRow(); drawLnCol(); gotoText(); cursorMoveRelative(0,-1);
} }
else if (key == con.KEY_DOWN) { else if (key == con.KEY_DOWN) {
nextRow(); drawLnCol(); gotoText(); cursorMoveRelative(0,1);
} }
else if (key == con.KEY_PAGE_UP) { else if (key == con.KEY_PAGE_UP) {
cursorRow -= paintHeight - 1; cursorMoveRelative(0, -paintHeight + 1);
scroll -= paintHeight - 1;
if (cursorRow < 0) cursorRow = 0;
if (scroll < 0) scroll = 0;
drawTextbuffer(); drawLineNumbers(); drawLnCol(); gotoText();
} }
else if (key == con.KEY_PAGE_DOWN) { else if (key == con.KEY_PAGE_DOWN) {
cursorRow += paintHeight - 1; cursorMoveRelative(0, paintHeight - 1);
scroll += paintHeight - 1;
if (cursorRow > textbuffer.length - 1) cursorRow = textbuffer.length - 1;
if (scroll > textbuffer.length - paintHeight + 2) scroll = textbuffer.length - paintHeight + 2;
else if (scroll < 0) scroll = 0;
drawTextbuffer(); drawLineNumbers(); drawLnCol(); gotoText();
} }
else if (key == con.KEY_HOME) { else if (key == con.KEY_HOME) {
cursorCol = 0; scrollHor = 0; cursoringCol = cursorCol; cursorMoveRelative(-Number.MAX_SAFE_INTEGER, 0);
drawTextLine(cursorRow - scroll); drawLnCol(); gotoText();
} }
else if (key == con.KEY_END) { else if (key == con.KEY_END) {
cursorCol = textbuffer[cursorRow].length; cursorMoveRelative(Number.MAX_SAFE_INTEGER, 0);
scrollHor = textbuffer[cursorRow].length - paintWidth + 2;
if (scrollHor < 0) scrollHor = 0;
drawTextLine(cursorRow - scroll); drawLnCol(); gotoText();
} }
else if (key >= 32 && key < 128) { // printables (excludes \n) else if (key >= 32 && key < 128) { // printables (excludes \n)
appendText(key); appendText(key); drawLnCol(); gotoText();
} }
} }