#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <helianthus.h>
#include "common.h"
static int answer = 0;
static char buffer[4096] = {};
static Sound beep;
static void scanDirectoryRecursive(const char *path, char **outTextBegin, char *outTextEnd, int level) {
Directory d = openDirectory(path);
if (d) {
int count = directoryGetCount(d);
int lenPrefix = level*4 + 1;
int lenPath = strlen(path) + 1;
int lenName = 0;
for(int i = 0; i < count; ++i) {
int ln = strlen(directoryGet(d, i));
if (ln > lenName) lenName = ln;
}
int lenLine = lenPrefix + lenName + 1;
char *subpath = calloc(1, lenPath + lenName + 1);
strcpy(subpath, path);
subpath[lenPath - 1] = '/';
for(int i = 0; i < count; ++i) {
if (*outTextBegin + lenLine > outTextEnd) break;
const char *name = directoryGet(d, i);
for(int i = 0; i < lenPrefix; ++i) *((*outTextBegin)++) = '-';
char *typeSign = *outTextBegin - 1;
strcpy(*outTextBegin, name);
*outTextBegin += strlen(name);
*((*outTextBegin)++) = '\n';
strcpy(subpath + lenPath, name);
if (directoryExists(subpath)) {
*typeSign = '>';
scanDirectoryRecursive(subpath, outTextBegin, outTextEnd, level+1);
} else
if (fileExists(subpath)) {
*typeSign = '*';
} else {
*typeSign = '#';
}
}
free(subpath);
closeDirectory(d);
}
}
void commonInit() {
char *bufPtr = buffer;
scanDirectoryRecursive("data", &bufPtr, bufPtr + sizeof(buffer) - 1, 0);
beep = createSound("data/sound/beep.ogg");
}
void commonDraw() {
saveState();
noFill();
textFontDefault();
textSize(16);
text(16, 48, buffer);
textf(16, 16, "answer: %d, fps: %6.2f", answer, 1/worldGetFrameTime());
if (mouseWentDown("left"))
soundPlay(beep, FALSE);
if (mouseWentDown("middle"))
askTextEx("Test\ntext input", buffer, sizeof(buffer), keyDown("any shift"), keyDown("any ctrl"));
if (mouseWentDown("right")) {
if (keyDown("any ctrl")) {
answer = questionBox3("Test question box3\nwith test message.", "cancel", "no", "yes");
} else
if (keyDown("any shift")) {
answer = questionBox("Test question box\nwith test message.", "no", "yes");
} else {
messageBox("Test message box\nwith test message.");
}
}
restoreState();
}