Skip to content

Commit 731a915

Browse files
committed
object names
1 parent 0733365 commit 731a915

7 files changed

Lines changed: 104 additions & 12 deletions

File tree

src/editor.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const float icon_size = 64.0f;
1616
const float padding = 10.0f;
1717
const float cell_size = icon_size + padding;
1818

19+
static void assign_entity_name(Entity& entity, const char* new_name) {
20+
if (!new_name || new_name[0] == '\0') return;
21+
entity.name = new_name;
22+
}
23+
1924
void Editor::handle_input() {
2025
float speed = 0.1f;
2126
Entity* e = scene.get_selected();
@@ -113,8 +118,8 @@ void Editor::draw_ui(Shader shader) {
113118

114119
if (ImGui::MenuItem("Dublicate")) {
115120
Entity ent_copy = ent;
116-
ent_copy.name = ent.name + " copy";
117121
ent_copy.id = static_cast<int>(scene.entities.size());
122+
ent_copy.name = scene.make_default_name_for(ent_copy);
118123
scene.entities.push_back(ent_copy);
119124
}
120125

@@ -130,10 +135,10 @@ void Editor::draw_ui(Shader shader) {
130135
if (ImGui::MenuItem(a.name.c_str())) {
131136
Entity e;
132137
e.id = static_cast<int>(scene.entities.size());
133-
e.name = "Object " + std::to_string(e.id);
134138
e.type = a.type;
135139
e.asset = &a;
136140
e.segments = 16;
141+
e.name = scene.make_default_name_for(e);
137142

138143
if (a.isProcedural) {
139144
e.model = a.generator(e.segments);
@@ -153,14 +158,13 @@ void Editor::draw_ui(Shader shader) {
153158

154159
if (renaming_index != -1) {
155160
ImGui::OpenPopup("Rename");
156-
renaming_index = -2;
157161
}
158162

159163
if (ImGui::BeginPopupModal("Rename", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
160164
ImGui::InputText("##rename", rename_buf, IM_ARRAYSIZE(rename_buf));
161165
if (ImGui::Button("OK")) {
162-
if (renaming_index == -2) {
163-
scene.entities[scene.selected].name = rename_buf;
166+
if (renaming_index >= 0 && renaming_index < static_cast<int>(scene.entities.size())) {
167+
assign_entity_name(scene.entities[renaming_index], rename_buf);
164168
}
165169
renaming_index = -1;
166170
ImGui::CloseCurrentPopup();
@@ -187,6 +191,14 @@ void Editor::draw_ui(Shader shader) {
187191

188192
Entity* e = scene.get_selected();
189193
if (e) {
194+
ImGui::Separator();
195+
char inspector_name[128] = {};
196+
const size_t copied = e->name.copy(inspector_name, sizeof(inspector_name) - 1);
197+
inspector_name[copied] = '\0';
198+
if (ImGui::InputText("Name", inspector_name, IM_ARRAYSIZE(inspector_name))) {
199+
assign_entity_name(*e, inspector_name);
200+
}
201+
190202
ImGui::Separator();
191203
ImGui::Text("Transform");
192204
float pos[3] = { e->position.x, e->position.y, e->position.z };
@@ -334,13 +346,11 @@ void Editor::draw_ui(Shader shader) {
334346
ImGui::InputFloat("Intensity", &e->light.intensity, 0.1f, 1.0f, "%.2f");
335347
ImGui::InputFloat("Range", &e->light.range, 0.1f, 1.0f, "%.2f");
336348

337-
static int light_counter = 0;
338-
339349
if (!e->light_created)
340350
{
341351
e->light = create_lighting(e->position, e->light.color);
342352

343-
e->light.id = light_counter++;
353+
e->light.id = allocate_light_id();
344354
e->light.light = CreateLight(LIGHT_POINT, e->position, Vector3Zero(), e->light.color, shader);
345355

346356
e->light_created = true;
@@ -520,4 +530,4 @@ void Editor::draw_assets_ui() {
520530
}
521531

522532
ImGui::End();
523-
}
533+
}

src/headers/entity.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
enum ObjectType { CUBE, SPHERE, CONE, CYLINDER, HEMISPHERE, TORUS };
99

10+
inline const char* object_type_name(ObjectType type) {
11+
switch (type) {
12+
case CUBE: return "Cube";
13+
case SPHERE: return "Sphere";
14+
case CONE: return "Cone";
15+
case CYLINDER: return "Cylinder";
16+
case HEMISPHERE: return "HemiSphere";
17+
case TORUS: return "Torus";
18+
default: return "Object";
19+
}
20+
}
21+
1022
struct ModelAsset {
1123
std::string name;
1224
std::string filepath;
@@ -84,6 +96,6 @@ struct Entity {
8496
segments(16),
8597
type(CUBE)
8698
{
87-
name = "Object " + std::to_string(id);
99+
name = object_type_name(type);
88100
}
89101
};

src/headers/lighting.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ struct Lighting {
1818
};
1919

2020
Lighting create_lighting(Vector3 pos, Color color);
21-
void update_lighting(Shader shader, Lighting& l);
21+
void update_lighting(Shader shader, Lighting& l);
22+
int allocate_light_id();

src/headers/scene.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "entity.h"
44
#include "lighting.h"
55
#include <memory>
6+
#include <string>
67

78
struct Scene {
89
std::vector<Entity> entities;
@@ -11,5 +12,7 @@ struct Scene {
1112
int selected = -1;
1213

1314
Entity* get_selected();
15+
std::string make_unique_name(const std::string& base_name) const;
16+
std::string make_default_name_for(const Entity& entity) const;
1417
void release_resources();
1518
};

src/lighting.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ void update_lighting(Shader shader, Lighting& l) {
1818
UpdateLightValues(shader, l.light);
1919
}
2020

21+
int allocate_light_id()
22+
{
23+
static int light_id_counter = 0;
24+
return light_id_counter++;
25+
}
26+
2127
Lighting create_lighting(Vector3 pos, Color color)
2228
{
2329
Lighting l = {};
@@ -28,4 +34,4 @@ Lighting create_lighting(Vector3 pos, Color color)
2834
l.intensity = 1.0f;
2935
l.range = 5.0f;
3036
return l;
31-
}
37+
}

src/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
#include "headers/editor.h"
77
#include "headers/camera.h"
88

9+
static Entity make_entity_from_asset(Scene& scene, ModelAsset* asset) {
10+
Entity entity;
11+
entity.id = static_cast<int>(scene.entities.size());
12+
entity.type = asset->type;
13+
entity.asset = asset;
14+
entity.segments = 16;
15+
entity.name = scene.make_default_name_for(entity);
16+
17+
if (asset->isProcedural) {
18+
entity.model = asset->generator(entity.segments);
19+
store_uv(&entity);
20+
} else {
21+
entity.model = asset->loadedModel;
22+
}
23+
24+
entity.texture = {0};
25+
return entity;
26+
}
27+
928
int main() {
1029
if (!std::filesystem::exists("assets")) std::filesystem::create_directory("assets");
1130

@@ -26,6 +45,26 @@ int main() {
2645
load_models();
2746
load_textures();
2847

48+
for (auto& asset : assets) {
49+
if (asset.type != SPHERE) continue;
50+
51+
Entity light_entity = make_entity_from_asset(editor.scene, &asset);
52+
light_entity.has_light = true;
53+
light_entity.name = editor.scene.make_default_name_for(light_entity);
54+
light_entity.position = { 2.0f, 3.0f, 2.0f };
55+
light_entity.scale = { 0.2f, 0.2f, 0.2f };
56+
light_entity.color = WHITE;
57+
light_entity.light = create_lighting(light_entity.position, WHITE);
58+
light_entity.light.range = 12.0f;
59+
light_entity.light.intensity = 1.5f;
60+
light_entity.light.id = allocate_light_id();
61+
light_entity.light.light = CreateLight(LIGHT_POINT, light_entity.position, Vector3Zero(), light_entity.light.color, shader);
62+
light_entity.light_created = true;
63+
64+
editor.scene.entities.push_back(light_entity);
65+
break;
66+
}
67+
2968
while (!WindowShouldClose()) {
3069
SetWindowTitle(TextFormat("Quark Engine / FPS: %d", GetFPS()));
3170

src/scene.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ Entity* Scene::get_selected() {
66
return &entities[selected];
77
}
88

9+
std::string Scene::make_unique_name(const std::string& base_name) const {
10+
auto is_name_taken = [this](const std::string& candidate) {
11+
for (const auto& entity : entities) {
12+
if (entity.name == candidate) return true;
13+
}
14+
return false;
15+
};
16+
17+
if (!is_name_taken(base_name)) return base_name;
18+
19+
for (int suffix = 1;; ++suffix) {
20+
const std::string candidate = base_name + " (" + std::to_string(suffix) + ")";
21+
if (!is_name_taken(candidate)) return candidate;
22+
}
23+
}
24+
25+
std::string Scene::make_default_name_for(const Entity& entity) const {
26+
const std::string base_name = entity.has_light ? "Light" : object_type_name(entity.type);
27+
return make_unique_name(base_name);
28+
}
29+
930
void Scene::release_resources() {
1031
std::unordered_set<void*> released_meshes;
1132

0 commit comments

Comments
 (0)