|
| 1 | +/* global Proxy _ */ |
| 2 | +/* |
| 3 | + * Copyright the Collabora Online contributors. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: MPL-2.0 |
| 6 | + * |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 8 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | + */ |
| 11 | + |
| 12 | +class ShapeHandleDiagramSubSection extends CanvasSectionObject { |
| 13 | + processingOrder: number = |
| 14 | + app.CSections.DefaultForDocumentObjects.processingOrder; |
| 15 | + drawingOrder: number = |
| 16 | + app.CSections.DefaultForDocumentObjects.drawingOrder + 1; // Handle events before the parent section. |
| 17 | + zIndex: number = app.CSections.DefaultForDocumentObjects.zIndex; |
| 18 | + documentObject: boolean = true; |
| 19 | + filledCombine: Path2D = new Path2D(); |
| 20 | + filledCombineUsed: boolean = false; |
| 21 | + |
| 22 | + constructor( |
| 23 | + parentHandlerSection: ShapeHandlesSection, |
| 24 | + sectionName: string, |
| 25 | + size: number[], |
| 26 | + documentPosition: cool.SimplePoint, |
| 27 | + ownInfo: any, |
| 28 | + ) { |
| 29 | + super(sectionName); |
| 30 | + |
| 31 | + this.size = size; |
| 32 | + |
| 33 | + this.sectionProperties.position = documentPosition.clone(); |
| 34 | + this.sectionProperties.parentHandlerSection = parentHandlerSection; |
| 35 | + this.sectionProperties.ownInfo = ownInfo; |
| 36 | + this.sectionProperties.mouseIsInside = false; |
| 37 | + this.sectionProperties.lastDraggingDistance = null; |
| 38 | + this.sectionProperties.mapPane = <HTMLElement>( |
| 39 | + document.querySelectorAll('.leaflet-map-pane')[0] |
| 40 | + ); |
| 41 | + this.sectionProperties.cursorStyle = 'grab'; //'pointer'; |
| 42 | + |
| 43 | + app.events.on( |
| 44 | + 'TextCursorVisibility', |
| 45 | + this.onTextCursorVisibility.bind(this), |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + onTextCursorVisibility(event: any): void { |
| 50 | + if (event.detail.visible) { |
| 51 | + this.setShowSection(false); |
| 52 | + this.interactable = false; |
| 53 | + } else { |
| 54 | + this.setShowSection(true); |
| 55 | + this.interactable = true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + onInitialize(): void { |
| 60 | + this.setPosition( |
| 61 | + this.sectionProperties.position.pX, |
| 62 | + this.sectionProperties.position.pY, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + onMouseEnter(point: cool.SimplePoint, e: MouseEvent): void { |
| 67 | + app.map.dontHandleMouse = true; |
| 68 | + this.sectionProperties.mapPane.style.cursor = |
| 69 | + this.sectionProperties.cursorStyle; |
| 70 | + } |
| 71 | + |
| 72 | + private createRoundRect( |
| 73 | + path: Path2D, |
| 74 | + x: number, |
| 75 | + y: number, |
| 76 | + w: number, |
| 77 | + h: number, |
| 78 | + r: number, |
| 79 | + ): void { |
| 80 | + path.moveTo(x, y + r); |
| 81 | + path.arcTo(x, y, x + r, y, r); |
| 82 | + path.lineTo(x + w - r, y); |
| 83 | + path.arcTo(x + w, y, x + w, y + r, r); |
| 84 | + path.lineTo(x + w, y + h - r); |
| 85 | + path.arcTo(x + w, y + h, x + w - r, y + h, r); |
| 86 | + path.lineTo(x + r, y + h); |
| 87 | + path.arcTo(x, y + h, x, y + h - r, r); |
| 88 | + path.lineTo(x, y + r); |
| 89 | + } |
| 90 | + |
| 91 | + private modifyColor(colour: string, delta: number): string { |
| 92 | + var hashUsed: boolean = false; |
| 93 | + var transparency: string = ''; |
| 94 | + if ('#' == colour[0]) { |
| 95 | + hashUsed = true; |
| 96 | + colour = colour.slice(1); |
| 97 | + } |
| 98 | + if (colour.length > 6) { |
| 99 | + transparency = colour.slice(6); |
| 100 | + colour = colour.slice(0, 6); |
| 101 | + } |
| 102 | + const number: number = parseInt(colour, 16); |
| 103 | + const r: number = Math.min(255, Math.max(0, (number >> 16) + delta)); |
| 104 | + const g: number = Math.min( |
| 105 | + 255, |
| 106 | + Math.max(0, ((number >> 8) & 0x00ff) + delta), |
| 107 | + ); |
| 108 | + const b: number = Math.min(255, Math.max(0, (number & 0x0000ff) + delta)); |
| 109 | + colour = ((r << 16) + (g << 8) + b).toString(16); |
| 110 | + return (hashUsed ? '#' : '') + colour.padStart(6, '0') + transparency; |
| 111 | + } |
| 112 | + |
| 113 | + onDraw(frameCount?: number, elapsedTime?: number): void { |
| 114 | + const origLineWidth: number = this.context.lineWidth; |
| 115 | + const halfWidthPx: number = |
| 116 | + this.sectionProperties.ownInfo.halfWidth * app.twipsToPixels; |
| 117 | + const halfWidthPy: number = |
| 118 | + this.sectionProperties.ownInfo.halfHeight * app.twipsToPixels; |
| 119 | + |
| 120 | + // create outerBound |
| 121 | + var outerBound = new Path2D(); |
| 122 | + this.createRoundRect( |
| 123 | + outerBound, |
| 124 | + 0, |
| 125 | + 0, |
| 126 | + this.size[0], |
| 127 | + this.size[1], |
| 128 | + halfWidthPx, |
| 129 | + ); |
| 130 | + |
| 131 | + // create innerBound |
| 132 | + var innerBound = new Path2D(); |
| 133 | + const innerRelative: number = 0.5; |
| 134 | + this.createRoundRect( |
| 135 | + innerBound, |
| 136 | + halfWidthPx * innerRelative, |
| 137 | + halfWidthPy * (1.0 + innerRelative), |
| 138 | + this.size[0] - halfWidthPx * (2 * innerRelative), |
| 139 | + this.size[1] - halfWidthPy * (1.0 + 2.0 * innerRelative), |
| 140 | + halfWidthPx * (1.0 - innerRelative), |
| 141 | + ); |
| 142 | + |
| 143 | + // reset filledCombine & create combination for fill |
| 144 | + this.filledCombine = new Path2D(); |
| 145 | + this.filledCombineUsed = true; |
| 146 | + this.filledCombine.addPath(outerBound); |
| 147 | + this.filledCombine.addPath(innerBound); |
| 148 | + |
| 149 | + // background gradient |
| 150 | + const selectionBackgroundGradient = this.context.createLinearGradient( |
| 151 | + 0, |
| 152 | + 0, |
| 153 | + this.size[0], |
| 154 | + this.size[1], |
| 155 | + ); |
| 156 | + selectionBackgroundGradient.addColorStop( |
| 157 | + 0, |
| 158 | + app.selectionBackgroundGradient[0], |
| 159 | + ); |
| 160 | + selectionBackgroundGradient.addColorStop( |
| 161 | + 0.5, |
| 162 | + app.selectionBackgroundGradient[1], |
| 163 | + ); |
| 164 | + selectionBackgroundGradient.addColorStop( |
| 165 | + 1, |
| 166 | + app.selectionBackgroundGradient[2], |
| 167 | + ); |
| 168 | + |
| 169 | + // create colors |
| 170 | + const color: string = app.selectionBackgroundGradient[0]; |
| 171 | + const color_dark: string = this.modifyColor(color, -32); |
| 172 | + |
| 173 | + // draw fill |
| 174 | + this.context.fillStyle = selectionBackgroundGradient; |
| 175 | + this.filledCombine.closePath(); |
| 176 | + this.context.fill(this.filledCombine, 'evenodd'); |
| 177 | + |
| 178 | + // draw outer |
| 179 | + this.context.strokeStyle = color_dark; |
| 180 | + this.context.lineWidth = 2; |
| 181 | + this.context.stroke(outerBound); |
| 182 | + |
| 183 | + // draw inner |
| 184 | + this.context.stroke(innerBound); |
| 185 | + |
| 186 | + this.context.lineWidth = origLineWidth; |
| 187 | + } |
| 188 | + |
| 189 | + onClick(point: cool.SimplePoint, e: MouseEvent): void { |
| 190 | + app.map.sendUnoCommand('.uno:EditDiagram'); |
| 191 | + } |
| 192 | + |
| 193 | + isHit(point: number[]): boolean { |
| 194 | + if (!super.isHit(point)) { |
| 195 | + // use baseclass method to see if we are outside local range (position, size) |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + if (this.filledCombineUsed) { |
| 200 | + // if we have the geometry from paint, use it for HitTest |
| 201 | + return this.context.isPointInPath( |
| 202 | + this.filledCombine, |
| 203 | + point[0] - this.myTopLeft[0], |
| 204 | + point[1] - this.myTopLeft[1], |
| 205 | + 'evenodd', |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + // if not, check for outer bounds defined by offsets from outside |
| 210 | + const halfWidthPx: number = |
| 211 | + this.sectionProperties.ownInfo.halfWidth * app.twipsToPixels; |
| 212 | + const halfWidthPy: number = |
| 213 | + this.sectionProperties.ownInfo.halfHeight * app.twipsToPixels; |
| 214 | + |
| 215 | + if ( |
| 216 | + point[0] <= this.myTopLeft[0] + halfWidthPx || |
| 217 | + point[0] >= this.myTopLeft[0] + this.size[0] - halfWidthPx || |
| 218 | + point[1] <= this.myTopLeft[1] + 2 * halfWidthPy || |
| 219 | + point[1] >= this.myTopLeft[1] + this.size[1] - halfWidthPy |
| 220 | + ) { |
| 221 | + return true; |
| 222 | + } |
| 223 | + |
| 224 | + return false; |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +app.definitions.shapeHandleDiagramSubSection = ShapeHandleDiagramSubSection; |
0 commit comments