import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as Platform from"../platform/platform.js";import*as UI from"../ui/ui.js";const UIStrings={panModeX:"Pan mode (X)",rotateModeV:"Rotate mode (V)",resetTransform:"Reset transform (0)"},str_=i18n.i18n.registerUIStrings("layer_viewer/TransformController.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class TransformController extends Common.ObjectWrapper.ObjectWrapper{constructor(t,e){if(super(),this._scale=1,this._offsetX=0,this._offsetY=0,this._rotateX=0,this._rotateY=0,this._oldRotateX=0,this._oldRotateY=0,this._originX=0,this._originY=0,this.element=t,this._registerShortcuts(),UI.UIUtils.installDragHandle(t,this._onDragStart.bind(this),this._onDrag.bind(this),this._onDragEnd.bind(this),"move",null),t.addEventListener("wheel",this._onMouseWheel.bind(this),!1),this._minScale=0,this._maxScale=1/0,this._controlPanelToolbar=new UI.Toolbar.Toolbar("transform-control-panel"),this._modeButtons={},!e){const t=new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.panModeX),"largeicon-pan");t.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this._setMode.bind(this,"Pan")),this._modeButtons.Pan=t,this._controlPanelToolbar.appendToolbarItem(t);const e=new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.rotateModeV),"largeicon-rotate");e.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this._setMode.bind(this,"Rotate")),this._modeButtons.Rotate=e,this._controlPanelToolbar.appendToolbarItem(e)}this._setMode("Pan");const o=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.resetTransform),"largeicon-center");o.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.resetAndNotify.bind(this,void 0)),this._controlPanelToolbar.appendToolbarItem(o),this._reset()}toolbar(){return this._controlPanelToolbar}_registerShortcuts(){UI.ShortcutRegistry.ShortcutRegistry.instance().addShortcutListener(this.element,{"layers.reset-view":async()=>(this.resetAndNotify(),!0),"layers.pan-mode":async()=>(this._setMode("Pan"),!0),"layers.rotate-mode":async()=>(this._setMode("Rotate"),!0),"layers.zoom-in":this._onKeyboardZoom.bind(this,1.1),"layers.zoom-out":this._onKeyboardZoom.bind(this,1/1.1),"layers.up":this._onKeyboardPanOrRotate.bind(this,0,-1),"layers.down":this._onKeyboardPanOrRotate.bind(this,0,1),"layers.left":this._onKeyboardPanOrRotate.bind(this,-1,0),"layers.right":this._onKeyboardPanOrRotate.bind(this,1,0)})}_postChangeEvent(){this.dispatchEventToListeners(Events.TransformChanged)}_reset(){this._scale=1,this._offsetX=0,this._offsetY=0,this._rotateX=0,this._rotateY=0}_setMode(t){this._mode!==t&&(this._mode=t,this._updateModeButtons())}_updateModeButtons(){for(const t in this._modeButtons)this._modeButtons[t].setToggled(t===this._mode)}resetAndNotify(t){this._reset(),this._postChangeEvent(),t&&t.preventDefault(),this.element.focus()}setScaleConstraints(t,e){this._minScale=t,this._maxScale=e,this._scale=Platform.NumberUtilities.clamp(this._scale,t,e)}clampOffsets(t,e,o,s){this._offsetX=Platform.NumberUtilities.clamp(this._offsetX,t,e),this._offsetY=Platform.NumberUtilities.clamp(this._offsetY,o,s)}scale(){return this._scale}offsetX(){return this._offsetX}offsetY(){return this._offsetY}rotateX(){return this._rotateX}rotateY(){return this._rotateY}_onScale(t,e,o){t=Platform.NumberUtilities.clamp(this._scale*t,this._minScale,this._maxScale)/this._scale,this._scale*=t,this._offsetX-=(e-this._offsetX)*(t-1),this._offsetY-=(o-this._offsetY)*(t-1),this._postChangeEvent()}_onPan(t,e){this._offsetX+=t,this._offsetY+=e,this._postChangeEvent()}_onRotate(t,e){this._rotateX=t,this._rotateY=e,this._postChangeEvent()}async _onKeyboardZoom(t){return this._onScale(t,this.element.clientWidth/2,this.element.clientHeight/2),!0}async _onKeyboardPanOrRotate(t,e){return"Rotate"===this._mode?this._onRotate(this._rotateX+5*e,this._rotateY+5*t):this._onPan(6*t,6*e),!0}_onMouseWheel(t){const e=t,o=Math.pow(1.1,-e.deltaY*(1/53));this._onScale(o,e.clientX-this.element.totalOffsetLeft(),e.clientY-this.element.totalOffsetTop())}_onDrag(t){const{clientX:e,clientY:o}=t;"Rotate"===this._mode?this._onRotate(this._oldRotateX+(this._originY-o)/this.element.clientHeight*180,this._oldRotateY-(this._originX-e)/this.element.clientWidth*180):(this._onPan(e-this._originX,o-this._originY),this._originX=e,this._originY=o)}_onDragStart(t){return this.element.focus(),this._originX=t.clientX,this._originY=t.clientY,this._oldRotateX=this._rotateX,this._oldRotateY=this._rotateY,!0}_onDragEnd(){this._originX=0,this._originY=0,this._oldRotateX=0,this._oldRotateY=0}}export var Events;!function(t){t.TransformChanged="TransformChanged"}(Events||(Events={}));