holy shit it works perfect
This commit is contained in:
@@ -8,9 +8,9 @@ local Slider = UIButton:subclass("Slider")
|
||||
function Slider:init()
|
||||
self.super:init()
|
||||
|
||||
self.valueRange = {2, 32}
|
||||
self.valueIncrement = 2
|
||||
self.defaultValue = 50
|
||||
self.valueRange = {-50, 50}
|
||||
self.valueIncrement = 5
|
||||
self.defaultValue = 0
|
||||
self.value = self.defaultValue
|
||||
self.smooth = true
|
||||
self.orientation = "vertical"
|
||||
@@ -18,20 +18,20 @@ function Slider:init()
|
||||
self.valueChange = Event:new()
|
||||
|
||||
self.valuePercent = 0
|
||||
|
||||
self.scrubDrawPosition = 0
|
||||
|
||||
self.scrubXSize = 20
|
||||
self.scrubColor = Color:new(0.5, 0.5, 0.5)
|
||||
|
||||
end
|
||||
|
||||
function Slider:doMouseActionCalculations()
|
||||
function Slider:doMouseMoveCalculations()
|
||||
|
||||
-- how much length the slider can actually work with
|
||||
local effectiveSize = self:getAbsoluteSize().x-self.scrubXSize
|
||||
|
||||
-- where the mouse is, inside of the slider
|
||||
local mousePosition = (love.mouse.getX() - self:getAbsolutePosition().x)
|
||||
local mousePosition = math.floor(love.mouse.getX() - self:getAbsolutePosition().x)
|
||||
|
||||
-- so that the scrub is put in the middle of the mouse
|
||||
local scrubPos = mousePosition-(self.scrubXSize/2)
|
||||
@@ -39,44 +39,39 @@ function Slider:doMouseActionCalculations()
|
||||
-- clamp the value so it never leaves the slider
|
||||
local positionClamped = JUtils.ClampNumber(scrubPos, 0, effectiveSize)
|
||||
|
||||
-- let's make a percentage value for where the slider should be placed
|
||||
local floatPos = (positionClamped / effectiveSize)
|
||||
local percent = (positionClamped/effectiveSize)
|
||||
|
||||
-- now let's calculate where to draw the scrub
|
||||
local drawPos = (positionClamped / self:getAbsoluteSize().x)
|
||||
|
||||
self.valuePercent = floatPos
|
||||
self.scrubDrawPosition = drawPos
|
||||
end
|
||||
|
||||
function Slider:lockValues()
|
||||
local range = self.valueRange[2]-self.valueRange[1]
|
||||
|
||||
local increments = range/self.valueIncrement
|
||||
local val = JUtils.NearestMultiple(percent*range, self.valueIncrement)--+self.valueRange[1]
|
||||
|
||||
print(increments)
|
||||
-- self.valuePercent = val/range
|
||||
|
||||
--self.valuePercent = math.floor(self.valuePercent*increments)/increments
|
||||
--self.scrubDrawPosition = math.floor(self.scrubDrawPosition*increments)/increments
|
||||
val = val + self.valueRange[1]
|
||||
|
||||
self.valuePercent = (math.floor((self.valuePercent*range)/self.valueIncrement)*self.valueIncrement)/range
|
||||
self.scrubDrawPosition = (math.floor((self.scrubDrawPosition*range)/self.valueIncrement)*self.valueIncrement)/range
|
||||
self.value = val
|
||||
|
||||
local thing = (self.valuePercent*range)+self.valueRange[1]
|
||||
|
||||
|
||||
print(thing)
|
||||
end
|
||||
|
||||
function Slider:updateScrubPos()
|
||||
local range = self.valueRange[2]-self.valueRange[1]
|
||||
self.valuePercent = (self.value-self.valueRange[1])/range
|
||||
self.scrubDrawPosition = (self.valuePercent*(self:getAbsoluteSize().x-self.scrubXSize))/self:getAbsoluteSize().x
|
||||
end
|
||||
|
||||
function Slider:update(delta)
|
||||
self.super:update(delta)
|
||||
|
||||
if self.parent then
|
||||
self:updateScrubPos()
|
||||
end
|
||||
|
||||
|
||||
if self.mouseDown then
|
||||
self:doMouseActionCalculations()
|
||||
self:doMouseMoveCalculations()
|
||||
|
||||
self:lockValues()
|
||||
|
||||
--print(self.scrubDrawPosition, self.valuePercent)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -88,7 +83,7 @@ function Slider:render()
|
||||
-- render the thing object idk
|
||||
|
||||
love.graphics.setColor(self.scrubColor:out())
|
||||
love.graphics.rectangle("fill", self:getAbsolutePosition().x+(self.getAbsoluteSize().x*self.scrubDrawPosition), self.getAbsolutePosition().y, self.scrubXSize+1, self.getAbsoluteSize().y)
|
||||
love.graphics.rectangle("fill", self:getAbsolutePosition().x+(self.getAbsoluteSize().x*self.scrubDrawPosition), self.getAbsolutePosition().y, self.scrubXSize, self.getAbsoluteSize().y)
|
||||
|
||||
self:renderChildren()
|
||||
end
|
||||
|
@@ -1,6 +1,11 @@
|
||||
local function round(number, decimalPlaces)
|
||||
decimalPlaces = decimalPlaces or 0
|
||||
local placer = 10^(-decimalPlaces)
|
||||
return (math.floor(number)/placer)*placer
|
||||
return (math.floor(number/placer + .5))*placer
|
||||
end
|
||||
|
||||
local function multiple(number, m)
|
||||
return round(number/m)*m
|
||||
end
|
||||
|
||||
local function clamp(number, min, max)
|
||||
@@ -11,6 +16,7 @@ end
|
||||
|
||||
|
||||
return {
|
||||
NearestMultiple = multiple,
|
||||
RoundNumber = round,
|
||||
ClampNumber = clamp,
|
||||
}
|
Reference in New Issue
Block a user