| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |   <ThouCanvas | 
					
						
							| 
									
										
										
										
											2023-01-17 22:42:21 +00:00
										 |  |  |     @mousedown.left="draw_start" | 
					
						
							|  |  |  |     @mouseup.left="draw_finish" | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     @mousedown.right="drag_start" | 
					
						
							|  |  |  |     @mouseup.right="drag_finish" | 
					
						
							|  |  |  |     @mousemove="on_mousemove" | 
					
						
							| 
									
										
										
										
											2023-01-17 23:56:07 +00:00
										 |  |  |     @click.middle="remove_rect" | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     @dblclick.left="remove_rect" | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |   > | 
					
						
							| 
									
										
										
										
											2023-01-25 11:39:58 +00:00
										 |  |  |     <SVGRect | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |       v-for="(rect, index) in rectangles" | 
					
						
							| 
									
										
										
										
											2023-01-24 23:17:10 +00:00
										 |  |  |       :key="`rect-${index}`" | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |       :rectangle="rect" | 
					
						
							|  |  |  |     /> | 
					
						
							| 
									
										
										
										
											2023-01-25 11:39:58 +00:00
										 |  |  |     <SVGRect | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |       v-if="preview_visible" | 
					
						
							| 
									
										
										
										
											2023-01-17 23:58:46 +00:00
										 |  |  |       :focused="true" | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |       :rectangle="preview_rectangle" | 
					
						
							|  |  |  |     /> | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |   </ThouCanvas> | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { Vue, Options } from "vue-class-component"; | 
					
						
							| 
									
										
										
										
											2023-01-25 11:39:58 +00:00
										 |  |  | import { Vector2D, Rectangle } from "../rects/rectangles"; | 
					
						
							|  |  |  | import ThouCanvas from "../rects/ThouCanvas.vue"; | 
					
						
							|  |  |  | import SVGRect from "../rects/SVGRect.vue"; | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Options({ | 
					
						
							|  |  |  |   components: { | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |     ThouCanvas, | 
					
						
							| 
									
										
										
										
											2023-01-25 11:39:58 +00:00
										 |  |  |     SVGRect, | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |   }, | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2023-01-24 23:19:25 +00:00
										 |  |  | export default class extends Vue { | 
					
						
							| 
									
										
										
										
											2023-01-18 00:04:43 +00:00
										 |  |  |   private readonly min_rect_area = 300; | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   private drawing = false; | 
					
						
							|  |  |  |   private preview_corner1 = new Vector2D(); | 
					
						
							|  |  |  |   private preview_corner2 = new Vector2D(); | 
					
						
							|  |  |  |   private dragging = false; | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |   private drag_rect?: Rectangle; | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   private drag_origin = new Vector2D(); | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |   public rectangles: Rectangle[] = []; | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   private get preview_visible(): boolean { | 
					
						
							|  |  |  |     return this.drawing || this.dragging; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private get preview_rectangle(): Rectangle { | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |     return new Rectangle(this.preview_corner1, this.preview_corner2); | 
					
						
							| 
									
										
										
										
											2023-01-19 00:27:38 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |   private pop_rectangle(point: Vector2D): Rectangle | undefined { | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |     const idx = this.rectangles.findIndex((rect) => rect.contains(point)); | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (idx === -1) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |     return this.rectangles.splice(idx, 1)[0]; | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |   private draw_start(event: MouseEvent, point: Vector2D) { | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     if (this.preview_visible) { | 
					
						
							| 
									
										
										
										
											2023-01-17 22:15:12 +00:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     this.drawing = true; | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |     this.preview_corner1 = point; | 
					
						
							|  |  |  |     this.preview_corner2 = point; | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   private draw_finish() { | 
					
						
							|  |  |  |     if (!this.drawing) { | 
					
						
							| 
									
										
										
										
											2023-01-17 22:42:21 +00:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     this.drawing = false; | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:50:25 +00:00
										 |  |  |     if (this.preview_rectangle.area < this.min_rect_area) { | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-17 23:50:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |     this.rectangles.push(this.preview_rectangle); | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |   private drag_start(event: MouseEvent, point: Vector2D) { | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     if (this.preview_visible) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |     this.drag_rect = this.pop_rectangle(point); | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |     if (this.drag_rect === undefined) { | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.dragging = true; | 
					
						
							|  |  |  |     this.drag_origin = point; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |     this.preview_corner1 = this.drag_rect.origin; | 
					
						
							|  |  |  |     this.preview_corner2 = this.drag_rect.corner; | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |   private drag_finish() { | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     if (!this.dragging) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.dragging = false; | 
					
						
							| 
									
										
										
										
											2023-01-23 14:38:49 +00:00
										 |  |  |     this.rectangles.push(this.preview_rectangle); | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |   private on_mousemove(event: MouseEvent, point: Vector2D) { | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     if (this.drawing) { | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |       this.preview_corner2 = point; | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |     } else if (this.dragging && this.drag_rect) { | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |       const movement = point.minus(this.drag_origin); | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 23:42:07 +00:00
										 |  |  |       this.preview_corner1 = this.drag_rect.origin.plus(movement); | 
					
						
							|  |  |  |       this.preview_corner2 = this.drag_rect.corner.plus(movement); | 
					
						
							| 
									
										
										
										
											2023-01-17 23:34:42 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |   private remove_rect(event: MouseEvent, point: Vector2D) { | 
					
						
							| 
									
										
										
										
											2023-01-19 00:27:21 +00:00
										 |  |  |     if (this.preview_visible) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  |     this.pop_rectangle(point); | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style scoped> | 
					
						
							| 
									
										
										
										
											2023-01-23 22:33:39 +00:00
										 |  |  | * { | 
					
						
							| 
									
										
										
										
											2023-01-17 18:25:56 +00:00
										 |  |  |   cursor: crosshair; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | </style> |