"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Tools
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdleTool = void 0;
const IModelApp_1 = require("../IModelApp");
const Tool_1 = require("./Tool");
const ViewTool_1 = require("./ViewTool");
/**
 * The default "idle" tool. If no tool is active, or the active tool does not respond to a given
 * event, input events are forwarded to the idle tool. The default idle tool converts middle mouse button events
 * and touch gestures into view navigation operations like pan, zoom, rotate, and fit.
 *
 * Controls are as follows:
 * - Mouse/keyboard:
 *   - mmb: pan
 *   - shift-mmb: rotate
 *   - wheel: zoom in/out
 *   - double-mmb: fit view
 * - Touch:
 *   - single-finger drag: rotate
 *   - two-finger drag: pan
 *   - pinch: zoom in/out
 *   - double-tap: fit view
 *
 * Touch inputs can be combined e.g. drag two fingers while moving them closer together => pan + zoom in
 * @public
 */
class IdleTool extends Tool_1.InteractiveTool {
    static toolId = "Idle";
    static hidden = true;
    async onMouseStartDrag(ev) {
        if (!ev.viewport)
            return Tool_1.EventHandled.No;
        let toolId;
        let handleId;
        switch (ev.button) {
            case Tool_1.BeButton.Middle:
                if (ev.isControlKey) {
                    toolId = ev.viewport.view.allow3dManipulations() ? "View.Look" : "View.Scroll";
                    handleId = ev.viewport.view.allow3dManipulations() ? ViewTool_1.ViewHandleType.Look : ViewTool_1.ViewHandleType.Scroll;
                }
                else if (ev.isShiftKey) {
                    toolId = "View.Rotate";
                    handleId = ViewTool_1.ViewHandleType.Rotate;
                }
                else {
                    toolId = "View.Pan";
                    handleId = ViewTool_1.ViewHandleType.Pan;
                }
                break;
            case Tool_1.BeButton.Data:
                // When no active tool is present install rotate view tool on drag of data button
                if (undefined !== IModelApp_1.IModelApp.toolAdmin.activeTool)
                    return Tool_1.EventHandled.No;
                toolId = "View.Rotate";
                handleId = ViewTool_1.ViewHandleType.Rotate;
                break;
            default:
                // When no active tool is present install pan view tool on drag of reset button
                if (undefined !== IModelApp_1.IModelApp.toolAdmin.activeTool)
                    return Tool_1.EventHandled.No;
                toolId = "View.Pan";
                handleId = ViewTool_1.ViewHandleType.Pan;
                break;
        }
        const currTool = IModelApp_1.IModelApp.toolAdmin.viewTool;
        if (currTool) {
            if (currTool instanceof ViewTool_1.ViewManip)
                return currTool.startHandleDrag(ev, handleId); // See if current view tool can drag using this handle, leave it active regardless...
            return Tool_1.EventHandled.No;
        }
        const viewTool = IModelApp_1.IModelApp.tools.create(toolId, ev.viewport, true, true);
        if (viewTool && await viewTool.run())
            return viewTool.startHandleDrag(ev);
        return Tool_1.EventHandled.Yes;
    }
    async onMiddleButtonUp(ev) {
        if (!ev.viewport)
            return Tool_1.EventHandled.No;
        if (ev.isDoubleClick) {
            const viewTool = new ViewTool_1.FitViewTool(ev.viewport, true);
            return await viewTool.run() ? Tool_1.EventHandled.Yes : Tool_1.EventHandled.No;
        }
        if (ev.isControlKey || ev.isShiftKey)
            return Tool_1.EventHandled.No;
        IModelApp_1.IModelApp.tentativePoint.process(ev);
        return Tool_1.EventHandled.Yes;
    }
    async onMouseWheel(ev) { return IModelApp_1.IModelApp.toolAdmin.processWheelEvent(ev, true); }
    async onTouchMoveStart(ev, startEv) {
        const tool = new ViewTool_1.DefaultViewTouchTool(startEv, ev);
        return await tool.run() ? Tool_1.EventHandled.Yes : Tool_1.EventHandled.No;
    }
    async onTouchTap(ev) {
        if (ev.isSingleTap) {
            // Send data down/up for single finger tap.
            await IModelApp_1.IModelApp.toolAdmin.convertTouchTapToButtonDownAndUp(ev, Tool_1.BeButton.Data);
            return Tool_1.EventHandled.Yes;
        }
        else if (ev.isTwoFingerTap) {
            // Send reset down/up for two finger tap.
            await IModelApp_1.IModelApp.toolAdmin.convertTouchTapToButtonDownAndUp(ev, Tool_1.BeButton.Reset);
            return Tool_1.EventHandled.Yes;
        }
        else if (ev.isDoubleTap) {
            const vp = ev.viewport;
            if (undefined === vp)
                return Tool_1.EventHandled.No;
            // Fit view on single finger double tap.
            const tool = new ViewTool_1.FitViewTool(vp, true);
            return await tool.run() ? Tool_1.EventHandled.Yes : Tool_1.EventHandled.No;
        }
        return Tool_1.EventHandled.No;
    }
    async exitTool() { }
    async run() { return true; }
}
exports.IdleTool = IdleTool;
//# sourceMappingURL=IdleTool.js.map