Classiclll's Blog

an old boy

グラフィックスセッティングを保存(push)/復元(pop)するクラス by Mr. TomC

TomC氏による「グラフィックスの現在のセッティングを保存(push)/復元(pop)するクラス」が、本家のDiscource で提示されてました。結構役に立つかも。(リンクを忘れたので、スクリプトを掲載)

注意! このスクリプトは非公式なP5の機能を用いているので、Vupで動かなくなる可能性があります。
gSettings gs = new gSettings();

void setup(){fill(128);gs.push();}
void draw() {
fill(0);
rect(10,10,80,80);
gs.pop();
rect(40,40,40,40);
gs.push();
}

public class gSettings
{
Stack gs;
//
private class Gstate {
public boolean smooth;
public int rectMode, ellipseMode;
public int textAlign;
public float textSize;
public boolean tint;
public int tintColor;
public boolean fill;
public int fillColor;
public boolean stroke;
public int strokeColor;
public float strokeWeight;
//
public Gstate() {
smooth = g.smooth;
rectMode = g.rectMode;
ellipseMode = g.ellipseMode;
textAlign = g.textAlign;
// textSize = g.textSize;
tint = g.tint;
fill = g.fill;
stroke = g.stroke;
// tintColor = g.tintColor;
// fillColor = g.fillColor;
// strokeColor = g.strokeColor;
strokeWeight = g.strokeWeight;
}
public void setGstate() {
if(smooth) smooth(); else noSmooth();
rectMode(rectMode);
ellipseMode(ellipseMode);
textAlign(textAlign);
// textSize(textSize);
if(tint) tint(tintColor); else noTint();
if(fill) fill(fillColor); else noFill();
if(stroke) stroke(strokeColor); else noStroke();
strokeWeight(strokeWeight);
}
}

public gSettings () {
gs = new Stack();
}

public void push()
{
gs.push(new Gstate());
}

public void pop()
{
if (gs.empty()) return;
Gstate s = (Gstate) gs.pop();
s.setGstate();
}
}