input.go (4005B)
1 // Copyright 2016 Hajime Hoshi 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build android ios 16 17 package mobile 18 19 import ( 20 "github.com/hajimehoshi/ebiten/v2/internal/driver" 21 ) 22 23 type pos struct { 24 X int 25 Y int 26 } 27 28 type Input struct { 29 cursorX int 30 cursorY int 31 keys map[driver.Key]struct{} 32 runes []rune 33 touches map[driver.TouchID]pos 34 gamepads []Gamepad 35 ui *UserInterface 36 } 37 38 func (i *Input) CursorPosition() (x, y int) { 39 i.ui.m.RLock() 40 defer i.ui.m.RUnlock() 41 return i.ui.adjustPosition(i.cursorX, i.cursorY) 42 } 43 44 func (i *Input) GamepadIDs() []driver.GamepadID { 45 i.ui.m.RLock() 46 defer i.ui.m.RUnlock() 47 48 ids := make([]driver.GamepadID, 0, len(i.gamepads)) 49 for _, g := range i.gamepads { 50 ids = append(ids, g.ID) 51 } 52 return ids 53 } 54 55 func (i *Input) GamepadSDLID(id driver.GamepadID) string { 56 i.ui.m.RLock() 57 defer i.ui.m.RUnlock() 58 59 for _, g := range i.gamepads { 60 if g.ID != id { 61 continue 62 } 63 return g.SDLID 64 } 65 return "" 66 } 67 68 func (i *Input) GamepadName(id driver.GamepadID) string { 69 i.ui.m.RLock() 70 defer i.ui.m.RUnlock() 71 72 for _, g := range i.gamepads { 73 if g.ID != id { 74 continue 75 } 76 return g.Name 77 } 78 return "" 79 } 80 81 func (i *Input) GamepadAxisNum(id driver.GamepadID) int { 82 i.ui.m.RLock() 83 defer i.ui.m.RUnlock() 84 85 for _, g := range i.gamepads { 86 if g.ID != id { 87 continue 88 } 89 return g.AxisNum 90 } 91 return 0 92 } 93 94 func (i *Input) GamepadAxis(id driver.GamepadID, axis int) float64 { 95 i.ui.m.RLock() 96 defer i.ui.m.RUnlock() 97 98 for _, g := range i.gamepads { 99 if g.ID != id { 100 continue 101 } 102 if g.AxisNum <= int(axis) { 103 return 0 104 } 105 return float64(g.Axes[axis]) 106 } 107 return 0 108 } 109 110 func (i *Input) GamepadButtonNum(id driver.GamepadID) int { 111 i.ui.m.RLock() 112 defer i.ui.m.RUnlock() 113 114 for _, g := range i.gamepads { 115 if g.ID != id { 116 continue 117 } 118 return g.ButtonNum 119 } 120 return 0 121 } 122 123 func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool { 124 i.ui.m.RLock() 125 defer i.ui.m.RUnlock() 126 127 for _, g := range i.gamepads { 128 if g.ID != id { 129 continue 130 } 131 if g.ButtonNum <= int(button) { 132 return false 133 } 134 return g.Buttons[button] 135 } 136 return false 137 } 138 139 func (i *Input) TouchIDs() []driver.TouchID { 140 i.ui.m.RLock() 141 defer i.ui.m.RUnlock() 142 143 if len(i.touches) == 0 { 144 return nil 145 } 146 147 var ids []driver.TouchID 148 for id := range i.touches { 149 ids = append(ids, id) 150 } 151 return ids 152 } 153 154 func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { 155 i.ui.m.RLock() 156 defer i.ui.m.RUnlock() 157 158 for tid, pos := range i.touches { 159 if id == tid { 160 return i.ui.adjustPosition(pos.X, pos.Y) 161 } 162 } 163 return 0, 0 164 } 165 166 func (i *Input) RuneBuffer() []rune { 167 return i.runes 168 } 169 170 func (i *Input) IsKeyPressed(key driver.Key) bool { 171 i.ui.m.RLock() 172 defer i.ui.m.RUnlock() 173 174 if i.keys == nil { 175 return false 176 } 177 _, ok := i.keys[key] 178 return ok 179 } 180 181 func (i *Input) Wheel() (xoff, yoff float64) { 182 return 0, 0 183 } 184 185 func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool { 186 return false 187 } 188 189 func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []*Touch, gamepads []Gamepad) { 190 i.ui.m.Lock() 191 defer i.ui.m.Unlock() 192 193 i.keys = map[driver.Key]struct{}{} 194 for k := range keys { 195 i.keys[k] = struct{}{} 196 } 197 198 i.runes = make([]rune, len(runes)) 199 copy(i.runes, runes) 200 201 i.touches = map[driver.TouchID]pos{} 202 for _, t := range touches { 203 i.touches[t.ID] = pos{ 204 X: t.X, 205 Y: t.Y, 206 } 207 } 208 209 i.gamepads = make([]Gamepad, len(gamepads)) 210 copy(i.gamepads, gamepads) 211 } 212 213 func (i *Input) resetForFrame() { 214 i.runes = nil 215 }