Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gamepad/keyboard support #1

Open
konsumer opened this issue May 7, 2021 · 0 comments
Open

gamepad/keyboard support #1

konsumer opened this issue May 7, 2021 · 0 comments

Comments

@konsumer
Copy link

konsumer commented May 7, 2021

First, this library looks awesome. Great work!

A common issue I have with love GUI libs is no gamepad support. I'm making a game that only uses gamepad/keys, but things like button could work if I could say "this is the current menu selection" or "the user has navigated to this button using the joypad".

I looked at the code and tried to figure it out, from an external-to-imgui perspective. This example is meant to use up/down keys and space to select:

imgui = require 'imgui'

local uiState = require "imgui.Core.UIState"

-- this tracks the current key choice
local currentItem = 1

-- NOTE: I'm new to imgui so there is probably a better way...
function love.draw()
  uiState.hotItem = currentItem

  imgui.layout.setFlex(true)
  imgui.layout.setColumns(2)

  if imgui.button('A') then
    imgui.label('You are clicking on the A button!!')
  else
    imgui.label('')
  end
  
  if imgui.button('B') then
    imgui.label('You are clicking on the B button!!')
  else
    imgui.label('')
  end
  
  if imgui.button('C') then
    imgui.label('You are clicking on the C button!!')
  else
    imgui.label('')
  end
  imgui.draw()
end

function love.keypressed(key)
  if key == 'space' then
    uiState.activeItem=currentItem
  end
  if key == 'up' then
    currentItem = currentItem - 1
  end
  if key == 'down' then
    currentItem = currentItem + 1
  end
  if currentItem > imgui['_genID'] then
    currentItem = 1
  end
  if currentItem < 1 then
    currentItem = imgui['_genID']
  end
end

function love.keyreleased(key)
  if key == 'space' then
    uiState.activeItem=nil
  end
end

Peek 2021-05-07 15-23

It seems to be tracking the current-selection ok, but I can't quite figure out how to say "this was chosen".

Do you have a suggestion of a better way to do it? Is there interest in making this more internal (I am using _genID which seems hacky, but I bet widgets could even track this on their own.)

Here is a ready-made demo, just rename to demo.love:
demo.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant