UserInfoUI.ts 8.1 KB

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