diff --git a/input.go b/input.go index 9d518be78..d3b7ce4f6 100644 --- a/input.go +++ b/input.go @@ -324,7 +324,7 @@ func UpdateStandardGamepadLayoutMappings(mappings string) (bool, error) { } // TouchID represents a touch's identifier. -type TouchID = driver.TouchID +type TouchID = ui.TouchID // AppendTouchIDs appends the current touch states to touches, and returns the extended buffer. // Giving a slice that already has enough capacity works efficiently. diff --git a/internal/cbackend/cbackend.go b/internal/cbackend/cbackend.go index a12540e78..ab2cafca2 100644 --- a/internal/cbackend/cbackend.go +++ b/internal/cbackend/cbackend.go @@ -66,8 +66,6 @@ import ( "reflect" "time" "unsafe" - - "github.com/hajimehoshi/ebiten/v2/internal/driver" ) type Gamepad struct { @@ -81,7 +79,7 @@ type Gamepad struct { } type Touch struct { - ID driver.TouchID + ID ui.TouchID X int Y int } @@ -153,7 +151,7 @@ func AppendTouches(touches []Touch) []Touch { for _, t := range cTouches { touches = append(touches, Touch{ - ID: driver.TouchID(t.id), + ID: ui.TouchID(t.id), X: int(t.x), Y: int(t.y), }) diff --git a/internal/driver/input.go b/internal/driver/input.go deleted file mode 100644 index b07c25fa3..000000000 --- a/internal/driver/input.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2019 The Ebiten Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package driver - -type TouchID int - -type Input interface { - AppendInputChars(runes []rune) []rune - AppendTouchIDs(touchIDs []TouchID) []TouchID - CursorPosition() (x, y int) - IsKeyPressed(key Key) bool - IsMouseButtonPressed(button MouseButton) bool - TouchPosition(id TouchID) (x, y int) - Wheel() (xoff, yoff float64) -} diff --git a/internal/driver/mousebutton.go b/internal/driver/mousebutton.go deleted file mode 100644 index dd9fbe979..000000000 --- a/internal/driver/mousebutton.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2015 Hajime Hoshi -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package driver - -type MouseButton int - -const ( - MouseButtonLeft MouseButton = iota - MouseButtonRight - MouseButtonMiddle -) diff --git a/internal/ui/input_cbackend.go b/internal/ui/input_cbackend.go index ea91faac9..74a0a5676 100644 --- a/internal/ui/input_cbackend.go +++ b/internal/ui/input_cbackend.go @@ -52,7 +52,7 @@ func (i *Input) AppendInputChars(runes []rune) []rune { return nil } -func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { +func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID { i.m.Lock() defer i.m.Unlock() @@ -70,11 +70,11 @@ func (i *Input) IsKeyPressed(key driver.Key) bool { return false } -func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { +func (i *Input) IsMouseButtonPressed(button MouseButton) bool { return false } -func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { +func (i *Input) TouchPosition(id TouchID) (x, y int) { i.m.Lock() defer i.m.Unlock() diff --git a/internal/ui/input_glfw.go b/internal/ui/input_glfw.go index b4f3134ee..c00626146 100644 --- a/internal/ui/input_glfw.go +++ b/internal/ui/input_glfw.go @@ -35,7 +35,7 @@ type Input struct { scrollY float64 cursorX int cursorY int - touches map[driver.TouchID]pos // TODO: Implement this (#417) + touches map[TouchID]pos // TODO: Implement this (#417) runeBuffer []rune ui *UserInterface } @@ -55,7 +55,7 @@ func (i *Input) CursorPosition() (x, y int) { return i.cursorX, i.cursorY } -func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { +func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID { if !i.ui.isRunning() { return nil } @@ -68,7 +68,7 @@ func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { return touchIDs } -func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { +func (i *Input) TouchPosition(id TouchID) (x, y int) { if !i.ui.isRunning() { return 0, 0 } @@ -118,7 +118,7 @@ func (i *Input) IsKeyPressed(key driver.Key) bool { return ok && i.keyPressed[gk] } -func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { +func (i *Input) IsMouseButtonPressed(button MouseButton) bool { if !i.ui.isRunning() { return false } @@ -149,10 +149,10 @@ func (i *Input) Wheel() (xoff, yoff float64) { return i.scrollX, i.scrollY } -var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{ - glfw.MouseButtonLeft: driver.MouseButtonLeft, - glfw.MouseButtonRight: driver.MouseButtonRight, - glfw.MouseButtonMiddle: driver.MouseButtonMiddle, +var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{ + glfw.MouseButtonLeft: MouseButtonLeft, + glfw.MouseButtonRight: MouseButtonRight, + glfw.MouseButtonMiddle: MouseButtonMiddle, } // update must be called from the main thread. diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go index d88ad25f6..611ffa62e 100644 --- a/internal/ui/input_js.go +++ b/internal/ui/input_js.go @@ -68,7 +68,7 @@ type Input struct { origCursorY int wheelX float64 wheelY float64 - touches map[driver.TouchID]pos + touches map[TouchID]pos runeBuffer []rune ui *UserInterface } @@ -81,14 +81,14 @@ func (i *Input) CursorPosition() (x, y int) { return int(xf), int(yf) } -func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { +func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID { for id := range i.touches { touchIDs = append(touchIDs, id) } return touchIDs } -func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { +func (i *Input) TouchPosition(id TouchID) (x, y int) { d := i.ui.DeviceScaleFactor() for tid, pos := range i.touches { if id == tid { @@ -128,13 +128,13 @@ func (i *Input) IsKeyPressed(key driver.Key) bool { return false } -var codeToMouseButton = map[int]driver.MouseButton{ - 0: driver.MouseButtonLeft, - 1: driver.MouseButtonMiddle, - 2: driver.MouseButtonRight, +var codeToMouseButton = map[int]MouseButton{ + 0: MouseButtonLeft, + 1: MouseButtonMiddle, + 2: MouseButtonRight, } -func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { +func (i *Input) IsMouseButtonPressed(button MouseButton) bool { if i.mouseButtonPressed == nil { i.mouseButtonPressed = map[int]bool{} } @@ -282,9 +282,9 @@ func (in *Input) updateTouchesFromEvent(e js.Value) { } for i := 0; i < j.Length(); i++ { jj := j.Call("item", i) - id := driver.TouchID(jj.Get("identifier").Int()) + id := TouchID(jj.Get("identifier").Int()) if in.touches == nil { - in.touches = map[driver.TouchID]pos{} + in.touches = map[TouchID]pos{} } in.touches[id] = pos{ X: jj.Get("clientX").Int(), @@ -307,9 +307,9 @@ func (i *Input) updateForGo2Cpp() { x := go2cpp.Call("getTouchX", idx) y := go2cpp.Call("getTouchY", idx) if i.touches == nil { - i.touches = map[driver.TouchID]pos{} + i.touches = map[TouchID]pos{} } - i.touches[driver.TouchID(id.Int())] = pos{ + i.touches[TouchID(id.Int())] = pos{ X: x.Int(), Y: y.Int(), } diff --git a/internal/ui/input_mobile.go b/internal/ui/input_mobile.go index 19ac75112..864167814 100644 --- a/internal/ui/input_mobile.go +++ b/internal/ui/input_mobile.go @@ -33,7 +33,7 @@ func (i *Input) CursorPosition() (x, y int) { return 0, 0 } -func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { +func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID { i.ui.m.RLock() defer i.ui.m.RUnlock() @@ -43,7 +43,7 @@ func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { return touchIDs } -func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { +func (i *Input) TouchPosition(id TouchID) (x, y int) { i.ui.m.RLock() defer i.ui.m.RUnlock() @@ -73,7 +73,7 @@ func (i *Input) Wheel() (xoff, yoff float64) { return 0, 0 } -func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool { +func (i *Input) IsMouseButtonPressed(key MouseButton) bool { return false } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index d15b6396e..8ba21ca0a 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -27,6 +27,16 @@ type Context interface { AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) } +type MouseButton int + +const ( + MouseButtonLeft MouseButton = iota + MouseButtonRight + MouseButtonMiddle +) + +type TouchID int + // RegularTermination represents a regular termination. // Run can return this error, and if this error is received, // the game loop should be terminated as soon as possible. diff --git a/internal/ui/ui_cbackend.go b/internal/ui/ui_cbackend.go index ec7b39341..0e938b3ec 100644 --- a/internal/ui/ui_cbackend.go +++ b/internal/ui/ui_cbackend.go @@ -128,7 +128,7 @@ func (*UserInterface) SetInitFocused(focused bool) { func (*UserInterface) Vibrate(duration time.Duration, magnitude float64) { } -func (*UserInterface) Input() driver.Input { +func (*UserInterface) Input() *Input { return &theUserInterface.input } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 95c0b43c4..4d44d0762 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -1431,7 +1431,7 @@ func (u *UserInterface) SetInitFocused(focused bool) { u.setInitFocused(focused) } -func (u *UserInterface) Input() driver.Input { +func (u *UserInterface) Input() *Input { return &u.input } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index cd3426aee..3098904ae 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -661,7 +661,7 @@ func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { } } -func (u *UserInterface) Input() driver.Input { +func (u *UserInterface) Input() *Input { return &u.input } diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 9a536113f..2baccf876 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -187,7 +187,7 @@ func (u *UserInterface) appMain(a app.App) { x, y := float64(e.X)/s, float64(e.Y)/s // TODO: Is it ok to cast from int64 to int here? touches[e.Sequence] = Touch{ - ID: driver.TouchID(e.Sequence), + ID: TouchID(e.Sequence), X: int(x), Y: int(y), } @@ -446,7 +446,7 @@ func (u *UserInterface) SetInitFocused(focused bool) { // Do nothing } -func (u *UserInterface) Input() driver.Input { +func (u *UserInterface) Input() *Input { return &u.input } @@ -455,7 +455,7 @@ func (u *UserInterface) Window() driver.Window { } type Touch struct { - ID driver.TouchID + ID TouchID X int Y int } diff --git a/mobile/ebitenmobileview/input.go b/mobile/ebitenmobileview/input.go index 50f628f52..0e333a353 100644 --- a/mobile/ebitenmobileview/input.go +++ b/mobile/ebitenmobileview/input.go @@ -30,7 +30,7 @@ type position struct { var ( keys = map[driver.Key]struct{}{} runes []rune - touches = map[driver.TouchID]position{} + touches = map[ui.TouchID]position{} ) var ( diff --git a/mobile/ebitenmobileview/input_android.go b/mobile/ebitenmobileview/input_android.go index f67a1fd65..238c596c8 100644 --- a/mobile/ebitenmobileview/input_android.go +++ b/mobile/ebitenmobileview/input_android.go @@ -20,8 +20,8 @@ import ( "math" "unicode" - "github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/gamepad" + "github.com/hajimehoshi/ebiten/v2/internal/ui" ) // https://developer.android.com/reference/android/view/KeyEvent @@ -183,10 +183,10 @@ var androidAxisIDToHatID2 = map[int]int{ func UpdateTouchesOnAndroid(action int, id int, x, y int) { switch action { case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE - touches[driver.TouchID(id)] = position{x, y} + touches[ui.TouchID(id)] = position{x, y} updateInput() case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP - delete(touches, driver.TouchID(id)) + delete(touches, ui.TouchID(id)) updateInput() } } diff --git a/mobile/ebitenmobileview/input_ios.go b/mobile/ebitenmobileview/input_ios.go index ed215b0f6..a59562b10 100644 --- a/mobile/ebitenmobileview/input_ios.go +++ b/mobile/ebitenmobileview/input_ios.go @@ -20,7 +20,7 @@ package ebitenmobileview import ( "fmt" - "github.com/hajimehoshi/ebiten/v2/internal/driver" + "github.com/hajimehoshi/ebiten/v2/internal/ui" ) // #cgo CFLAGS: -x objective-c @@ -50,12 +50,12 @@ func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) { switch phase { case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary: id := getIDFromPtr(ptr) - touches[driver.TouchID(id)] = position{x, y} + touches[ui.TouchID(id)] = position{x, y} updateInput() case C.UITouchPhaseEnded, C.UITouchPhaseCancelled: id := getIDFromPtr(ptr) delete(ptrToID, ptr) - delete(touches, driver.TouchID(id)) + delete(touches, ui.TouchID(id)) updateInput() default: panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase)) diff --git a/mousebuttons.go b/mousebuttons.go index ec18d2531..102d6c9ff 100644 --- a/mousebuttons.go +++ b/mousebuttons.go @@ -15,15 +15,15 @@ package ebiten import ( - "github.com/hajimehoshi/ebiten/v2/internal/driver" + "github.com/hajimehoshi/ebiten/v2/internal/ui" ) // A MouseButton represents a mouse button. -type MouseButton = driver.MouseButton +type MouseButton = ui.MouseButton // MouseButtons const ( - MouseButtonLeft MouseButton = driver.MouseButtonLeft - MouseButtonRight MouseButton = driver.MouseButtonRight - MouseButtonMiddle MouseButton = driver.MouseButtonMiddle + MouseButtonLeft MouseButton = ui.MouseButtonLeft + MouseButtonRight MouseButton = ui.MouseButtonRight + MouseButtonMiddle MouseButton = ui.MouseButtonMiddle )