Skip to content

Commit

Permalink
Split VGUI elements
Browse files Browse the repository at this point in the history
  • Loading branch information
marchc1 committed Dec 27, 2024
1 parent ecd85ce commit 993a53f
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 337 deletions.
2 changes: 2 additions & 0 deletions lua/ponder/opener_cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function Ponder.Open(uuid)

if not IsValid(Ponder.UIWindow) then
Ponder.UIWindow = vgui.Create "Ponder.UI"
else
Ponder.UIWindow:Remove()
end

local UI = Ponder.UIWindow
Expand Down
34 changes: 34 additions & 0 deletions lua/vgui/ponder_controlbutton.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local CONTROL_BUTTON = {}
function CONTROL_BUTTON:Init()
self:SetText("")
self:SetTooltipPanelOverride("Ponder.ControlTooltip")
end

local padding = 0
local paddingIfHover = -4
local paddingIfDepressed = 4

function CONTROL_BUTTON:SetImage(path)
self.Material = Material(path, "mips smooth")
end

function CONTROL_BUTTON:Paint(w, h)
local finalPadding = self.Depressed and paddingIfDepressed or self.Hovered and paddingIfHover or padding

surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(self.Material)
surface.DrawTexturedRectRotated(w / 2, h / 2, 48 - finalPadding, 48 - finalPadding, 0)
end

function CONTROL_BUTTON:OnMousePressed(mousecode)
DButton.OnMousePressed(self, mousecode)
self.Depressed = true
end

function CONTROL_BUTTON:OnMouseReleased(mousecode)
DButton.OnMouseReleased(self, mousecode)
self.Depressed = false
end


derma.DefineControl("Ponder.ControlButton", "Control button", CONTROL_BUTTON, "DButton")
270 changes: 0 additions & 270 deletions lua/vgui/ponder_controls.lua
Original file line number Diff line number Diff line change
@@ -1,273 +1,3 @@
local PROGRESSPANEL = {}

local barInsidePadding = 8
local barInsidePadding2 = barInsidePadding * 2

function PROGRESSPANEL:Init()
self:SetMouseInputEnabled(true)
end

local pointy = Material("icon32/hand_point_180.png", "mips smooth")
local bar = Material("ponder/ui/progress/bar.png", "mips smooth")
local stretchy = Material("ponder/ui/progress/stretchy.png", "mips smooth")
local grabby = Material("ponder/ui/progress/grabby.png", "mips smooth")
local left = Material("ponder/ui/progress/left.png", "mips smooth")
local right = Material("ponder/ui/progress/right.png", "mips smooth")

function PROGRESSPANEL:Paint(w, _)
local progress = self:GetFraction()
surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(left); surface.DrawTexturedRect(0, 0, 3, 24)
surface.SetMaterial(stretchy); surface.DrawTexturedRect(3, 0, w - 6, 24)
surface.SetMaterial(right); surface.DrawTexturedRect(w - 3, 0, 3, 24)

local storyboardLength = self.UI.Storyboard.Length

local upperTall = 24

local storyboardLength = self.UI.Storyboard.Length
local chapter_bar_outline_size = 3

for i = 1, #self.UI.Storyboard.Chapters do
local chap = self.UI.Storyboard.Chapters[i]
local ratio = chap.StartTime / storyboardLength
surface.SetDrawColor(71, 71, 79, 150)
surface.DrawRect(barInsidePadding + ((w - barInsidePadding2) * ratio) - (chapter_bar_outline_size / 2), barInsidePadding / 2, chapter_bar_outline_size, upperTall - barInsidePadding)
end
surface.SetDrawColor(255, 255, 255, 255)

draw.NoTexture(); surface.SetDrawColor(30, 30, 30, 60); surface.DrawRect(1 + barInsidePadding, 6, w - barInsidePadding2 - 1, 12)
surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(bar); surface.DrawTexturedRect(2 + barInsidePadding, 7, (w - barInsidePadding2 - 2) * progress, 10)
surface.SetMaterial(grabby); surface.DrawTexturedRect(3 + barInsidePadding + ((w - barInsidePadding2 - 6) * progress) - 7, 0, 14, 24)

if self.HoveredChapter then
local storyboard = self.UI.Storyboard
local chapter = storyboard.Chapters[self.HoveredChapter]
local startX = barInsidePadding + ((chapter.StartTime / storyboard.Length) * (w - barInsidePadding2))

