Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit b3395440e6073f2aabd725bb1f8670dc15baac4e @xconstruct committed Jul 27, 2009
Showing with 288 additions and 0 deletions.
  1. +179 −0 LibFx-1.1.lua
  2. +11 −0 LibFx-1.1.toc
  3. +30 −0 LibStub.lua
  4. +68 −0 README.mkd
@@ -0,0 +1,179 @@
+--[[
+Name: LibFx-1.1
+Author: Cargor (xconstruct@gmail.com)
+Dependencies: LibStub
+]]
+
+local MAJOR, MINOR = "LibFx-1.1", 1
+local LibFx = LibStub:NewLibrary(MAJOR, MINOR)
+
+if not LibFx then return end
+
+local ramps, anims, running = {}, {}, {}
+local numRunning = 0
+
+
+--[[*****************************
+ LibFx:RegisterAnimation(name, func, startFunc)
+ Registers a new animation for use within LibFx
+ - name = Name of animation
+ - get = Function for getting the initial value
+ - set = Function for setting the value
+*******************************]]
+function LibFx.RegisterAnimation(name, get, set)
+ if(not anims[name]) then
+ anims[name] = {Get = get, Set = set}
+ return anims[name]
+ end
+end
+
+function LibFx.GetAnimationByName(name)
+ return anims[name]
+end
+
+function LibFx.AnimationsIterator()
+ return pairs(anims)
+end
+
+--[[*****************************
+ LibFx:RegisterRamp(name, func)
+ Registers a new ramp for use within LibFx
+ - name = Name of ramp
+ - func = Function which returns the value to use
+*******************************]]
+function LibFx.RegisterRamp(name, func)
+ if(not ramps[name]) then ramps[name] = func end
+end
+
+function LibFx.GetRampByName(name)
+ return ramps[name]
+end
+
+function LibFx.RampIterator()
+ return pairs(ramps)
+end
+
+--[[*****************************
+ LibFx.New(fx)
+ Creates a new fx-object
+ - fx = table holding options
+*******************************]]
+local mt = {
+__index = LibFx,
+__call = function(self, ...) self:Start() end,
+}
+function LibFx.New(fx)
+ assert(fx, MAJOR..": No fx-table specified")
+ fx = setmetatable(fx, mt)
+ if(type(fx.anim) == "string") then fx.anim = anims[fx.anim] end
+ assert(fx.anim, MAJOR..": Animation not specified")
+ if(type(fx.ramp) == "string") then fx.ramp = ramps[fx.ramp] end
+ if(not fx.ramp) then fx.ramp = ramps["Linear"] end
+ assert(fx.ramp, MAJOR..": Ramp not specified")
+ if(not fx.frame) then fx.frame = frame end
+ assert(fx.frame, MAJOR..": Frame not specified")
+ if(not fx.duration) then fx.duration = 1 end
+ return fx
+end
+
+--[[*****************************
+ LibFx:Start()
+ Starts an existing Fx
+*******************************]]
+function LibFx.Start(fx)
+ if(not fx or running[fx]) then return end
+ fx.anim.Get(fx)
+ fx.runTime = 0
+ if(numRunning == 0) then LibFx.Updater:Show() end
+ running[fx] = true
+ numRunning = numRunning + 1
+ if(fx.onStart) then fx.onStart(fx.frame, fx) end
+end
+
+--[[*****************************
+ LibFx:Stop()
+ Ends an existing Fx
+*******************************]]
+function LibFx.Stop(fx)
+ if(not fx or not running[fx]) then return end
+
+ numRunning = numRunning - 1
+ running[fx] = nil
+ if(fx.onComplete) then fx.onComplete(fx.frame, fx) end
+ if(numRunning == 0) then LibFx.Updater:Hide() end
+end
+
+--[[*****************************
+ LibFx:IsRunning()
+ Returns wether the fx is currently running
+*******************************]]
+function LibFx.IsRunning(fx)
+ return running[fx]
+end
+
+--[[*****************************
+ Private functions
+*******************************]]
+
+local updateFrame = CreateFrame"Frame"
+function updateFrame:Update(elapsed)
+ for fx, _ in pairs(running) do
+ fx.runTime = fx.runTime + elapsed
+ local progress = min(fx.runTime/fx.duration, 1)
+ fx.progress = fx.ramp and fx.ramp(progress) or progress
+ fx.anim.Set(fx)
+ if(fx.runTime > fx.duration) then
+ fx:Stop()
+ end
+ end
+end
+updateFrame:SetScript("OnUpdate", updateFrame.Update)
+updateFrame:Hide()
+LibFx.Updater = updateFrame
+
+
+
+--[[*****************************
+ Default functions
+*******************************]]
+LibFx.RegisterRamp("Smooth", function(percent) return 1/(1+2.7^(-percent*12+6)) end)
+
+LibFx.RegisterAnimation("Alpha", function(fx)
+ fx.start = fx.frame:GetAlpha()
+ fx.diff = fx.finish - fx.start
+end, function(fx)
+ fx.frame:SetAlpha(fx.start + fx.diff * fx.progress)
+end)
+
+LibFx.RegisterAnimation("Scale", function(fx)
+ fx.start = fx.frame:GetScale()
+ fx.diff = fx.finish - fx.start
+end, function(fx)
+ fx.frame:SetScale(fx.start + fx.diff * fx.progress)
+end)
+
+LibFx.RegisterAnimation("Height", function(fx)
+ fx.start = fx.frame:GetHeight()
+ fx.diff = fx.finish - fx.start
+end, function(fx)
+ fx.frame:SetHeight(fx.start + fx.diff * fx.progress)
+end)
+
+LibFx.RegisterAnimation("Width", function(fx)
+ fx.start = fx.frame:SetWidth()
+ fx.diff = fx.finish - fx.start
+end, function(fx)
+ fx.frame:GetWidth(fx.start + fx.diff * fx.progress)
+end)
+
+LibFx.RegisterAnimation("Translate", function(fx)
+ local p = fx.p or {}
+ fx.p = p
+ p[1], p[2], p[3], p[4], p[5] = fx.frame:GetPoint()
+end, function(fx)
+ local frame, p = fx.frame, fx.p
+ local x = p[4] + (fx.xOffset or 0) * fx.progress
+ local y = p[5] + (fx.yOffset or 0) * fx.progress
+ frame:ClearAllPoints()
+ frame:SetPoint(p[1], p[2], p[3], x, y)
+end)
@@ -0,0 +1,11 @@
+## Interface: 30200
+## Title: LibFx-1.1
+## Author: Cargor
+## Version: 1.1
+## X-eMail: xconstruct@gmail.com
+## X-Website: http://www.xconstruct.net
+## Notes: Animations! And these actually work ... unlike Blizz' ones
+## OptionalDeps: cargoShip
+
+LibStub.lua
+LibFx-1.1.lua
@@ -0,0 +1,30 @@
+-- LibStub is a simple versioning stub meant for use in Libraries. http://www.wowace.com/wiki/LibStub for more info
+-- LibStub is hereby placed in the Public Domain Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke
+local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS!
+local LibStub = _G[LIBSTUB_MAJOR]
+
+if not LibStub or LibStub.minor < LIBSTUB_MINOR then
+ LibStub = LibStub or {libs = {}, minors = {} }
+ _G[LIBSTUB_MAJOR] = LibStub
+ LibStub.minor = LIBSTUB_MINOR
+
+ function LibStub:NewLibrary(major, minor)
+ assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)")
+ minor = assert(tonumber(strmatch(minor, "%d+")), "Minor version must either be a number or contain a number.")
+
+ local oldminor = self.minors[major]
+ if oldminor and oldminor >= minor then return nil end
+ self.minors[major], self.libs[major] = minor, self.libs[major] or {}
+ return self.libs[major], oldminor
+ end
+
+ function LibStub:GetLibrary(major, silent)
+ if not self.libs[major] and not silent then
+ error(("Cannot find a library instance of %q."):format(tostring(major)), 2)
+ end
+ return self.libs[major], self.minors[major]
+ end
+
+ function LibStub:IterateLibraries() return pairs(self.libs) end
+ setmetatable(LibStub, { __call = LibStub.GetLibrary })
+end
@@ -0,0 +1,68 @@
+LibFx-1.1
+===============
+
+Animations! And these actually work ... unlike Blizz' ones
+
+Core API
+--------
+
+### FxObject = LibFx.New(FxTable) ###
+Creates a new fx-object
+* FxTable = table holding options
+
+### LibFx:RegisterAnimation(name, func, startFunc) ###
+Registers a new animation for use within LibFx
+
+* name = Name of animation
+* get = Function for getting the initial value
+* set = Function for setting the value the statusbar texture (default "ADD")
+
+### LibFx:RegisterRamp(name, func) ###
+ Registers a new ramp for use within LibFx
+
+* name = Name of ramp
+* func = Function which returns the value to use
+
+FxTable / FxObject options
+----------------
+* anim: String or table which holds the animation
+* ramp: String or table how the animation should progress (default: Linear)
+* frame: The frame which should be animated
+* onStart: Optional function which is called on startup with func(frame, fxObject)
+* onComplete: Optional function which is called on stop with func(frame, fxObject)
+
+FxObject functions
+------------------
+### fx:Start() ###
+Starts the animation
+
+### fx:Stop()
+Stops the animation
+
+### fx:IsRunning()
+Returns whether the animation is running
+
+Default animations
+------------------
+* Alpha: Fade the frame to the value of fxTable.finish
+* Scale: Scale the frame to the value of fxTable.finish
+* Height: Set the frame's height to the value of fxTable.finish
+* Width: Set the frame's width to the value of fxTable.finish
+* Translate: Move the frame by fxTable.xOffset and fxTable.yOffset
+
+Default ramps
+-------------
+* Linear: A linear curve
+* Smooth: A logistic curve
+
+Example
+-------
+ local LFX = LibStub("LibFx-1.1")
+ local fadeFrame = LFX.New{
+ frame = MyFrame,
+ anim = "Alpha",
+ ramp = "Smooth",
+ finish = 0,
+ duration = 0.3,
+ }
+Fades out MyFrame in 0.3 seconds by using a logistic / smooth curve.

0 comments on commit b339544

Please sign in to comment.