Skip to content

Commit c9a66f6

Browse files
committed
Basic code for world inspector.
1 parent 30d67be commit c9a66f6

16 files changed

Lines changed: 621 additions & 23 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function(tiny_engine_configure_target TARGET_NAME)
7070
# Add includes (relative to the target's directory).
7171
target_include_directories(${TARGET_NAME} SYSTEM PUBLIC "../../ext")
7272
target_include_directories(${TARGET_NAME} PUBLIC include)
73+
target_include_directories(${TARGET_NAME} PRIVATE src)
7374

7475
# Enable ASan.
7576
if(NOT IS_RELEASE_BUILD AND NOT MSVC AND NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT ENGINE_DISABLE_ASAN_IN_DEBUG)

src/editor/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ set(PROJECT_SOURCES
88
src/main.c
99
src/editor.c
1010
src/editor_camera.c
11-
include/editor.h
12-
include/editor_camera.h
11+
src/editor.h
12+
src/editor_camera.h
13+
src/ui/editor_ui.c
14+
src/ui/editor_ui.h
15+
src/ui/theme.h
16+
src/ui/world_inspector.c
17+
src/ui/world_inspector.h
1318
)
1419

1520
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})

src/editor/src/editor.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdio.h>
44
#include <editor_camera.h>
55
#include <game/model.h>
6+
#include <game/camera.h>
67
#include <game_manager.h>
78
#include <io/log.h>
89
#include <misc/memory_usage.h>
@@ -13,6 +14,7 @@
1314
#include <widget/widget.h>
1415
#include <window.h>
1516
#include <world.h>
17+
#include <ui/editor_ui.h>
1618

