From 9523c6f3496f1d696020fdce3036ac79bd29c9f9 Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Mon, 15 Apr 2019 20:03:00 +0300 Subject: [PATCH] Add meta swap support to projected items --- gui/builtinContextMenus/metaSwap.py | 50 ++++++++++++------- gui/fitCommands/__init__.py | 9 ++-- gui/fitCommands/calc/drone/projectedAdd.py | 6 ++- gui/fitCommands/calc/drone/projectedRemove.py | 13 ++--- gui/fitCommands/gui/localDrone/changeMeta.py | 4 +- .../gui/localFighter/changeMeta.py | 4 +- .../{changeMeta.py => changeMetas.py} | 4 +- .../gui/projectedDrone/changeAmount.py | 2 +- .../gui/projectedDrone/changeMeta.py | 43 ++++++++++++++++ gui/fitCommands/gui/projectedDrone/remove.py | 2 +- .../gui/projectedFighter/changeMeta.py | 38 ++++++++++++++ .../gui/projectedModule/changeMeta.py | 39 +++++++++++++++ 12 files changed, 177 insertions(+), 37 deletions(-) rename gui/fitCommands/gui/localModule/{changeMeta.py => changeMetas.py} (92%) create mode 100644 gui/fitCommands/gui/projectedDrone/changeMeta.py create mode 100644 gui/fitCommands/gui/projectedFighter/changeMeta.py create mode 100644 gui/fitCommands/gui/projectedModule/changeMeta.py diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index f1e98f4cb..3e9e8bdb4 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -19,12 +19,15 @@ class MetaSwap(ContextMenu): return False if self.mainFrame.getActiveFit() is None or srcContext not in ( - "fittingModule", - "droneItem", - "fighterItem", - "boosterItem", - "implantItem", - "cargoItem", + 'fittingModule', + 'droneItem', + 'fighterItem', + 'boosterItem', + 'implantItem', + 'cargoItem', + 'projectedModule', + 'projectedDrone', + 'projectedFighter' ): return False @@ -48,7 +51,7 @@ class MetaSwap(ContextMenu): return True def getText(self, itmContext, selection): - return "Variations" + return 'Variations' def getSubMenu(self, context, selection, rootMenu, i, pitem): self.moduleLookup = {} @@ -56,9 +59,9 @@ class MetaSwap(ContextMenu): fit = sFit.getFit(self.mainFrame.getActiveFit()) def get_metalevel(x): - if "metaLevel" not in x.attributes: + if 'metaLevel' not in x.attributes: return 0 - return x.attributes["metaLevel"].value + return x.attributes['metaLevel'].value def get_metagroup(x): return x.metaGroup.ID if x.metaGroup is not None else 0 @@ -78,7 +81,7 @@ class MetaSwap(ContextMenu): # If on Windows we need to bind out events into the root menu, on other # platforms they need to go to our sub menu - if "wxMSW" in wx.PlatformInfo: + if 'wxMSW' in wx.PlatformInfo: bindmenu = rootMenu else: bindmenu = m @@ -86,10 +89,10 @@ class MetaSwap(ContextMenu): # Sort items by metalevel, and group within that metalevel items = list(self.variations) - if "implantItem" in context: + if 'implantItem' in context: # sort implants based on name items.sort(key=lambda x: x.name) - elif "boosterItem" in context: + elif 'boosterItem' in context: # boosters don't have meta or anything concrete that we can rank by. Go by chance to inflict side effect items.sort(key=get_boosterrank) else: @@ -100,14 +103,14 @@ class MetaSwap(ContextMenu): group = None for item in items: # Apparently no metaGroup for the Tech I variant: - if "subSystem" in item.effects: + if 'subSystem' in item.effects: thisgroup = item.marketGroup.marketGroupName elif item.metaGroup is None: - thisgroup = "Tech I" + thisgroup = 'Tech I' else: thisgroup = item.metaGroup.name - if thisgroup != group and context not in ("implantItem", "boosterItem"): + if thisgroup != group and context not in ('implantItem', 'boosterItem'): group = thisgroup id = ContextMenu.nextID() m.Append(id, '─ %s ─' % group) @@ -133,15 +136,15 @@ class MetaSwap(ContextMenu): fit = Fit.getInstance().getFit(fitID) if context == 'fittingModule': positions = [mod.modPosition for mod in self.selection] - self.mainFrame.command.Submit(cmd.GuiChangeModuleMetaCommand( + self.mainFrame.command.Submit(cmd.GuiChangeLocalModuleMetasCommand( fitID=fitID, positions=positions, newItemID=item.ID)) elif context == 'droneItem': position = fit.drones.index(self.selection[0]) - self.mainFrame.command.Submit(cmd.GuiChangeDroneMetaCommand( + self.mainFrame.command.Submit(cmd.GuiChangeLocalDroneMetaCommand( fitID=fitID, position=position, newItemID=item.ID)) elif context == 'fighterItem': position = fit.fighters.index(self.selection[0]) - self.mainFrame.command.Submit(cmd.GuiChangeFighterMetaCommand( + self.mainFrame.command.Submit(cmd.GuiChangeLocalFighterMetaCommand( fitID=fitID, position=position, newItemID=item.ID)) elif context == 'implantItem': position = fit.implants.index(self.selection[0]) @@ -154,6 +157,17 @@ class MetaSwap(ContextMenu): elif context == 'cargoItem': self.mainFrame.command.Submit(cmd.GuiChangeCargoMetaCommand( fitID=fitID, itemID=self.selection[0].itemID, newItemID=item.ID)) + elif context == 'projectedModule': + position = fit.projectedModules.index(self.selection[0]) + self.mainFrame.command.Submit(cmd.GuiChangeProjectedModuleMetaCommand( + fitID=fitID, position=position, newItemID=item.ID)) + elif context == 'projectedDrone': + self.mainFrame.command.Submit(cmd.GuiChangeProjectedDroneMetaCommand( + fitID=fitID, itemID=self.selection[0].itemID, newItemID=item.ID)) + elif context == 'projectedFighter': + position = fit.projectedFighters.index(self.selection[0]) + self.mainFrame.command.Submit(cmd.GuiChangeProjectedFighterMetaCommand( + fitID=fitID, position=position, newItemID=item.ID)) MetaSwap.register() diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index 8a100cc25..3eb140b16 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -12,7 +12,6 @@ from .gui.commandFit.remove import GuiRemoveCommandFitCommand from .gui.commandFit.toggleState import GuiToggleCommandFitStateCommand from .gui.fitRename import GuiRenameFitCommand from .gui.guiCargoToModule import GuiCargoToModuleCommand -from .gui.localModule.changeMeta import GuiChangeModuleMetaCommand from .gui.guiModuleToCargo import GuiModuleToCargoCommand from .gui.guiSwapCloneModule import GuiModuleSwapOrCloneCommand from .gui.implant.add import GuiAddImplantCommand @@ -23,17 +22,18 @@ from .gui.implant.toggleState import GuiToggleImplantStateCommand from .gui.itemsRebase import GuiRebaseItemsCommand from .gui.localDrone.add import GuiAddLocalDroneCommand from .gui.localDrone.changeAmount import GuiChangeLocalDroneAmountCommand -from .gui.localDrone.changeMeta import GuiChangeDroneMetaCommand +from .gui.localDrone.changeMeta import GuiChangeLocalDroneMetaCommand from .gui.localDrone.remove import GuiRemoveLocalDroneCommand from .gui.localDrone.toggleState import GuiToggleLocalDroneStateCommand from .gui.localFighter.abilityToggleState import GuiToggleLocalFighterAbilityStateCommand from .gui.localFighter.add import GuiAddLocalFighterCommand from .gui.localFighter.changeAmount import GuiChangeLocalFighterAmountCommand -from .gui.localFighter.changeMeta import GuiChangeFighterMetaCommand +from .gui.localFighter.changeMeta import GuiChangeLocalFighterMetaCommand from .gui.localFighter.remove import GuiRemoveLocalFighterCommand from .gui.localFighter.toggleState import GuiToggleLocalFighterStateCommand from .gui.localModule.add import GuiAddLocalModuleCommand from .gui.localModule.changeCharges import GuiChangeLocalModuleChargesCommand +from .gui.localModule.changeMetas import GuiChangeLocalModuleMetasCommand from .gui.localModule.changeSpool import GuiChangeLocalModuleSpoolCommand from .gui.localModule.changeStates import GuiChangeLocalModuleStatesCommand from .gui.localModule.fill import GuiFillWithLocalModulesCommand @@ -43,11 +43,13 @@ from .gui.localModule.mutatedRevert import GuiRevertMutatedLocalModuleCommand from .gui.localModule.remove import GuiRemoveLocalModuleCommand from .gui.projectedDrone.add import GuiAddProjectedDroneCommand from .gui.projectedDrone.changeAmount import GuiChangeProjectedDroneAmountCommand +from .gui.projectedDrone.changeMeta import GuiChangeProjectedDroneMetaCommand from .gui.projectedDrone.remove import GuiRemoveProjectedDroneCommand from .gui.projectedDrone.toggleState import GuiToggleProjectedDroneStateCommand from .gui.projectedFighter.abilityToggleState import GuiToggleProjectedFighterAbilityStateCommand from .gui.projectedFighter.add import GuiAddProjectedFighterCommand from .gui.projectedFighter.changeAmount import GuiChangeProjectedFighterAmountCommand +from .gui.projectedFighter.changeMeta import GuiChangeProjectedFighterMetaCommand from .gui.projectedFighter.remove import GuiRemoveProjectedFighterCommand from .gui.projectedFighter.toggleState import GuiToggleProjectedFighterStateCommand from .gui.projectedFit.add import GuiAddProjectedFitCommand @@ -56,6 +58,7 @@ from .gui.projectedFit.remove import GuiRemoveProjectedFitCommand from .gui.projectedFit.toggleState import GuiToggleProjectedFitStateCommand from .gui.projectedModule.add import GuiAddProjectedModuleCommand from .gui.projectedModule.changeCharges import GuiChangeProjectedModuleChargesCommand +from .gui.projectedModule.changeMeta import GuiChangeProjectedModuleMetaCommand from .gui.projectedModule.changeSpool import GuiChangeProjectedModuleSpoolCommand from .gui.projectedModule.changeState import GuiChangeProjectedModuleStateCommand from .gui.projectedModule.remove import GuiRemoveProjectedModuleCommand diff --git a/gui/fitCommands/calc/drone/projectedAdd.py b/gui/fitCommands/calc/drone/projectedAdd.py index e1be7f2c1..550aa8641 100644 --- a/gui/fitCommands/calc/drone/projectedAdd.py +++ b/gui/fitCommands/calc/drone/projectedAdd.py @@ -1,3 +1,5 @@ +import math + import wx from logbook import Logger @@ -60,7 +62,7 @@ class CalcAddProjectedDroneCommand(wx.Command): drone.amount = self.savedDroneInfo.amount drone.amountActive = self.savedDroneInfo.amountActive return True - # Removing new stack + # Removing previously added stack from .projectedRemove import CalcRemoveProjectedDroneCommand - cmd = CalcRemoveProjectedDroneCommand(fitID=self.fitID, droneInfo=self.droneInfo) + cmd = CalcRemoveProjectedDroneCommand(fitID=self.fitID, itemID=self.droneInfo.itemID, amount=math.inf) return cmd.Do() diff --git a/gui/fitCommands/calc/drone/projectedRemove.py b/gui/fitCommands/calc/drone/projectedRemove.py index 39f753035..e56879d91 100644 --- a/gui/fitCommands/calc/drone/projectedRemove.py +++ b/gui/fitCommands/calc/drone/projectedRemove.py @@ -11,21 +11,22 @@ pyfalog = Logger(__name__) class CalcRemoveProjectedDroneCommand(wx.Command): - def __init__(self, fitID, droneInfo): + def __init__(self, fitID, itemID, amount): wx.Command.__init__(self, True, 'Remove Projected Drone') self.fitID = fitID - self.droneInfo = droneInfo + self.itemID = itemID + self.amountToRemove = amount self.savedDroneInfo = None def Do(self): - pyfalog.debug('Doing removal of projected drone {} from fit {}'.format(self.droneInfo, self.fitID)) + pyfalog.debug('Doing removal of {} projected drones {} from fit {}'.format(self.amountToRemove, self.itemID, self.fitID)) fit = Fit.getInstance().getFit(self.fitID) - drone = next((pd for pd in fit.projectedDrones if pd.itemID == self.droneInfo.itemID), None) + drone = next((pd for pd in fit.projectedDrones if pd.itemID == self.itemID), None) if drone is None: pyfalog.warning('Unable to find projected drone') return False self.savedDroneInfo = DroneInfo.fromDrone(drone) - drone.amount = max(drone.amount - self.droneInfo.amount, 0) + drone.amount = max(drone.amount - self.amountToRemove, 0) # Remove stack if we have no items remaining if drone.amount == 0: fit.projectedDrones.remove(drone) @@ -36,7 +37,7 @@ class CalcRemoveProjectedDroneCommand(wx.Command): return True def Undo(self): - pyfalog.debug('Undoing removal of projected drone {} from fit {}'.format(self.droneInfo, self.fitID)) + pyfalog.debug('Undoing removal of {} projected drones {} from fit {}'.format(self.amountToRemove, self.itemID, self.fitID)) fit = Fit.getInstance().getFit(self.fitID) # Change stack if we still have it drone = next((pd for pd in fit.projectedDrones if pd.itemID == self.savedDroneInfo.itemID), None) diff --git a/gui/fitCommands/gui/localDrone/changeMeta.py b/gui/fitCommands/gui/localDrone/changeMeta.py index 18e2bc6f5..cf3e61467 100644 --- a/gui/fitCommands/gui/localDrone/changeMeta.py +++ b/gui/fitCommands/gui/localDrone/changeMeta.py @@ -10,10 +10,10 @@ from gui.fitCommands.helpers import DroneInfo, InternalCommandHistory from service.fit import Fit -class GuiChangeDroneMetaCommand(wx.Command): +class GuiChangeLocalDroneMetaCommand(wx.Command): def __init__(self, fitID, position, newItemID): - wx.Command.__init__(self, True, 'Change Drone Meta') + wx.Command.__init__(self, True, 'Change Local Drone Meta') self.internalHistory = InternalCommandHistory() self.fitID = fitID self.position = position diff --git a/gui/fitCommands/gui/localFighter/changeMeta.py b/gui/fitCommands/gui/localFighter/changeMeta.py index 1d3dc17e6..b0e654c1d 100644 --- a/gui/fitCommands/gui/localFighter/changeMeta.py +++ b/gui/fitCommands/gui/localFighter/changeMeta.py @@ -8,10 +8,10 @@ from gui.fitCommands.helpers import FighterInfo, InternalCommandHistory from service.fit import Fit -class GuiChangeFighterMetaCommand(wx.Command): +class GuiChangeLocalFighterMetaCommand(wx.Command): def __init__(self, fitID, position, newItemID): - wx.Command.__init__(self, True, 'Change Fighter Meta') + wx.Command.__init__(self, True, 'Change Local Fighter Meta') self.internalHistory = InternalCommandHistory() self.fitID = fitID self.position = position diff --git a/gui/fitCommands/gui/localModule/changeMeta.py b/gui/fitCommands/gui/localModule/changeMetas.py similarity index 92% rename from gui/fitCommands/gui/localModule/changeMeta.py rename to gui/fitCommands/gui/localModule/changeMetas.py index 8d1d11094..42808a5e8 100644 --- a/gui/fitCommands/gui/localModule/changeMeta.py +++ b/gui/fitCommands/gui/localModule/changeMetas.py @@ -8,10 +8,10 @@ from gui.fitCommands.helpers import InternalCommandHistory, ModuleInfo from service.fit import Fit -class GuiChangeModuleMetaCommand(wx.Command): +class GuiChangeLocalModuleMetasCommand(wx.Command): def __init__(self, fitID, positions, newItemID): - wx.Command.__init__(self, True, 'Change Module Meta') + wx.Command.__init__(self, True, 'Change Local Module Metas') self.internalHistory = InternalCommandHistory() self.fitID = fitID self.positions = positions diff --git a/gui/fitCommands/gui/projectedDrone/changeAmount.py b/gui/fitCommands/gui/projectedDrone/changeAmount.py index a17a81b7c..3da19047c 100644 --- a/gui/fitCommands/gui/projectedDrone/changeAmount.py +++ b/gui/fitCommands/gui/projectedDrone/changeAmount.py @@ -23,7 +23,7 @@ class GuiChangeProjectedDroneAmountCommand(wx.Command): if self.amount > 0: cmd = CalcChangeProjectedDroneAmountCommand(fitID=self.fitID, itemID=self.itemID, amount=self.amount) else: - cmd = CalcRemoveProjectedDroneCommand(fitID=self.fitID, droneInfo=DroneInfo(itemID=self.itemID, amount=math.inf, amountActive=math.inf)) + cmd = CalcRemoveProjectedDroneCommand(fitID=self.fitID, itemID=self.itemID, amount=math.inf) success = self.internalHistory.submit(cmd) Fit.getInstance().recalc(self.fitID) wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) diff --git a/gui/fitCommands/gui/projectedDrone/changeMeta.py b/gui/fitCommands/gui/projectedDrone/changeMeta.py new file mode 100644 index 000000000..93332dcc5 --- /dev/null +++ b/gui/fitCommands/gui/projectedDrone/changeMeta.py @@ -0,0 +1,43 @@ +import math + +import wx + +import gui.mainFrame +from gui import globalEvents as GE +from gui.fitCommands.calc.drone.projectedAdd import CalcAddProjectedDroneCommand +from gui.fitCommands.calc.drone.projectedRemove import CalcRemoveProjectedDroneCommand +from gui.fitCommands.helpers import DroneInfo, InternalCommandHistory +from service.fit import Fit + + +class GuiChangeProjectedDroneMetaCommand(wx.Command): + + def __init__(self, fitID, itemID, newItemID): + wx.Command.__init__(self, True, 'Change Projected Drone Meta') + self.internalHistory = InternalCommandHistory() + self.fitID = fitID + self.itemID = itemID + self.newItemID = newItemID + + def Do(self): + sFit = Fit.getInstance() + fit = sFit.getFit(self.fitID) + drone = next((pd for pd in fit.projectedDrones if pd.itemID == self.itemID), None) + if drone is None: + return False + if drone.itemID == self.newItemID: + return False + info = DroneInfo.fromDrone(drone) + info.itemID = self.newItemID + cmdRemove = CalcRemoveProjectedDroneCommand(fitID=self.fitID, itemID=self.itemID, amount=math.inf) + cmdAdd = CalcAddProjectedDroneCommand(fitID=self.fitID, droneInfo=info) + success = self.internalHistory.submitBatch(cmdRemove, cmdAdd) + sFit.recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success + + def Undo(self): + success = self.internalHistory.undoAll() + Fit.getInstance().recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success diff --git a/gui/fitCommands/gui/projectedDrone/remove.py b/gui/fitCommands/gui/projectedDrone/remove.py index 6f33334b8..fdfaf63c8 100644 --- a/gui/fitCommands/gui/projectedDrone/remove.py +++ b/gui/fitCommands/gui/projectedDrone/remove.py @@ -17,7 +17,7 @@ class GuiRemoveProjectedDroneCommand(wx.Command): self.amount = amount def Do(self): - cmd = CalcRemoveProjectedDroneCommand(fitID=self.fitID, droneInfo=DroneInfo(itemID=self.itemID, amount=self.amount, amountActive=self.amount)) + cmd = CalcRemoveProjectedDroneCommand(fitID=self.fitID, itemID=self.itemID, amount=self.amount) success = self.internalHistory.submit(cmd) Fit.getInstance().recalc(self.fitID) wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) diff --git a/gui/fitCommands/gui/projectedFighter/changeMeta.py b/gui/fitCommands/gui/projectedFighter/changeMeta.py new file mode 100644 index 000000000..02a74c4a7 --- /dev/null +++ b/gui/fitCommands/gui/projectedFighter/changeMeta.py @@ -0,0 +1,38 @@ +import wx + +import gui.mainFrame +from gui import globalEvents as GE +from gui.fitCommands.calc.fighter.projectedAdd import CalcAddProjectedFighterCommand +from gui.fitCommands.calc.fighter.projectedRemove import CalcRemoveProjectedFighterCommand +from gui.fitCommands.helpers import FighterInfo, InternalCommandHistory +from service.fit import Fit + + +class GuiChangeProjectedFighterMetaCommand(wx.Command): + + def __init__(self, fitID, position, newItemID): + wx.Command.__init__(self, True, 'Change Projected Fighter Meta') + self.internalHistory = InternalCommandHistory() + self.fitID = fitID + self.position = position + self.newItemID = newItemID + + def Do(self): + sFit = Fit.getInstance() + fighter = sFit.getFit(self.fitID).projectedFighters[self.position] + if fighter.itemID == self.newItemID: + return False + info = FighterInfo.fromFighter(fighter) + info.itemID = self.newItemID + cmdRemove = CalcRemoveProjectedFighterCommand(fitID=self.fitID, position=self.position) + cmdAdd = CalcAddProjectedFighterCommand(fitID=self.fitID, fighterInfo=info) + success = self.internalHistory.submitBatch(cmdRemove, cmdAdd) + sFit.recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success + + def Undo(self): + success = self.internalHistory.undoAll() + Fit.getInstance().recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success diff --git a/gui/fitCommands/gui/projectedModule/changeMeta.py b/gui/fitCommands/gui/projectedModule/changeMeta.py new file mode 100644 index 000000000..5259f6a2d --- /dev/null +++ b/gui/fitCommands/gui/projectedModule/changeMeta.py @@ -0,0 +1,39 @@ +import wx + +import gui.mainFrame +from gui import globalEvents as GE +from gui.fitCommands.calc.module.projectedAdd import CalcAddProjectedModuleCommand +from gui.fitCommands.calc.module.projectedRemove import CalcRemoveProjectedModuleCommand +from gui.fitCommands.helpers import InternalCommandHistory, ModuleInfo +from service.fit import Fit + + +class GuiChangeProjectedModuleMetaCommand(wx.Command): + + def __init__(self, fitID, position, newItemID): + wx.Command.__init__(self, True, 'Change Projected Module Meta') + self.internalHistory = InternalCommandHistory() + self.fitID = fitID + self.position = position + self.newItemID = newItemID + + def Do(self): + sFit = Fit.getInstance() + fit = sFit.getFit(self.fitID) + module = fit.projectedModules[self.position] + if module.itemID == self.newItemID: + return + info = ModuleInfo.fromModule(module) + info.itemID = self.newItemID + cmdRemove = CalcRemoveProjectedModuleCommand(fitID=self.fitID, position=self.position) + cmdAdd = CalcAddProjectedModuleCommand(fitID=self.fitID, modInfo=info) + success = self.internalHistory.submitBatch(cmdRemove, cmdAdd) + sFit.recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success + + def Undo(self): + success = self.internalHistory.undoAll() + Fit.getInstance().recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success