-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.lua
More file actions
222 lines (178 loc) · 4.92 KB
/
example.lua
File metadata and controls
222 lines (178 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
--[[
Example tool for demonstrating BiValues functionality.
]]
TOOL.Name = "BiValues Example Tool";
TOOL.Category = "Poser";
TOOL.Command = nil;
TOOL.ConfigName = "";
local DefaultValues = {
PosX = 0, PosY = 0, PosZ = 0,
AngP = 0, AngY = 0, AngR = 0,
ScaleX = 1, ScaleY = 1, ScaleZ = 1,
};
local ManipulationKeys = {
"PosX", "PosY", "PosZ",
"AngP", "AngY", "AngR",
"ScaleX", "ScaleY", "ScaleZ",
};
local function EntityChanged(container, key, value)
if not IsValid(value) then
container.BoneChoices = {};
container.Bone = -1;
container:_ApplyBulk(function()
for key, val in pairs(DefaultValues) do
container[key] = 0;
end
end);
return;
end
local entity = value;
local bones = {};
for b = 0, entity:GetBoneCount() - 1 do
local name = entity:GetBoneName(b);
if name ~= "__INVALIDBONE__" then
bones[name] = b;
end
end
container.BoneChoices = bones;
container.Bone = -1;
container:_ApplyBulk(function()
for key, val in pairs(DefaultValues) do
container[key] = 0;
end
end);
end
local function BoneChanged(container, key, value)
local entity = container.Entity;
local bone = value;
if not IsValid(entity) or not bone or bone == -1 then
return;
end
container:_ApplyBulk(function()
local manPos = entity:GetManipulateBonePosition(bone);
local manAng = entity:GetManipulateBoneAngles(bone);
local manScale = entity:GetManipulateBoneScale(bone);
container.PosX = manPos.x;
container.PosY = manPos.y;
container.PosZ = manPos.z;
container.AngP = manAng.p;
container.AngY = manAng.y;
container.AngR = manAng.r;
container.ScaleX = manScale.x;
container.ScaleY = manScale.y;
container.ScaleZ = manScale.z;
end);
end
local function Manipulated(container, key, value)
local entity = container.Entity;
local bone = container.Bone;
if not IsValid(entity) or not bone or bone == -1 then
return;
end
local manPos = Vector(container.PosX, container.PosY, container.PosZ);
local manAng = Angle(container.AngP, container.AngY, container.AngR);
local manScale = Vector(container.ScaleX, container.ScaleY, container.ScaleZ);
entity:ManipulateBonePosition(bone, manPos);
entity:ManipulateBoneAngles(bone, manAng);
entity:ManipulateBoneScale(bone, manScale);
end
local function CreateContainer(player)
if player.BVET then
return;
end
local defaults = {
Entity = nil,
BoneChoices = {},
Bone = nil,
PosX = 0, PosY = 0, PosZ = 0,
AngP = 0, AngY = 0, AngR = 0,
ScaleX = 0, ScaleY = 0, ScaleZ = 0,
};
if SERVER then
defaults.Reset = function(container, key)
container:_ApplyBulk(function()
for key, val in pairs(DefaultValues) do
container[key] = val;
end
end);
end
end
local data;
if SERVER then
data = BiValues.New(player, "BVET", {IsPrivate = true, UseSync = true, AutoApply = true}, defaults);
else
data = BiValues.New("BVET", {IsPrivate = true, UseSync = true, AutoApply = true}, defaults);
end
if SERVER then
data:_Listen("Entity", EntityChanged);
data:_Listen("Bone", BoneChanged);
for key, val in pairs(DefaultValues) do
data:_Listen(key, Manipulated);
end
end
if CLIENT then
for key, val in pairs(DefaultValues) do
data:_BindToConVar(key, CreateClientConVar("bvet_" .. key:lower(), val, false), {ValueType = "number"});
end
end
player.BVET = data;
end
function TOOL:LeftClick(trace)
if not IsValid(trace.Entity) then
return false;
end
if CLIENT then
return true;
end
local data = self:GetOwner().BVET;
data.Entity = trace.Entity;
data.Bone = data.Entity:TranslatePhysBoneToBone(trace.PhysicsBone);
return true;
end
function TOOL:RightClick(trace)
if CLIENT then
return true;
end
local data = self:GetOwner().BVET;
data.Entity = Entity(-1);
return true;
end
function TOOL:Deploy()
local player = self:GetOwner();
if not player.BVET then
CreateContainer(player);
end
end
if CLIENT then
language.Add("tool.example.name", "BiValues Example Tool");
language.Add("tool.example.desc", "Advanced bone tool -like tool to demonstrate BiValues functionality");
language.Add("tool.example.0", "Left click to select entities for manipulation.");
function TOOL.BuildCPanel(cpanel)
local player = LocalPlayer();
if not player.BVET then
CreateContainer(player);
end
local data = player.BVET;
local boneBox = vgui.Create("DComboBox", cpanel);
boneBox:SetValue("Select bone");
cpanel:AddItem(boneBox);
boneBox:Bind(data, "BoneChoices", "ComboBox");
boneBox:Bind(data, "Bone", "ComboBoxSelect");
for _, key in pairs(ManipulationKeys) do
local sliderTitle = vgui.Create("DLabel", cpanel);
sliderTitle:SetText(key);
sliderTitle:SizeToContents();
cpanel:AddItem(sliderTitle);
local slider = vgui.Create("Slider", cpanel);
slider:SetMinMax(-500, 500);
slider:SetValue(0);
cpanel:AddItem(slider);
slider:Bind(data, key, "Number");
end
local resetBtn = vgui.Create("DButton", cpanel);
resetBtn:SetText("Reset");
resetBtn:SetSize(120, 30);
cpanel:AddItem(resetBtn);
resetBtn:Bind(data, "Reset", "Button");
end
end