future 1 year ago
parent
commit
ba7de23515
2 changed files with 191 additions and 343 deletions
  1. 135 300
      assets/resources/ui/home.prefab
  2. 56 43
      assets/script/ui/home/home_guangbo.ts

File diff suppressed because it is too large
+ 135 - 300
assets/resources/ui/home.prefab


+ 56 - 43
assets/script/ui/home/home_guangbo.ts

@@ -1,64 +1,77 @@
-import { _decorator, Component, Label, Node, Tween, tween, UITransform, Vec3 } from 'cc';
+import { _decorator, CacheMode, Color, Component, Label, Node, Size, Tween, tween, UITransform, Vec3, view } from 'cc';
 import { guangboData } from '../../data';
 const { ccclass, property } = _decorator;
 
 @ccclass('home_guangbo')
 export class home_guangbo extends Component {
-    @property(Node) lab_1:Node = null
-    @property(Node) lab_2:Node = null
+    @property(Node) lab_content:Node = null
     private cur_index = 0
     private data_list:guangboData[] = []
+    private screen_width:number = 1080
+    private speed:number = 5
+    private left_padding:number = 30
 
     init(data_list:guangboData[]) {
         this.data_list = data_list
-        this.lab_1.getComponent(Label).string = this.data_list[this.cur_index].title
-        this.unscheduleAllCallbacks()
-        this.schedule(()=>{
-            this.cur_index++
-            if(this.cur_index>=this.data_list.length) {
-                this.cur_index = 0
-            }
-            this.lab_1.getComponent(Label).string = this.data_list[this.cur_index].title
-        },2.5)
+        // console.log(this.data_list)
+        this.cur_index = 0
+        this.getContentText()
     }
 
-    private startSchedule() {
-        // this.lab_2.active = false
-        this.setText(this.lab_1, this.data_list[this.cur_index].title)
-        this.schedule(()=>{
-            this.cur_index ++
-            this.runFrame(this.lab_1)
-            this.runFrame(this.lab_2)
-            if(this.cur_index>=this.data_list.length) {
-                this.cur_index = 0
-            }
-
-        },2.5)
+    private getContentText() {
+        if(this.cur_index>=this.data_list.length) {
+            this.cur_index = 0
+        }
+        let cur_content = this.data_list[this.cur_index].title
+        let lab_com = this.lab_content.getComponent(Label)
+        lab_com.getComponent(Label).string = cur_content
+        let cur_content_width = this.computeTextWidth(cur_content,lab_com.fontSize,lab_com.lineHeight)
+        // console.log('cur_content_width=',cur_content_width)
+        let cur_content_height = this.lab_content.getComponent(UITransform).contentSize.height
+        this.lab_content.getComponent(UITransform).setContentSize(new Size(cur_content_width,cur_content_height))
+        this.lab_content.setPosition(this.screen_width/2+this.left_padding,0,0)
+        this.runContent(cur_content_width)
     }
 
-    private stopSchedule() {
+    private runContent(content_width:number) {
         this.unscheduleAllCallbacks()
-    }
-
-    private runFrame(current_node:Node) {
-        let cur_y = current_node.getPosition().y
-        tween(current_node).to(0.5,{position: new Vec3(0,cur_y+50,0)},{
-        }).call(()=>{
-            let later_y = current_node.getPosition().y
-            if(later_y==50) {
-                // current_node.active = false
-                current_node.setPosition(new Vec3(0,-50,0))
-            } else if(later_y==0) {
-                this.setText(current_node, this.data_list[this.cur_index].title)
-                current_node.active = true
-            } else if(later_y==-50) {
-                current_node.active = false
+        let run_pos_x = this.lab_content.getPosition().x
+        this.schedule(()=>{
+            run_pos_x -= this.speed
+            // console.log('cur_lab_content_postion_x=',cur_lab_content_postion_x)
+            this.lab_content.setPosition(run_pos_x,0,0)
+            if(run_pos_x<=-(content_width+this.screen_width/2)) {
+                run_pos_x = this.screen_width/2+this.left_padding
+                this.unscheduleAllCallbacks()
+                this.cur_index++
+                this.getContentText()
             }
-        }).start()
+        },0.08)
     }
 
-    private setText(current_node:Node, str:string) {
-        current_node.getComponent(Label).string = '恭喜广州吴彦祖-' + str
+    /**
+     * @param text 要显示文本内容
+     * @param designSize label的设计宽高
+     * @param fontSize 字体大小
+     * @param lineHeight 行高
+     */
+    private computeTextWidth(text: string, fontSize: number, lineHeight: number):number {
+        let node = new Node();
+        var newlabel :Label = node.addComponent(Label);        
+        newlabel.fontSize = fontSize;
+        newlabel.lineHeight = lineHeight;
+        newlabel.string = text;
+        newlabel.color = new Color(0, 0, 0, 0);
+        //计算宽度
+        newlabel.overflow = Label.Overflow.NONE; //设置不换行和不等比压缩,文字原来有多长就多长
+        newlabel.cacheMode = CacheMode.CHAR; //这里要设一下CacheMode.CHAR, 否则Label最大长度只有2048,不能再大了.
+        
+        newlabel.updateRenderData(true);
+        let textWidth = newlabel.getComponent(UITransform).contentSize.width;
+        node.destroy();
+ 
+        return textWidth;
     }
+
 }
 

Some files were not shown because too many files changed in this diff