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

Local Input Config for Keyboard #1067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 37 additions & 6 deletions Scripts/Python/xIniInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
"""

import xIniHelper
from PlasmaConstants import *
from Plasma import *


gFilename = "./init/input.fni"
gIniFile = None
gFilename = "input.ini"
gFilenameAndPath = ""

# the different volume commands
kBindCmd = "Keyboard.BindAction"
Expand All @@ -60,19 +62,48 @@
kTurnLeft = '"Turn Left"'
kTurnRight = '"Turn Right"'
kJump = '"Jump Key"'
kStrafeLeft = '"Strafe Left"'
kStrafeRight = '"Strafe Right"'
kExit = '"Exit Mode"'
kExiGUI = '"Exit GUI Mode"'
kToggleFP = '"Toggle First Person"'
kOpenYB = '"Game.KIOpenYeeshaBook"'
kHelp = '"Game.KIHelp"'
kOpenKi = '"Game.KIOpenKI"'
kTakePicture = '"Game.KITakePicture"'
kKICreateJournal = '"Game.KICreateJournal"'
kTalk = '"Push to talk"'
kChat = '"Game.EnterChatMode"'
kCreateMarkerFolder = '"Game.KICreateMarkerFolder"'
kCreateMarker = '"Game.KICreateMarker"'

kIniArray = [ kWalkForward, kWalkBack, kTurnLeft, kTurnRight, kJump, kStrafeLeft, kStrafeRight, kExit, kToggleFP, kOpenYB, kHelp, kOpenKi, kTakePicture, kKICreateJournal, kTalk, kChat, kCreateMarkerFolder, kCreateMarker ]



def ConstructFilenameAndPath():
global gFilenameAndPath
if gFilenameAndPath == "":
if PtIsInternalRelease():
# check for local file
localNameAndPath = "init/" + gFilename
if PtFileExists(localNameAndPath):
gFilenameAndPath = localNameAndPath
PtDebugPrint("xIniInput::ConstructFilenameAndPath(): Using internal \"" + gFilenameAndPath + "\" file")
return
# otherwise, use the standard init path
gFilenameAndPath = PtGetInitPath() + "/" + gFilename
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably return the path instead of stashing it as a global variable. That way, you can get rid of the gFilenameAndPath thing and just do gIniFile.writeFile(ConstructFilenameAndPath()).

PtDebugPrint("xIniInput::ConstructFilenameAndPath(): Using user-level \"" + gFilenameAndPath + "\" file")

def WriteIni():
global gIniFile
if gIniFile:
gIniFile.writeFile(gFilename)
ConstructFilenameAndPath()
gIniFile.writeFile(gFilenameAndPath)

def ReadIni():
global gIniFile
gIniFile = xIniHelper.iniFile(gFilename)
ConstructFilenameAndPath()
gIniFile = xIniHelper.iniFile(gFilenameAndPath)
if gIniFile.isEmpty():
# add defaults
gIniFile.addEntry("# This is an auto-generated file.")
Expand All @@ -93,7 +124,7 @@ def GetControlKey(controlKey):
if entry and entry.getValue(0) and entry.getValue(1):
return entry.getValue(0),entry.getValue(1)
else:
return "(unmapped),","(unmapped),"
return None

def SetConsoleKey(consoleCommand,primary="(unmapped),"):
if gIniFile:
Expand Down
50 changes: 37 additions & 13 deletions Scripts/Python/xOptionsMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@

from xGUILinkHandler import xGUILinkHandler
import xJournalBookDefs
import xIniInput
import xIniAudio
import xIniDisplay
import xIniNumSounds
import re

# define the attributes that will be entered in max
OptionsMenuDlg = ptAttribGUIDialog(1,"The Options Menu dialog")
Expand Down Expand Up @@ -502,12 +504,13 @@ def BeginAgeUnLoad(self,avatar):
gJournalBook.hide()

def OnServerInitComplete(self):
xIniInput.ReadIni()
if self.refreshBindings:
self.refreshBindings = False

testInputINI = xIniInput.GetControlKey(xIniInput.kWalkForward)
vault = ptVault()
entry = vault.findChronicleEntry("KeyMap")
if entry is None:
if entry is None and testInputINI is None:
# not found... create defaults
self.ISetDefaultKeyMappings()

Expand Down Expand Up @@ -723,10 +726,9 @@ def OnGUINotify(self,id,control,event):
textField.setStringW(PtGetLocalizedString("OptionsMenu.Main.GoBack"))
self.IShowMappedKeys(control,gKM1ControlCodesRow1,gKM1ControlCodesRow2)
# read the ini file in
# xIniInput.ReadIni()
xIniInput.ReadIni()
else:
# xIniInput.WriteIni()
pass
xIniInput.WriteIni()
elif event == kAction or event == kValueChanged:
kmID = control.getTagID()
if kmID == kKMOkBtn:
Expand All @@ -745,6 +747,7 @@ def OnGUINotify(self,id,control,event):
for key in KeyMapArray:
NewKeyMapString += key + " "
self.setNewChronicleVar("KeyMap", NewKeyMapString.rstrip())
xIniInput.SetConsoleKey('"' + cCode + '"', key1 + ',')
elif cCode is not None:
otherID = kmID + 100
otherField = ptGUIControlEditBox(KeyMapDlg.dialog.getControlFromTag(otherID))
Expand All @@ -758,14 +761,15 @@ def OnGUINotify(self,id,control,event):
for key in KeyMapArray:
NewKeyMapString += key + " "
self.setNewChronicleVar("KeyMap", NewKeyMapString.rstrip())
xIniInput.SetControlKey('"' + controlStr + '"', key1 + ',', key2 + ',')
# lose the focus when done
KeyMapDlg.dialog.noFocus()
# force writing the keymap
km.writeKeyMap()
# re-show the keymap because they may have been stupid and map the same key to multiple actions
self.IShowMappedKeys(KeyMapDlg.dialog,gKM1ControlCodesRow1,gKM1ControlCodesRow2)
# need to re-set the ini file, in case something got unmapped
#self.IMatchIniToGame()
self.IMatchIniToGame()
elif kmID in gKM1ControlCodesRow2.keys():
NewKeyMapString = ""
# get the new keys and bind
Expand All @@ -782,7 +786,7 @@ def OnGUINotify(self,id,control,event):
for key in KeyMapArray:
NewKeyMapString += key + " "
self.setNewChronicleVar("KeyMap", NewKeyMapString.rstrip())
#xIniInput.SetConsoleKey('"'+cCode+'"',key1+',')
xIniInput.SetConsoleKey('"'+cCode+'"',key1+',')
elif cCode is not None:
otherID = kmID - 100
otherField = ptGUIControlEditBox(KeyMapDlg.dialog.getControlFromTag(otherID))
Expand All @@ -796,15 +800,15 @@ def OnGUINotify(self,id,control,event):
for key in KeyMapArray:
NewKeyMapString += key + " "
self.setNewChronicleVar("KeyMap", NewKeyMapString.rstrip())
#xIniInput.SetControlKey('"'+controlStr+'"',key1+',',key2+',')
xIniInput.SetControlKey('"'+controlStr+'"',key1+',',key2+',')
# lose the focus when done
KeyMapDlg.dialog.noFocus()
# force writing the keymap
km.writeKeyMap()
# re-show the keymap because they may have been stupid and map the same key to multiple actions
self.IShowMappedKeys(KeyMapDlg.dialog,gKM1ControlCodesRow1,gKM1ControlCodesRow2)
# need to re-set the ini file, in case something got unmapped
#self.IMatchIniToGame()
self.IMatchIniToGame()
elif kmID == kKMDefaultsBtn:
self.ISetDefaultKeyMappings()
self.IShowMappedKeys(KeyMapDlg.dialog,gKM1ControlCodesRow1,gKM1ControlCodesRow2)
Expand Down Expand Up @@ -1876,10 +1880,18 @@ def LoadAdvSettings(self):

def LoadKeyMap(self):
km = ptKeyMap()
KeyMapString = self.getChronicleVar("KeyMap")
if not KeyMapString:
PtDebugPrint("xOptionsMenu.LoadKeyMap():\tHmm... Empty chronicle...")
return
KeyMapString = ""
kWalkForward = xIniInput.GetControlKey(xIniInput.kWalkForward)
if kWalkForward is None:
KeyMapString = self.getChronicleVar("KeyMap")
if not KeyMapString:
PtDebugPrint("xOptionsMenu.LoadKeyMap():\tHmm... Empty chronicle...")
return
else:
for key in xIniInput.kIniArray:
key = xIniInput.GetControlKey(key)
KeyMapString += re.sub('\$ $', ' ', key[0].replace(",","")+"$"+re.sub('"Game.*', '', key[1].replace(",",""))+" ")


KeyMapArray = KeyMapString.split()
# set the key binds back to the saved
Expand Down Expand Up @@ -1940,6 +1952,18 @@ def IShowMappedKeys(self,dlg,mapRow1,mapRow2):
# disable this field
field.hide()

def IMatchIniToGame(self):
km = ptKeyMap()
for control_code in defaultControlCodeBinds.keys():
if type(control_code) == type(''):
key1 = km.convertVKeyToChar(km.getBindingKeyConsole(control_code), km.getBindingFlagsConsole(control_code))
xIniInput.SetConsoleKey('"' + control_code + '"', key1 + ',')
else:
controlStr = km.convertControlCodeToString(control_code)
key1 = km.convertVKeyToChar(km.getBindingKey1(control_code), km.getBindingFlags1(control_code))
key2 = km.convertVKeyToChar(km.getBindingKey2(control_code), km.getBindingFlags2(control_code))
xIniInput.SetControlKey('"' + controlStr + '"', key1 + ',', key2 + ',')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These string concats are very difficult for me to read because it uses two different kinds of quotation marks. I'd prefer to see these become f-strings.


def ISetDefaultKeyMappings(self):
km = ptKeyMap()
KeyMapString = ""
Expand Down