From 7f100353e2fc59bd75a0a636cc965386ccc860bd Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Sun, 16 Aug 2015 18:30:27 +0100 Subject: [PATCH] Make variations menu actually swap out module Functional, but not handling multiple selections well --- eos/effectHandlerHelpers.py | 4 ++++ gui/builtinContextMenus/metaSwap.py | 29 ++++++++++++++++++++++++-- service/fit.py | 32 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/eos/effectHandlerHelpers.py b/eos/effectHandlerHelpers.py index 3bf576a4c..eca4d6437 100644 --- a/eos/effectHandlerHelpers.py +++ b/eos/effectHandlerHelpers.py @@ -159,6 +159,10 @@ class HandledModuleList(HandledList): dummy.position = index self[index] = dummy + def toModule(self, index, mod): + mod.position = index + self[index] = mod + def freeSlot(self, slot): for i in range(len(self) -1, -1, -1): mod = self[i] diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index f2f611134..84bb86543 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -4,6 +4,12 @@ from gui.itemStats import ItemStatsDialog import gui.mainFrame import service import wx +import gui.globalEvents as GE + +# TODO: +# Handle multiple selection better +# Icons? +# Submenu for officer? class MetaSwap(ContextMenu): def __init__(self): @@ -22,6 +28,8 @@ class MetaSwap(ContextMenu): return "Variations" def getSubMenu(self, context, selection, rootMenu, i, pitem): + self.moduleLookup = {} + def get_metalevel(x): return x.attributes["metaLevel"].value @@ -44,8 +52,25 @@ class MetaSwap(ContextMenu): m.Enable(id, False) id = wx.NewId() - name = item.name - m.AppendItem(wx.MenuItem(rootMenu, id, name)) + mitem = wx.MenuItem(rootMenu, id, item.name) + rootMenu.Bind(wx.EVT_MENU, self.handleModule, mitem) + self.moduleLookup[id] = item + m.AppendItem(mitem) return m + def handleModule(self, event): + item = self.moduleLookup.get(event.Id, None) + if item is None: + event.Skip() + return + + sFit = service.Fit.getInstance() + fitID = self.mainFrame.getActiveFit() + fit = sFit.getFit(fitID) + + pos = fit.modules.index(self.module) + sFit.changeModule(fitID, pos, item.ID) + + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + MetaSwap.register() diff --git a/service/fit.py b/service/fit.py index ee6294cd7..ce400bdf1 100644 --- a/service/fit.py +++ b/service/fit.py @@ -440,6 +440,38 @@ class Fit(object): eos.db.commit() return numSlots != len(fit.modules) + def changeModule(self, fitID, position, newItemID): + fit = eos.db.getFit(fitID) + if fit.modules[position].isEmpty: + return None + + # Dummy it out in case the next bit fails + fit.modules.toDummy(position) + + item = eos.db.getItem(newItemID, eager=("attributes", "group.category")) + try: + m = eos.types.Module(item) + except ValueError: + return False + + if m.fits(fit): + m.owner = fit + fit.modules.toModule(position, m) + 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) + # 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 + else: + return None + def moveCargoToModule(self, fitID, moduleIdx, cargoIdx, copyMod=False): """ Moves cargo to fitting window. Can either do a copy, move, or swap with current module