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 DisplayCalculator from '../setting/DisplayCalculator';
17import { Log } from './Log';
18
19export class ComponentPosition {
20  private static TAG: string = '[ComponentPosition]:'
21  private static shutterButtonWidth: number = 76
22  private static shutterButtonWidthVideo: number = 56
23  private static footBarComponentWidth: number = 98
24  private static footBarInPreviewMaxMargin: number = 98
25  private static tarBarHeight: number = 360
26  private static tarBarWidth: number = 48
27  private static tabBarInPreviewMaxMargin: number = 48
28  private static tabBarLeftDistance: number = 10
29  private static tabBarBottomDistance: number = 40
30  private static zoomViewComponentWidth: number = 82
31  private static zoomViewToPreview: number = 75
32  private static zoomViewWidthVideo: number = 60
33  private static zoomViewRightDistance: number = 4
34  private static foldControlHeight: number = 650
35  private static controlItemHeight: number = 32
36
37  public static previewPosition(screenWidth: number, screenHeight: number, previewWidth: number, previewHeight: number) {
38    Log.info(`${this.TAG} previewPosition `)
39    let position = (Math.abs(screenWidth - previewWidth)  < 1) ? { x: 0, y: (screenHeight - previewHeight) / 2}
40                                                       : { x: (screenWidth - previewWidth) / 2, y: 0 }
41    return position
42  }
43
44  public static previewTabletPosition(screenWidth: number, screenHeight: number, previewWidth: number, previewHeight: number) {
45    Log.info(`${this.TAG} previewTabletPosition ` + previewHeight + previewWidth)
46    if ((screenHeight == previewHeight &&  previewWidth > previewHeight)) {
47      return {x: 0, y: 0}
48    }
49    let position = (Math.abs(screenWidth - previewWidth)  < 1) ? { x: 0, y: (screenHeight - previewHeight) / 2}
50                                                               : { x: (screenWidth - previewWidth) / 2, y: 0 }
51    return position
52  }
53
54  public static footBarPosition(screenWidth: number, screenHeight: number, previewWidth: number, previewHeight: number) {
55    Log.info(`${this.TAG} footBarPosition `)
56    if (screenWidth == previewWidth && (3 * previewWidth > 4 * previewHeight)) {
57      let previewSize = DisplayCalculator.calcSurfaceDisplaySize(screenWidth, screenHeight, 4, 3)
58      previewWidth = previewSize.width
59      previewHeight = previewSize.height
60    }
61    let position = screenWidth <= previewWidth + this.footBarInPreviewMaxMargin * 2 ?
62      { x: (screenWidth / 2 + previewWidth / 2 - this.footBarComponentWidth), y: 0 } :
63      { x: (screenWidth * 3 / 4 + previewWidth / 4 - this.footBarComponentWidth / 2), y: 0}
64    return position
65  }
66
67  public static tabBarPosition(screenWidth: number, screenHeight: number, previewWidth: number, previewHeight: number) {
68    Log.info(`${this.TAG} tabBarPosition `)
69    if (screenWidth == previewWidth && (3 * previewWidth > 4 * previewHeight)) {
70      let previewSize = DisplayCalculator.calcSurfaceDisplaySize(screenWidth, screenHeight, 4, 3)
71      previewWidth = previewSize.width
72      previewHeight = previewSize.height
73    }
74    let xPosition: number = (screenWidth == previewWidth) ? this.tabBarLeftDistance :
75      ((screenWidth >= previewWidth + this.tabBarInPreviewMaxMargin * 2) ?
76      ((screenWidth - previewWidth) / 4) - this.tarBarWidth / 2  :
77      ((screenWidth - previewWidth) / 2) + this.tabBarLeftDistance)
78    let yPosition: number = (screenWidth * 9 <= screenHeight * 16) ?
79      (screenHeight / 2 + screenWidth * 9 / 32 - this.tarBarHeight) :
80      (screenHeight - this.tarBarHeight - this.tabBarBottomDistance)
81    let position = { x: xPosition, y: 44}
82    return position
83  }
84
85  public static zoomViewPosition(screenWidth: number, screenHeight: number, previewWidth: number, previewHeight: number,
86                                 videoState: string) {
87    Log.info(`${this.TAG} zoomViewPosition `)
88    let zoomViewWidth: number
89    let shutterButtonWidth: number
90    if (screenWidth == previewWidth && (3 * previewWidth > 4 * previewHeight)) {
91      let previewSize = DisplayCalculator.calcSurfaceDisplaySize(screenWidth, screenHeight, 4, 3)
92      previewWidth = previewSize.width
93      previewHeight = previewSize.height
94    }
95    if (videoState === "beforeTakeVideo") {
96      zoomViewWidth = this.zoomViewComponentWidth
97      shutterButtonWidth = this.shutterButtonWidth
98    } else {
99      zoomViewWidth = this.zoomViewWidthVideo
100      shutterButtonWidth = this.shutterButtonWidthVideo
101    }
102    let xPosition: number = 0;
103    if (screenWidth < previewWidth + this.footBarInPreviewMaxMargin * 2) {
104      xPosition = (screenWidth / 2 + previewWidth / 2 - ((this.footBarComponentWidth + this.zoomViewComponentWidth
105      + zoomViewWidth + shutterButtonWidth) / 2 + this.zoomViewRightDistance))
106    } else if (screenWidth < previewWidth + zoomViewWidth * 4 + shutterButtonWidth * 2) {
107      xPosition = screenWidth / 2 + previewWidth / 2 - this.zoomViewToPreview
108    } else {
109      xPosition = screenWidth * 5 / 8 + previewWidth * 3 / 8 - shutterButtonWidth / 4 - this.zoomViewComponentWidth / 2
110    }
111    let position = { x: xPosition, y: 0 }
112    return position
113  }
114
115  public static getControlHeight(width: number, height: number) {
116    let position = this.isUnfoldControl(width, height) ? this.controlItemHeight * 5 : this.controlItemHeight * 3
117    return position
118  }
119
120  public static isUnfoldControl(width: number, height: number) {
121    Log.info(`${this.TAG} isUnfoldControl `)
122    let previewSize = DisplayCalculator.calcSurfaceDisplaySize(width, height, 4, 3)
123    return (previewSize.height > this.foldControlHeight)
124  }
125
126  public static getShutterButtonMargin(screenWidth: number, screenHeight: number, previewHeight: number) {
127    Log.info(`${this.TAG} getShutterButtonMargin `)
128    if (previewHeight < 400) {
129      return 24
130    }
131    let previewSize = DisplayCalculator.calcSurfaceDisplaySize(screenWidth, screenHeight, 4, 3)
132    let x = previewSize.height * 3 / 100 + 24
133    return x
134  }
135
136  public static getFootBarHeight(screenWidth: number, screenHeight: number, previewHeight: number) {
137    let x =  this.getShutterButtonMargin(screenWidth, screenHeight, previewHeight) * 2 + 164
138    return x
139  }
140
141  public static getFootBarMargin(screenWidth: number, screenHeight: number, previewHeight: number) {
142    Log.info(`${this.TAG} getFootBarMargin `)
143    if (previewHeight < 400) {
144      return 0
145    }
146    let previewSize = DisplayCalculator.calcSurfaceDisplaySize(screenWidth, screenHeight, 4, 3)
147    let x =  previewSize.height * 12 / 100 - 48
148    return x
149  }
150
151  public static getFootBarPosition(previewHeight: number) {
152    return (previewHeight < 400) ? { x: 0, y: -30 } : { x: 0, y: 0 }
153  }
154
155  public static getControlPosition(previewHeight: number) {
156    return (previewHeight < 400) ? { x: 0, y: -40 } : { x: 0, y: 0 }
157  }
158}