Skip to content
This repository was archived by the owner on Sep 23, 2022. It is now read-only.

Commit 7b666ef

Browse files
author
OverPoweredDev
committed
got backspace and delete working
1 parent 096b2bf commit 7b666ef

13 files changed

Lines changed: 164 additions & 66 deletions

editor/Content.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Content.h"
2+
3+
Content::Content(Document *doc, Cursor *cur) {
4+
this->document = doc;
5+
this->cursor = cur;
6+
}
7+
8+
void Content::shiftLeft() {
9+
if (cursor->x > OFFSET_X) {
10+
cursor->x -= cursor->charWidth;
11+
cursor->charNumber -= 1;
12+
}
13+
}
14+
15+
void Content::shiftRight() {
16+
if (cursor->charNumber < LINE_LIMIT && cursor->x < SCREEN_WIDTH) {
17+
cursor->x += cursor->charWidth;
18+
cursor->charNumber += 1;
19+
}
20+
}
21+
22+
void Content::shiftUp() {
23+
if (cursor->y > 0) {
24+
cursor->y -= cursor->height;
25+
cursor->lineNumber -= 1;
26+
}
27+
}
28+
29+
void Content::shiftDown() {
30+
if (cursor->lineNumber < document->lineCount) {
31+
cursor->y += cursor->height;
32+
cursor->lineNumber += 1;
33+
}
34+
}
35+
36+
void Content::shiftPos(int lineNum, int charNum) {
37+
if (charNum < LINE_LIMIT && lineNum < document->lineCount) {
38+
cursor->x = OFFSET_X + (cursor->charWidth * charNum);
39+
cursor->y = cursor->height * lineNum;
40+
cursor->lineNumber = lineNum;
41+
cursor->charNumber = charNum;
42+
}
43+
}
44+

editor/Content.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef LOOP_EDITORVIEW_H
2+
#define LOOP_EDITORVIEW_H
3+
4+
#include "Utility.h"
5+
#include "Document.h"
6+
#include "Cursor.h"
7+
8+
class Content {
9+
private:
10+
Document *document;
11+
Cursor *cursor;
12+
13+
public:
14+
Content(Document *doc, Cursor *cur);
15+
16+
//cursor shifting
17+
void shiftUp();
18+
void shiftDown();
19+
void shiftLeft();
20+
void shiftRight();
21+
void shiftPos(int lineNum, int charNum);
22+
23+
//getters
24+
int getCharHeight();
25+
int getCharWidth();
26+
};
27+
28+
29+
#endif //LOOP_EDITORVIEW_H

editor/Cursor.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,11 @@ void Cursor::fill(u32 *screen_pixels) {
1313
}
1414
}
1515

