Merge commit '08236fc790e711afe886b6ef545511d35e4e5c6c'

This commit is contained in:
2025-05-24 01:44:07 -05:00
39 changed files with 388 additions and 202 deletions

View File

@ -154,7 +154,7 @@ class AssetRef: public ox::SignalHandler {
private:
constexpr ox::Error emitUpdated() const noexcept {
updated.emit();
return ox::Error(0);
return {};
}
};

View File

@ -260,7 +260,7 @@ ox::Result<DstType> convertObjToObj(
template<typename DstType>
ox::Result<DstType> convert(Context &ctx, ox::BufferView const&src) noexcept {
OX_REQUIRE(out, convert(ctx, src, ox::ModelTypeName_v<DstType>, ox::ModelTypeVersion_v<DstType>));
return std::move(wrapCast<DstType>(out));
return std::move(wrapCast<DstType>(*out));
}
template<typename DstType>

View File

@ -43,7 +43,7 @@ static ox::Error runApp(
oxLogError(turbine::setWindowIcon(*ctx, WindowIcons()));
turbine::setWindowTitle(*ctx, keelCtx(*ctx).appName);
turbine::setKeyEventHandler(*ctx, keyEventHandler);
turbine::setRefreshWithin(*ctx, 0);
turbine::requireRefreshWithin(*ctx, 0);
StudioUI ui(*ctx, projectDataDir);
StudioUIDrawer drawer(ui);
turbine::gl::addDrawer(*ctx, &drawer);
@ -71,9 +71,9 @@ static ox::Error run(
}
ox::Error run(
ox::StringView project,
ox::StringView appName,
ox::StringView projectDataDir,
ox::StringView const project,
ox::StringView const appName,
ox::StringView const projectDataDir,
ox::SpanView<ox::CString> args) noexcept {
return studio::run(ox::sfmt("{} {}", project, appName), projectDataDir, args);
}

View File

@ -2,6 +2,8 @@
* Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <turbine/gfx.hpp>
#include <studio/imguiutil.hpp>
#include "deleteconfirmation.hpp"
@ -43,6 +45,9 @@ void DeleteConfirmation::draw(Context &ctx) noexcept {
ImGui::OpenPopup(title().c_str());
m_stage = Stage::Open;
m_open = true;
// require extended refresh in case new contents require resize
// this can take around a second
turbine::requireRefreshFor(ctx.tctx, 1000);
[[fallthrough]];
case Stage::Open:
drawWindow(ctx.tctx, m_open, [this] {

View File

@ -7,6 +7,8 @@
#include <imgui.h>
#include <ox/std/def.hpp>
#include <keel/media.hpp>
#include <glutils/glutils.hpp>
#include <turbine/turbine.hpp>
@ -17,6 +19,8 @@
#include "font.hpp"
#include "studioui.hpp"
#include <complex>
#ifdef OX_OS_Darwin
#define STUDIO_CTRL "Cmd"
#else
@ -54,7 +58,7 @@ void registerModule(Module const*mod) noexcept {
}
struct StudioConfig {
struct StudioConfigV1 {
static constexpr auto TypeName = "net.drinkingtea.studio.StudioConfig";
static constexpr auto TypeVersion = 1;
ox::String projectPath;
@ -63,13 +67,65 @@ struct StudioConfig {
bool showProjectExplorer = true;
};
OX_MODEL_BEGIN(StudioConfig)
OX_MODEL_BEGIN(StudioConfigV1)
OX_MODEL_FIELD_RENAME(activeTabItemName, active_tab_item_name)
OX_MODEL_FIELD_RENAME(projectPath, project_path)
OX_MODEL_FIELD_RENAME(openFiles, open_files)
OX_MODEL_FIELD_RENAME(showProjectExplorer, show_project_explorer)
OX_MODEL_END()
struct StudioConfigV2 {
static constexpr auto TypeName = "net.drinkingtea.studio.StudioConfig";
static constexpr auto TypeVersion = 2;
struct ProjectConfig {
static constexpr auto TypeName = "net.drinkingtea.studio.ProjectConfig";
static constexpr auto TypeVersion = 2;
ox::String projectPath;
ox::String activeTabItemName;
ox::Vector<ox::String> openFiles;
};
ox::Vector<ProjectConfig> projects;
bool showProjectExplorer = true;
[[nodiscard]]
constexpr ProjectConfig const *project() const {
return projects.empty() ? nullptr : &projects[0];
}
[[nodiscard]]
constexpr ProjectConfig *project() {
return projects.empty() ? nullptr : &projects[0];
}
};
OX_MODEL_BEGIN(StudioConfigV2::ProjectConfig)
OX_MODEL_FIELD_RENAME(activeTabItemName, active_tab_item_name)
OX_MODEL_FIELD_RENAME(projectPath, project_path)
OX_MODEL_FIELD_RENAME(openFiles, open_files)
OX_MODEL_END()
OX_MODEL_BEGIN(StudioConfigV2)
OX_MODEL_FIELD(projects)
OX_MODEL_FIELD_RENAME(showProjectExplorer, show_project_explorer)
OX_MODEL_END()
static ox::Error convertStudioConfigV1ToStudioConfigV2(
keel::Context&,
StudioConfigV1 &src,
StudioConfigV2 &dst) noexcept {
dst.projects.emplace_back(StudioConfigV2::ProjectConfig{
.projectPath = std::move(src.projectPath),
.activeTabItemName = std::move(src.activeTabItemName),
.openFiles = std::move(src.openFiles),
});
dst.showProjectExplorer = src.showProjectExplorer;
return {};
}
using StudioConfig = StudioConfigV2;
StudioUI::StudioUI(turbine::Context &ctx, ox::StringParam projectDataDir) noexcept:
m_sctx{*this, ctx},
m_tctx{ctx},
@ -87,6 +143,9 @@ StudioUI::StudioUI(turbine::Context &ctx, ox::StringParam projectDataDir) noexce
// and hopefully it will change at some point.
io.Fonts->AddFontFromMemoryTTF(const_cast<uint8_t*>(font.data()), static_cast<int>(font.size()), 13, &fontCfg);
}
auto &kctx = keelCtx(m_tctx);
kctx.converters.emplace_back(keel::Converter::make<convertStudioConfigV1ToStudioConfigV2>());
oxLogError(headerizeConfigFile<StudioConfigV1>(kctx));
turbine::setApplicationData(m_tctx, &m_sctx);
turbine::setShutdownHandler(m_tctx, shutdownHandler);
m_projectExplorer.fileChosen.connect(this, &StudioUI::openFile);
@ -102,21 +161,14 @@ StudioUI::StudioUI(turbine::Context &ctx, ox::StringParam projectDataDir) noexce
m_newMenu.finished.connect(this, &StudioUI::openFile);
m_closeAppConfirm.response.connect(this, &StudioUI::handleCloseAppResponse);
m_closeFileConfirm.response.connect(this, &StudioUI::handleCloseFileResponse);
m_removeRecentProject.dlg.response.connect(this, &StudioUI::handleRemoveRecentProjectResponse);
loadModules();
// open project and files
auto const [config, err] = studio::readConfig<StudioConfig>(keelCtx(m_tctx));
m_showProjectExplorer = config.showProjectExplorer;
if (!err) {
auto const openProjErr = openProjectPath(config.projectPath);
if (!openProjErr) {
for (auto const&f: config.openFiles) {
auto const openFileErr = openFileActiveTab(f, config.activeTabItemName == f);
if (openFileErr) {
oxErrorf("\nCould not open editor for file:\n\t{}\nReason:\n\t{}\n", f, toStr(openFileErr));
continue;
}
m_activeEditor = m_editors.back().value->get();
}
if (auto const pc = config.project()) {
oxLogError(openProjectPath(pc->projectPath));
}
} else {
if constexpr(!ox::defines::Debug) {
@ -142,7 +194,7 @@ void StudioUI::navigateTo(ox::StringParam path, ox::StringParam navArgs) noexcep
void StudioUI::draw() noexcept {
glutils::clearScreen();
drawMenu();
auto const&viewport = *ImGui::GetMainViewport();
auto const &viewport = *ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport.WorkPos);
ImGui::SetNextWindowSize(viewport.WorkSize);
ImGui::SetNextWindowViewport(viewport.ID);
@ -194,6 +246,18 @@ void StudioUI::drawMenu() noexcept {
if (ImGui::MenuItem("Open Project...", STUDIO_CTRL "+O")) {
m_taskRunner.add(*ox::make<FileDialogManager>(this, &StudioUI::openProjectPath));
}
if (ImGui::BeginMenu("Recent Projects", m_recentProjects.size() > 1)) {
for (size_t i = 1; i < m_recentProjects.size(); ++i) {
auto const &p = m_recentProjects[i];
if (ImGui::MenuItem(p.c_str())) {
if (openProjectPath(p)) {
m_removeRecentProject.idx = i;
m_removeRecentProject.dlg.open();
}
}
}
ImGui::EndMenu();
}
if (ImGui::MenuItem(
"Save",
STUDIO_CTRL "+S",
@ -270,7 +334,7 @@ void StudioUI::drawTabBar() noexcept {
void StudioUI::drawTabs() noexcept {
for (auto it = m_editors.begin(); it != m_editors.end();) {
auto const&e = *it;
auto const &e = *it;
auto open = true;
auto const unsavedChanges = e->unsavedChanges() ? ImGuiTabItemFlags_UnsavedDocument : 0;
auto const selected = m_activeEditorUpdatePending == e.get() ? ImGuiTabItemFlags_SetSelected : 0;
@ -279,9 +343,11 @@ void StudioUI::drawTabs() noexcept {
if (m_activeEditor != e.get()) [[unlikely]] {
m_activeEditor = e.get();
studio::editConfig<StudioConfig>(keelCtx(m_tctx), [&](StudioConfig &config) {
config.activeTabItemName = m_activeEditor->itemPath();
if (auto const pc = config.project()) {
pc->activeTabItemName = m_activeEditor->itemPath();
}
});
turbine::setRefreshWithin(m_tctx, 0);
turbine::requireRefreshWithin(m_tctx, 0);
} else [[likely]] {
if (m_activeEditorUpdatePending == e.get()) [[unlikely]] {
m_activeEditorUpdatePending = nullptr;
@ -337,14 +403,14 @@ void StudioUI::drawTabs() noexcept {
}
}
void StudioUI::loadEditorMaker(EditorMaker const&editorMaker) noexcept {
for (auto const&ext : editorMaker.fileTypes) {
void StudioUI::loadEditorMaker(EditorMaker const &editorMaker) noexcept {
for (auto const &ext : editorMaker.fileTypes) {
m_editorMakers[ext] = editorMaker.make;
}
}
void StudioUI::loadModule(Module const&mod) noexcept {
for (auto const&editorMaker : mod.editors(m_sctx)) {
void StudioUI::loadModule(Module const &mod) noexcept {
for (auto const &editorMaker : mod.editors(m_sctx)) {
loadEditorMaker(editorMaker);
}
for (auto &im : mod.itemMakers(m_sctx)) {
@ -464,14 +530,16 @@ ox::Error StudioUI::renameFile(ox::StringViewCR path) noexcept {
return m_renameFile.openPath(path);
}
ox::Error StudioUI::handleMoveFile(ox::StringViewCR oldPath, ox::StringViewCR newPath, ox::UUID const&) noexcept {
ox::Error StudioUI::handleMoveFile(ox::StringViewCR oldPath, ox::StringViewCR newPath, ox::UUID const &) noexcept {
for (auto &f : m_openFiles) {
if (f == oldPath) {
f = newPath;
editConfig<StudioConfig>(keelCtx(m_sctx), [&](StudioConfig &cfg) {
auto p = find(cfg.openFiles.begin(), cfg.openFiles.end(), oldPath);
*p = newPath;
cfg.activeTabItemName = newPath;
if (auto const pc = cfg.project()) {
auto p = find(pc->openFiles.begin(), pc->openFiles.end(), oldPath);
*p = newPath;
pc->activeTabItemName = newPath;
}
});
break;
}
@ -546,8 +614,36 @@ ox::Error StudioUI::openProjectPath(ox::StringParam path) noexcept {
m_openFiles.clear();
m_editors.clear();
studio::editConfig<StudioConfig>(keelCtx(m_tctx), [&](StudioConfig &config) {
config.projectPath = ox::String(m_project->projectPath());
config.openFiles.clear();
auto const pcIt = std::find_if(
config.projects.begin(), config.projects.end(),
[this](StudioConfig::ProjectConfig const &pc) {
return pc.projectPath == m_project->projectPath();
});
if (pcIt != config.projects.end()) {
auto p = std::move(*pcIt);
std::ignore = config.projects.erase(pcIt);
auto &pc = *config.projects.emplace(0, std::move(p));
for (auto const &f: pc.openFiles) {
auto const openFileErr = openFileActiveTab(f, pc.activeTabItemName == f);
if (openFileErr) {
oxErrorf("\nCould not open editor for file:\n\t{}\nReason:\n\t{}\n", f, toStr(openFileErr));
continue;
}
m_activeEditor = m_editors.back().value->get();
}
} else {
config.projects.emplace(0, StudioConfig::ProjectConfig{
.projectPath = ox::String{m_project->projectPath()},
.activeTabItemName = {},
.openFiles = {},
});
}
config.projects.resize(ox::min<size_t>(10, config.projects.size()));
m_recentProjects.clear();
m_recentProjects.reserve(config.projects.size());
for (auto const &p : config.projects) {
m_recentProjects.emplace_back(p.projectPath);
}
});
return m_projectExplorer.refreshProjectTreeModel();
}
@ -593,8 +689,10 @@ ox::Error StudioUI::openFileActiveTab(ox::StringViewCR path, bool const makeActi
}
// save to config
studio::editConfig<StudioConfig>(keelCtx(m_tctx), [&path](StudioConfig &config) {
if (!config.openFiles.contains(path)) {
config.openFiles.emplace_back(path);
if (auto const pc = config.project()) {
if (!pc->openFiles.contains(path)) {
pc->openFiles.emplace_back(path);
}
}
});
return {};
@ -618,6 +716,22 @@ ox::Error StudioUI::handleCloseFileResponse(ig::PopupResponse const response) no
return {};
}
ox::Error StudioUI::handleRemoveRecentProjectResponse(ig::PopupResponse const response) noexcept {
if (response == ig::PopupResponse::OK) {
auto const p = std::move(m_recentProjects[m_removeRecentProject.idx]);
studio::editConfig<StudioConfig>(keelCtx(m_tctx), [&p](StudioConfig &config) {
std::ignore = config.projects.erase(
std::remove_if(
config.projects.begin(), config.projects.end(),
[&p](StudioConfig::ProjectConfig const &pc) {
return pc.projectPath == p;
}));
});
return m_recentProjects.erase(m_removeRecentProject.idx).error;
}
return {};
}
ox::Error StudioUI::closeCurrentFile() noexcept {
for (auto &e : m_editors) {
if (m_activeEditor == e.get()) {
@ -636,7 +750,9 @@ ox::Error StudioUI::closeFile(ox::StringViewCR path) noexcept {
std::ignore = m_openFiles.erase(std::remove(m_openFiles.begin(), m_openFiles.end(), path));
// save to config
studio::editConfig<StudioConfig>(keelCtx(m_tctx), [&](StudioConfig &config) {
std::ignore = config.openFiles.erase(std::remove(config.openFiles.begin(), config.openFiles.end(), path));
if (auto const pc = config.project()) {
std::ignore = pc->openFiles.erase(std::remove(pc->openFiles.begin(), pc->openFiles.end(), path));
}
});
return {};
}

View File

@ -44,6 +44,7 @@ class StudioUI: public ox::SignalHandler {
bool m_closeActiveTab{};
ox::Vector<ox::Pair<ox::String>> m_queuedMoves;
ox::Vector<ox::Pair<ox::String>> m_queuedDirMoves;
ox::Vector<ox::String> m_recentProjects;
NewMenu m_newMenu{keelCtx(m_tctx)};
AboutPopup m_aboutPopup{m_tctx};
DeleteConfirmation m_deleteConfirmation;
@ -57,7 +58,11 @@ class StudioUI: public ox::SignalHandler {
MakeCopyPopup m_copyFilePopup;
RenameFile m_renameFile;
NewProject m_newProject{m_projectDataDir};
ox::Array<Widget*, 10> const m_widgets {
struct {
ig::QuestionPopup dlg{"Remove From Recents?", "Unable to load project. Remove from recent projects?"};
size_t idx{};
} m_removeRecentProject;
ox::Array<Widget*, 11> const m_widgets {
&m_closeFileConfirm,
&m_closeAppConfirm,
&m_copyFilePopup,
@ -68,6 +73,7 @@ class StudioUI: public ox::SignalHandler {
&m_newDirDialog,
&m_renameFile,
&m_messagePopup,
&m_removeRecentProject.dlg,
};
bool m_showProjectExplorer = true;
struct NavAction {
@ -100,9 +106,9 @@ class StudioUI: public ox::SignalHandler {
void drawTabs() noexcept;
void loadEditorMaker(EditorMaker const&editorMaker) noexcept;
void loadEditorMaker(EditorMaker const &editorMaker) noexcept;
void loadModule(Module const&mod) noexcept;
void loadModule(Module const &mod) noexcept;
void loadModules() noexcept;
@ -124,7 +130,7 @@ class StudioUI: public ox::SignalHandler {
ox::Error renameFile(ox::StringViewCR path) noexcept;
ox::Error handleMoveFile(ox::StringViewCR oldPath, ox::StringViewCR newPath, ox::UUID const&id) noexcept;
ox::Error handleMoveFile(ox::StringViewCR oldPath, ox::StringViewCR newPath, ox::UUID const &id) noexcept;
ox::Error handleDeleteDir(ox::StringViewCR path) noexcept;
@ -144,6 +150,8 @@ class StudioUI: public ox::SignalHandler {
ox::Error handleCloseFileResponse(ig::PopupResponse response) noexcept;
ox::Error handleRemoveRecentProjectResponse(ig::PopupResponse response) noexcept;
ox::Error closeCurrentFile() noexcept;
ox::Error closeFile(ox::StringViewCR path) noexcept;

View File

@ -4,21 +4,21 @@
#pragma once
#include <ox/claw/claw.hpp>
#include <ox/fs/fs.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/oc/oc.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/defines.hpp>
#include <ox/std/fmt.hpp>
#include <ox/std/trace.hpp>
#include <ox/std/string.hpp>
#include <ox/std/trace.hpp>
#include <keel/context.hpp>
namespace studio {
namespace detail {
inline ox::String slashesToPct(ox::StringView str) noexcept {
inline ox::String slashesToPct(ox::StringViewCR str) noexcept {
auto out = ox::String{str};
for (auto&c: out) {
if (c == '/' || c == '\\') {
@ -30,78 +30,102 @@ inline ox::String slashesToPct(ox::StringView str) noexcept {
}
[[nodiscard]]
ox::String configPath(keel::Context const&ctx) noexcept;
ox::String configPath(keel::Context const&kctx) noexcept;
template<typename T>
ox::Result<T> readConfig(keel::Context &ctx, ox::StringViewCR name) noexcept {
ox::Result<T> readConfig(keel::Context &kctx, ox::StringViewCR name) noexcept {
oxAssert(name != "", "Config type has no TypeName");
auto const path = ox::sfmt("/{}.json", detail::slashesToPct(name));
ox::PassThroughFS fs(configPath(ctx));
ox::PassThroughFS fs(configPath(kctx));
auto const [buff, err] = fs.read(path);
if (err) {
//oxErrf("Could not read config file: {} - {}\n", path, toStr(err));
return err;
}
return ox::readOC<T>(buff);
OX_REQUIRE(id, ox::readClawTypeId(buff));
ox::Result<T> out;
if (id != ox::ModelTypeId_v<T>) {
out = keel::convert<T>(kctx, buff);
} else {
out = ox::readClaw<T>(buff);
}
OX_RETURN_ERROR(out);
OX_RETURN_ERROR(keel::ensureValid(out.value));
return out;
}
template<typename T>
ox::Result<T> readConfig(keel::Context &ctx) noexcept {
ox::Result<T> readConfig(keel::Context &kctx) noexcept {
constexpr auto TypeName = ox::requireModelTypeName<T>();
return readConfig<T>(ctx, TypeName);
return readConfig<T>(kctx, TypeName);
}
template<typename T>
ox::Error writeConfig(keel::Context &ctx, ox::StringViewCR name, T const&data) noexcept {
ox::Error writeConfig(keel::Context &kctx, ox::StringViewCR name, T const&data) noexcept {
oxAssert(name != "", "Config type has no TypeName");
auto const path = ox::sfmt("/{}.json", detail::slashesToPct(name));
ox::PassThroughFS fs(configPath(ctx));
ox::PassThroughFS fs(configPath(kctx));
if (auto const err = fs.mkdir("/", true)) {
//oxErrf("Could not create config directory: {} - {}\n", path, toStr(err));
return err;
}
OX_REQUIRE_M(buff, ox::writeOC(data));
OX_REQUIRE_M(buff, ox::writeClaw(data, ox::ClawFormat::Organic));
*buff.back().value = '\n';
if (auto const err = fs.write(path, buff.data(), buff.size())) {
//oxErrf("Could not read config file: {} - {}\n", path, toStr(err));
return ox::Error(2, "Could not read config file");
return ox::Error{2, "Could not read config file"};
}
return {};
}
template<typename T>
ox::Error writeConfig(keel::Context &ctx, T const&data) noexcept {
ox::Error writeConfig(keel::Context &kctx, T const&data) noexcept {
constexpr auto TypeName = ox::requireModelTypeName<T>();
return writeConfig(ctx, TypeName, data);
return writeConfig(kctx, TypeName, data);
}
template<typename T, typename Func>
void openConfig(keel::Context &ctx, ox::StringViewCR name, Func f) noexcept {
void openConfig(keel::Context &kctx, ox::StringViewCR name, Func f) noexcept {
oxAssert(name != "", "Config type has no TypeName");
auto const [c, err] = readConfig<T>(ctx, name);
auto const [c, err] = readConfig<T>(kctx, name);
oxLogError(err);
f(c);
}
template<typename T, typename Func>
void openConfig(keel::Context &ctx, Func f) noexcept {
void openConfig(keel::Context &kctx, Func f) noexcept {
constexpr auto TypeName = ox::requireModelTypeName<T>();
openConfig<T>(ctx, TypeName, f);
openConfig<T>(kctx, TypeName, f);
}
template<typename T, typename Func>
void editConfig(keel::Context &ctx, ox::StringViewCR name, Func f) noexcept {
void editConfig(keel::Context &kctx, ox::StringViewCR name, Func f) noexcept {
oxAssert(name != "", "Config type has no TypeName");
auto [c, err] = readConfig<T>(ctx, name);
auto [c, err] = readConfig<T>(kctx, name);
oxLogError(err);
f(c);
oxLogError(writeConfig(ctx, name, c));
oxLogError(writeConfig(kctx, name, c));
}
template<typename T, typename Func>
void editConfig(keel::Context &ctx, Func f) noexcept {
void editConfig(keel::Context &kctx, Func f) noexcept {
constexpr auto TypeName = ox::requireModelTypeName<T>();
editConfig<T>(ctx, TypeName, f);
editConfig<T>(kctx, TypeName, f);
}
/**
* Older config files didn't use ClawHeaders, so they can't
* use the normal conversion system.
* Functions like this shouldn't be necessary moving forward.
*/
template<typename T>
ox::Error headerizeConfigFile(keel::Context &kctx, ox::StringViewCR name = ox::ModelTypeName_v<T>) noexcept {
auto const path = ox::sfmt("/{}.json", name);
ox::PassThroughFS fs(configPath(kctx));
OX_REQUIRE_M(buff, fs.read(path));
OX_REQUIRE_M(cv1, ox::readOC<T>(buff));
OX_RETURN_ERROR(ox::writeClaw(cv1, ox::ClawFormat::Organic).moveTo(buff));
return fs.write(path, buff);
}
}

View File

@ -275,6 +275,9 @@ void QuestionPopup::draw(Context &ctx) noexcept {
ImGui::OpenPopup(m_title.c_str());
m_stage = Stage::Open;
m_open = true;
// require extended refresh in case new contents require resize
// this can take around a second
turbine::requireRefreshFor(ctx.tctx, 1000);
[[fallthrough]];
case Stage::Open:
centerNextWindow(ctx.tctx);

View File

@ -49,6 +49,19 @@ ox::Error setWindowBounds(Context &ctx, ox::Bounds const&bnds) noexcept;
* @param ctx - Context
* @param ms - milliseconds
*/
void setRefreshWithin(Context &ctx, int ms) noexcept;
void requireRefreshWithin(Context &ctx, int ms) noexcept;
/**
* Stimulates screen to draw for a period of a short period of
* time (168 ms on GLFW implementation).
* @param ctx - Context
*/
void requireRefresh(Context &ctx) noexcept;
/**
* Stimulates screen to draw for a specified period of time.
* @param ctx - Context
*/
void requireRefreshFor(Context &ctx, int ms) noexcept;
}

View File

@ -155,6 +155,10 @@ static ox::Result<IconData> toGlfwImgPixels(ox::SpanView<uint8_t> const &iconPng
return out;
}
static void setMandatoryRefreshPeriod(Context &ctx, TimeMs const newVal) {
ctx.mandatoryRefreshPeriodEnd = ox::max(ctx.mandatoryRefreshPeriodEnd, newVal);
}
ox::Error setWindowIcon(Context &ctx, ox::SpanView<ox::SpanView<uint8_t>> const &iconPngs) noexcept {
if constexpr(ox::defines::OS != ox::OS::Darwin) {
ox::Vector<IconData, 8> src;
@ -216,10 +220,19 @@ ox::Error setWindowBounds(Context &ctx, ox::Bounds const&bnds) noexcept {
return {};
}
void setRefreshWithin(Context &ctx, int ms) noexcept {
void requireRefreshWithin(Context &ctx, int const ms) noexcept {
ctx.refreshWithinMs = ox::min(ms, ctx.refreshWithinMs);
}
void requireRefresh(Context &ctx) noexcept {
setMandatoryRefreshPeriod(ctx, ticksMs(ctx) + config::MandatoryRefreshPeriod);
}
void requireRefreshFor(Context &ctx, int ms) noexcept {
ms = ox::max(0, ms);
setMandatoryRefreshPeriod(ctx, ticksMs(ctx) + static_cast<TimeMs>(ms));
}
static void draw(Context &ctx) noexcept {
// draw start
#if TURBINE_USE_IMGUI
@ -288,12 +301,12 @@ static void handleGlfwCursorPosEvent(GLFWwindow*, double, double) noexcept {
static void handleGlfwMouseButtonEvent(GLFWwindow *window, int, int, int) noexcept {
auto &ctx = *static_cast<Context*>(glfwGetWindowUserPointer(window));
ctx.mandatoryRefreshPeriodEnd = ticksMs(ctx) + config::MandatoryRefreshPeriod;
setMandatoryRefreshPeriod(ctx, ticksMs(ctx) + config::MandatoryRefreshPeriod);
}
static void handleGlfwKeyEvent(GLFWwindow *window, int const key, int, int const action, int) noexcept {
auto &ctx = *static_cast<Context*>(glfwGetWindowUserPointer(window));
ctx.mandatoryRefreshPeriodEnd = ticksMs(ctx) + config::MandatoryRefreshPeriod;
setMandatoryRefreshPeriod(ctx, ticksMs(ctx) + config::MandatoryRefreshPeriod);
if (action == GLFW_PRESS) {
handleKeyPress(ctx, key, true);
} else if (action == GLFW_RELEASE) {
@ -303,7 +316,7 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int const key, int, int const
static void handleGlfwWindowCloseEvent(GLFWwindow *window) noexcept {
auto &ctx = *static_cast<Context*>(glfwGetWindowUserPointer(window));
ctx.mandatoryRefreshPeriodEnd = ticksMs(ctx) + config::MandatoryRefreshPeriod;
setMandatoryRefreshPeriod(ctx, ticksMs(ctx) + config::MandatoryRefreshPeriod);
ctx.running = ctx.shutdownHandler ? !ctx.shutdownHandler(ctx) : false;
glfwSetWindowShouldClose(window, !ctx.running);
glfwPostEmptyEvent();
@ -316,7 +329,7 @@ ox::Result<ox::UPtr<Context>> init(
using namespace std::chrono;
ctx->startTime = static_cast<TimeMs>(
duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count());
ctx->mandatoryRefreshPeriodEnd = ticksMs(*ctx) + config::MandatoryRefreshPeriod;
setMandatoryRefreshPeriod(*ctx, ticksMs(*ctx) + config::MandatoryRefreshPeriod);
// init GLFW context
glfwInit();
glfwSetErrorCallback(handleGlfwError);