当前位置: 首页 > article >正文

【cocos creator】下拉框

https://download.csdn.net/download/K86338236/90038176

请添加图片描述
请添加图片描述


const { ccclass, property } = cc._decorator;

type DropDownOptionData = {
    optionString?: string,
    optionSf?: cc.SpriteFrame
}
type DropDownItemData = {
    label: cc.Label,
    sprite: cc.Sprite,
    toggle: cc.Toggle
}

@ccclass()
export default class DropDown extends cc.Component {
    @property(cc.Node)
    private touchNode: cc.Node = undefined;
    @property(cc.Node)
    private arrow: cc.Node = undefined;
    @property(cc.ScrollView)
    private template: cc.ScrollView = undefined;
    @property(cc.Node)
    private optionItem: cc.Node = undefined;
    @property(cc.Node)
    private spriteCaption: cc.Sprite = undefined;
    @property(cc.Label)
    private labelCaption: cc.Label = undefined;
    private optionDatas: DropDownOptionData[] = [];
    chooseIndex = -1;
    showTemplate = false


    protected start(): void {
        if (this.touchNode) {
            this.touchNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
            //@ts-ignore
            this.touchNode._touchListener.setSwallowTouches(false);//取消触摸节点的穿透事件
        }
        this.reset()
        let data = [{ optionString: "11" }, { optionString: "22" }]
        this.init(data)
    }

    reset() {
        this.showTemplate = false
        this.spriteCaption.spriteFrame = null;
        this.labelCaption.string = "";
        this.chooseIndex = -1
        this.template.node.active = this.showTemplate;
        let content = this.template.content
        content.children.forEach(element => {
            element.active = false
        });
        this.optionDatas = [];
        this.upDateTemplate()
    }

    init(data: DropDownOptionData[]) {
        this.optionDatas = data
        this.upDateTemplate()
    }

    chooseItem(event, data) {
        let number = Number(data)
        this.chooseIndex = isNaN(number) ? -1 : number
        this.updateChoose()
        this.hideTample()
    }

    upDateTemplate() {
        let content = this.template.content
        let item = this.optionItem
        content.children.forEach(element => {
            element.active = false
        });
        for (let i = 0; i < this.optionDatas.length; i++) {
            const element = this.optionDatas[i];
            let node = content.children[i]
            if (!node) {
                node = cc.instantiate(item)
                node.parent = content;
            }
            if (!node) content;
            let lable = node.getComponentInChildren(cc.Label)
            let sprite = node.getComponentInChildren(cc.Sprite)
            if (lable) lable.string = element.optionString || ""
            if (sprite) sprite.spriteFrame = element.optionSf || null
            if (lable || sprite) node.active = true;
            node.getChildByName("choose").active = i == this.chooseIndex;
            node.getComponent(cc.Button).clickEvents[0].customEventData = i + "";
        }
        if (this.chooseIndex >= 0) {
            const chooseData = this.optionDatas[this.chooseIndex];
            if (this.spriteCaption) this.spriteCaption.spriteFrame = chooseData.optionSf || null
            if (this.labelCaption) this.labelCaption.string = chooseData.optionString || ""
        }
        this.template.node.height = Math.min(cc.winSize.height / 2 + this.node.y, this.optionDatas.length * this.optionItem.height)
        this.template.node.y = this.node.y - this.node.height / 2
        this.template.node.x = this.node.x
        this.template.node.width = this.node.width
    }

    updateChoose() {
        let content = this.template.content
        for (let i = 0; i < this.optionDatas.length; i++) {
            let node = content.children[i]
            if (!node) return
            node.getChildByName("choose").active = i == this.chooseIndex;
        }
        if (this.chooseIndex >= 0) {
            const chooseData = this.optionDatas[this.chooseIndex];
            if (this.spriteCaption) this.spriteCaption.spriteFrame = chooseData.optionSf || null
            if (this.labelCaption) this.labelCaption.string = chooseData.optionString || ""
        }
    }

    onOptionClick() {
        this.showTemplate = !this.showTemplate
        this.updateShowTamplate()
    }

    showTample() {
        this.showTemplate = true
        this.updateShowTamplate()
    }

    hideTample() {
        this.showTemplate = false
        this.updateShowTamplate()
    }

    updateShowTamplate() {
        this.template.node.active = this.showTemplate;
        this.arrow.scaleY = this.showTemplate ? -1 : 1
    }

    onTouchStart() {
        this.hideTample()
    }
}


http://www.kler.cn/a/410426.html

相关文章:

  • 下载并安装Visual Studio 2017过程
  • UE5肉鸽游戏教程学习
  • 001 MATLAB介绍
  • 优化Docker镜像:提升部署效率与降低资源消耗
  • vue项目中中怎么获取环境变量
  • Java---反射机制
  • Windows系统电脑安装TightVNC服务端结合内网穿透实现异地远程桌面
  • kafka学习-01
  • Unity 2020、2021、2022、2023、6000下载安装
  • 【测试工具JMeter篇】JMeter性能测试入门级教程(一)出炉,测试君请各位收藏了!!!
  • 2024 APMCM亚太数学建模C题 - 宠物行业及相关产业的发展分析和策略 完整参考论文(1)
  • [算法] 前缀函数与KMP算法
  • 数据集-目标检测系列- 荷花 莲花 检测数据集 lotus>> DataBall
  • LeetCode 0632.最小区间:优先队列
  • 成功案例 | Fortinet助力宾堡打造数字化安全“美味王国”
  • java TreeMap 详解
  • 【GAMES101笔记速查——Lecture 19 Cameras,Lenses and Light Fields】
  • C# .Net Core通过StreamLoad向Doris写入CSV数据
  • C# 创建快捷方式文件和硬链接文件
  • 大语言模型---通过数值梯度的方式计算损失值L对模型权重矩阵W的梯度;数值梯度的公式;数值梯度计算过程
  • macOS上进行Ant Design Pro实战教程(一)
  • 【51单片机】程序实验56.独立按键-矩阵按键
  • 【初阶数据与算法】线性表之顺序表的定义与实现
  • 跨平台开发_RTC程序设计:实时音视频权威指南 2
  • Web day02 Js Vue Ajax
  • Java的字符串操作(二)(代码示例)