|
| 1 | +/* |
| 2 | + * If not stated otherwise in this file or this component's LICENSE file the |
| 3 | + * following copyright and licenses apply: |
| 4 | + * |
| 5 | + * Copyright 2020 Metrological |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the License); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import Lightning from '../Lightning' |
| 21 | + |
| 22 | +export default class SubtitleComponent extends Lightning.Component { |
| 23 | + static _template() { |
| 24 | + return { |
| 25 | + visible: false, |
| 26 | + rect: true, |
| 27 | + color: 0x90000000, |
| 28 | + shader: { type: Lightning.shaders.RoundedRectangle, radius: 5 }, |
| 29 | + Text: { |
| 30 | + y: 5, |
| 31 | + x: 20, |
| 32 | + text: { |
| 33 | + fontColor: 0xffffffff, |
| 34 | + fontSize: 38, |
| 35 | + lineHeight: 38 * 1.4, |
| 36 | + textAlign: 'center', |
| 37 | + wordWrap: true, |
| 38 | + maxLines: 3, |
| 39 | + shadow: true, |
| 40 | + shadowColor: 0xff333333, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + _init() { |
| 47 | + this._textTextureDefaults = new Lightning.textures.TextTexture(this.stage).cloneArgs() |
| 48 | + |
| 49 | + this.tag('Text').on('txLoaded', ({ _source }) => { |
| 50 | + this.w = _source.w + this.tag('Text').x * 2 |
| 51 | + this.h = _source.h |
| 52 | + this.position() |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + get textFormat() { |
| 57 | + const textTag = this.tag('Text').text |
| 58 | + return { |
| 59 | + fontFace: textTag.fontFace || 'sans-serif', |
| 60 | + fontSize: textTag.fontSize, |
| 61 | + lineHeight: textTag.lineHeight, |
| 62 | + textAlign: textTag.textAlign, |
| 63 | + wordWrap: true, |
| 64 | + maxLines: textTag.maxLines, |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + show() { |
| 69 | + this.visible = true |
| 70 | + } |
| 71 | + |
| 72 | + hide() { |
| 73 | + this.visible = false |
| 74 | + } |
| 75 | + |
| 76 | + position() { |
| 77 | + this.x = this._calculateX(this.xPos) |
| 78 | + this.y = this._calculateY(this.yPos) |
| 79 | + } |
| 80 | + |
| 81 | + set viewportW(v) { |
| 82 | + this._viewportW = v |
| 83 | + this.x = this._calculateX(this.xPos) |
| 84 | + } |
| 85 | + |
| 86 | + get viewportW() { |
| 87 | + return this._viewportW || this.application.finalW |
| 88 | + } |
| 89 | + |
| 90 | + set viewportH(v) { |
| 91 | + this._viewportH = v |
| 92 | + this.y = this._calculateY(this.yPos) |
| 93 | + } |
| 94 | + |
| 95 | + get viewportH() { |
| 96 | + return this._viewportH || this.application.finalH |
| 97 | + } |
| 98 | + |
| 99 | + _calculateX(x) { |
| 100 | + if (x === 'center') { |
| 101 | + x = (this.viewportW - this.finalW) / 2 |
| 102 | + } else if (x === 'left') { |
| 103 | + x = 60 |
| 104 | + } else if (x === 'right') { |
| 105 | + x = this.viewportW - this.finalW - 60 |
| 106 | + } |
| 107 | + return x |
| 108 | + } |
| 109 | + |
| 110 | + set xPos(v) { |
| 111 | + this._x = v |
| 112 | + this.x = this._calculateX(v) |
| 113 | + } |
| 114 | + |
| 115 | + get xPos() { |
| 116 | + return this._x || 'center' |
| 117 | + } |
| 118 | + |
| 119 | + _calculateY(y) { |
| 120 | + if (y === 'center') { |
| 121 | + return (this.viewportH - this.finalH) / 2 |
| 122 | + } else if (y === 'top') { |
| 123 | + return 60 |
| 124 | + } else if (y === 'bottom') { |
| 125 | + return this.viewportH - this.finalH - 60 |
| 126 | + } |
| 127 | + return y |
| 128 | + } |
| 129 | + |
| 130 | + set yPos(v) { |
| 131 | + this._y = v |
| 132 | + this.y = this._calculateY(v) |
| 133 | + } |
| 134 | + |
| 135 | + get yPos() { |
| 136 | + return this._y || 'bottom' |
| 137 | + } |
| 138 | + |
| 139 | + set fontFamily(v) { |
| 140 | + this.tag('Text').text.fontFace = v |
| 141 | + } |
| 142 | + |
| 143 | + set fontSize(v) { |
| 144 | + this.tag('Text').text.fontSize = v |
| 145 | + this.tag('Text').text.lineHeight = v * 1.3 |
| 146 | + } |
| 147 | + |
| 148 | + set fontColor(v) { |
| 149 | + this.tag('Text').color = v |
| 150 | + } |
| 151 | + |
| 152 | + set backgroundColor(v) { |
| 153 | + this.color = v |
| 154 | + } |
| 155 | + |
| 156 | + _defineBreakpoint(text, breakpoint) { |
| 157 | + if (breakpoint >= this.maxWidth) return this.maxWidth |
| 158 | + const info = Lightning.textures.TextTexture.renderer( |
| 159 | + this.stage, |
| 160 | + this.stage.platform.getDrawingCanvas(), |
| 161 | + { |
| 162 | + ...this._textTextureDefaults, |
| 163 | + ...this.textFormat, |
| 164 | + ...{ wordWrapWidth: breakpoint }, |
| 165 | + text, |
| 166 | + } |
| 167 | + )._calculateRenderInfo() |
| 168 | + |
| 169 | + if (info.width <= breakpoint && info.lines.length <= 2) { |
| 170 | + return breakpoint |
| 171 | + } else { |
| 172 | + return this._defineBreakpoint(text, breakpoint * 1.25) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + set text(v) { |
| 177 | + this.alpha = 0 |
| 178 | + if (v && v.length) { |
| 179 | + const breakpoint = this._defineBreakpoint(v, 640) |
| 180 | + |
| 181 | + this.tag('Text').text.wordWrapWidth = breakpoint |
| 182 | + this.tag('Text').text = v |
| 183 | + this.alpha = 1 |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + set textAlign(v) { |
| 188 | + this._textAlign = v |
| 189 | + this.tag('Text').text.textAlign = v |
| 190 | + } |
| 191 | + |
| 192 | + set maxWidth(v) { |
| 193 | + this._maxWidth = v |
| 194 | + } |
| 195 | + |
| 196 | + get maxWidth() { |
| 197 | + return (this._maxWidth || 1200) - this.tag('Text').x * 2 |
| 198 | + } |
| 199 | + |
| 200 | + set maxLines(v) { |
| 201 | + this.tag('Text').text.maxLines = v |
| 202 | + } |
| 203 | +} |
0 commit comments