cancel: both works, fixed swapped var names

class_based_refactor
Nicholas Hope 2022-09-17 22:17:34 -04:00
parent 5014161b02
commit 4babdedb81
1 changed files with 54 additions and 32 deletions

View File

@ -63,22 +63,27 @@ class bind(object):
def verify(self):
try:
typeName, self.key = self.key.split(' ', 1)
self.key = self.key.lower()
except ValueError:
# catastrophic error: either no type or no key, assume no type
self.errors.append(f'could not find type in "{self.key}"')
return
try:
self.alias = self.fields.pop('alias')
if not isinstance(self.alias, bool):
self.errors.append(f'alias should be "yes" or "no", not "{self.alias}"')
self.alias = False
except (KeyError, AttributeError, TypeError):
# KeyError, if dict but no alias field;
# AttributeError, if no pop method;
# TypeError, if pop method won't take str() as arg
self.alias = False
try:
typeName, self.key = self.key.split(' ', 1)
if not self.alias:
# don't mess with alias names
self.key = self.key.lower()
except ValueError:
# catastrophic error: either no type or no key, assume no type
self.errors.append(f'could not find type in "{self.key}"')
return
if (not self.alias) and (self.key not in validKeyList):
self.errors.append(f'invalid key name: "{self.key}"')
@ -163,7 +168,7 @@ class impulse(bind):
elif cmd == 'load_itempreset' and restOfCmd.isalpha():
try:
restOfCmd = restOfCmd.lower()
restOfCmd = ['a','b','c','d'].index(restOfCmd)
restOfCmd = str(['a','b','c','d'].index(restOfCmd))
except ValueError:
# not a load_itempreset shortcut
pass
@ -367,7 +372,7 @@ class double(bind):
self.err('requires `type` field')
return
# cancel mode
# cancel mode, must happend after type has been inferred
if 'cancel' in self.fields:
self.cancel = self.fields.pop('cancel')
if self.cancel in ('released', 'both'):
@ -408,38 +413,20 @@ class double(bind):
self.secondary.alias = True
altCode = self.secondary.toTF2()
# Make code to toggle between the two actions
# Make code to switch between the two actions
pShiftStr = f'+shift_{self.key}'
mShiftStr = f'-shift_{self.key}'
if self.cancel == 'both':
# code to cancel both if either is released
# it copies the - statement from each to both.
# if it just extracted the name of the - statement,
# you'd end up with each recursively calling the other
mainLines = mainCode.splitlines()
mainMinusLine = mainLines[1]
# second arg, without first quote
mainMinusStr = mainMinusLine.split(' ', 2)[2][1:]
altLines = altCode.splitlines()
altMinusLine = altLines[1]
# same as above
altMinusStr = altMinusLine.split(' ', 2)[2][1:]
mainLines[1] = mainLines[1][:-1] + f';{altMinusStr}'
altLines[1] = altLines[1][:-1] + f';{mainMinusStr}'
mainCode = '\n'.join(mainLines) + '\n'
altCode = '\n'.join(altLines) + '\n'
mainCode, altCode = self.cancelBoth(mainCode, altCode)
if self.type == 'hold':
self.primStr = '+' + self.primStr
self.secondStr = '+' + self.secondStr
result = mainCode + altCode +\
f'alias {pShiftStr} "{bindOrAlias} {self.key} {self.primStr}"\n' +\
f'alias {mShiftStr} "{bindOrAlias} {self.key} {self.secondStr}"\n'+\
f'alias {pShiftStr} "{bindOrAlias} {self.key} {self.secondStr}"\n' +\
f'alias {mShiftStr} "{bindOrAlias} {self.key} {self.primStr}"\n'+\
f'{bindOrAlias} {self.key} "{self.primStr}"\n'
try:
@ -465,6 +452,41 @@ class double(bind):
return result
def cancelBoth(self, mainCode, altCode) -> (str, str):
# code to cancel both if either is released
# it copies the - statement from each to both.
# if it just extracted the name of the - statement,
# you'd end up with each recursively calling the other
mainLines = mainCode.splitlines()
mainMinusLine = mainLines[1]
# second arg, without first or last quote
mainMinusStr = mainMinusLine.split(' ', 2)[2][1:-1]
altLines = altCode.splitlines()
altMinusLine = altLines[1]
# same as above
altMinusStr = altMinusLine.split(' ', 2)[2][1:-1]
# remove duplicate - actions
mainMinusList = set(mainMinusStr.split(';'))
altMinusList = set(altMinusStr.split(';'))
uniqMain = mainMinusList.difference(altMinusList)
uniqAlt = altMinusList.difference(mainMinusList)
mainMinusStr = ';'.join(uniqMain)
altMinusStr = ';'.join(uniqAlt)
if not uniqMain.issuperset(uniqAlt):
# main has things alt doesn't
mainLines[1] = mainLines[1][:-1] + f';{altMinusStr}"'
if not uniqAlt.issuperset(uniqMain):
altLines[1] = altLines[1][:-1] + f';{mainMinusStr}"'
return (
'\n'.join(mainLines) + '\n',
'\n'.join(altLines) + '\n'
)
class repeat(bind):
def verify(self):