view tests/test_font.nim @ 877:773af36148bd

did: changes
author Sam <sam@basx.dev>
date Sat, 27 Jan 2024 21:08:31 +0700
parents 164b41a2d5a6
children b032768df631
line wrap: on
line source

import std/unicode

import semicongine

proc main() =
  # setup engine
  var engine = initEngine("Test fonts")
  engine.initRenderer([])

  # build scene
  var scene = Scene(name: "main")
  # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes)
  var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 250'f32)
  var textbox = initText(32, font, "_", color = newVec4f(1, 0, 0, 1))
  let fontscale = 0.001
  scene.add textbox
  textbox.mesh.transform = scale(fontscale, fontscale)
  engine.loadScene(scene)

  let cursor = Rune('_')
  while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
    if engine.windowWasResized():
      var winSize = engine.getWindow().size
      textbox.mesh.transform = scale(fontscale * (winSize[1] / winSize[0]), fontscale)
    for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I,
        Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S,
        Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
      if engine.keyWasPressed(c):
        if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
          textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes & cursor
        else:
          textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes[0].toLower() & cursor
    if engine.keyWasPressed(Enter):
      textbox.text = textbox.text[0 ..< ^1] & Rune('\n') & cursor
    if engine.keyWasPressed(Space):
      textbox.text = textbox.text[0 ..< ^1] & Rune(' ') & cursor
    if engine.keyWasPressed(Backspace) and textbox.text.len > 1:
      textbox.text = textbox.text[0 ..< ^2] & cursor
    engine.renderScene(scene)
  engine.destroy()


when isMainModule:
  main()