Make modification of mutated stats undoable

This commit is contained in:
DarkPhoenix
2019-05-02 00:18:17 +03:00
parent bbc8fd0f97
commit b3ef55cb7f
8 changed files with 151 additions and 15 deletions

View File

@@ -4,6 +4,7 @@ import random
import wx
from logbook import Logger
import gui.fitCommands as cmd
import gui.globalEvents as GE
import gui.mainFrame
from gui.bitmap_loader import BitmapLoader
@@ -11,6 +12,7 @@ from service.fit import Fit
from .attributeSlider import AttributeSlider, EVT_VALUE_CHANGED
from .itemAttributes import ItemParams
pyfalog = Logger(__name__)
@@ -47,18 +49,26 @@ class ItemMutatorPanel(wx.Panel):
self.randomBtn = wx.Button(self, wx.ID_ANY, "Random stats", wx.DefaultPosition, wx.DefaultSize, 0)
footerSizer.Add(self.randomBtn, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 5)
self.randomBtn.Bind(wx.EVT_BUTTON, self.mutaList.randomMutatedValues)
self.revertBtn = wx.Button(self, wx.ID_ANY, "Revert changes", wx.DefaultPosition, wx.DefaultSize, 0)
footerSizer.Add(self.revertBtn, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 5)
self.revertBtn.Bind(wx.EVT_BUTTON, self.mutaList.revertChanges)
mainSizer.Add(footerSizer, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(mainSizer)
self.Layout()
def submitMutationChanges(self):
self.mutaList.submitMutationChanges()
class ItemMutatorList(wx.ScrolledWindow):
def __init__(self, parent, mod):
wx.ScrolledWindow.__init__(self, parent)
self.SetScrollRate(0, 15)
self.activeFit = gui.mainFrame.MainFrame.getInstance().getActiveFit()
self.carryingFitID = gui.mainFrame.MainFrame.getInstance().getActiveFit()
self.initialMutations = {}
self.mod = mod
self.timer = None
goodColor = wx.Colour(96, 191, 0)
@@ -79,6 +89,8 @@ class ItemMutatorList(wx.ScrolledWindow):
sizer.Add(wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL), 0, wx.ALL | wx.EXPAND, 5)
first = False
self.initialMutations[m.attrID] = m.value
highIsGood = higOverrides.get((mod.item.group.name, m.attribute.name), m.highIsGood)
# Format: [raw value, modifier applied to base raw value, display value]
range1 = (m.minValue, m.attribute.unit.SimplifyValue(m.minValue))
@@ -157,7 +169,7 @@ class ItemMutatorList(wx.ScrolledWindow):
value = m.attribute.unit.ComplicateValue(value)
sFit = Fit.getInstance()
sFit.changeMutatedValue(m, value)
sFit.changeMutatedValuePrelim(m, value)
if self.timer:
self.timer.Stop()
self.timer = None
@@ -170,36 +182,54 @@ class ItemMutatorList(wx.ScrolledWindow):
def resetMutatedValues(self, evt):
sFit = Fit.getInstance()
for slider, m in self.event_mapping.items():
value = sFit.changeMutatedValue(m, m.baseValue)
value = sFit.changeMutatedValuePrelim(m, m.baseValue)
value = m.attribute.unit.SimplifyValue(value)
slider.SetValue(value)
evt.Skip()
def randomMutatedValues(self, evt):
sFit = Fit.getInstance()
for slider, m in self.event_mapping.items():
value = random.uniform(m.minValue, m.maxValue)
value = sFit.changeMutatedValue(m, value)
value = sFit.changeMutatedValuePrelim(m, value)
value = m.attribute.unit.SimplifyValue(value)
slider.SetValue(value)
evt.Skip()
def revertChanges(self, evt):
sFit = Fit.getInstance()
for slider, m in self.event_mapping.items():
if m.attrID in self.initialMutations:
value = sFit.changeMutatedValuePrelim(m, self.initialMutations[m.attrID])
value = m.attribute.unit.SimplifyValue(value)
slider.SetValue(value)
evt.Skip()
def submitMutationChanges(self):
fit = Fit.getInstance().getFit(self.carryingFitID)
if self.mod in fit.modules:
currentMutation = {}
for m in self.event_mapping.values():
currentMutation[m.attrID] = m.value
mainFrame = gui.mainFrame.MainFrame.getInstance()
mainFrame.getCommandForFit(self.carryingFitID).Submit(cmd.GuiChangeLocalModuleMutationCommand(
fitID=self.carryingFitID,
position=fit.modules.index(self.mod),
mutation=currentMutation,
oldMutation=self.initialMutations))
def callLater(self):
self.timer = None
sFit = Fit.getInstance()
# recalc the fit that this module affects. This is not necessarily the currently active fit
sFit.refreshFit(self.activeFit)
sFit.refreshFit(self.carryingFitID)
mainFrame = gui.mainFrame.MainFrame.getInstance()
activeFit = mainFrame.getActiveFit()
if activeFit != self.activeFit:
if activeFit != self.carryingFitID:
# if we're no longer on the fit this module is affecting, simulate a "switch fit" so that the active fit
# can be recalculated (if needed)
sFit.switchFit(activeFit)

View File