16-
void Cursor::shiftLeft() {
17-
if (x > OFFSET_X) {
18-
this->x -= charWidth;
19-
this->charNumber -= 1;
20-
}
21-
}
22-
23-
void Cursor::shiftRight() {
24-
if(charNumber < LINE_LIMIT && x < SCREEN_WIDTH) {
25-
this->x += charWidth;
26-
this->charNumber += 1;
27-
}
28-
}
29-
30-
void Cursor::shiftUp() {
31-
this->y -= height;
32-
this->lineNumber -= 1;
33-
}
34-
35-
void Cursor::shiftDown() {
36-
this->y += height;
37-
this->lineNumber += 1;
38-
}
39-
4016
//getters
41-
int Cursor::getLineNumber(){
17+
int Cursor::getLineNumber() {
4218
return lineNumber;
4319
}
4420

45-
int Cursor::getCharNumber(){
21+
int Cursor::getCharNumber() {
4622
return charNumber;
4723
}

editor/Cursor.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#define L00P_RECTANGLE_H
33

44
#include "Utility.h"
5+
#include "Document.h"
56

67
class Cursor {
7-
private:
8+
public:
89
int x = OFFSET_X;
910
int y = 0;
1011
int width = 2;
@@ -14,20 +15,11 @@ class Cursor {
1415
int lineNumber = 0;
1516
u32 color = 30;
1617

17-
public:
1818
Cursor(TTF_Font *font);
19-
2019
void fill(u32 *screen_pixels);
2120

22-
void shiftUp();
23-
void shiftDown();
24-
void shiftLeft();
25-
void shiftRight();
26-
27-
//getters
2821
int getLineNumber();
2922
int getCharNumber();
30-
int getLineHeight();
3123
};
3224

3325

editor/Document.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Document::init(string filename) {
1717

1818
for (int i = 0; i < lineCount; i++) {
1919
lineBuffer[i] = (char *) malloc(80 * sizeof(char));
20-
memset(lineBuffer[i], ' ', 80);
20+
memset(lineBuffer[i], ' ', 80 * sizeof(char));
2121
}
2222

2323
//reset cursor
@@ -26,9 +26,9 @@ void Document::init(string filename) {
2626

2727
while (getline(inFile, line)) {
2828
for (char c: line)
29-
lineBuffer[numLine][numChar++] = c;
29+
insertPos(numLine, numChar++, c);
3030
numChar = 0;
31-
numLine += 1;
31+
numLine++;
3232
}
3333

3434
this->documentHasChanged = false;
@@ -62,12 +62,37 @@ void Document::printToConsole() {
6262

6363

6464
//editing
65-
void Document::insert(int lineNum, int charNum, char c) {
66-
lineBuffer[lineNum][charNum] = c;
65+
void Document::createLine(int lineNum, int numLines) {
66+
int prevCount = lineCount;
67+
lineCount += numLines;
68+
realloc(lineBuffer, lineCount * sizeof(char *));
69+
70+
//allocate memory for empty lines
71+
for (int i = prevCount; i < lineCount; i++) {
72+
lineBuffer[i] = (char *) malloc(80 * sizeof(char));
73+
memset(lineBuffer[i], ' ', 80 * sizeof(char));
74+
}
75+
76+
//shift any existing lines down
77+
for (int i = lineCount - 1; i < lineNum; i--) {
78+
shiftLineDown(i);
79+
}
80+
}
81+
82+
void Document::shiftLineDown(int lineNum) {
83+
lineBuffer[lineNum + 1] = lineBuffer[lineNum];
84+
memset(lineBuffer[lineNum], ' ', 80 * sizeof(char));
85+
}
86+
87+
void Document::insertPos(int lineNum, int charNum, char c) {
88+
if (isValid(c))
89+
lineBuffer[lineNum][charNum] = c;
6790
}
6891

6992
void Document::deletePos(int lineNum, int charNum) {
70-
lineBuffer[lineNum][charNum] = ' ';
93+
for(int i = charNum+1; i < 80; i++)
94+
lineBuffer[lineNum][i-1] = lineBuffer[lineNum][i];
95+
lineBuffer[lineNum][80] = ' ';
7196
}
7297

7398

editor/Document.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
using namespace std;
88

99
class Document {
10-
private:
10+
public:
1111
char **lineBuffer;
1212
bool documentHasChanged;
1313
int lineCount = 0;
1414

15-
public:
1615
//file handling
1716
void init(std::string filename);
1817
void saveFile(std::string filename);
@@ -21,7 +20,9 @@ class Document {
2120
void printToConsole();
2221

2322
//editing
24-
void insert(int lineNum, int charNum, char c);
23+
void createLine(int lineNum, int numLines);
24+
void shiftLineDown(int lineNum);
25+
void insertPos(int lineNum, int charNum, char c);
2526
void deletePos(int lineNum, int charNum);
2627

2728
//getters

editor/Utility.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef L00P_UTILITY_H
2+
#define L00P_UTILITY_H
3+
4+
#include <SDL2/SDL.h>
5+
#include <SDL2/SDL_ttf.h>
6+
#include <string>
7+
#include <iostream>
8+
#include <cassert>
9+
10+
#define SCREEN_WIDTH 1024
11+
#define SCREEN_HEIGHT 576
12+
#define COMMON_COLOR_MAX 255
13+
#define OFFSET_X 40
14+
#define LINE_LIMIT 80
15+
#define SDLK_ENTER 13
16+
17+
typedef uint32_t u32;
18+
19+
static bool isValid(char c) {
20+
if (c >= '!' && c <= '~')
21+
return true;
22+
return false;
23+
}
24+
25+
#endif //L00P_UTILITY_H

fonts/DejaVuSansMono.ttf

-333 KB
Binary file not shown.

fonts/FreeMono.ttf

-579 KB
Binary file not shown.

fonts/RobotoMono-Medium.ttf

112 KB
Binary file not shown.

0 commit comments

Comments
 (0)