Skip to content

Instantly share code, notes, and snippets.

@Opposite34
Last active May 3, 2024 11:18
Show Gist options
  • Select an option

  • Save Opposite34/f22a7542afdfae190341b55f32649753 to your computer and use it in GitHub Desktop.

Select an option

Save Opposite34/f22a7542afdfae190341b55f32649753 to your computer and use it in GitHub Desktop.
A small text-input-like typing coded in nim. Only alphanumeric, space, comma, and period are supported
import boxy, opengl, windy
let windowSize = ivec2(1280, 720)
let window = newWindow("test", windowSize)
makeContextCurrent(window)
loadExtensions()
let bxy = newBoxy()
let typeface = readTypeface("RobotoMono-VariableFont_wght.ttf") # please use monospaced fonts
proc drawText(
bxy: Boxy,
imageKey: string,
transform: Mat3,
typeface: Typeface,
text: string,
size: float32,
color: Color
) =
var font = newFont(typeface)
font.size = size
font.paint = color
let
arrangement = typeset(@[newSpan(text, font)], bounds = window.size.vec2)
globalBounds = arrangement.computeBounds(transform).snapToPixels()
textImage = newImage(globalBounds.w.int, globalBounds.h.int)
imageSpace = translate(-globalBounds.xy) * transform
textImage.fillText(arrangement, imageSpace)
bxy.addImage(imageKey, textImage)
bxy.drawImage(imageKey, globalBounds.xy)
var frame: int
# for our text, which dynamically changed when typed (maxed out at charlimit)
type TextProperties = object
text: string
unmodifiedColor: Color
modifiedColor: Color
size: float
charlimit: int
wasModified = false
proc getColor(self: TextProperties): Color =
if self.wasModified:
self.modifiedColor
else:
self.unmodifiedColor
proc clear(self: var TextProperties) =
self.text = " "
proc addChar(self: var TextProperties, chara: char) =
if not self.wasModified or self.text == " ":
self.text = ""
self.wasModified = true
if self.text.len < self.charlimit:
self.text.add(chara)
proc delChar(self: var TextProperties) =
if self.text.len == 1 or not self.wasModified:
self.text = " "
self.wasModified = true
else:
self.text = self.text[0 .. ^2]
const TextSize = 50
var textProperties = TextProperties(
text: "Type Something",
unmodifiedColor: color(0.8, 0.8, 0.8, 1),
modifiedColor: color(1, 1, 1, 1),
size: TextSize,
charlimit: int(2000/TextSize),
wasModified: false)
# calls every frame to draw things
window.onFrame = proc() =
# clear screen and begin a new frame
bxy.beginFrame(windowSize)
if textProperties.text != " ":
bxy.drawText(
"main-image",
#this text-position calculation is probably different every font
translate(vec2(window.size.x/2 - (float(TextSize * textProperties.text.len) / 3.3), window.size.y/2 - (TextSize) + 50)),
typeface,
textProperties.text,
textProperties.size,
textProperties.getColor
)
# end the frame
bxy.endFrame()
# swap buffers to the new boxy frame
window.swapBuffers()
inc frame
var shiftIsPressed = false
var leftCtrlIsPressed = false
var charCode: int
window.onButtonRelease = proc(button: Button) =
if button == KeyLeftShift or button == KeyRightShift:
shiftIsPressed = false
if button == KeyLeftControl:
leftCtrlIsPressed = false
window.onButtonPress = proc(button: Button) =
case button:
of Key0 .. Key9:
charCode = int(button) + 39
textProperties.addChar(chr(charCode))
of KeyA .. KeyZ:
charCode = int(button) + 78
if shiftIsPressed:
charCode -= 32
textProperties.addChar(chr(charCode))
of KeySpace:
textProperties.addChar(chr(32))
of KeyComma:
textProperties.addChar(chr(44))
of KeyPeriod:
textProperties.addChar(chr(46))
of KeyBackspace:
if leftCtrlIsPressed:
textProperties.clear()
else:
textProperties.delChar()
of KeyLeftShift, KeyRightShift:
shiftIsPressed = true
of KeyLeftControl:
leftCtrlIsPressed = true
else:
discard
while not window.closeRequested:
pollEvents()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment