105 lines
3.4 KiB
TypeScript
105 lines
3.4 KiB
TypeScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
/**
|
|
* `CVPCB_FOOTPRINT_VIEWER_SELECTION_TOOL`
|
|
* (`cvpcb/tools/cvpcb_fpviewer_selection_tool.cpp`): the footprint viewer's
|
|
* idle tool. Selections aren't supported there; it clears, opens the context
|
|
* menu, and zooms to fit on a middle double click.
|
|
*/
|
|
import { KICURSOR } from '@ziroeda/common/gal/cursors.js';
|
|
import type { ACTION_MENU } from '@ziroeda/common/tool/action_menu.js';
|
|
import { ACTIONS } from '@ziroeda/common/tool/actions.js';
|
|
import { CONDITIONAL_MENU } from '@ziroeda/common/tool/conditional_menu.js';
|
|
import type { COROUTINE_BODY } from '@ziroeda/common/tool/coroutine.js';
|
|
import { SELECTION } from '@ziroeda/common/tool/selection.js';
|
|
import type { RESET_REASON } from '@ziroeda/common/tool/tool_base.js';
|
|
import {
|
|
BUT_LEFT,
|
|
BUT_MIDDLE,
|
|
BUT_RIGHT,
|
|
TA_UNDO_REDO_PRE,
|
|
type TOOL_EVENT,
|
|
} from '@ziroeda/common/tool/tool_event.js';
|
|
import { SYNC_HANDLER, TOOL_INTERACTIVE } from '@ziroeda/common/tool/tool_interactive.js';
|
|
import type { DISPLAY_FOOTPRINTS_FRAME } from '../display_footprints_frame.js';
|
|
|
|
export class CVPCB_FOOTPRINT_VIEWER_SELECTION_TOOL extends TOOL_INTERACTIVE {
|
|
/// Pointer to the parent frame.
|
|
private m_frame: DISPLAY_FOOTPRINTS_FRAME | null = null;
|
|
|
|
/// Current state of selection (not really used: no selection in display footprints frame).
|
|
private readonly m_selection = new SELECTION();
|
|
|
|
constructor() {
|
|
super('common.InteractiveSelection');
|
|
}
|
|
|
|
/// @copydoc TOOL_INTERACTIVE::Init()
|
|
override Init(): boolean {
|
|
this.getEditFrame<DISPLAY_FOOTPRINTS_FRAME>().AddStandardSubMenus(this.m_menu);
|
|
return true;
|
|
}
|
|
|
|
/// @copydoc TOOL_BASE::Reset()
|
|
override Reset(_aReason: RESET_REASON): void {
|
|
this.m_frame = this.getEditFrame<DISPLAY_FOOTPRINTS_FRAME>();
|
|
}
|
|
|
|
/**
|
|
* The main loop.
|
|
*/
|
|
*Main(_aEvent: TOOL_EVENT): COROUTINE_BODY<number> {
|
|
// Main loop: keep receiving events
|
|
for (let evt = yield* this.Wait(); evt; evt = yield* this.Wait()) {
|
|
const frame = this.m_frame!;
|
|
|
|
if (frame.ToolStackIsEmpty()) frame.GetCanvas()?.SetCurrentCursor(KICURSOR.ARROW);
|
|
|
|
if (evt.IsClick(BUT_LEFT)) {
|
|
this.clearSelection();
|
|
} else if (evt.IsClick(BUT_RIGHT)) {
|
|
this.m_menu.ShowContextMenu(this.m_selection);
|
|
} else if (evt.IsDblClick(BUT_MIDDLE)) {
|
|
this.m_toolMgr!.RunAction(ACTIONS.zoomFitScreen);
|
|
} else if (evt.IsCancel() || evt.Action() === TA_UNDO_REDO_PRE) {
|
|
this.clearSelection();
|
|
} else {
|
|
evt.SetPassEvent();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Selections aren't currently supported in the footprint viewer.
|
|
*/
|
|
GetSelection(): SELECTION {
|
|
return this.m_selection;
|
|
}
|
|
|
|
clearSelection(): void {}
|
|
|
|
/**
|
|
* Update the menu to reflect the current tool states.
|
|
*
|
|
* @param aEvent is the event generated by the tool framework.
|
|
*/
|
|
UpdateMenu(aEvent: TOOL_EVENT): number {
|
|
const actionMenu = aEvent.Parameter<ACTION_MENU | null>();
|
|
|
|
if (actionMenu instanceof CONDITIONAL_MENU) actionMenu.Evaluate(this.m_selection);
|
|
|
|
if (actionMenu) actionMenu.UpdateAll();
|
|
|
|
return 0;
|
|
}
|
|
|
|
/// Set up handlers for various events.
|
|
protected override setTransitions(): void {
|
|
this.Go(SYNC_HANDLER(this.UpdateMenu), ACTIONS.updateMenu.MakeEvent());
|
|
this.Go(this.Main, ACTIONS.selectionActivate.MakeEvent());
|
|
}
|
|
}
|