surface.SetDrawColor(255, 255, 255, 255)
surface.DrawRect(startX - 0, 24, 1, 60)
surface.SetDrawColor(255, 255, 255, 55)
surface.DrawRect(startX - 1, 24, 3, 60)
surface.SetDrawColor(255, 255, 255, 255)
-- determine if we're going backwards or forwards
surface.SetMaterial(pointy)
local s = math.sin(CurTime() * 8) * 8
if self.UI.Playback:GetSeconds() > chapter.StartTime then -- going backwards
surface.DrawTexturedRectUV(startX + 16 + s, 32, 32, 32, 0, 0, 1, 1)
else -- going forwards
surface.DrawTexturedRectUV(startX - 16 + s - 32, 32, 32, 32, 1, 0, 0, 1)
end
end
end

function PROGRESSPANEL:Think()
if not self.UI then return end
if not self.UI.Storyboard then return end

self:SetFraction(self.UI.Playback:GetProgress())

if self:IsHovered() then
-- Calculate where they are
local storyboard = self.UI.Storyboard
local mpx, _ = self:ScreenToLocal(input.GetCursorPos())
local timeline_second = ((mpx - barInsidePadding) / (self:GetWide() - barInsidePadding2)) * storyboard.Length
local found_chapter = nil
for chapIndex, chapter in ipairs(storyboard.Chapters) do
if timeline_second >= chapter.StartTime and timeline_second <= (chapter.StartTime + chapter.Length) then
found_chapter = chapIndex
break
end
end
self.HoveredChapter = found_chapter
else
self.HoveredChapter = nil
end
end

function PROGRESSPANEL:LinkTo(ui)
self.UI = ui
end

function PROGRESSPANEL:OnMouseReleased()
if self.HoveredChapter then
if self.UI.Playback.Complete then
self.UI.Playback:Play()
end

self.UI.Playback:SeekChapter(self.HoveredChapter)
end
end

derma.DefineControl("Ponder.Progress", "Ponder progress bar", PROGRESSPANEL, "DProgress")

local SPEEDPANEL = {}

function SPEEDPANEL:Init()
self.Buttons = {}

self:AddSpeed("0.25", .25)
self:AddSpeed("0.5", .5)
self:AddSpeed("0.75", .75)
self:AddSpeed("1", 1)
self:AddSpeed("1.25", 1.25)
self:AddSpeed("1.5", 1.5)
self:AddSpeed("1.75", 1.75)
self:AddSpeed("2", 2)
self:AddSpeed("4", 4)

