mirror of
https://github.com/ivanizag/izapple2.git
synced 2024-12-21 18:29:45 +00:00
Use the mouse as joystick if none available
This commit is contained in:
parent
fe72071245
commit
8f2b12d6ef
@ -44,7 +44,7 @@ func SDLRun(a *apple2.Apple2) {
|
|||||||
s.start()
|
s.start()
|
||||||
a.SetSpeakerProvider(s)
|
a.SetSpeakerProvider(s)
|
||||||
|
|
||||||
j := newSDLJoysticks()
|
j := newSDLJoysticks(true)
|
||||||
a.SetJoysticksProvider(j)
|
a.SetJoysticksProvider(j)
|
||||||
|
|
||||||
go a.Run()
|
go a.Run()
|
||||||
@ -66,6 +66,11 @@ func SDLRun(a *apple2.Apple2) {
|
|||||||
j.putAxisEvent(t)
|
j.putAxisEvent(t)
|
||||||
case *sdl.JoyButtonEvent:
|
case *sdl.JoyButtonEvent:
|
||||||
j.putButtonEvent(t)
|
j.putButtonEvent(t)
|
||||||
|
case *sdl.MouseMotionEvent:
|
||||||
|
w, h := window.GetSize()
|
||||||
|
j.putMouseMotionEvent(t, w, h)
|
||||||
|
case *sdl.MouseButtonEvent:
|
||||||
|
j.putMouseButtonEvent(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,13 +15,15 @@ joystick 1.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
type sdlJoysticks struct {
|
type sdlJoysticks struct {
|
||||||
paddle [4]uint8
|
paddle [4]uint8
|
||||||
hasPaddle [4]bool
|
hasPaddle [4]bool
|
||||||
button [4]bool
|
button [4]bool
|
||||||
keys [3]bool
|
keys [3]bool
|
||||||
|
mousebuttons [3]bool
|
||||||
|
useMouse bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSDLJoysticks() *sdlJoysticks {
|
func newSDLJoysticks(useMouseAlt bool) *sdlJoysticks {
|
||||||
var j sdlJoysticks
|
var j sdlJoysticks
|
||||||
|
|
||||||
err := sdl.InitSubSystem(sdl.INIT_JOYSTICK)
|
err := sdl.InitSubSystem(sdl.INIT_JOYSTICK)
|
||||||
@ -44,6 +46,15 @@ func newSDLJoysticks() *sdlJoysticks {
|
|||||||
j.paddle[2] = 255
|
j.paddle[2] = 255
|
||||||
j.paddle[3] = 255
|
j.paddle[3] = 255
|
||||||
|
|
||||||
|
if useMouseAlt && !j.hasPaddle[0] {
|
||||||
|
// Use the mouse as joystick
|
||||||
|
j.useMouse = true
|
||||||
|
j.hasPaddle[0] = true
|
||||||
|
j.hasPaddle[1] = true
|
||||||
|
j.paddle[0] = 127
|
||||||
|
j.paddle[1] = 127
|
||||||
|
}
|
||||||
|
|
||||||
// To enter Apple IIe on self test mode
|
// To enter Apple IIe on self test mode
|
||||||
//j.keys[1] = true
|
//j.keys[1] = true
|
||||||
|
|
||||||
@ -68,6 +79,46 @@ func (j *sdlJoysticks) putButtonEvent(e *sdl.JoyButtonEvent) {
|
|||||||
j.button[uint8(e.Which)*2+e.Button] = (e.State != 0)
|
j.button[uint8(e.Which)*2+e.Button] = (e.State != 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mouseToJoyFull(x int32, w int32) uint8 {
|
||||||
|
return uint8(256 * x / (w + 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func mouseToJoyCentered(x int32, w int32) uint8 {
|
||||||
|
r := x - (w / 2) + 127
|
||||||
|
if r >= 255 {
|
||||||
|
r = 255
|
||||||
|
}
|
||||||
|
if r < 0 {
|
||||||
|
r = 0
|
||||||
|
}
|
||||||
|
return uint8(r)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *sdlJoysticks) putMouseMotionEvent(e *sdl.MouseMotionEvent, width int32, height int32) {
|
||||||
|
if j.useMouse {
|
||||||
|
// The mouse moves on all the window
|
||||||
|
//j.paddle[0] = mouseToJoyFull(e.X, width)
|
||||||
|
//j.paddle[1] = mouseToJoyFull(e.Y, height)
|
||||||
|
|
||||||
|
// The mouse moves around the center of the window
|
||||||
|
j.paddle[0] = mouseToJoyCentered(e.X, width)
|
||||||
|
j.paddle[1] = mouseToJoyCentered(e.Y, height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *sdlJoysticks) putMouseButtonEvent(e *sdl.MouseButtonEvent) {
|
||||||
|
pressed := e.State == sdl.PRESSED
|
||||||
|
switch e.Button {
|
||||||
|
case 1: //BUTTON_LEFT
|
||||||
|
j.mousebuttons[0] = pressed
|
||||||
|
case 3: //BUTTON_RIGHT
|
||||||
|
j.mousebuttons[1] = pressed
|
||||||
|
case 2: //BUTTON_MIDDLE
|
||||||
|
j.mousebuttons[2] = pressed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (j *sdlJoysticks) putKey(keyEvent *sdl.KeyboardEvent) {
|
func (j *sdlJoysticks) putKey(keyEvent *sdl.KeyboardEvent) {
|
||||||
/*
|
/*
|
||||||
We will simultate joystick buttons with keyboard keys.
|
We will simultate joystick buttons with keyboard keys.
|
||||||
@ -92,12 +143,12 @@ func (j *sdlJoysticks) ReadButton(i int) bool {
|
|||||||
var value bool
|
var value bool
|
||||||
switch i {
|
switch i {
|
||||||
case 0:
|
case 0:
|
||||||
value = j.button[0] || j.keys[0]
|
value = j.button[0] || j.keys[0] || j.mousebuttons[0]
|
||||||
case 1:
|
case 1:
|
||||||
// It can be secondary of first or primary of second
|
// It can be secondary of first or primary of second
|
||||||
value = j.button[1] || j.button[2] || j.keys[1]
|
value = j.button[1] || j.button[2] || j.keys[1] || j.mousebuttons[1]
|
||||||
case 2:
|
case 2:
|
||||||
value = j.button[3] || j.keys[2]
|
value = j.button[3] || j.keys[2] || j.mousebuttons[2]
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user