/
SkinNoteDistributionGraph.java
456 lines (405 loc) · 14.1 KB
/
SkinNoteDistributionGraph.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package bms.player.beatoraja.skin;
import bms.model.*;
import bms.player.beatoraja.MainState;
import bms.player.beatoraja.play.BMSPlayer;
import bms.player.beatoraja.skin.Skin.SkinObjectRenderer;
import bms.player.beatoraja.song.SongData;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
/**
* ノーツ分布を表示するグラフ
*
* @author exch
*/
public final class SkinNoteDistributionGraph extends SkinObject {
private MainState state;
// TODO 各Textureを1枚にまとめてblBindTextureの回数を削減する
private TextureRegion backtex;
private TextureRegion shapetex;
private TextureRegion cursortex;
private Pixmap back = null;
private Pixmap shape = null;
private Pixmap cursor = null;
private BMSModel model;
private SongData current;
private int[][] data = new int[0][0];
private static final Color[][] JGRAPH = {
{ Color.valueOf("44ff44"), Color.valueOf("228822"), Color.valueOf("ff4444"), Color.valueOf("4444ff"), Color.valueOf("222288"), Color.valueOf("cccccc"),
Color.valueOf("880000") },
{ Color.valueOf("555555"), Color.valueOf("0088ff"), Color.valueOf("00ff88"), Color.valueOf("ffff00"),
Color.valueOf("ff8800"), Color.valueOf("ff0000") },
{ Color.valueOf("555555"), Color.valueOf("44ff44"), Color.valueOf("0088ff"), Color.valueOf("0066cc"),
Color.valueOf("004488"), Color.valueOf("002244"), Color.valueOf("ff8800"), Color.valueOf("cc6600"),
Color.valueOf("884400"), Color.valueOf("442200") } };
private static final Color[][] pmsGraphColor = {
{ Color.valueOf("44ff44"), Color.valueOf("228822"), Color.valueOf("ff4444"), Color.valueOf("4444ff"), Color.valueOf("222288"), Color.valueOf("cccccc"),
Color.valueOf("880000") },
{ Color.valueOf("555555"), Color.valueOf("ff5eb0"), Color.valueOf("ffbe32"), Color.valueOf("dc463c"),
Color.valueOf("6cc6ff"), Color.valueOf("6cc6ff") },
{ Color.valueOf("555555"), Color.valueOf("ff5eb0"), Color.valueOf("0088ff"), Color.valueOf("0066cc"),
Color.valueOf("004488"), Color.valueOf("002244"), Color.valueOf("ff8800"), Color.valueOf("cc6600"),
Color.valueOf("884400"), Color.valueOf("442200") } };
private Pixmap[] chips;
private int max = 20;
private int type;
public static final int TYPE_NORMAL = 0;
public static final int TYPE_JUDGE = 1;
public static final int TYPE_EARLYLATE = 2;
private static final int[] DATA_LENGTH = {7, 6, 10};
private boolean isBackTexOff = false;
private int delay = 500;
private boolean isOrderReverse = false;
private boolean isNoGap = false;
private boolean isNoGapX = false;
/*
* 処理済みノート数 プレイ時は処理済みノート数に変化があった時だけ更新する
*/
private int pastNotes = 0;
private long notesLastUpdateTime;
private long cursorLastUpdateTime;
private int starttime;
private int endtime;
private float freq;
private float render;
private static final Color TRANSPARENT_COLOR = Color.valueOf("00000000");
public SkinNoteDistributionGraph() {
this(TYPE_NORMAL, 500, 0, 0, 0, 0);
}
public SkinNoteDistributionGraph(int type, int delay, int backTexOff, int orderReverse, int noGap, int noGapX) {
this(null, type, delay, backTexOff, orderReverse, noGap, noGapX);
}
public SkinNoteDistributionGraph(Pixmap[] chips, int type, int delay, int backTexOff, int orderReverse, int noGap, int noGapX) {
this.chips = chips;
this.type = type;
this.isBackTexOff = backTexOff == 1;
this.delay = delay;
this.isOrderReverse = orderReverse == 1;
this.isNoGap = noGap == 1;
this.isNoGapX = noGapX == 1;
pastNotes = 0;
}
public void prepare(long time, MainState state) {
prepare(time, state, null, -1, -1, -1);
}
public void prepare(long time, MainState state, Rectangle r, int starttime, int endtime, float freq) {
super.prepare(time, state);
if(r != null) {
region.set(r);
draw = true;
}
this.state = state;
this.starttime = starttime;
this.endtime = endtime;
this.freq = freq;
render = time >= delay ? 1.0f : (float) time / delay;
}
public void draw(SkinObjectRenderer sprite) {
final SongData song = state.resource.getSongdata();
final BMSModel model = song != null ? song.getBMSModel() : null;
// TODO スキン定義側で分岐できないか?
if(chips == null) {
Color[] graphcolor = type != TYPE_NORMAL && model != null && model.getMode() == Mode.POPN_9K ?
pmsGraphColor[type] : JGRAPH[type];
chips = new Pixmap[graphcolor.length];
for(int i = 0;i < graphcolor.length;i++) {
chips[i] = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
chips[i].drawPixel(0, 0, Color.toIntBits(255, (int)(graphcolor[i].b * 255), (int)(graphcolor[i].g * 255), (int)(graphcolor[i].r * 255)));
}
}
if(shapetex == null || song != current || (this.model == null && model != null)) {
current = song;
this.model = model;
if(type == TYPE_NORMAL && song != null && song.getInformation() != null) {
updateGraph(song.getInformation().getDistributionValues());
} else {
updateGraph();
}
}
//プレイ時、判定をリアルタイムで更新する
/*
* TODO 高速化のアイデア
* BMSPlayerから更新したノーツの時間だけを渡し。指定時間のデータ/イメージのみ更新する
* backtex更新は初回のみ
*/
if(model != null && state instanceof BMSPlayer) {
if(System.currentTimeMillis() > notesLastUpdateTime + 750) {
if(type != TYPE_NORMAL && pastNotes != ((BMSPlayer)state).getPastNotes()) {
pastNotes = ((BMSPlayer)state).getPastNotes();
updateData();
updateTexture(false);
}
notesLastUpdateTime = System.currentTimeMillis();
}
if(System.currentTimeMillis() > cursorLastUpdateTime + 50) {
final int oldw = cursor != null ? cursor.getWidth() : 0;
final int oldh = cursor != null ? cursor.getHeight() : 0;
final int w = data.length * 5;
final int h = max * 5;
cursor.setColor(TRANSPARENT_COLOR);
cursor.fill();
// スタートカーソル描画
if (starttime >= 0) {
int dx = (int) (starttime * w / (data.length * 1000));
cursor.setColor(Color.toIntBits(255, 128, 255, 128));
cursor.fillRectangle(dx, 0, 3, h);
}
// エンドカーソル描画
if (endtime >= 0) {
int dx = (int) (endtime * w / (data.length * 1000));
cursor.setColor(Color.toIntBits(255, 128, 128, 255));
cursor.fillRectangle(dx, 0, 3, h);
}
// 現在カーソル描画
if (state instanceof BMSPlayer && state.timer.isTimerOn(SkinProperty.TIMER_PLAY)) {
float currenttime = state.timer.getNowTime(SkinProperty.TIMER_PLAY);
if (freq > 0) {
currenttime *= freq;
}
int dx = (int) (currenttime * w / (data.length * 1000));
cursor.setColor(Color.toIntBits(255, 255, 255, 255));
cursor.fillRectangle(dx, 0, 3, h);
}
if(cursortex == null) {
cursortex = new TextureRegion(new Texture(cursor));
} else if(oldw != w || oldh != h) {
cursortex.getTexture().dispose();
cursortex = new TextureRegion(new Texture(cursor));
} else {
cursortex.getTexture().draw(cursor, 0, 0);
}
cursorLastUpdateTime = System.currentTimeMillis();
}
draw(sprite, backtex, region.x, region.y + region.height, region.width, -region.height);
shapetex.setRegionWidth((int) (shapetex.getTexture().getWidth() * render));
draw(sprite, shapetex, region.x, region.y + region.height, region.width * render, -region.height);
draw(sprite, cursortex, region.x, region.y + region.height, region.width, -region.height);
} else {
draw(sprite, backtex, region.x, region.y + region.height, region.width, -region.height);
shapetex.setRegionWidth((int) (shapetex.getTexture().getWidth() * render));
draw(sprite, shapetex, region.x, region.y + region.height, region.width * render, -region.height);
}
}
public void draw(SkinObjectRenderer sprite, long time, MainState state, Rectangle r, int starttime, int endtime, float freq) {
prepare(time, state, r, starttime, endtime, freq);
if(draw) {
draw(sprite);
}
}
private void updateGraph(int[][] distribution) {
data = distribution;
max = 20;
for(int i = 0;i < distribution.length;i++) {
int count = 0;
for(int j = 0;j < distribution[0].length;j++) {
count += distribution[i][j];
}
if (max < count) {
max = Math.min((count / 10) * 10 + 10, 100);
}
}
updateTexture(true);
}
private void updateGraph() {
if (model == null) {
data = new int[0][DATA_LENGTH[type]];
} else {
data = new int[model.getLastTime() / 1000 + 1][DATA_LENGTH[type]];
updateData();
}
updateTexture(true);
}
private void updateData() {
int pos = -1;
int count = 0;
max = 20;
for(int[] d : data) {
Arrays.fill(d, 0);
}
final Mode mode = model.getMode();
for (TimeLine tl : model.getAllTimeLines()) {
final int index = tl.getTime() / 1000;
if(index >= data.length) {
break;
}
if (index != pos) {
if (max < count) {
max = Math.min((count / 10) * 10 + 10, 100);
}
pos = index;
count = type == TYPE_NORMAL ? data[index][1] + data[index][4] : 0;
}
for (int i = 0; i < mode.key; i++) {
Note n = tl.getNote(i);
if (n != null) {
final int st = n.getState();
final int t = n.getPlayTime();
switch (type) {
case TYPE_NORMAL:
if (n instanceof NormalNote) {
data[index][mode.isScratchKey(i) ? 2 : 5]++;
count++;
} else if (n instanceof LongNote) {
if(!((LongNote)n).isEnd()) {
for(int lnindex = index;lnindex <= ((LongNote)n).getPair().getTime() / 1000;lnindex++) {
data[lnindex][mode.isScratchKey(i) ? 1 : 4]++;
}
count++;
}
if((model.getLntype() == BMSModel.LNTYPE_LONGNOTE && n instanceof LongNote
&& ((LongNote) n).isEnd())) {
data[index][mode.isScratchKey(i) ? 0 : 3]++;
data[index][mode.isScratchKey(i) ? 1 : 4]--;
}
} else if (n instanceof MineNote) {
data[index][6]++;
count++;
}
break;
case TYPE_JUDGE:
if (n instanceof MineNote || (model.getLntype() == BMSModel.LNTYPE_LONGNOTE && n instanceof LongNote
&& ((LongNote) n).isEnd())) {
break;
}
data[index][st]++;
count++;
break;
case TYPE_EARLYLATE:
if (n instanceof MineNote || (model.getLntype() == BMSModel.LNTYPE_LONGNOTE && n instanceof LongNote
&& ((LongNote) n).isEnd())) {
break;
}
if (st <= 1) {
data[index][st]++;
} else {
data[index][t >= 0 ? st : st + 4]++;
}
count++;
break;
}
}
}
}
}
private void updateTexture(boolean updateall) {
final int oldw = shape != null ? shape.getWidth() : 0;
final int oldh = shape != null ? shape.getHeight() : 0;
final int w = data.length * 5;
final int h = max * 5;
boolean refresh = false;
if(shape == null) {
back = new Pixmap(w, h, Pixmap.Format.RGBA8888);
shape = new Pixmap(w, h, Pixmap.Format.RGBA8888);
cursor = new Pixmap(w, h, Pixmap.Format.RGBA8888);
refresh = true;
} else if(oldw != w || oldh != h) {
back.dispose();
shape.dispose();
cursor.dispose();
back = new Pixmap(w, h, Pixmap.Format.RGBA8888);
shape = new Pixmap(w, h, Pixmap.Format.RGBA8888);
cursor = new Pixmap(w, h, Pixmap.Format.RGBA8888);
refresh = true;
} else if(updateall){
back.setColor(TRANSPARENT_COLOR);
back.fill();
shape.setColor(TRANSPARENT_COLOR);
shape.fill();
cursor.setColor(TRANSPARENT_COLOR);
cursor.fill();
refresh = true;
}
int start = 0;
int end = data.length;
if(updateall) {
if(!isBackTexOff) {
back.setColor(0, 0, 0, 0.8f);
back.fill();
for (int i = 10; i < max; i += 10) {
back.setColor(0.007f * i, 0.007f * i, 0, 1.0f);
back.fillRectangle(0, i * 5, data.length * 5, 50);
}
for (int i = 0; i < data.length; i++) {
// x軸補助線描画
if (i % 60 == 0) {
back.setColor(0.25f, 0.25f, 0.25f, 1.0f);
back.drawLine(i * 5, 0, i * 5, max * 5);
} else if (i % 10 == 0) {
back.setColor(0.125f, 0.125f, 0.125f, 1.0f);
back.drawLine(i * 5, 0, i * 5, max * 5);
}
}
} else if(!refresh){
for (int i = 0; i < data.length; i++) {
if(data[i][0] > 0) {
start = Math.max(0, i - 2);
end = Math.min(data.length, i + 3);
break;
}
}
}
if(backtex == null) {
backtex = new TextureRegion(new Texture(back));
} else if(oldw != w || oldh != h) {
backtex.getTexture().dispose();
backtex = new TextureRegion(new Texture(back));
} else {
backtex.getTexture().draw(back, 0, 0);
}
}
for (int i = start; i < end; i++) {
final int[] n = data[i];
if(!isOrderReverse) {
for (int j = 0, k = n[0], index = 0; j < max && index < n.length;) {
if (k > 0) {
k--;
shape.drawPixmap(chips[index], 0, 0, 1, 1, i * 5, j * 5, 4 + (isNoGapX ? 1 : 0), 4 + (isNoGap ? 1 : 0));
j++;
} else {
index++;
if (index == n.length) {
break;
}
k = n[index];
}
}
} else {
for (int j = 0, k = n[n.length - 1], index = n.length - 1; j < max && index < n.length;) {
if (k > 0) {
k--;
shape.drawPixmap(chips[index], 0, 0, 1, 1, i * 5, j * 5, 4 + (isNoGapX ? 1 : 0), 4 + (isNoGap ? 1 : 0));
j++;
} else {
index--;
if (index < 0) {
break;
}
k = n[index];
}
}
}
}
if(shapetex == null) {
shapetex = new TextureRegion(new Texture(shape));
} else if(oldw != w || oldh != h) {
shapetex.getTexture().dispose();
shapetex = new TextureRegion(new Texture(shape));
} else {
shapetex.getTexture().draw(shape, 0, 0);
}
}
@Override
public void dispose() {
Optional.ofNullable(backtex).ifPresent(t -> t.getTexture().dispose());
backtex = null;
Optional.ofNullable(shapetex).ifPresent(t -> t.getTexture().dispose());
shapetex = null;
Optional.ofNullable(cursortex).ifPresent(t -> t.getTexture().dispose());
cursortex = null;
Optional.ofNullable(chips).ifPresent(t -> Stream.of(t).forEach(Pixmap::dispose));
setDisposed();
}
}