Allow to use floats in damage pattern editor

This commit is contained in:
DarkPhoenix
2019-08-12 16:52:08 +03:00
parent 931d8d355f
commit 4f784e2eea
3 changed files with 54 additions and 27 deletions

View File

@@ -0,0 +1,30 @@
"""
Migration 33
Allow use of floats in damage pattern values
"""
tmpTable = """
CREATE TABLE "damagePatternsTemp" (
"ID" INTEGER NOT NULL,
"name" VARCHAR,
"emAmount" FLOAT,
"thermalAmount" FLOAT,
"kineticAmount" FLOAT,
"explosiveAmount" FLOAT,
"ownerID" INTEGER,
"created" DATETIME,
"modified" DATETIME,
PRIMARY KEY ("ID"),
FOREIGN KEY("ownerID") REFERENCES users ("ID")
)
"""
def upgrade(saveddata_engine):
saveddata_engine.execute(tmpTable)
saveddata_engine.execute(
'INSERT INTO damagePatternsTemp (ID, name, emAmount, thermalAmount, kineticAmount, explosiveAmount, ownerID, created, modified) '
'SELECT ID, name, emAmount, thermalAmount, kineticAmount, explosiveAmount, ownerID, created, modified FROM damagePatterns')
saveddata_engine.execute('DROP TABLE damagePatterns')
saveddata_engine.execute('ALTER TABLE damagePatternsTemp RENAME TO damagePatterns')

View File

