UserInfoUI.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /** @format */
  2. import {BaseUI} from './BaseUI'
  3. import {Data, Mgr} from '../GameControl'
  4. import {ccUtils} from '../utils/ccUtils'
  5. import {observer, render, node, label, editBox, list} from '../mobx/observer'
  6. import {msgCmd} from '../proto/msg_cmd'
  7. import List from '../uiutils/List'
  8. import {LANGUAGE_TYPE, TIMESTAMP_TYPE, LOCAL} from '../enums/Enum'
  9. import {RanksLevelConfig} from '../config/RanksLevelConfig'
  10. import {roleRenameRsp} from '../proto/game'
  11. const {ccclass, property} = cc._decorator
  12. /** 每座建筑的等级列表 */
  13. @ccclass
  14. @observer
  15. export class UserInfoUI extends BaseUI {
  16. @list('build_list')
  17. buildList: List // 建筑资源列表
  18. @node('playerinfo') //玩家信息
  19. playerInfoNode: cc.Node
  20. @label('playerinfo/id_frame/scrollingtext/lb') //UID 滚动文本
  21. scrollingTextUID: cc.Label
  22. @label('playerinfo/name_frame/scrollingtext/lb') //名称 滚动文本
  23. scrollingTextName: cc.Label
  24. @node('upd_name') //玩家信息界面
  25. updateNameNode: cc.Node
  26. @editBox('upd_name/input_name') //玩家信息界面
  27. inputName: cc.EditBox
  28. minInputName: number = 4 //更改名字最小字符长度
  29. maxInputName: number = 21 //更改名字最大字符长度
  30. onLoad() {}
  31. onShow(args, fromUI: number) {
  32. Mgr.net.add(msgCmd.cmd_role_rename_rsp, this, this.onRoleRenameRsp)
  33. this.initUIData()
  34. this.buildList.numItems = Data.main.buildData.length
  35. this.initScrollingText(this.scrollingTextName)
  36. this.initScrollingText(this.scrollingTextUID)
  37. Mgr.global.initAvatar(cc.find('avatar', this.playerInfoNode))
  38. this.showRedDot()
  39. }
  40. onHide(): any {
  41. Mgr.event.removeAll(this)
  42. }
  43. //显示界面红点
  44. showRedDot() {
  45. const regex = /^[\u4E00-\u9FA5a-zA-Z0-9\s]+$/
  46. cc.find('red_dot', this.playerInfoNode).active = !regex.test(Data.user.nickname)
  47. }
  48. initUIData() {
  49. let curLevelCfg = RanksLevelConfig[Data.user.level + 1]
  50. if (!curLevelCfg) curLevelCfg = Object.values(RanksLevelConfig).pop()
  51. ccUtils.setLabel('lv.' + Data.user.level, this.playerInfoNode, 'lv_frame/lb')
  52. ccUtils.setLabel(Data.user.exp + '/' + curLevelCfg.exp, this.playerInfoNode, 'lv_frame/lb_num')
  53. ccUtils.setProgress(Data.user.exp / curLevelCfg.exp, this.playerInfoNode, 'lv_frame/lv_pb')
  54. ccUtils.setLabel(Mgr.global.getPowerString(), this.playerInfoNode, 'power_frame/lb_num')
  55. ccUtils.setLabel(Data.user.uid, this.playerInfoNode, 'id_frame/scrollingtext/lb')
  56. ccUtils.setLabel(Data.user.nickname, this.playerInfoNode, 'name_frame/scrollingtext/lb')
  57. this.scrollingTextUID.node['isLeftTop'] = true
  58. this.scrollingTextName.node['isLeftTop'] = true //是否到左边顶部了
  59. }
  60. initScrollingText(label: cc.Label) {
  61. //如果是赋值后马上调用 文本宽度可能不是最新的、刷新一下文本宽度
  62. ;(<any>label)._forceUpdateRenderData()
  63. // 停止节点上的所有动作
  64. let s_lb = label.node
  65. s_lb.stopAllActions()
  66. let offset = (s_lb.width - s_lb.parent.width) / 2 //偏移值
  67. if (s_lb.width < s_lb.parent.width) {
  68. return
  69. }
  70. // 创建一个 cc.MoveBy 动作
  71. let moveByAction
  72. let callbackAction
  73. //判断是否文本是不是再最左边 默认不是 默认居中
  74. if (s_lb['isLeftTop']) {
  75. s_lb.x = offset
  76. moveByAction = cc.moveTo(3, cc.v2(-offset, 0))
  77. callbackAction = cc.callFunc(() => {
  78. s_lb['isLeftTop'] = false
  79. this.initScrollingText(label)
  80. })
  81. } else {
  82. s_lb.x = -offset
  83. moveByAction = cc.moveTo(3, cc.v2(offset, 0))
  84. callbackAction = cc.callFunc(() => {
  85. s_lb['isLeftTop'] = true
  86. this.initScrollingText(label)
  87. })
  88. }
  89. const delayAction = cc.delayTime(2)
  90. const sequence = cc.sequence(delayAction, moveByAction, callbackAction)
  91. cc.tween(s_lb).then(sequence).start()
  92. }
  93. //网络事件>改名=======================================
  94. onRoleRenameRsp(data: roleRenameRsp) {
  95. Mgr.storage.set(LOCAL.isEditName, true)
  96. Data.user.nickname = data.name
  97. Data.user.renameTime = data.time
  98. this.updateNameNode.active = false
  99. Mgr.ui.tip(LANGUAGE_TYPE.successfully)
  100. ccUtils.setLabel(Data.user.nickname, this.playerInfoNode, 'name_frame/scrollingtext/lb')
  101. this.initScrollingText(this.scrollingTextName)
  102. this.showRedDot()
  103. }
  104. //输入修改名称字符
  105. onInputNameChange(editBox: cc.EditBox) {
  106. //如果输入的昵称长度不符合限制 就改成原名称
  107. if (editBox.string.getStrLen() < this.minInputName) {
  108. Mgr.ui.tip(LANGUAGE_TYPE.minInputLimit)
  109. ccUtils.setEditBox(Data.user.nickname, this.inputName.node)
  110. return
  111. } else if (editBox.string.getStrLen() > this.maxInputName) {
  112. Mgr.ui.tip(LANGUAGE_TYPE.maxInputLimit)
  113. ccUtils.setEditBox(Data.user.nickname, this.inputName.node)
  114. return
  115. }
  116. }
  117. //确认修改名称
  118. onClickConfirmUpdateName() {
  119. const regex = /^[\u4E00-\u9FA5a-zA-Z0-9\s]+$/
  120. let time = Data.main.serverTime - Data.user.renameTime
  121. //输入违规
  122. if (!regex.test(this.inputName.string)) {
  123. Mgr.ui.tip(LANGUAGE_TYPE.specificSymbol)
  124. return
  125. }
  126. //48个小时内不能改名 服务器时间 - 修改名字的时间 = 间隔时间 >= 2天就可以改名了
  127. if (time >= TIMESTAMP_TYPE.day * 2) {
  128. Mgr.net.send(msgCmd.cmd_role_rename, {name: this.inputName.string})
  129. } else {
  130. Mgr.ui.tip(`${Mgr.i18n.getLabel(LANGUAGE_TYPE.changeNameCD)}...`)
  131. return
  132. }
  133. }
  134. //点击打开修改名称界面
  135. onClickOpenUpdateName() {
  136. this.updateNameNode.active = true
  137. ccUtils.setEditBox(Data.user.nickname, this.inputName.node)
  138. }
  139. //关闭修改名称界面
  140. onClickCloseUpdateName() {
  141. this.updateNameNode.active = false
  142. }
  143. //点击复制uid
  144. onClickCopyUID() {
  145. Mgr.platform.copyToClipboard(Data.user.uid)
  146. }
  147. }