Reimplement switcher to use built-ins too and be multi-level
This commit is contained in:
@@ -123,6 +123,7 @@ class DamagePattern:
|
||||
_builtins = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.builtin = False
|
||||
self.update(*args, **kwargs)
|
||||
|
||||
def update(self, emAmount=25, thermalAmount=25, kineticAmount=25, explosiveAmount=25):
|
||||
@@ -144,6 +145,7 @@ class DamagePattern:
|
||||
pattern = DamagePattern(emAmount=em, thermalAmount=therm, kineticAmount=kin, explosiveAmount=explo)
|
||||
pattern.ID = id
|
||||
pattern.name = name
|
||||
pattern.builtin = True
|
||||
cls._builtins[id] = pattern
|
||||
|
||||
def calculateEhp(self, fit):
|
||||
|
||||
@@ -91,6 +91,7 @@ class TargetProfile:
|
||||
_builtins = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.builtin = False
|
||||
self.update(*args, **kwargs)
|
||||
|
||||
def update(self, emAmount=0, thermalAmount=0, kineticAmount=0, explosiveAmount=0, maxVelocity=None, signatureRadius=None, radius=None):
|
||||
@@ -117,6 +118,7 @@ class TargetProfile:
|
||||
profile = TargetProfile(*data)
|
||||
profile.ID = id
|
||||
profile.name = name
|
||||
profile.builtin = True
|
||||
cls._builtins[id] = profile
|
||||
|
||||
@classmethod
|
||||
@@ -132,6 +134,7 @@ class TargetProfile:
|
||||
radius=0)
|
||||
cls._idealTarget.name = 'Ideal Target'
|
||||
cls._idealTarget.ID = -1
|
||||
cls._idealTarget.builtin = True
|
||||
return cls._idealTarget
|
||||
|
||||
@property
|
||||
|
||||
@@ -35,8 +35,8 @@ class ChangeDamagePattern(ContextMenuUnconditional):
|
||||
self.patterns.sort(key=lambda p: (p.name not in ["Uniform", "Selected Ammo"], p.name))
|
||||
|
||||
self.patternEventMap = {}
|
||||
self.items = (OrderedDict(), OrderedDict())
|
||||
|
||||
self.items = (OrderedDict(), OrderedDict())
|
||||
for pattern in self.patterns:
|
||||
remainingName = pattern.name.strip()
|
||||
container = self.items
|
||||
|
||||
@@ -34,7 +34,7 @@ class TargetProfileAdder(ContextMenuUnconditional):
|
||||
if profile is False:
|
||||
event.Skip()
|
||||
return
|
||||
self.callingWindow.addProfile(profile)
|
||||
self.callingWindow._addProfile(profile)
|
||||
|
||||
def addProfile(self, rootMenu, profile):
|
||||
id = ContextMenuUnconditional.nextID()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import gui.mainFrame
|
||||
from eos.saveddata.targetProfile import TargetProfile
|
||||
from graphs.wrapper import TargetWrapper
|
||||
from gui.contextMenu import ContextMenuSingle
|
||||
from gui.targetProfileEditor import TargetProfileEditor
|
||||
@@ -17,7 +16,7 @@ class TargetProfileEditorMenu(ContextMenuSingle):
|
||||
return False
|
||||
if not mainItem.isProfile:
|
||||
return False
|
||||
if mainItem.item is TargetProfile.getIdeal():
|
||||
if mainItem.item.builtin:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from collections import OrderedDict
|
||||
from itertools import chain
|
||||
|
||||
# noinspection PyPackageRequirements
|
||||
import wx
|
||||
@@ -20,7 +21,7 @@ class TargetProfileSwitcher(ContextMenuUnconditional):
|
||||
return False
|
||||
|
||||
sTR = svc_TargetProfile.getInstance()
|
||||
self.profiles = sTR.getUserTargetProfileList()
|
||||
self.profiles = list(chain(sTR.getBuiltinTargetProfileList(), sTR.getUserTargetProfileList()))
|
||||
self.profiles.sort(key=lambda p: (p.name in ['None'], p.name))
|
||||
|
||||
return len(self.profiles) > 0
|
||||
@@ -30,7 +31,7 @@ class TargetProfileSwitcher(ContextMenuUnconditional):
|
||||
return 'Target Resists'
|
||||
|
||||
def handleResistSwitch(self, event):
|
||||
profile = self.profileIds.get(event.Id, False)
|
||||
profile = self.profileEventMap.get(event.Id, False)
|
||||
if profile is False:
|
||||
event.Skip()
|
||||
return
|
||||
@@ -40,77 +41,63 @@ class TargetProfileSwitcher(ContextMenuUnconditional):
|
||||
sFit.setTargetProfile(fitID, profile)
|
||||
wx.PostEvent(self.mainFrame, GE.FitChanged(fitIDs=(fitID,)))
|
||||
|
||||
def addProfile(self, rootMenu, profile):
|
||||
def _addProfile(self, parentMenu, profile, name):
|
||||
id = ContextMenuUnconditional.nextID()
|
||||
name = getattr(profile, '_name', profile.name) if profile is not None else 'No Profile'
|
||||
|
||||
self.profileIds[id] = profile
|
||||
item = wx.MenuItem(rootMenu, id, name, kind=wx.ITEM_CHECK)
|
||||
rootMenu.Bind(wx.EVT_MENU, self.handleResistSwitch, item)
|
||||
self.profileEventMap[id] = profile
|
||||
menuItem = wx.MenuItem(parentMenu, id, name, kind=wx.ITEM_CHECK)
|
||||
parentMenu.Bind(wx.EVT_MENU, self.handleResistSwitch, menuItem)
|
||||
|
||||
# determine active profile
|
||||
sFit = Fit.getInstance()
|
||||
fitID = self.mainFrame.getActiveFit()
|
||||
f = sFit.getFit(fitID)
|
||||
tr = f.targetProfile
|
||||
checked = sFit.getFit(fitID).targetProfile is profile
|
||||
return menuItem, checked
|
||||
|
||||
checked = tr == profile
|
||||
|
||||
return item, checked
|
||||
def _addCategory(self, parentMenu, name):
|
||||
id = ContextMenuUnconditional.nextID()
|
||||
menuItem = wx.MenuItem(parentMenu, id, name)
|
||||
parentMenu.Bind(wx.EVT_MENU, self.handleResistSwitch, menuItem)
|
||||
return menuItem
|
||||
|
||||
def getSubMenu(self, callingWindow, context, rootMenu, i, pitem):
|
||||
self.profileIds = {}
|
||||
self.subMenus = OrderedDict()
|
||||
self.singles = []
|
||||
self.profileEventMap = {}
|
||||
|
||||
sub = wx.Menu()
|
||||
self.items = (OrderedDict(), OrderedDict())
|
||||
for profile in self.profiles:
|
||||
start, end = profile.name.find('['), profile.name.find(']')
|
||||
if start is not -1 and end is not -1:
|
||||
currBase = profile.name[start + 1:end]
|
||||
name = profile.name[end + 1:].strip()
|
||||
if not name:
|
||||
self.singles.append(profile)
|
||||
continue
|
||||
# set helper attr
|
||||
setattr(profile, '_name', name)
|
||||
if currBase not in self.subMenus:
|
||||
self.subMenus[currBase] = []
|
||||
self.subMenus[currBase].append(profile)
|
||||
else:
|
||||
self.singles.append(profile)
|
||||
# Add reset
|
||||
msw = 'wxMSW' in wx.PlatformInfo
|
||||
mitem, checked = self.addProfile(rootMenu if msw else sub, None)
|
||||
sub.Append(mitem)
|
||||
mitem.Check(checked)
|
||||
sub.AppendSeparator()
|
||||
remainingName = profile.name.strip()
|
||||
container = self.items
|
||||
while True:
|
||||
start, end = remainingName.find('['), remainingName.find(']')
|
||||
if start == -1 or end == -1:
|
||||
container[0][remainingName] = profile
|
||||
break
|
||||
container = container[1].setdefault(remainingName[start + 1:end], (OrderedDict(), OrderedDict()))
|
||||
remainingName = remainingName[end + 1:].strip()
|
||||
|
||||
# Single items, no parent
|
||||
for profile in self.singles:
|
||||
mitem, checked = self.addProfile(rootMenu if msw else sub, profile)
|
||||
sub.Append(mitem)
|
||||
mitem.Check(checked)
|
||||
# Category as menu item - expands further
|
||||
msw = "wxMSW" in wx.PlatformInfo
|
||||
|
||||
# Items that have a parent
|
||||
for menuName, profiles in list(self.subMenus.items()):
|
||||
# Create parent item for root menu that is simply name of parent
|
||||
item = wx.MenuItem(rootMenu, ContextMenuUnconditional.nextID(), menuName)
|
||||
|
||||
# Create menu for child items
|
||||
grandSub = wx.Menu()
|
||||
|
||||
# Apply child menu to parent item
|
||||
item.SetSubMenu(grandSub)
|
||||
|
||||
# Append child items to child menu
|
||||
for profile in profiles:
|
||||
mitem, checked = self.addProfile(rootMenu if msw else grandSub, profile)
|
||||
grandSub.Append(mitem)
|
||||
def makeMenu(container, parentMenu, first=False):
|
||||
menu = wx.Menu()
|
||||
if first:
|
||||
mitem, checked = self._addProfile(rootMenu if msw else parentMenu, None, 'No Profile')
|
||||
menu.Append(mitem)
|
||||
mitem.Check(checked)
|
||||
sub.Append(item) # finally, append parent item to root menu
|
||||
menu.AppendSeparator()
|
||||
for name, pattern in container[0].items():
|
||||
menuItem, checked = self._addProfile(rootMenu if msw else parentMenu, pattern, name)
|
||||
menu.Append(menuItem)
|
||||
menuItem.Check(checked)
|
||||
for name, subcontainer in container[1].items():
|
||||
menuItem = self._addCategory(rootMenu if msw else parentMenu, name)
|
||||
subMenu = makeMenu(subcontainer, menu)
|
||||
menuItem.SetSubMenu(subMenu)
|
||||
menu.Append(menuItem)
|
||||
menu.Bind(wx.EVT_MENU, self.handleResistSwitch)
|
||||
return menu
|
||||
|
||||
return sub
|
||||
subMenu = makeMenu(self.items, rootMenu, first=True)
|
||||
return subMenu
|
||||
|
||||
|
||||
TargetProfileSwitcher.register()
|
||||
|
||||
Reference in New Issue
Block a user