Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTextEditor.c
More file actions
Latest commit
149 lines (120 loc) · 4.67 KB
/
Copy pathTextEditor.c
File metadata and controls
149 lines (120 loc) · 4.67 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 2022 Daijiro Fukuda <fukuda@clear-code.com>
// This file is part of RaylibIMEInputSampleApp.
// RaylibIMEInputSampleApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// RaylibIMEInputSampleApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include"TextEditor.h"
#include"TextureManager.h"
#include"PreeditManager.h"
#defineMAX_TEXT_NUM 1024
staticints_FontSize=16;
staticints_PosX;
staticints_PosY;
staticints_Width;
staticints_Height;
staticTextureManagers_TextureManagerForCommitted;
staticints_TextCommitted[MAX_TEXT_NUM];
staticints_TextCommittedNum=0;
staticTexture2Ds_TextureCommitted;
staticTextureManagers_TextureManagerForPreedit;
staticTexture2Ds_TexturePreedit;
staticbools_NeedToUpdateTexture= false;
#ifdefMANAGE_PREEDIT_CANDIDATE
staticTextureManagers_TextureManagerForCandidate;
staticTexture2Ds_TexturePreeditCandidate;
staticvoidOnPreeditCandidateChanged(int**candidates, int*lengths, intpageSize, intselectedIndex)
{
s_TexturePreeditCandidate=TextureManager_CreateTextureForCandidate(&s_TextureManagerForCandidate, candidates, lengths, pageSize, selectedIndex);
}
#endif
staticvoidOnPreeditChanged(int*preedit, intlength)
{
s_TexturePreedit=TextureManager_CreateTexture(&s_TextureManagerForPreedit, preedit, length);
}
staticintCursorHight()
{
// It might be possible to get this padding from FreeType, but I don't know how.
constintpadding=2;
returns_FontSize+padding;
}
boolTextEditor_Init(intposX, intposY, intwidth, intheight)
{
s_PosX=posX;
s_PosY=posY;
s_Width=width;
s_Height=height;
boolhasSucceeded=TextureManager_Initialize(&s_TextureManagerForCommitted, s_Width, s_Height, s_FontSize);
if (!hasSucceeded)
{
printf("Error: TextureManager failed to initialize.\n");
return false;
}
hasSucceeded=TextureManager_Initialize(&s_TextureManagerForPreedit, 400, 200, s_FontSize);
if (!hasSucceeded)
{
printf("Error: TextureManager failed to initialize.\n");
return false;
}
PreeditManager_Init();
PreeditManager_UpdateCursorRectangle(s_PosX, s_PosY, 1, CursorHight());
PreeditManager_SetOnPreeditChanged(OnPreeditChanged);
#ifdefMANAGE_PREEDIT_CANDIDATE
hasSucceeded=TextureManager_Initialize(&s_TextureManagerForCandidate, 400, 400, s_FontSize);
if (!hasSucceeded)
{
printf("Error: TextureManager failed to initialize.\n");
return false;
}
PreeditManager_SetOnPreeditCandidateChanged(OnPreeditCandidateChanged);
#endif
return true;
}
voidTextEditor_Update()
{
intunicodePoint=GetCharPressed();
while (unicodePoint>0)
{
if (unicodePoint >= 32&&s_TextCommittedNum<MAX_TEXT_NUM)
{
s_TextCommitted[s_TextCommittedNum++] =unicodePoint;
s_NeedToUpdateTexture= true;
}
unicodePoint=GetCharPressed();
}
if (IsKeyPressed(KEY_BACKSPACE) &&s_TextCommittedNum>0)
{
s_TextCommittedNum--;
s_NeedToUpdateTexture= true;
}
}
voidTextEditor_Draw()
{
if (s_NeedToUpdateTexture)
{
s_TextureCommitted=TextureManager_CreateTexture(&s_TextureManagerForCommitted, s_TextCommitted, s_TextCommittedNum);
PreeditManager_UpdateCursorRectangle(s_PosX+s_TextureManagerForCommitted.m_CursorPosX,
s_PosY+s_TextureManagerForCommitted.m_CursorPosY,
1,
CursorHight());
s_NeedToUpdateTexture= false;
}
DrawRectangleLines(s_PosX, s_PosY, s_Width, s_Height, BLACK);
DrawTexture(s_TextureCommitted, s_PosX, s_PosY, WHITE);
DrawTexture(s_TexturePreedit,
s_PosX+s_TextureManagerForCommitted.m_CursorPosX,
s_PosY+s_TextureManagerForCommitted.m_CursorPosY,
WHITE);
#ifdefMANAGE_PREEDIT_CANDIDATE
DrawTexture(s_TexturePreeditCandidate,
s_PosX+s_TextureManagerForCommitted.m_CursorPosX,
s_PosY+s_TextureManagerForCommitted.m_CursorPosY+s_FontSize+10,
WHITE);
#endif
}