346 lines
12 KiB
JavaScript
346 lines
12 KiB
JavaScript
// 相机偏置功能管理
|
||
(function () {
|
||
// 相机偏置全局变量
|
||
let cameraOffset = {
|
||
x: 0,
|
||
y: 0,
|
||
z: 0
|
||
};
|
||
|
||
// 编辑模式状态
|
||
let editMode = false;
|
||
|
||
// 在页面加载完成后初始化
|
||
document.addEventListener('DOMContentLoaded', function () {
|
||
console.log("相机偏置模块初始化...");
|
||
setTimeout(initCameraOffset, 50);
|
||
});
|
||
|
||
// 初始化相机偏置功能
|
||
function initCameraOffset() {
|
||
try {
|
||
// 加载偏置值
|
||
loadCameraOffset();
|
||
|
||
// 绑定编辑按钮事件
|
||
bindEditButton();
|
||
|
||
// 绑定偏置调整按钮事件
|
||
bindOffsetButtons();
|
||
|
||
console.log("相机偏置模块初始化完成");
|
||
} catch (error) {
|
||
console.error("相机偏置模块初始化失败:", error);
|
||
}
|
||
}
|
||
|
||
// 绑定编辑按钮事件
|
||
function bindEditButton() {
|
||
const editBtn = document.getElementById('edit-offset-btn');
|
||
if (editBtn) {
|
||
editBtn.onclick = async function () {
|
||
if (!editMode) {
|
||
console.log(lang);
|
||
// 显示警告提示
|
||
let editOffsetHintText = await getPopupText("editOffsetHintText");
|
||
if (window.showPopup) {
|
||
window.showPopup(
|
||
editOffsetHintText,
|
||
{ confirm: true, cancel: true }
|
||
).then((confirmed) => {
|
||
if (confirmed) {
|
||
enableEditMode();
|
||
}
|
||
});
|
||
} else {
|
||
if (confirm(editOffsetHintText)) {
|
||
enableEditMode();
|
||
}
|
||
}
|
||
}
|
||
};
|
||
}
|
||
|
||
// 绑定取消按钮
|
||
const cancelBtn = document.getElementById('cancel-offset-btn');
|
||
if (cancelBtn) {
|
||
cancelBtn.onclick = function () {
|
||
disableEditMode();
|
||
// 重新加载原始偏置数据
|
||
loadCameraOffset();
|
||
};
|
||
}
|
||
}
|
||
|
||
// 启用编辑模式
|
||
function enableEditMode() {
|
||
editMode = true;
|
||
|
||
// 显示所有控制元素
|
||
const controls = document.querySelectorAll('.offset-control');
|
||
controls.forEach(control => {
|
||
control.style.display = 'flex';
|
||
});
|
||
|
||
// 显示保存面板
|
||
const savePanel = document.getElementById('save-offset-panel');
|
||
if (savePanel) {
|
||
savePanel.style.display = 'flex';
|
||
}
|
||
|
||
// 修改编辑按钮样式
|
||
const editBtn = document.getElementById('edit-offset-btn');
|
||
if (editBtn) {
|
||
editBtn.textContent = '正在编辑';
|
||
editBtn.style.backgroundColor = '#ff9800';
|
||
editBtn.style.color = 'white';
|
||
}
|
||
}
|
||
|
||
// 禁用编辑模式
|
||
function disableEditMode() {
|
||
editMode = false;
|
||
|
||
// 隐藏所有控制元素
|
||
const controls = document.querySelectorAll('.offset-control');
|
||
controls.forEach(control => {
|
||
control.style.display = 'none';
|
||
});
|
||
|
||
// 隐藏保存面板
|
||
const savePanel = document.getElementById('save-offset-panel');
|
||
if (savePanel) {
|
||
savePanel.style.display = 'none';
|
||
}
|
||
|
||
// 恢复编辑按钮样式
|
||
const editBtn = document.getElementById('edit-offset-btn');
|
||
if (editBtn) {
|
||
editBtn.textContent = '编辑';
|
||
editBtn.style.backgroundColor = '';
|
||
editBtn.style.color = '';
|
||
}
|
||
}
|
||
|
||
// 绑定偏置按钮事件
|
||
function bindOffsetButtons() {
|
||
// X轴减少按钮
|
||
const xMinusBtn = document.getElementById('offset-x-minus');
|
||
if (xMinusBtn) {
|
||
xMinusBtn.onclick = function () {
|
||
console.log("X减按钮点击");
|
||
if (cameraOffset.x > -50) {
|
||
cameraOffset.x -= 2;
|
||
updateOffsetUI();
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到X轴减按钮");
|
||
}
|
||
|
||
// X轴增加按钮
|
||
const xPlusBtn = document.getElementById('offset-x-plus');
|
||
if (xPlusBtn) {
|
||
xPlusBtn.onclick = function () {
|
||
console.log("X加按钮点击");
|
||
if (cameraOffset.x < 50) {
|
||
cameraOffset.x += 2;
|
||
updateOffsetUI();
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到X轴加按钮");
|
||
}
|
||
|
||
// Y轴减少按钮
|
||
const yMinusBtn = document.getElementById('offset-y-minus');
|
||
if (yMinusBtn) {
|
||
yMinusBtn.onclick = function () {
|
||
console.log("Y减按钮点击");
|
||
if (cameraOffset.y > -50) {
|
||
cameraOffset.y -= 2;
|
||
updateOffsetUI();
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到Y轴减按钮");
|
||
}
|
||
|
||
// Y轴增加按钮
|
||
const yPlusBtn = document.getElementById('offset-y-plus');
|
||
if (yPlusBtn) {
|
||
yPlusBtn.onclick = function () {
|
||
console.log("Y加按钮点击");
|
||
if (cameraOffset.y < 50) {
|
||
cameraOffset.y += 2;
|
||
updateOffsetUI();
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到Y轴加按钮");
|
||
}
|
||
|
||
// Z轴减少按钮
|
||
const zMinusBtn = document.getElementById('offset-z-minus');
|
||
if (zMinusBtn) {
|
||
zMinusBtn.onclick = function () {
|
||
console.log("Z减按钮点击");
|
||
if (cameraOffset.z > -50) {
|
||
cameraOffset.z -= 2;
|
||
updateOffsetUI();
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到Z轴减按钮");
|
||
}
|
||
|
||
// Z轴增加按钮
|
||
const zPlusBtn = document.getElementById('offset-z-plus');
|
||
if (zPlusBtn) {
|
||
zPlusBtn.onclick = function () {
|
||
console.log("Z加按钮点击");
|
||
if (cameraOffset.z < 50) {
|
||
cameraOffset.z += 2;
|
||
updateOffsetUI();
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到Z轴加按钮");
|
||
}
|
||
|
||
// 保存按钮
|
||
const saveBtn = document.getElementById('save-offset-btn');
|
||
if (saveBtn) {
|
||
saveBtn.onclick = async function () {
|
||
console.log("保存按钮点击");
|
||
let saveOffsetText = await getPopupText("saveOffsetText");
|
||
if (window.showPopup) {
|
||
window.showPopup(saveOffsetText).then((confirmed) => {
|
||
if (confirmed) {
|
||
saveCameraOffset();
|
||
}
|
||
});
|
||
} else {
|
||
if (confirm(saveOffsetText)) {
|
||
saveCameraOffset();
|
||
}
|
||
}
|
||
};
|
||
} else {
|
||
console.error("未找到保存按钮");
|
||
}
|
||
}
|
||
|
||
// 加载相机偏置值
|
||
function loadCameraOffset() {
|
||
fetch("/get_vtxdb_data", {
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
table: "robot_config",
|
||
key: "camera.camera_offset"
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then((data) => {
|
||
console.log("获取偏置数据成功:", data);
|
||
if (data.status === "success" && data.data) {
|
||
// 如果数据存在,更新偏置值
|
||
const offsetData = data.data;
|
||
if (Array.isArray(offsetData) && offsetData.length === 3) {
|
||
cameraOffset.x = offsetData[0];
|
||
cameraOffset.y = offsetData[1];
|
||
cameraOffset.z = offsetData[2];
|
||
console.log("更新偏置值:", cameraOffset);
|
||
}
|
||
} else {
|
||
// 如果数据不存在,使用默认值0
|
||
cameraOffset = { x: 0, y: 0, z: 0 };
|
||
console.log("使用默认偏置值:", cameraOffset);
|
||
}
|
||
// 更新UI显示
|
||
updateOffsetUI();
|
||
})
|
||
.catch((error) => {
|
||
console.error("获取偏置数据失败:", error);
|
||
// 出错时使用默认值
|
||
cameraOffset = { x: 0, y: 0, z: 0 };
|
||
updateOffsetUI();
|
||
});
|
||
}
|
||
|
||
// 更新UI显示
|
||
function updateOffsetUI() {
|
||
try {
|
||
const xValueElem = document.getElementById("offset-x-value");
|
||
const yValueElem = document.getElementById("offset-y-value");
|
||
const zValueElem = document.getElementById("offset-z-value");
|
||
|
||
if (xValueElem) xValueElem.textContent = cameraOffset.x;
|
||
if (yValueElem) yValueElem.textContent = cameraOffset.y;
|
||
if (zValueElem) zValueElem.textContent = cameraOffset.z;
|
||
|
||
console.log("UI更新成功:", cameraOffset);
|
||
} catch (error) {
|
||
console.error("UI更新失败:", error);
|
||
}
|
||
}
|
||
|
||
// 保存相机偏置值
|
||
function saveCameraOffset() {
|
||
fetch("/set_vtxdb_data", {
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
table: "robot_config",
|
||
key: "camera.camera_offset",
|
||
item_data: [cameraOffset.x, cameraOffset.y, cameraOffset.z]
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then(async (data) => {
|
||
console.log("保存偏置数据成功:", data);
|
||
let saveOffsetSuccessText = await getPopupText("saveOffsetSuccessText");
|
||
if (data.status === "success") {
|
||
if (window.showPopup) {
|
||
window.showPopup(saveOffsetSuccessText, {
|
||
confirm: true,
|
||
cancel: false
|
||
}).then(() => {
|
||
// 保存成功后退出编辑模式
|
||
disableEditMode();
|
||
});
|
||
} else {
|
||
alert(saveOffsetSuccessText);
|
||
disableEditMode();
|
||
}
|
||
} else {
|
||
if (window.showPopup) {
|
||
let saveOffsetFailedText = await getPopupText("saveOffsetFailedText");
|
||
window.showPopup(`${saveOffsetFailedText}: ${data.message}`, {
|
||
confirm: true,
|
||
cancel: false
|
||
});
|
||
} else {
|
||
alert(`${saveOffsetFailedText}: ${data.message}`);
|
||
}
|
||
}
|
||
})
|
||
.catch(async (error) => {
|
||
console.error("保存偏置数据失败:", error);
|
||
if (window.showPopup) {
|
||
let saveOffsetErrorText = await getPopupText("saveOffsetErrorText");
|
||
window.showPopup(saveOffsetErrorText, {
|
||
confirm: true,
|
||
cancel: false
|
||
});
|
||
} else {
|
||
alert(saveOffsetErrorText);
|
||
}
|
||
});
|
||
}
|
||
})();
|