implement UpdateWidget() to Node/Imm

This commit is contained in:
2022-08-27 00:31:13 +09:00
parent a703ba3bbc
commit 901f5c4ab9
3 changed files with 50 additions and 40 deletions

View File

@@ -15,6 +15,7 @@ constexpr size_t kBufferSizeMax = 64 * 1024 * 1024;
// Pushes a metatable for Thread object, available on global table as 'nf7'.
static void PushMeta(lua_State*) noexcept;
lua_State* Thread::Init(lua_State* L) noexcept {
assert(state_ == kInitial);
@@ -73,6 +74,45 @@ void Thread::Abort() noexcept {
}
Thread::Handler Thread::CreateNodeLambdaHandler(
const std::shared_ptr<nf7::Node::Lambda>& caller,
const std::shared_ptr<nf7::Node::Lambda>& callee) noexcept {
return [caller, callee](auto& th, auto L) {
switch (th.state()) {
case nf7::luajit::Thread::kPaused:
switch (lua_gettop(L)) {
case 0:
th.ExecResume(L);
return;
case 2: {
auto k = luaL_checkstring(L, 1);
auto v = nf7::luajit::CheckValue(L, 2);
caller->env().ExecSub(
caller, [caller, callee, k = std::string {k}, v = std::move(v)]() {
caller->Handle(k, v, callee);
});
} return;
default:
if (auto log = th.logger()) {
log->Warn("invalid use of yield, nf7:yield() or nf7:yield(name, value)");
}
th.Resume(L, 0);
return;
}
case nf7::luajit::Thread::kFinished:
return;
default:
if (auto log = th.logger()) {
log->Warn(std::string {"luajit execution error: "}+lua_tostring(L, -1));
}
return;
}
};
}
static void PushMeta(lua_State* L) noexcept {
if (luaL_newmetatable(L, Thread::kTypeName)) {
PushWeakPtrDeleter<Thread>(L);

View File

@@ -46,7 +46,7 @@ class Thread final : public std::enable_shared_from_this<Thread> {
nf7::Future<T>::Promise& pro, std::function<T(lua_State*)>&&) noexcept;
// Creates a handler to emit yielded value to Node::Lambda.
static inline Handler CreateNodeLambdaHandler(
static Handler CreateNodeLambdaHandler(
const std::shared_ptr<nf7::Node::Lambda>& caller,
const std::shared_ptr<nf7::Node::Lambda>& callee) noexcept;
@@ -183,42 +183,4 @@ Thread::Handler Thread::CreatePromiseHandler(
};
}
Thread::Handler Thread::CreateNodeLambdaHandler(
const std::shared_ptr<nf7::Node::Lambda>& caller,
const std::shared_ptr<nf7::Node::Lambda>& callee) noexcept {
return [caller, callee](auto& th, auto L) {
switch (th.state()) {
case nf7::luajit::Thread::kPaused:
switch (lua_gettop(L)) {
case 0:
th.ExecResume(L);
return;
case 2: {
auto k = luaL_checkstring(L, 1);
auto v = nf7::luajit::CheckValue(L, 2);
caller->env().ExecSub(
caller, [caller, callee, k = std::string {k}, v = std::move(v)]() {
caller->Handle(k, v, callee);
});
} return;
default:
if (auto log = th.logger()) {
log->Warn("invalid use of yield, nf7:yield() or nf7:yield(name, value)");
}
th.Resume(L, 0);
return;
}
case nf7::luajit::Thread::kFinished:
return;
default:
if (auto log = th.logger()) {
log->Warn(std::string {"luajit execution error: "}+lua_tostring(L, -1));
}
return;
}
};
}
} // namespace nf7::luajit

View File

@@ -45,7 +45,8 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node {
class Lambda;
Imm(nf7::Env& env, nf7::gui::Value&& v = {}) noexcept :
nf7::File(kType, env), nf7::DirItem(DirItem::kNone),
nf7::File(kType, env),
nf7::DirItem(DirItem::kWidget),
life_(*this), mem_(std::move(v), *this) {
}
@@ -71,6 +72,7 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node {
}
void UpdateNode(nf7::Node::Editor&) noexcept override;
void UpdateWidget() noexcept override;
nf7::File::Interface* interface(const std::type_info& t) noexcept override {
return InterfaceSelector<
@@ -138,6 +140,12 @@ void Imm::UpdateNode(nf7::Node::Editor&) noexcept {
mem_.Commit();
}
}
void Imm::UpdateWidget() noexcept {
ImGui::TextUnformatted("Node/Imm");
if (mem_.data().UpdateEditor()) {
mem_.Commit();
}
}
}
} // namespace nf7