From 9309ddff07321572c70a2e12faaf0937f312c8c3 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Sat, 21 Jul 2018 15:34:58 -0400 Subject: [PATCH] Created undo/redo command for module remove, along with reapplying some module-specific attributes (state, charge). Fixed a bug when trying to add a module that doesn't fit --- gui/builtinViews/fittingView.py | 10 +-------- gui/fitCommands/__init__.py | 3 ++- gui/fitCommands/moduleAdd.py | 10 +++++---- gui/fitCommands/moduleRemove.py | 37 +++++++++++++++++++++++++++++++++ service/fit.py | 18 +++++++++------- 5 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 gui/fitCommands/moduleRemove.py diff --git a/gui/builtinViews/fittingView.py b/gui/builtinViews/fittingView.py index d25f2f097..47255542f 100644 --- a/gui/builtinViews/fittingView.py +++ b/gui/builtinViews/fittingView.py @@ -401,18 +401,10 @@ class FittingView(d.Display): def removeModule(self, modules): """Removes a list of modules from the fit""" - sFit = Fit.getInstance() - if not isinstance(modules, list): modules = [modules] - positions = [mod.modPosition for mod in modules] - result = sFit.removeModule(self.activeFitID, positions) - - if result is not None: - self.slotsChanged() - ids = {mod.item.ID for mod in modules} - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.activeFitID, action="moddel", typeID=ids)) + self.mainFrame.command.Submit(cmd.FitModuleRemoveCommand(self.activeFitID, modules)) def addModule(self, x, y, srcIdx): """Add a module from the market browser""" diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index fcb59fc63..3aaaf1409 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -1,2 +1,3 @@ from .moduleStateChange import FitModuleStateChangeCommand -from.moduleAdd import FitModuleAddCommand \ No newline at end of file +from .moduleAdd import FitModuleAddCommand +from .moduleRemove import FitModuleRemoveCommand \ No newline at end of file diff --git a/gui/fitCommands/moduleAdd.py b/gui/fitCommands/moduleAdd.py index c7f4e21f7..e5b038317 100644 --- a/gui/fitCommands/moduleAdd.py +++ b/gui/fitCommands/moduleAdd.py @@ -7,8 +7,8 @@ from gui import globalEvents as GE class FitModuleAddCommand(wx.Command): def __init__(self, fitID, itemID): - # todo: instead of modules, needs to be positions. Dead objects are a thing wx.Command.__init__(self, True, "Module Add") + # todo: evaluate mutaplasmid modules self.mainFrame = gui.mainFrame.MainFrame.getInstance() self.sFit = Fit.getInstance() self.fitID = fitID @@ -16,6 +16,7 @@ class FitModuleAddCommand(wx.Command): self.new_position = None def Do(self): + # todo: figure how not to add this command to stack if module doesn't fit correctly. populate, self.new_position = self.sFit.appendModule(self.fitID, self.itemID) if populate is not None: # self.slotsChanged() # unsure how to handle this right now? Perhaps move this to the event itself? @@ -23,7 +24,8 @@ class FitModuleAddCommand(wx.Command): return True def Undo(self): - # todo: self.slotsChanged() - result = self.sFit.removeModule(self.fitID, [self.new_position]) - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="moddel", typeID=self.itemID)) + if (self.new_position): + # todo: self.slotsChanged() + result = self.sFit.removeModule(self.fitID, [self.new_position]) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="moddel", typeID=self.itemID)) return True diff --git a/gui/fitCommands/moduleRemove.py b/gui/fitCommands/moduleRemove.py new file mode 100644 index 000000000..df1bda3a6 --- /dev/null +++ b/gui/fitCommands/moduleRemove.py @@ -0,0 +1,37 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from collections import namedtuple + + +ModuleInfoCache = namedtuple('ModuleInfoCache', ['modPosition', 'itemID', 'state', 'charge']) + +class FitModuleRemoveCommand(wx.Command): + def __init__(self, fitID, modules): + # todo: evaluate mutaplasmid modules + wx.Command.__init__(self, True, "Module Remove") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.fitID = fitID + self.modCache = [ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge) for mod in modules] + + def Do(self): + self.sFit.getFit(self.fitID) + result = self.sFit.removeModule(self.fitID, [mod.modPosition for mod in self.modCache]) + + if result is not None: + # self.slotsChanged() # todo: fix + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="moddel", typeID=set([mod.itemID for mod in self.modCache]))) + return True + + def Undo(self): + for mod in self.modCache: + m = self.sFit.changeModule(self.fitID, mod.modPosition, mod.itemID, False) + m.state = mod.state + m.charge = mod.charge + self.sFit.recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="modadd", typeID=set([mod.itemID for mod in self.modCache]))) + + return True diff --git a/service/fit.py b/service/fit.py index 245007dda..1172cd2fb 100644 --- a/service/fit.py +++ b/service/fit.py @@ -568,7 +568,7 @@ class Fit(object): return numSlots != len(fit.modules), m.modPosition else: - return None + return None, None def removeModule(self, fitID, positions): """Removes modules based on a number of positions.""" @@ -627,7 +627,7 @@ class Fit(object): else: return None - def changeModule(self, fitID, position, newItemID): + def changeModule(self, fitID, position, newItemID, recalc=True): fit = eos.db.getFit(fitID) # We're trying to add a charge to a slot, which won't work. Instead, try to add the charge to the module in that slot. @@ -656,15 +656,16 @@ class Fit(object): if m.isValidState(State.ACTIVE): m.state = State.ACTIVE - # As some items may affect state-limiting attributes of the ship, calculate new attributes first - self.recalc(fit) + if (recalc): + # As some items may affect state-limiting attributes of the ship, calculate new attributes first + self.recalc(fit) # Then, check states of all modules and change where needed. This will recalc if needed self.checkStates(fit, m) fit.fill() eos.db.commit() - return True + return m else: return None @@ -1073,7 +1074,7 @@ class Fit(object): def isAmmo(itemID): return eos.db.getItem(itemID).category.name == "Charge" - def setAmmo(self, fitID, ammoID, modules): + def setAmmo(self, fitID, ammoID, modules, recalc=True): pyfalog.debug("Set ammo for fit ID: {0}", fitID) if fitID is None: return @@ -1085,7 +1086,8 @@ class Fit(object): if mod.isValidCharge(ammo): mod.charge = ammo - self.recalc(fit) + if recalc: + self.recalc(fit) @staticmethod def getTargetResists(fitID): @@ -1259,6 +1261,8 @@ class Fit(object): self.recalc(fit) def recalc(self, fit): + if isinstance(fit, int): + fit = self.getFit(fit) start_time = time() pyfalog.info("=" * 10 + "recalc: {0}" + "=" * 10, fit.name)