#include "debugdraw.hpp" #include "editor.hpp" #include "model.hpp" #include "scene.hpp" #include "ui.hpp" #include "world.hpp" static struct { UI* ui; UI::Toolbar* toolbar; UI::Button* mat_btn; UI::Modal* mat_win; UI::Label* mat_name; UI::Slider* metalness_input; UI::Slider* roughness_input; UI::Slider* ao_input; Entity_Id selected; Model_Instance* selected_inst; int selected_mesh; } editor; static int mat_win_handler(UI::Element* e, const UI::Message& m) { (void)e; if (m.type == UI::Message::Type::destroy) { editor.mat_btn->enable(); editor.mat_win = 0; } return 0; } static void setup_mat_edit() { if (editor.selected_inst) { Mesh* meshes = editor.selected_inst->m->get_meshes(); Mesh& msh = meshes[editor.selected_mesh]; Material* mat = msh.material; editor.metalness_input->enable(); editor.metalness_input->input->set_val(mat->metalness); editor.roughness_input->enable(); editor.roughness_input->input->set_val(mat->roughness); editor.ao_input->enable(); editor.ao_input->input->set_val(mat->ao); editor.mat_name->set_text(mat->name); } else { editor.metalness_input->disable(); editor.roughness_input->disable(); editor.ao_input->disable(); editor.mat_name->set_text(""); } } static int mat_btn_handler(UI::Element* e, const UI::Message& m) { (void)e; if (m.type == UI::Message::Type::click) { editor.mat_win = editor.ui->create_element( editor.ui->root, "Material Editor" ); int rows[] = { 200, 250, 0 }; editor.mat_win->handler = mat_win_handler; editor.mat_btn->disable(); editor.mat_name = editor.ui->create_element( editor.mat_win->contents, "" ); auto container = editor.ui->create_element( editor.mat_win->contents, rows ); #define bind_mat_float(el, name, point) \ editor.ui->create_element( \ container, \ name \ ); \ el = editor.ui->create_element( \ container, \ 0.0f, \ 1.0f \ ); \ el->input->handler = []( \ UI::Element* e, \ const UI::Message& m \ ) { \ if ( \ m.type == UI::Message::Type::input_changed && \ editor.selected_inst \ ) { \ UI::Float_Input* in = (UI::Float_Input*)e; \ Mesh* meshes = editor.selected_inst->m->get_meshes(); \ Mesh& mesh = meshes[editor.selected_mesh]; \ Material* mat = mesh.material; \ mat->point = in->val; \ } \ return 0; \ }; bind_mat_float( editor.metalness_input, "Metalness", metalness ); bind_mat_float( editor.roughness_input, "Roughness", roughness ); bind_mat_float( editor.ao_input, "Ambient Occlusion", ao ); #undef bind_mat_float setup_mat_edit(); } return 0; } void init_editor(UI* ui) { editor.ui = ui; editor.toolbar = ui->create_element(ui->root); editor.mat_btn = ui->create_element( editor.toolbar, "Edit Material" ); editor.mat_win = 0; editor.mat_btn->handler = mat_btn_handler; } void deinit_editor() { } void editor_on_select(World& w, Entity_Id e, int m) { if (!editor.ui->hovered) { editor.selected = e; if (e && w.has(e)) { auto& cm = w.get(e); editor.selected_inst = cm.i; editor.selected_mesh = m; } else { editor.selected_inst = 0; editor.selected_mesh = -1; } if (editor.mat_win) { setup_mat_edit(); } } } void editor_draw(Line_Renderer& lr) { if (editor.selected_inst) { lr.add_box( editor.selected_inst->bounds[editor.selected_mesh] ); } }