1#include "font_typer.h"
2
3#include "editor.h"
4
5#include <base/log.h>
6#include <base/str.h>
7#include <base/time.h>
8
9#include <engine/keys.h>
10
11#include <game/editor/editor_actions.h>
12
13#include <algorithm>
14
15using namespace std::chrono_literals;
16
17void CFontTyper::CState::Reset()
18{
19 m_Active = false;
20 m_TextIndex = ivec2(0, 0);
21 m_LineStart = std::nullopt;
22 m_pLastLayer = nullptr;
23 m_TilesPlacedSinceActivate = 0;
24}
25
26void CFontTyper::OnInit(CEditor *pEditor)
27{
28 CEditorComponent::OnInit(pEditor);
29
30 m_CursorTextTexture = pEditor->Graphics()->LoadTexture(pFilename: "editor/cursor_text.png", StorageType: IStorage::TYPE_ALL, Flags: 0);
31}
32
33void CFontTyper::SetTile(ivec2 Pos, unsigned char Index, const std::shared_ptr<CLayerTiles> &pLayer)
34{
35 CTile Tile = {
36 .m_Index: Index, // index
37 .m_Flags: 0, // flags
38 .m_Skip: 0, // skip
39 .m_MustBe0: 0, // reserved
40 };
41 pLayer->SetTile(x: Pos.x, y: Pos.y, Tile);
42 Map()->m_FontTyperState.m_TilesPlacedSinceActivate++;
43}
44
45void CFontTyper::PlaceTile(unsigned char Index, const std::shared_ptr<CLayerTiles> &pLayer)
46{
47 CState &State = Map()->m_FontTyperState;
48 // handle cursor behind right column and do line break
49 if(State.m_TextIndex.x == pLayer->m_Width)
50 {
51 if(Index == 0)
52 return;
53 State.m_TextIndex.x = State.m_LineStart.value_or(u: 0);
54 State.m_TextIndex.y++;
55
56 // corner case
57 if(State.m_TextIndex.y >= pLayer->m_Height)
58 {
59 State.m_TextIndex.x = pLayer->m_Width;
60 State.m_TextIndex.y = pLayer->m_Height - 1;
61 return;
62 }
63 }
64
65 if(Index != 0 && (!State.m_LineStart.has_value() || State.m_TextIndex.x < State.m_LineStart))
66 State.m_LineStart = State.m_TextIndex.x;
67 SetTile(Pos: State.m_TextIndex, Index, pLayer);
68 State.m_TextIndex.x++;
69}
70
71bool CFontTyper::OnInput(const IInput::CEvent &Event)
72{
73 CState &State = Map()->m_FontTyperState;
74
75 std::shared_ptr<CLayerTiles> pLayer = std::static_pointer_cast<CLayerTiles>(r: Map()->SelectedLayerType(Index: 0, Type: LAYERTYPE_TILES));
76 if(!pLayer)
77 {
78 if(State.m_Active)
79 TextModeOff();
80 return false;
81 }
82 if(pLayer->m_Image == -1)
83 return false;
84
85 if(!State.m_Active)
86 {
87 if(Event.m_Key == KEY_T && Input()->ModifierIsPressed() && !Ui()->IsPopupOpen() && Editor()->m_Dialog == DIALOG_NONE)
88 {
89 if(pLayer && pLayer->m_KnownTextModeLayer)
90 {
91 TextModeOn();
92 }
93 else
94 {
95 m_ConfirmActivatePopupContext.Reset();
96 m_ConfirmActivatePopupContext.YesNoButtons();
97 str_copy(dst&: m_ConfirmActivatePopupContext.m_aMessage, src: "Enable text mode? Pressing letters and numbers on your keyboard will place tiles.");
98 Ui()->ShowPopupConfirm(X: Ui()->MouseX(), Y: Ui()->MouseY(), pContext: &m_ConfirmActivatePopupContext);
99 }
100 }
101 return false;
102 }
103
104 // only handle key down and not also key up
105 if(!(Event.m_Flags & IInput::FLAG_PRESS))
106 return false;
107
108 if(State.m_LineStart.has_value())
109 State.m_LineStart = std::clamp(val: State.m_LineStart.value(), lo: 0, hi: pLayer->m_Width - 1);
110
111 // handle all ctrl+S binds instead of writing "S"
112 if(Input()->ModifierIsPressed() && Input()->KeyIsPressed(Key: KEY_S))
113 {
114 TextModeOff();
115 return false;
116 }
117
118 if(Input()->ModifierIsPressed() && Input()->KeyIsPressed(Key: KEY_V))
119 {
120 std::string Clipboard = Input()->GetClipboardText();
121 if(!Clipboard.empty())
122 {
123 if(Clipboard.size() > 10000)
124 {
125 Editor()->ShowFileDialogError(pFormat: "The clipboard contains %" PRIzu " characters, please post it in chunks", Clipboard.size());
126 return false;
127 }
128 str_sanitize(str: Clipboard.data());
129 for(auto &Char : Clipboard)
130 {
131 if(Char == '\r')
132 continue;
133 if(Char == '\t')
134 Char = ' ';
135
136 // handle space
137 if(Char == ' ')
138 {
139 PlaceTile(Index: 0, pLayer);
140 }
141 // handle linebreaks
142 else if(Char == '\n')
143 {
144 if(State.m_TextIndex.y < pLayer->m_Height - 1)
145 {
146 State.m_TextIndex.y++;
147 if(State.m_LineStart.has_value())
148 State.m_TextIndex.x = State.m_LineStart.value();
149 }
150 }
151 // handle numbers
152 else if(str_isnum(c: Char))
153 {
154 if(Char == '0')
155 PlaceTile(Index: KEY_0 - KEY_1 + NUMBER_OFFSET, pLayer);
156 else
157 PlaceTile(Index: Char - '1' + NUMBER_OFFSET, pLayer);
158 }
159 else
160 {
161 Char = str_uppercase(c: Char);
162 if(Char >= 'A' && Char <= 'Z')
163 {
164 PlaceTile(Index: Char - 'A' + LETTER_OFFSET, pLayer);
165 }
166 }
167 }
168 }
169 return false;
170 }
171
172 // letters
173 if(Event.m_Key >= KEY_A && Event.m_Key <= KEY_Z)
174 PlaceTile(Index: Event.m_Key - KEY_A + LETTER_OFFSET, pLayer);
175 // numbers
176 if(Event.m_Key >= KEY_1 && Event.m_Key <= KEY_0)
177 PlaceTile(Index: Event.m_Key - KEY_1 + NUMBER_OFFSET, pLayer);
178 if(Event.m_Key >= KEY_KP_1 && Event.m_Key <= KEY_KP_0)
179 PlaceTile(Index: Event.m_Key - KEY_KP_1 + NUMBER_OFFSET, pLayer);
180
181 // deletion
182 if(Event.m_Key == KEY_BACKSPACE && State.m_TextIndex.x > 0)
183 {
184 State.m_TextIndex.x--;
185 SetTile(Pos: State.m_TextIndex, Index: 0, pLayer);
186 }
187 else if(Event.m_Key == KEY_DELETE)
188 {
189 if(State.m_TextIndex.x < pLayer->m_Width)
190 SetTile(Pos: State.m_TextIndex, Index: 0, pLayer);
191 }
192
193 // space
194 if(Event.m_Key == KEY_SPACE)
195 PlaceTile(Index: 0, pLayer);
196 // newline
197 if(Event.m_Key == KEY_RETURN)
198 {
199 State.m_TextIndex.y++;
200 if(State.m_LineStart.has_value())
201 State.m_TextIndex.x = State.m_LineStart.value();
202 }
203
204 // special key navigation
205 if(Event.m_Key == KEY_HOME)
206 {
207 for(int StartIndex = State.m_LineStart.value_or(u: 0); StartIndex < pLayer->m_Width; ++StartIndex)
208 {
209 State.m_TextIndex.x = StartIndex;
210 if(pLayer->GetTile(x: StartIndex, y: State.m_TextIndex.y).m_Index != 0)
211 break;
212 }
213 // no char found
214 if(pLayer->GetTile(x: State.m_TextIndex.x, y: State.m_TextIndex.y).m_Index == 0)
215 State.m_TextIndex.x = State.m_LineStart.value_or(u: 0);
216 }
217 else if(Event.m_Key == KEY_END)
218 {
219 int LastIndex = -1;
220 for(int EndIndex = State.m_LineStart.value_or(u: 0); EndIndex < pLayer->m_Width; ++EndIndex)
221 {
222 if(pLayer->GetTile(x: EndIndex, y: State.m_TextIndex.y).m_Index)
223 LastIndex = EndIndex;
224 }
225 State.m_TextIndex.x = LastIndex >= 0 ? LastIndex + 1 : State.m_LineStart.value_or(u: 0);
226 }
227
228 // arrow key navigation
229 if(Event.m_Key == KEY_LEFT)
230 {
231 State.m_TextIndex.x--;
232 if(Input()->ModifierIsPressed())
233 {
234 while(State.m_TextIndex.x >= 1 && State.m_TextIndex.x <= pLayer->m_Width - 2 && pLayer->GetTile(x: State.m_TextIndex.x, y: State.m_TextIndex.y).m_Index)
235 State.m_TextIndex.x--;
236 }
237 }
238 if(Event.m_Key == KEY_RIGHT)
239 {
240 State.m_TextIndex.x++;
241 if(Input()->ModifierIsPressed())
242 {
243 while(State.m_TextIndex.x >= 1 && State.m_TextIndex.x <= pLayer->m_Width - 2 && pLayer->GetTile(x: State.m_TextIndex.x, y: State.m_TextIndex.y).m_Index)
244 State.m_TextIndex.x++;
245 }
246 }
247 if(Event.m_Key == KEY_UP)
248 State.m_TextIndex.y--;
249 if(Event.m_Key == KEY_DOWN)
250 State.m_TextIndex.y++;
251 State.m_TextIndex.x = std::clamp(val: State.m_TextIndex.x, lo: 0, hi: pLayer->m_Width);
252 State.m_TextIndex.y = std::clamp(val: State.m_TextIndex.y, lo: 0, hi: pLayer->m_Height - 1);
253 m_CursorRenderTime = time_get_nanoseconds() - 501ms;
254 float Dist = distance(
255 a: vec2(State.m_TextIndex.x, State.m_TextIndex.y),
256 b: (Editor()->MapView()->GetWorldOffset() + Editor()->MapView()->GetEditorOffset()) / 32);
257 Dist /= Editor()->MapView()->GetWorldZoom();
258 if(Dist > 10.0f)
259 {
260 Editor()->MapView()->SetWorldOffset(vec2(State.m_TextIndex.x, State.m_TextIndex.y) * 32 - Editor()->MapView()->GetEditorOffset());
261 }
262
263 return false;
264}
265
266void CFontTyper::TextModeOn()
267{
268 std::shared_ptr<CLayerTiles> pLayer = std::static_pointer_cast<CLayerTiles>(r: Map()->SelectedLayerType(Index: 0, Type: LAYERTYPE_TILES));
269 if(!pLayer)
270 return;
271 if(pLayer->m_Image == -1)
272 return;
273
274 SetCursor();
275 Map()->m_FontTyperState.m_TilesPlacedSinceActivate = 0;
276 Map()->m_FontTyperState.m_Active = true;
277 pLayer->m_KnownTextModeLayer = true;
278
279 // hack to not show picker when pressing space
280 Editor()->m_Dialog = DIALOG_PSEUDO_FONT_TYPER;
281}
282
283void CFontTyper::TextModeOff()
284{
285 if(Editor()->m_Dialog == DIALOG_PSEUDO_FONT_TYPER)
286 Editor()->m_Dialog = DIALOG_NONE;
287 if(Map()->m_FontTyperState.m_TilesPlacedSinceActivate)
288 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorBrushDrawAction>(args: Map(), args&: Map()->m_SelectedGroup), pDisplay: "Font typer");
289 Map()->m_FontTyperState.Reset();
290}
291
292void CFontTyper::SetCursor()
293{
294 Map()->m_FontTyperState.m_TextIndex.x = (int)(Editor()->MapView()->MouseWorldPos().x / 32);
295 Map()->m_FontTyperState.m_TextIndex.y = (int)(Editor()->MapView()->MouseWorldPos().y / 32);
296 m_CursorRenderTime = time_get_nanoseconds() - 501ms;
297}
298
299void CFontTyper::Render()
300{
301 CState &State = Map()->m_FontTyperState;
302
303 if(m_ConfirmActivatePopupContext.m_Result == CUi::SConfirmPopupContext::CONFIRMED)
304 {
305 TextModeOn();
306 m_ConfirmActivatePopupContext.Reset();
307 }
308 if(m_ConfirmActivatePopupContext.m_Result != CUi::SConfirmPopupContext::UNSET)
309 m_ConfirmActivatePopupContext.Reset();
310
311 if(!State.m_Active)
312 return;
313
314 if(Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ESCAPE))
315 TextModeOff();
316 str_copy(dst&: Editor()->m_aTooltip, src: "Type on your keyboard to insert letters and numbers. Press Escape to end text mode.");
317
318 std::shared_ptr<CLayerTiles> pLayer = std::static_pointer_cast<CLayerTiles>(r: Map()->SelectedLayerType(Index: 0, Type: LAYERTYPE_TILES));
319 if(!pLayer)
320 return;
321
322 // exit if selected layer changes
323 if(State.m_pLastLayer && State.m_pLastLayer != pLayer)
324 {
325 TextModeOff();
326 State.m_pLastLayer = pLayer;
327 return;
328 }
329 // exit if dialog or edit box pops up
330 if(Editor()->m_Dialog != DIALOG_PSEUDO_FONT_TYPER || CLineInput::GetActiveInput())
331 {
332 TextModeOff();
333 return;
334 }
335 State.m_pLastLayer = pLayer;
336 State.m_TextIndex.x = std::clamp(val: State.m_TextIndex.x, lo: 0, hi: pLayer->m_Width);
337 State.m_TextIndex.y = std::clamp(val: State.m_TextIndex.y, lo: 0, hi: pLayer->m_Height - 1);
338
339 const auto CurTime = time_get_nanoseconds();
340 if((CurTime - m_CursorRenderTime) > 1s)
341 m_CursorRenderTime = time_get_nanoseconds();
342 if((CurTime - m_CursorRenderTime) > 500ms)
343 {
344 std::shared_ptr<CLayerGroup> pGroup = Map()->SelectedGroup();
345 pGroup->MapScreen();
346 Graphics()->WrapClamp();
347 Graphics()->TextureSet(Texture: m_CursorTextTexture);
348 Graphics()->QuadsBegin();
349 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
350 IGraphics::CQuadItem QuadItem(State.m_TextIndex.x * 32, State.m_TextIndex.y * 32, 32.0f, 32.0f);
351 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
352 Graphics()->QuadsEnd();
353 Graphics()->WrapNormal();
354 Ui()->MapScreen();
355 }
356}
357
358bool CFontTyper::IsActive() const
359{
360 return Map()->m_FontTyperState.m_Active;
361}
362