123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- import { SColor, SLine, SPainter, SPoint, SRect } from "@persagy-web/draw/";
- import { SMouseButton, SMouseEvent, SUndoStack } from "@persagy-web/base";
- import { SMathUtil } from "@persagy-web/big/lib/utils/SMathUtil";
- import { SItemStatus } from "@persagy-web/big";
- import {
- SGraphPointListInsert,
- SGraphPointListUpdate,
- SLineStyle,
- SGraphItem
- } from "@persagy-web/graph/";
- import { SGraphEdit } from ".."
- import { Marker } from "../type/Marker";
- export class SBaseLineEdit extends SGraphEdit {
-
-
-
- data: Marker
-
- startItem: SGraphItem | null = null;
-
- endItem: SGraphItem | null = null;
-
- private minX = Number.MAX_SAFE_INTEGER;
-
- private maxX = Number.MIN_SAFE_INTEGER;
-
- private minY = Number.MAX_SAFE_INTEGER;
-
- private maxY = Number.MIN_SAFE_INTEGER;
-
- private _line: SPoint[] = [];
- get line(): SPoint[] {
- return this._line;
- }
- set line(arr: SPoint[]) {
- this._line = arr;
- this.update();
- }
-
-
- private _verAndLeve: Boolean = false;
- get verAndLeve(): Boolean {
- return this._verAndLeve;
- }
- set verAndLeve(bool: Boolean) {
- this._verAndLeve = bool;
- this.update();
- }
-
- protected _status: SItemStatus = SItemStatus.Normal;
- get status(): SItemStatus {
- return this._status;
- }
- set status(v: SItemStatus) {
- this._status = v;
- this.undoStack.clear();
- this.update();
- }
-
- private _strokeColor: SColor = SColor.Black;
- get strokeColor(): SColor {
- return this._strokeColor;
- }
- set strokeColor(v: SColor) {
- this._strokeColor = v;
- this.update();
- }
-
- private _lineStyle: SLineStyle = SLineStyle.Solid;
- get lineStyle(): SLineStyle {
- return this._lineStyle;
- }
- set lineStyle(v: SLineStyle) {
- this._lineStyle = v;
- this.update();
- }
-
- private _fillColor: SColor = SColor.White;
- get fillColor(): SColor {
- return this._fillColor;
- }
- set fillColor(v: SColor) {
- this._fillColor = v;
- this.update();
- }
-
- private _activeFillColor: SColor = new SColor("#2196f3");
- get activeFillColor(): SColor {
- return this._activeFillColor;
- }
- set activeFillColor(v: SColor) {
- this._activeFillColor = v;
- this.update();
- }
-
- private _lineWidth: number = 1;
- get lineWidth(): number {
- return this._lineWidth;
- }
- set lineWidth(v: number) {
- this._lineWidth = v;
- this.update();
- }
-
- dis: number = 5;
-
- protected sceneDis: number = 5;
-
- curIndex: number = -1;
-
- private curPoint: SPoint | null = null;
-
- private undoStack: SUndoStack = new SUndoStack();
-
- constructor(parent: SGraphItem | null, data: Marker) {
- super(parent);
- this.showSelect = false;
- this.data = data;
- if (data.style && data.style.default) {
-
- if (data.style.default.line) {
- let setPointList: SPoint[];
- setPointList = data.style.default.line.map((i: { x: number; y: number; }) => {
- return new SPoint(i.x, i.y)
- });
- this.line = setPointList;
- }
-
- if (data.style.default.strokeColor) {
- this.strokeColor = new SColor(data.style.default.strokeColor)
- }
-
- if (data.style.default.fillColor) {
- this.fillColor = new SColor(data.style.default.fillColor)
- }
-
- if (data.style.default.lineWidth) {
- this.lineWidth = data.style.default.lineWidth
- }
-
- if (data.style.default.lineStyle) {
- this.lineStyle = data.style.default.lineStyle
- }
- }
- }
-
- private addPoint(p: SPoint): void {
- if (this.line.length < 2) {
- this.line.push(p);
- this.recordAction(SGraphPointListInsert, [this.line, p]);
- } else {
- this.line[1] = p;
- this.recordAction(SGraphPointListInsert, [this.line, p, 1]);
- this.status = SItemStatus.Normal;
- this.releaseItem();
- this.$emit("finishCreated");
- this.pointChange();
- }
- this.update();
- }
-
- onDoubleClick(event: SMouseEvent): boolean {
- if (this.status == SItemStatus.Normal) {
- this.status = SItemStatus.Edit;
- this.grabItem(this);
- } else if (this.status == SItemStatus.Edit) {
- this.status = SItemStatus.Normal;
- this.releaseItem();
- }
- this.update();
- return true;
- }
-
- onMouseDown(event: SMouseEvent): boolean {
- this.curIndex = -1;
- this.curPoint = null;
- if (event.shiftKey || this._verAndLeve) {
- event = this.compare(event);
- }
- if (event.buttons == SMouseButton.LeftButton) {
- if (this.status == SItemStatus.Normal) {
-
- super.onMouseDown(event);
- return true;
- } else if (this.status == SItemStatus.Edit) {
-
- this.findNearestPoint(new SPoint(event.x, event.y));
- } else if (this.status == SItemStatus.Create) {
- this.addPoint(new SPoint(event.x, event.y));
- return true;
- }
- }
- return true;
- }
-
- onMouseUp(event: SMouseEvent): boolean {
- if (this.status == SItemStatus.Edit) {
- if (this.curIndex > -1) {
- const p = new SPoint(
- this.line[this.curIndex].x,
- this.line[this.curIndex].y
- );
- this.recordAction(SGraphPointListUpdate, [
- this.line,
- this.curPoint,
- p,
- this.curIndex
- ]);
- }
- } else if (this.status == SItemStatus.Normal) {
- super.onMouseUp(event);
- return true
- }
- this.curIndex = -1;
- this.curPoint = null;
- return true;
- }
-
- onKeyUp(event: KeyboardEvent): void {
-
- if (27 == event.keyCode) {
- if (this.status == SItemStatus.Edit) {
- this.status = SItemStatus.Normal;
- this.releaseItem();
- } else if (this.status == SItemStatus.Create) {
- this.update()
- this.releaseItem();
- this.parent = null;
- }
- }
- }
-
- onMouseMove(event: SMouseEvent): boolean {
- if (event.shiftKey || this._verAndLeve) {
- event = this.compare(event);
- }
- if (this.status == SItemStatus.Create) {
- if (this.line[0] instanceof SPoint) {
- this.line[1] = new SPoint(event.x, event.y);
- this.pointChange();
- }
- } else if (this.status == SItemStatus.Edit) {
- if (-1 != this.curIndex) {
- this.line[this.curIndex].x = event.x;
- this.line[this.curIndex].y = event.y;
- this.pointChange();
- }
- } else {
- return super.onMouseMove(event);
- }
- this.update();
- return true;
- }
-
- protected pointChange(): void {
-
- }
-
- findNearestPoint(p: SPoint): void {
- let len = this.sceneDis;
- for (let i = 0; i < this.line.length; i++) {
- let dis = SMathUtil.pointDistance(
- p.x,
- p.y,
- this.line[i].x,
- this.line[i].y
- );
- if (dis < len) {
- len = dis;
- this.curIndex = i;
- this.curPoint = new SPoint(this.line[this.curIndex]);
- }
- }
- }
-
- protected recordAction(SGraphCommand: any, any: any[]): void {
-
- const command = new SGraphCommand(this.scene, this, ...any);
- this.undoStack.push(command);
- }
-
- moveToOrigin(): void {
- this.line = this.line.map(t => {
- t.x = t.x + this.x;
- t.y = t.y + this.y;
- return t;
- });
- this.x = 0;
- this.y = 0;
- }
-
- compare(event: SMouseEvent): SMouseEvent {
- if (this.line.length) {
- let last = new SPoint(event.x, event.y);
- if (this.status == SItemStatus.Create) {
- last = this.line[0];
- } else if (this.status == SItemStatus.Edit) {
- if (this.curIndex == 1) {
- last = this.line[0];
- } else if (this.curIndex == 0) {
- last = this.line[1];
- }
- }
- const dx = Math.abs(event.x - last.x);
- const dy = Math.abs(event.y - last.y);
- if (dy > dx) {
- event.x = last.x;
- } else {
- event.y = last.y;
- }
- }
- return event;
- }
-
- contains(x: number, y: number): boolean {
- if (this.line.length == 2) {
- let p = new SPoint(x, y);
- if (
- SMathUtil.pointToLine(p, new SLine(this.line[0], this.line[1]))
- .MinDis < this.sceneDis
- ) {
- return true;
- }
- }
- return false;
- }
-
- undo(): void {
- if (this.status != SItemStatus.Normal) {
- this.undoStack.undo();
- }
- }
-
- redo(): void {
- if (this.status != SItemStatus.Normal) {
- this.undoStack.redo();
- }
- }
-
- cancelOperate(): void {
- if (this.status == SItemStatus.Create) {
- this.parent = null;
- this.releaseItem();
- } else if (this.status == SItemStatus.Edit) {
- this.status = SItemStatus.Normal;
- this.releaseItem();
- }
- }
-
- boundingRect(): SRect {
- if (this.line.length) {
- this.minX = this.line[0].x;
- this.maxX = this.line[0].x;
- this.minY = this.line[0].y;
- this.maxY = this.line[0].y;
- this.line.forEach(it => {
- let x = it.x,
- y = it.y;
- if (x < this.minX) {
- this.minX = x;
- }
- if (y < this.minY) {
- this.minY = y;
- }
- if (x > this.maxX) {
- this.maxX = x;
- }
- if (y > this.maxY) {
- this.maxY = y;
- }
- });
- }
- return new SRect(
- this.minX,
- this.minY,
- this.maxX - this.minX,
- this.maxY - this.minY
- );
- }
-
- toData(): any {
- this.moveToOrigin()
- const Line = [{ x: this.line[0].x, y: this.line[0].y }, { x: this.line[1].x, y: this.line[1].y }];
- this.data.style.default.line = Line;
- this.data.style.default.lineWidth = this.lineWidth;
- this.data.style.default.lineStyle = this.lineStyle;
- this.data.style.default.strokeColor = this.strokeColor.value;
- this.data.style.default.fillColor = this.fillColor.value;
- return this.data
- }
-
- onDraw(painter: SPainter): void {
- this.sceneDis = painter.toPx(this.dis);
- painter.pen.lineWidth = painter.toPx(this.lineWidth);
- painter.pen.color = this.strokeColor;
- if (this.line.length == 2) {
-
- painter.pen.color = this.strokeColor;
- if (this.lineStyle == SLineStyle.Dashed) {
- painter.pen.lineDash = [
- painter.toPx(this.lineWidth * 3),
- painter.toPx(this.lineWidth * 7)
- ];
- } else if (this.lineStyle == SLineStyle.Dotted) {
- painter.pen.lineDash = [
- painter.toPx(2 * this.lineWidth),
- painter.toPx(2 * this.lineWidth)
- ];
- }
- if (this.selected && this.status == SItemStatus.Normal) {
- painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
- painter.shadow.shadowBlur = 10;
- painter.shadow.shadowColor = new SColor(`#00000033`);
- painter.shadow.shadowOffsetX = 5;
- painter.shadow.shadowOffsetY = 5;
- }
- painter.drawLine(this.line[0], this.line[1]);
- if (
- this.status == SItemStatus.Edit ||
- this.status == SItemStatus.Create
- ) {
-
- this.line.forEach((p, i): void => {
- painter.brush.color = this.fillColor;
- if (i == this.curIndex) {
- painter.brush.color = this.activeFillColor;
- }
- painter.drawCircle(p.x, p.y, painter.toPx(5));
- });
- }
- } else if (this.line.length == 1) {
- if (
- this.status == SItemStatus.Edit ||
- this.status == SItemStatus.Create
- ) {
-
- painter.brush.color = this.fillColor;
- painter.drawCircle(
- this.line[0].x,
- this.line[0].y,
- painter.toPx(5)
- );
- }
- }
- }
- }
|