mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-10 23:04:04 +09:00
edit.js: text cursor moves intelligently on navigating lines, but scrolling is still wip
This commit is contained in:
@@ -33,6 +33,7 @@ let cursorCol = 0;
|
|||||||
let exit = false;
|
let exit = false;
|
||||||
let scene = -1; // -1: main, 0: filemenu, 1: editmenu , ...
|
let scene = -1; // -1: main, 0: filemenu, 1: editmenu , ...
|
||||||
let bulletinShown = false;
|
let bulletinShown = false;
|
||||||
|
let cursoringCol = 0;
|
||||||
|
|
||||||
// load existing file if it's there
|
// load existing file if it's there
|
||||||
let editingExistingFile = filesystem.open(driveLetter, filePath, "R");
|
let editingExistingFile = filesystem.open(driveLetter, filePath, "R");
|
||||||
@@ -58,6 +59,7 @@ function reset_status() {
|
|||||||
textbuffer = [""];
|
textbuffer = [""];
|
||||||
cursorRow = 0;
|
cursorRow = 0;
|
||||||
cursorCol = 0;
|
cursorCol = 0;
|
||||||
|
cursoringCol = cursorCol;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DRAWING FUNCTIONS //
|
// DRAWING FUNCTIONS //
|
||||||
@@ -255,6 +257,7 @@ function appendLine() {
|
|||||||
// go to the next line
|
// go to the next line
|
||||||
cursorRow += 1;
|
cursorRow += 1;
|
||||||
cursorCol = 0;
|
cursorCol = 0;
|
||||||
|
cursoringCol = cursorCol;
|
||||||
|
|
||||||
if (cursorRow >= windowHeight - scrollPeek) {
|
if (cursorRow >= windowHeight - scrollPeek) {
|
||||||
scroll += 1;
|
scroll += 1;
|
||||||
@@ -266,19 +269,33 @@ function appendLine() {
|
|||||||
|
|
||||||
function nextCol() {
|
function nextCol() {
|
||||||
if (cursorCol < textbuffer[cursorRow].length) {
|
if (cursorCol < textbuffer[cursorRow].length) {
|
||||||
cursorCol += 1;
|
cursorCol += 1; cursoringCol = cursorCol; // do NOT inc/dec on cursoringCol
|
||||||
updateScrollState();
|
updateScrollState(true, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function prevCol() {
|
function prevCol() {
|
||||||
if (cursorCol > 0) {
|
if (cursorCol > 0) {
|
||||||
cursorCol -= 1;
|
cursorCol -= 1; cursoringCol = cursorCol; // do NOT inc/dec on cursoringCol
|
||||||
updateScrollState();
|
updateScrollState(true, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateScrollState() {
|
function nextRow() {
|
||||||
|
if (cursorRow < textbuffer.length) {
|
||||||
|
cursorRow += 1;
|
||||||
|
updateScrollState(false, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function prevRow() {
|
||||||
|
if (cursorRow > 0) {
|
||||||
|
cursorRow -= 1;
|
||||||
|
updateScrollState(false, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateScrollState(hor, vert) {
|
||||||
gotoText(); // update cursor pos
|
gotoText(); // update cursor pos
|
||||||
let cursorPos = con.getyx();
|
let cursorPos = con.getyx();
|
||||||
let cx = cursorPos[1]; let cy = cursorPos[0];
|
let cx = cursorPos[1]; let cy = cursorPos[0];
|
||||||
@@ -287,17 +304,30 @@ function updateScrollState() {
|
|||||||
serial.println(`paintSize: ${paintWidth}x${paintHeight}, cursYX:${cursorPos} cursCol: ${cursorCol} scrH: ${scrollHor}`);
|
serial.println(`paintSize: ${paintWidth}x${paintHeight}, cursYX:${cursorPos} cursCol: ${cursorCol} scrH: ${scrollHor}`);
|
||||||
|
|
||||||
// update horizontal scroll stats
|
// update horizontal scroll stats
|
||||||
if (cursorCol >= paintWidth - 1 && cx == paintWidth - 1 + PAINT_START_X) {
|
if (hor) {
|
||||||
scrollHor += 1;
|
if (cursorCol >= paintWidth - 1 && cx == paintWidth - 1 + PAINT_START_X) {
|
||||||
drawTextLine(cursorRow - scroll);
|
scrollHor += 1;
|
||||||
}
|
drawTextLine(cursorRow - scroll);
|
||||||
else if (scrollHor > 0 && cursorCol == scrollHor && cx < PAINT_START_X + 1) {
|
}
|
||||||
scrollHor -= 1;
|
else if (scrollHor > 0 && cursorCol == scrollHor && cx < PAINT_START_X + 1) {
|
||||||
drawTextLine(cursorRow - scroll);
|
scrollHor -= 1;
|
||||||
|
drawTextLine(cursorRow - scroll);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update vertical scroll stats
|
// update vertical scroll stats
|
||||||
|
if (vert) {
|
||||||
|
cursorCol = Math.min(tlen, cursoringCol); // move the column pointer intelligently, just like any decent text editor would do
|
||||||
|
|
||||||
|
if (cursorRow >= paintHeight - 1 && cy == paintHeight - 1 + PAINT_START_Y) {
|
||||||
|
scrollHor += 1;
|
||||||
|
drawTextbuffer();
|
||||||
|
}
|
||||||
|
else if (scroll > 0 && cursorRow == scroll && cy < PAINT_START_Y + 1) {
|
||||||
|
scrollHor -= 1;
|
||||||
|
drawTextbuffer();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -335,8 +365,14 @@ while (!exit) {
|
|||||||
else if (key == con.KEY_RIGHT && cursorCol < textbuffer[cursorRow].length) {
|
else if (key == con.KEY_RIGHT && cursorCol < textbuffer[cursorRow].length) {
|
||||||
nextCol(); drawLnCol(); gotoText();
|
nextCol(); drawLnCol(); gotoText();
|
||||||
}
|
}
|
||||||
|
else if (key == con.KEY_UP && cursorRow > 0) {
|
||||||
|
prevRow(); drawLnCol(); gotoText();
|
||||||
|
}
|
||||||
|
else if (key == con.KEY_DOWN && cursorRow < textbuffer.length) {
|
||||||
|
nextRow(); drawLnCol(); gotoText();
|
||||||
|
}
|
||||||
else if (key == con.KEY_HOME) {
|
else if (key == con.KEY_HOME) {
|
||||||
cursorCol = 0; scrollHor = 0;
|
cursorCol = 0; scrollHor = 0; cursoringCol = cursorCol;
|
||||||
drawTextLine(cursorRow - scroll); drawLnCol(); gotoText();
|
drawTextLine(cursorRow - scroll); drawLnCol(); gotoText();
|
||||||
}
|
}
|
||||||
else if (key == con.KEY_END) {
|
else if (key == con.KEY_END) {
|
||||||
|
|||||||
Reference in New Issue
Block a user