Merge pull request #18 from penolakushari/gizmo_improvement

Gizmo improvement
This commit is contained in:
penolakushari 2023-03-04 00:41:18 +03:00 committed by GitHub
commit a1caf7b6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 296 additions and 52 deletions

View File

@ -85,6 +85,7 @@ hook.Add("PlayerSpawn","rgmSpawn",function(pl) --PlayerSpawn is a hook that runs
pl.rgmAngLocks = {}
pl.rgm.Rotate = false
pl.rgm.Scale = false
pl.rgm.GizmoOffset = Vector(0, 0, 0)
end
if not pl.rgmSync or not pl.rgmSyncOne then

View File

@ -47,22 +47,6 @@ net.Receive("rgmAxis",function(len)
}
end)
net.Receive("rgmAxisUpdate",function(len)
local self = net.ReadEntity()
local pos = net.ReadVector()
local ang = net.ReadAngle()
local discpos = net.ReadVector()
local discang = net.ReadAngle()
if not self.Axises then return end
self.TargetPos = pos
self.TargetAng = ang
self.TargetDiscPos = discpos
self.TargetDiscAng = discang
end)
function ENT:DrawLines(scale,width)
local pl = LocalPlayer()

View File

@ -12,7 +12,6 @@ local TYPE_DISC = 3
util.AddNetworkString("rgmAxisRequest")
util.AddNetworkString("rgmAxis")
util.AddNetworkString("rgmAxisUpdate")
local function SendAxisToPlayer(Axis, pl)
@ -231,13 +230,10 @@ function ENT:Think()
local bone = pl.rgm.PhysBone
if not IsValid(ent) or not pl.rgm.Bone or not self.Axises then return end
local OldPos = self:GetPos()
local OldAng = self:GetAngles()
local OldDiscPos = self.DiscLarge:GetLocalPos()
local OldDiscAng = self.DiscLarge:GetLocalAngles()
local pos, ang
local rotate = pl.rgm.Rotate or false
local scale = pl.rgm.Scale or false
local offset, offsetlocal = pl.rgm.GizmoOffset, self.localizedoffset
if pl.rgm.IsPhysBone then
@ -294,7 +290,13 @@ function ENT:Think()
end
end
self:SetPos(pos)
if not pl.rgm.Moving or not rotate then
if offsetlocal then
self:SetPos(LocalToWorld(offset, Angle(0, 0, 0), pos, ang))
else
self:SetPos(pos + offset)
end
end
local localstate = self.localizedpos
if rotate then
@ -316,28 +318,25 @@ function ENT:Think()
self.DiscP:SetLocalAngles(Angle(0, 90, 0))
self.DiscR:SetLocalAngles(Angle(0, 0, 0))
end
self.LocalAngles = ang
self.BonePos = pos
end
local pos, poseye = self:GetPos(), pl:EyePos()
local disc = self.DiscLarge
local ang = (self:GetPos()-pl:EyePos()):Angle()
local ang = (pos - poseye):Angle()
ang = self:WorldToLocalAngles(ang)
disc:SetLocalAngles(ang)
local NewPos = self:GetPos()
local NewAng = self:GetAngles()
local NewDiscPos = self.DiscLarge:GetLocalPos()
local NewDiscAng = self.DiscLarge:GetLocalAngles()
if NewPos ~= OldPos or NewAng ~= OldAng
or NewDiscPos ~= OldDiscPos or NewDiscAng ~= OldDiscAng then
net.Start("rgmAxisUpdate")
net.WriteEntity(self)
net.WriteVector(self:GetPos())
net.WriteAngle(self:GetAngles())
net.WriteVector(disc:GetLocalPos())
net.WriteAngle(disc:GetLocalAngles())
net.Send(self.Owner)
end
pos, poseye = self:WorldToLocal(pos), self:WorldToLocal(poseye)
local xangle, yangle = (Vector(pos.y, pos.z, 0) - Vector(poseye.y, poseye.z, 0)):Angle(), (Vector(pos.x, pos.z, 0) - Vector(poseye.x, poseye.z, 0)):Angle()
local XAng, YAng, ZAng = Angle(0, 0, xangle.y + 90) + Vector(1,0,0):Angle(), Angle(0, 90, 90) - Angle(0,0,yangle.y), Angle(0, ang.y, 0) + Vector(0,0,1):Angle()
self.ArrowX:SetLocalAngles(XAng)
self.ScaleX:SetLocalAngles(XAng)
self.ArrowY:SetLocalAngles(YAng)
self.ScaleY:SetLocalAngles(YAng)
self.ArrowZ:SetLocalAngles(ZAng)
self.ScaleZ:SetLocalAngles(ZAng)
self:NextThink(CurTime()+0.001)
return true

