Skip to content

Commit 56cfdc3

Browse files
committed
fixed newline bug, added basic highlighting
1 parent 4e92b2b commit 56cfdc3

File tree

7 files changed

+106
-61
lines changed

7 files changed

+106
-61
lines changed

src/Core/datatypes.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ better::Text better::updateText(better::Text textEdit, char newChar) {
88
const int textHeight {60};
99
const int textWidth {150};
1010
//change the topline number variable if needs to scroll
11-
bool endOfLine {textEdit.cursor.column == textEdit.textEdit[textEdit.cursor.row].size() ? true : false};
12-
bool endOfScreen {textEdit.cursor.column + 1 >= textEdit.topColumnNumber + textWidth ? true : false};
11+
bool endOfLine {textEdit.cursor.column == textEdit.textEdit[textEdit.cursor.row].size()};
12+
bool endOfScreen {textEdit.cursor.column + 1 >= textEdit.topColumnNumber + textWidth};
1313
immer::flex_vector<char> line {endOfLine ? textEdit.textEdit[textEdit.cursor.row].push_back(newChar) : textEdit.textEdit[textEdit.cursor.row].insert(textEdit.cursor.column, newChar)}; //add the character to the line (change to insert unless the cursor is at the end of the line)
14-
return {textEdit.textEdit.set(textEdit.cursor.row,line),{textEdit.cursor.row, textEdit.cursor.column + 1}, {{false,false,false,false},textEdit.data.isShift,textEdit.data.isCaps,textEdit.data.isScroll,textEdit.data.isCtrl,textEdit.data.clearHistory,-1,textEdit.data.menu,textEdit.data.filename}, textEdit.topLineNumber, endOfScreen ? textEdit.topColumnNumber + 1 : textEdit.topColumnNumber, textEdit.highlightStart, textEdit.highlightEnd};
14+
better::Text tempText {textEdit.textEdit.set(textEdit.cursor.row,line),{textEdit.cursor.row, textEdit.cursor.column + 1}, {{false,false,false,false},textEdit.data.isShift,textEdit.data.isCaps,textEdit.data.isScroll,textEdit.data.isCtrl,textEdit.data.clearHistory,-1,textEdit.data.menu,textEdit.data.filename}, textEdit.topLineNumber, endOfScreen ? textEdit.topColumnNumber + 1 : textEdit.topColumnNumber, textEdit.highlightStart, textEdit.highlightEnd};
15+
return tempText;
1516
}
1617

1718
better::Text better::backspace(better::Text text) { //couple of bugs with backspace, figure out once got all chars rendering

src/Core/datatypes.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ better::Cursor findCursorPos(int topLine, int topColumn, SDL_Event event);
2020

2121
struct editorData {
2222
bool menusToDraw[4] = {false, false, false, false};
23-
bool isShift = false;
24-
bool isCaps = false;
25-
bool isScroll = false;
26-
bool isCtrl = false;
27-
bool clearHistory = false;
28-
int index = -1;
23+
bool isShift {false};
24+
bool isCaps {false};
25+
bool isScroll {false};
26+
bool isCtrl {false};
27+
bool clearHistory {false};
28+
int index {-1};
2929
std::vector<std::string> menu;
3030
std::string filename;
3131
const int textHeight {60};

src/Core/fileUtils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ immer::flex_vector<immer::flex_vector<char>> better::readFile(std::string filena
4242

4343
immer::flex_vector<char> better::stringToVector(std::string string) {
4444
immer::flex_vector_transient<char> vector {};
45-
for (const char character: string) {
45+
for (const char character : string) {
4646
vector.push_back(character);
4747
}
4848
return vector.persistent();
@@ -86,7 +86,7 @@ std::filesystem::path better::fileDialog() {
8686
text.highlightEnd = {0,0};
8787

8888
SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0x22, 0x22, 0x22, 0xFF));
89-
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd);
89+
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd, 0x222222FF, 0x5588AAFF, 0x222222FF, 0x5588AAFF, 0x5588AAFF);
9090
better::drawMenuBar(surface, menu, 0xDDDDDDFF, 0x666666FF, 800);
9191
SDL_UpdateWindowSurface(window);
9292
SDL_FillRect(surface, &screen, SDL_MapRGBA(surface->format, 0x22, 0x22, 0x22, 0xFF));
@@ -102,7 +102,7 @@ std::filesystem::path better::fileDialog() {
102102
text.topLineNumber = newText.topLineNumber;
103103
text.topColumnNumber = newText.topColumnNumber;
104104
text.data.isScroll = true;
105-
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd);
105+
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd, 0x222222FF, 0x5588AAFF, 0x222222FF, 0x5588AAFF, 0x5588AAFF);
106106
SDL_UpdateWindowSurface(window);
107107
SDL_FillRect(surface, &screen, SDL_MapRGBA(surface->format, 0x22, 0x22, 0x22, 0xFF));
108108
}
@@ -143,7 +143,7 @@ std::filesystem::path better::fileDialog() {
143143
text.topColumnNumber = 0;
144144
text.topLineNumber = 0;
145145

146-
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd);
146+
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd, 0x222222FF, 0x5588AAFF, 0x222222FF, 0x5588AAFF, 0x5588AAFF);
147147
SDL_UpdateWindowSurface(window);
148148
SDL_FillRect(surface, &screen, SDL_MapRGBA(surface->format, 0x22, 0x22, 0x22, 0xFF));
149149
continue;
@@ -179,7 +179,7 @@ std::filesystem::path better::fileDialog() {
179179
text.topColumnNumber = 0;
180180
text.topLineNumber = 0;
181181

182-
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd);
182+
better::renderText(surface, text.textEdit, text.topLineNumber, text.topColumnNumber, 35, 100, text.highlightStart, text.highlightEnd, 0x222222FF, 0x5588AAFF, 0x222222FF, 0x5588AAFF, 0x5588AAFF);
183183
SDL_UpdateWindowSurface(window);
184184
SDL_FillRect(surface, &screen, SDL_MapRGBA(surface->format, 0x22, 0x22, 0x22, 0xFF));
185185
}

src/Core/main.cpp

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <vector>
99
#include <string>
1010
#include <unordered_map>
11+
#include <iostream>
1112

1213
#include "datatypes.hpp"
1314
#include "fileUtils.hpp"
@@ -16,7 +17,7 @@
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

6768
better::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

7172
void 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

313320
better::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

591598
better::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

629639
better::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

Comments
 (0)