-
Notifications
You must be signed in to change notification settings - Fork 258
/
viewer-database.js
148 lines (126 loc) · 3.66 KB
/
viewer-database.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import Phaser from 'phaser';
import GridTablePlugin from '../../plugins/gridtable-plugin.js';
import TouchStatePlugin from '../../plugins/touchstate-plugin.js';
import ContainerLitePlugin from '../../plugins/containerlite-plugin.js';
const GetValue = Phaser.Utils.Objects.GetValue;
class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}
preload() {
this.load.image('bg', 'assets/images/white-dot.png');
}
create() {
var database = getDataBase();
var config = {
database: database,
x: 400,
y: 300,
width: 250,
height: 400,
depth: -1,
cellHeight: 60,
cellWidth: 60,
cellsCount: database.length,
columns: 4
};
var table = newTable(this, config);
// draw bound
this.add.graphics()
.lineStyle(3, 0xff0000)
.strokeRectShape(table.getBounds());
//table.updateTable(true);
}
update() {}
}
var getDataBase = function () {
var data = [];
for (var i = 0; i < 100; i++) {
data.push({
id: i + 133
});
}
return data;
}
var newTable = function (scene, config) {
var table;
var database = GetValue(config, 'database', []);
var newCellObject = function (cell) {
var scene = cell.scene;
var data = database[cell.index];
var bg = scene.add.image(30, 30, 'bg')
.setName('background')
.setDisplaySize(56, 56)
.setTint(0x333333);
var txt = scene.add.text(5, 5, data.id)
.setName('id')
.setInteractive()
.on('pointerup', function () {
console.log('click cell ' + cell.index);
deleteCell(cell);
});
cell.setData('id', data.id);
var container = scene.add.rexContainerLite(0, 0)
.add(bg)
.add(txt);
return container;
};
var deleteCell = function (cell) {
database.splice(cell.index, 1); // remove item from database
refreshViewer();
}
var refreshViewer = function () {
table.setCellsCount(database.length).updateTable();
table.iterateVisibleCell(updateCellObject);
};
var updateCellObject = function (cell) {
var data = database[cell.index];
cell.getContainer().getByName('id').setText(data.id);
};
config.cellVisibleCallback = function (cell) {
cell.setContainer(newCellObject(cell));
//console.log('Cell ' + cell.index + ' visible');
};
table = scene.make.rexGridTable(config);
addDragContentBehavior(table);
return table;
}
var addDragContentBehavior = function (table) {
table.touchState = table.scene.plugins.get('rexTouchState').add(table)
.on('touchmove', function (pointer) {
table.addTableOXY(this.dx, this.dy).updateTable();
});
return table;
}
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
plugins: {
global: [{
key: 'rexGridTable',
plugin: GridTablePlugin,
start: true
},
{
key: 'rexTouchState',
plugin: TouchStatePlugin,
start: true
},
{
key: 'rexContainerLite',
plugin: ContainerLitePlugin,
start: true
}
]
}
};
var game = new Phaser.Game(config);