this repo has no description

add touchpad and buttons to gamepad demo #13

merged opened by voigt.tngl.sh targeting main from add-gamepad
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:6q572hlx7omtsszji5w2fyw3/sh.tangled.repo.pull/3mc7q24z5km22
+168
Diff #0
gamepad/eg_6x10.fff

This is a binary file and will not be displayed.

+7
gamepad/firefly.toml
··· 1 + author_id = "voigt" 2 + app_id = "go-gamepad" 3 + author_name = "Christoph Voigt" 4 + app_name = "Gamepad Demo (Go)" 5 + 6 + [files] 7 + font = { path = "eg_6x10.fff", url = "https://fonts.fireflyzero.com/fonts/ascii/eg_6x10.fff" }
+161
gamepad/main.go
··· 1 + package main 2 + 3 + import ( 4 + ff "github.com/firefly-zero/firefly-go/firefly" 5 + ) 6 + 7 + var ( 8 + font ff.Font 9 + 10 + point *ff.Point 11 + center ff.Point 12 + 13 + buttonN bool 14 + buttonE bool 15 + buttonS bool 16 + buttonW bool 17 + ) 18 + 19 + const ( 20 + radius = 24 21 + third = ff.Width / 3 22 + 23 + touchYcenter = third 24 + touchXcenter = ff.Height/2 - radius/2 25 + buttonYcenter = third * 2 26 + buttonXcenter = ff.Height/2 - radius/2 27 + ) 28 + 29 + func init() { 30 + ff.Boot = boot 31 + ff.Render = render 32 + ff.Update = update 33 + ff.Render = render 34 + } 35 + 36 + func boot() { 37 + font = ff.LoadFile("font", nil).Font() 38 + } 39 + 40 + func update() { 41 + // Buttons 42 + buttons := ff.ReadButtons(ff.Combined) 43 + 44 + if buttons.N { 45 + buttonN = true 46 + } else { 47 + buttonN = false 48 + } 49 + if buttons.E { 50 + buttonE = true 51 + } else { 52 + buttonE = false 53 + } 54 + if buttons.S { 55 + buttonS = true 56 + } else { 57 + buttonS = false 58 + } 59 + if buttons.W { 60 + buttonW = true 61 + } else { 62 + buttonW = false 63 + } 64 + 65 + // Touchpad 66 + center = ff.Point{X: touchXcenter, Y: touchYcenter} 67 + 68 + input, pressed := ff.ReadPad(ff.Combined) 69 + if pressed { 70 + point = &ff.Point{ 71 + X: center.X + input.X/30 - radius/2 + 7, 72 + Y: center.Y - input.Y/30 - radius/2 + 7, 73 + } 74 + } else { 75 + point = nil 76 + } 77 + } 78 + 79 + func circleCenter(p ff.Point) ff.Point { 80 + return ff.Point{X: p.X + (radius / 2.4), Y: p.Y + (radius / 1.6)} 81 + } 82 + 83 + func render() { 84 + ff.ClearScreen(ff.ColorWhite) 85 + 86 + paintButtons() 87 + paintTouchpad() 88 + 89 + // drawHelpers() 90 + } 91 + 92 + func paintButtons() { 93 + // Buttons 94 + // B 95 + styleE := ff.Style{FillColor: ff.ColorWhite, StrokeColor: ff.ColorDarkBlue, StrokeWidth: 1} 96 + if buttonE { 97 + styleE.FillColor = ff.ColorRed 98 + } 99 + bButton := ff.Point{X: buttonYcenter + radius, Y: buttonXcenter} 100 + ff.DrawCircle(bButton, radius, styleE) 101 + ff.DrawText( 102 + "B", 103 + font, (circleCenter(bButton)), ff.ColorLightGray, 104 + ) 105 + 106 + // A 107 + styleS := ff.Style{FillColor: ff.ColorWhite, StrokeColor: ff.ColorDarkBlue, StrokeWidth: 1} 108 + if buttonS { 109 + styleS.FillColor = ff.ColorGreen 110 + } 111 + aButton := ff.Point{X: buttonYcenter, Y: buttonXcenter + radius} 112 + ff.DrawCircle(aButton, radius, styleS) 113 + ff.DrawText( 114 + "A", 115 + font, (circleCenter(aButton)), ff.ColorLightGray, 116 + ) 117 + 118 + // Y 119 + styleN := ff.Style{FillColor: ff.ColorWhite, StrokeColor: ff.ColorDarkBlue, StrokeWidth: 1} 120 + if buttonN { 121 + styleN.FillColor = ff.ColorYellow 122 + } 123 + yButton := ff.Point{X: buttonYcenter, Y: buttonXcenter - radius} 124 + ff.DrawCircle(yButton, radius, styleN) 125 + ff.DrawText( 126 + "Y", 127 + font, (circleCenter(yButton)), ff.ColorLightGray, 128 + ) 129 + 130 + // X 131 + styleW := ff.Style{FillColor: ff.ColorWhite, StrokeColor: ff.ColorDarkBlue, StrokeWidth: 1} 132 + if buttonW { 133 + styleW.FillColor = ff.ColorBlue 134 + } 135 + xButton := ff.Point{X: buttonYcenter - radius, Y: buttonXcenter} 136 + ff.DrawCircle(xButton, radius, styleW) 137 + ff.DrawText( 138 + "X", 139 + font, (circleCenter(xButton)), ff.ColorLightGray, 140 + ) 141 + } 142 + 143 + func paintTouchpad() { 144 + style := ff.Style{FillColor: ff.ColorWhite, StrokeColor: ff.ColorDarkBlue, StrokeWidth: 1} 145 + ff.DrawCircle(ff.Point{X: touchYcenter - radius*2, Y: touchXcenter - radius}, radius*3, style) 146 + 147 + if point != nil { 148 + style = ff.Style{FillColor: ff.ColorBlue, StrokeColor: ff.ColorDarkBlue, StrokeWidth: 1} 149 + ff.DrawCircle(*point, 10, style) 150 + // ff.DrawLine(ff.Point{X: touchXcenter, Y: touchYcenter}, *point, ff.LineStyle{Color: ff.ColorDarkBlue, Width: 1}) 151 + } 152 + } 153 + 154 + // Helper Lines 155 + func drawHelpers() { 156 + ff.DrawLine(ff.Point{buttonYcenter, 0}, ff.Point{buttonYcenter, ff.Height}, ff.LineStyle{ff.ColorGray, 1}) 157 + ff.DrawLine(ff.Point{touchYcenter, 0}, ff.Point{touchYcenter, ff.Height}, ff.LineStyle{ff.ColorGray, 1}) 158 + ff.DrawLine(ff.Point{0, ff.Height / 2}, ff.Point{ff.Width, ff.Height / 2}, ff.LineStyle{ff.ColorGray, 1}) 159 + ff.DrawLine(ff.Point{touchYcenter - radius/2, 0}, ff.Point{touchYcenter - radius/2, ff.Height}, ff.LineStyle{ff.ColorGray, 1}) 160 + ff.DrawLine(ff.Point{buttonYcenter + radius, 0}, ff.Point{buttonYcenter + radius, ff.Height}, ff.LineStyle{ff.ColorGray, 1}) 161 + }

History

2 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
add touchpad and buttons to gamepad demo
expand 0 comments
pull request successfully merged
voigt.tngl.sh submitted #0
1 commit
expand
add touchpad and buttons to gamepad demo
expand 0 comments