88#include < vector>
99#include < string>
1010#include < unordered_map>
11+ #include < iostream>
1112
1213#include " datatypes.hpp"
1314#include " fileUtils.hpp"
1617
1718// Progress bar for saving, etc
1819
19- namespace better { // TODO: highlighting text, shortcuts
20+ namespace better { // TODO: highlighting
2021
2122// ! better::verticalNav scrolls the text vertically based on arrow keys. This is done incrementing or decrementing the variable topLine.
2223// ! The topLine variable tells the render function to start rendering at that line in the text buffer.
@@ -66,7 +67,7 @@ better::Text deleteHighlighted(better::Text text);
6667
6768better::Text handleKey (better::Text text, char key);
6869
69- void edit1 (SDL_Window* window, std::string filename, const int textHeight, const int textWidth);
70+ void edit1 (SDL_Window* window, std::string filename, const int textHeight, const int textWidth, Uint32 colorbg, Uint32 colorfg, Uint32 colorhighlight, Uint32 colorparens, Uint32 colorcomments );
7071
7172void copyClipboard (better::Text text);
7273
@@ -96,9 +97,14 @@ int main(int argc, char* argv[]) {
9697 1200 ,960 ,0 );
9798
9899 const int textHeight {60 };
99- const int textWidth{150 };
100+ const int textWidth {150 };
101+ Uint32 colorbg {0x222222FF };
102+ Uint32 colorfg {0x5588AAFF };
103+ Uint32 colorhighlight {0x444444FF };
104+ Uint32 colorparens {0xAA6969FF };
105+ Uint32 colorcomments {0x666666FF };
100106
101- better::edit1 (window, filename, textHeight, textWidth);
107+ better::edit1 (window, filename, textHeight, textWidth, colorbg, colorfg, colorhighlight, colorparens, colorcomments );
102108 return 0 ;
103109}
104110
@@ -176,7 +182,7 @@ better::Text better::cutClipboard(better::Text text) {
176182 return better::deleteHighlighted (text);
177183}
178184
179- void better::edit1 (SDL_Window* window, std::string filename, const int textHeight, const int textWidth) {
185+ void better::edit1 (SDL_Window* window, std::string filename, const int textHeight, const int textWidth, Uint32 colorbg, Uint32 colorfg, Uint32 colorhighlight, Uint32 colorparens, Uint32 colorcomments ) {
180186 std::vector<better::Text> texts {};
181187
182188 SDL_Cursor* guiCursor {SDL_CreateSystemCursor (SDL_SYSTEM_CURSOR_IBEAM)};
@@ -189,11 +195,11 @@ void better::edit1(SDL_Window* window, std::string filename, const int textHeigh
189195 std::string menus {" File Edit View Settings " };
190196 std::vector<std::vector<std::string>> menuText {{" Open" ," Save" ," Exit" },{" Cut" ," Copy" ," Paste" },{},{" Edit Settings" }};
191197
192- better::Text firstText {better::readFile (filename), {0 ,0 }, {{false ,false ,false ,false }, false , false , false , false , false , -1 , {}, filename}, 0 , 0 };
198+ better::Text firstText {better::readFile (filename), {0 ,0 }, {{false ,false ,false ,false }, false , false , false , false , false , -1 , {}, filename}, 0 , 0 , { 0 , 0 }, { 0 , 0 } };
193199 texts.push_back (firstText);
194200
195201 SDL_FillRect (surface, NULL , SDL_MapRGBA (surface->format , 0x22 , 0x22 , 0x22 , 0xFF ));
196- better::renderText (surface, texts.back ().textEdit , texts.back ().topLineNumber , texts.back ().topColumnNumber , textHeight, textWidth, {0 ,0 }, {0 ,0 });
202+ better::renderText (surface, texts.back ().textEdit , texts.back ().topLineNumber , texts.back ().topColumnNumber , textHeight, textWidth, {0 ,0 }, {0 ,0 }, colorbg, colorfg, colorhighlight, colorparens, colorcomments );
197203 better::renderCursor (surface, texts.back ().cursor .column , texts.back ().cursor .row , texts.back ().topLineNumber , texts.back ().topColumnNumber );
198204 better::drawMenuBar (surface, menus, 0xDDDDDDFF , 0x666666FF , 1200 );
199205
@@ -241,7 +247,8 @@ void better::edit1(SDL_Window* window, std::string filename, const int textHeigh
241247 }
242248
243249 else if (event.type == SDL_KEYDOWN || event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEWHEEL || event.type == SDL_MOUSEBUTTONUP) {
244- better::Text tempText {(handlers[event.type ])(texts.back (), event, surface, guiCursor)};
250+ better::Text textTemp {texts.back ().textEdit , {texts.back ().cursor .row ,texts.back ().cursor .column }, {{texts.back ().data .menusToDraw [0 ],texts.back ().data .menusToDraw [1 ],texts.back ().data .menusToDraw [2 ],texts.back ().data .menusToDraw [3 ]}, texts.back ().data .isShift , texts.back ().data .isCaps , texts.back ().data .isScroll , texts.back ().data .isCtrl , texts.back ().data .clearHistory , texts.back ().data .index , texts.back ().data .menu , texts.back ().data .filename }, texts.back ().topLineNumber , texts.back ().topColumnNumber , texts.back ().highlightStart , texts.back ().highlightEnd };
251+ better::Text tempText {(handlers[event.type ])(textTemp, event, surface, guiCursor)};
245252 if (tempText.data .clearHistory ) {
246253 texts.clear ();
247254 texts.push_back (tempText);
@@ -260,7 +267,7 @@ void better::edit1(SDL_Window* window, std::string filename, const int textHeigh
260267 }
261268
262269 SDL_FillRect (surface, &screen, SDL_MapRGBA (surface->format , 0x22 , 0x22 , 0x22 , 0xFF ));
263- better::renderText (surface, texts.back ().textEdit , texts.back ().topLineNumber , texts.back ().topColumnNumber , textHeight, textWidth, texts.back ().highlightStart , texts.back ().highlightEnd );
270+ better::renderText (surface, texts.back ().textEdit , texts.back ().topLineNumber , texts.back ().topColumnNumber , textHeight, textWidth, texts.back ().highlightStart , texts.back ().highlightEnd , colorbg, colorfg, colorhighlight, colorparens, colorcomments );
264271 if (!texts.back ().data .isScroll ) {
265272 better::renderCursor (surface, texts.back ().cursor .column , texts.back ().cursor .row , texts.back ().topLineNumber , texts.back ().topColumnNumber );
266273 }
@@ -272,7 +279,7 @@ void better::edit1(SDL_Window* window, std::string filename, const int textHeigh
272279 texts.back ().data .menu = menuText[texts.back ().data .index ];
273280 }
274281
275- better::drawMenus (surface, texts.back ().data .menu , 0xDDDDDDFF , 0x444444FF );
282+ better::drawMenus (surface, texts.back ().data .menu , 0xDDDDDDFF , colorhighlight );
276283
277284 SDL_UpdateWindowSurface (window);
278285 }
@@ -312,10 +319,12 @@ better::Text better::mouseWheel(better::Text text, SDL_Event event, SDL_Surface*
312319
313320better::Text better::keyDown (better::Text text, SDL_Event event, SDL_Surface* surface, SDL_Cursor* guiCursor) {
314321 if (event.key .keysym .scancode == SDL_SCANCODE_DOWN || event.key .keysym .scancode == SDL_SCANCODE_UP) {
322+ text.highlightStart = text.highlightEnd ;
315323 return better::verticalNav (text, event.key .keysym .scancode , text.data .textHeight , text.data .textWidth );
316324 }
317325
318326 else if (event.key .keysym .scancode == SDL_SCANCODE_RIGHT || event.key .keysym .scancode == SDL_SCANCODE_LEFT) {
327+ text.highlightStart = text.highlightEnd ;
319328 return better::horizontalNav (text, event.key .keysym .scancode , text.data .textHeight , text.data .textWidth );
320329 }
321330 else if (event.key .keysym .scancode == SDL_SCANCODE_RETURN) {
@@ -391,7 +400,6 @@ better::Text better::keyDown(better::Text text, SDL_Event event, SDL_Surface* su
391400 text.cursor .row = 0 ;
392401 better::resetMenus (text.data .menusToDraw );
393402 }
394-
395403 if ((text.highlightStart .row != text.highlightEnd .row ) || (text.highlightEnd .column > text.highlightStart .column )) {
396404 return better::handleKey (better::deleteHighlighted (text),key);
397405 }
@@ -415,7 +423,6 @@ better::Text better::handleKey(better::Text text, char key) {
415423 return better::horizontalNav (better::updateText (better::updateText (text,' \' ' ),' \' ' ),SDL_SCANCODE_LEFT, text.data .textHeight , text.data .textWidth );
416424 case ' \" ' :
417425 return better::horizontalNav (better::updateText (better::updateText (text,key),' \" ' ),SDL_SCANCODE_LEFT, text.data .textHeight , text.data .textWidth );
418- case ' \t ' :
419426 default :
420427 if (key == ' (' || key == ' <' || key == ' [' ) {
421428 return better::autoBracket (text,key);
@@ -589,78 +596,84 @@ char better::shift(char key) {
589596}
590597
591598better::Text better::verticalNav (better::Text text, SDL_Keycode key, const int textHeight, const int textWidth) {
599+ better::Cursor cursor {text.cursor };
592600 switch (key) {
593601 case SDL_SCANCODE_DOWN:
594- if (text. cursor .row < text.textEdit .size ()) {
595- if ((text. cursor .row == text.textEdit .size () - 1 ) || (text. cursor .row == text.topLineNumber + textHeight - 1 && text.topLineNumber + textHeight - 1 >= text.textEdit .size () - 1 )) {
602+ if (cursor.row < text.textEdit .size ()) {
603+ if ((cursor.row == text.textEdit .size () - 1 ) || (cursor.row == text.topLineNumber + textHeight - 1 && text.topLineNumber + textHeight - 1 >= text.textEdit .size () - 1 )) {
596604 return text;
597605 }
598- if (text.textEdit [text.cursor .row + 1 ].size () < text.cursor .column ) { // check the next row has less elements than current column of cursor position
599- text.cursor .column = text.textEdit [text.cursor .row + 1 ].size ();
600- text.cursor .row += 1 ;
606+ if (text.textEdit [cursor.row + 1 ].size () < cursor.column ) { // check the next row has less elements than current column of cursor position
607+ cursor.column -= 1 ;
608+ cursor.column = text.textEdit [cursor.row + 1 ].size ();
609+ cursor.row += 1 ;
601610 }
602611 else {
603- text. cursor .row += 1 ;
612+ cursor.row += 1 ;
604613 }
605- if (text. cursor .row == text.topLineNumber + textHeight - 1 && text.topLineNumber + textHeight - 1 < text.textEdit .size ()) {
614+ if (cursor.row == text.topLineNumber + textHeight - 1 && text.topLineNumber + textHeight - 1 < text.textEdit .size ()) {
606615 text.topLineNumber += 1 ; // scroll down
607616 }
608617 }
609618 break ;
610619
611620 case SDL_SCANCODE_UP:
612621 if (text.cursor .row ) {
613- if ((text. cursor .row == text.topLineNumber ) && text. cursor .row ) {
622+ if ((cursor.row == text.topLineNumber ) && cursor.row ) {
614623 text.topLineNumber -= 1 ;
615624 }
616- if (text.textEdit [text.cursor .row - 1 ].size () < text. cursor .column ) { // check the next row has less elements than current column of cursor position
617- text. cursor .column = text.textEdit [text. cursor .row - 1 ].size ();
618- text. cursor .row -= 1 ;
625+ if (text.textEdit [text.cursor .row - 1 ].size () < cursor.column ) { // check the next row has less elements than current column of cursor position
626+ cursor.column = text.textEdit [cursor.row - 1 ].size ();
627+ cursor.row -= 1 ;
619628 }
620629 else {
621- text. cursor .row -= 1 ;
630+ cursor.row -= 1 ;
622631 }
623632 }
624633 break ;
625634 }
635+ text.cursor = cursor;
626636 return text;
627637}
628638
629639better::Text better::horizontalNav (better::Text text, SDL_Keycode key, const int textHeight, const int textWidth) { // cases for if cursor is at top right/bottom left of screen
640+ better::Cursor cursor{text.cursor };
630641 switch (key) {
631642 case SDL_SCANCODE_RIGHT:
632- if ((text.cursor .row != text.textEdit .size () - 1 ) || (text.cursor .row == text.textEdit .size () - 1 && text.cursor .column < text.textEdit [text.cursor .row ].size ())) {
633- if (text.cursor .column == text.textEdit [text.cursor .row ].size ()) {
634- text.cursor .column = 0 ;
635- text.cursor .row += 1 ;
643+ if ((cursor.row != text.textEdit .size () - 1 ) || (cursor.row == text.textEdit .size () - 1 && cursor.column < text.textEdit [cursor.row ].size ())) {
644+ if (cursor.column == text.textEdit [cursor.row ].size ()) {
645+ cursor.column -= 1 ;
646+ cursor.column = 0 ;
647+ cursor.row += 1 ;
636648 text.topColumnNumber = 0 ;
637649 }
638650 else {
639- text. cursor .column += 1 ;
651+ cursor.column += 1 ;
640652 }
641- if (text. cursor .column == text.topColumnNumber + textWidth) {
653+ if (cursor.column == text.topColumnNumber + textWidth) {
642654 text.topColumnNumber += 1 ;
643655 }
644656 }
645657 break ;
646658
647659 case SDL_SCANCODE_LEFT:
648- if ((text. cursor .row ) || (!text. cursor .row && text. cursor .column )) {
649- if (!text. cursor .column ) {
650- text. cursor .row -= 1 ;
651- text. cursor .column = text.textEdit [text. cursor .row ].size ();
652- if (text. cursor .column > textWidth - 2 ) {
653- text.topColumnNumber = (text. cursor .column - textWidth) + 1 ;
660+ if ((cursor.row ) || (!cursor.row && cursor.column )) {
661+ if (!cursor.column ) {
662+ cursor.row -= 1 ;
663+ cursor.column = text.textEdit [cursor.row ].size ();
664+ if (cursor.column > textWidth - 2 ) {
665+ text.topColumnNumber = (cursor.column - textWidth) + 1 ;
654666 }
655667 }
656668 else {
657- text. cursor .column -= 1 ;
669+ cursor.column -= 1 ;
658670 }
659- if ((text. cursor .column == text.topColumnNumber ) && (text. cursor .column )) {
671+ if ((cursor.column == text.topColumnNumber ) && (cursor.column )) {
660672 text.topColumnNumber -= 1 ;
661673 }
662674 }
663675 break ;
664676 }
677+ text.cursor = cursor;
665678 return text;
666679}
0 commit comments