-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextio.d
112 lines (93 loc) · 1.87 KB
/
textio.d
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
module textio;
import std.conv;
import dlib.core.memory;
import dlib.image.color;
import dgl.core.api;
import dgl.core.event;
import dgl.core.application;
import dgl.templates.app3d;
import dgl.ui.ftfont;
import dgl.ui.textline;
class TextListener: EventListener
{
dchar[100] arr;
string str;
uint pos = 0;
this(EventManager emngr)
{
super(emngr);
}
override void onTextInput(dchar code)
{
if (pos < 100)
{
arr[pos] = code;
pos++;
}
}
override void onKeyDown(int key)
{
if (key == SDLK_BACKSPACE)
back();
str = toString();
}
void reset()
{
arr[] = 0;
pos = 0;
}
void back()
{
if (pos > 0)
pos--;
}
override string toString()
{
return to!string(arr[0..pos]);
}
}
class TextIOApp: Application3D
{
FreeTypeFont font;
TextLine text;
TextListener tlistener;
this()
{
super();
font = New!FreeTypeFont("data/DroidSans.ttf", 20);
text = New!TextLine(font, "Hello, World!");
text.color = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
auto entityText = createEntity2D(text);
entityText.position.x = 10;
entityText.position.y = 10;
tlistener = New!TextListener(eventManager);
text.setText(tlistener.str);
}
~this()
{
Delete(tlistener);
Delete(text);
Delete(font);
}
override void onUpdate(double dt)
{
super.onUpdate(dt);
tlistener.processEvents();
text.setText(tlistener.str);
}
override void onKeyDown(int key)
{
if (key == SDLK_ESCAPE)
{
exit();
}
}
}
void main()
{
initDGL();
auto app = New!TextIOApp();
app.run();
Delete(app);
deinitDGL();
}