-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextEditor.c
149 lines (120 loc) · 4.67 KB
/
TextEditor.c
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 <[email protected]>
// 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"
#define MAX_TEXT_NUM 1024
static int s_FontSize = 16;
static int s_PosX;
static int s_PosY;
static int s_Width;
static int s_Height;
static TextureManager s_TextureManagerForCommitted;
static int s_TextCommitted[MAX_TEXT_NUM];
static int s_TextCommittedNum = 0;
static Texture2D s_TextureCommitted;
static TextureManager s_TextureManagerForPreedit;
static Texture2D s_TexturePreedit;
static bool s_NeedToUpdateTexture = false;
#ifdef MANAGE_PREEDIT_CANDIDATE
static TextureManager s_TextureManagerForCandidate;
static Texture2D s_TexturePreeditCandidate;
static void OnPreeditCandidateChanged(int** candidates, int* lengths, int pageSize, int selectedIndex)
{
s_TexturePreeditCandidate = TextureManager_CreateTextureForCandidate(&s_TextureManagerForCandidate, candidates, lengths, pageSize, selectedIndex);
}
#endif
static void OnPreeditChanged(int* preedit, int length)
{
s_TexturePreedit = TextureManager_CreateTexture(&s_TextureManagerForPreedit, preedit, length);
}
static int CursorHight()
{
// It might be possible to get this padding from FreeType, but I don't know how.
const int padding = 2;
return s_FontSize + padding;
}
bool TextEditor_Init(int posX, int posY, int width, int height)
{
s_PosX = posX;
s_PosY = posY;
s_Width = width;
s_Height = height;
bool hasSucceeded = 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);
#ifdef MANAGE_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;
}
void TextEditor_Update()
{
int unicodePoint = 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;
}
}
void TextEditor_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);
#ifdef MANAGE_PREEDIT_CANDIDATE
DrawTexture(s_TexturePreeditCandidate,
s_PosX + s_TextureManagerForCommitted.m_CursorPosX,
s_PosY + s_TextureManagerForCommitted.m_CursorPosY + s_FontSize + 10,
WHITE);
#endif
}