github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/textinput/screen.go (about) 1 package main 2 3 import ( 4 "time" 5 6 "github.com/go-gl/gl/v2.1/gl" 7 "github.com/go-gl/glfw/v3.2/glfw" 8 ) 9 10 type Screen struct { 11 Tick float64 12 Font *Font 13 Text string 14 } 15 16 func NewScreen() *Screen { 17 screen := &Screen{} 18 screen.Font = ArcadeFont.Load() 19 return screen 20 } 21 22 func (screen *Screen) KeyCallback(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { 23 if action == glfw.Release { 24 return 25 } 26 27 if key == glfw.KeyEnter { 28 screen.Text += "\n" 29 return 30 } 31 32 if key == glfw.KeyBackspace { 33 if len(screen.Text) > 0 { 34 screen.Text = screen.Text[:len(screen.Text)-1] 35 } 36 return 37 } 38 39 if glfw.KeyA <= key && key <= glfw.KeyZ { 40 screen.Text += string(key - glfw.KeyA + 'a') 41 return 42 } 43 44 if glfw.Key0 <= key && key <= glfw.Key9 { 45 screen.Text += string(key - glfw.Key0 + '0') 46 return 47 } 48 49 switch key { 50 case glfw.KeySpace: 51 screen.Text += " " 52 case glfw.KeyPeriod: 53 screen.Text += "." 54 case glfw.KeyComma: 55 screen.Text += "," 56 case glfw.KeySemicolon: 57 screen.Text += ";" 58 case glfw.KeySlash: 59 screen.Text += "/" 60 case glfw.KeyEqual: 61 screen.Text += "=" 62 } 63 } 64 65 func (screen *Screen) Update(window *glfw.Window) { 66 now := time.Now() 67 68 width, height := window.GetSize() 69 gl.MatrixMode(gl.MODELVIEW) 70 gl.LoadIdentity() 71 72 gl.Viewport(0, 0, int32(width), int32(height)) 73 gl.Ortho(0, float64(width), float64(height), 0, 30, -30) 74 75 gl.Clear(gl.COLOR_BUFFER_BIT) 76 gl.Enable(gl.BLEND) 77 gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) 78 gl.Enable(gl.ALPHA_TEST) 79 80 text := screen.Text 81 if now.Unix()&1 == 1 { 82 text += "|" 83 } 84 screen.Font.DrawText(Point{1, 1}, 74, text) 85 }