-
Notifications
You must be signed in to change notification settings - Fork 0
/
SgAnimator.pde
312 lines (260 loc) · 8 KB
/
SgAnimator.pde
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
// programme python "key-mon" pour afficher les touches du clavier
// TODO:
// Revoir la fenêtre de sur-chargement de fichier (pour la version 0.8)
// Editor : Part selection menu when click on many overlaping parts
// Lib : Part affine transform per posture
// Display bones (connected pivots)
// Option to reset control values (by double clicking on them ?)
// Elastic function
// Option to duplicate previous AnimationCollection when new animCollection
// UV coords in polygon class
// Lib : avatar.playSequentially()
// Ouverture de plusieurs fichiers avec Tabs
// Librairie d'Avatars à l'ouverture d'un fichier
// Add a chart for every Animation to show function progression over time
/* IDÉES ABANDONNÉES :
* Lib : Points de pivots différents par postures
*/
/*
BUGS:
* controllerClicked is not reseted after a mouse drag on a controller (eg. timeline sliders)
* Check physics shapes after soft-transforming
* Resize window doesn't resize UI immediately (click on displaced controls are missed)
*/
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.utils.*;
import gwel.game.anim.*;
import gwel.game.graphics.*;
import gwel.game.entities.*;
import gwel.game.utils.*;
import controlP5.*;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.*;
import java.lang.reflect.Field;
String version = "0.9.1";
String appName = "SgAnimator " + version;
MainScreen mainScreen;
LoadScreen loadScreen;
Screen welcomeScreen;
Screen helpScreen1;
Screen helpScreenEasing;
Screen currentScreen;
Screen previousScreen;
Tooltip tooltip;
PRenderer renderer;
PGraphicsRenderer cameraRenderer;
Avatar avatar;
String baseFilename; // Used for window title and to save file (independent of its source format)
ComplexShape selected = null;
String[] functionsName;
PostureCollection postures;
int postureIndex = 0;
boolean fileDirty = false; // When true, postures in editor are yet to be saved in avatar
Animation animationClipboard;
boolean showUI = false;
boolean paramLocked = false;
boolean mustUpdateUI = false;
File fileToLoad = null; // Change current screen to loadScreen
PFont defaultFont, defaultFontSmall;
PFont titleFont;
PFont tooltipFont, tooltipFontSmall;
PFont iconFont;
PImage checkboard;
void settings() {
//fullScreen();
size(1024, 700); // P2D renderer is needed to draw the chessboard texture under the camera, but it breaks SVG imports
noSmooth(); // Needed for the camera view
}
void setup() {
windowResizable(true);
windowTitle(appName);
// Sets the texture filtering to NEAREST sampling
// Used by camera pixelisation with P2D renderer
//((PGraphicsOpenGL) g).textureSampling(2);
// Used for checkboard pattern
textureWrap(REPEAT);
cp5 = new ControlP5(this);
titleFont = createFont("PrintBold-J5o.ttf", 48);
defaultFont = createFont("DejaVu Sans Mono", 24);
defaultFontSmall = createFont("DejaVu Sans Mono", 12);
tooltipFont = createFont("PrintClearly-GGP.ttf", 32);
tooltipFontSmall = createFont("PrintClearly-GGP.ttf", 25);
iconFont = createFont("fa-solid-900.ttf", 20);
checkboard = loadImage("checkboard.png");
renderer = new PRenderer(this);
cameraRenderer = new PGraphicsRenderer(this);
tooltip = new Tooltip();
mainScreen = new MainScreen();
mainScreen.hideUI();
welcomeScreen = new WelcomeScreen();
helpScreen1 = new HelpScreen1();
helpScreenEasing = new HelpScreenEasing();
currentScreen = welcomeScreen;
int numFn = Animation.timeFunctions.length;
functionsName = new String[numFn];
for (int i=0; i<numFn; i++) {
int idx = Animation.timeFunctions[i].getName().lastIndexOf('.');
functionsName[i] = Animation.timeFunctions[i].getName().substring(idx+3);
}
//File toLoad = new File("/home/gweltaz/Bureau/svg/bois.json");
//loadJsonFile(toLoad);
}
void select(ComplexShape part) {
showUI = true;
selected = part;
renderer.setSelected(part);
if (selected == null) {
cp5.remove("accordion");
accordion = null;
} else {
//avatar.getShape().invalidateBoundingBox(); // Fixes bb not updated between parts selection (which moves when playing)
updateUI();
}
}
void savePosture() {
Posture posture = new Posture();
String postureName = mainScreen.transport.postureName.getText();
if (postureName == null || postureName.trim().isEmpty())
postureName = "posture" + postureIndex;
posture.name = postureName;
posture.duration = mainScreen.transport.animDuration.getValue();
Animation[][] groups = new Animation[avatar.getPartsList().length][];
Arrays.fill(groups, null);
for (int i = 0; i < groups.length; i++) {
ComplexShape part = avatar.getPartsList()[i];
if (part.getAnimationList().length > 0)
groups[i] = part.getAnimationList();
}
posture.groups = groups;
if (postureIndex >= postures.size()) {
postures.addPosture(posture);
} else {
postures.updatePosture(postureIndex, posture);
}
avatar.postures = postures;
fileDirty = false;
}
void draw() {
if (fileToLoad != null) {
loadScreen.createModalBox(fileToLoad);
previousScreen = currentScreen;
currentScreen = loadScreen;
fileToLoad = null;
}
currentScreen.draw();
tooltip.update(17); // 17ms steps
}
public void keyPressed(KeyEvent event) {
currentScreen.keyPressed(event);
}
public void keyReleased(KeyEvent event) {
currentScreen.keyReleased(event);
}
public void mousePressed(MouseEvent event) {
currentScreen.mousePressed(event);
}
public void mouseReleased(MouseEvent event) {
currentScreen.mouseReleased(event);
}
public void mouseClicked(MouseEvent event) {
currentScreen.mouseClicked(event);
}
public void mouseWheel(MouseEvent event) {
currentScreen.mouseWheel(event);
}
public void mouseDragged(MouseEvent event) {
currentScreen.mouseDragged(event);
}
public abstract class Screen {
public void draw() {
}
public void keyPressed(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
public void mouseClicked(MouseEvent event) {
}
public void mouseWheel(MouseEvent event) {
}
public void mouseDragged(MouseEvent event) {
}
}
void drawKey(int x, int y, String k, float height) {
int fontSize = floor(height * 0.5f);
if (fontSize <= 12)
textFont(defaultFontSmall);
else
textFont(defaultFont);
textSize(fontSize);
float margin = 0.1f * height;
float wi = max(height - 2*margin, textWidth(k) + 10);
float w = wi + 2*margin;
fill(180);
stroke(63);
rect(x, y, w, height, 0.15f * height);
fill(220);
stroke(240);
float hi = height * 0.8f;
rect(x + margin, y + 0.8f*margin, wi, hi, 0.15f * hi);
fill(0);
text(k, x + 2*margin, y + hi - 1.5f*margin);
}
public class Tooltip {
private String tip;
private int textOffset;
private int timer;
private int duration;
private final int fadeout = 600;
private boolean error = false;
private boolean small = false;
public Tooltip() {
timer = 0;
duration = -999;
}
public void update(int timedelta) {
timer += timedelta;
int fadetime = max(0, duration+fadeout - timer);
if (timer < duration) {
fill(error ? #ff0000 : 0);
if (small)
textFont(tooltipFontSmall);
else
textFont(tooltipFont);
text(tip, textOffset, height-20);
} else if (fadetime > 0) {
int alpha = round(map(fadetime, 0, fadeout, 0, 255));
fill(error ? #ff0000 : 0, alpha);
if (small)
textFont(tooltipFontSmall);
else
textFont(tooltipFont);
text(tip, textOffset, height-20);
}
}
public void say(String s) {
tip = s;
timer = 0;
duration = s.length() * 60;
textFont(tooltipFont);
int textWidth = floor(textWidth(s));
textOffset = floor((width-textWidth) / 2);
small = false;
error = false;
if (textOffset < 0) {
small = true;
textFont(tooltipFontSmall);
textWidth = floor(textWidth(s));
textOffset = floor((width-textWidth) / 2);
}
}
public void warn(String s) {
say(s);
error = true;
}
}