This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add joystick support #5
Open
WetDesertRock
wants to merge
5
commits into
rxi:master
Choose a base branch
from
WetDesertRock:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adds basic joystick support. Unfortunately the user has to track the joysticks as the event callbacks give the joystick index. Currently I'm mulling over the best way to do this.
Oh, for a usage example this is the program I use to test it out: function juno.onLoad()
joystick = nil
end
function juno.onUpdate(dt)
if juno.joystick.getCount() >= 1 and joystick == nil then
joystick = juno.joystick.open(0)
print(string.format("Opening joystick with name '%s' reported id is %d",juno.joystick.getName(0),joystick:index()))
print(string.format(" Axes count: %d",joystick:getAxisCount()))
print(string.format(" Hat count: %d",joystick:getHatCount()))
print(string.format(" Ball count: %d",joystick:getBallCount()))
print(string.format(" Button count: %d",joystick:getButtonCount()))
end
end
function juno.onJoyHatMotion(joystick,hat,state)
print("Hat Motion:",joystick,hat)
for d,v in pairs(state) do
print(" ",d,v)
end
end |
Juno now opens all the joysticks upon open. This means that instead of doing juno.joystick.open you can just see all open joysticks with the table juno.joystick.joysticks.
Up for suggestions on a nicer way to normalize it.
Simplified it, you don't need to do any .open calls yourself. The only things you need will be in the juno.joystick.joysticks table array. Here is a project to help test and visualize it. function juno.onLoad()
for _,joystick in ipairs(juno.joystick.joysticks) do
print(string.format("Joystick with name '%s' reported id is %d",juno.joystick.getName(joystick:index()),joystick:index()))
print(string.format(" Axes count: %d",joystick:getAxisCount()))
print(string.format(" Hat count: %d",joystick:getHatCount()))
print(string.format(" Ball count: %d",joystick:getBallCount()))
print(string.format(" Button count: %d",joystick:getButtonCount()))
end
font_title = juno.Font.fromEmbedded(14)
font_small = juno.Font.fromEmbedded(12)
font_smallest = juno.Font.fromEmbedded(8)
end
local hatlines = {
up = {6,0,6,4},
down = {6,8,6,12},
left = {0,6,4,6},
right = {8,6,12,6}
}
function juno.onDraw()
local y = 0
for _,joystick in ipairs(juno.joystick.joysticks) do
juno.graphics.setColor(1,1,1)
juno.graphics.drawText(font_title,"Joystick: "..juno.joystick.getName(joystick:index()),20,y)
y = y+17
if joystick:getAxisCount() > 0 then
local x = 20
juno.graphics.drawText(font_small,"Axes: "..joystick:getAxisCount(), 20,y)
y = y+15
for i=0,joystick:getAxisCount()-1 do
local xline = x+10 + (10*joystick:getAxis(i))
juno.graphics.drawText(font_smallest,i,x,y)
juno.graphics.drawLine(xline,y+8,xline,y+14)
juno.graphics.drawLine(x,y+11,x+20,y+11)
x = x+25
end
y = y+15
end
if joystick:getHatCount() > 0 then
local x = 20
juno.graphics.drawText(font_small,"Hats: "..joystick:getHatCount(), 20,y)
y = y+15
for i=0,joystick:getHatCount()-1 do
juno.graphics.setColor(1,1,1)
juno.graphics.drawText(font_smallest,i,x,y)
for dir,enabled in pairs(joystick:getHat(i)) do
local hl = hatlines[dir]
if enabled then
juno.graphics.setColor(1,1,1)
else
juno.graphics.setColor(0.5,0.5,0.5)
end
juno.graphics.drawLine(x+hl[1],y+hl[2]+5,x+hl[3],y+hl[4]+5)
end
x = x+16
end
y = y+16
end
juno.graphics.setColor(1,1,1)
if joystick:getButtonCount() > 0 then
local x = 20
juno.graphics.drawText(font_small,"Buttons: "..joystick:getButtonCount(), 20,y)
y = y+15
for i=0,joystick:getButtonCount()-1 do
juno.graphics.setColor(0,0,0)
juno.graphics.drawText(font_smallest,i,x+6,y+6)
if joystick:getButton(i) then
juno.graphics.setColor(1,1,1)
else
juno.graphics.setColor(0.5,0.5,0.5)
end
juno.graphics.drawRect(x,y,12,12)
x = x+16
end
y = y+16
end
juno.graphics.setColor(1,1,1)
end
end |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds basic joystick support. Unfortunately the user has to track the
joysticks as the event callbacks give the joystick index. Currently I'm
mulling over the best way to do this.
If you don't want to merge yet tell me why and I'll make fixes. Because you are using SDL1 I don't have access to gamepad mapping APIs from SDL2.