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

Joystick Configuration and Fixes #5477

Merged
merged 7 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions data/lang/input-core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
"description": "Axis binding",
"message": "Map Yaw"
},
"BIND_MAP_VIEW_ZOOM": {
"description": "Axis binding",
"message": "Map Zoom"
},
"BIND_MAP_WARP_TO_CURRENT_SYSTEM": {
"description": "Descriptive name for the MapWarpToCurrentSystem action.",
"message": "Warp to Current System"
Expand Down Expand Up @@ -321,5 +325,57 @@
"Z": {
"description": "keybinding: z axis",
"message": "Z"
},
"GROUP_LANDING_CONTROL": {
"description": "Group label for landing-related inputs",
"message": "Landing Control"
},
"BIND_CONTROL_LANDING_GEAR": {
"description": "Axis description",
"message": "Raise / Lower Landing Gear"
},
"BIND_TOGGLE_LANDING_GEAR": {
"description": "Action description",
"message": "Toggle Landing Gear"
},
"NUM_AXES": {
"description": "",
"message": "Number of axes:"
},
"NUM_BUTTONS": {
"description": "",
"message": "Number of buttons:"
},
"NUM_HATS": {
"description": "",
"message": "Number of hats:"
},
"CURRENT_VALUE": {
"description": "",
"message": "Current value:"
},
"HALF_AXIS_MODE": {
"description": "",
"message": "Half-Axis mode"
},
"DEADZONE": {
"description": "",
"message": "Deadzone"
},
"CURVE": {
"description": "",
"message": "Curve"
},
"RX": {
"description": "keybinding: second x axis",
"message": "RX"
},
"RY": {
"description": "keybinding: second y axis",
"message": "RY"
},
"RZ": {
"description": "keybinding: second z axis",
"message": "RZ"
}
}
13 changes: 9 additions & 4 deletions data/libs/bind-manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ local Input = require 'Input'
local linput = require 'Lang'.GetResource("input-core")
local lc = require 'Lang'.GetResource("core")

local axis_names = { linput.X, linput.Y, linput.Z, linput.RX, linput.RY, linput.RZ }
local mouse_names = { linput.MOUSE_LMB, linput.MOUSE_MMB, linput.MOUSE_RMB }

function bindManager.getAxisName(axis)
return linput.AXIS .. (axis_names[axis + 1] or tostring(axis))
end

--
-- Function: bindManager.getBindingDesc
--
Expand All @@ -25,16 +32,14 @@ local lc = require 'Lang'.GetResource("core")
--
function bindManager.getBindingDesc(bind)
if not bind then return end
local axis_names = { linput.X, linput.Y, linput.Z }
local mouse_names = { linput.MOUSE_LMB, linput.MOUSE_MMB, linput.MOUSE_RMB }
if bind.key then
return Input.GetKeyName(bind.key)
elseif bind.joystick and bind.hat then
return bindManager.joyAcronym(bind.joystick) .. linput.HAT .. bind.hat .. linput.DIRECTION .. bind.dir
elseif bind.joystick and bind.button then
return bindManager.joyAcronym(bind.joystick) .. linput.BUTTON .. bind.button
elseif bind.joystick and bind.axis then
return (bind.direction < 0 and "-" or "") .. bindManager.joyAcronym(bind.joystick) .. linput.AXIS .. (axis_names[bind.axis + 1] or tostring(bind.axis))
return (bind.direction < 0 and "-" or "") .. bindManager.joyAcronym(bind.joystick) .. bindManager.getAxisName(bind.axis)
elseif bind.mouse then
return linput.MOUSE .. (mouse_names[bind.mouse] or tostring(bind.mouse))
end
Expand Down Expand Up @@ -96,7 +101,7 @@ end
--
function bindManager.localizeBindingId(str)
local jsonIndex = str:gsub("([^A-Z0-9_])([A-Z0-9])", "%1_%2"):upper()
return rawget(linput, jsonIndex) or ('[NO_JSON] '..jsonIndex)
return linput[jsonIndex]
end

--
Expand Down
8 changes: 4 additions & 4 deletions data/pigui/libs/forwarded.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ui.listBox = pigui.ListBox
ui.textWrapped = pigui.TextWrapped
ui.textColored = pigui.TextColored
ui.inputText = pigui.InputText
ui.checkbox = pigui.Checkbox
ui.checkbox = pigui.Checkbox ---@type fun(label: string, checked: boolean): changed:boolean, value:boolean
ui.separator = pigui.Separator
ui.pushTextWrapPos = pigui.PushTextWrapPos
ui.popTextWrapPos = pigui.PopTextWrapPos
Expand Down Expand Up @@ -118,13 +118,13 @@ ui.isItemHovered = pigui.IsItemHovered
ui.isItemActive = pigui.IsItemActive
ui.isItemClicked = pigui.IsItemClicked
ui.isWindowHovered = pigui.IsWindowHovered
ui.vSliderInt = pigui.VSliderInt
ui.sliderInt = pigui.SliderInt
ui.vSliderInt = pigui.VSliderInt ---@type fun(l: string, v: integer, min: integer, max: integer, fmt: string?): value:integer, changed:boolean
ui.sliderInt = pigui.SliderInt ---@type fun(l: string, v: integer, min: integer, max: integer, fmt: string?): value:integer, changed:boolean
ui.colorEdit = pigui.ColorEdit
ui.nextItemWidth = pigui.NextItemWidth
ui.pushItemWidth = pigui.PushItemWidth
ui.popItemWidth = pigui.PopItemWidth
ui.sliderFloat = pigui.SliderFloat
ui.sliderFloat = pigui.SliderFloat ---@type fun(l: string, v: number, min: number, max: number, fmt: string?): value:number, changed:boolean
ui.beginTabBar = pigui.BeginTabBar
ui.beginTabItem = pigui.BeginTabItem
ui.endTabItem = pigui.EndTabItem
Expand Down
145 changes: 137 additions & 8 deletions data/pigui/modules/settings-window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,129 @@ local function axisBinding(info)
end
end

