function makeEntityMatrix(entity: number): Float32Array {
const [f, r, u, a] = GetEntityMatrix(entity);
return new Float32Array([
r[0], r[1], r[2], 0,
f[0], f[1], f[2], 0,
u[0], u[1], u[2], 0,
a[0], a[1], a[2], 1,
]);
}
function applyEntityMatrix(entity: number, mat: Float32Array | number[]) {
SetEntityMatrix(
entity,
mat[4], mat[5], mat[6], // right
mat[0], mat[1], mat[2], // forward
mat[8], mat[9], mat[10], // up
mat[12], mat[13], mat[14], // at
);
}
function clearEntityDraw(entity: number) {
if (!DoesEntityExist(entity)) return;
SetEntityDrawOutline(entity, false);
}
let lastDebugEnt: number | null = null;
let gizmoToggle = false;
export const handleEditorMode = (editorMode: boolean) => {
let hitEntDebug = lastDebugEnt || 0;
// If editorMode was enabled and is now disabled we want to clear our last draw ent
if (!editorMode && lastDebugEnt) {
clearEntityDraw(lastDebugEnt);
lastDebugEnt = 0;
}
if (!editorMode) return;
const isPressed = IsDisabledControlPressed(0, 24);
const wasReleased = IsDisabledControlJustReleased(0, 24);
if (wasReleased && lastDebugEnt) {
clearEntityDraw(lastDebugEnt);
lastDebugEnt = 0;
}
// We don't want to switch entities if we're currently moving one.
if (!isPressed && !gizmoToggle) {
hitEntDebug = SelectEntityAtCursor(6 | (1 << 5), true);
// For some reason SetEntityMatrix doesn't work with the entity guid SelectEntityAtCursor gives, so we have to get the closest object instead
const [x, y, z] = GetEntityCoords(hitEntDebug, true);
const model = GetEntityModel(hitEntDebug);
hitEntDebug = GetClosestObjectOfType(x, y, z, 2.0, model, false, true, true);
}
// If we don't have an entity then we don't want to display
if (hitEntDebug === 0 && !lastDebugEnt) return;
const matrixBuff = makeEntityMatrix(hitEntDebug) as any
const changed = DrawGizmo(matrixBuff, 'Editor1')
if (changed) {
// Apply the changes from the buff
applyEntityMatrix(hitEntDebug, matrixBuff);
}
if (hitEntDebug) {
SetEntityDrawOutline(hitEntDebug, true);
}
if (hitEntDebug !== lastDebugEnt) {
if (lastDebugEnt) {
clearEntityDraw(lastDebugEnt);
}
lastDebugEnt = hitEntDebug;
}
}
RegisterCommand("gizmoToggleSelection", () => {
if (lastDebugEnt || gizmoToggle) {
gizmoToggle = !gizmoToggle;
}
}, false)
RegisterKeyMapping("gizmoToggleSelection", "Toggles the gizmo to lock for the current entity", "MOUSE_BUTTON", "MOUSE_RIGHT")
RegisterKeyMapping("+gizmoSelect", "Selects the currently highlighted gizmo", "MOUSE_BUTTON", "MOUSE_LEFT")
RegisterKeyMapping("+gizmoTranslation", "Sets mode of the gizmo to translation", "keyboard", "T")
RegisterKeyMapping("+gizmoRotation", "Sets mode for the gizmo to rotation", "keyboard", "R")
RegisterKeyMapping("+gizmoScale", "Sets mode for the gizmo to scale", "keyboard", "S")
RegisterKeyMapping("+gizmoLocal", "Sets gizmo to be local to the entity instead of world", "keyboard", "L")