1719
struct te_editor {
1820
// Always valid pointer. Must be destroyed during the editor's destruction.
@@ -24,6 +26,12 @@ struct te_editor {
2426
// Not NULL if game world exists.
2527
te_world* game_world;
2628

29+
// Not NULL if exists.
30+
te_world* editor_world;
31+
32+
// Always valid.
33+
te_editor_ui* ui;
34+
2735
// Time (in seconds) since @ref game_world_stats_widget was updated.
2836
float time_since_stats_update_sec;
2937
};
@@ -32,8 +40,10 @@ te_editor*
3240
editor_create() {
3341
te_editor* editor = malloc(sizeof(te_editor));
3442
editor->editor_camera = editor_camera_create();
43+
editor->ui = editor_ui_create();
3544
editor->game_world_stats_widget = NULL;
3645
editor->game_world = NULL;
46+
editor->editor_world = NULL;
3747
editor->time_since_stats_update_sec = 10.0f;
3848

3949
return editor;
@@ -42,19 +52,33 @@ editor_create() {
4252
void
4353
editor_destroy(te_editor* editor) {
4454
editor_camera_destroy(editor->editor_camera);
55+
editor_ui_destroy(editor->ui);
4556

4657
free(editor);
4758
}
4859

60+
static void
61+
editor_create_editor_world(te_editor* editor, struct te_game_manager* game_manager) {
62+
editor->editor_world = game_manager_create_world(game_manager, "editor world");
63+
64+
// Create a dummy camera to display editor's UI.
65+
te_camera* camera = camera_create();
66+
world_spawn_camera(editor->editor_world, camera);
67+
world_set_active_camera(editor->editor_world, camera);
68+
69+
ui_spawn(editor->ui, editor->editor_world);
70+
}
71+
4972
void
5073
editor_on_game_started(void* game_instance, te_game_manager* game_manager) {
5174
// Load font.
5275
te_renderer* renderer = game_manager_get_renderer(game_manager);
5376
te_font_manager* font_manager = renderer_get_font_manager(renderer);
5477
font_manager_load_font(font_manager, "engine/font/font.ttf");
5578

56-
// Create default game world.
79+
// Create worlds.
5780
te_editor* editor = game_instance;
81+
editor_create_editor_world(editor, game_manager);
5882
editor_create_game_world(editor, game_manager);
5983
}
6084

src/editor/src/editor_camera.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <game/camera.h>
66
#include <math_funcs.h>
77
#include <misc/globals.h>
8+
#include <ui/theme.h>
89
#include <world.h>
910

1011
#define DEFAULT_CAMERA_SPEED 4.0f
@@ -56,8 +57,17 @@ editor_camera_spawn(te_editor_camera* editor_camera, struct te_world* world) {
5657
camera_set_position(editor_camera->camera, (vec3){0.0f, 2.0f, 4.0f});
5758
camera_set_rotation(editor_camera->camera, (vec3){0.0f, 0.0f, 0.0f});
5859

60+
// Spawn.
5961
world_spawn_camera(world, editor_camera->camera);
6062
world_set_active_camera(world, editor_camera->camera);
63+
64+
// Set viewport.
65+
vec4 viewport;
66+
glm_vec4_copy(
67+
(vec4){theme_get_left_panel_width(), 0.0f,
68+
1.0f - (theme_get_left_panel_width() + theme_get_right_panel_width()), 1.0f},
69+
viewport);
70+
camera_set_viewport(editor_camera->camera, viewport);
6171
}
6272

6373
void

src/editor/src/ui/editor_ui.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <ui/editor_ui.h>
2+
3+
#include <world.h>
4+
#include <ui/theme.h>
5+
#include <ui/world_inspector.h>
6+
#include <widget/rect_widget.h>
7+
#include <widget/widget.h>
8+
#include <world.h>
9+
10+
struct te_editor_ui {
11+
te_world_inspector* world_inspector;
12+
};
13+
14+
te_editor_ui*
15+
editor_ui_create(void) {
16+
te_editor_ui* ui = malloc(sizeof(te_editor_ui));
17+
18+
ui->world_inspector = world_inspector_create();
19+
20+
return ui;
21+
}
22+
23+
void
24+
editor_ui_destroy(te_editor_ui* ui) {
25+
world_inspector_destroy(ui->world_inspector);
26+
free(ui);
27+
}
28+
29+
void
30+
ui_spawn(te_editor_ui* ui, te_world* editor_world) {
31+
vec4 background_color;
32+
theme_get_background_panel_color(background_color);
33+
34+
// Left panel.
35+
te_rect_widget* left_rect = rect_widget_create();
36+
{
37+
te_widget* widget = rect_widget_get_widget(left_rect);
38+
widget_set_relative_position(widget, (vec2){0.0f, 0.0f});
39+
widget_set_relative_size(widget, (vec2){theme_get_left_panel_width(), 1.0f});
40+
}
41+
rect_widget_set_color(left_rect, background_color);
42+
43+
// Right panel.
44+
te_rect_widget* right_rect = rect_widget_create();
45+
{
46+
te_widget* widget = rect_widget_get_widget(right_rect);
47+
widget_set_relative_position(
48+
widget, (vec2){1.0f - theme_get_right_panel_width(), 0.0f});
49+
widget_set_relative_size(widget, (vec2){theme_get_left_panel_width(), 1.0f});
50+
}
51+
rect_widget_set_color(right_rect, background_color);
52+
53+
// Add editor widgets.
54+
world_inspector_add(ui->world_inspector, rect_widget_get_widget(left_rect));
55+
56+
// Spawn.
57+
world_spawn_widget(editor_world, rect_widget_get_widget(left_rect));
58+
world_spawn_widget(editor_world, rect_widget_get_widget(right_rect));
59+
}

src/editor/src/ui/editor_ui.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
typedef struct te_editor_ui te_editor_ui;
4+
struct te_world;
5+
6+
te_editor_ui* editor_ui_create(void);
7+
void editor_ui_destroy(te_editor_ui* ui);
8+
9+
// Creates and spawns editor's UI widgets.
10+
void ui_spawn(te_editor_ui* ui, struct te_world* editor_world);

src/editor/src/ui/theme.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#pragma once
2+
3+
#include <cglm/vec4.h>
4+
5+
static inline float
6+
prv_theme_vertical_to_horizontal_ratio(void) {
7+
return 0.6f;
8+
}
9+
10+
// Returns width in range [0.0; 1.0] of the left panel (displays world inspector and filesystem panel).
11+
static inline float
12+
theme_get_left_panel_width(void) {
13+
return 0.125f;
14+
}
15+
16+
// Returns width in range [0.0; 1.0] of the right panel (displays object inspector).
17+
static inline float
18+
theme_get_right_panel_width(void) {
19+
return 0.125f;
20+
}
21+
22+
static inline void
23+
theme_get_background_panel_color(vec4 rgba) {
24+
glm_vec4_copy((vec4){0.15f, 0.15f, 0.15f, 1.0f}, rgba);
25+
}
26+
27+
// Returns height in range [0.0; 1.0] (relative to the left panel height) of the world inspector.
28+
static inline float
29+
theme_get_world_inspector_height(void) {
30+
return 0.7f;
31+
}
32+
33+
static inline float
34+
theme_get_text_height(void) {
35+
return 0.019f;
36+
}
37+
38+
static inline float
39+
theme_get_horizontal_padding(void) {
40+
return 0.003f;
41+
}
42+
43+
static inline float
44+
theme_get_vertical_padding(void) {
45+
return 0.001f;
46+
}
47+
48+
static inline float
49+
theme_get_horizontal_padding_in_button(void) {
50+
return 0.05f * prv_theme_vertical_to_horizontal_ratio();
51+
}
52+
53+
static inline float
54+
theme_get_vertical_padding_in_button(void) {
55+
return theme_get_horizontal_padding_in_button() * prv_theme_vertical_to_horizontal_ratio();
56+
}
57+
58+
static inline float
59+
theme_get_horizontal_spacing(void) {
60+
return 0.002f;
61+
}
62+
63+
static inline float
64+
theme_get_vertical_spacing(void) {
65+
return theme_get_horizontal_spacing() * prv_theme_vertical_to_horizontal_ratio();
66+
}
67+
68+
static inline float
69+
theme_get_button_height(void) {
70+
return 0.027f;
71+
}
72+
73+
static inline void
74+
theme_get_accent_color(vec4 rgba) {
75+
glm_vec4_copy((vec4){0.85f, 0.35f, 0.2f, 1.0f}, rgba);
76+
}
77+
78+
static inline void
79+
theme_get_button_color(vec4 rgba) {
80+
glm_vec4_copy((vec4){0.225f, 0.225f, 0.225f, 1.0f}, rgba);
81+
}
82+
83+
static inline void
84+
theme_get_button_color_hovered(vec4 rgba) {
85+
theme_get_button_color(rgba);
86+
glm_vec4_adds(rgba, 0.2f, rgba);
87+
}
88+
89+
static inline void
90+
theme_get_button_color_pressed(vec4 rgba) {
91+
theme_get_button_color(rgba);
92+
glm_vec4_adds(rgba, 0.1f, rgba);
93+
}

0 commit comments

Comments
 (0)