local function drawJoystickAxisInfo(joystick, i)
local c, val, enabled

ui.columns(2, "axis info")

ui.text(linput.CURRENT_VALUE)
ui.sameLine()
ui.text(string.format("%0.2f", joystick:GetAxisValue(i)))

ui.nextColumn()

c, enabled = ui.checkbox(linput.HALF_AXIS_MODE, joystick:GetAxisZeroToOne(i))
if c then
joystick:SetAxisZeroToOne(i, enabled)
end

ui.nextColumn()

val, c = ui.sliderFloat(linput.DEADZONE, joystick:GetAxisDeadzone(i), 0.0, 1.0, "%0.2f")
if c then joystick:SetAxisDeadzone(i, val) end

ui.nextColumn()

val, c = ui.sliderFloat(linput.CURVE, joystick:GetAxisCurve(i), 0.0, 2.0, "%0.2f")
if c then joystick:SetAxisCurve(i, val) end

ui.columns(1, "")
end

local selectedJoystick = nil
local function showJoystickInfo(id)
local joystick = Input.GetJoystick(id)

ui.withFont(pionillium.heading, function()
local buttonSize = Vector2(ui.getTextLineHeightWithSpacing())

if ui.iconButton(icons.time_backward_1x, buttonSize, lui.GO_BACK .. "##" .. id) then
Input.SaveJoystickConfig(selectedJoystick)
selectedJoystick = nil
end

ui.sameLine()
ui.alignTextToLineHeight()
ui.text(joystick.name)
end)

ui.spacing()

ui.text(linput.NUM_BUTTONS)
ui.sameLine()
ui.text(joystick.numButtons)

ui.text(linput.NUM_HATS)
ui.sameLine()
ui.text(joystick.numHats)

ui.text(linput.NUM_AXES)
ui.sameLine()
ui.text(joystick.numAxes)

ui.spacing()

for i = 0, joystick.numAxes - 1 do

local width = ui.getContentRegion().x * 0.5
local open = ui.collapsingHeader(bindManager.getAxisName(i))

local isHalfAxis = joystick:GetAxisZeroToOne(i)
local value = joystick:GetAxisValue(i)

-- Draw axis preview indicator
ui.sameLine(width)

local pos = ui.getCursorScreenPos()
pos.y = pos.y + ui.getItemSpacing().y * 0.5

local size = Vector2(width - ui.getItemSpacing().x, ui.getTextLineHeight())
ui.addRectFilled(pos, pos + size, colors.darkGrey, 0, 0)

if isHalfAxis then
size.x = size.x * value
else
pos.x = pos.x + size.x * 0.5
size.x = size.x * 0.5 * value
end

ui.addRectFilled(pos, pos + size, colors.primary, 0, 0)
ui.newLine()

-- Draw axis details
if open then
ui.withID(i, function()
drawJoystickAxisInfo(joystick, i)
end)
end

ui.spacing()

end

end

local function showJoystickList(id)
local connected = Input.IsJoystickConnected(id)
local buttonSize = Vector2(ui.getTextLineHeightWithSpacing())

if connected then
if ui.iconButton(icons.pencil, buttonSize, lui.EDIT .. "##" .. id) then
selectedJoystick = id
end
else
ui.dummy(buttonSize)
end

ui.sameLine()
ui.alignTextToLineHeight()
ui.text(bindManager.joyAcronym(id) .. ":")

local status = connected and linput.CONNECTED or linput.NOT_CONNECTED
ui.sameLine()
ui.textColored(ui.theme.colors.grey, Input.GetJoystickName(id) .. ", " .. status)
end

local function showControlsOptions()
ui.text(lui.CONTROL_OPTIONS)

Expand All @@ -470,14 +593,17 @@ local function showControlsOptions()
if joystick_count > 0 then
ui.separator()
ui.text(linput.JOYSTICKS .. ":")
for id = 0, joystick_count - 1 do
ui.text(" " .. bindManager.joyAcronym(id) .. ":")
ui.sameLine()
ui.withStyleColors({["Text"] = ui.theme.colors.grey}, function()
local status = Input.IsJoystickConnected(id) and linput.CONNECTED or linput.NOT_CONNECTED
ui.text(Input.GetJoystickName(id) .. ", " .. status)
end)
end
ui.spacing()

ui.withFont(pionillium.body, function()
if selectedJoystick then
showJoystickInfo(selectedJoystick)
else
for id = 0, joystick_count - 1 do
showJoystickList(id)
end
end
end)
end

for _,page in ipairs(binding_pages) do
Expand Down Expand Up @@ -558,6 +684,9 @@ ui.optionsWindow = ModalWindow.New("Options", function()
Game.SetTimeAcceleration("1x")
Input.EnableBindings();
end
if selectedJoystick then
Input.SaveJoystickConfig(selectedJoystick)
end
end)

if Game.player then
Expand Down
3 changes: 3 additions & 0 deletions data/pigui/themes/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ theme.colors = {
white = styleColors.white,
lightGrey = styleColors.gray_300,
grey = styleColors.gray_500,
darkGrey = styleColors.gray_800,
black = styleColors.black,
primary = styleColors.primary_500,
darkPrimary = styleColors.primary_700,

alertYellow = styleColors.warning_300,
alertRed = styleColors.danger_500,
Expand Down
Loading