forked from BYOBMO/BMOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTextScroll.cpp
125 lines (112 loc) · 2.36 KB
/
CTextScroll.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "CTextScroll.h"
#include "CText.h"
#include "CSurfWindow.h"
#include "CApplication.h"
CTextScroll::CTextScroll(): CWindow()
{
mUpdate = false;
mFirstLine = 0;
mLastLine = 0;
mOffset = 0;
mPosition = 0;
}
void CTextScroll::AddLine(string text)
{
mText.push_back(text);
mUpdate = true;
}
void CTextScroll::SetScroll(int position)
{
int fontH = TTF_FontHeight(CText::sFontSmall);
mPosition = position;
mUpdate = true;
mFirstLine = position / fontH;
mLastLine = mFirstLine + (mParent->GetHeight() / fontH) + 1;
if (mLastLine > mText.size())
{
mLastLine = mText.size();
}
mOffset = position - (mFirstLine * fontH);
//printf("%d %d %d %d %d\n", position, mFirstLine, mLastLine, mOffset, fontH);
}
void CTextScroll::Draw()
{
int i;
if (mUpdate)
{
SDL_Texture* oldTarget;
CSurfWindow* surf = new CSurfWindow();
int fontH = TTF_FontHeight(CText::sFontSmall);
oldTarget = SDL_GetRenderTarget(CApplication::sRenderer);
SDL_SetRenderTarget(CApplication::sRenderer, mTexture);
SDL_RenderClear(CApplication::sRenderer);
for (i = mFirstLine; i < mLastLine; i++)
{
surf->Y = (i - mFirstLine) * fontH - mOffset;
surf->RenderText(CText::sFontSmall, mText[i], CText::cBlack);
surf->DrawTexture(CApplication::sRenderer);
}
SDL_SetRenderTarget(CApplication::sRenderer, oldTarget);
delete(surf);
mUpdate = false;
}
CTexture::Draw();
}
void CTextScroll::KeyDown(SDL_KeyboardEvent e)
{
if (e.keysym.sym == SDLK_UP)
{
if (mOffset == 0 && mFirstLine == 0)
{
if (mParent != NULL)
{
mParent->KeyDown(e);
}
}
else
{
mPosition = mPosition - 10;
if (mPosition < 0)
{
mPosition = 0;
}
SetScroll(mPosition);
}
}
else if (e.keysym.sym == SDLK_PAGEUP)
{
mPosition = mPosition - mParent->GetHeight();
if (mPosition < 0)
{
mPosition = 0;
}
SetScroll(mPosition);
}
else if (e.keysym.sym == SDLK_PAGEDOWN)
{
mPosition += mParent->GetHeight();
SetScroll(mPosition);
}
else if (e.keysym.sym == SDLK_DOWN)
{
mPosition += 10;
SetScroll(mPosition);
}
else if (mParent != NULL)
{
mParent->KeyDown(e);
return;
}
for (int i = 0; i < mCallbacks.size(); i++)
{
mCallbacks[i](this);
}
}
void CTextScroll::AddTextPositionHandler(std::function<void(CTextScroll*)> callback)
{
mCallbacks.push_back(callback);
}
void CTextScroll::SetSize(int w, int h)
{
CreateBlank(w, h, SDL_TEXTUREACCESS_TARGET);
}