summaryrefslogtreecommitdiff
path: root/editor.cpp
blob: a51c421fa436b1fb2d6667d2168f822ea5518b85 (plain) (blame)
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
#include "debugdraw.hpp"
#include "editor.hpp"
#include "model.hpp"
#include "ui.hpp"

static struct {
	UI* ui;
	UI::Toolbar* toolbar;
	UI::Button* mat_btn;
	UI::Modal* mat_win;
	UI::Label* mat_name;
	UI::Slider* metalness_input;
	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.mat_name->set_text(mat->name);
	} else {
		editor.metalness_input->disable();
		editor.mat_name->set_text("<no material>");
	}
}

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<UI::Modal>(
			editor.ui->root,
			"Material Editor"
		);
		auto container = editor.ui->create_element<UI::Container>(
			editor.mat_win->contents
		);
		editor.mat_win->handler = mat_win_handler;
		editor.mat_btn->disable();
		editor.mat_name = editor.ui->create_element<UI::Label>(
			container,
			""
		);
		editor.metalness_input = editor.ui->create_element<UI::Slider>(
			container,
			0.0f,
			1.0f
		);
		editor.metalness_input->input->handler = [](
			UI::Element* e,
			const UI::Message& m
		) {
			if (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->metalness = in->val;
			}
			return 0;
		};
		setup_mat_edit();
	}
	return 0;
}

void init_editor(UI* ui) {
	editor.ui = ui;
	editor.toolbar = ui->create_element<UI::Toolbar>(ui->root);
	editor.mat_btn = ui->create_element<UI::Button>(
		editor.toolbar,
		"Edit Material"
	);
	editor.mat_win = 0;
	editor.mat_btn->handler = mat_btn_handler;
}

void deinit_editor() {

}

void editor_on_select(Model_Instance* instance, int mesh) {
	if (!editor.ui->hovered) {
		editor.selected_inst = instance;
		editor.selected_mesh = mesh;
		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]
		);
	}
}