-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fragment.cpp
72 lines (63 loc) · 1.38 KB
/
Fragment.cpp
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
#include "SSD1306.h"
#include "Fragments.h"
/**
* @author Saurav Sajeev
*/
FragmentManager::FragmentManager(OLED &oled) : oled(&oled) {}
OLED *FragmentManager::getOLED()
{
return oled;
}
Fragment::Fragment(FragmentManager &manager)
: manager(manager), drawableCount(0), drawableCapacity(2)
{
drawables = new Drawable *[drawableCapacity];
}
Fragment::~Fragment()
{
for (int i = 0; i < drawableCount; ++i)
{
delete drawables[i];
}
delete[] drawables;
}
void Fragment::add(Drawable *drawable)
{
if (drawableCount >= drawableCapacity)
{
drawableCapacity *= 2;
Drawable **newDrawables = new Drawable *[drawableCapacity];
for (int i = 0; i < drawableCount; ++i)
{
newDrawables[i] = drawables[i];
yield();
}
if (drawables != nullptr)
delete[] drawables;
drawables = newDrawables;
}
drawables[drawableCount++] = drawable;
}
void Fragment::inflate()
{
OLED &oled = *manager.getOLED();
for (int i = 0; i < drawableCount; ++i)
{
yield();
drawables[i]->draw(oled);
}
lastCount = drawableCount;
}
void Fragment::recycle()
{
OLED &oled = *manager.getOLED();
for (int i = lastCount - 1; i < drawableCount; ++i)
{
drawables[i]->draw(oled);
}
lastCount = drawableCount;
}
void Fragment::detach()
{
delete this;
}