Asset viewer

This commit is contained in:
John Alanbrook 2022-08-19 16:25:08 +00:00
parent 37ef9affb2
commit dda8f1cc24
4 changed files with 152 additions and 191 deletions

View file

@ -49,7 +49,13 @@ static bool renderGizmos = false;
static bool showGrid = true; static bool showGrid = true;
static bool debugDrawPhysics = false; static bool debugDrawPhysics = false;
const char *allowed_extensions[] = {"jpg", "png", "gltf", "glsl"}; const char *allowed_extensions[] = {"jpg", "png", "rb", "wav", "mp3", };
void text_ed_cb(GLFWwindow *win, unsigned int codepoint);
void asset_srch_cb(GLFWwindow *win, unsigned int codepoint)
{
printf("Pushed %d.\n", codepoint);
}
static const char *editor_filename = "editor.ini"; static const char *editor_filename = "editor.ini";
@ -60,7 +66,7 @@ struct asset {
static struct asset *assets = NULL; static struct asset *assets = NULL;
static char asset_search_buffer[100] = {0}; static char asset_search_buffer[100] = {'\0'};
struct fileasset *selected_asset; struct fileasset *selected_asset;
@ -68,6 +74,8 @@ static int selected_index = -1;
int show_desktop = 0; int show_desktop = 0;
int tex_view = 0;
static int grid1_width = 1; static int grid1_width = 1;
static int grid1_span = 100; static int grid1_span = 100;
static int grid2_width = 3; static int grid2_width = 3;
@ -158,77 +166,23 @@ end:
return found; return found;
} }
static int MyCallback() { void filter_asset_srch()
/*
if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) {
data->InsertChars(data->CursorPos, "..");
} else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) {
if (data->EventKey == ImGuiKey_UpArrow) {
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, "Pressed Up!");
data->SelectAll();
} else if (data->EventKey == ImGuiKey_DownArrow) {
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, "Pressed Down!");
data->SelectAll();
}
} else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) {
int i = 0;
if (data->Buf[0] == '\0')
while (i < shlen(assets))
assets[i].value->searched = true;
else
while (i < shlen(assets))
assets[i].value->searched =
(strstr(assets[i].value->filename, data->Buf) ==
NULL) ? false : true;
}
*/
return 0;
}
static int TextEditCallback() // ImGuiInputTextCallbackData * data)
{ {
/* if (asset_search_buffer[0] == '\0') {
static int dirty = 0; for (int i = 0; i < shlen(assets); i++)
assets[i].value->searched = true;
if (data->EventChar == '\n') { } else {
dirty = 1; for (int i = 0; i < shlen(assets); i++)
} else if (data->EventChar == '(') { assets[i].value->searched = (strstr(assets[i].value->filename, asset_search_buffer) == NULL) ? false : true;
//data->EventChar = 245;
dirty = 2;
} else if (data->EventChar == ')') {
dirty = 3;
}
if (data->EventFlag == ImGuiInputTextFlags_CallbackAlways) {
if (dirty == 1) {
dirty = 0;
char *c = &data->Buf[data->CursorPos - 2];
while (*c != '\n')
c--;
c++;
if (isblank(*c)) {
char *ce = c;
while (isblank(*ce))
ce++;
data->InsertChars(data->CursorPos, c, ce);
}
} }
} }
*/ void filter_autoindent()
return 0; {
} }
void editor_save() { void editor_save() {
FILE *feditor = fopen(editor_filename, "w+"); FILE *feditor = fopen(editor_filename, "w+");
fwrite(&editor, sizeof(editor), 1, feditor); fwrite(&editor, sizeof(editor), 1, feditor);
@ -236,8 +190,17 @@ void editor_save() {
} }
static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) { static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) {
if (editor_wantkeyboard()) if (editor_wantkeyboard()) {
if (editor.asset_srch & NK_EDIT_ACTIVE) {
filter_asset_srch();
}
if ((editor.text_ed & NK_EDIT_ACTIVE) && key == GLFW_KEY_ENTER) {
filter_autoindent();
}
return; return;
}
if (action == GLFW_RELEASE) { if (action == GLFW_RELEASE) {
switch(key) { switch(key) {
@ -416,14 +379,21 @@ void editor_init(struct mSDLWindow *window) {
glfwSetKeyCallback(window->window, edit_input_cb); glfwSetKeyCallback(window->window, edit_input_cb);
glfwSetMouseButtonCallback(window->window, edit_mouse_cb); glfwSetMouseButtonCallback(window->window, edit_mouse_cb);
//glfwSetCharCallback(window->window, text_ed_cb);
//glfwSetCharCallback(window->window, asset_srch_cb);
} }
// TODO: Implement int editor_wantkeyboard() {
int editor_wantkeyboard() { return 0; } if (editor.text_ed & NK_EDIT_ACTIVE)
return 1;
const int nuk_std = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | if (editor.asset_srch & NK_EDIT_ACTIVE)
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE; return 1;
return 0;
}
const int nuk_std = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_TITLE;
const struct nk_rect nk_rect_std = {250, 250, 250, 250}; const struct nk_rect nk_rect_std = {250, 250, 250, 250};
@ -439,12 +409,6 @@ void editor_project_gui() {
// phys2d_dbgdrawcircle(objects[i]->circle); // phys2d_dbgdrawcircle(objects[i]->circle);
} }
static char text[3][64];
static int text_len[3];
static const char *items[] = {"Item 0", "item 1", "item 2"};
static int selected_item = 0;
static int check = 1;
/* /*
if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) { if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) {
nk_layout_row_dynamic(ctx, 25, 1); nk_layout_row_dynamic(ctx, 25, 1);
@ -628,38 +592,14 @@ void editor_project_gui() {
NK_MENU_START(assets) NK_MENU_START(assets)
nk_layout_row_dynamic(ctx,25,1); nk_layout_row_dynamic(ctx,25,1);
nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, asset_search_buffer, 100, nk_filter_ascii); editor.asset_srch = nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, asset_search_buffer, 100, nk_filter_ascii);
/*
if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) {
data->InsertChars(data->CursorPos, "..");
} else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) {
if (data->EventKey == ImGuiKey_UpArrow) {
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, "Pressed Up!");
data->SelectAll();
} else if (data->EventKey == ImGuiKey_DownArrow) {
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, "Pressed Down!");
data->SelectAll();
}
} else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) {
int i = 0;
if (data->Buf[0] == '\0')
while (i < shlen(assets))
assets[i].value->searched = true;
else
while (i < shlen(assets))
assets[i].value->searched =
(strstr(assets[i].value->filename, data->Buf) ==
NULL) ? false : true;
}
*/
if (nk_button_label(ctx, "Reload all files")) if (nk_button_label(ctx, "Reload all files"))
get_all_files(); get_all_files();
for (int i = 0; i < shlen(assets); i++) { for (int i = 0; i < shlen(assets); i++) {
if (!assets[i].value->searched) if (!assets[i].value->searched)
continue; continue;
@ -785,7 +725,7 @@ void editor_selectasset(struct fileasset *asset) {
float tex_scale = (float) ASSET_WIN_SIZE / (float)tex_gui_anim.tex->width; float tex_scale = (float) ASSET_WIN_SIZE / (float)tex_gui_anim.tex->width;
if (tex_scale >= 10.f) if (tex_scale >= 10.f)
tex_scale = 10.f; tex_scale = 10.f;
} else if (!strcmp(ext + 1, "glsl")) { } else if (!strcmp(ext + 1, "rb")) {
asset->type = ASSET_TYPE_TEXT; asset->type = ASSET_TYPE_TEXT;
FILE *fasset = fopen(asset->filename, "rb"); FILE *fasset = fopen(asset->filename, "rb");
@ -801,78 +741,58 @@ void editor_selectasset(struct fileasset *asset) {
} }
void editor_selectasset_str(char *path) { void editor_selectasset_str(char *path) {
struct fileasset *asset = (struct fileasset *)shget(assets, path); struct fileasset *asset = shget(assets, path);
if (asset) if (asset)
editor_selectasset(asset); editor_selectasset(asset);
} }
void editor_asset_tex_gui(struct Texture *tex) { void editor_asset_tex_gui(struct Texture *tex) {
/* nuke_labelf("%dx%d", tex->width, tex->height);
ImGui::Text("%dx%d", tex->width, tex->height); nuke_prop_float("Zoom", 0.01f, &tex_scale, 10.f, 0.1f, 0.01f);
ImGui::SliderFloat("Zoom", &tex_scale, 0.01f, 10.f);
int old_sprite = tex->opts.sprite; int old_sprite = tex->opts.sprite;
ImGui::Checkbox("Sprite", (bool *) &tex->opts.sprite);
nuke_checkbox("Sprite", &tex->opts.sprite);
if (old_sprite != tex->opts.sprite) if (old_sprite != tex->opts.sprite)
tex_gpu_load(tex); tex_gpu_load(tex);
nuke_radio_btn("Raw", &tex_view, 0);
nuke_radio_btn("View 1", &tex_view, 1);
nuke_radio_btn("View 2", &tex_view, 2);
ImGui::RadioButton("Raw", &tex_view, 0); nuke_checkbox("Animation", &tex->opts.animation);
ImGui::SameLine(); ImGui::RadioButton("View 1", &tex_view, 1);
ImGui::SameLine(); ImGui::RadioButton("View 2", &tex_view, 2);
ImGui::Checkbox("Animation", (bool *) &tex->opts.animation);
if (tex->opts.animation) { if (tex->opts.animation) {
int old_frames = tex->anim.frames; int old_frames = tex->anim.frames;
int old_ms = tex->anim.ms; int old_ms = tex->anim.ms;
ImGui::SliderInt("Frames", &tex->anim.frames, 1, 20);
ImGui::SliderInt("FPS", &tex->anim.ms, 1, 24);
nuke_property_int("Frames", 1, &tex->anim.frames, 20, 1);
nuke_property_int("FPS", 1, &tex->anim.ms, 24, 1);
if (tex_gui_anim.playing) { if (tex_gui_anim.playing) {
if (ImGui::Button("Pause")) if (nuke_btn("Pause"))
anim_pause(&tex_gui_anim); anim_pause(&tex_gui_anim);
ImGui::SameLine(); if (tex_gui_anim.playing && nuke_btn("Stop"))
if (tex_gui_anim.playing && ImGui::Button("Stop"))
anim_stop(&tex_gui_anim); anim_stop(&tex_gui_anim);
} else { } else {
if (ImGui::Button("Play")) if (nuke_btn("Play"))
anim_play(&tex_gui_anim); anim_play(&tex_gui_anim);
ImGui::SameLine(); if (nuke_btn("Bkwd"))
if (ImGui::Button("Bkwd"))
anim_bkwd(&tex_gui_anim); anim_bkwd(&tex_gui_anim);
if (nuke_btn("Fwd"))
ImGui::SameLine();
if (ImGui::Button("Fwd"))
anim_fwd(&tex_gui_anim); anim_fwd(&tex_gui_anim);
} }
nuke_labelf("Frame %d/%d", tex_gui_anim.frame+1, tex_gui_anim.tex->anim.frames);
ImGui::SameLine();
ImGui::Text("Frame %d/%d", tex_gui_anim.frame + 1,
tex_gui_anim.tex->anim.frames);
if (old_frames != tex->anim.frames || old_ms != tex->anim.ms) if (old_frames != tex->anim.frames || old_ms != tex->anim.ms)
tex_anim_set(&tex_gui_anim); tex_anim_set(&tex_gui_anim);
}
/*
ImVec2 uv0 = ImVec2(tex_gui_anim.uv.x, tex_gui_anim.uv.y); ImVec2 uv0 = ImVec2(tex_gui_anim.uv.x, tex_gui_anim.uv.y);
ImVec2 uv1 = ImVec2(tex_gui_anim.uv.x + tex_gui_anim.uv.w, ImVec2 uv1 = ImVec2(tex_gui_anim.uv.x + tex_gui_anim.uv.w,
@ -887,27 +807,39 @@ void editor_asset_tex_gui(struct Texture *tex) {
tex->height * tex_scale)); tex->height * tex_scale));
} }
*/ */
nk_layout_row_static(ctx, tex->height*tex_scale, tex->width*tex_scale, 1);
nk_image(ctx, nk_image_id(tex->id));
}
void text_ed_cb(GLFWwindow *win, unsigned int codepoint)
{
printf("Pressed button %d\n", codepoint);
if (editor.text_ed & NK_EDIT_ACTIVE) {
if (codepoint == '\n') {
printf("Hit newline.\n");
}
}
} }
void editor_asset_text_gui(char *text) { void editor_asset_text_gui(char *text) {
/* nk_layout_row_dynamic(ctx, 600, 1);
ImGui::InputTextMultiline("File edit", text, ASSET_TEXT_BUF, editor.text_ed = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX, text, ASSET_TEXT_BUF, nk_filter_ascii);
ImVec2(600, 500),
ImGuiInputTextFlags_CallbackAlways | nuke_nel(4);
ImGuiInputTextFlags_CallbackCharFilter, if (nk_button_label(ctx, "Save")) {
TextEditCallback);
if (ImGui::Button("Save")) {
FILE *f = fopen(selected_asset->filename, "wd"); FILE *f = fopen(selected_asset->filename, "wd");
size_t len = strlen(text); size_t len = strlen(text);
fwrite(text, len, 1, f); fwrite(text, len, 1, f);
fclose(f); fclose(f);
} }
*/
/* TODO: Nicer formatting for text input. Auto indent. */
} }
void editor_asset_gui(struct fileasset *asset) { void editor_asset_gui(struct fileasset *asset) {
nk_begin(ctx, "Asset Viewer", nk_rect_std, nuk_std); NK_FORCE(asset)
nuke_nel(2); nuke_nel(2);
nk_labelf(ctx, NK_TEXT_LEFT, "%s", selected_asset->filename); nk_labelf(ctx, NK_TEXT_LEFT, "%s", selected_asset->filename);
@ -929,7 +861,7 @@ void editor_asset_gui(struct fileasset *asset) {
break; break;
} }
nk_end(ctx); NK_FORCE_END()
} }
void editor_makenewobject() {} void editor_makenewobject() {}

View file

@ -33,6 +33,7 @@ struct editorVars {
editor_win viewmode; editor_win viewmode;
editor_win debug; editor_win debug;
editor_win assets; editor_win assets;
editor_win asset;
editor_win repl; editor_win repl;
editor_win export; editor_win export;
editor_win level; editor_win level;
@ -40,6 +41,8 @@ struct editorVars {
editor_win components; editor_win components;
editor_win simulate; editor_win simulate;
editor_win prefab; editor_win prefab;
nk_flags text_ed;
nk_flags asset_srch;
}; };
struct mGameObject; struct mGameObject;

View file

@ -60,14 +60,30 @@ void nk_property_float2(struct nk_context *ctx, const char *label, float min, fl
nk_property_float(ctx, "#Y", min, &val[1], max, step, dragstep); nk_property_float(ctx, "#Y", min, &val[1], max, step, dragstep);
} }
int nuke_btn(const char *lbl) {
return nk_button_label(ctx, lbl);
}
void nuke_property_int(const char *lbl, int min, int *val, int max, int step) {
nk_property_int(ctx, lbl, min, val, max, step, step);
}
void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp) { void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp) {
if (nk_option_label(ctx, label, (bool)*val == cmp)) *val = cmp; if (nk_option_label(ctx, label, (bool)*val == cmp)) *val = cmp;
} }
void nuke_radio_btn(const char *lbl, int *val, int cmp) {
nk_radio_button_label(ctx, lbl, val, cmp);
}
void nuke_checkbox(const char *lbl, int *val) {
nk_checkbox_label(ctx, lbl, val);
}
void nuke_nel(int cols) { void nuke_nel(int cols) {
nk_layout_row_dynamic(ctx, 25, cols); nk_layout_row_dynamic(ctx, 25, cols);
} }
void nuke_label(const char *s) { void nuke_label(const char *s) {
nk_label(ctx, NK_TEXT_LEFT, s);
} }

View file

@ -14,8 +14,18 @@ void nuke_end();
void nk_property_float3(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep); void nk_property_float3(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep);
void nk_property_float2(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep); void nk_property_float2(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep);
void nuke_property_int(const char *lbl, int min, int *val, int max, int step);
void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp); void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp);
void nuke_radio_btn(const char *lbl, int *val, int cmp);
void nuke_checkbox(const char *lbl, int *val);
void nuke_nel(int cols); void nuke_nel(int cols);
void nuke_label(const char *s); void nuke_label(const char *s);
void nuke_prop_float(const char *label, float min, float *val, float max, float step, float dragstep);
int nuke_btn(const char *lbl);
#define nuke_labelf(STR, ...) nk_labelf(ctx, NK_TEXT_LEFT, STR, __VA_ARGS__)
#define nuke_prop_float(LABEL, MIN, VAL, MAX, STEP, DRAG) nk_property_float(ctx, LABEL, MIN, VAL, MAX, STEP, DRAG)
#endif #endif