From a5a152d3956d2100b5d7c3cffbcd0daaa3529bc7 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Tue, 14 Aug 2018 00:58:50 -0400 Subject: [PATCH] Move meta swap functionality to command process --- gui/builtinContextMenus/metaSwap.py | 134 +++++++++++++------------- gui/fitCommands/__init__.py | 3 +- gui/fitCommands/calc/fitAddFighter.py | 2 +- gui/fitCommands/guiMetaSwap.py | 64 ++++++++++++ 4 files changed, 136 insertions(+), 67 deletions(-) create mode 100644 gui/fitCommands/guiMetaSwap.py diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index 871ebad68..bd98c6bfa 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -122,82 +122,86 @@ class MetaSwap(ContextMenu): id = ContextMenu.nextID() mitem = wx.MenuItem(rootMenu, id, item.name) bindmenu.Bind(wx.EVT_MENU, self.handleModule, mitem) - self.moduleLookup[id] = item + print(context) + self.moduleLookup[id] = item, context m.Append(mitem) return m def handleModule(self, event): - item = self.moduleLookup.get(event.Id, None) + item, context = self.moduleLookup.get(event.Id, None) if item is None: event.Skip() return - sFit = Fit.getInstance() fitID = self.mainFrame.getActiveFit() - fit = sFit.getFit(fitID) - for selected_item in self.selection: - if isinstance(selected_item, Module): - pos = fit.modules.index(selected_item) - sFit.changeModule(fitID, pos, item.ID) - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + self.mainFrame.command.Submit(cmd.GuiMetaSwapCommand(fitID, context, item.ID, self.selection)) - elif isinstance(selected_item, Drone): - drone_count = None - - for idx, drone_stack in enumerate(fit.drones): - if drone_stack is selected_item: - drone_count = drone_stack.amount - sFit.removeDrone(fitID, idx, drone_count, False) - break - - if drone_count: - sFit.addDrone(fitID, item.ID, drone_count, True) - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) - - elif isinstance(selected_item, Fighter): - fighter_count = None - - for idx, fighter_stack in enumerate(fit.fighters): - # Right now fighters always will have max stack size. - # Including this for future improvement, so if adjustable - # fighter stacks get added we're ready for it. - if fighter_stack is selected_item: - if fighter_stack.amount > 0: - fighter_count = fighter_stack.amount - elif fighter_stack.amount == -1: - fighter_count = fighter_stack.amountActive - else: - fighter_count.amount = 0 - - sFit.removeFighter(fitID, idx, False) - break - - sFit.addFighter(fitID, item.ID, True) - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) - - elif isinstance(selected_item, Booster): - for idx, booster_stack in enumerate(fit.boosters): - if booster_stack is selected_item: - self.mainFrame.command.Submit(cmd.GuiRemoveBoosterCommand(fitID, idx)) - self.mainFrame.command.Submit(cmd.GuiAddBoosterCommand(fitID, item.ID)) - break - - elif isinstance(selected_item, Implant): - for idx, implant_stack in enumerate(fit.implants): - if implant_stack is selected_item: - self.mainFrame.command.Submit(cmd.GuiRemoveImplantCommand(fitID, idx)) - self.mainFrame.command.Submit(cmd.GuiAddImplantCommand(fitID, item.ID)) - break - - elif isinstance(selected_item, Cargo): - for idx, cargo_stack in enumerate(fit.cargo): - if cargo_stack is selected_item: - # todo: make a command to change varieance of all items, or maybe per item type, which would - # utilize the two fitting commands that we need to remove then add? - sFit.removeCargo(fitID, idx) - self.mainFrame.command.Submit(cmd.GuiAddCargoCommand(fitID, item.ID, cargo_stack.amount, True)) - break + # for selected_item in self.selection: + # if isinstance(selected_item, Module): + # pos = fit.modules.index(selected_item) + # self.mainFrame.command.Submit(cmd.GuiModuleAddCommand(fitID, item.ID, pos)) + # + # + # sFit.changeModule(fitID, pos, item.ID) + # wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + # + # elif isinstance(selected_item, Drone): + # drone_count = None + # + # for idx, drone_stack in enumerate(fit.drones): + # if drone_stack is selected_item: + # drone_count = drone_stack.amount + # sFit.removeDrone(fitID, idx, drone_count, False) + # break + # + # if drone_count: + # sFit.addDrone(fitID, item.ID, drone_count, True) + # wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + # + # elif isinstance(selected_item, Fighter): + # fighter_count = None + # + # for idx, fighter_stack in enumerate(fit.fighters): + # # Right now fighters always will have max stack size. + # # Including this for future improvement, so if adjustable + # # fighter stacks get added we're ready for it. + # if fighter_stack is selected_item: + # if fighter_stack.amount > 0: + # fighter_count = fighter_stack.amount + # elif fighter_stack.amount == -1: + # fighter_count = fighter_stack.amountActive + # else: + # fighter_count.amount = 0 + # + # sFit.removeFighter(fitID, idx, False) + # break + # + # sFit.addFighter(fitID, item.ID, True) + # wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + # + # elif isinstance(selected_item, Booster): + # for idx, booster_stack in enumerate(fit.boosters): + # if booster_stack is selected_item: + # self.mainFrame.command.Submit(cmd.GuiRemoveBoosterCommand(fitID, idx)) + # self.mainFrame.command.Submit(cmd.GuiAddBoosterCommand(fitID, item.ID)) + # break + # + # elif isinstance(selected_item, Implant): + # for idx, implant_stack in enumerate(fit.implants): + # if implant_stack is selected_item: + # self.mainFrame.command.Submit(cmd.GuiRemoveImplantCommand(fitID, idx)) + # self.mainFrame.command.Submit(cmd.GuiAddImplantCommand(fitID, item.ID)) + # break + # + # elif isinstance(selected_item, Cargo): + # for idx, cargo_stack in enumerate(fit.cargo): + # if cargo_stack is selected_item: + # # todo: make a command to change varieance of all items, or maybe per item type, which would + # # utilize the two fitting commands that we need to remove then add? + # sFit.removeCargo(fitID, idx) + # self.mainFrame.command.Submit(cmd.GuiAddCargoCommand(fitID, item.ID, cargo_stack.amount, True)) + # break diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index 5acf62ffd..6515b5e27 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -18,4 +18,5 @@ from .guiRemoveProjected import GuiRemoveProjectedCommand from .guiCargoToModule import GuiCargoToModuleCommand from .guiModuleToCargo import GuiModuleToCargoCommand from .guiAddFighter import GuiAddFighterCommand -from .guiRemoveFighter import GuiRemoveFighterCommand \ No newline at end of file +from .guiRemoveFighter import GuiRemoveFighterCommand +from .guiMetaSwap import GuiMetaSwapCommand \ No newline at end of file diff --git a/gui/fitCommands/calc/fitAddFighter.py b/gui/fitCommands/calc/fitAddFighter.py index 9ceda53a1..dfaccbf3d 100644 --- a/gui/fitCommands/calc/fitAddFighter.py +++ b/gui/fitCommands/calc/fitAddFighter.py @@ -14,7 +14,7 @@ class FitAddFighterCommand(wx.Command): """" from sFit.addFighter """ - def __init__(self, fitID, itemID, amount=1, replace=False): + def __init__(self, fitID, itemID): wx.Command.__init__(self, True, "Cargo add") self.fitID = fitID self.itemID = itemID diff --git a/gui/fitCommands/guiMetaSwap.py b/gui/fitCommands/guiMetaSwap.py new file mode 100644 index 000000000..e40906070 --- /dev/null +++ b/gui/fitCommands/guiMetaSwap.py @@ -0,0 +1,64 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from .calc.fitRemoveImplant import FitRemoveImplantCommand +from .calc.fitAddImplant import FitAddImplantCommand +from .calc.fitRemoveBooster import FitRemoveBoosterCommand +from .calc.fitAddBooster import FitAddBoosterCommand +from .calc.fitRemoveCargo import FitRemoveCargoCommand +from .calc.fitAddCargo import FitAddCargoCommand +from .calc.fitReplaceModule import FitReplaceModuleCommand +from .calc.fitAddFighter import FitAddFighterCommand +from .calc.fitRemoveFighter import FitRemoveFighterCommand + +class GuiMetaSwapCommand(wx.Command): + def __init__(self, fitID, context, itemID, selection: list): + wx.Command.__init__(self, True, "Meta Swap") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.internal_history = wx.CommandProcessor() + self.fitID = fitID + self.itemID = itemID + self.context = context + self.data = [] + fit = self.sFit.getFit(fitID) + + if context == 'fittingModule': + for x in selection: + self.data.append(((FitReplaceModuleCommand, fitID, fit.modules.index(x), itemID),),) + elif context == 'implantItem': + for x in selection: + idx = fit.implants.index(x) + self.data.append(((FitRemoveImplantCommand, fitID, idx), (FitAddImplantCommand, fitID, itemID))) + elif context == 'boosterItem': + for x in selection: + idx = fit.boosters.index(x) + self.data.append(((FitRemoveBoosterCommand, fitID, idx), (FitAddBoosterCommand, fitID, itemID))) + elif context == 'cargoItem': + for x in selection: + self.data.append(((FitRemoveCargoCommand, fitID, x.itemID, 1, True), (FitAddCargoCommand, fitID, itemID, x.amount))) + elif context == 'fighterItem': + for x in selection: + self.data.append(((FitRemoveFighterCommand, fitID, fit.fighters.index(x)), (FitAddFighterCommand, fitID, itemID))) + elif context == 'droneItem': + raise NotImplementedError() + + + def Do(self): + for cmds in self.data: + for cmd in cmds: + self.internal_history.Submit(cmd[0](*cmd[1:])) + + self.sFit.recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + + def Undo(self): + for x in self.internal_history.Commands: + self.internal_history.Undo() + self.sFit.recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True +