動作環境
Processing 3.1.1 on Windows 8.1 pro(64bit)
ControlP5 v2.2.6
欲しい機能
C++ BuilderのTMemoのように、複数行のテキスト表示機能が欲しい。
テキストの行数が多くなった時点で、上の行が消えて新規の行が追加されるという機能。
ProcessingやControlP5関連でAPIを探したが、それらしいものを見つけられなかったので実装してみた。
code
split()という関数があったので、それを使った。
import controlP5.*;
String dispLabel = "";
int count = 0;
void setup() {
size(700,400);
frameRate(1);
}
String suppressToNLines(String srcstr, int nlines)
{
String[] wrk = split(srcstr, '\n');
int loop = min(wrk.length, nlines + 1);
int strt = 0;
if ( wrk.length == (nlines + 1) ) {
strt = 1;
}
println(loop);
String res = "";
for(int idx = strt; idx < loop; idx++) {
if (res.length() > 0) {
res = res + '\n';
}
res = res + wrk[idx];
}
return res;
}
void draw() {
background(255);
count++;
if (dispLabel.length() > 0) {
dispLabel = dispLabel + "\r\n";
}
dispLabel = dispLabel + str(count);
dispLabel = dispLabel + " 3.14 2.718 6.022 1023";
dispLabel = suppressToNLines(dispLabel, /*nlines=*/5);
if (dispLabel.length() > 0) {
fill(50);
text(dispLabel, 10, 10, 700, 80);
}
}
結果
それなりに動いている気がする。