add Node/Imm
This commit is contained in:
@@ -16,10 +16,11 @@ class GenericMemento : public Memento {
|
||||
class CustomTag;
|
||||
|
||||
GenericMemento(File& owner, T&& data) noexcept :
|
||||
owner_(&owner), data_(std::move(data)) {
|
||||
owner_(&owner), initial_(T(data)), data_(std::move(data)) {
|
||||
}
|
||||
~GenericMemento() noexcept {
|
||||
tag_ = nullptr;
|
||||
tag_ = nullptr;
|
||||
last_ = nullptr;
|
||||
assert(map_.empty());
|
||||
}
|
||||
|
||||
@@ -27,7 +28,7 @@ class GenericMemento : public Memento {
|
||||
if (tag_) return tag_;
|
||||
auto [itr, emplaced] = map_.emplace(next_++, data_);
|
||||
assert(emplaced);
|
||||
return tag_ = std::make_shared<CustomTag>(*this, itr->first);
|
||||
return last_ = tag_ = std::make_shared<CustomTag>(*this, itr->first);
|
||||
}
|
||||
void Restore(const std::shared_ptr<Tag>& tag) override {
|
||||
assert(tag);
|
||||
@@ -35,7 +36,7 @@ class GenericMemento : public Memento {
|
||||
assert(itr != map_.end());
|
||||
data_ = itr->second;
|
||||
tag_ = tag;
|
||||
assert(tag_);
|
||||
last_ = tag;
|
||||
|
||||
if (owner_->id()) {
|
||||
owner_->env().Handle(
|
||||
@@ -57,15 +58,25 @@ class GenericMemento : public Memento {
|
||||
T& data() noexcept { return data_; }
|
||||
const T& data() const noexcept { return data_; }
|
||||
|
||||
const T& last() const noexcept {
|
||||
if (!last_) return initial_;
|
||||
|
||||
auto itr = map_.find(last_->id());
|
||||
assert(itr != map_.end());
|
||||
return itr->second;
|
||||
}
|
||||
|
||||
private:
|
||||
File* const owner_;
|
||||
|
||||
const T initial_;
|
||||
T data_;
|
||||
|
||||
Tag::Id next_ = 0;
|
||||
std::unordered_map<Tag::Id, T> map_;
|
||||
|
||||
std::shared_ptr<nf7::Memento::Tag> tag_;
|
||||
std::shared_ptr<nf7::Memento::Tag> last_;
|
||||
|
||||
void NotifyUpdate() noexcept {
|
||||
if (owner_->id()) {
|
||||
|
49
common/gui_resizer.hh
Normal file
49
common/gui_resizer.hh
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
|
||||
namespace nf7::gui {
|
||||
|
||||
bool Resizer(ImVec2* size, const ImVec2& min, const ImVec2& max, float scale,
|
||||
const char* idstr = "##resizer") noexcept {
|
||||
const auto id = ImGui::GetID(idstr);
|
||||
|
||||
size->x = std::clamp(size->x, min.x, max.x);
|
||||
size->y = std::clamp(size->y, min.y, max.y);
|
||||
|
||||
auto ctx = ImGui::GetCurrentContext();
|
||||
auto& io = ImGui::GetIO();
|
||||
|
||||
const auto base = ImGui::GetCursorScreenPos();
|
||||
const auto pos = base + *size*scale;
|
||||
const auto rc = ImRect {pos.x-1*scale, pos.y-1*scale, pos.x, pos.y};
|
||||
|
||||
bool hovered, held;
|
||||
const bool ret = ImGui::ButtonBehavior(rc, id, &hovered, &held,
|
||||
ImGuiButtonFlags_FlattenChildren |
|
||||
ImGuiButtonFlags_PressedOnClickRelease);
|
||||
|
||||
if (hovered || held) ctx->MouseCursor = ImGuiMouseCursor_ResizeNESW;
|
||||
|
||||
ImGuiCol col = ImGuiCol_Button;
|
||||
if (hovered) col = ImGuiCol_ButtonHovered;
|
||||
if (held) {
|
||||
col = ImGuiCol_ButtonActive;
|
||||
*size = (io.MousePos + (ImVec2{scale, scale}-ctx->ActiveIdClickOffset) - base) / scale;
|
||||
size->x = std::clamp(size->x, min.x, max.x);
|
||||
size->y = std::clamp(size->y, min.y, max.y);
|
||||
}
|
||||
|
||||
const auto newpos = base + *size*scale;
|
||||
|
||||
auto dlist = ImGui::GetWindowDrawList();
|
||||
dlist->AddTriangleFilled(
|
||||
newpos, newpos+ImVec2{0, -scale}, newpos+ImVec2{-scale, 0},
|
||||
ImGui::GetColorU32(col));
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace nf7::gui
|
@@ -9,6 +9,7 @@
|
||||
#include <yas/serialize.hpp>
|
||||
#include <yas/types/std/string.hpp>
|
||||
#include <yas/types/std/string_view.hpp>
|
||||
#include <yas/types/std/variant.hpp>
|
||||
#include <yas/types/utility/usertype.hpp>
|
||||
|
||||
#include "nf7.hh"
|
||||
@@ -18,11 +19,14 @@
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class Value final {
|
||||
class Value {
|
||||
public:
|
||||
using IncompatibleException = std::bad_variant_access;
|
||||
class IncompatibleException : public nf7::Exception {
|
||||
public:
|
||||
using nf7::Exception::Exception;
|
||||
};
|
||||
|
||||
class Pulse final { };
|
||||
class Pulse { };
|
||||
class Data;
|
||||
|
||||
using Boolean = bool;
|
||||
@@ -38,6 +42,23 @@ class Value final {
|
||||
Value& operator=(const Value&) = default;
|
||||
Value& operator=(Value&&) = default;
|
||||
|
||||
Value(Pulse v) noexcept : value_(v) { }
|
||||
Value& operator=(Pulse v) noexcept { value_ = v; return *this; }
|
||||
Value(Integer v) noexcept : value_(v) { }
|
||||
Value& operator=(Integer v) noexcept { value_ = v; return *this; }
|
||||
Value(Scalar v) noexcept : value_(v) { }
|
||||
Value& operator=(Scalar v) noexcept { value_ = v; return *this; }
|
||||
Value(Boolean v) noexcept : value_(v) { }
|
||||
Value& operator=(Boolean v) noexcept { value_ = v; return *this; }
|
||||
Value(std::string_view v) noexcept : value_(std::string(v)) { }
|
||||
Value& operator=(std::string_view v) noexcept { value_ = std::string(v); return *this; }
|
||||
Value(String&& v) noexcept : value_(std::move(v)) { }
|
||||
Value& operator=(String&& v) noexcept { value_ = std::move(v); return *this; }
|
||||
Value(const DataPtr& v) noexcept : value_(v) { }
|
||||
Value& operator=(const DataPtr& v) noexcept { value_ = v; return *this; }
|
||||
Value(DataPtr&& v) noexcept : value_(std::move(v)) { }
|
||||
Value& operator=(DataPtr&& v) noexcept { value_ = std::move(v); return *this; }
|
||||
|
||||
bool isPulse() const noexcept { return std::holds_alternative<Pulse>(value_); }
|
||||
bool isBoolean() const noexcept { return std::holds_alternative<Boolean>(value_); }
|
||||
bool isInteger() const noexcept { return std::holds_alternative<Integer>(value_); }
|
||||
@@ -51,6 +72,12 @@ class Value final {
|
||||
const String& string() const { return std::get<String>(value_); }
|
||||
const DataPtr& data() const { return std::get<DataPtr>(value_); }
|
||||
|
||||
Integer& integer() { return std::get<Integer>(value_); }
|
||||
Boolean& boolean() { return std::get<Boolean>(value_); }
|
||||
Scalar& scalar() { return std::get<Scalar>(value_); }
|
||||
String& string() { return std::get<String>(value_); }
|
||||
DataPtr& data() { return std::get<DataPtr>(value_); }
|
||||
|
||||
const char* typeName() const noexcept {
|
||||
struct Visitor final {
|
||||
public:
|
||||
@@ -114,11 +141,11 @@ struct serializer<
|
||||
nf7::Value::DataPtr> {
|
||||
public:
|
||||
template <typename Archive>
|
||||
static Archive& save(Archive& ar, const nf7::Value::DataPtr&) {
|
||||
static Archive& save(Archive&, const nf7::Value::DataPtr&) {
|
||||
throw nf7::Exception("cannot serialize Value::DataPtr");
|
||||
}
|
||||
template <typename Archive>
|
||||
static Archive& load(Archive& ar, nf7::Value::DataPtr&) {
|
||||
static Archive& load(Archive&, nf7::Value::DataPtr&) {
|
||||
throw nf7::DeserializeException("cannot deserialize Value::DataPtr");
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user