View File

@ -6,6 +6,12 @@ AddCSLuaFile("shared.lua")
function ENT:ProcessMovement(offpos,offang,eyepos,eyeang,ent,bone,ppos,pnorm, isphys, StartGrab, NPhysPos)
local intersect = self:GetGrabPos(eyepos,eyeang,ppos)
local localized = self:WorldToLocal(intersect)
local axis = self:GetParent()
local offset = axis.Owner.rgm.GizmoOffset
if axis.localizedoffset then
offset = LocalToWorld(offset, Angle(0, 0, 0), axis:GetPos(), axis.LocalAngles)
offset = offset - axis:GetPos()
end
local pos, ang
if isphys then
@ -14,7 +20,7 @@ function ENT:ProcessMovement(offpos,offang,eyepos,eyeang,ent,bone,ppos,pnorm, is
localized = Vector(localized.x,0,0)
intersect = self:LocalToWorld(localized)
ang = obj:GetAngles()
pos,_a = LocalToWorld(Vector(offpos.x,0,0),Angle(0,0,0),intersect,self:GetAngles())
pos,_a = LocalToWorld(Vector(offpos.x,0,0),Angle(0,0,0),intersect - offset,self:GetAngles())
else
pos = ent:GetManipulateBonePosition(bone)
localized = Vector(localized.x - StartGrab.x,0,0)

View File

@ -33,19 +33,27 @@ function ENT:ProcessMovement(offpos,offang,eyepos,eyeang,ent,bone,ppos,pnorm, is
if isphys then
local axis = self:GetParent()
local offset = axis.Owner.rgm.GizmoOffset
if axis.localizedoffset and not axis.relativerotate then
offset = LocalToWorld(offset, Angle(0,0,0), axis:GetPos(), axis.LocalAngles)
offset = offset - axis:GetPos()
end
localized = Vector(localized.y,localized.z,0):Angle()
local pos = self:GetPos()
local ang = self:LocalToWorldAngles(Angle(0,0,localized.y))
_p,_a = LocalToWorld(Vector(0,0,0),offang,pos,ang)
_p = pos
if axis.relativerotate then
offset = WorldToLocal(axis.BonePos, Angle(0, 0, 0), axis:GetPos(), axis.LocalAngles)
_p,_a = LocalToWorld(Vector(0,0,0),offang,pos,ang)
_p = LocalToWorld(offset, _a, pos, _a)
else
_p,_a = LocalToWorld(Vector(0,0,0),offang,pos,ang)
_p = pos - offset
end
else
local rotateang, axisangle
axisangle = axistable[self.axistype]
--[[ _a = ent:GetManipulateBoneAngles(bone)
localized = WorldToLocal(localized, localized:Angle(), Vector(0, 0, 0), startAngle:Angle())
localized = Vector(localized.x, localized.z, 0):Angle()
rotateang = NPhysAngle[self.axistype] + localized.y -- putting it in another variable to avoid constant adding onto the angle variable
_a[self.axistype] = rotateang]]
local _, boneang = ent:GetBonePosition(bone)
local startlocal = LocalToWorld(startAngle, startAngle:Angle(), Vector(0,0,0), axisangle) -- first we get our vectors into world coordinates, relative to the axis angles

View File

@ -5,13 +5,19 @@ AddCSLuaFile("shared.lua")
function ENT:ProcessMovement(offpos,offang,eyepos,eyeang,ent,bone,ppos,pnorm, isphys, startGrab, NPhysPos)
local intersect = self:GetGrabPos(eyepos,eyeang,ppos,pnorm)
local axis = self:GetParent()
local offset = axis.Owner.rgm.GizmoOffset
if axis.localizedoffset then
offset = LocalToWorld(offset, Angle(0, 0, 0), axis:GetPos(), axis.LocalAngles)
offset = offset - axis:GetPos()
end
local pos, ang
local pl = self:GetParent().Owner
if isphys then
local obj = ent:GetPhysicsObjectNum(bone)
ang = obj:GetAngles()
pos = LocalToWorld(offpos,Angle(0,0,0),intersect,self:GetAngles())
pos = LocalToWorld(offpos,Angle(0,0,0),intersect - offset,self:GetAngles())
else
local localized, startmove, finalpos, boneang
if ent:GetBoneParent(bone) ~= -1 then

View File

@ -6,6 +6,8 @@ TOOL.ConfigName = ""
TOOL.ClientConVar["localpos"] = 0
TOOL.ClientConVar["localang"] = 1
TOOL.ClientConVar["localoffset"] = 1
TOOL.ClientConVar["relativerotate"] = 0
TOOL.ClientConVar["scale"] = 10
TOOL.ClientConVar["width"] = 0.5
TOOL.ClientConVar["fulldisc"] = 0
@ -50,7 +52,7 @@ local function RGMGetBone(pl, ent, bone)
local bonen = phys or bone
pl.rgm.PhysBone = bonen
if pl.rgm.IsPhysBone then
if pl.rgm.IsPhysBone and not (ent:GetClass() == "prop_physics") then -- physics props only have 1 phys object which is tied to bone -1, and that bone doesn't really exist
pl.rgm.Bone = ent:TranslatePhysBoneToBone(bonen)
else
pl.rgm.Bone = bonen
@ -77,10 +79,16 @@ util.AddNetworkString("rgmLockBoneResponse")
util.AddNetworkString("rgmSelectEntity")
util.AddNetworkString("rgmResetGizmo")
util.AddNetworkString("rgmOperationSwitch")
util.AddNetworkString("rgmSetGizmoToBone")
util.AddNetworkString("rgmUpdateGizmo")
util.AddNetworkString("rgmResetBone")
util.AddNetworkString("rgmResetScale")
util.AddNetworkString("rgmScaleZero")
util.AddNetworkString("rgmAdjustBone")
util.AddNetworkString("rgmGizmoOffset")
util.AddNetworkString("rgmUpdateSliders")
@ -186,6 +194,45 @@ net.Receive("rgmSelectEntity", function(len, pl)
net.Send(pl)
end)
net.Receive("rgmResetGizmo", function(len, pl)
if not pl.rgm then return end
pl.rgm.GizmoOffset = Vector(0,0,0)
net.Start("rgmUpdateGizmo")
net.WriteVector(pl.rgm.GizmoOffset)
net.Send(pl)
end)
net.Receive("rgmOperationSwitch", function(len, pl)
local tool = pl:GetTool("ragdollmover")
if not tool then return end
tool:SetOperation(1)
end)
net.Receive("rgmSetGizmoToBone", function(len, pl)
local vector = net.ReadVector()
if not vector or not pl.rgm then return end
local axis = pl.rgm.Axis
local ent = pl.rgm.Entity
if ent:GetClass() == "prop_ragdoll" and pl.rgm.IsPhysBone then
ent = ent:GetPhysicsObjectNum(pl.rgm.PhysBone)
end
if axis.localizedoffset then
vector = WorldToLocal(vector, Angle(0,0,0), ent:GetPos(), ent:GetAngles())
else
vector = WorldToLocal(vector, Angle(0,0,0), ent:GetPos(), Angle(0,0,0))
end
pl.rgm.GizmoOffset = vector
net.Start("rgmUpdateGizmo")
net.WriteVector(pl.rgm.GizmoOffset)
net.Send(pl)
end)
net.Receive("rgmResetBone", function(len, pl)
ent = pl.rgm.Entity
ent:ManipulateBoneAngles(pl.rgm.Bone, Angle(0, 0, 0))
@ -237,11 +284,18 @@ net.Receive("rgmAdjustBone", function(len, pl)
ent:ManipulateBoneScale(pl.rgm.Bone, Change)
end
local mode, axis, value = net.ReadInt(32), net.ReadInt(32), net.ReadFloat()
local mode, axis, value = net.ReadInt(3), net.ReadInt(3), net.ReadFloat()
ManipulateBone[mode](axis, value)
end)
net.Receive("rgmGizmoOffset", function(len, pl)
local axis = net.ReadUInt(3)
local value = net.ReadFloat()
pl.rgm.GizmoOffset[axis] = value
end)
hook.Add("PlayerDisconnected", "RGMCleanupGizmos", function(pl)
if IsValid(pl.rgm.Axis) then
pl.rgm.Axis:Remove()
@ -294,6 +348,40 @@ end
function TOOL:LeftClick(tr)
if self:GetOperation() == 1 then
if SERVER then
local pl = self:GetOwner()
local axis, ent = pl.rgm.Axis, pl.rgm.Entity
local offset = Vector(0,0,0)
if not IsValid(axis) or not IsValid(ent) then self:SetOperation(0) return true end
offset = tr.HitPos
if ent:GetClass() == "prop_ragdoll" and pl.rgm.IsPhysBone then
ent = ent:GetPhysicsObjectNum(pl.rgm.PhysBone)
elseif ent:GetClass() == "prop_physics" then
ent = ent:GetPhysicsObjectNum(0)
end
if axis.localizedoffset then
offset = WorldToLocal(offset, Angle(0,0,0), ent:GetPos(), ent:GetAngles())
else
offset = WorldToLocal(offset, Angle(0,0,0), ent:GetPos(), Angle(0,0,0))
end
pl.rgm.GizmoOffset = offset
net.Start("rgmUpdateGizmo")
net.WriteVector(pl.rgm.GizmoOffset)
net.Send(pl)
end
self:SetOperation(0)
return true
end
if CLIENT then return false end
local pl = self:GetOwner()
@ -400,11 +488,58 @@ function TOOL:LeftClick(tr)
end
function TOOL:RightClick(tr)
if self:GetOperation() == 1 then
if SERVER then
local pl = self:GetOwner()
local axis = pl.rgm.Axis
local ent, rgment = tr.Entity, pl.rgm.Entity
local offset = Vector(0,0,0)
if not IsValid(axis) or not IsValid(rgment) then self:SetOperation(0) return true end
if IsValid(ent) then
local object = ent:GetPhysicsObjectNum(tr.PhysicsBone)
if not object then object = ent end
offset = object:GetPos()
else
offset = tr.HitPos
end
if rgment:GetClass() == "prop_ragdoll" and pl.rgm.IsPhysBone then
rgment = rgment:GetPhysicsObjectNum(pl.rgm.PhysBone)
elseif rgment:GetClass() == "prop_physics" then
rgment = rgment:GetPhysicsObjectNum(0)
end
if axis.localizedoffset then
offset = WorldToLocal(offset, Angle(0,0,0), rgment:GetPos(), rgment:GetAngles())
else
offset = WorldToLocal(offset, Angle(0,0,0), rgment:GetPos(), Angle(0,0,0))
end
pl.rgm.GizmoOffset = offset
net.Start("rgmUpdateGizmo")
net.WriteVector(pl.rgm.GizmoOffset)
net.Send(pl)
end
self:SetOperation(0)
return true
end
return false
end
function TOOL:Reload()
if CLIENT then return false end
if self:GetOperation() == 1 then
self:SetOperation(0)
return false
end
local pl = self:GetOwner()
RunConsoleCommand("ragdollmover_resetroot")
@ -465,6 +600,12 @@ if SERVER then
if axis.localizedang ~= tobool(self:GetClientNumber("localang",1)) then
axis.localizedang = tobool(self:GetClientNumber("localang",1))
end
if axis.localizedoffset ~= tobool(self:GetClientNumber("localoffset",1)) then
axis.localizedoffset = tobool(self:GetClientNumber("localoffset",1))
end
if axis.relativerotate ~= tobool(self:GetClientNumber("relativerotate",1)) then
axis.relativerotate = tobool(self:GetClientNumber("relativerotate",1))
end
end
local moving = pl.rgm.Moving or false
@ -592,6 +733,15 @@ end
if CLIENT then
TOOL.Information = {
{ name = "left_gizmomode", op = 1 },
{ name = "right_gizmomode", op = 1 },
{ name = "reload_gizmomode", op = 1 },
{ name = "left_default", op = 0 },
{ name = "info_default", op = 0 },
{ name = "reload_default", op = 0 },
}
local BONE_PHYSICAL = 1
local BONE_NONPHYSICAL = 2
local BONE_PROCEDURAL = 3
@ -659,8 +809,28 @@ local function CManipSlider(cpanel, text, mode, axis, min, max, dec)
function slider:OnValueChanged(value)
net.Start("rgmAdjustBone")
net.WriteInt(mode, 32)
net.WriteInt(axis, 32)
net.WriteInt(mode, 3)
net.WriteInt(axis, 3)
net.WriteFloat(value)
net.SendToServer()
end
cpanel:AddItem(slider)
return slider
end
local function CGizmoSlider(cpanel, text, axis, min, max, dec)
local slider = vgui.Create("DNumSlider", cpanel)
slider:SetText(text)
slider:SetDecimals(dec)
slider:SetMinMax(min,max)
slider:SetDark(true)
slider:SetValue(0)
slider:SetDefaultValue(0)
function slider:OnValueChanged(value)
net.Start("rgmGizmoOffset")
net.WriteInt(axis, 3)
net.WriteFloat(value)
net.SendToServer()
end
@ -735,6 +905,20 @@ local function CBinder(cpanel)
end
end
local function RGMResetGizmo()
local pl = LocalPlayer()
if not pl.rgm then return end
net.Start("rgmResetGizmo")
net.SendToServer()
end
local function RGMGizmoMode()
local pl = LocalPlayer()
if not pl.rgm then return end
net.Start("rgmOperationSwitch")
net.SendToServer()
end
local function RGMResetBone()
local pl = LocalPlayer()
if not pl.rgm then return end
@ -907,6 +1091,7 @@ end
local BonePanel, EntPanel
local Pos1, Pos2, Pos3, Rot1, Rot2, Rot3, Scale1, Scale2, Scale3
local Gizmo1, Gizmo2, Gizmo3
local LockRotB, LockPosB
local nodes, entnodes
local HoveredBone
@ -960,6 +1145,29 @@ local function RGMBuildBoneMenu(ent, bonepanel)
net.SendToServer()
end
nodes[v.id].DoRightClick = function()
local bonemenu = DermaMenu(false, bonepanel)
bonemenu:AddOption("Put Gizmo offset here", function()
local pl = LocalPlayer()
if not pl.rgm or not IsValid(pl.rgm.Entity) then return end
local ent = pl.rgm.Entity
local bone = v.id
local pos = ent:GetBonePosition(bone)
if pos == ent:GetPos() then
local matrix = ent:GetBoneMatrix(bone)
pos = matrix:GetTranslation()
end
net.Start("rgmSetGizmoToBone")
net.WriteVector(pos)
net.SendToServer()
end)
bonemenu:Open()
end
nodes[v.id].Label.OnCursorEntered = function()
HoveredBone = v.id
end
@ -1017,6 +1225,15 @@ function TOOL.BuildCPanel(CPanel)
CNumSlider(Col1,"#tool.ragdollmover.width","ragdollmover_width",0.1,1.0,2)
CCheckBox(Col1,"#tool.ragdollmover.fulldisc","ragdollmover_fulldisc")
local GizmoOffset = CCol(Col1, "#tool.ragdollmover.gizmooffsetpanel", true)
CCheckBox(GizmoOffset,"#tool.ragdollmover.gizmolocaloffset","ragdollmover_localoffset")
CCheckBox(GizmoOffset,"#tool.ragdollmover.gizmorelativerotate","ragdollmover_relativerotate")
Gizmo1 = CGizmoSlider(GizmoOffset, "#tool.ragdollmover.xoffset", 1, -300, 300, 2)
Gizmo2 = CGizmoSlider(GizmoOffset, "#tool.ragdollmover.yoffset", 2, -300, 300, 2)
Gizmo3 = CGizmoSlider(GizmoOffset, "#tool.ragdollmover.zoffset", 3, -300, 300, 2)
CButton(GizmoOffset, "#tool.ragdollmover.resetoffset", RGMResetGizmo)
CButton(GizmoOffset, "#tool.ragdollmover.setoffset", RGMGizmoMode)
local Col2 = CCol(CPanel,"#tool.ragdollmover.ikpanel")
CCheckBox(Col2,"#tool.ragdollmover.ik3","ragdollmover_ik_hand_L")
CCheckBox(Col2,"#tool.ragdollmover.ik4","ragdollmover_ik_hand_R")
@ -1119,6 +1336,13 @@ net.Receive("rgmUpdateLists", function(len)
end
end)
net.Receive("rgmUpdateGizmo", function(len)
local vector = net.ReadVector()
if not IsValid(Gizmo1) then return end
Gizmo1:SetValue(vector.x)
Gizmo2:SetValue(vector.y)
Gizmo3:SetValue(vector.z)
end)
net.Receive("rgmUpdateBones", function(len)
local ent = net.ReadEntity()

View File

@ -1,7 +1,14 @@
tool.ragdollmover.name=Ragdoll Mover
tool.ragdollmover.desc=Allows advanced movement of ragdolls!
tool.ragdollmover.0=Left click to select and move bones. Click with mid mouse button to toggle between move/rotate.
tool.ragdollmover.left_default=Select and move bones
tool.ragdollmover.info_default=Toggle between move/rotate/scale by using buttons that are set through tool's menu (Default: Middle and right mouse buttons)
tool.ragdollmover.reload_default=Switch to the root bone of the ragdoll
tool.ragdollmover.left_gizmomode=Set offset to location you aim at
tool.ragdollmover.right_gizmomode=Set offset to coordinate center of the object you aim at
tool.ragdollmover.reload_gizmomode=Cancel set offset mode
tool.ragdollmover.gizmopanel=Gizmo
tool.ragdollmover.localpos=Localized position gizmo.
@ -10,6 +17,15 @@ tool.ragdollmover.scale=Scale
tool.ragdollmover.width=Width
tool.ragdollmover.fulldisc=Fully visible discs.
tool.ragdollmover.gizmooffsetpanel=Gizmo Offset
tool.ragdollmover.gizmolocaloffset=Localized offset
tool.ragdollmover.gizmorelativerotate=Rotate relative to gizmo
tool.ragdollmover.xoffset=X Offset
tool.ragdollmover.yoffset=Y Offset
tool.ragdollmover.zoffset=Z Offset
tool.ragdollmover.resetoffset=Reset offset
tool.ragdollmover.setoffset=Set offset with toolgun
tool.ragdollmover.ikpanel=IK Chains
tool.ragdollmover.ik3=Left Hand IK
tool.ragdollmover.ik4=Right Hand IK