LyricController.ets 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import { Lyric } from './bean/Lyric'
  2. // default config
  3. const DEFAULT_LINE_SPACE = 16
  4. const DEFAULT_TEXT_SIZE = 18
  5. const DEFAULT_HIGHLIGHT_SCALE = 1.2
  6. const DEFAULT_TEXT_COLOR = "#80000000"
  7. const DEFAULT_HIGHLIGHT_COLOR = "#000000"
  8. const DEFAULT_EDGE_COLOR = "#ffffff"
  9. const DEFAULT_ANIM_DURATION = 300
  10. const DEFAULT_CACHE_SIZE = 4
  11. const DEFAULT_BLUR_DEGREE = 4
  12. /**
  13. * The controller for LyricView.
  14. */
  15. export class LyricController {
  16. private lyric: Lyric | null = null
  17. private textColor: string = DEFAULT_TEXT_COLOR
  18. private blurDegree: number = DEFAULT_BLUR_DEGREE
  19. private textSize: number = DEFAULT_TEXT_SIZE
  20. private highlightColor: string = DEFAULT_HIGHLIGHT_COLOR
  21. private highlightScale: number = DEFAULT_HIGHLIGHT_SCALE
  22. private edgeColor: string = DEFAULT_EDGE_COLOR
  23. private lineSpace: number = DEFAULT_LINE_SPACE
  24. private animDuration: number = DEFAULT_ANIM_DURATION
  25. private cacheSize: number = DEFAULT_CACHE_SIZE
  26. private isBold: boolean = true
  27. private emptyHint: string = "--"
  28. private alignMode: "left" | "center" = "center"
  29. private isHightLightCenter: boolean = true
  30. private textWeight: number = FontWeight.Medium
  31. /**
  32. * Listener to observe the lyric data set changed. This is a inner function, do not call this.
  33. */
  34. onDataChangedListener: (lyric: Lyric | null) => void = () => {
  35. }
  36. /**
  37. * Listener to observe the media position changed. This is a inner function, do not call this.
  38. */
  39. onPositionChangedListener: (position: number) => void = () => {
  40. }
  41. /**
  42. * Listener to observe the user config changed. This is a inner function, do not call this.
  43. */
  44. onInvalidated: (reLayout: boolean) => void = () => {
  45. }
  46. /**
  47. * Update the current position of media player to refresh the lyric view.
  48. * @param mediaPosition The current position of media player.
  49. */
  50. updatePosition(mediaPosition: number) {
  51. this.onPositionChangedListener?.(mediaPosition)
  52. }
  53. /**
  54. * Set the lyric datasource.
  55. * @param lyric The lyric datasource.
  56. * @returns LyricConfig
  57. */
  58. setLyric(lyric: Lyric | null): LyricController {
  59. this.lyric = lyric
  60. this.onDataChangedListener(lyric)
  61. return this
  62. }
  63. /**
  64. * Get the lyric datasource.
  65. * @returns The lyric datasource.
  66. */
  67. getLyric(): Lyric | null {
  68. return this.lyric
  69. }
  70. setTextWeight(textWeight: number): LyricController {
  71. this.textWeight = textWeight
  72. this.onInvalidated(false)
  73. return this
  74. }
  75. getTextWeight(): number {
  76. return this.textWeight
  77. }
  78. /**
  79. * Set the color of normal lyric text. The default color is 0xff929292.
  80. * @param color The color of normal lyric text.
  81. * @returns LyricConfig
  82. */
  83. setTextColor(color: string): LyricController {
  84. this.textColor = color
  85. this.onInvalidated(false)
  86. return this
  87. }
  88. /**
  89. * Get the color of normal lyric text.
  90. * @returns The color of normal lyric text.
  91. */
  92. getTextColor(): string {
  93. return this.textColor
  94. }
  95. /**
  96. * Set the text size(vp) of normal lyric. The default size is 48px.
  97. * @param textSize The text size of normal lyric.
  98. * @returns LyricConfig
  99. */
  100. setTextSize(textSize: number): LyricController {
  101. this.textSize = textSize
  102. this.onInvalidated(true)
  103. return this
  104. }
  105. /**
  106. * Get the text size(vp) of normal lyric.
  107. * @returns The text size of normal lyric.
  108. */
  109. getTextSize(): number {
  110. return this.textSize
  111. }
  112. setHightLightCenter(isHightLightCenter: boolean): LyricController {
  113. this.isHightLightCenter = isHightLightCenter
  114. this.onInvalidated(true)
  115. return this
  116. }
  117. getHightLightCenter(): boolean {
  118. return this.isHightLightCenter
  119. }
  120. setBlurDegree(degree: number): LyricController {
  121. this.blurDegree = degree
  122. this.onInvalidated(true)
  123. return this
  124. }
  125. getBlurDegree(): number {
  126. return this.blurDegree
  127. }
  128. /**
  129. * Set the color of focused lyric text. The default color is Black.
  130. * @param color The color of focused lyric text.
  131. * @returns LyricConfig
  132. */
  133. setHighlightColor(color: string): LyricController {
  134. this.highlightColor = color
  135. this.onInvalidated(false)
  136. return this
  137. }
  138. /**
  139. * Get the color of focused lyric text.
  140. * @returns The color of focused lyric text.
  141. */
  142. getHighlightColor(): string {
  143. return this.highlightColor
  144. }
  145. /**
  146. * Set the scale of focused lyric. The default scale is 1.2f.
  147. * @param scale The scale of focused lyric.
  148. * @returns LyricConfig
  149. */
  150. setHighlightScale(scale: number): LyricController {
  151. this.highlightScale = scale
  152. this.onInvalidated(true)
  153. return this
  154. }
  155. /**
  156. * Get the scale of focused lyric.
  157. * @returns The scale of focused lyric.
  158. */
  159. getHighlightScale(): number {
  160. return this.highlightScale
  161. }
  162. /**
  163. * Set is bold or not for the focused lyric. Default is true.
  164. * @param bold True to set text bold style, false to set normal style.
  165. * @returns LyricConfig
  166. */
  167. setHighlightBold(isBold: boolean): LyricController {
  168. this.isBold = isBold
  169. this.onInvalidated(false)
  170. return this
  171. }
  172. /**
  173. * The bold style of focused lyric.
  174. * @returns True to set text bold style, false to set normal style.
  175. */
  176. isHighlightBold(): boolean {
  177. return this.isBold
  178. }
  179. /**
  180. * Set the fade edge color of top and bottom. The default color is White.
  181. * @deprecated since v1.0.2
  182. * @param color The fade edge color of top and bottom.
  183. * @returns LyricConfig
  184. */
  185. setEdgeColor(color: string): LyricController {
  186. this.edgeColor = color
  187. this.onInvalidated(false)
  188. return this
  189. }
  190. /**
  191. * Get the fade edge color of top and bottom.
  192. * @deprecated since v1.0.2
  193. * @returns The fade edge color of top and bottom.
  194. */
  195. getEdgeColor(): string {
  196. return this.edgeColor
  197. }
  198. /**
  199. * Set the line space(vp) between two lyric lines. The default line space is 16px.
  200. * @param lineSpace The line space between two lyric lines.
  201. * @returns LyricConfig
  202. */
  203. setLineSpace(lineSpace: number): LyricController {
  204. this.lineSpace = lineSpace
  205. this.onInvalidated(true)
  206. return this
  207. }
  208. /**
  209. * Get the line space between two lyric lines.
  210. * @returns The line space between two lyric lines.
  211. */
  212. getLineSpace(): number {
  213. return this.lineSpace
  214. }
  215. /**
  216. * Set the duration of translate animation. The default duration is 500ms.
  217. * @param duration The duration of translate animation.
  218. * @returns LyricConfig
  219. */
  220. setAnimationDuration(duration: number): LyricController {
  221. this.animDuration = duration
  222. return this
  223. }
  224. /**
  225. * Get the duration of translate animation.
  226. * @returns The duration of translate animation.
  227. */
  228. getAnimationDuration(): number {
  229. return this.animDuration
  230. }
  231. /**
  232. * Set the size of lyric lines to draw out of top and bottom, the result is min of cacheSize and the lyric lines.
  233. * The default cache size is 2.
  234. * @param cacheSize The size of lyric lines to draw out of top and bottom.
  235. * @returns LyricConfig
  236. */
  237. setCacheSize(cacheSize: number): LyricController {
  238. this.cacheSize = cacheSize
  239. this.onInvalidated(false)
  240. return this
  241. }
  242. /**
  243. * Get the size of lyric lines to draw out of top and bottom.
  244. * @returns The size of lyric lines to draw out of top and bottom.
  245. */
  246. getCacheSize(): number {
  247. return this.cacheSize
  248. }
  249. /**
  250. * Set the hint text when is no lyric to display.
  251. * @param hint The hint text when is no lyric to display.
  252. * @returns LyricConfig
  253. */
  254. setEmptyHint(hint: string): LyricController {
  255. this.emptyHint = hint
  256. this.onInvalidated(false)
  257. return this
  258. }
  259. /**
  260. * Get the hint text when is no lyric to display.
  261. * @returns The hint text when is no lyric to display.
  262. */
  263. getEmptyHint(): string {
  264. return this.emptyHint
  265. }
  266. /**
  267. * Set the alignment of the lyric text to display from.
  268. * @param align The value is "left" | "center", default is "left".
  269. * @returns LyricConfig
  270. */
  271. setAlignMode(align: "left" | "center"): LyricController {
  272. this.alignMode = align
  273. this.onInvalidated(true)
  274. return this
  275. }
  276. /**
  277. * Get the alignment of the lyric text to display from.
  278. * @returns The value is "left" | "center".
  279. */
  280. getAlignMode(): "left" | "center" {
  281. return this.alignMode
  282. }
  283. /**
  284. * Invalidate the ui by user, this will not reLayout, just do draw.
  285. */
  286. invalidate() {
  287. this.onInvalidated(false)
  288. }
  289. }