@@ -250,6 +250,55 @@ namespace pg
250250
251251 cursorUi->setVisible (isFocused);
252252 }
253+
254+ resetBlinkTimer ();
255+ }
256+
257+ void TextInputSystem::onEvent (const TickEvent& event)
258+ {
259+ // Accumulate time (TickEvent::tick is in milliseconds)
260+ blinkTimer += event.tick / 1000 .0f ;
261+
262+ if (blinkTimer >= blinkInterval)
263+ {
264+ blinkTimer -= blinkInterval;
265+ cursorVisible = !cursorVisible;
266+
267+ for (const auto & entity : viewGroup<TextInputComponent, FocusableComponent>())
268+ {
269+ auto focus = entity->get <FocusableComponent>();
270+ auto text = entity->get <TextInputComponent>();
271+
272+ if (not focus->focused or not text->cursorEntity )
273+ continue ;
274+
275+ auto cursorUi = text->cursorEntity ->get <PositionComponent>();
276+
277+ if (cursorUi)
278+ cursorUi->setVisible (cursorVisible);
279+ }
280+ }
281+ }
282+
283+ void TextInputSystem::resetBlinkTimer ()
284+ {
285+ blinkTimer = 0 .0f ;
286+ cursorVisible = true ;
287+
288+ // Immediately make all focused cursors visible
289+ for (const auto & entity : viewGroup<TextInputComponent, FocusableComponent>())
290+ {
291+ auto focus = entity->get <FocusableComponent>();
292+ auto text = entity->get <TextInputComponent>();
293+
294+ if (not focus->focused or not text->cursorEntity )
295+ continue ;
296+
297+ auto cursorUi = text->cursorEntity ->get <PositionComponent>();
298+
299+ if (cursorUi)
300+ cursorUi->setVisible (true );
301+ }
253302 }
254303
255304 void TextInputSystem::updateCursorVisual (EntityRef entity, CompRef<TextInputComponent> textComp)
@@ -262,6 +311,9 @@ namespace pg
262311 if (not cursorAnchor)
263312 return ;
264313
314+ // Reset the blink timer so cursor stays visible after any interaction
315+ resetBlinkTimer ();
316+
265317 // Compute the X offset of the cursor by summing glyph advances up to cursorPos
266318 float cursorX = 0 .0f ;
267319
@@ -272,16 +324,20 @@ namespace pg
272324
273325 if (ttfSystem)
274326 {
275- const auto & fontChars = ttfSystem->charactersMap [ttf->fontPath ];
276- float scale = ttf->scale ;
277-
278- for (size_t i = 0 ; i < textComp->cursorPos and i < textComp->text .size (); i++)
327+ auto mapIt = ttfSystem->charactersMap .find (ttf->fontPath );
328+ if (mapIt != ttfSystem->charactersMap .end ())
279329 {
280- char c = textComp->text [i];
281- auto it = fontChars.find (c);
282- if (it != fontChars.end ())
330+ const auto & fontChars = mapIt->second ;
331+ float scale = ttf->scale ;
332+
333+ for (size_t i = 0 ; i < textComp->cursorPos and i < textComp->text .size (); i++)
283334 {
284- cursorX += (it->second .advance >> 6 ) * scale;
335+ char c = textComp->text [i];
336+ auto it = fontChars.find (c);
337+ if (it != fontChars.end ())
338+ {
339+ cursorX += (it->second .advance >> 6 ) * scale;
340+ }
285341 }
286342 }
287343 }
0 commit comments