/
counterbugtech
/
KontrBugCAD
Обзор
Документация
Войти
/
counterbugtech
/
KontrBugCAD
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/managers/ToolManager.js
1 строка
7 KB
counterbugtech
версия 0.9.37
17 май 2026, 13:43
Верифицирован
17 май 2026, 13:43
31de57b
Код
Авторство
О чём код?
import{SelectTool}from"../tools/select-tool.js";import{MoveTool}from"../tools/move-tool.js";import{RotateTool}from"../tools/rotate-tool.js";import{ScaleTool}from"../tools/scale-tool.js";import{SketchTool}from"../tools/sketch/sketch-tool.js";import{ExtrudeTool}from"../tools/create/extrude-tool.js";import{RevolveTool}from"../tools/create/revolve-tool.js";import{WorkPlaneTool}from"../tools/workplane-tool.js";import{RulerTool}from"../tools/ruler-tool.js";import{GearTool}from"../tools/gear-tool.js";import{ThreadTool}from"../tools/thread-tool.js";import{SplitTool}from"../tools/split-tool.js";import{MirrorTool}from"../tools/mirror-tool.js";import{GroupTool}from"../tools/group-tool.js";import{UngroupTool}from"../tools/ungroup-tool.js";import{FilletChamferTool}from"../tools/fillet-chamfer-tool.js";import{Text3DTool}from"../tools/text3d-tool.js";import{BooleanUnionTool,BooleanSubtractTool,BooleanIntersectTool}from"../tools/boolean-tool.js";import{RepairTool}from"../tools/repair-tool.js";import{DecimateTool}from"../tools/decimate-tool.js";import{ImageTool}from"../tools/image-tool.js";import{AlignTool}from"../tools/align-tool.js";import{SectionByPlaneTool}from"../tools/SectionByPlaneTool.js";import{DimensionTool}from"../tools/dimension-tool.js";export class ToolManager{constructor(o){this.editor=o,this.tools=new Map,this.currentTool=null,this.previousTool=null}initTools(){this.registerTool("select",new SelectTool(this.editor)),this.registerTool("move",new MoveTool(this.editor)),this.registerTool("rotate",new RotateTool(this.editor)),this.registerTool("scale",new ScaleTool(this.editor)),this.registerTool("sketch",new SketchTool(this.editor)),this.registerTool("extrude",new ExtrudeTool(this.editor)),this.registerTool("revolve",new RevolveTool(this.editor)),this.registerTool("workplane",new WorkPlaneTool(this.editor)),this.registerTool("rulerTool",new RulerTool(this.editor)),this.registerTool("gearGenerator",new GearTool(this.editor)),this.registerTool("threadGenerator",new ThreadTool(this.editor)),this.registerTool("split",new SplitTool(this.editor)),this.registerTool("mirror",new MirrorTool(this.editor)),this.registerTool("group",new GroupTool(this.editor)),this.registerTool("ungroup",new UngroupTool(this.editor)),this.registerTool("chamfer",new FilletChamferTool(this.editor)),this.registerTool("text3dTool",new Text3DTool(this.editor)),this.registerTool("boolean-union",new BooleanUnionTool(this.editor)),this.registerTool("boolean-subtract",new BooleanSubtractTool(this.editor)),this.registerTool("boolean-intersect",new BooleanIntersectTool(this.editor)),this.registerTool("repair",new RepairTool(this.editor)),this.registerTool("decimate",new DecimateTool(this.editor)),this.registerTool("imageTool",new ImageTool(this.editor)),this.registerTool("align",new AlignTool(this.editor)),this.registerTool("sectionByPlane",new SectionByPlaneTool(this.editor)),this.registerTool("dimensionTool",new DimensionTool(this.editor)),this.setCurrentTool("select")}registerTool(o,e){this.tools.set(o,e);const t=document.querySelector(`[data-tool="${o}"]`);document.querySelector(`[data-tool="${o}"]`);return t&&(e.uiButton=t,t.addEventListener("click",e=>{e.preventDefault(),this.setCurrentTool(o)})),e}notifyModelRebuilt(){this.currentTool&&"function"==typeof this.currentTool.onModelRebuilt&&this.currentTool.onModelRebuilt()}setCurrentTool(o){if(console.log(`Setting current tool to: ${o}`),this.currentTool&&this.currentTool.name===o)return void console.log(`Tool ${o} is already active`);this.currentTool&&(console.log(`Deactivating current tool: ${this.currentTool.name}`),this.currentTool.deactivate(),this.previousTool=this.currentTool),"select"==o?this.editor.uiManager.switchToDefaultTab():this.editor.uiManager.switchTab("properties");const e=this.tools.get(o);if(e){if(console.log(`Activating tool: ${o}`),this.currentTool=e,e.requiresSelection&&0===this.editor.selectedObjects.length)return this.editor.showStatus(`Для ${e.name} необходимо выбрать объект`,"error"),void(this.previousTool?this.setCurrentTool(this.previousTool.name):this.setCurrentTool("select"));this.currentTool.activate(),this.editor.currentTool=o,this.updateToolUI(o),this.editor.updatePropertiesPanel()}else console.error(`Tool not found: ${o}`)}getTool(o){return this.tools.get(o)}getCurrentTool(){return this.currentTool}updateToolUI(o){document.querySelectorAll(".tool-btn").forEach(o=>{o.classList.remove("active")});document.querySelectorAll("[data-tool]").forEach(e=>{e.classList.remove("active"),e.dataset.tool===o&&e.classList.add("active")});if(document.querySelectorAll(".dropdown-menu a[data-tool]").forEach(e=>{e.classList.remove("active"),e.dataset.tool===o&&e.classList.add("active")}),["move","rotate","scale"].includes(o)){const o=document.querySelector('.dropdown-toggle[title="Трансформация"]');o&&o.classList.add("active")}}restorePreviousTool(){this.previousTool?this.setCurrentTool(this.previousTool.name):this.setCurrentTool("select")}handleMouseDown(o){if(this.currentTool&&this.currentTool.isActive){const e=this.currentTool.onMouseDown(o);return console.log(`Tool ${this.currentTool.name} handled mouse down: ${e}`),e}return!1}handleMouseMove(o){this.currentTool&&this.currentTool.isActive&&this.currentTool.onMouseMove(o)}handleMouseUp(o){if(this.currentTool&&this.currentTool.isActive){const e=this.currentTool.onMouseUp(o);console.log(`Tool ${this.currentTool.name} handled mouse up: ${e}`)}}handleKeyDown(o){if(this.currentTool&&this.currentTool.isActive){const e=this.currentTool.onKeyDown(o);if(console.log(`Tool ${this.currentTool.name} handled KeyDown: ${e}`),e)return!0}switch(o.key.toLowerCase()){case"escape":if(this.currentTool&&"sketch"!==this.currentTool.name)return this.setCurrentTool("select"),o.preventDefault(),!0;break;case"m":case"ь":if(o.ctrlKey||o.metaKey)return this.setCurrentTool("rulerTool"),o.preventDefault(),!0;break;case"m":case"ь":if(o.ctrlKey&&o.shiftKey)return this.setCurrentTool("mirror"),o.preventDefault(),!0;break;case"g":case"п":if(o.ctrlKey&&!o.shiftKey)return this.setCurrentTool("group"),o.preventDefault(),!0;if(o.ctrlKey&&o.shiftKey)return this.setCurrentTool("ungroup"),o.preventDefault(),!0;break;case"w":case"ц":if(!o.ctrlKey&&!o.shiftKey&&!o.altKey)return this.setCurrentTool("move"),o.preventDefault(),!0;break;case"r":case"к":if(!o.ctrlKey&&!o.shiftKey&&!o.altKey)return this.setCurrentTool("rotate"),o.preventDefault(),!0;break;case"s":case"ы":if(!o.ctrlKey&&!o.shiftKey&&!o.altKey)return this.setCurrentTool("scale"),o.preventDefault(),!0;break;case"a":case"ф":if(o.ctrlKey&&o.shiftKey)return this.setCurrentTool("align"),o.preventDefault(),!0}return!1}handleKeyUp(o){this.currentTool&&this.currentTool.isActive&&this.currentTool.onKeyUp(o)}handleDoubleClick(o){return!(!this.currentTool||!this.currentTool.isActive)&&this.currentTool.onDoubleClick(o)}}