From d74f2b2e422dcc2de83c71a9c9174f08629781dd Mon Sep 17 00:00:00 2001 From: Ryan Holmes Date: Mon, 13 Aug 2018 17:01:16 -0400 Subject: [PATCH] Rework the logic of dragging module to cargo --- gui/builtinAdditionPanes/cargoView.py | 22 +++------ gui/fitCommands/__init__.py | 3 +- gui/fitCommands/guiModuleToCargo.py | 70 +++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 gui/fitCommands/guiModuleToCargo.py diff --git a/gui/builtinAdditionPanes/cargoView.py b/gui/builtinAdditionPanes/cargoView.py index dbc722eac..8eb54875f 100644 --- a/gui/builtinAdditionPanes/cargoView.py +++ b/gui/builtinAdditionPanes/cargoView.py @@ -126,22 +126,14 @@ class CargoView(d.Display): if not result: return - if dstRow != -1: # we're swapping with cargo - if mstate.cmdDown: # if copying, append to cargo - sFit.addCargo(self.mainFrame.getActiveFit(), module.item.ID if not module.item.isAbyssal else module.baseItemID) - else: # else, move / swap - # self.mainFrame.command.Submit(cmd.GuiCargoToModuleCommand( - # self.mainFrame.getActiveFit(), - # module.modPosition, - # dstRow)) - sFit.moveCargoToModule(self.mainFrame.getActiveFit(), module.position, dstRow) - else: # dragging to blank spot, append - sFit.addCargo(self.mainFrame.getActiveFit(), module.item.ID if not module.item.isAbyssal else module.baseItemID) + cargoPos = dstRow if dstRow > -1 else None - if not mstate.cmdDown: # if not copying, remove module - sFit.removeModule(self.mainFrame.getActiveFit(), module.position) - - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.mainFrame.getActiveFit(), action="moddel", typeID=module.item.ID)) + self.mainFrame.command.Submit(cmd.GuiModuleToCargoCommand( + self.mainFrame.getActiveFit(), + module.modPosition, + cargoPos, + mstate.cmdDown + )) def fitChanged(self, event): sFit = Fit.getInstance() diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index 75889255f..966295b7e 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -15,4 +15,5 @@ from .guiSetMode import GuiSetModeCommand from .guiToggleCommand import GuiToggleCommandCommand from .guiAddProjected import GuiAddProjectedCommand from .guiRemoveProjected import GuiRemoveProjectedCommand -from .guiCargoToModule import GuiCargoToModuleCommand \ No newline at end of file +from .guiCargoToModule import GuiCargoToModuleCommand +from .guiModuleToCargo import GuiModuleToCargoCommand \ No newline at end of file diff --git a/gui/fitCommands/guiModuleToCargo.py b/gui/fitCommands/guiModuleToCargo.py new file mode 100644 index 000000000..ef8e84b7a --- /dev/null +++ b/gui/fitCommands/guiModuleToCargo.py @@ -0,0 +1,70 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from gui.fitCommands.calc.fitSetCharge import FitSetChargeCommand +from gui.fitCommands.calc.fitReplaceModule import FitReplaceModuleCommand +from gui.fitCommands.calc.fitRemoveCargo import FitRemoveCargoCommand +from gui.fitCommands.calc.fitRemoveModule import FitRemoveModuleCommand + +from .calc.fitAddCargo import FitAddCargoCommand +from logbook import Logger +pyfalog = Logger(__name__) + +class GuiModuleToCargoCommand(wx.Command): + def __init__(self, fitID, moduleIdx, cargoIdx, copy=False): + wx.Command.__init__(self, True, "Module State Change") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.fitID = fitID + self.moduleIdx = moduleIdx + self.cargoIdx = cargoIdx + self.copy = copy + self.internal_history = wx.CommandProcessor() + + def Do(self): + sFit = Fit.getInstance() + fit = sFit.getFit(self.fitID) + module = fit.modules[self.moduleIdx] + result = False + + if self.cargoIdx: # we're swapping with cargo + if self.copy: # if copying, simply add item to cargo + result = self.internal_history.Submit(FitAddCargoCommand(self.mainFrame.getActiveFit(), module.item.ID if not module.item.isAbyssal else module.baseItemID)) + else: # otherwise, try to swap by replacing module with cargo item. If successful, remove old cargo and add new cargo + + cargo = fit.cargo[self.cargoIdx] + self.modReplaceCmd = FitReplaceModuleCommand(self.fitID, module.modPosition, cargo.itemID) + + result = self.internal_history.Submit(self.modReplaceCmd) + + if not result: + # creating module failed for whatever reason + return False + + if self.modReplaceCmd.old_module is not None: + # we're swapping with an existing module, so remove cargo and add module + self.removeCmd = FitRemoveCargoCommand(self.fitID, cargo.itemID) + result = self.internal_history.Submit(self.removeCmd) + + self.addCargoCmd = FitAddCargoCommand(self.fitID, self.modReplaceCmd.old_module.itemID) + result = self.internal_history.Submit(self.addCargoCmd) + + else: # dragging to blank spot, append + result = self.internal_history.Submit(FitAddCargoCommand(self.mainFrame.getActiveFit(), + module.item.ID if not module.item.isAbyssal else module.baseItemID)) + + if not self.copy: # if not copying, remove module + self.internal_history.Submit(FitRemoveModuleCommand(self.mainFrame.getActiveFit(), [self.moduleIdx])) + + + if result: + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="moddel", typeID=module.item.ID)) + return result + + def Undo(self): + for _ in self.internal_history.Commands: + self.internal_history.Undo() + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True