@@ -37,6 +37,7 @@ from .gui.localFighter.toggleStates import GuiToggleLocalFighterStatesCommand
from .gui.localModule.add import GuiAddLocalModuleCommand
from .gui.localModule.changeCharges import GuiChangeLocalModuleChargesCommand
from .gui.localModule.changeMetas import GuiChangeLocalModuleMetasCommand
from .gui.localModule.changeMutation import GuiChangeLocalModuleMutationCommand
from .gui.localModule.changeSpool import GuiChangeLocalModuleSpoolCommand
from .gui.localModule.changeStates import GuiChangeLocalModuleStatesCommand
from .gui.localModule.clone import GuiCloneLocalModuleCommand

View File

@@ -0,0 +1,56 @@
import wx
from logbook import Logger
import eos.db
from service.fit import Fit
pyfalog = Logger(__name__)
class CalcChangeLocalModuleMutationCommand(wx.Command):
def __init__(self, fitID, position, mutation, oldMutation=None, commit=True):
wx.Command.__init__(self, True, 'Change Local Module Mutation')
self.fitID = fitID
self.position = position
self.mutation = mutation
self.savedMutation = oldMutation
self.commit = commit
def Do(self):
pyfalog.debug('Doing changing of local module mutation at position {} to {} for fit ID {}'.format(
self.position, self.mutation, self.fitID))
sFit = Fit.getInstance()
fit = sFit.getFit(self.fitID)
mod = fit.modules[self.position]
if not mod.isMutated:
return False
if self.savedMutation is None:
self.savedMutation = {}
for mutator in mod.mutators.values():
self.savedMutation[mutator.attrID] = mutator.value
if self.mutation == self.savedMutation:
return False
for mutator in mod.mutators.values():
if mutator.attrID not in self.mutation:
continue
if mutator.value != self.mutation[mutator.attrID]:
mutator.value = self.mutation[mutator.attrID]
if self.commit:
eos.db.commit()
return True
def Undo(self):
pyfalog.debug('Undoing changing of local module mutation at position {} to {} for fit ID {}'.format(
self.position, self.mutation, self.fitID))
cmd = CalcChangeLocalModuleMutationCommand(
fitID=self.fitID,
position=self.position,
mutation=self.savedMutation,
commit=self.commit)
return cmd.Do()

View File

@@ -15,7 +15,7 @@ pyfalog = Logger(__name__)
class CalcCloneLocalModuleCommand(wx.Command):
def __init__(self, fitID, srcPosition, dstPosition):
wx.Command.__init__(self, True, 'Clone Module')
wx.Command.__init__(self, True, 'Clone Local Module')
self.fitID = fitID
self.srcPosition = srcPosition
self.dstPosition = dstPosition

View File

@@ -0,0 +1,39 @@
import wx
import gui.mainFrame
from gui import globalEvents as GE
from gui.fitCommands.calc.module.localChangeMutation import CalcChangeLocalModuleMutationCommand
from gui.fitCommands.helpers import InternalCommandHistory
from service.fit import Fit
class GuiChangeLocalModuleMutationCommand(wx.Command):
def __init__(self, fitID, position, mutation, oldMutation=None):
wx.Command.__init__(self, True, 'Change Local Module Mutation')
self.internalHistory = InternalCommandHistory()
self.fitID = fitID
self.position = position
self.mutation = mutation
self.oldMutation = oldMutation
def Do(self):
cmd = CalcChangeLocalModuleMutationCommand(
fitID=self.fitID,
position=self.position,
mutation=self.mutation,
oldMutation=self.oldMutation)
success = self.internalHistory.submit(cmd)
sFit = Fit.getInstance()
sFit.recalc(self.fitID)
sFit.fill(self.fitID)
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))
return success
def Undo(self):
success = self.internalHistory.undoAll()
sFit = Fit.getInstance()
sFit.recalc(self.fitID)
sFit.fill(self.fitID)
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))
return success

View File

@@ -147,6 +147,7 @@ class ItemStatsDialog(wx.Dialog):
def closeEvent(self, event):
self.closeWindow()
self.container.onParentClose()
event.Skip()
def closeWindow(self):
@@ -156,6 +157,7 @@ class ItemStatsDialog(wx.Dialog):
self.Destroy()
class ItemStatsContainer(wx.Panel):
def __init__(self, parent, stuff, item, context=None):
wx.Panel.__init__(self, parent)
sMkt = Market.getInstance()
@@ -214,3 +216,8 @@ class ItemStatsContainer(wx.Panel):
tab, _ = self.nbContainer.HitTest(event.Position)
if tab != -1:
self.nbContainer.SetSelection(tab)
def onParentClose(self):
mutaPanel = getattr(self, 'mutator', None)
if mutaPanel is not None:
mutaPanel.submitMutationChanges()

View File

@@ -234,6 +234,9 @@ class MainFrame(wx.Frame):
def command(self) -> wx.CommandProcessor:
return Fit.getCommandProcessor(self.getActiveFit())
def getCommandForFit(self, fitID) -> wx.CommandProcessor:
return Fit.getCommandProcessor(fitID)
def ShowUpdateBox(self, release, version):
dlg = UpdateDialog(self, release, version)
dlg.ShowModal()

View File

@@ -339,11 +339,11 @@ class Fit:
fit.notes))
return fits
def changeMutatedValue(self, mutator, value):
def changeMutatedValuePrelim(self, mutator, value):
pyfalog.debug("Changing mutated value for {} / {}: {} => {}".format(mutator.module, mutator.module.mutaplasmid, mutator.value, value))
mutator.value = value
eos.db.commit()
if mutator.value != value:
mutator.value = value
eos.db.flush()
return mutator.value
def changeChar(self, fitID, charID):