1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Log } from '../../utils/Log'; 17import { Dispatch, OhCombinedState } from '../../redux/store'; 18import { getStore } from '../../redux/store'; 19 20class StateStruct { 21 xComponentWidth: number = 0; 22 xComponentHeight: number = 0; 23} 24 25class AssistiveGridViewDispatcher { 26 private mDispatch: Dispatch = (data) => data; 27 28 public setDispatch(dispatch: Dispatch) { 29 this.mDispatch = dispatch; 30 } 31} 32 33@Component 34export struct AssistiveGridView { 35 private TAG: string = '[AssistiveGridView]:'; 36 private mGlobalAlpha: number = 0.5; 37 private mLineWidth: number = 0.5; 38 private mShadowColor: string = 'argb(#7F000000)'; 39 private mShadowOffsetX: number = 0; 40 private mShadowOffsetY: number = 0; 41 private mStrokeStyle: string = '#FFFFFF'; 42 private scaleLen: number = 4; 43 private settings: RenderingContextSettings = new RenderingContextSettings(true); 44 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 45 private mAction: AssistiveGridViewDispatcher = new AssistiveGridViewDispatcher(); 46 @State state: StateStruct = new StateStruct(); 47 48 aboutToAppear(): void { 49 Log.info(`${this.TAG} aboutToAppear invoke E`); 50 getStore().subscribe((state: OhCombinedState) => { 51 this.state = { 52 xComponentWidth: state.previewReducer.xComponentWidth, 53 xComponentHeight: state.previewReducer.xComponentHeight 54 }; 55 }, (dispatch: Dispatch) => { 56 this.mAction.setDispatch(dispatch); 57 }); 58 Log.info(`${this.TAG} aboutToAppear invoke X`) 59 } 60 61 build() { 62 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 63 Canvas(this.context) 64 .width('100%') 65 .height('100%') 66 .onReady(() => { 67 this.context.clearRect(0, 0, this.state.xComponentWidth, this.state.xComponentHeight) 68 this.drawRuler() 69 }) 70 } 71 .width(this.state.xComponentWidth) 72 .height(this.state.xComponentHeight) 73 } 74 75 drawRuler(): void { 76 let xCWidth = this.state.xComponentWidth 77 let xCHeight = this.state.xComponentHeight 78 let curX = xCWidth / 3 79 let curY = xCHeight / 3 80 this.context.beginPath() 81 this.context.globalAlpha = this.mGlobalAlpha 82 this.context.lineWidth = this.mLineWidth 83 this.context.strokeStyle = this.mStrokeStyle 84 this.context.shadowColor = this.mShadowColor 85 this.context.shadowOffsetX = this.mShadowOffsetX 86 this.context.shadowOffsetY = this.mShadowOffsetY 87 for (let i = 1; i <= this.scaleLen; i++) { 88 if (i <= this.scaleLen / 2) { 89 this.context.moveTo(curX * i, 0) 90 this.context.lineTo(curX * i, xCHeight) 91 } else { 92 let n = Math.floor(i / 2) 93 this.context.moveTo(0, curY * n) 94 this.context.lineTo(xCWidth * n, curY * n) 95 } 96 } 97 this.context.closePath() 98 this.context.stroke() 99 } 100}