Mercurial > games > semicongine
changeset 1455:4a06e29d1a27
did: fix input handling somewhat, changing input method on linux not really working...
author | sam <sam@basx.dev> |
---|---|
date | Sat, 22 Mar 2025 00:35:13 +0700 |
parents | eaad5e0443f8 |
children | af7754b983d3 |
files | semicongine/input.nim semicongine/text.nim |
diffstat | 2 files changed, 33 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/semicongine/input.nim Fri Mar 21 18:16:24 2025 +0700 +++ b/semicongine/input.nim Sat Mar 22 00:35:13 2025 +0700 @@ -6,7 +6,10 @@ import ./rendering import ./storage -proc updateInputs*(): bool = +proc updateInputs*(readChars: bool = false): bool = + # in order to prevent key-events to generate while the program + # is reading text-input from the keyboard, set `readChars` to true + # reset input states engine().input.keyWasPressed = {} engine().input.keyWasReleased = {} @@ -25,6 +28,9 @@ else: engine().input.mousePosition = newMousePos + proc isControlChar(r: Rune): bool = + (0x00'i32 <= int32(r) and int32(r) <= 0x1F'i32) or int(r) == 0x7f + var killed = false for event in engine().vulkan.window.pendingEvents(): case event.eventType @@ -33,14 +39,16 @@ of ResizedWindow: engine().input.windowWasResized = true of KeyPressed: - engine().input.keyWasPressed.incl event.key - engine().input.keyIsDown.incl event.key # exclude control characters for text input - if not (0x00'i32 <= int32(event.char) and int32(event.char) <= 0x1F'i32): + if readChars and not event.char.isControlChar(): engine().input.characterInput = event.char + else: + engine().input.keyWasPressed.incl event.key + engine().input.keyIsDown.incl event.key of KeyReleased: - engine().input.keyWasReleased.incl event.key - engine().input.keyIsDown.excl event.key + if not readChars or event.char.isControlChar(): + engine().input.keyWasReleased.incl event.key + engine().input.keyIsDown.excl event.key of MousePressed: engine().input.mouseWasPressed.incl event.button engine().input.mouseIsDown.incl event.button
--- a/semicongine/text.nim Fri Mar 21 18:16:24 2025 +0700 +++ b/semicongine/text.nim Sat Mar 22 00:35:13 2025 +0700 @@ -37,9 +37,9 @@ current.add c yield current -proc width*(font: Font, text: seq[Rune]): float32 = +proc width*(font: Font, text: seq[Rune], withTrailingWhiteSpace = false): float32 = for i in 0 ..< text.len: - if not (i == text.len - 1 and text[i].isWhiteSpace): + if i < text.len - 1 or not text[i].isWhiteSpace or withTrailingWhiteSpace: if text[i] in font.advance: result += font.advance[text[i]] else: @@ -48,20 +48,29 @@ result += font.kerning.getOrDefault((text[i], text[i + 1]), 0) return result -proc width*(font: Font, text: string): float32 = - width(font, text.toRunes) +proc width*(font: Font, text: string, withTrailingWhiteSpace = false): float32 = + width(font, text.toRunes, withTrailingWhiteSpace = withTrailingWhiteSpace) -proc textDimension*(font: Font, text: seq[Rune]): Vec2f = +proc textDimension*( + font: Font, text: seq[Rune], withTrailingWhiteSpace = false +): Vec2f = let nLines = text.countIt(it == Rune('\n')).float32 let h = (nLines * font.lineAdvance + font.lineHeight) - let w = max(splitLines(text).toSeq.mapIt(width(font, it))) + let w = max( + splitLines(text).toSeq.mapIt( + width(font, it, withTrailingWhiteSpace = withTrailingWhiteSpace) + ) + ) return vec2(w, h) -proc textDimension*(font: Font, text: string): Vec2f = - textDimension(font, text.toRunes()) +proc textDimension*(font: Font, text: string, withTrailingWhiteSpace = false): Vec2f = + textDimension(font, text.toRunes(), withTrailingWhiteSpace = withTrailingWhiteSpace) -proc textDimension*(textBuffer: TextBuffer, text: string | seq[Rune]): Vec2f = - textDimension(textBuffer.font, text) * textBuffer.baseScale +proc textDimension*( + textBuffer: TextBuffer, text: string | seq[Rune], withTrailingWhiteSpace = false +): Vec2f = + textDimension(textBuffer.font, text, withTrailingWhiteSpace = withTrailingWhiteSpace) * + textBuffer.baseScale proc updateGlyphData*(textbuffer: var TextBuffer, textHandle: TextHandle) = assert textHandle.generation == textbuffer.generation