zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

input.go (4582B)


      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 //go:build android || ios
     16 // +build android ios
     17 
     18 package mobile
     19 
     20 import (
     21 	"github.com/hajimehoshi/ebiten/v2/internal/driver"
     22 )
     23 
     24 type pos struct {
     25 	X int
     26 	Y int
     27 }
     28 
     29 type Input struct {
     30 	keys     map[driver.Key]struct{}
     31 	runes    []rune
     32 	touches  map[driver.TouchID]pos
     33 	gamepads []Gamepad
     34 	ui       *UserInterface
     35 }
     36 
     37 func (i *Input) CursorPosition() (x, y int) {
     38 	return 0, 0
     39 }
     40 
     41 func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
     42 	i.ui.m.RLock()
     43 	defer i.ui.m.RUnlock()
     44 
     45 	for _, g := range i.gamepads {
     46 		gamepadIDs = append(gamepadIDs, g.ID)
     47 	}
     48 	return gamepadIDs
     49 }
     50 
     51 func (i *Input) GamepadSDLID(id driver.GamepadID) string {
     52 	i.ui.m.RLock()
     53 	defer i.ui.m.RUnlock()
     54 
     55 	for _, g := range i.gamepads {
     56 		if g.ID != id {
     57 			continue
     58 		}
     59 		return g.SDLID
     60 	}
     61 	return ""
     62 }
     63 
     64 func (i *Input) GamepadName(id driver.GamepadID) string {
     65 	i.ui.m.RLock()
     66 	defer i.ui.m.RUnlock()
     67 
     68 	for _, g := range i.gamepads {
     69 		if g.ID != id {
     70 			continue
     71 		}
     72 		return g.Name
     73 	}
     74 	return ""
     75 }
     76 
     77 func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
     78 	i.ui.m.RLock()
     79 	defer i.ui.m.RUnlock()
     80 
     81 	for _, g := range i.gamepads {
     82 		if g.ID != id {
     83 			continue
     84 		}
     85 		return g.AxisNum
     86 	}
     87 	return 0
     88 }
     89 
     90 func (i *Input) GamepadAxisValue(id driver.GamepadID, axis int) float64 {
     91 	i.ui.m.RLock()
     92 	defer i.ui.m.RUnlock()
     93 
     94 	for _, g := range i.gamepads {
     95 		if g.ID != id {
     96 			continue
     97 		}
     98 		if g.AxisNum <= int(axis) {
     99 			return 0
    100 		}
    101 		return float64(g.Axes[axis])
    102 	}
    103 	return 0
    104 }
    105 
    106 func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
    107 	i.ui.m.RLock()
    108 	defer i.ui.m.RUnlock()
    109 
    110 	for _, g := range i.gamepads {
    111 		if g.ID != id {
    112 			continue
    113 		}
    114 		return g.ButtonNum
    115 	}
    116 	return 0
    117 }
    118 
    119 func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
    120 	i.ui.m.RLock()
    121 	defer i.ui.m.RUnlock()
    122 
    123 	for _, g := range i.gamepads {
    124 		if g.ID != id {
    125 			continue
    126 		}
    127 		if g.ButtonNum <= int(button) {
    128 			return false
    129 		}
    130 		return g.Buttons[button]
    131 	}
    132 	return false
    133 }
    134 
    135 func (i *Input) IsStandardGamepadLayoutAvailable(id driver.GamepadID) bool {
    136 	// TODO: Implement this (#1557)
    137 	return false
    138 }
    139 
    140 func (i *Input) IsStandardGamepadButtonPressed(id driver.GamepadID, button driver.StandardGamepadButton) bool {
    141 	// TODO: Implement this (#1557)
    142 	return false
    143 }
    144 
    145 func (i *Input) StandardGamepadButtonValue(id driver.GamepadID, button driver.StandardGamepadButton) float64 {
    146 	// TODO: Implement this (#1557)
    147 	return 0
    148 }
    149 
    150 func (i *Input) StandardGamepadAxisValue(id driver.GamepadID, axis driver.StandardGamepadAxis) float64 {
    151 	// TODO: Implement this (#1557)
    152 	return 0
    153 }
    154 
    155 func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
    156 	i.ui.m.RLock()
    157 	defer i.ui.m.RUnlock()
    158 
    159 	for id := range i.touches {
    160 		touchIDs = append(touchIDs, id)
    161 	}
    162 	return touchIDs
    163 }
    164 
    165 func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
    166 	i.ui.m.RLock()
    167 	defer i.ui.m.RUnlock()
    168 
    169 	for tid, pos := range i.touches {
    170 		if id == tid {
    171 			return i.ui.adjustPosition(pos.X, pos.Y)
    172 		}
    173 	}
    174 	return 0, 0
    175 }
    176 
    177 func (i *Input) AppendInputChars(runes []rune) []rune {
    178 	i.ui.m.Lock()
    179 	defer i.ui.m.Unlock()
    180 	return append(runes, i.runes...)
    181 }
    182 
    183 func (i *Input) IsKeyPressed(key driver.Key) bool {
    184 	i.ui.m.RLock()
    185 	defer i.ui.m.RUnlock()
    186 
    187 	if i.keys == nil {
    188 		return false
    189 	}
    190 	_, ok := i.keys[key]
    191 	return ok
    192 }
    193 
    194 func (i *Input) Wheel() (xoff, yoff float64) {
    195 	return 0, 0
    196 }
    197 
    198 func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool {
    199 	return false
    200 }
    201 
    202 func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []*Touch, gamepads []Gamepad) {
    203 	i.ui.m.Lock()
    204 	defer i.ui.m.Unlock()
    205 
    206 	i.keys = map[driver.Key]struct{}{}
    207 	for k := range keys {
    208 		i.keys[k] = struct{}{}
    209 	}
    210 
    211 	i.runes = make([]rune, len(runes))
    212 	copy(i.runes, runes)
    213 
    214 	i.touches = map[driver.TouchID]pos{}
    215 	for _, t := range touches {
    216 		i.touches[t.ID] = pos{
    217 			X: t.X,
    218 			Y: t.Y,
    219 		}
    220 	}
    221 
    222 	i.gamepads = make([]Gamepad, len(gamepads))
    223 	copy(i.gamepads, gamepads)
    224 }
    225 
    226 func (i *Input) resetForFrame() {
    227 	i.runes = nil
    228 }