~sbaildon/sInterface

b0e3d7f659720f618a3bff6722388e0628adc685 — Sean Baildon 3 months ago d0e235d fennel
something kinda working for better progress bars
2 files changed, 76 insertions(+), 18 deletions(-)

M src/modules/progress_bars/experience.fnl
M src/modules/progress_bars/progress_bars.fnl
M src/modules/progress_bars/experience.fnl => src/modules/progress_bars/experience.fnl +64 -16
@@ 1,30 1,78 @@
(local (_ ns) ...)

(fn player-max-level []
    (let [(restricted-level _ _) (GetRestrictedAccountData)]
(fn max-player-level []
    (let [(restricted-level _ _) (_G.GetRestrictedAccountData)]
      (if (> restricted-level 0) restricted-level _G.MAX_PLAYER_LEVEL)))

(fn unit-is-max-level? [unit]
  (= (_G.UnitLevel unit) (max-player-level)))

(fn get-experience-current []
  ((if (IsWatchingHonorAsXP) _G.UnitHonor _G.UnitXP) "player"))
  ((if (_G.IsWatchingHonorAsXP) _G.UnitHonor _G.UnitXP) :player))

(fn get-experience-max []
  ((if (IsWatchingHonorAsXP) _G.UnitHonorMax _G.UnitXPMax) "player"))
  ((if (_G.IsWatchingHonorAsXP) _G.UnitHonorMax _G.UnitXPMax) :player))

(fn get-level []
  ((if (IsWatchingHonorAsXP) _G.UnitHonorLevel _G.UnitLevel) "player"))
  ((if (_G.IsWatchingHonorAsXP) _G.UnitHonorLevel _G.UnitLevel) :player))

(fn get-rested []
  (or ((if (IsWatchingHonorAsXP) _G.GetHonorExhaustion _G.GetXPExhaustion)) 0))
  (or ((if (_G.IsWatchingHonorAsXP) _G.GetHonorExhaustion _G.GetXPExhaustion)) 0))

(fn set-colour [bar]
  (let [[r g b a] (if (_G.IsWatchingHonorAsXP) [1 0.25 0 1] [1 0 1 1])]
    (bar:SetStatusBarColor r g b a)))

(fn set-values [bar]
  (let [cur (get-experience-current)
        max (get-experience-max)
        lvl (get-level)
        rst (get-rested)]
    (bar:SetAnimatedValues cur 0 max lvl)
    (bar.Exhaustion:SetMinMaxValues 0 max)
    (bar.Exhaustion:SetValue (math.min (+ cur rst) max))))

(fn update [bar]
  (set-colour bar)
  (set-values bar))

(fn register-xp-events [bar]
  (each [_ event (ipairs [:PLAYER_XP_UPDATE :HONOR_LEVEL_UPDATE :HONOR_XP_UPDATE :UPDATE_EXHAUSTION])]
    (bar:RegisterEvent event)))

(fn unregister-xp-events [bar]
  (each [_ event (ipairs [:PLAYER_XP_UPDATE :HONOR_LEVEL_UPDATE :HONOR_XP_UPDATE :UPDATE_EXHAUSTION])]
    (bar:UnregisterEvent event)))

(fn visibility [self]
  (if (or (and (unit-is-max-level? :player) (not (_G.IsWatchingHonorAsXP))) (_G.IsXPUserDisabled))
      (unregister-xp-events self.Experience)
      (do (register-xp-events self.Experience) (update self.Experience))))

(local bars ns.sInterfaceProgressBars)

(let [frame (bars:CreateBar :experience)]
  (frame:SetScript :OnEvent visibility)

  (fn frame.Enable [self]
    (each [_ event (ipairs [:PLAYER_LEVEL_UP :DISABLE_XP_GAIN :ENABLE_XP_GAIN :PLAYER_ENTERING_WORLD])]
      (frame:RegisterEvent event))
    (visibility frame))

  (fn frame.Disable [self]
    (each [_ event (ipairs [:PLAYER_LEVEL_UP :DISABLE_XP_GAIN :ENABLE_XP_GAIN :PLAYER_ENTERING_WORLD])]
      (frame:UnregisterEvent event)))

(let [bars ns.sInterfaceProgressBars
      holder (bars:CreateBar :experience)
      experience (CreateFrame :StatusBar :experience holder :AnimatedStatusBarTemplate)]
  (let [experience (CreateFrame :StatusBar :experience frame :AnimatedStatusBarTemplate)]
    (experience:SetMatchBarValueToAnimation true)
    (experience:SetAllPoints holder)
    (experience:SetAllPoints frame)
    (experience:SetStatusBarTexture "Interface\\AddOns\\sInterface\\media\\bar" :ARTWORK)
    (experience:SetFrameLevel (holder:GetFrameLevel))
    (experience:SetStatusBarColor 1 0 1 1)
    (let [cur (get-experience-current)
          max (get-experience-max)
          lvl (get-level)]
      (experience:SetAnimatedValues cur 0 max lvl)))
    (experience:SetScript :OnEvent update)
    (set frame.Experience experience)
    (let [exhaustion (CreateFrame :StatusBar :exhaustion experience)]
      (exhaustion:SetStatusBarTexture "Interface\\AddOns\\sInterface\\media\\bar" :ARTWORK)
      (exhaustion:SetAllPoints experience)
      (exhaustion:SetStatusBarColor 0 0.4 1)
      (exhaustion:SetFrameLevel (- (experience:GetFrameLevel) 1))
      (set experience.Exhaustion exhaustion)))
  (bars:EnableBar :experience))

M src/modules/progress_bars/progress_bars.fnl => src/modules/progress_bars/progress_bars.fnl +12 -2
@@ 5,12 5,22 @@
  (progress-bars:SetPoint :TOPLEFT minimap :BOTTOMLEFT 0 -10)
  (progress-bars:SetPoint :TOPRIGHT minimap :TOPRIGHT 0 -10)
  (progress-bars:SetHeight 20)
  (fn progress-bars.CreateBar [self barName]
    (let [frame (CreateFrame :Frame (.. :sInterfaceProgressBars_ barName) self)]
  (tset progress-bars :bars {})
  (fn progress-bars.CreateBar [self bar-name]
    (let [frame (CreateFrame :Frame (.. :sInterfaceProgressBars_ bar-name) self)]
      (frame:SetPoint :LEFT self :LEFT)
      (frame:SetPoint :RIGHT self :RIGHT)
      (frame:SetFrameLevel (self:GetFrameLevel))
      (frame:SetHeight 4)
      (tset (. self :bars) bar-name frame)
      (ns.E:bordered frame)
      frame))
  (fn progress-bars.DisableBar [self bar-name]
    (match (?. self :bars bar-name)
      nil nil
      bar (bar:Disable)))
  (fn progress-bars.EnableBar [self bar-name]
    (match (?. self :bars bar-name)
      nil nil
      bar (bar:Enable)))
  (set ns.sInterfaceProgressBars progress-bars))