-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimulationWindow.pde
More file actions
150 lines (125 loc) · 3.64 KB
/
SimulationWindow.pde
File metadata and controls
150 lines (125 loc) · 3.64 KB
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
class SimulationWindow extends PApplet
{
public SimulationSettings settings;
public SimulationData data;
private boolean showWaterSources;
SimulationWindow(SimulationSettings settings)
{
this.settings = settings;
data = new SimulationData(settings);
}
void settings()
{
size(settings.getDisplayWidth(), settings.getDisplayHeight());
}
void setup()
{
surface.setTitle("Erosion Simulator");
// setDefaultClosePolicy(this, true);
}
void doSimulationStep()
{
if (settings.logToConsole) println("----------\nStarting simulation step",data.getSimulationStep());
data.water.doSimulationStep();
data.incrementSimulationStep();
if (settings.logToConsole) println("Finished simulation step");
}
void draw()
{
if (settings.displayScaleChanged)
{
surface.setSize(settings.getDisplayWidth(),settings.getDisplayHeight());
}
if (settings.running)
{
doSimulationStep();
}
PGraphics pg = data.canvas;
pg.beginDraw();
data.terrain.draw(pg, settings.displayGradient);
data.water.draw(pg);
pg.endDraw();
pushMatrix();
scale(settings.displayScale);
image(pg, 0,0);
popMatrix();
// Draw brush cursor
noFill();
stroke(0,255,0);
ellipseMode(RADIUS);
int radius = settings.activeBrush.getRadius() * settings.displayScale;
ellipse(mouseX,mouseY, radius,radius);
// Save mouse position for display in the settings window
data.mouseX = mouseX;
data.mouseY = mouseY;
}
void mousePressed()
{
doBrush(true);
}
void mouseDragged()
{
doBrush(false);
}
void mouseReleased()
{
settings.showWaterSources = false;
}
void doBrush(boolean isClick)
{
int scaledX = mouseX / settings.displayScale;
int scaledY = mouseY / settings.displayScale;
if (scaledX > settings.getWidth() || scaledY > settings.getHeight()) return;
boolean rightClick = (mouseButton == RIGHT);
switch (settings.getMouseMode())
{
case WATERSOURCE: // Modify water sources
if (isClick) settings.showWaterSources = true;
data.water.waterSources.applyBrush(scaledX, scaledY, settings.waterBrush, rightClick);
break;
case HEIGHT: // Modify terrain
data.terrain.heightmap.applyBrush(scaledX, scaledY, settings.terrainBrush, rightClick);
break;
case ROCK: // Modify terrain resistance
break;
case DEBUG: // Read heightmap values
float height = data.terrain.getHeightValue(scaledX, scaledY);
println("Clicked",scaledX,"x",scaledY,"y",height,"value");
// println("Clicked",scaledX,"x",scaledY,"y",height.toUnsignedString(),"value");
break;
}
}
void mouseWheel(MouseEvent event)
{
int currentBrushSize = sliderBrushRadius.getValueI();
sliderBrushRadius.setValue( currentBrushSize - event.getCount()*10 );
}
public void saveSimulationFrame()
{
String filename = String.format("%d%d%d-%d:%d:%d.png",year(),month(),day(),hour(),minute(),second());
String path = "Outputs/" + filename;
PImage colored = data.terrain.toGradientImage(settings.displayGradient);
colored.save(path);
println("Saved image", path);
}
public void saveHeightmap()
{
String filename = String.format("%d%d%d-%d:%d:%d.png",year(),month(),day(),hour(),minute(),second());
String path = "Outputs/" + filename;
Gradient grayscale = gradientPresets.get("Grayscale");
PImage heightmap = data.terrain.toGradientImage( grayscale );
PImage watersources = data.water.waterSources.toGradientImage( grayscale );
PImage packedHeightmap = ColorConverter.mergeChannels( createImage(heightmap.width,heightmap.height,RGB), heightmap, watersources );
packedHeightmap.save(path);
println("Saved image", path);
}
void keyPressed()
{
switch (key)
{
case 'p':
settings.logToConsole = !settings.logToConsole;
break;
}
}
}