From addb813a2b0bf525143a5901fc155870907b8339 Mon Sep 17 00:00:00 2001 From: walker Date: Sat, 20 May 2023 18:46:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E4=BA=92=E6=8F=92=E4=BB=B6=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E3=80=81=E5=90=8D=E7=A7=B0=E8=B0=83=E6=95=B4=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E9=A1=B5=E9=9D=A2=E5=9F=BA=E7=A1=80=E5=B8=83?= =?UTF-8?q?=E5=B1=80=20=E7=BB=98=E5=88=B6store=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/draw-app/DrawProperties.vue | 15 +++++ .../draw-app/properties/LinkProperty.vue | 65 +++++++++++++++++++ .../draw-app/templates/LinkTemplate.vue | 65 +++++++++++++++++++ ...{BaseGraphicData.ts => GraphicDataBase.ts} | 6 +- .../app/graphics/IscsFanInteraction.ts | 6 +- src/examples/app/graphics/LinkInteraction.ts | 6 +- src/examples/app/index.ts | 18 ++++- src/jlgraphic/app/JlDrawApp.ts | 13 ++-- src/jlgraphic/app/JlGraphicApp.ts | 4 +- .../plugins/GraphicTransformPlugin.ts | 4 +- src/jlgraphic/plugins/InteractionPlugin.ts | 48 +++++++++++--- src/layouts/DrawLayout.vue | 47 +++----------- src/stores/draw-store.ts | 29 +++++++++ 13 files changed, 256 insertions(+), 70 deletions(-) create mode 100644 src/components/draw-app/DrawProperties.vue create mode 100644 src/components/draw-app/properties/LinkProperty.vue create mode 100644 src/components/draw-app/templates/LinkTemplate.vue rename src/examples/app/graphics/{BaseGraphicData.ts => GraphicDataBase.ts} (94%) create mode 100644 src/stores/draw-store.ts diff --git a/src/components/draw-app/DrawProperties.vue b/src/components/draw-app/DrawProperties.vue new file mode 100644 index 0000000..19967f3 --- /dev/null +++ b/src/components/draw-app/DrawProperties.vue @@ -0,0 +1,15 @@ + + + diff --git a/src/components/draw-app/properties/LinkProperty.vue b/src/components/draw-app/properties/LinkProperty.vue new file mode 100644 index 0000000..2e4673e --- /dev/null +++ b/src/components/draw-app/properties/LinkProperty.vue @@ -0,0 +1,65 @@ + + + diff --git a/src/components/draw-app/templates/LinkTemplate.vue b/src/components/draw-app/templates/LinkTemplate.vue new file mode 100644 index 0000000..2e4673e --- /dev/null +++ b/src/components/draw-app/templates/LinkTemplate.vue @@ -0,0 +1,65 @@ + + + diff --git a/src/examples/app/graphics/BaseGraphicData.ts b/src/examples/app/graphics/GraphicDataBase.ts similarity index 94% rename from src/examples/app/graphics/BaseGraphicData.ts rename to src/examples/app/graphics/GraphicDataBase.ts index c329bf2..e004380 100644 --- a/src/examples/app/graphics/BaseGraphicData.ts +++ b/src/examples/app/graphics/GraphicDataBase.ts @@ -21,7 +21,7 @@ export interface IProtoGraphicData extends pb_1.Message { code: string; } -export abstract class BaseGraphicData implements GraphicData { +export abstract class GraphicDataBase implements GraphicData { _data: IProtoGraphicData; constructor(data: IProtoGraphicData) { this._data = data; @@ -92,10 +92,10 @@ export abstract class BaseGraphicData implements GraphicData { clone(): GraphicData { throw new Error('Method not implemented.'); } - copyFrom(gd: BaseGraphicData): void { + copyFrom(gd: GraphicDataBase): void { pb_1.Message.copyInto(gd._data, this._data); } - eq(other: BaseGraphicData): boolean { + eq(other: GraphicDataBase): boolean { return pb_1.Message.equals(this._data, other._data); } } diff --git a/src/examples/app/graphics/IscsFanInteraction.ts b/src/examples/app/graphics/IscsFanInteraction.ts index 6c851af..1eb2f5f 100644 --- a/src/examples/app/graphics/IscsFanInteraction.ts +++ b/src/examples/app/graphics/IscsFanInteraction.ts @@ -1,16 +1,16 @@ import * as pb_1 from 'google-protobuf'; import { IIscsFanData } from 'src/graphics/iscs-fan/IscsFan'; import { graphicData } from '../protos/draw_data_storage'; -import { BaseGraphicData } from './BaseGraphicData'; +import { GraphicDataBase } from './GraphicDataBase'; -export class IscsFanData extends BaseGraphicData implements IIscsFanData { +export class IscsFanData extends GraphicDataBase implements IIscsFanData { constructor(data?: graphicData.IscsFan) { let fan; if (data) { fan = data; } else { fan = new graphicData.IscsFan({ - common: BaseGraphicData.defaultCommonInfo(), + common: GraphicDataBase.defaultCommonInfo(), }); } super(fan); diff --git a/src/examples/app/graphics/LinkInteraction.ts b/src/examples/app/graphics/LinkInteraction.ts index adec883..477b2c0 100644 --- a/src/examples/app/graphics/LinkInteraction.ts +++ b/src/examples/app/graphics/LinkInteraction.ts @@ -2,14 +2,14 @@ import * as pb_1 from 'google-protobuf'; import { IPointData } from 'pixi.js'; import { ILinkData } from 'src/graphics/link/Link'; import { graphicData } from '../protos/draw_data_storage'; -import { BaseGraphicData } from './BaseGraphicData'; +import { GraphicDataBase } from './GraphicDataBase'; -export class LinkData extends BaseGraphicData implements ILinkData { +export class LinkData extends GraphicDataBase implements ILinkData { constructor(data?: graphicData.Link) { let link; if (!data) { link = new graphicData.Link({ - common: BaseGraphicData.defaultCommonInfo(), + common: GraphicDataBase.defaultCommonInfo(), }); } else { link = data; diff --git a/src/examples/app/index.ts b/src/examples/app/index.ts index 4fa4ac1..9677f78 100644 --- a/src/examples/app/index.ts +++ b/src/examples/app/index.ts @@ -70,7 +70,22 @@ export const DefaultCanvasMenu = new ContextMenu({ ], }); -export function initDrawApp(app: JlDrawApp) { +let drawApp: JlDrawApp | null = null; + +export function getDrawApp(): JlDrawApp | null { + return drawApp; +} + +export function destroyDrawApp(): void { + if (drawApp) { + drawApp.destroy(); + drawApp = null; + } +} + +export function initDrawApp(dom: HTMLElement): JlDrawApp { + drawApp = new JlDrawApp(dom); + const app = drawApp; app.setOptions({ drawAssistants: [ new LinkDraw(app, () => { @@ -137,6 +152,7 @@ export function initDrawApp(app: JlDrawApp) { }, }) ); + return drawApp; } const StorageKey = 'graphic-storage'; diff --git a/src/jlgraphic/app/JlDrawApp.ts b/src/jlgraphic/app/JlDrawApp.ts index 848caeb..b9b9a42 100644 --- a/src/jlgraphic/app/JlDrawApp.ts +++ b/src/jlgraphic/app/JlDrawApp.ts @@ -11,22 +11,19 @@ import { } from 'pixi.js'; import { GraphicIdGenerator } from '../core/IdGenerator'; import { GraphicData, GraphicTemplate, JlGraphic } from '../core/JlGraphic'; -import { - AppDragEvent, - BoundsGraphic, - GraphicTransformEvent, - ShiftData, - TransformPoints, -} from '../plugins'; import { JlOperation } from '../operation/JlOperation'; import { + AppDragEvent, AppInteractionPlugin, CombinationKey, + GraphicTransformEvent, InteractionPlugin, KeyListener, + ShiftData, ViewportMovePlugin, } from '../plugins'; import { CommonMouseTool } from '../plugins/CommonMousePlugin'; +import { MenuItemOptions } from '../ui/Menu'; import { DOWN, LEFT, RIGHT, UP, recursiveChildren } from '../utils'; import { CanvasData, @@ -35,8 +32,6 @@ import { ICanvasProperties, JlCanvas, } from './JlGraphicApp'; -import { ContextMenu } from '../ui/ContextMenu'; -import { MenuItemOptions, MenuOptions } from '../ui/Menu'; /** * 图形绘制助手 diff --git a/src/jlgraphic/app/JlGraphicApp.ts b/src/jlgraphic/app/JlGraphicApp.ts index 6f93a5a..a55e29d 100644 --- a/src/jlgraphic/app/JlGraphicApp.ts +++ b/src/jlgraphic/app/JlGraphicApp.ts @@ -276,8 +276,8 @@ export interface GraphicAppEvents extends GlobalMixins.GraphicAppEvents { graphicstored: [graphic: JlGraphic]; graphicdeleted: [graphic: JlGraphic]; loadfinish: []; - 'interaction-plugin-resume': [activeTool: InteractionPlugin]; // 交互插件启用 - 'interaction-plugin-pause': [activeTool: InteractionPlugin]; // 交互插件停止 + 'interaction-plugin-resume': [plugin: InteractionPlugin]; // 交互插件启用 + 'interaction-plugin-pause': [plugin: InteractionPlugin]; // 交互插件停止 'options-update': [options: GraphicAppOptions]; // 配置更新 graphicselectedchange: [graphic: JlGraphic, selected: boolean]; graphicchildselectedchange: [child: DisplayObject, selected: boolean]; diff --git a/src/jlgraphic/plugins/GraphicTransformPlugin.ts b/src/jlgraphic/plugins/GraphicTransformPlugin.ts index 2a4e9e6..d277651 100644 --- a/src/jlgraphic/plugins/GraphicTransformPlugin.ts +++ b/src/jlgraphic/plugins/GraphicTransformPlugin.ts @@ -8,7 +8,7 @@ import { } from 'pixi.js'; import { AppDragEvent, - BaseInteractionPlugin, + InteractionPluginBase, InteractionPluginType, KeyListener, } from '.'; @@ -154,7 +154,7 @@ export class GraphicTransformEvent { } } -export class GraphicTransformPlugin extends BaseInteractionPlugin { +export class GraphicTransformPlugin extends InteractionPluginBase { static Name = '__graphic_transform_plugin'; /** diff --git a/src/jlgraphic/plugins/InteractionPlugin.ts b/src/jlgraphic/plugins/InteractionPlugin.ts index 321adfe..34a5f5e 100644 --- a/src/jlgraphic/plugins/InteractionPlugin.ts +++ b/src/jlgraphic/plugins/InteractionPlugin.ts @@ -33,9 +33,12 @@ export interface InteractionPlugin { * 是否生效 */ isActive(): boolean; + isAppPlugin(): boolean; + isOtherPlugin(): boolean; + isGraphicPlugin(): boolean; } -export abstract class BaseInteractionPlugin implements InteractionPlugin { +export abstract class InteractionPluginBase implements InteractionPlugin { readonly _type: string; name: string; // 唯一标识 app: GraphicApp; @@ -55,6 +58,7 @@ export abstract class BaseInteractionPlugin implements InteractionPlugin { resume(): void { this.bind(); this._pause = false; + this.app.emit('interaction-plugin-resume', this); } /** * 停止 @@ -62,6 +66,7 @@ export abstract class BaseInteractionPlugin implements InteractionPlugin { pause(): void { this.unbind(); this._pause = true; + this.app.emit('interaction-plugin-pause', this); } abstract bind(): void; @@ -72,6 +77,21 @@ export abstract class BaseInteractionPlugin implements InteractionPlugin { isActive(): boolean { return !this._pause; } + isGraphicPlugin(): boolean { + return this._type === InteractionPluginType.Graphic; + } + isAppPlugin(): boolean { + return this._type === InteractionPluginType.App; + } + isOtherPlugin(): boolean { + return this._type === InteractionPluginType.Other; + } +} + +export abstract class OtherInteractionPlugin extends InteractionPluginBase { + constructor(app: GraphicApp, name: string) { + super(app, name, InteractionPluginType.Other); + } } export class AppDragEvent { @@ -152,7 +172,7 @@ export class AppDragEvent { /** * 拖拽操作插件 */ -export class DragPlugin extends BaseInteractionPlugin { +export class DragPlugin extends OtherInteractionPlugin { static Name = '__drag_operation_plugin'; private threshold = 3; target: DisplayObject | null = null; @@ -160,7 +180,7 @@ export class DragPlugin extends BaseInteractionPlugin { startClientPoint: Point | null = null; drag = false; constructor(app: GraphicApp) { - super(app, DragPlugin.Name, InteractionPluginType.Other); + super(app, DragPlugin.Name); app.on('options-update', (options: IGraphicAppConfig) => { if (options.threshold !== undefined) { this.threshold = options.threshold; @@ -258,7 +278,7 @@ export class DragPlugin extends BaseInteractionPlugin { /** * 视口移动插件 */ -export class ViewportMovePlugin extends BaseInteractionPlugin { +export class ViewportMovePlugin extends OtherInteractionPlugin { static Name = '__viewport_move_plugin'; static MoveInterval = 20; // 移动间隔,单位ms @@ -270,7 +290,7 @@ export class ViewportMovePlugin extends BaseInteractionPlugin { moveSpeedy = 0; constructor(app: GraphicApp) { - super(app, ViewportMovePlugin.Name, InteractionPluginType.Other); + super(app, ViewportMovePlugin.Name); } static new(app: GraphicApp): ViewportMovePlugin { @@ -360,9 +380,7 @@ export class ViewportMovePlugin extends BaseInteractionPlugin { /** * 应用交互插件,同时只能生效一个 */ -export abstract class AppInteractionPlugin extends BaseInteractionPlugin { - readonly _type = InteractionPluginType.App; - +export abstract class AppInteractionPlugin extends InteractionPluginBase { constructor(name: string, app: GraphicApp) { super(app, name, InteractionPluginType.App); } @@ -372,8 +390,7 @@ export abstract class AppInteractionPlugin extends BaseInteractionPlugin { */ resume(): void { this.app.pauseAppInteractionPlugins(); - this.bind(); - this._pause = false; + super.resume(); } } @@ -409,16 +426,27 @@ export abstract class GraphicInteractionPlugin isActive(): boolean { return !this._pause; } + isAppPlugin(): boolean { + return false; + } + isOtherPlugin(): boolean { + return false; + } + isGraphicPlugin(): boolean { + return true; + } resume(): void { const list = this.filter(...this.app.queryStore.getAllGraphics()); this.binds(list); this._pause = false; + this.app.emit('interaction-plugin-resume', this); } pause(): void { const list = this.filter(...this.app.queryStore.getAllGraphics()); this.unbinds(list); this._pause = true; + this.app.emit('interaction-plugin-pause', this); } /** diff --git a/src/layouts/DrawLayout.vue b/src/layouts/DrawLayout.vue index 0a2ba5e..e762f9a 100644 --- a/src/layouts/DrawLayout.vue +++ b/src/layouts/DrawLayout.vue @@ -97,35 +97,7 @@ - - - - - - - Inbox - - - - - - - - Outbox - - + @@ -135,10 +107,13 @@ diff --git a/src/stores/draw-store.ts b/src/stores/draw-store.ts new file mode 100644 index 0000000..0e521b6 --- /dev/null +++ b/src/stores/draw-store.ts @@ -0,0 +1,29 @@ +import { defineStore } from 'pinia'; +import { destroyDrawApp, initDrawApp } from 'src/examples/app'; +import { GraphicDrawAssistant } from 'src/jlgraphic'; + +export const useDrawStore = defineStore('draw', { + state: () => ({ + drawMode: false, + }), + getters: {}, + actions: { + initDrawApp(dom: HTMLElement) { + const app = initDrawApp(dom); + app.on('interaction-plugin-resume', (plugin) => { + if (plugin.isAppPlugin()) { + if (plugin instanceof GraphicDrawAssistant) { + this.drawMode = true; + } else { + this.drawMode = false; + } + } + }); + return app; + }, + destroy() { + this.drawMode = false; + destroyDrawApp(); + }, + }, +});