self:SetSize(96, 26 * #self.Buttons)
end

function SPEEDPANEL:Paint() end

function SPEEDPANEL:AddSpeed(speedText, speedMult)
local b = self:Add("DButton")
b:SetText(speedText .. "x")
b:SetSize(128, 20)
b:DockMargin(2, 2, 2, 2)
b:Dock(TOP)

b.SpeedMod = speedMult

local spInst = self
function b:DoClick()
spInst.UI.Playback.Speed = speedMult
for _, v in ipairs(spInst.Buttons) do
v.Active = false
end
self.Active = true
end
function b:Paint(w, h)
local skin = self:GetSkin()

if self.Depressed or self:IsSelected() or self:GetToggle() then
return skin.tex.Button_Down(0, 0, w, h)
end

if self:GetDisabled() then
return skin.tex.Button_Dead(0, 0, w, h)
end

if self.Hovered then
return skin.tex.Button_Hovered(0, 0, w, h)
end

if self.Active then
local s = (math.sin(CurTime() * 7) + 1) / 2
local r, g, b = 190 + (65 * s), 200 + (55 * s), 255
skin.tex.Button(0, 0, w, h, Color(r, g, b))
else
skin.tex.Button(0, 0, w, h)
end
end
self.Buttons[#self.Buttons + 1] = b
end

function SPEEDPANEL:LinkTo(ui)
self.UI = ui
for _, v in ipairs(self.Buttons) do
v.Active = v.SpeedMod == ui.Playback.Speed
end
end

derma.DefineControl("Ponder.SpeedPanel", "Ponder speed panel", SPEEDPANEL, "DPanel")


local TOOLTIP = {}
AccessorFunc(TOOLTIP, "_text", "Text", FORCE_STRING)
AccessorFunc(TOOLTIP, "_font", "Font", FORCE_STRING)

function TOOLTIP:Init()
self.Birth = RealTime()

self:SetText("")
self:SetFont("DermaDefault")
self:SetDrawOnTop(true)
end

function TOOLTIP:Lifetime()
return RealTime() - self.Birth
end

function TOOLTIP:GetTextSize()
surface.SetFont(self:GetFont())
return surface.GetTextSize(self:GetText())
end

function TOOLTIP:Close()
self:Remove()
end

function TOOLTIP:Think()
local sW, sH = self:GetTextSize()
self:SetTall(sH + 12)
self:SetWide((sW * self:GetAlphaMult(0.7)) + 12)

--local pX, pY = input.GetCursorPos()
--self:SetPos(pX - (self:GetWide() / 2), pY - 8 - sH - 6)
local target = self.Target
local tpx, tpy = target:LocalToScreen(0, 0)
tpx = tpx + (target:GetWide() / 2)
self:SetPos(tpx - (self:GetWide() / 2), tpy - 8 - sH - 6)
end

function TOOLTIP:GetAlphaMult(min)
return math.ease.InOutQuad(math.Clamp(math.Remap(self:Lifetime(), 0, 0.1, min or 0, 1), 0, 1))
end

function TOOLTIP:Paint(w, h)
surface.SetDrawColor(245, 245, 245, 220 * self:GetAlphaMult())
surface.DrawRect(0, 0, w, h)
surface.SetDrawColor(25, 25, 25, 255 * self:GetAlphaMult())
surface.DrawOutlinedRect(0, 0, w, h)

draw.SimpleText(self:GetText(), self:GetFont(), w / 2, h / 2, Color(25, 25, 25, 255 * self:GetAlphaMult()), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end

function TOOLTIP:OpenForPanel(pnl)
self.Target = pnl
end

derma.DefineControl("Ponder.ControlTooltip", "Control tooltip", TOOLTIP, "DPanel")


local CONTROL_BUTTON = {}
function CONTROL_BUTTON:Init()
self:SetText("")
self:SetTooltipPanelOverride("Ponder.ControlTooltip")
end

local padding = 0
local paddingIfHover = -4
local paddingIfDepressed = 4

function CONTROL_BUTTON:SetImage(path)
self.Material = Material(path, "mips smooth")
end

function CONTROL_BUTTON:Paint(w, h)
local finalPadding = self.Depressed and paddingIfDepressed or self.Hovered and paddingIfHover or padding

surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(self.Material)
surface.DrawTexturedRectRotated(w / 2, h / 2, 48 - finalPadding, 48 - finalPadding, 0)
end

function CONTROL_BUTTON:OnMousePressed(mousecode)
DButton.OnMousePressed(self, mousecode)
self.Depressed = true
end

function CONTROL_BUTTON:OnMouseReleased(mousecode)
DButton.OnMouseReleased(self, mousecode)
self.Depressed = false
end


derma.DefineControl("Ponder.ControlButton", "Control button", CONTROL_BUTTON, "DButton")

local PANEL = {}

DEFINE_BASECLASS "DPanel"
Expand Down
56 changes: 56 additions & 0 deletions lua/vgui/ponder_controltooltip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local TOOLTIP = {}
AccessorFunc(TOOLTIP, "_text", "Text", FORCE_STRING)
AccessorFunc(TOOLTIP, "_font", "Font", FORCE_STRING)

function TOOLTIP:Init()
self.Birth = RealTime()

self:SetText("")
self:SetFont("DermaDefault")
self:SetDrawOnTop(true)
end

function TOOLTIP:Lifetime()
return RealTime() - self.Birth
end

function TOOLTIP:GetTextSize()
surface.SetFont(self:GetFont())
return surface.GetTextSize(self:GetText())
end

function TOOLTIP:Close()
self:Remove()
end

function TOOLTIP:Think()
local sW, sH = self:GetTextSize()
self:SetTall(sH + 12)
self:SetWide((sW * self:GetAlphaMult(0.7)) + 12)

--local pX, pY = input.GetCursorPos()
--self:SetPos(pX - (self:GetWide() / 2), pY - 8 - sH - 6)
local target = self.Target
local tpx, tpy = target:LocalToScreen(0, 0)
tpx = tpx + (target:GetWide() / 2)
self:SetPos(tpx - (self:GetWide() / 2), tpy - 8 - sH - 6)
end

function TOOLTIP:GetAlphaMult(min)
return math.ease.InOutQuad(math.Clamp(math.Remap(self:Lifetime(), 0, 0.1, min or 0, 1), 0, 1))
end

function TOOLTIP:Paint(w, h)
surface.SetDrawColor(245, 245, 245, 220 * self:GetAlphaMult())
surface.DrawRect(0, 0, w, h)
surface.SetDrawColor(25, 25, 25, 255 * self:GetAlphaMult())
surface.DrawOutlinedRect(0, 0, w, h)

draw.SimpleText(self:GetText(), self:GetFont(), w / 2, h / 2, Color(25, 25, 25, 255 * self:GetAlphaMult()), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end

function TOOLTIP:OpenForPanel(pnl)
self.Target = pnl
end

derma.DefineControl("Ponder.ControlTooltip", "Control tooltip", TOOLTIP, "DPanel")
Loading

0 comments on commit 993a53f

Please sign in to comment.