-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
207 lines (166 loc) · 6.08 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2022 鏡原 尚 <kagamihara@sena-networks.co.jp>
// 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 "raylib.h"
#include <string.h>
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "config.h"
#include "TextEditor.h"
static bool s_InitAsFullscreen = false;
static int s_ScreenWidth = 800;
static int s_ScreenHeight = 450;
// The sizes of the screen as windowed (not fullscreened).
// Used for toggling windowed/fullscreened.
static int s_WindowedWidth = 800;
static int s_WindowedHeight = 450;
// Call "ToggleFullscreen" after this frame count.
// On X11, updating the window size is not applied in the same frame,
// so need this to toggle to the fullscreen with the specified size.
static int s_DelayFrameCountToFullscreen = 0;
static bool ParseArgs(int argc, char** argv)
{
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-f") == 0)
{
printf("Fullscreen.\n");
s_InitAsFullscreen = true;
SetConfigFlags(FLAG_FULLSCREEN_MODE);
continue;
}
if (strcmp(argv[i], "-r") == 0)
{
printf("Resizable.\n");
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
continue;
}
printf("Options:\n");
printf(" -f use full screen\n");
printf(" -r make window resizable\n");
return false;
}
return true;
}
static bool IsTogglingFullscreen()
{
if (s_DelayFrameCountToFullscreen > 0)
return true;
return false;
}
static void DrawUtils()
{
int cursorX;
int cursorY;
int cursorWidth;
int cursorHeight;
GetPreeditCursorRectangle(&cursorX, &cursorY, &cursorWidth, &cursorHeight);
bool isImeOn = IsImeOn();
int x = 10;
int y = 5;
DrawText(TextFormat("Screen Size (w, h): (%d, %d)", s_ScreenWidth, s_ScreenHeight),
x, y, 20, BLACK);
y += 22;
if (isImeOn)
DrawText("IME status: ON", x, y, 20, BLACK);
else
DrawText("IME status: OFF", x, y, 20, BLACK);
y += 22;
DrawText(TextFormat("preedit cursor (x, y, w, h): (%d, %d, %d, %d)", cursorX, cursorY, cursorWidth, cursorHeight),
x, y, 20, BLACK);
y += 22;
y += 5;
if (GuiButton((Rectangle){ x, y, 200, 30 }, "Toggle IME Status"))
SetImeStatus(!isImeOn);
x += 210;
if (GuiButton((Rectangle){ x, y, 200, 30 }, "Reset Preedit"))
ResetPreedit();
x += 210;
if (GuiButton((Rectangle){ x, y, 200, 30 }, "Toggle Fullscreen"))
{
if (!IsTogglingFullscreen())
{
if (IsWindowFullscreen())
{
ToggleFullscreen();
SetWindowSize(s_WindowedWidth, s_WindowedHeight);
}
else
{
const int monitorIndex = GetCurrentMonitor();
SetWindowSize(GetMonitorWidth(monitorIndex), GetMonitorHeight(monitorIndex));
// On X11, updating the window size is not applied in the same frame,
// so need to call "ToggleFullscreen" after some frames.
s_DelayFrameCountToFullscreen = 3;
}
}
}
}
int main(int argc, char** argv)
{
// Initialization
//--------------------------------------------------------------------------------------
if (!ParseArgs(argc, argv))
return -1;
const int editorStartY = 125;
const int editorMargin = 10;
#ifdef USE_SOFT_FULLSCREEN
SetConfigFlags(FLAG_SOFT_FULLSCREEN);
#endif
#ifdef MANAGE_PREEDIT_CANDIDATE
SetConfigFlags(FLAG_MANAGE_PREEDIT_CANDIDATE);
#endif
if (!s_InitAsFullscreen)
InitWindow(s_ScreenWidth, s_ScreenHeight, "RaylibIMEInputSampleApp");
else
{
InitWindow(0, 0, "RaylibIMEInputSampleApp");
s_ScreenWidth = GetScreenWidth();
s_ScreenHeight = GetScreenHeight();
}
bool hasSucceeded = TextEditor_Init(editorMargin, editorStartY + editorMargin,
s_ScreenWidth - 2 * editorMargin,
s_ScreenHeight - editorStartY - 2 * editorMargin);
if (!hasSucceeded)
{
printf("Error: TextEditor failed to initialize.\n");
return -1;
}
SetTargetFPS(30);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (s_DelayFrameCountToFullscreen > 0 && --s_DelayFrameCountToFullscreen == 0)
ToggleFullscreen();
s_ScreenWidth = GetScreenWidth();
s_ScreenHeight = GetScreenHeight();
TextEditor_Update();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawUtils();
TextEditor_Draw();
DrawFPS(700, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}