From cc28057851e68ffe5ef8afb11cea7f75a1aa5ee1 Mon Sep 17 00:00:00 2001 From: dandovlatov Date: Wed, 25 Nov 2026 02:50:31 +0400 Subject: [PATCH] Kept the level editor local-only - Ignored/untracked src/editor and docs/LEVEL_EDITOR.md (files stay on disk) - Guarded the editor CMake target so fresh clones configure without it - Trimmed README to level-data/bake docs and dropped stale editor doc links --- .gitignore | 7 + CMakeLists.txt | 16 +- README.md | 41 +- docs/LEVEL_DESIGN_REVIEW.md | 2 +- docs/LEVEL_EDITOR.md | 95 --- src/editor/editor_app.cpp | 1177 ----------------------------------- src/editor/editor_app.hpp | 132 ---- src/editor/main.cpp | 64 -- 8 files changed, 26 insertions(+), 1508 deletions(-) delete mode 100644 docs/LEVEL_EDITOR.md delete mode 100644 src/editor/editor_app.cpp delete mode 100644 src/editor/editor_app.hpp delete mode 100644 src/editor/main.cpp diff --git a/.gitignore b/.gitignore index c5e7705..9dd6db3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,10 @@ build/ compile_commands.json __pycache__/ *.blend1 + +# The standalone level editor is local-only, never pushed. +src/editor/ +docs/LEVEL_EDITOR.md + +# Local Pi Forgejo credentials; never commit. +pi-forgejo-credentials.md diff --git a/CMakeLists.txt b/CMakeLists.txt index f5a2bf8..8493095 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,12 +111,16 @@ add_test(NAME level_fixture_bad_gap COMMAND level_tool --check ${CMAKE_SOURCE_DIR}/tests/data/bad_gap.json) set_tests_properties(level_fixture_bad_gap PROPERTIES WILL_FAIL TRUE) -add_executable(editor - src/editor/main.cpp - src/editor/editor_app.cpp - src/render/raygui_impl.cpp) -target_include_directories(editor PRIVATE src third_party/raygui third_party/clay) -target_link_libraries(editor PRIVATE raylib level_core) +# The standalone level editor is local-only and not shipped with the game. +# Build it only when its sources are present in the working tree. +if(EXISTS ${CMAKE_SOURCE_DIR}/src/editor/main.cpp) + add_executable(editor + src/editor/main.cpp + src/editor/editor_app.cpp + src/render/raygui_impl.cpp) + target_include_directories(editor PRIVATE src third_party/raygui third_party/clay) + target_link_libraries(editor PRIVATE raylib level_core) +endif() add_executable(cntrl src/main.cpp diff --git a/README.md b/README.md index 0f2c718..905a892 100644 --- a/README.md +++ b/README.md @@ -103,41 +103,16 @@ F9 hides it. raylib 6.2 is fetched automatically by CMake (FetchContent). Needs a C++23 compiler (GCC 13+) and OpenGL/X11 dev headers (already required by GLFW). -## Level editor +## Level data -The editor is a **separate binary** (`editor`) and shares only the raylib-free -`level_core` library with the game: +The standalone level editor is a local-only tool; its sources and docs are not +part of this repository. `cntrl --level ` can still playtest any level +JSON directly. -```sh -cmake --build build -j6 -./build/editor # loads assets/levels/floor01.json -./build/editor --level my_level.json -./build/editor --level my_level.json --game ./build/cntrl -``` - -LMB selects/drags props, RMB orbits, MMB pans, wheel zooms, `F` frames the -level. Keys `1`–`4` switch between Select, Wall, Door and Decor tools: walls -and doors are drawn by clicking two ground points, decor is dragged from the -asset browser (all GLBs under `assets/models/`) into the 3D view. `Del` deletes -the selection, `Ctrl+Z`/`Ctrl+Y` (`Ctrl+Shift+Z`) undo/redo, `Ctrl+S` saves, -`Tab` toggles the inspector, `Esc` quits. Deleting a wall segment opens a gap -which can be restored from the inspector. See `docs/LEVEL_EDITOR.md` for the -full key map and field reference. - -The **inspector** (right dock) edits every field of the current selection: -prop kind, position, size, yaw, scale, model, `solid`/`destructible`; wall -lines and gaps; doors; rooms; rituals; thermo doors; and the spawn/key/exit -anchors. With nothing selected it edits the level name and map dimensions. -The validation panel runs a walkable-grid flood fill from the spawn (with -gated doors both open and closed) and reports unreachable rooms, the key or -the exit, overlapping rooms, duplicate ids and missing models. - -`Ctrl+P` saves if needed and launches the game on the current level -(`cntrl --level `), so drafts can be playtested without baking. The -level file is the authoring source of truth; `Floor 01` is baked into the -client at build time (`src/game/level_baked.hpp`, regenerated by -`level_tool --bake` when the JSON changes), so `cntrl` ships without a level -file dependency. Baking refuses invalid levels: +`assets/levels/floor01.json` is the authoring source of truth. It is baked into +the client at build time (`src/game/level_baked.hpp`, regenerated by +`level_tool --bake` when the JSON changes), so `cntrl` ships without a runtime +level-file dependency. Baking refuses invalid levels: ```sh ./build/level_tool --dump-floor01 assets/levels/floor01.json diff --git a/docs/LEVEL_DESIGN_REVIEW.md b/docs/LEVEL_DESIGN_REVIEW.md index f66f9a5..e8bf809 100644 --- a/docs/LEVEL_DESIGN_REVIEW.md +++ b/docs/LEVEL_DESIGN_REVIEW.md @@ -39,7 +39,7 @@ The findings above are the audit baseline. Fixes have since landed in four phase Verification performed: `cmake --build build -j6` clean; standalone C++ harnesses for the ritual state machine and clearance/side-room flow pass; `--floor-overview` screenshots confirm the new corridor, projector marker, locked doors and HUD stamp. -**Level-editor iteration (same date, follow-up work):** the standalone editor gained a raygui inspector covering every `LevelData` field, stable wall/segment IDs (deleting a segment opens a reversible gap), two-state connectivity validation (doors open *and* closed), a bake gate that refuses invalid levels, `cntrl --level ` for playtesting drafts, and fixture-backed CTest coverage. This is tooling only; the open gameplay findings below are unchanged. See `docs/LEVEL_EDITOR.md`. +**Level-editor iteration (same date, follow-up work):** the standalone editor gained a raygui inspector covering every `LevelData` field, stable wall/segment IDs (deleting a segment opens a reversible gap), two-state connectivity validation (doors open *and* closed), a bake gate that refuses invalid levels, `cntrl --level ` for playtesting drafts, and fixture-backed CTest coverage. This is tooling only; the open gameplay findings below are unchanged. The editor is local-only and not shipped with the game. --- diff --git a/docs/LEVEL_EDITOR.md b/docs/LEVEL_EDITOR.md deleted file mode 100644 index 070c38b..0000000 --- a/docs/LEVEL_EDITOR.md +++ /dev/null @@ -1,95 +0,0 @@ -# FBC: NIGHT SHIFT — Level Editor - -The editor authors `assets/levels/*.json`, the single source of truth for the -playable floor. The game reads a JSON at runtime (`cntrl --level `) and -falls back to the build-time baked copy (`src/game/level_baked.hpp`), which is -regenerated by `level_tool --bake` whenever the JSON changes. - -## Build and launch - -```sh -cmake --build build -j6 -./build/editor # assets/levels/floor01.json -./build/editor --level my_level.json --game ./build/cntrl -./build/editor --screenshot /tmp/editor.png # headless smoke capture -./build/editor --place plant 12.5 44 # debug: place an asset by name -``` - -`--game` sets the binary launched by `Ctrl+P`; without it the editor detects -`./cntrl`, `./build/cntrl` or `../cntrl` in that order. `--place` -selects the placed prop so its inspector fields can be inspected in a capture. - -## Controls - -| Input | Action | -|---|---| -| LMB (Select) | Select nearest prop/wall/door/room/ritual/thermo/anchor; drag props | -| LMB (Wall / Door) | First click starts the span, second click finishes it | -| LMB (Decor) | Place the selected asset from the browser | -| LMB drag from browser | Place the selected asset at the drop point | -| RMB or Alt+LMB | Orbit | -| MMB | Pan | -| Wheel | Zoom (scrolls the asset browser over its panel) | -| `1`–`4` | Select / Wall / Door / Decor tools | -| `Del` / `Backspace` | Delete the selection (anchors can only be moved) | -| `Ctrl+Z` / `Ctrl+Y` / `Ctrl+Shift+Z` | Undo / redo | -| `Ctrl+S` | Save to the current level path | -| `Ctrl+P` | Save if dirty, then launch `cntrl --level ` | -| `F` | Frame the whole level | -| `Tab` | Toggle the inspector | -| `Esc` | Quit | - -## Inspector field reference - -The right-hand dock edits the current selection. Numeric fields are sliders, -enums are combo boxes, booleans are check boxes and names/paths are text boxes. -Edits share one undo step per interaction (one per drag or text-box focus). - -| Selection | Fields | -|---|---| -| none (level) | `name`, `map_width`, `map_height`, `scale` | -| Prop | `kind`, `pos`, `w`, `d`, `h`, `y offset`, `yaw deg`, `scale`, `model`, `room_kind`, `solid`, `destructible` | -| Wall line | `axis`, `coord`, `from`, `to`, `height`, each gap's start/end, **DEL GAP**, **OPEN SEGMENT GAP** | -| Door | `id`, `rect` (`x0/y0/x1/y1`), `height` | -| Room | `name`, `rect`, `kind`, `flow`, `side idx` | -| Ritual | `type`, `pos`, `radius`, `duration`, `ring radius`, `ring count`, `light radius`, `light mult`, `light dur` | -| Thermo door | `pos`, `span`, `yaw deg` | -| Spawn / Key / Exit | `pos`; Key/Exit also their pickup/trigger radius | - -**Scale convention:** `scale` is the multiplier passed straight to raylib's -`DrawModelEx`, identical to the game's presenter. `w/d/h` are metadata used for -selection boxes and (for `solid` props) collision. Deleting a wall segment -adds a gap to the wall line; gaps are normalized (clamped, sorted, merged) and -can be removed from the inspector to close a passage again. - -## Validation and baking - -The validation readout at the top of the inspector is computed by -`level::validate()` (the same code `level_tool --validate` uses): - -- gated doors are tested both open and closed; a main room reachable only - through a gated door is an error, a gated side room is expected; -- solid props and walls both block the walkable-grid flood fill from spawn; -- spawn/key/exit must lie inside the map and outside walls; -- rooms must be reachable from spawn, key and exit too; -- duplicate door ids, duplicate ritual types, overlapping rooms, malformed - gaps, missing/out-of-range numeric fields and missing model files are - reported. - -```sh -./build/level_tool --check assets/levels/floor01.json # round-trip + validation -./build/level_tool --validate assets/levels/floor01.json # validation only -./build/level_tool --bake # refuses invalid levels -cmake --build build --target check_level -ctest --test-dir build --output-on-failure -``` - -Schema is `version: 1`; `from_json` accepts unknown optional fields but -rejects out-of-range values. New fields should default safely when absent. - -## Other tools - -`tools/blender/import_level.py` reads the same JSON into a reviewable Blender -blockout (set `CNTRL_LEVEL` for a different file, `CNTRL_OUT` for a different -`.blend` output). `tests/data/` holds a valid fixture and an intentionally -invalid one wired into CTest. diff --git a/src/editor/editor_app.cpp b/src/editor/editor_app.cpp deleted file mode 100644 index 8a03662..0000000 --- a/src/editor/editor_app.cpp +++ /dev/null @@ -1,1177 +0,0 @@ -#include "editor/editor_app.hpp" - -#include "raygui.h" -#include "raymath.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace editor { -namespace { - -constexpr int kPanelWidth = 260; -constexpr int kInspectorWidth = 320; -constexpr int kTopBar = 42; -constexpr int kToolbar = 36; -constexpr int kBottomBar = 30; -constexpr int kItemHeight = 26; - -constexpr const char* kRoomKinds = "Gate;Plaza;Projector;OfficeSuite;Silos;Stairwell;Side"; -constexpr const char* kRoomKindChoices = - "(none);Gate;Plaza;Projector;OfficeSuite;Silos;Stairwell;Side"; -constexpr const char* kPropKinds = - "Turnstile;Desk;MeetingTable;Screen;Seat;Silo;Shelf;ArchiveShelf;" - "Beacon;Pillar;Decor"; -constexpr const char* kDoorKinds = "Janitor;Booth;Locker"; - -Color tint(RoomKind kind, unsigned char alpha) { - switch (kind) { - case RoomKind::Gate: return {94, 143, 190, alpha}; - case RoomKind::Plaza: return {110, 58, 62, alpha}; - case RoomKind::Projector: return {198, 157, 36, alpha}; - case RoomKind::OfficeSuite: return {70, 110, 92, alpha}; - case RoomKind::Silos: return {80, 120, 160, alpha}; - case RoomKind::Stairwell: return {160, 90, 140, alpha}; - default: return {100, 100, 108, alpha}; - } -} - -Color prop_color(PropKind kind) { - switch (kind) { - case PropKind::Pillar: return {130, 132, 138, 255}; - case PropKind::Screen: return {40, 46, 54, 255}; - case PropKind::Seat: return {92, 78, 66, 255}; - case PropKind::Silo: return {88, 106, 120, 255}; - case PropKind::Shelf: return {118, 96, 72, 255}; - case PropKind::ArchiveShelf: return {122, 112, 92, 255}; - case PropKind::Beacon: return {150, 160, 168, 255}; - case PropKind::Decor: return {128, 126, 122, 255}; - default: return {120, 120, 128, 255}; - } -} - -float screen_distance(Vector2 a, Vector2 b) { - const float dx = a.x - b.x; - const float dy = a.y - b.y; - return std::sqrt(dx * dx + dy * dy); -} - -Vec2 snap(Vec2 p) { - return {std::round(p.x * 2.0f) * 0.5f, std::round(p.y * 2.0f) * 0.5f}; -} - -bool resolve_asset_path(const std::string& path, std::string& out) { - if (std::filesystem::exists(path)) { - out = path; - return true; - } - const std::string fallback = "../" + path; - if (std::filesystem::exists(fallback)) { - out = fallback; - return true; - } - return false; -} - -} // namespace - -App::App(std::string path) : path_(std::move(path)) { - std::string error; - if (!level::load(path_, level_, error)) { - std::fprintf(stderr, "editor: %s; using built-in Floor 01\n", error.c_str()); - level_ = level::floor01_default(); - } - assets_ = level::scan_assets(); - if (!assets_.empty()) selected_asset_ = 0; -} - -Vector3 App::world(Vec2 p, float y) const { - const Vec2 w = level::to_world(level_, p); - return {w.x, y, w.y}; -} - -Vector3 App::world_direct(Vec2 p, float y) const { - return {p.x, y, p.y}; -} - -Vec2 App::prop_world_pos(const level::Prop& p) const { - if (p.room_kind.empty()) return level::to_world(level_, p.pos); - for (const level::Room& r : level_.rooms) { - if (std::string(level::room_kind_name(r.kind)) != p.room_kind) continue; - const level::Rect w = level::to_world_rect(level_, r.rect); - return {(w.x0 + w.x1) * 0.5f + p.pos.x, - (w.y0 + w.y1) * 0.5f + p.pos.y}; - } - return level::to_world(level_, p.pos); -} - -Vector3 App::camera_offset() const { - const float cp = std::cos(pitch_); - return {cp * std::sin(yaw_) * distance_, - std::sin(pitch_) * distance_, - cp * std::cos(yaw_) * distance_}; -} - -void App::init() { - frame_level(); - camera_.up = {0.0f, 1.0f, 0.0f}; - camera_.fovy = 55.0f; - last_mouse_ = GetMousePosition(); - update_camera(); - revalidate(); -} - -void App::frame_level() { - target_ = {0.0f, 0.0f, 0.0f}; - distance_ = std::max(level_.map_width, level_.map_height) * level_.scale * 1.4f; - yaw_ = 0.0f; - pitch_ = 0.85f; -} - -void App::update_camera() { - const Vector2 mouse = GetMousePosition(); - const Vector2 delta = {mouse.x - last_mouse_.x, mouse.y - last_mouse_.y}; - last_mouse_ = mouse; - - if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT) - || (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && IsKeyDown(KEY_LEFT_ALT))) { - yaw_ += delta.x * 0.005f; - pitch_ = std::clamp(pitch_ + delta.y * 0.004f, 0.08f, 1.52f); - } - if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) { - const Vector3 forward = Vector3Normalize( - Vector3Subtract(target_, camera_.position)); - const Vector3 right = Vector3Normalize( - Vector3CrossProduct(forward, camera_.up)); - const Vector3 up = Vector3CrossProduct(right, forward); - const float k = distance_ * 0.0016f; - target_ = Vector3Add(target_, - Vector3Add(Vector3Scale(right, -delta.x * k), - Vector3Scale(up, delta.y * k))); - } - const float wheel = GetMouseWheelMove(); - if (wheel != 0.0f) { - if (inside_panel(mouse)) { - const int max_scroll = std::max(0, static_cast(assets_.size()) - 12); - asset_scroll_ = std::clamp(asset_scroll_ - static_cast(wheel), - 0, max_scroll); - } else if (!inside_inspector(mouse)) { - distance_ = std::clamp(distance_ * std::exp(-wheel * 0.12f), - 6.0f, 900.0f); - } - } - camera_.position = Vector3Add(target_, camera_offset()); - camera_.target = target_; -} - -bool App::inside_panel(Vector2 mouse) const { - return mouse.x >= 0.0f && mouse.x <= kPanelWidth - && mouse.y >= kTopBar + kToolbar - && mouse.y <= GetScreenHeight() - kBottomBar; -} - -bool App::inside_inspector(Vector2 mouse) const { - return inspector_ && mouse.x >= GetScreenWidth() - kInspectorWidth - && mouse.y >= kTopBar && mouse.y <= GetScreenHeight() - kBottomBar; -} - -int App::asset_at(Vector2 mouse) const { - if (!inside_panel(mouse)) return -1; - const float top = static_cast(kTopBar + kToolbar + 24); - if (mouse.y < top) return -1; - const int index = asset_scroll_ + static_cast((mouse.y - top) / kItemHeight); - if (index < 0 || index >= static_cast(assets_.size())) return -1; - return index; -} - -bool App::ground_point(Vec2& map_pos) const { - const Ray ray = GetScreenToWorldRay(GetMousePosition(), camera_); - if (std::fabs(ray.direction.y) < 1e-6f) return false; - const float t = -ray.position.y / ray.direction.y; - if (t <= 0.0f) return false; - const Vector3 point = Vector3Add(ray.position, Vector3Scale(ray.direction, t)); - map_pos = snap(level::to_map(level_, {point.x, point.z})); - return true; -} - -void App::push_snapshot(const std::string& snapshot) { - undo_.push_back(snapshot); - if (undo_.size() > 64) undo_.erase(undo_.begin()); - redo_.clear(); -} - -void App::push_undo() { - push_snapshot(level::serialize(level_)); -} - -void App::begin_edit() { - if (interacting_) return; - push_undo(); - interacting_ = true; -} - -void App::end_edit() { interacting_ = false; } - -void App::commit() { - dirty_ = true; - revalidate(); -} - -void App::restore(const std::string& snapshot) { - json::Value root; - std::string error; - if (!json::parse(snapshot, root, error)) return; - level::LevelData next; - if (!level::from_json(root, next, error)) return; - level_ = std::move(next); - selection_ = {}; - commit_text(); - dirty_ = true; - revalidate(); -} - -void App::revalidate() { - validation_ = level::validate(level_); -} - -void App::undo() { - if (undo_.empty()) return; - redo_.push_back(level::serialize(level_)); - restore(undo_.back()); - undo_.pop_back(); -} - -void App::redo() { - if (redo_.empty()) return; - undo_.push_back(level::serialize(level_)); - restore(redo_.back()); - redo_.pop_back(); -} - -void App::debug_place(const std::string& asset_name, Vec2 map_pos) { - for (size_t i = 0; i < assets_.size(); ++i) { - std::string name = assets_[i].name; - std::transform(name.begin(), name.end(), name.begin(), - [](unsigned char c) { return static_cast(std::tolower(c)); }); - if (name == asset_name) { - selected_asset_ = static_cast(i); - place_decor(map_pos); - target_ = world(map_pos, 0.0f); - distance_ = 40.0f; - pitch_ = 0.55f; - return; - } - } - std::fprintf(stderr, "editor: unknown asset '%s'\n", asset_name.c_str()); -} - -void App::place_decor(Vec2 map_pos) { - if (selected_asset_ < 0 || selected_asset_ >= static_cast(assets_.size())) return; - const std::string& path = assets_[static_cast(selected_asset_)].path; - const Vector3 size = model_size(path); - push_undo(); - level::Prop p; - p.kind = PropKind::Decor; - p.pos = map_pos; - p.w = size.x > 0.01f ? size.x : 1.0f; - p.d = size.z > 0.01f ? size.z : 1.0f; - p.h = size.y > 0.01f ? size.y : 1.0f; - p.model = path; - p.scale = 1.0f; - level_.props.push_back(p); - selection_ = {Entity::Prop, static_cast(level_.props.size()) - 1, -1}; - status_message_ = "placed decor"; - status_timer_ = 2.0f; - commit(); -} - -void App::finish_wall(Vec2 map_pos) { - if (!pending_set_) { - pending_ = map_pos; - pending_set_ = true; - return; - } - const Vec2 a = pending_; - const Vec2 b = map_pos; - pending_set_ = false; - if (std::fabs(b.x - a.x) < 0.01f && std::fabs(b.y - a.y) < 0.01f) return; - level::WallLine w; - if (std::fabs(b.x - a.x) >= std::fabs(b.y - a.y)) { - w.axis = 0; - w.coord = (a.y + b.y) * 0.5f; - w.from = std::min(a.x, b.x); - w.to = std::max(a.x, b.x); - } else { - w.axis = 1; - w.coord = (a.x + b.x) * 0.5f; - w.from = std::min(a.y, b.y); - w.to = std::max(a.y, b.y); - } - push_undo(); - level_.walls.push_back(w); - selection_ = {Entity::Wall, static_cast(level_.walls.size()) - 1, 0}; - status_message_ = "added wall line"; - status_timer_ = 2.0f; - commit(); -} - -void App::finish_door(Vec2 map_pos) { - if (!pending_set_) { - pending_ = map_pos; - pending_set_ = true; - return; - } - const Vec2 a = pending_; - const Vec2 b = map_pos; - pending_set_ = false; - if (std::fabs(b.x - a.x) < 0.01f || std::fabs(b.y - a.y) < 0.01f) return; - const int id = level::allocate_door_id(level_); - if (id < 0) { - status_message_ = "all door ids are taken"; - status_timer_ = 3.0f; - return; - } - level::Door d; - d.id = static_cast(id); - d.rect = {std::min(a.x, b.x), std::min(a.y, b.y), - std::max(a.x, b.x), std::max(a.y, b.y)}; - push_undo(); - level_.doors.push_back(d); - selection_ = {Entity::Door, static_cast(level_.doors.size()) - 1, -1}; - status_message_ = std::string("added door: ") + level::door_name(d.id); - status_timer_ = 2.0f; - commit(); -} - -void App::select_at(Vector2 mouse) { - float best_dist = 22.0f; - Selection best; - - for (size_t i = 0; i < level_.props.size(); ++i) { - const level::Prop& p = level_.props[i]; - const float d = screen_distance(mouse, - GetWorldToScreen(world_direct(prop_world_pos(p), - p.room_kind.empty() ? p.h * 0.5f : p.y), camera_)); - if (d < best_dist) { - best_dist = d; - best = {Entity::Prop, static_cast(i), -1}; - } - } - const auto walls = level::build_indexed_walls(level_); - for (const level::IndexedWall& wall : walls) { - const level::Rect& r = wall.wall.rect; - const Vector3 center{(r.x0 + r.x1) * 0.5f, wall.wall.height * 0.5f, - (r.y0 + r.y1) * 0.5f}; - const float d = screen_distance(mouse, GetWorldToScreen(center, camera_)); - if (d < best_dist) { - best_dist = d; - if (wall.source == level::WallSource::Line) { - best = {Entity::Wall, static_cast(wall.line), - static_cast(wall.segment)}; - } else if (wall.source == level::WallSource::Door) { - best = {Entity::Door, static_cast(wall.line), -1}; - } else { - best = {Entity::Prop, static_cast(wall.line), -1}; - } - } - } - for (size_t i = 0; i < level_.rooms.size(); ++i) { - const level::Rect& r = level_.rooms[i].rect; - const Vector3 center = world({(r.x0 + r.x1) * 0.5f, - (r.y0 + r.y1) * 0.5f}, 0.5f); - const float d = screen_distance(mouse, GetWorldToScreen(center, camera_)); - if (d < best_dist) { - best_dist = d; - best = {Entity::Room, static_cast(i), -1}; - } - } - for (size_t i = 0; i < level_.rituals.size(); ++i) { - const Vector3 center = world(level_.rituals[i].pos, 1.0f); - const float d = screen_distance(mouse, GetWorldToScreen(center, camera_)); - if (d < best_dist) { - best_dist = d; - best = {Entity::Ritual, static_cast(i), -1}; - } - } - for (size_t i = 0; i < level_.thermo.size(); ++i) { - const Vector3 center = world(level_.thermo[i].pos, 2.4f); - const float d = screen_distance(mouse, GetWorldToScreen(center, camera_)); - if (d < best_dist) { - best_dist = d; - best = {Entity::Thermo, static_cast(i), -1}; - } - } - const std::pair anchors[] = { - {Entity::Spawn, level_.anchors.spawn}}; - for (const auto& anchor : anchors) { - const float d = screen_distance(mouse, - GetWorldToScreen(world(anchor.second, 1.2f), camera_)); - if (d < best_dist) { - best_dist = d; - best = {anchor.first, -1, -1}; - } - } - - selection_ = best; - if (selection_.kind == Entity::Prop && selection_.index >= 0) { - dragged_prop_ = selection_.index; - } -} - -void App::delete_selection() { - switch (selection_.kind) { - case Entity::Prop: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.props.size())) { - push_undo(); - level_.props.erase(level_.props.begin() + selection_.index); - status_message_ = "deleted prop"; - commit(); - } - break; - case Entity::Room: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.rooms.size())) { - push_undo(); - level_.rooms.erase(level_.rooms.begin() + selection_.index); - status_message_ = "deleted room"; - commit(); - } - break; - case Entity::Wall: { - const std::string before = level::serialize(level_); - if (remove_wall_segment(level_, static_cast(selection_.index), - static_cast(selection_.sub))) { - push_snapshot(before); - status_message_ = "opened a gap in the wall"; - commit(); - } else { - status_message_ = "could not delete that wall segment"; - status_timer_ = 3.0f; - } - break; - } - case Entity::Door: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.doors.size())) { - push_undo(); - level_.doors.erase(level_.doors.begin() + selection_.index); - status_message_ = "deleted door"; - commit(); - } - break; - case Entity::Ritual: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.rituals.size())) { - push_undo(); - level_.rituals.erase(level_.rituals.begin() + selection_.index); - status_message_ = "deleted ritual"; - commit(); - } - break; - case Entity::Thermo: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.thermo.size())) { - push_undo(); - level_.thermo.erase(level_.thermo.begin() + selection_.index); - status_message_ = "deleted thermo door"; - commit(); - } - break; - default: - status_message_ = "anchors cannot be deleted; move them instead"; - status_timer_ = 3.0f; - break; - } - selection_ = {}; -} - -void App::playtest() { - if (dirty_) { - if (!level::save(path_, level_)) { - status_message_ = "save failed; cannot playtest"; - status_timer_ = 3.0f; - return; - } - dirty_ = false; - } - const std::string command = "\"" + game_path_ + "\" --level \"" + path_ + "\" &"; - std::system(command.c_str()); - status_message_ = "launched playtest: " + game_path_; - status_timer_ = 4.0f; -} - -void App::handle_keys() { - const bool ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL); - const bool shift = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT); - if (IsKeyPressed(KEY_ESCAPE)) should_close_ = true; - if (IsKeyPressed(KEY_F) && text_focus_ < 0) frame_level(); - if (IsKeyPressed(KEY_TAB) && text_focus_ < 0) inspector_ = !inspector_; - if (ctrl && IsKeyPressed(KEY_S)) { - if (level::save(path_, level_)) { - dirty_ = false; - status_message_ = "saved " + path_; - status_timer_ = 2.0f; - } else { - status_message_ = "save failed"; - status_timer_ = 3.0f; - } - } - if (ctrl && IsKeyPressed(KEY_P)) playtest(); - if (ctrl && (IsKeyPressed(KEY_Z) && shift)) redo(); - else if (ctrl && IsKeyPressed(KEY_Z)) undo(); - if (ctrl && IsKeyPressed(KEY_Y)) redo(); - if (text_focus_ < 0 - && (IsKeyPressed(KEY_DELETE) || IsKeyPressed(KEY_BACKSPACE))) { - delete_selection(); - } - if (text_focus_ < 0) { - if (IsKeyPressed(KEY_ONE)) tool_ = Tool::Select; - if (IsKeyPressed(KEY_TWO)) tool_ = Tool::Wall; - if (IsKeyPressed(KEY_THREE)) tool_ = Tool::Door; - if (IsKeyPressed(KEY_FOUR)) tool_ = Tool::Decor; - } -} - -void App::handle_pointer() { - const Vector2 mouse = GetMousePosition(); - - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - if (inside_inspector(mouse) || inside_panel(mouse)) { - const int index = asset_at(mouse); - if (inside_panel(mouse) && index >= 0) { - selected_asset_ = index; - dragging_panel_asset_ = true; - } - return; - } - if (tool_ == Tool::Select) { - select_at(mouse); - } else { - Vec2 map_pos; - if (ground_point(map_pos)) { - if (tool_ == Tool::Wall) finish_wall(map_pos); - else if (tool_ == Tool::Door) finish_door(map_pos); - else if (tool_ == Tool::Decor) place_decor(map_pos); - } - } - } - - if (dragging_panel_asset_ && IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { - if (!inside_panel(mouse) && !inside_inspector(mouse)) { - Vec2 map_pos; - if (ground_point(map_pos)) place_decor(map_pos); - } - dragging_panel_asset_ = false; - } - - if (dragging_prop_ >= 0 && IsMouseButtonDown(MOUSE_BUTTON_LEFT) - && tool_ == Tool::Select && !inside_panel(mouse) && !inside_inspector(mouse)) { - Vec2 map_pos; - if (ground_point(map_pos) && dragging_prop_ < static_cast(level_.props.size())) { - level::Prop& p = level_.props[static_cast(dragging_prop_)]; - Vec2 next_pos = map_pos; - if (!p.room_kind.empty()) { - const Vec2 wpos = level::to_world(level_, map_pos); - for (const level::Room& r : level_.rooms) { - if (std::string(level::room_kind_name(r.kind)) != p.room_kind) continue; - const level::Rect wr = level::to_world_rect(level_, r.rect); - next_pos = {wpos.x - (wr.x0 + wr.x1) * 0.5f, - wpos.y - (wr.y0 + wr.y1) * 0.5f}; - break; - } - } - if (p.pos.x != next_pos.x || p.pos.y != next_pos.y) { - begin_edit(); - p.pos = next_pos; - commit(); - } - } - } - if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) dragging_prop_ = -1; -} - -void App::update() { - update_camera(); - handle_keys(); - handle_pointer(); - if (interacting_ && text_focus_ < 0 && !IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - end_edit(); - } - if (status_timer_ > 0.0f) status_timer_ -= GetFrameTime(); -} - -Model& App::model_for(const std::string& path) { - static Model empty{}; - auto it = models_.find(path); - if (it != models_.end()) return it->second; - std::string resolved; - if (!resolve_asset_path(path, resolved)) { - std::fprintf(stderr, "editor: asset not found: %s\n", path.c_str()); - return empty; - } - Model model = LoadModel(resolved.c_str()); - if (model.meshCount == 0) { - std::fprintf(stderr, "editor: failed to load model: %s\n", resolved.c_str()); - } - auto inserted = models_.emplace(path, model); - return inserted.first->second; -} - -Vector3 App::model_size(const std::string& path) { - auto it = model_sizes_.find(path); - if (it != model_sizes_.end()) return it->second; - Model& model = model_for(path); - Vector3 lo{1e9f, 1e9f, 1e9f}; - Vector3 hi{-1e9f, -1e9f, -1e9f}; - for (int i = 0; i < model.meshCount; ++i) { - const Mesh& mesh = model.meshes[i]; - const float* vertices = static_cast(mesh.vertices); - if (vertices == nullptr) continue; - for (int v = 0; v < mesh.vertexCount; ++v) { - const float x = vertices[v * 3]; - const float y = vertices[v * 3 + 1]; - const float z = vertices[v * 3 + 2]; - lo.x = std::min(lo.x, x); - lo.y = std::min(lo.y, y); - lo.z = std::min(lo.z, z); - hi.x = std::max(hi.x, x); - hi.y = std::max(hi.y, y); - hi.z = std::max(hi.z, z); - } - } - Vector3 size{0.0f, 0.0f, 0.0f}; - if (model.meshCount > 0 && hi.x > lo.x) { - size = {hi.x - lo.x, hi.y - lo.y, hi.z - lo.z}; - } - model_sizes_[path] = size; - return model_sizes_[path]; -} - -void App::draw_model(const std::string& path, Vec2 world_pos, float yaw, - float scale, float y) { - Model& model = model_for(path); - if (model.meshCount == 0) return; - DrawModelEx(model, world_direct(world_pos, y), {0.0f, 1.0f, 0.0f}, - yaw * 180.0f / PI, {scale, scale, scale}, WHITE); -} - -void App::draw_grid() const { - const float hx = level_.map_width * level_.scale * 0.5f; - const float hz = level_.map_height * level_.scale * 0.5f; - const Color minor = {60, 62, 68, 255}; - const Color major = {88, 90, 98, 255}; - for (float x = -hx; x <= hx + 0.01f; x += 10.0f) { - const Color c = std::fabs(std::fmod(x, 50.0f)) < 0.01f ? major : minor; - DrawLine3D({x, 0.04f, -hz}, {x, 0.04f, hz}, c); - } - for (float z = -hz; z <= hz + 0.01f; z += 10.0f) { - const Color c = std::fabs(std::fmod(z, 50.0f)) < 0.01f ? major : minor; - DrawLine3D({-hx, 0.04f, z}, {hx, 0.04f, z}, c); - } -} - -void App::draw_rooms() const { - for (size_t i = 0; i < level_.rooms.size(); ++i) { - const level::Room& r = level_.rooms[i]; - const level::Rect w = level::to_world_rect(level_, r.rect); - const Vector3 center{(w.x0 + w.x1) * 0.5f, 0.03f, (w.y0 + w.y1) * 0.5f}; - const Vector3 size{w.x1 - w.x0, 0.04f, w.y1 - w.y0}; - DrawCube(center, size.x, size.y, size.z, tint(r.kind, 46)); - const bool selected = selection_.kind == Entity::Room - && selection_.index == static_cast(i); - DrawCubeWires(center, size.x, size.y, size.z, - selected ? GOLD : tint(r.kind, 190)); - } -} - -void App::draw_walls() const { - const auto walls = level::build_indexed_walls(level_); - for (const level::IndexedWall& iw : walls) { - const level::WorldWall& w = iw.wall; - const Vector3 center{(w.rect.x0 + w.rect.x1) * 0.5f, w.height * 0.5f, - (w.rect.y0 + w.rect.y1) * 0.5f}; - const Vector3 size{w.rect.x1 - w.rect.x0, w.height, - w.rect.y1 - w.rect.y0}; - Color c = {150, 152, 158, 255}; - if (w.door == static_cast(DoorId::Janitor)) c = {200, 100, 60, 255}; - if (w.door == static_cast(DoorId::Booth)) c = {200, 160, 60, 255}; - if (w.door == static_cast(DoorId::Locker)) c = {160, 90, 200, 255}; - DrawCube(center, size.x, size.y, size.z, c); - const bool selected = - (iw.source == level::WallSource::Line - && selection_.kind == Entity::Wall - && selection_.index == static_cast(iw.line) - && selection_.sub == static_cast(iw.segment)) - || (iw.source == level::WallSource::Door - && selection_.kind == Entity::Door - && selection_.index == static_cast(iw.line)); - if (selected) { - DrawCubeWires(center, size.x + 0.1f, size.y + 0.1f, size.z + 0.1f, GOLD); - } - } - for (const level::ThermoDoor& t : level_.thermo) { - const Vec2 center = level::to_world(level_, t.pos); - const float span = t.span * level_.scale; - const bool across_x = std::fabs(std::sin(t.yaw)) < 0.5f; - DrawCube({center.x, 2.4f, center.y}, - across_x ? span : 0.35f, 0.3f, across_x ? 0.35f : span, - {90, 200, 210, 220}); - } - if (pending_set_) DrawSphere(world(pending_, 1.0f), 1.0f, GOLD); -} - -void App::draw_props() { - for (size_t i = 0; i < level_.props.size(); ++i) { - const level::Prop& p = level_.props[i]; - const Vec2 wp = prop_world_pos(p); - const float base_y = p.room_kind.empty() ? p.h * 0.5f : p.y; - const char* path = p.room_kind.empty() - ? (p.model.empty() ? nullptr : p.model.c_str()) - : level::actor_model_path(p.model); - if (path != nullptr) { - draw_model(path, wp, p.yaw, p.scale, base_y); - } else { - DrawCube(world_direct(wp, base_y), p.w, p.h, p.d, prop_color(p.kind)); - } - if (selection_.kind == Entity::Prop && selection_.index == static_cast(i)) { - DrawCubeWires(world_direct(wp, base_y), p.w + 0.12f, p.h + 0.12f, - p.d + 0.12f, GOLD); - } - } -} - -void App::draw_anchors() const { - const auto marker = [&](Vec2 p, Entity kind, Color c) { - const bool selected = selection_.kind == kind; - DrawCylinder(world(p, 1.2f), 1.0f, 1.0f, 2.4f, 8, - selected ? GOLD : c); - }; - marker(level_.anchors.spawn, Entity::Spawn, {80, 220, 120, 255}); - for (const level::Ritual& r : level_.rituals) { - const Vec2 center = level::to_world(level_, r.pos); - DrawCylinder({center.x, 0.6f, center.y}, r.radius, r.radius, 0.08f, 18, - {210, 170, 60, 90}); - DrawCylinder({center.x, 1.1f, center.y}, 0.6f, 0.6f, 2.2f, 8, GOLD); - } -} - -void App::draw_asset_panel() const { - DrawRectangle(0, kTopBar + kToolbar, kPanelWidth, - GetScreenHeight() - kTopBar - kToolbar - kBottomBar, - {22, 23, 28, 245}); - DrawLine(kPanelWidth, kTopBar + kToolbar, kPanelWidth, - GetScreenHeight() - kBottomBar, {70, 70, 78, 255}); - DrawText("TOOLS", 8, kTopBar + 8, 12, {150, 150, 158, 255}); - const char* tools[] = {"1 SELECT", "2 WALL", "3 DOOR", "4 DECOR"}; - for (int i = 0; i < 4; ++i) { - const int x = 8 + i * 63; - const bool active = tool_ == static_cast(i); - DrawRectangle(x, kTopBar + 20, 60, 18, - active ? Color{190, 150, 50, 255} : Color{48, 49, 56, 255}); - DrawText(tools[i], x + 3, kTopBar + 23, 10, - active ? BLACK : Color{200, 200, 205, 255}); - } - - const int list_top = kTopBar + kToolbar + 24; - char header[64]; - std::snprintf(header, sizeof(header), "ASSETS (%d) - drag into view", - static_cast(assets_.size())); - DrawText(header, 8, kTopBar + kToolbar + 5, 12, {230, 195, 90, 255}); - for (size_t i = 0; i < assets_.size(); ++i) { - const int row = static_cast(i) - asset_scroll_; - if (row < 0 || row >= 12) continue; - const int y = list_top + row * kItemHeight; - if (static_cast(i) == selected_asset_) { - DrawRectangle(2, y, kPanelWidth - 4, kItemHeight, {70, 64, 42, 255}); - } - DrawText(assets_[i].name.c_str(), 12, y + 6, 12, {220, 220, 225, 255}); - DrawText(assets_[i].category.c_str(), kPanelWidth - 64, y + 6, 10, - {130, 130, 138, 255}); - } -} - -bool App::field_header(const char* text, float x, float y, float w) { - DrawRectangle(x, y, w, 20, {44, 46, 54, 255}); - DrawText(text, x + 6, y + 4, 12, {235, 200, 90, 255}); - return false; -} - -bool App::field_bool(float x, float y, float w, const char* label, bool& value) { - DrawText(label, x, y + 3, 11, {200, 200, 205, 255}); - Rectangle r{x + 120, y, w - 128, 18}; - bool v = value; - const int changed = GuiCheckBox(r, nullptr, &v); - if (changed && v != value) { - begin_edit(); - value = v; - commit(); - } - return changed != 0; -} - -bool App::field_float(float x, float y, float w, const char* label, float& value, - float lo, float hi) { - DrawText(label, x, y + 3, 11, {200, 200, 205, 255}); - Rectangle r{x + 120, y, w - 128, 18}; - float v = std::clamp(value, lo, hi); - const int changed = GuiSlider(r, nullptr, TextFormat("%.2f", v), &v, lo, hi); - if (changed && std::fabs(v - value) > 1e-6f) { - begin_edit(); - value = v; - commit(); - } - return changed != 0; -} - -bool App::field_int(float x, float y, float w, const char* label, int& value, - int lo, int hi) { - DrawText(label, x, y + 3, 11, {200, 200, 205, 255}); - Rectangle r{x + 120, y, w - 128, 18}; - float v = static_cast(std::clamp(value, lo, hi)); - const int changed = GuiSlider(r, nullptr, - TextFormat("%d", static_cast(std::lround(v))), - &v, static_cast(lo), static_cast(hi)); - const int next = std::clamp(static_cast(std::lround(v)), lo, hi); - if (changed && next != value) { - begin_edit(); - value = next; - commit(); - } - return changed != 0; -} - -bool App::field_enum(float x, float y, float w, const char* label, int& value, - const char* items, int count) { - DrawText(label, x, y + 3, 11, {200, 200, 205, 255}); - Rectangle r{x + 120, y, w - 128, 18}; - int v = std::clamp(value, 0, count - 1); - const int changed = GuiComboBox(r, items, &v); - if (changed && v != value) { - begin_edit(); - value = v; - commit(); - } - return changed != 0; -} - -void App::commit_text() { - text_focus_ = -1; - end_edit(); -} - -bool App::field_text(float x, float y, float w, int id, const char* label, - std::string& value) { - DrawText(label, x, y + 3, 11, {200, 200, 205, 255}); - Rectangle r{x + 120, y, w - 128, 18}; - const Vector2 mouse = GetMousePosition(); - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointRec(mouse, r) - && text_focus_ != id) { - commit_text(); - text_focus_ = id; - text_rect_ = r; - std::snprintf(text_buf_, sizeof(text_buf_), "%s", value.c_str()); - begin_edit(); - } - if (text_focus_ == id) { - text_rect_ = r; - const int changed = GuiTextBox(r, text_buf_, static_cast(sizeof(text_buf_)), true); - if (changed) { - value = text_buf_; - commit(); - } - return changed != 0; - } - char display[256]; - std::snprintf(display, sizeof(display), "%s", value.c_str()); - GuiTextBox(r, display, static_cast(sizeof(display)), false); - return false; -} - -void App::draw_inspector_fields(float x, float y, float w) { - int row = 0; - const auto next_y = [&]() { return y + static_cast(row++) * 22.0f; }; - const float max_y = static_cast(GetScreenHeight() - kBottomBar - 24); - - switch (selection_.kind) { - case Entity::None: { - field_header("LEVEL", x, next_y(), w); - field_text(x, next_y(), w, 1, "name", level_.name); - field_float(x, next_y(), w, "map width", level_.map_width, 16.0f, 1000.0f); - field_float(x, next_y(), w, "map height", level_.map_height, 16.0f, 1000.0f); - field_float(x, next_y(), w, "scale", level_.scale, 0.1f, 10.0f); - field_header("COUNTS", x, next_y(), w); - char counts[128]; - std::snprintf(counts, sizeof(counts), "rooms %zu walls %zu props %zu", - level_.rooms.size(), level_.walls.size(), level_.props.size()); - DrawText(counts, x + 6, next_y(), 11, {200, 200, 205, 255}); - break; - } - case Entity::Prop: { - if (selection_.index < 0 - || selection_.index >= static_cast(level_.props.size())) break; - level::Prop& p = level_.props[static_cast(selection_.index)]; - field_header("PROP", x, next_y(), w); - int kind = static_cast(p.kind); - if (field_enum(x, next_y(), w, "kind", kind, kPropKinds, 12)) { - p.kind = static_cast(std::clamp(kind, 0, 11)); - } - field_float(x, next_y(), w, "pos x", p.pos.x, 0.0f, level_.map_width); - field_float(x, next_y(), w, "pos y", p.pos.y, 0.0f, level_.map_height); - field_float(x, next_y(), w, "width", p.w, 0.01f, 60.0f); - field_float(x, next_y(), w, "depth", p.d, 0.01f, 60.0f); - field_float(x, next_y(), w, "height", p.h, 0.01f, 60.0f); - field_float(x, next_y(), w, "y offset", p.y, -5.0f, 30.0f); - float yaw_deg = p.yaw * 180.0f / PI; - if (field_float(x, next_y(), w, "yaw deg", yaw_deg, -180.0f, 180.0f)) { - p.yaw = yaw_deg * PI / 180.0f; - } - field_float(x, next_y(), w, "scale", p.scale, 0.01f, 20.0f); - field_text(x, next_y(), w, 2, "model", p.model); - int room_kind = 0; - for (int i = 0; i < 7; ++i) { - if (p.room_kind == level::room_kind_name(static_cast(i))) { - room_kind = i + 1; - break; - } - } - if (field_enum(x, next_y(), w, "room kind", room_kind, - kRoomKindChoices, 8)) { - p.room_kind = room_kind == 0 - ? "" - : level::room_kind_name( - static_cast(room_kind - 1)); - } - field_bool(x, next_y(), w, "solid", p.solid); - field_bool(x, next_y(), w, "destructible", p.destructible); - break; - } - case Entity::Room: { - if (selection_.index < 0 - || selection_.index >= static_cast(level_.rooms.size())) break; - level::Room& r = level_.rooms[static_cast(selection_.index)]; - field_header("ROOM", x, next_y(), w); - field_text(x, next_y(), w, 4, "name", r.name); - field_float(x, next_y(), w, "x0", r.rect.x0, 0.0f, level_.map_width); - field_float(x, next_y(), w, "y0", r.rect.y0, 0.0f, level_.map_height); - field_float(x, next_y(), w, "x1", r.rect.x1, 0.0f, level_.map_width); - field_float(x, next_y(), w, "y1", r.rect.y1, 0.0f, level_.map_height); - int kind = static_cast(r.kind); - if (field_enum(x, next_y(), w, "kind", kind, kRoomKinds, 7)) { - r.kind = static_cast(std::clamp(kind, 0, 6)); - } - field_int(x, next_y(), w, "flow", r.flow, 0, 20); - field_int(x, next_y(), w, "side idx", r.side_index, -1, 7); - break; - } - case Entity::Wall: { - if (selection_.index < 0 - || selection_.index >= static_cast(level_.walls.size())) break; - level::WallLine& line = level_.walls[static_cast(selection_.index)]; - field_header("WALL LINE", x, next_y(), w); - field_int(x, next_y(), w, "axis", line.axis, 0, 1); - field_float(x, next_y(), w, "coord", line.coord, 0.0f, - std::max(level_.map_width, level_.map_height)); - field_float(x, next_y(), w, "from", line.from, 0.0f, - std::max(level_.map_width, level_.map_height)); - field_float(x, next_y(), w, "to", line.to, 0.0f, - std::max(level_.map_width, level_.map_height)); - field_float(x, next_y(), w, "height", line.height, 0.5f, 12.0f); - for (size_t g = 0; g < line.gaps.size() && next_y() < max_y; ++g) { - char label[32]; - std::snprintf(label, sizeof(label), "gap %zu", g); - field_float(x, next_y(), w, label, line.gaps[g].first, line.from, line.to); - std::snprintf(label, sizeof(label), " end %zu", g); - field_float(x, next_y(), w, label, line.gaps[g].second, line.from, line.to); - char button[32]; - std::snprintf(button, sizeof(button), "DEL GAP %zu", g); - if (GuiButton({x + 120, next_y(), w - 128, 18}, button)) { - push_undo(); - restore_wall_gap(level_, static_cast(selection_.index), g); - commit(); - } - } - if (GuiButton({x + 6, next_y(), w - 12, 20}, "OPEN SEGMENT GAP")) { - const std::string before = level::serialize(level_); - if (remove_wall_segment(level_, static_cast(selection_.index), - static_cast(selection_.sub))) { - push_snapshot(before); - commit(); - } - } - break; - } - case Entity::Door: { - if (selection_.index < 0 - || selection_.index >= static_cast(level_.doors.size())) break; - level::Door& d = level_.doors[static_cast(selection_.index)]; - field_header("DOOR", x, next_y(), w); - int id = static_cast(d.id); - if (field_enum(x, next_y(), w, "id", id, kDoorKinds, 3)) { - d.id = static_cast(std::clamp(id, 0, 2)); - } - field_float(x, next_y(), w, "x0", d.rect.x0, 0.0f, level_.map_width); - field_float(x, next_y(), w, "y0", d.rect.y0, 0.0f, level_.map_height); - field_float(x, next_y(), w, "x1", d.rect.x1, 0.0f, level_.map_width); - field_float(x, next_y(), w, "y1", d.rect.y1, 0.0f, level_.map_height); - field_float(x, next_y(), w, "height", d.height, 0.5f, 12.0f); - break; - } - case Entity::Ritual: { - if (selection_.index < 0 - || selection_.index >= static_cast(level_.rituals.size())) break; - level::Ritual& r = level_.rituals[static_cast(selection_.index)]; - field_header("RITUAL", x, next_y(), w); - field_text(x, next_y(), w, 5, "type", r.type); - field_float(x, next_y(), w, "pos x", r.pos.x, 0.0f, level_.map_width); - field_float(x, next_y(), w, "pos y", r.pos.y, 0.0f, level_.map_height); - field_float(x, next_y(), w, "radius", r.radius, 0.1f, 40.0f); - field_float(x, next_y(), w, "duration", r.duration, 1.0f, 900.0f); - field_float(x, next_y(), w, "ring radius", r.ring_radius, 0.0f, 60.0f); - field_int(x, next_y(), w, "ring count", r.ring_count, 0, 32); - field_float(x, next_y(), w, "light radius", r.light_radius, 0.0f, 80.0f); - field_float(x, next_y(), w, "light mult", r.light_damage_mult, 0.01f, 5.0f); - field_float(x, next_y(), w, "light dur", r.light_duration, 0.0f, 300.0f); - break; - } - case Entity::Thermo: { - if (selection_.index < 0 - || selection_.index >= static_cast(level_.thermo.size())) break; - level::ThermoDoor& t = level_.thermo[static_cast(selection_.index)]; - field_header("THERMO DOOR", x, next_y(), w); - field_float(x, next_y(), w, "pos x", t.pos.x, 0.0f, level_.map_width); - field_float(x, next_y(), w, "pos y", t.pos.y, 0.0f, level_.map_height); - field_float(x, next_y(), w, "span", t.span, 0.5f, 120.0f); - float yaw_deg = t.yaw * 180.0f / PI; - if (field_float(x, next_y(), w, "yaw deg", yaw_deg, -180.0f, 180.0f)) { - t.yaw = yaw_deg * PI / 180.0f; - } - break; - } - case Entity::Spawn: { - field_header("ANCHOR", x, next_y(), w); - Vec2* p = &level_.anchors.spawn; - field_float(x, next_y(), w, "pos x", p->x, 0.0f, level_.map_width); - field_float(x, next_y(), w, "pos y", p->y, 0.0f, level_.map_height); - break; - } - } -} - -void App::draw_inspector() { - if (!inspector_) return; - const float x = static_cast(GetScreenWidth() - kInspectorWidth); - const float top = static_cast(kTopBar); - const float h = static_cast(GetScreenHeight() - kTopBar - kBottomBar); - DrawRectangle(x, top, static_cast(kInspectorWidth), h, {20, 21, 26, 248}); - DrawLine(x, top, x, top + h, {70, 70, 78, 255}); - DrawText("INSPECTOR [TAB]", x + 8, top + 6, 12, {230, 195, 90, 255}); - - if (text_focus_ >= 0 && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) - && !CheckCollisionPointRec(GetMousePosition(), text_rect_)) { - commit_text(); - } - - float y = top + 26.0f; - char summary[96]; - std::snprintf(summary, sizeof(summary), "VALIDATION %s %d err / %d warn", - validation_.ok() ? "OK" : "FAILED", - static_cast(validation_.errors.size()), - static_cast(validation_.warnings.size())); - DrawText(summary, x + 8, y, 11, - validation_.ok() ? Color{110, 220, 140, 255} : Color{240, 110, 90, 255}); - y += 16.0f; - int issues = 0; - for (const auto& e : validation_.errors) { - if (issues++ >= 4) break; - DrawText(e.c_str(), x + 8, y, 10, {240, 130, 110, 255}); - y += 13.0f; - } - for (const auto& w : validation_.warnings) { - if (issues++ >= 4) break; - DrawText(w.c_str(), x + 8, y, 10, {220, 190, 110, 255}); - y += 13.0f; - } - if (status_timer_ > 0.0f && !status_message_.empty()) { - DrawText(status_message_.c_str(), x + 8, y, 10, {150, 210, 240, 255}); - y += 13.0f; - } - DrawLine(x + 6, y, x + kInspectorWidth - 6, y, {70, 70, 78, 255}); - draw_inspector_fields(x + 8, y + 6, static_cast(kInspectorWidth) - 16); -} - -void App::draw_ui() const { - DrawRectangle(0, 0, GetScreenWidth(), kTopBar, {18, 18, 22, 235}); - DrawText("FBC: NIGHT SHIFT / LEVEL EDITOR", 14, 12, 18, {235, 200, 90, 255}); - const std::string status = path_ + (dirty_ ? " * UNSAVED" : ""); - DrawText(status.c_str(), 420, 14, 14, {190, 190, 195, 255}); - - char info[256]; - switch (selection_.kind) { - case Entity::Prop: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.props.size())) { - const auto& p = level_.props[static_cast(selection_.index)]; - std::snprintf(info, sizeof(info), - "PROP %d %s %s map(%.1f, %.1f) scale %.2f", - selection_.index, level::prop_kind_name(p.kind), - p.model.empty() ? "-" : p.model.c_str(), - p.pos.x, p.pos.y, p.scale); - } else info[0] = '\0'; - break; - case Entity::Wall: - std::snprintf(info, sizeof(info), "WALL LINE %d SEGMENT %d", - selection_.index, selection_.sub); - break; - case Entity::Room: - if (selection_.index >= 0 - && selection_.index < static_cast(level_.rooms.size())) { - const auto& r = level_.rooms[static_cast(selection_.index)]; - std::snprintf(info, sizeof(info), "ROOM %d %s kind %s flow %d", - selection_.index, r.name.c_str(), - level::room_kind_name(r.kind), r.flow); - } else info[0] = '\0'; - break; - default: - std::snprintf(info, sizeof(info), - "%zu rooms | %zu wall lines | %zu doors | %zu props | %zu rituals | undo %zu", - level_.rooms.size(), level_.walls.size(), level_.doors.size(), - level_.props.size(), level_.rituals.size(), undo_.size()); - break; - } - DrawText(info, kPanelWidth + 12, kTopBar + 10, 14, {220, 220, 225, 255}); - - DrawRectangle(0, GetScreenHeight() - kBottomBar, GetScreenWidth(), kBottomBar, - {18, 18, 22, 235}); - DrawText("LMB select/drag | 1-4 tools | Del delete | Ctrl+Z/Y undo | F frame | Tab inspector | Ctrl+S save | Ctrl+P playtest | Esc quit", - 14, GetScreenHeight() - 23, 14, {170, 170, 178, 255}); -} - -void App::draw() { - BeginDrawing(); - ClearBackground({26, 27, 32, 255}); - BeginMode3D(camera_); - draw_grid(); - draw_rooms(); - draw_walls(); - draw_props(); - draw_anchors(); - EndMode3D(); - draw_asset_panel(); - draw_ui(); - draw_inspector(); - EndDrawing(); -} - -void App::shutdown() { - commit_text(); - for (auto& item : models_) UnloadModel(item.second); - models_.clear(); -} - -} // namespace editor diff --git a/src/editor/editor_app.hpp b/src/editor/editor_app.hpp deleted file mode 100644 index 3b54c42..0000000 --- a/src/editor/editor_app.hpp +++ /dev/null @@ -1,132 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "level/asset_catalog.hpp" -#include "level/level_data.hpp" -#include "level/validation.hpp" -#include "raylib.h" - -namespace editor { - -enum class Tool { Select, Wall, Door, Decor }; - -enum class Entity { None, Prop, Room, Wall, Door, Ritual, Thermo, Spawn }; - -struct Selection { - Entity kind = Entity::None; - int index = -1; // entity index; for Wall this is the WallLine index - int sub = -1; // visible segment index for Wall -}; - -class App { -public: - explicit App(std::string path); - - void init(); - void update(); - void draw(); - void shutdown(); - - bool should_close() const { return should_close_; } - const std::string& path() const { return path_; } - bool dirty() const { return dirty_; } - void debug_place(const std::string& asset_name, Vec2 map_pos); - void set_game_path(const std::string& path) { game_path_ = path; } - -private: - void update_camera(); - void handle_keys(); - void handle_pointer(); - void select_at(Vector2 mouse); - void place_decor(Vec2 map_pos); - void finish_wall(Vec2 map_pos); - void finish_door(Vec2 map_pos); - void delete_selection(); - void push_undo(); - void push_snapshot(const std::string& snapshot); - void begin_edit(); - void end_edit(); - void commit(); - void revalidate(); - void undo(); - void redo(); - void restore(const std::string& snapshot); - void frame_level(); - void playtest(); - - bool ground_point(Vec2& map_pos) const; - bool inside_panel(Vector2 mouse) const; - bool inside_inspector(Vector2 mouse) const; - int asset_at(Vector2 mouse) const; - Model& model_for(const std::string& path); - Vector3 model_size(const std::string& path); - void draw_model(const std::string& path, Vec2 world_pos, float yaw, - float scale, float y); - - Vector3 camera_offset() const; - Vector3 world(Vec2 p, float y = 0.0f) const; - Vector3 world_direct(Vec2 p, float y = 0.0f) const; - Vec2 prop_world_pos(const level::Prop& p) const; - void draw_rooms() const; - void draw_grid() const; - void draw_walls() const; - void draw_props(); - void draw_anchors() const; - void draw_ui() const; - void draw_asset_panel() const; - void draw_inspector(); - void draw_inspector_fields(float x, float y, float w); - - bool field_header(const char* text, float x, float y, float w); - bool field_bool(float x, float y, float w, const char* label, bool& value); - bool field_float(float x, float y, float w, const char* label, float& value, - float lo, float hi); - bool field_int(float x, float y, float w, const char* label, int& value, - int lo, int hi); - bool field_enum(float x, float y, float w, const char* label, int& value, - const char* items, int count); - bool field_text(float x, float y, float w, int id, const char* label, - std::string& value); - void commit_text(); - - level::LevelData level_; - level::ValidationResult validation_; - std::string path_; - std::string game_path_ = "./cntrl"; - Camera3D camera_{}; - Vector3 target_{}; - float yaw_ = 0.0f; - float pitch_ = 0.85f; - float distance_ = 320.0f; - Selection selection_; - bool dirty_ = false; - bool should_close_ = false; - Vector2 last_mouse_{}; - - Tool tool_ = Tool::Select; - std::vector assets_; - int selected_asset_ = -1; - int asset_scroll_ = 0; - bool dragging_panel_asset_ = false; - bool dragging_prop_ = false; - int dragged_prop_ = -1; - bool pending_set_ = false; - Vec2 pending_{}; - std::vector undo_; - std::vector redo_; - std::unordered_map models_; - std::unordered_map model_sizes_; - - bool inspector_ = true; - bool interacting_ = false; - int text_focus_ = -1; - Rectangle text_rect_{}; - char text_buf_[256] = {}; - std::string status_message_; - float status_timer_ = 0.0f; -}; - -} // namespace editor diff --git a/src/editor/main.cpp b/src/editor/main.cpp deleted file mode 100644 index f2975ce..0000000 --- a/src/editor/main.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include -#include - -#include "editor/editor_app.hpp" -#include "raylib.h" - -int main(int argc, char** argv) { - std::string path = "assets/levels/floor01.json"; - std::string game_path; - std::string screenshot; - std::string place_asset; - float place_x = 0.0f; - float place_y = 0.0f; - for (int i = 1; i < argc; ++i) { - if (std::strcmp(argv[i], "--level") == 0 && i + 1 < argc) { - path = argv[++i]; - } else if (std::strcmp(argv[i], "--game") == 0 && i + 1 < argc) { - game_path = argv[++i]; - } else if (std::strcmp(argv[i], "--screenshot") == 0 && i + 1 < argc) { - screenshot = argv[++i]; - } else if (std::strcmp(argv[i], "--place") == 0 && i + 3 < argc) { - place_asset = argv[++i]; - place_x = std::stof(argv[++i]); - place_y = std::stof(argv[++i]); - } else if (std::strcmp(argv[i], "--help") == 0) { - std::printf("usage: editor [--level ] [--game ]\n" - " [--screenshot ]\n" - " [--place ]\n"); - return 0; - } - } - if (game_path.empty()) { - for (const char* candidate : {"./cntrl", "./build/cntrl", "../cntrl"}) { - if (std::filesystem::exists(candidate)) { - game_path = candidate; - break; - } - } - if (game_path.empty()) game_path = "./build/cntrl"; - } - - SetTraceLogLevel(LOG_WARNING); - InitWindow(1400, 900, "FBC: NIGHT SHIFT - Level Editor"); - SetTargetFPS(60); - - editor::App app(path); - app.set_game_path(game_path); - app.init(); - if (!place_asset.empty()) app.debug_place(place_asset, {place_x, place_y}); - int frames = 0; - while (!WindowShouldClose() && !app.should_close()) { - app.update(); - app.draw(); - if (!screenshot.empty() && ++frames == 30) { - TakeScreenshot(screenshot.c_str()); - break; - } - } - app.shutdown(); - CloseWindow(); - return 0; -}