Rework slider to use values rather than modifiers

This commit is contained in:
DarkPhoenix
2018-11-14 17:15:04 +03:00
parent 0ab6d6e845
commit 90698dde90
4 changed files with 31 additions and 42 deletions

View File

@@ -567,6 +567,10 @@ class Unit(EqBase):
""" This is a mapping of various tweaks that we have to do between the internal representation of an attribute
value and the display (for example, 'Millisecond' units have the display name of 's', so we have to convert value
from ms to s) """
# Each entry contains:
# Function to convert value to display value
# Function to convert display value to value
# Function which controls attribute unit name used with attribute
return {
"Inverse Absolute Percent": (
lambda v: (1 - v) * 100,
@@ -653,6 +657,5 @@ class Unit(EqBase):
return value
class Traits(EqBase):
pass

View File

@@ -167,8 +167,6 @@ class AttributeGauge(wx.Window):
def SetValue(self, value, animate=True):
""" Sets the current position of the gauge. """
print("=" * 20, self._percentage)
if self._value == value:
return

View File

@@ -46,17 +46,17 @@ class AttributeSlider(wx.Panel):
# Slider which abstracts users values from internal values (because the built in slider does not deal with floats
# and the like), based on http://wxpython-users.wxwidgets.narkive.com/ekgBzA7u/anyone-ever-thought-of-a-floating-point-slider
def __init__(self, parent, baseValue, minMod, maxMod, inverse=False, id=-1):
def __init__(self, parent, baseValue, minValue, maxValue, inverse=False, id=-1):
wx.Panel.__init__(self, parent, id=id)
self.parent = parent
self.inverse = inverse
self.base_value = baseValue
self.UserMinValue = minMod
self.UserMaxValue = maxMod
self.UserMinValue = minValue
self.UserMaxValue = maxValue
self.inverse = inverse
# The internal slider basically represents the percentage towards the end of the range. It has to be normalized
# in this way, otherwise when we start off with a base, if the range is skewed to one side, the base value won't
@@ -65,12 +65,12 @@ class AttributeSlider(wx.Panel):
# Additionally, since we want the slider to be accurate to 3 decimal places, we need to blow out the two ends here
# (if we have a slider that needs to land on 66.66% towards the right, it will actually be converted to 66%. Se we need it to support 6,666)
#
self.SliderMinValue = -100
self.SliderMaxValue = 100
self.SliderValue = 0
range = [(self.UserMinValue * self.base_value), (self.UserMaxValue * self.base_value)]
range = [self.UserMinValue, self.UserMaxValue]
self.ctrl = wx.SpinCtrlDouble(self, min=min(range), max=max(range))
self.ctrl.SetDigits(3)
@@ -93,18 +93,11 @@ class AttributeSlider(wx.Panel):
def SetValue(self, value, post_event=True):
# todo: check this against values that might be 2.5x and whatnot
mod = value / self.base_value
self.ctrl.SetValue(value)
slider_percentage = 0
if mod < 1:
modEnd = self.UserMinValue
slider_percentage = (1 - mod) / (1 - modEnd) * -100
elif mod > 1:
modEnd = self.UserMaxValue
slider_percentage = ((mod - 1) / (modEnd - 1)) * 100
# print(slider_percentage)
if self.inverse:
slider_percentage *= -1
invert_factor = -1 if self.inverse else 1
if value >= self.base_value:
slider_percentage = (value - self.base_value) / (self.UserMaxValue - self.base_value) * 100 * invert_factor
else:
slider_percentage = (value - self.base_value) / (self.base_value - self.UserMinValue) * 100 * invert_factor
self.slider.SetValue(slider_percentage)
if post_event:
wx.PostEvent(self, ValueChanged(self, None, value, None, slider_percentage))

View File

@@ -30,17 +30,9 @@ class ItemMutator(wx.Panel):
self.event_mapping = {}
for m in sorted(stuff.mutators.values(), key=lambda x: x.attribute.displayName):
baseValueFormated = m.attribute.unit.TranslateValue(m.baseValue)[0]
valueFormated = m.attribute.unit.TranslateValue(m.value)[0]
slider = AttributeSlider(self, baseValueFormated, m.minMod, m.maxMod, not m.highIsGood)
slider.SetValue(valueFormated, False)
slider.Bind(EVT_VALUE_CHANGED, self.changeMutatedValue)
self.event_mapping[slider] = m
headingSizer = wx.BoxSizer(wx.HORIZONTAL)
# create array for the two ranges
min_t = [round(m.minValue, 3), m.minMod, None]
max_t = [round(m.maxValue, 3), m.maxMod, None]
min_t = [m.minValue, m.minMod, None]
max_t = [m.maxValue, m.maxMod, None]
# Then we need to determine if it's better than original, which will be the color
min_t[2] = min_t[1] < 1 if not m.highIsGood else 1 < min_t[1]
@@ -56,14 +48,8 @@ class ItemMutator(wx.Panel):
worse_range = max_t
else:
worse_range = min_t
#
# print("{}: \nHigh is good: {}".format(m.attribute.displayName, m.attribute.highIsGood))
# print("Value {}".format(m.baseValue))
#
# print(min_t)
# print(max_t)
# print(better_range)
# print(worse_range)
headingSizer = wx.BoxSizer(wx.HORIZONTAL)
font = parent.GetFont()
font.SetWeight(wx.BOLD)
@@ -75,10 +61,10 @@ class ItemMutator(wx.Panel):
headingSizer.Add(displayName, 3, wx.ALL | wx.EXPAND, 0)
range_low = wx.StaticText(self, wx.ID_ANY, ItemParams.FormatValue(*m.attribute.unit.TranslateValue(worse_range[0])))
range_low = wx.StaticText(self, wx.ID_ANY, ItemParams.FormatValue(*m.attribute.unit.TranslateValue(round(worse_range[0], 3))))
range_low.SetForegroundColour(self.goodColor if worse_range[2] else self.badColor)
range_high = wx.StaticText(self, wx.ID_ANY, ItemParams.FormatValue(*m.attribute.unit.TranslateValue(better_range[0])))
range_high = wx.StaticText(self, wx.ID_ANY, ItemParams.FormatValue(*m.attribute.unit.TranslateValue(round(better_range[0], 3))))
range_high.SetForegroundColour(self.goodColor if better_range[2] else self.badColor)
headingSizer.Add(range_low, 0, wx.ALL | wx.EXPAND, 0)
@@ -87,7 +73,16 @@ class ItemMutator(wx.Panel):
mainSizer.Add(headingSizer, 0, wx.ALL | wx.EXPAND, 5)
slider = AttributeSlider(parent=self,
baseValue=m.attribute.unit.TranslateValue(m.baseValue)[0],
minValue=m.attribute.unit.TranslateValue(min_t[0])[0],
maxValue=m.attribute.unit.TranslateValue(max_t[0])[0],
inverse=better_range is min_t)
slider.SetValue(m.attribute.unit.TranslateValue(m.value)[0], False)
slider.Bind(EVT_VALUE_CHANGED, self.changeMutatedValue)
self.event_mapping[slider] = m
mainSizer.Add(slider, 0, wx.RIGHT | wx.LEFT | wx.EXPAND, 10)
mainSizer.Add(wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL), 0, wx.ALL | wx.EXPAND, 5)
mainSizer.AddStretchSpacer()