Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 135 additions & 19 deletions lua/entities/gmod_wire_fpga/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ function ENT:GetWorldTipBodySize()
return w_total, h_total
end

local white = Color(255,255,255,255)
local error = Color(255,0,0,255)
local cputime = Color(150,150,255,255)
local cputimeavg = Color(130,240,130,255)
local black = Color(0,0,0,255)

function ENT:DrawWorldTipBody(pos)
local data = self:GetOverlayData()
if not data then return end

local name = data.name

local white = Color(255,255,255,255)
local error = Color(255,0,0,255)
local cputime = Color(150,150,255,255)
local cputimeavg = Color(130,240,130,255)
local black = Color(0,0,0,255)

local w_total, yoffset = 0, pos.min.y

-------------------
Expand Down Expand Up @@ -113,14 +112,17 @@ FPGAInsideViewPosition = {
400
}

local cull = Color(0,0,0,255)
local background = Color(25,25,25,240)

function ENT:DrawInsideViewBackground()
local x1 = ScrW()/2 + FPGAInsideViewPosition[1]
local x2 = ScrW()/2 + FPGAInsideViewPosition[2]
local y1 = ScrH()/2 + FPGAInsideViewPosition[3]
local y2 = ScrH()/2 + FPGAInsideViewPosition[4]

draw.NoTexture()
surface.SetDrawColor(Color(25,25,25,240))
surface.SetDrawColor(background)

