Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Add joystick support #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

WetDesertRock
Copy link

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.

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.
@WetDesertRock
Copy link
Author

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.
@WetDesertRock
Copy link
Author

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.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant