Undo1.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div>
  3. <el-button @click="addCircle">Circle</el-button>
  4. <el-button @click="addRect">Rect</el-button>
  5. <el-button @click="deleteItem">Delete</el-button>
  6. <el-button @click="undo">Undo</el-button>
  7. <el-button @click="redo">Redo</el-button>
  8. <canvas id="undoFrame" width="800" height="400" />
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { SMouseEvent, SUndoStack } from "@saga-web/base";
  13. import { SColor, SPainter, SRect } from "@saga-web/draw";
  14. import { SGraphItem, SGraphScene, SGraphView } from "@saga-web/graph";
  15. import {SGraphCommand} from "@saga-web/graph/lib";
  16. import {SUndoCommand} from "@saga-web/base/lib";
  17. import {SPoint} from "@saga-web/draw/lib";
  18. class RectItem extends SGraphItem {
  19. width = 100;
  20. height = 100;
  21. constructor(parent: SGraphItem | null) {
  22. super(parent);
  23. this.moveable = true;
  24. this.selectable = true;
  25. this.selected = true;
  26. }
  27. boundingRect(): SRect {
  28. return new SRect(0, 0, this.width, this.height);
  29. }
  30. onDraw(canvas: SPainter): void {
  31. canvas.pen.color = SColor.Blue;
  32. canvas.pen.lineWidth = 5;
  33. canvas.brush.color = this.selected ? SColor.Red : SColor.Green;
  34. canvas.drawRect(0, 0, this.width, this.height)
  35. }
  36. }
  37. class CircleItem extends SGraphItem {
  38. r = 50;
  39. constructor(parent: SGraphItem | null) {
  40. super(parent);
  41. this.moveable = true;
  42. this.selectable = true;
  43. }
  44. boundingRect(): SRect {
  45. return new SRect(0, 0, this.r * 2, this.r * 2);
  46. }
  47. onDraw(canvas: SPainter): void {
  48. canvas.pen.color = SColor.Blue;
  49. canvas.pen.lineWidth = 5;
  50. canvas.brush.color = this.selected ? SColor.Red : SColor.Green;
  51. canvas.drawCircle(this.r, this.r, this.r);
  52. }
  53. }
  54. class SScene extends SGraphScene {
  55. undoStack = new SUndoStack();
  56. /** 定义命令 */
  57. cmd = 0;
  58. constructor() {
  59. super();
  60. }
  61. onMouseUp(event: SMouseEvent): boolean {
  62. switch(this.cmd) {
  63. case 1:
  64. this.addCircle(event.x, event.y);
  65. break;
  66. case 2:
  67. this.addRect(event.x, event.y);
  68. break;
  69. default:
  70. super.onMouseUp(event);
  71. }
  72. this.cmd = 0;
  73. return false
  74. }
  75. private addCircle(x: number, y: number): void {
  76. let item = new CircleItem(null);
  77. item.moveTo(x - 50, y - 50);
  78. this.addItem(item);
  79. this.undoStack.push(new SGraphAddCommand(this, item));
  80. item.connect("onMove", this, this.onItemMove.bind(this));
  81. }
  82. private addRect(x: number, y: number): void {
  83. let item = new RectItem(null);
  84. item.moveTo(x - 50, y - 50);
  85. this.addItem(item);
  86. this.undoStack.push(new SGraphAddCommand(this, item));
  87. item.connect("onMove", this, this.onItemMove.bind(this));
  88. }
  89. onItemMove(item: SGraphItem, ...arg: any): void {
  90. this.undoStack.push(new SGraphMoveCommand(this, item, arg[0][0] as SPoint, arg[0][1] as SPoint));
  91. }
  92. }
  93. class TestView extends SGraphView {
  94. constructor() {
  95. super("undoFrame");
  96. }
  97. }
  98. class SGraphAddCommand extends SGraphCommand {
  99. item: SGraphItem;
  100. parent: SGraphItem | null;
  101. constructor(scene: SGraphScene, item: SGraphItem) {
  102. super(scene);
  103. this.item = item;
  104. this.parent = item.parent;
  105. }
  106. mergeWith(command: SUndoCommand): boolean {
  107. return false;
  108. }
  109. redo(): void {
  110. this.item.parent = this.parent;
  111. this.parent.update();
  112. }
  113. undo(): void {
  114. this.item.parent = null;
  115. this.parent.update();
  116. }
  117. }
  118. class SGraphDeleteCommand extends SGraphCommand {
  119. mergeWith(command: SUndoCommand): boolean {
  120. return false;
  121. }
  122. redo(): void {
  123. }
  124. undo(): void {
  125. }
  126. }
  127. class SGraphMoveCommand extends SGraphCommand {
  128. item: SGraphItem;
  129. old: SPoint;
  130. pos: SPoint;
  131. constructor(scene: SGraphScene, item: SGraphItem, old: SPoint, pos: SPoint) {
  132. super(scene);
  133. this.item = item;
  134. this.old = old;
  135. this.pos = pos;
  136. }
  137. mergeWith(command: SUndoCommand): boolean {
  138. if (command instanceof SGraphMoveCommand && command.item == this.item) {
  139. command.pos = this.pos;
  140. return true;
  141. }
  142. return false;
  143. }
  144. redo(): void {
  145. this.item.pos = new SPoint(this.pos.x, this.pos.y);
  146. this.item.update();
  147. }
  148. undo(): void {
  149. this.item.pos = new SPoint(this.old.x, this.old.y);
  150. this.item.update();
  151. }
  152. }
  153. export default {
  154. data() {
  155. return {
  156. scene: new SScene(),
  157. }
  158. },
  159. mounted(): void {
  160. let view = new TestView();
  161. view.scene = this.scene;//new SScene(); //this.data.scene;
  162. },
  163. methods: {
  164. addCircle() {
  165. this.scene.cmd = 1;
  166. },
  167. addRect() {
  168. this.scene.cmd = 2;
  169. },
  170. deleteItem() {
  171. },
  172. undo() {
  173. console.log("undo");
  174. this.scene.undoStack.undo();
  175. },
  176. redo() {
  177. this.scene.undoStack.redo();
  178. }
  179. }
  180. }
  181. </script>
  182. <style scoped>
  183. </style>