aboutsummaryrefslogtreecommitdiff
path: root/src/editor.cpp
blob: cde860bc5d57a4832d713cf3fd8f78a5f5f0f668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "editor.h"

editor::editor(sPos _size)
{
	setSize(_size.x + 2, _size.y + 2);
	size = _size;

	currentPosition = {0,0};
	currentColor = _COL_WHITE | _COL_BLACK_BG;
	currentChar = ' ';
	currentMode = 0;

	storage = new cObject(size.x, size.y);
	storage->drawRectangle(currentChar, currentChar, {0,0}, {size.x-1, size.y-1}, _COL_BLACK_BG | _COL_WHITE, _COL_BLACK_BG | _COL_WHITE);
	storage->setPosition(1,1);

	drawLine('-', {1,0}, {_size.x, 0}, _COL_DEFAULT);
	drawLine('-', {1, _size.y + 1}, {_size.x, _size.y + 1}, _COL_DEFAULT);
	drawLine('|', {0,1}, {0,_size.y}, _COL_DEFAULT);
	drawLine('|', {_size.x + 1,1}, {_size.x + 1,_size.y}, _COL_DEFAULT);

	drawPoint('+', {0,0} ,_COL_DEFAULT);
	drawPoint('+', {_size.x + 1,0} ,_COL_DEFAULT);
	drawPoint('+', {0,_size.y + 1} ,_COL_DEFAULT);
	drawPoint('+', {_size.x + 1,_size.y + 1} ,_COL_DEFAULT);

	update();
}

editor::~editor()
{
	delete storage;
	destruct();
}

void editor::onChar(unsigned char _c)
{
	storage->drawPoint(_c, currentPosition, _COL_DEFAULT);
	currentChar = _c;
	update();
}

void editor::onClick(sPos _pos, unsigned int _button)
{
	if (_pos.x < 1 || _pos.y < 1 || _pos.x > size.x || _pos.y > size.y)
		return;

	currentPosition = {_pos.x - 1, _pos.y - 1};

	update();
}

void editor::setCursor(sPos _pos)
{

	if (_pos.x < 0 || _pos.y < 0 || _pos.x >= size.x || _pos.y >= size.y)
		return;

	currentPosition = _pos;

	update();
}

void editor::setColor(uint16_t _color)
{
	currentColor = _color;

	update();
}

void editor::setChar(unsigned char _c)
{
	currentChar = _c;

	update();
}

void editor::update()
{
	storage->write(this, {0,0});
	
	int tx = currentPosition.x + 1;
	int ty = currentPosition.y + 1;
	
	drawPoint( cScreen[tx][ty], {tx, ty}, _COL_WHITE_BG | _COL_BLACK);

	char text[5];
	snprintf(text, 4, "#%c#", currentChar);
	drawText(text, {0, size.y + 1}, currentColor);
}

void editor::moveCursor(sPos _relMovement)
{
	setCursor({currentPosition.x + _relMovement.x, currentPosition.y + _relMovement.y});
}

void editor::accept()
{
	switch (currentMode) {
		default:
			storage->drawPoint(currentChar, currentPosition, currentColor);
			update();
			break;
	};
}