|
1 | 1 | /* eslint-disable no-redeclare */
|
2 |
| -import { Color, Length, Observable, PercentLength, Visibility } from '@nativescript/core'; |
| 2 | +import { Color, HorizontalAlignment, Length, Observable, PercentLength, Utils, VerticalAlignment, Visibility } from '@nativescript/core'; |
3 | 3 | import { booleanConverter } from '@nativescript/core/ui/core/view-base';
|
4 | 4 | import { Canvas, CanvasView, Cap, Join, Paint, Style } from '../canvas';
|
5 | 5 | import { parseCap, parseDashEffect, parseJoin, parseShadow, parseType } from '../utils';
|
@@ -172,13 +172,47 @@ export default abstract class Shape extends Observable {
|
172 | 172 | })
|
173 | 173 | shadow: Shadow;
|
174 | 174 | @stringProperty({ nonPaintProp: true }) visibility: Visibility = 'visible';
|
| 175 | + @stringProperty({ nonPaintProp: true }) horizontalAlignment: HorizontalAlignment & 'middle'; |
| 176 | + @stringProperty({ nonPaintProp: true }) verticalAlignment: VerticalAlignment & 'center'; |
| 177 | + protected handleAlignment = false; |
175 | 178 |
|
176 | 179 | abstract drawOnCanvas(canvas: Canvas, parent: CanvasView): void;
|
177 | 180 |
|
178 |
| - drawMyShapeOnCanvas(canvas: Canvas, parent: CanvasView) { |
| 181 | + drawMyShapeOnCanvas(canvas: Canvas, parent: CanvasView, width: number, height: number) { |
179 | 182 | if (this.visibility !== 'visible') {
|
180 | 183 | return;
|
181 | 184 | }
|
| 185 | + if (!this.handleAlignment) { |
| 186 | + const paddingLeft = parent.effectivePaddingLeft + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderLeftWidth); |
| 187 | + const paddingRight = parent.effectivePaddingRight + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderRightWidth); |
| 188 | + |
| 189 | + const paddingTop = parent.effectivePaddingTop + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderTopWidth); |
| 190 | + const paddingBottom = parent.effectivePaddingBottom + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderBottomWidth); |
| 191 | + canvas.save(); |
| 192 | + if (paddingLeft > 0) { |
| 193 | + canvas.translate(paddingLeft, 0); |
| 194 | + } |
| 195 | + if (paddingRight > 0) { |
| 196 | + canvas.translate(-paddingRight, 0); |
| 197 | + } |
| 198 | + if (paddingTop > 0) { |
| 199 | + canvas.translate(0, paddingTop); |
| 200 | + } |
| 201 | + if (paddingBottom > 0) { |
| 202 | + canvas.translate(0, -paddingBottom); |
| 203 | + } |
| 204 | + if (this.horizontalAlignment === 'right') { |
| 205 | + canvas.translate(width, 0); |
| 206 | + } else if (this.horizontalAlignment === 'center' || this.horizontalAlignment === 'middle') { |
| 207 | + canvas.translate(width / 2, 0); |
| 208 | + } |
| 209 | + if (this.verticalAlignment === 'bottom') { |
| 210 | + canvas.translate(0, height); |
| 211 | + } else if (this.verticalAlignment === 'center' || this.verticalAlignment === 'middle') { |
| 212 | + canvas.translate(0, height / 2); |
| 213 | + } |
| 214 | + } |
| 215 | + |
182 | 216 | const paint = this.paint;
|
183 | 217 | // console.log('drawMyShapeOnCanvas', paint.getColor(), this.strokeColor, this.fillColor);
|
184 | 218 | if (this.strokeColor || this.fillColor) {
|
@@ -209,5 +243,8 @@ export default abstract class Shape extends Observable {
|
209 | 243 | } else {
|
210 | 244 | this.drawOnCanvas(canvas, parent);
|
211 | 245 | }
|
| 246 | + if (!this.handleAlignment) { |
| 247 | + canvas.restore(); |
| 248 | + } |
212 | 249 | }
|
213 | 250 | }
|
0 commit comments