Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions engine/src/toast/console/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ void Console::setNodeRoot(std::function<Box<Node>()> root) {
m_model.setNodeRoot(std::move(root));
}

void Console::setPassControl(PassControl control) {
const std::lock_guard lock(m_mutex);
m_model.setPassControl(std::move(control));
}

void Console::setRestart(std::function<Result()> restart) {
const std::lock_guard lock(m_mutex);
m_model.setRestart(std::move(restart));
}

void Console::setAssetCheck(std::function<bool(std::string_view)> check) {
const std::lock_guard lock(m_mutex);
m_model.setAssetCheck(std::move(check));
Expand Down
4 changes: 4 additions & 0 deletions engine/src/toast/console/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class TOAST_API Console {

void setNodeRoot(std::function<Box<Node>()> root);

void setPassControl(PassControl control);

void setRestart(std::function<Result()> restart);

private:
void handleKey(int key, int mods);
void execute();
Expand Down
165 changes: 151 additions & 14 deletions engine/src/toast/console/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <cctype>
#include <format>
#include <optional>
#include <toast/params/registry.hpp>
#include <toast/params/text.hpp>
#include <unordered_set>
Expand All @@ -19,6 +20,12 @@ using namespace toast::params;

constexpr std::string_view k_whitespace = " \t";

auto lowered(std::string_view text) -> std::string {
std::string out {text};
std::ranges::transform(out, out.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return out;
}

auto trim(std::string_view text) -> std::string_view {
const size_t first = text.find_first_not_of(k_whitespace);
if (first == std::string_view::npos) {
Expand Down Expand Up @@ -108,7 +115,58 @@ auto valueNames(const ParamBase& param) -> std::vector<std::string> {
return names;
}

auto completeSet(std::string_view line, size_t cursor) -> Completion {
constexpr std::string_view k_pass_scope = "renderer.pass";

struct PassEntry {
std::string slug;
bool enabled = true;
};

auto slugOf(std::string_view name) -> std::string {
std::string out;
for (const unsigned char c : name) {
if (std::isalnum(c) != 0) {
out += static_cast<char>(std::tolower(c));
} else if (!out.empty() && out.back() != '_') {
out += '_';
}
}
while (!out.empty() && out.back() == '_') {
out.pop_back();
}
return out;
}

auto passEntries(const PassControl& control) -> std::vector<PassEntry> {
std::vector<PassEntry> out;
if (!control.list) {
return out;
}
for (const auto& [name, enabled] : control.list()) {
std::string slug = slugOf(name);
if (slug.empty()) {
continue;
}
const auto same = std::ranges::find_if(out, [&](const PassEntry& entry) { return entry.slug == slug; });
if (same != out.end()) {
same->enabled = same->enabled || enabled;
} else {
out.push_back({std::move(slug), enabled});
}
}
return out;
}

auto passSlugOf(std::string_view path) -> std::optional<std::string> {
for (const std::string_view prefix : {"renderer.pass.", "render.pass."}) {
if (path.size() >= prefix.size() && lowered(path.substr(0, prefix.size())) == prefix) {
return lowered(path.substr(prefix.size()));
}
}
return std::nullopt;
}

auto completeSet(std::string_view line, size_t cursor, const std::vector<PassEntry>& passes) -> Completion {
cursor = std::min(cursor, line.size());
Completion completion;

Expand All @@ -119,19 +177,30 @@ auto completeSet(std::string_view line, size_t cursor) -> Completion {
}

if (cursor > word_end) {
const ParamBase* param = Registry::get().find(line.substr(word_begin, word_end - word_begin));
if (param == nullptr) {
const std::string_view path = line.substr(word_begin, word_end - word_begin);
const ParamBase* param = Registry::get().find(path);
std::vector<std::string> names;
if (param != nullptr) {
completion.hint = currentText(*param);
names = valueNames(*param);
} else if (const auto slug = passSlugOf(path)) {
const auto pass = std::ranges::find_if(passes, [&](const PassEntry& entry) { return entry.slug == *slug; });
if (pass == passes.end()) {
return completion;
}
completion.hint = pass->enabled ? "true" : "false";
names = {"true", "false"};
} else {
return completion;
}
completion.hint = currentText(*param);
const size_t argument_begin = std::min(line.find_first_not_of(k_whitespace, word_end), line.size());
const size_t partial_end = std::max(cursor, argument_begin);
const std::string_view partial = line.substr(argument_begin, partial_end - argument_begin);
completion.replace_begin = partial_end == cursor && argument_begin > cursor ? cursor : argument_begin;
completion.replace_end = partial_end;

std::vector<Scored> scored;
for (const auto& name : valueNames(*param)) {
for (const auto& name : names) {
if (const auto score = fuzzyScore(partial, name)) {
scored.push_back({
{name, name, {}, EntryKind::value},
Expand Down Expand Up @@ -170,6 +239,20 @@ auto completeSet(std::string_view line, size_t cursor) -> Completion {
return completion;
}

if (lowered(scope) == k_pass_scope) {
std::vector<Scored> entries;
for (const PassEntry& pass : passes) {
if (const auto score = fuzzyScore(partial, pass.slug)) {
entries.push_back({
{pass.slug, pass.slug, pass.enabled ? "true" : "false", EntryKind::variable},
*score
});
}
}
append(completion.entries, entries);
return completion;
}

const size_t group_dot = scope.find('.');
const GroupBase* group = Registry::get().findGroup(scope.substr(0, group_dot));
if (group == nullptr) {
Expand Down Expand Up @@ -205,6 +288,14 @@ auto completeSet(std::string_view line, size_t cursor) -> Completion {
}
}
}
if (lowered(scope) == "renderer" && !passes.empty()) {
if (const auto score = fuzzyScore(partial, "pass"); score && seen.insert("pass").second) {
subgroups.push_back({
{"pass", "pass.", {}, EntryKind::subgroup},
*score
});
}
}
append(completion.entries, variables);
append(completion.entries, subgroups);
return completion;
Expand All @@ -228,10 +319,11 @@ auto Model::complete(std::string_view line, size_t cursor) const -> Completion {
completion.replace_begin = first;
completion.replace_end = first_end;
std::vector<Scored> commands;
for (const std::string_view name : {"set", "node", "show", "hide", "restore"}) {
for (const std::string_view name : {"set", "node", "show", "hide", "restore", "reload"}) {
if (const auto score = fuzzyScore(typed, name)) {
const bool bare = name == "restore" || name == "reload";
commands.push_back({
{std::string {name}, name == "restore" ? std::string {name} : std::string {name} + " ", {}, EntryKind::command},
{std::string {name}, bare ? std::string {name} : std::string {name} + " ", {}, EntryKind::command},
*score
});
}
Expand All @@ -248,7 +340,7 @@ auto Model::complete(std::string_view line, size_t cursor) const -> Completion {
return completeHide(line, cursor, first_end);
}
if (command == "set") {
completion = completeSet(line.substr(first_end), cursor - first_end);
completion = completeSet(line.substr(first_end), cursor - first_end, passEntries(m_passes));
completion.replace_begin += first_end;
completion.replace_end += first_end;
}
Expand All @@ -270,6 +362,12 @@ auto Model::execute(std::string_view raw) -> Result {
registry.requestRestore();
return {true, "restoring everything from disk"};
}
if (command == "reload" && arguments.empty()) {
if (!m_restart) {
return {false, "reload is not available yet"};
}
return m_restart();
}
if (command == "node") {
return executeNode(m_node_root, arguments);
}
Expand All @@ -283,7 +381,7 @@ auto Model::execute(std::string_view raw) -> Result {
if (registry.find(command) != nullptr) {
return {false, std::format("did you mean 'set {}'?", line)};
}
return {false, std::format("no command '{}'; there are set, node, show, hide and restore", command)};
return {false, std::format("no command '{}'; there are set, node, show, hide, restore and reload", command)};
}
if (arguments.empty()) {
return {false, "usage: set <path> [value]"};
Expand All @@ -293,6 +391,10 @@ auto Model::execute(std::string_view raw) -> Result {
const std::string_view path = arguments.substr(0, path_end);
const std::string_view value_text = path_end == std::string_view::npos ? std::string_view {} : trim(arguments.substr(path_end));

if (passSlugOf(path)) {
return executePass(path, value_text);
}

ParamBase* param = registry.find(path);
if (param == nullptr && path.starts_with("render.")) {
// `render.view_mode` for `renderer.view_mode`
Expand Down Expand Up @@ -360,14 +462,49 @@ auto Model::execute(std::string_view raw) -> Result {
return {true, std::format("{} = {}", path, shown)};
}

namespace {
auto Model::executePass(std::string_view path, std::string_view value_text) -> Result {
const std::string slug = *passSlugOf(path);
const std::vector<PassEntry> passes = passEntries(m_passes);
const auto pass = std::ranges::find_if(passes, [&](const PassEntry& entry) { return entry.slug == slug; });
if (pass == passes.end()) {
if (passes.empty()) {
return {false, "no render passes yet; the renderer is not running"};
}
std::string names;
for (const PassEntry& entry : passes) {
names += names.empty() ? "" : ", ";
names += entry.slug;
}
return {false, std::format("no render pass '{}'; there are {}", slug, names)};
}

auto lowered(std::string_view text) -> std::string {
std::string out {text};
std::ranges::transform(out, out.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return out;
const std::string canonical = "renderer.pass." + slug;
if (value_text.empty()) {
return {true, std::format("{} = {}", canonical, pass->enabled)};
}

const std::string word = lowered(value_text);
bool enabled = false;
if (word == "true" || word == "on" || word == "1") {
enabled = true;
} else if (word != "false" && word != "off" && word != "0") {
return {false, std::format("{}: '{}' is not true or false", canonical, value_text)};
}

for (const auto& [name, was_enabled] : m_passes.list()) {
if (slugOf(name) == slug) {
m_passes.set(name, enabled);
}
}
return {true, std::format("{} = {}", canonical, enabled)};
}

void Model::setPassControl(PassControl control) {
m_passes = std::move(control);
}

void Model::setRestart(std::function<Result()> restart) {
m_restart = std::move(restart);
}

auto Model::describe(const Pin& pin) const -> std::pair<std::string, std::string> {
Expand Down
13 changes: 13 additions & 0 deletions engine/src/toast/console/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ struct Result {
std::string message;
};

struct PassControl {
std::function<std::vector<std::pair<std::string, bool>>()> list;
std::function<void(std::string_view name, bool enabled)> set;
};

class TOAST_API Model {
public:
[[nodiscard]]
Expand All @@ -68,7 +73,13 @@ class TOAST_API Model {

void setAssetCheck(std::function<bool(std::string_view)> check);

void setPassControl(PassControl control);

void setRestart(std::function<Result()> restart);

private:
auto executePass(std::string_view path, std::string_view value_text) -> Result;

struct Pin {
std::string spec;
bool parameter = false;
Expand All @@ -83,6 +94,8 @@ class TOAST_API Model {

std::function<bool(std::string_view)> m_asset_check;
std::function<Box<Node>()> m_node_root;
PassControl m_passes;
std::function<Result()> m_restart;
std::vector<Pin> m_pins;
};

Expand Down
34 changes: 34 additions & 0 deletions engine/src/toast/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,40 @@ void Engine::init() {
console::Console::get().setAssetCheck([](std::string_view uri) {
return !assets::AssetManager::isInitialized() || assets::AssetManager::resolveURI(uri).has_value();
});
console::Console::get().setPassControl({
.list =
[] {
std::vector<std::pair<std::string, bool>> passes;
for (const auto& pass : renderer::listRenderPasses()) {
passes.emplace_back(pass.name, pass.enabled);
}
return passes;
},
.set = [](std::string_view name, bool enabled) { renderer::setRenderPassEnabled(name, enabled); },
});
console::Console::get().setRestart([this]() -> console::Result {
{
std::scoped_lock lock(m->owners_mutex);
if (const auto it = m->owners.find(m->active_workspace); it != m->owners.end()) {
if (Workspace* workspace = it->second->asWorkspace(); workspace != nullptr && workspace->isValid()) {
return {false, "reload not available on Editor"};
}
}
}
Box<Node> root = World::root();
if (!root.exists()) {
return {false, "there is no root node to restart"};
}
const UID prefab = root->sourcePrefab().uid();
if (prefab.data() == 0) {
return {false, std::format("'{}' was not loaded from a prefab, so it cannot be reloaded", root->name())};
}
const std::string name {root->name()}; // the box dies with the node
World::cacheNode(*root);
World::destroyNode(*root);
World::loadNode(prefab);
return {true, std::format("reloading '{}'", name)};
});
console::Console::get().setNodeRoot([this]() -> Box<Node> {
{
std::scoped_lock lock(m->owners_mutex);
Expand Down
Loading
Loading