@@ -17,7 +17,7 @@
# along with eos. If not, see <http://www.gnu.org/licenses/>. # along with eos. If not, see <http://www.gnu.org/licenses/>.
# =============================================================================== # ===============================================================================
from sqlalchemy import Table, Column, Integer, ForeignKey, String, DateTime from sqlalchemy import Table, Column, Integer, Float, ForeignKey, String, DateTime
from sqlalchemy.orm import mapper from sqlalchemy.orm import mapper
import datetime import datetime
@@ -27,10 +27,10 @@ from eos.saveddata.damagePattern import DamagePattern
damagePatterns_table = Table("damagePatterns", saveddata_meta, damagePatterns_table = Table("damagePatterns", saveddata_meta,
Column("ID", Integer, primary_key=True), Column("ID", Integer, primary_key=True),
Column("name", String), Column("name", String),
Column("emAmount", Integer), Column("emAmount", Float),
Column("thermalAmount", Integer), Column("thermalAmount", Float),
Column("kineticAmount", Integer), Column("kineticAmount", Float),
Column("explosiveAmount", Integer), Column("explosiveAmount", Float),
Column("ownerID", ForeignKey("users.ID"), nullable=True), Column("ownerID", ForeignKey("users.ID"), nullable=True),
Column("created", DateTime, nullable=True, default=datetime.datetime.now), Column("created", DateTime, nullable=True, default=datetime.datetime.now),
Column("modified", DateTime, nullable=True, onupdate=datetime.datetime.now) Column("modified", DateTime, nullable=True, onupdate=datetime.datetime.now)

View File

@@ -20,25 +20,24 @@
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
import wx import wx
from logbook import Logger from logbook import Logger
# noinspection PyPackageRequirements
from wx.lib.intctrl import IntCtrl
from gui.auxFrame import AuxiliaryFrame from gui.auxFrame import AuxiliaryFrame
from gui.bitmap_loader import BitmapLoader from gui.bitmap_loader import BitmapLoader
from gui.builtinViews.entityEditor import BaseValidator, EntityEditor from gui.builtinViews.entityEditor import BaseValidator, EntityEditor
from gui.utils.clipboard import fromClipboard, toClipboard from gui.utils.clipboard import fromClipboard, toClipboard
from gui.utils.inputs import FloatBox
from service.damagePattern import DamagePattern, ImportError from service.damagePattern import DamagePattern, ImportError
pyfalog = Logger(__name__) pyfalog = Logger(__name__)
class DmgPatternTextValidor(BaseValidator): class DmgPatternNameValidator(BaseValidator):
def __init__(self): def __init__(self):
BaseValidator.__init__(self) BaseValidator.__init__(self)
def Clone(self): def Clone(self):
return DmgPatternTextValidor() return DmgPatternNameValidator()
def Validate(self, win): def Validate(self, win):
entityEditor = win.parent entityEditor = win.parent
@@ -62,7 +61,7 @@ class DmgPatternTextValidor(BaseValidator):
class DmgPatternEntityEditor(EntityEditor): class DmgPatternEntityEditor(EntityEditor):
def __init__(self, parent): def __init__(self, parent):
EntityEditor.__init__(self, parent, "Damage Profile") EntityEditor.__init__(self, parent, "Damage Profile")
self.SetEditorValidator(DmgPatternTextValidor) self.SetEditorValidator(DmgPatternNameValidator)
def getEntitiesFromContext(self): def getEntitiesFromContext(self):
sDP = DamagePattern.getInstance() sDP = DamagePattern.getInstance()
@@ -134,18 +133,16 @@ class DmgPatternEditor(AuxiliaryFrame):
border = 5 border = 5
# set text edit # set text edit
setattr(self, "%sEdit" % type_, IntCtrl(self, wx.ID_ANY, 0, wx.DefaultPosition, defSize)) editBox = FloatBox(parent=self, id=wx.ID_ANY, value=0, pos=wx.DefaultPosition, size=defSize)
setattr(self, "%sPerc" % type_, wx.StaticText(self, wx.ID_ANY, "0%")) percLabel = wx.StaticText(self, wx.ID_ANY, "0%")
editObj = getattr(self, "%sEdit" % type_) setattr(self, "%sEdit" % type_, editBox)
setattr(self, "%sPerc" % type_, percLabel)
dmgeditSizer.Add(bmp, 0, style, border) dmgeditSizer.Add(bmp, 0, style, border)
dmgeditSizer.Add(editObj, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_VERTICAL, 5) dmgeditSizer.Add(editBox, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_VERTICAL, 5)
dmgeditSizer.Add(getattr(self, "%sPerc" % type_), 0, wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 5) dmgeditSizer.Add(percLabel, 0, wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 5)
editObj.Bind(wx.EVT_TEXT, self.ValuesUpdated) editBox.Bind(wx.EVT_TEXT, self.OnFieldChanged)
editObj.SetLimited(True)
editObj.SetMin(0)
editObj.SetMax(2000000)
contentSizer.Add(dmgeditSizer, 1, wx.EXPAND | wx.ALL, 5) contentSizer.Add(dmgeditSizer, 1, wx.EXPAND | wx.ALL, 5)
self.slfooter = wx.StaticLine(self) self.slfooter = wx.StaticLine(self)
@@ -196,17 +193,17 @@ class DmgPatternEditor(AuxiliaryFrame):
self.patternChanged() self.patternChanged()
def ValuesUpdated(self, event=None): def OnFieldChanged(self, event=None):
if self.block: if self.block:
return return
p = self.entityEditor.getActiveEntity() p = self.entityEditor.getActiveEntity()
total = sum([getattr(self, "%sEdit" % attr).GetValue() for attr in self.DAMAGE_TYPES]) total = sum([getattr(self, "%sEdit" % attr).GetValueFloat() for attr in self.DAMAGE_TYPES])
for type_ in self.DAMAGE_TYPES: for type_ in self.DAMAGE_TYPES:
editObj = getattr(self, "%sEdit" % type_) editBox = getattr(self, "%sEdit" % type_)
percObj = getattr(self, "%sPerc" % type_) percLabel = getattr(self, "%sPerc" % type_)
setattr(p, "%sAmount" % type_, editObj.GetValue()) setattr(p, "%sAmount" % type_, editBox.GetValueFloat())
percObj.SetLabel("%.1f%%" % (float(editObj.GetValue()) * 100 / total if total > 0 else 0)) percLabel.SetLabel("%.1f%%" % (float(editBox.GetValueFloat()) * 100 / total if total > 0 else 0))
self.totSizer.Layout() self.totSizer.Layout()
@@ -245,10 +242,10 @@ class DmgPatternEditor(AuxiliaryFrame):
for field in self.DAMAGE_TYPES: for field in self.DAMAGE_TYPES:
edit = getattr(self, "%sEdit" % field) edit = getattr(self, "%sEdit" % field)
amount = int(round(getattr(p, "%sAmount" % field))) amount = int(round(getattr(p, "%sAmount" % field)))
edit.SetValue(amount) edit.ChangeValueFloat(amount)
self.block = False self.block = False
self.ValuesUpdated() self.OnFieldChanged()
def __del__(self): def __del__(self):
pass pass