Fix the state change command to not store the modules themselves, but the positions

This commit is contained in:
blitzmann
2018-08-03 01:42:06 -04:00
parent c84c79c917
commit aa282990a5
2 changed files with 41 additions and 24 deletions

View File

@@ -268,7 +268,9 @@ class FittingView(d.Display):
sel = []
row = self.GetFirstSelected()
while row != -1:
sel.append(self.mods[self.GetItemData(row)])
mod = self.mods[self.GetItemData(row)]
if mod and not isinstance(mod, Rack):
sel.append(mod)
row = self.GetNextSelected(row)
return sel
@@ -629,7 +631,7 @@ class FittingView(d.Display):
ctrl = event.cmdDown or event.middleIsDown
click = "ctrl" if ctrl is True else "right" if event.GetButton() == 3 else "left"
self.mainFrame.command.Submit(cmd.GuiModuleStateChangeCommand(fitID, self.mods[self.GetItemData(row)], mods, click))
self.mainFrame.command.Submit(cmd.GuiModuleStateChangeCommand(fitID, self.mods[self.GetItemData(row)].modPosition, [mod.modPosition for mod in mods], click))
# update state tooltip
tooltip = self.activeColumns[col].getToolTip(self.mods[self.GetItemData(row)])

View File

@@ -9,40 +9,53 @@ pyfalog = Logger(__name__)
import eos.db
class FitChangeStatesCommand(wx.Command):
def __init__(self, fitID, baseMod, modules, click):
"""
Fitting command that trys to change the state of modules in [positions]. We use the base module to determine the
state that we will try to apply for all modules.
"""
def __init__(self, fitID, baseModPos, positions, click):
# todo: instead of modules, needs to be positions. Dead objects are a thing
wx.Command.__init__(self, True, "Module State Change")
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
self.sFit = Fit.getInstance()
self.fitID = fitID
self.baseMod = baseMod
self.modules = modules
self.baseModPos = baseModPos
self.positions = positions
self.click = click
self.changed = None
self.old_states = {}
for mod in modules:
# we don't use the actual module as the key, because it may have been deleted in subsequent calls (even if
# we undo a deletion, wouldn't be the same obj). So, we store the position
self.old_states[mod.modPosition] = mod.state
def Do(self):
pyfalog.debug("Toggle module state for fit ID: {0}", self.fitID)
changed = False
proposedState = Module.getProposedState(self.baseMod, self.click)
fit = eos.db.getFit(self.fitID)
if proposedState != self.baseMod.state:
changed = True
self.baseMod.state = proposedState
for mod in self.modules:
if mod != self.baseMod:
p = Module.getProposedState(mod, self.click, proposedState)
mod.state = p
if p != mod.state:
changed = True
baseMod = fit.modules[self.baseModPos]
# make sure positions only include non-empty positions
self.positions = [x for x in self.positions if not fit.modules[x].isEmpty]
for x in self.positions:
self.old_states[x] = fit.modules[x].state
proposedState = Module.getProposedState(baseMod, self.click)
pyfalog.debug("Attempting to change modules to {}", proposedState)
if proposedState != baseMod.state:
pyfalog.debug("Toggle {} state: {} for fit ID: {}", baseMod, proposedState, self.fitID)
self.changed = True
baseMod.state = proposedState
for i in [x for x in self.positions if x != self.baseModPos]: # dont consider base module position
mod = fit.modules[i]
p = Module.getProposedState(mod, self.click, proposedState)
mod.state = p
if p != mod.state:
pyfalog.debug("Toggle {} state: {} for fit ID: {}", mod, p, self.fitID)
self.changed = True
# if we haven't change the state (eg, overheat -> overheat), simply fail the command
if changed:
self.changed = changed
if self.changed:
eos.db.commit()
# As some items may affect state-limiting attributes of the ship, calculate new attributes first
# self.recalc(fit)
@@ -55,5 +68,7 @@ class FitChangeStatesCommand(wx.Command):
# todo: some sanity checking to make sure that we are applying state back to the same modules?
fit = self.sFit.getFit(self.fitID)
for k, v in self.old_states.items():
fit.modules[k].state = v
mod = fit.modules[k]
pyfalog.debug("Reverting {} to state {} for fit ID", mod, v, self.fitID)
mod.state = v
return True