Skip to content

Commit 23d6cd8

Browse files
author
JohnPaul
committed
scrolling up and down fully implemented
1 parent e50c584 commit 23d6cd8

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

src/editor.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ enum programState runEditor()
8181
{
8282
editor_ScrollDownUnwrapped();
8383
editor.redrawText = true;
84-
// while(kb_IsDown(kb_KeyDown)) kb_Scan();
84+
}
85+
if(kb_IsDown(kb_KeyUp))
86+
{
87+
editor_ScrollUpUnwrapped();
88+
editor.redrawText = true;
8589
}
8690
}
8791

@@ -112,12 +116,15 @@ void drawEditorBackground()
112116

113117
void drawEditorText()
114118
{
119+
dbg_printf("drawing lines:\n");
115120
fontlib_SetForegroundColor(black);
116121
for(int i = 0, y = 20; i < MAX_LINES_ON_EDITOR_SCREEN; i++, y+= 15)
117122
{
118123
fontlib_SetCursorPosition(2, y);
119124
drawLine(editor.linePointers[i], editor.lineLengths[editor.lineOffset + i]);
125+
dbg_printf("line %d, len %d\n", i, (editor.lineLengths[editor.lineOffset + i]));
120126
}
127+
dbg_printf("\n");
121128
}
122129

123130
struct menuBar *loadEditorMenuBar()
@@ -278,7 +285,7 @@ char *editor_LoadWord(char *readPos, int *lenBuffer, int *widthBuffer, int maxWi
278285
return readPos;
279286
}
280287

281-
char* editor_LoadLine(char *readPos, int *lenBuffer)
288+
char* editor_LoadWrappedLine(char *readPos, int *lenBuffer)
282289
{
283290
char *prevReadPos;
284291
int lineLen = 0, lineWidth = 0;
@@ -432,7 +439,7 @@ void drawLine(char *start, int len)
432439
}
433440
}
434441

435-
bool editor_ScrollDown(void)
442+
bool editor_ScrollDownWrapped(void)
436443
{
437444
char *lastLine = editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN -1];
438445
int lastLineLen = editor.lineLengths[editor.lineOffset + MAX_LINES_ON_EDITOR_SCREEN - 1];
@@ -534,7 +541,6 @@ bool editor_ScrollDownUnwrapped(void)
534541
for(int i = 0; i < MAX_LINES_ON_EDITOR_SCREEN - 1; i++)
535542
{
536543
editor.linePointers[i] = editor.linePointers[i + 1];
537-
editor.lineLengths[i] = editor.lineLengths[i + 1];
538544
}
539545

540546
// increment the line offset
@@ -549,6 +555,31 @@ bool editor_ScrollDownUnwrapped(void)
549555
return true;
550556
}
551557

558+
bool editor_ScrollUpUnwrapped(void)
559+
{
560+
// if we can't scroll up, too bad so sad
561+
if(editor.lineOffset < 1)
562+
{
563+
return false;
564+
}
565+
566+
// shift all the line pointers down one
567+
for(int i = (MAX_LINES_ON_EDITOR_SCREEN - 1); i >= 1; i--)
568+
{
569+
editor.linePointers[i] = editor.linePointers[i - 1];
570+
}
571+
572+
// decrement the line offset
573+
editor.lineOffset--;
574+
575+
// to find the pointer to the line we are scrolling up to,
576+
// subtract its stored length from the pointer to the line right below it
577+
editor.linePointers[0] = editor.linePointers[1] - editor.lineLengths[editor.lineOffset];
578+
579+
// we scrolled up, so success!
580+
return true;
581+
}
582+
552583
void editor_LoadUnwrappedScreen(char *startingPtr, int startingLine)
553584
{
554585
int length;

src/editor.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ char *editor_LoadWord(char *readPos, int *lenBuffer, int *widthBuffer, int maxWi
8989
* @param lenBuffer pointer to variable to hold the line width
9090
* @return returns a pointer to the next line, or NULL if there are no more lines
9191
*/
92-
char* editor_LoadLine(char *readPos, int *lenBuffer);
92+
char* editor_LoadWrappedLine(char *readPos, int *lenBuffer);
9393

9494
// returns the pointer to the start of the next line without word wrapping
9595
// stores the line's length (in characters) in lenBuffer
@@ -102,19 +102,24 @@ char *getNextBufferChar(char *prev);
102102

103103
char *getPrevBufferChar(char *cur);
104104

105-
// draws a line of text taking into account the split buffer
105+
// draws a line of text with a maximum length 'len' and taking into account the split buffer
106106
void drawLine(char *start, int len);
107107

108108
// scrolls down 1 line
109-
// only used if settings specify word wrapping
109+
// used if settings specify word wrapping
110110
// returns true if you can scroll down, false if you can't
111-
bool editor_ScrollDown(void);
111+
bool editor_ScrollDownWrapped(void);
112112

113113
// scrolls down 1 line
114-
// only used if settings specify no word wrapping
114+
// used if settings specify no word wrapping
115115
// returns true if you can scroll down, false if you can't
116116
bool editor_ScrollDownUnwrapped(void);
117117

118+
// scrolls up 1 line
119+
// used if settings specify no word wrapping
120+
// returns true if you can scroll up, false if you can't
121+
bool editor_ScrollUpUnwrapped(void);
122+
118123
#ifdef __cplusplus
119124
}
120125
#endif

0 commit comments

Comments
 (0)