difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var ToggleItem = cc.Class({
    extends: ViewClass,
 
    ctor: function () {
        this.prefabPath = PathTool.getPrefabPath("backpack", "toggle_item");
        this.initRootWnd()
        this.data = null;
    },
 
    initRootWnd: function () {
        if (!this.prefabPath) return;
        LoaderManager.getInstance().loadRes(this.prefabPath, function (root_prefab) {
            this.root_wnd = root_prefab;
            if (this.parent)
                this.parent.addChild(this.root_wnd);
            if (this.position)
                this.setPosition(this.position)
            if (this.scale)
                this.root_wnd.scale = this.scale;
            this.initWidget();
            this.root_wnd.active = this.visible;
            if (this.is_select != null) {
                this.setSelected(this.is_select)
            }
        }.bind(this))
    },
 
    initWidget: function () {
        this.toggle_tg = this.seekChild("toggle", cc.Toggle);
        this.toggle_tg.node.on(cc.Node.EventType.TOUCH_END, function () {
            if (this.call_back) {
                if (this.data) {
                    this.call_back(!this.data.select);
                    this.data.select = !this.data.select;
 
                } else {
                    this.call_back(this.toggle_tg.isChecked);
                }
            }
        }, this)
    },
 
    setData: function (data) {
        this.data = data;
        if (this.root_wnd)
            this.updateWidget();
    },
 
    updateWidget: function () {
        if (this.data == null) return
    },
 
    setPosition: function (pos) {
        this.position = pos;
        if (this.root_wnd)
            this.root_wnd.setPosition(pos.x - 60, pos.y - 60)
    },
 
    setScale: function (scale) {
        this.scale = scale;
        if (this.root_wnd)
            this.root_wnd.scale = scale;
    },
 
    setSelected: function (bool) {
        this.is_select = bool;
        if (this.toggle_tg) {
            Utils.delayRun(this.toggle_tg.node, 1 / 60, function () {
                if (bool) {
                    this.toggle_tg.check();
                } else {
                    this.toggle_tg.uncheck();
                }
            }.bind(this))
        }
    },
 
    isSelected: function () {
        return !this.toggle_tg.isChecked
    },
 
    addClickCallBack: function (func) {
        this.call_back = func;
    },
 
    setVisible: function (visible) {
        this.visible = visible;
        if (this.root_wnd)
            this.root_wnd.active = visible;
    },
 
});