Rework the logic of dragging module to cargo

This commit is contained in:
Ryan Holmes
2018-08-13 17:01:16 -04:00
parent 9977866eb0
commit d74f2b2e42
3 changed files with 79 additions and 16 deletions

View File

@@ -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()

View File

@@ -15,4 +15,5 @@ from .guiSetMode import GuiSetModeCommand
from .guiToggleCommand import GuiToggleCommandCommand
from .guiAddProjected import GuiAddProjectedCommand
from .guiRemoveProjected import GuiRemoveProjectedCommand
from .guiCargoToModule import GuiCargoToModuleCommand
from .guiCargoToModule import GuiCargoToModuleCommand
from .guiModuleToCargo import GuiModuleToCargoCommand

View File

@@ -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