local poly = {
{x = x1, y = y1, u = 0, v = 0 },
Expand All @@ -132,14 +134,61 @@ function ENT:DrawInsideViewBackground()
render.CullMode(MATERIAL_CULLMODE_CCW)
surface.DrawPoly(poly)

surface.SetDrawColor(Color(0,0,0,255))
surface.SetDrawColor(cull)

for i=1,#poly-1 do
surface.DrawLine(poly[i].x, poly[i].y, poly[i+1].x, poly[i+1].y)
end
surface.DrawLine(poly[#poly].x, poly[#poly].y, poly[1].x, poly[1].y)
end

function ENT:DrawBezierCurve(x1, y1, x2, y2, color, segments, flipStart, flipEnd)
segments = segments or 20

local distance = math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
local offsetX = math.min(distance * 0.5, 100)

local cx1 = x1 + (flipStart and -offsetX or offsetX)
local cy1 = y1
local cx2 = x2 - (flipEnd and -offsetX or offsetX)
local cy2 = y2

surface.SetDrawColor(color)

local prevX, prevY = x1, y1
for i = 1, segments do
local t = i / segments
local it = 1 - t

-- Cubic Bezier formula
local x = it^3 * x1 + 3 * it^2 * t * cx1 + 3 * it * t^2 * cx2 + t^3 * x2
local y = it^3 * y1 + 3 * it^2 * t * cy1 + 3 * it * t^2 * cy2 + t^3 * y2

surface.DrawLine(prevX, prevY, x, y)
prevX, prevY = x, y
end
end

function ENT:DrawCircle(x, y, radius, segments)
segments = segments or 16
local circle = {}

for i = 0, segments do
local angle = (i / segments) * math.pi * 2
table.insert(circle, {
x = x + math.cos(angle) * radius,
y = y + math.sin(angle) * radius
})
end

draw.NoTexture()
surface.DrawPoly(circle)
end

local waypointColor = Color(255, 198, 109, 255)
local shadowColor = Color(0, 0, 0, 100)
local nodeColor = Color(100, 100, 100, 255)

function ENT:DrawInsideView()
local centerX = ScrW()/2 + (FPGAInsideViewPosition[2] - FPGAInsideViewPosition[1])/2 + FPGAInsideViewPosition[1]
local centerY = ScrH()/2 + (FPGAInsideViewPosition[4] - FPGAInsideViewPosition[3])/2 + FPGAInsideViewPosition[3]
Expand All @@ -149,6 +198,7 @@ function ENT:DrawInsideView()
local scale = math.min(scaleX, scaleY)

local nodeSize = FPGANodeSize/self.ViewData.Scale * scale
local cornerRadius = math.max(2, nodeSize * 0.15)

--to make sure we don't draw outside the edges
render.SetScissorRect(
Expand All @@ -161,22 +211,75 @@ function ENT:DrawInsideView()

--edges
for _, edge in pairs(self.ViewData.Edges) do
surface.SetDrawColor(FPGATypeColor[edge.type])
surface.DrawLine(
centerX + edge.from.x * scale, centerY + edge.from.y * scale,
centerX + edge.to.x * scale, centerY + edge.to.y * scale
)
local x1 = centerX + edge.from.x * scale
local y1 = centerY + edge.from.y * scale

local points = {{x1, y1}}

if edge.waypoints then
for _, wp in ipairs(edge.waypoints) do
points[#points + 1] = {centerX + wp.x * scale, centerY + wp.y * scale}
end
end

local x2 = centerX + edge.to.x * scale
local y2 = centerY + edge.to.y * scale
points[#points + 1] = {x2, y2}

local hasWaypoints = #points > 2
local lastFlipped

for i = 1, #points - 1 do
local flipStart, flipEnd

if hasWaypoints then
local goingLeft = points[i+1][1] < points[i][1]

if i == 1 then
flipStart = false
flipEnd = goingLeft
else
flipStart = lastFlipped
flipEnd = (i < #points - 1) and goingLeft
end
lastFlipped = goingLeft
end

self:DrawBezierCurve(
points[i][1], points[i][2],
points[i+1][1], points[i+1][2],
FPGATypeColor[edge.type], nil, flipStart, flipEnd
)
end

if edge.waypoints then
for i, wp in ipairs(edge.waypoints) do
local wpX = centerX + wp.x * scale
local wpY = centerY + wp.y * scale
local wpRadius = math.max(2, nodeSize * 0.1)

surface.SetDrawColor(waypointColor)
self:DrawCircle(wpX, wpY, wpRadius)
end
end
end

--nodes
surface.SetDrawColor(Color(100, 100, 100, 255))
for _, node in pairs(self.ViewData.Nodes) do
surface.DrawRect(centerX + node.x * scale, centerY + node.y * scale, nodeSize, nodeSize * node.size)
local x = centerX + node.x * scale
local y = centerY + node.y * scale
local w = nodeSize
local h = nodeSize * node.size

-- Draw shadow
draw.RoundedBox(cornerRadius, x + 1, y + 1, w, h, shadowColor)

-- Draw node body
draw.RoundedBox(cornerRadius, x, y, w, h, nodeColor)
end

--labels
surface.SetFont("FPGALabel")
surface.SetTextColor(Color(255, 255, 255, 255))
surface.SetTextColor(white)
for _, label in pairs(self.ViewData.Labels) do

local tx, ty = surface.GetTextSize(label.text)
Expand Down Expand Up @@ -245,7 +348,7 @@ function ENT:ConstructInsideView(viewData)
self.ViewData.Edges = {}
if viewData.Edges then
for _, edge in pairs(viewData.Edges) do
table.insert(self.ViewData.Edges, {
local edgeData = {
from = {
x = (edge.sX - self.ViewData.Center[1]) / self.ViewData.Scale,
y = (edge.sY - self.ViewData.Center[2]) / self.ViewData.Scale,
Expand All @@ -255,7 +358,20 @@ function ENT:ConstructInsideView(viewData)
y = (edge.eY - self.ViewData.Center[2]) / self.ViewData.Scale,
},
type = FPGATypeEnumLookup[edge.t]
})
}

-- Add waypoints if they exist
if edge.w then
edgeData.waypoints = {}
for _, wp in ipairs(edge.w) do
table.insert(edgeData.waypoints, {
x = (wp[1] - self.ViewData.Center[1]) / self.ViewData.Scale,
y = (wp[2] - self.ViewData.Center[2]) / self.ViewData.Scale,
})
end
end

table.insert(self.ViewData.Edges, edgeData)
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions lua/entities/gmod_wire_fpga/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,23 @@ function ENT:SynthesizeViewData(data)
local fromNode = data.Nodes[fromNodeId]
local outputNum = connection[2]

table.insert(viewData.Edges, {
local edgeData = {
sX = math.Round(fromNode.x + FPGANodeSize),
sY = math.Round(fromNode.y + (outputNum - 0.5) * FPGANodeSize),
eX = math.Round(node.x),
eY = math.Round(node.y + (inputNum - 0.5) * FPGANodeSize),
t = FPGATypeEnum[getInputType(gate, inputNum)]
})
}

-- Add waypoints if they exist
if connection.waypoints and #connection.waypoints > 0 then
edgeData.w = {}
for _, wp in ipairs(connection.waypoints) do
table.insert(edgeData.w, {math.Round(wp[1] + FPGANodeSize / 2), math.Round(wp[2] + FPGANodeSize / 2)})
end
end

table.insert(viewData.Edges, edgeData)
end
end

Expand Down
Loading
Loading