Skip to content

Commit

Permalink
Settings get set
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-cukic committed Nov 24, 2024
1 parent 6740b78 commit d31b4fe
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 58 deletions.
3 changes: 2 additions & 1 deletion src/ui/Dashboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ void Dashboard::setNewDescription(const std::shared_ptr<DashboardDescription>& d
void Dashboard::load() {
if (m_desc->source != unsavedSource()) {
fetch(
m_desc->source, m_desc->filename, {What::Flowgraph, What::Dashboard}, [_this = shared()](std::array<std::string, 2>&& data) { _this->load(std::move(data[0]), std::move(data[1])); },
m_desc->source, m_desc->filename, {What::Flowgraph, What::Dashboard}, //
[_this = shared()](std::array<std::string, 2>&& data) { _this->load(std::move(data[0]), std::move(data[1])); },
[_this = shared()]() {
auto error = fmt::format("Invalid flowgraph for dashboard {}/{}", _this->m_desc->source->path, _this->m_desc->filename);
components::Notification::error(error);
Expand Down
6 changes: 4 additions & 2 deletions src/ui/DashboardPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ void DashboardPage::drawPlots(Dashboard& dashboard, DigitizerUi::DashboardPage::
plotItemHovered = true;

if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
m_editPane.block = dashboard.localFlowGraph.findBlock(s->blockName);
// TODO(NOW) which node was clicked -- it needs to have a sane way to get this?
m_editPane.block = nullptr; // dashboard.localFlowGraph.findBlock(s->blockName);
m_editPane.closeTime = std::chrono::system_clock::now() + LookAndFeel::instance().editPaneCloseDelay;
}

Expand Down Expand Up @@ -548,6 +549,7 @@ void DashboardPage::drawGrid(float w, float h) {
}

void DashboardPage::drawLegend(Dashboard& dashboard, const DashboardPage::Mode& mode) noexcept {
#ifdef TODO_PORT
alignForWidth(std::max(10.f, legend_box.x), 0.5f);
legend_box.x = 0.f;
{
Expand Down Expand Up @@ -612,6 +614,7 @@ void DashboardPage::drawLegend(Dashboard& dashboard, const DashboardPage::Mode&
}
}
// end draw legend
#endif
}

void DashboardPage::newPlot(Dashboard& dashboard) {
Expand Down Expand Up @@ -682,7 +685,6 @@ void DashboardPage::newPlot(Dashboard& dashboard) {
void DashboardPage::addSignalCallback(Dashboard& dashboard, Block* block) {
ImGui::CloseCurrentPopup();
auto* newsink = dashboard.createSink();
// newPlot(dashboard);
dashboard.localFlowGraph.connect(&block->outputs()[0], &newsink->inputs()[0]);
newPlot(dashboard);
auto source = std::ranges::find_if(dashboard.sources(), [newsink](const auto& s) { return s.blockName == newsink->name; });
Expand Down
10 changes: 7 additions & 3 deletions src/ui/Flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,20 @@ void FlowGraph::parse(const std::filesystem::path& file) {
}

void FlowGraph::parse(const std::string& str) {
fmt::print("FlowGraph::parse {}\n", str);
clear();

auto graph = [this, &str]() {
auto grGraph = [this, &str]() {
try {
return gr::loadGrc(*_pluginLoader, str);
} catch (const std::string& e) {
throw std::runtime_error(e);
}
}();

graph.forEachBlock([&](const auto& grBlock) {
fmt::print("Number of blocks {}, number of edges {}\n", grGraph.blocks().size(), grGraph.edges().size());

grGraph.forEachBlock([&](const auto& grBlock) {
auto typeName = grBlock.typeName();
typeName = std::string_view(typeName.begin(), typeName.find('<'));
auto type = BlockDefinition::registry().get(typeName);
Expand Down Expand Up @@ -391,7 +394,7 @@ void FlowGraph::parse(const std::string& str) {
portDefinition.definition);
};

graph.forEachEdge([&](const auto& edge) {
grGraph.forEachEdge([&](const auto& edge) {
auto srcBlock = findBlock(edge._sourceBlock->uniqueName());
assert(srcBlock);
// TODO do not ignore subindexes
Expand All @@ -403,6 +406,7 @@ void FlowGraph::parse(const std::string& str) {
if (srcPort == srcBlock->m_outputs.end() || dstPort == dstBlock->m_inputs.end()) {
return;
}

connect(&*srcPort, &*dstPort);
});

Expand Down
2 changes: 1 addition & 1 deletion src/ui/FlowgraphItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ void FlowGraphItem::draw(FlowGraph* fg, const ImVec2& size) {

if (ImGui::IsMouseReleased(ImGuiMouseButton_Left) && mouseDrag < 200 && ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) {
auto n = ax::NodeEditor::GetHoveredNode();
auto block = n.AsPointer<Block>();
auto block = n.AsPointer<UiGraphBlock>();

if (!block) {
m_editPaneContext.block = nullptr;
Expand Down
13 changes: 4 additions & 9 deletions src/ui/GraphModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,13 @@ void UiGraphModel::handleBlockSettingsChanged(std::string uniqueName, const gr::
return;
}
fmt::print("\u001b[32mNew settings for {} are {}\n\u001b[0m", uniqueName, data);
}

void UiGraphModel::handleBlockSettingsStaged(std::string uniqueName, const gr::property_map& data) {
auto [blockIt, found] = findBlockByName(uniqueName);
if (!found) {
// TODO warning
fmt::print("\u001b[32mWARNING: Settings staged for an unknown block {} -> {}\n\u001b[0m", uniqueName, data);
return;
for (const auto& [key, value] : data) {
blockIt->blockSettings.insert_or_assign(key, value);
}
fmt::print("\u001b[32mNew settings for {} are {}\n\u001b[0m", uniqueName, data);
}

void UiGraphModel::handleBlockSettingsStaged(std::string uniqueName, const gr::property_map& data) { handleBlockSettingsChanged(uniqueName, data); }

void UiGraphModel::handleEdgeEmplaced(const gr::property_map& data) {
UiGraphEdge edge(this);
if (setEdgeData(edge, data)) {
Expand Down
94 changes: 58 additions & 36 deletions src/ui/components/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../common/LookAndFeel.hpp"

#include "../App.hpp"
#include "../Dashboard.hpp"
#include "../DashboardPage.hpp"

Expand Down Expand Up @@ -71,6 +72,7 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
size = ImGui::GetContentRegionAvail();

int outputsCount = 0;
#ifdef TODO_PORT
{
const char* prevString = verticalLayout ? "\uf062" : "\uf060";
for (const auto& out : context.block->outputs()) {
Expand Down Expand Up @@ -109,7 +111,9 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
if (!verticalLayout) {
ImGui::SameLine();
}
#endif

#ifdef TODO_PORT
{
// Draw the two add block buttons
{
Expand Down Expand Up @@ -189,7 +193,9 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
ImGui::SameLine();
}
}
#endif

#ifdef TODO_PORT
if (context.mode != BlockControlsPanelContext::Mode::None) {
{
IMW::Group group;
Expand All @@ -210,7 +216,7 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
IMW::Disabled dg(!ret.has_value());
if (ImGui::Button("Ok")) {
BlockDefinition* selected = ret->first;
auto name = fmt::format("{}({})", selected->name, context.block->name);
auto name = fmt::format("{}({})", selected->name, context.block->blockName);

auto block = selected->createBlock(name);

Expand Down Expand Up @@ -249,18 +255,20 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
ImGui::SameLine();
}
}
#endif

{
IMW::Child settings("Settings", verticalLayout ? ImVec2(size.x, ImGui::GetContentRegionAvail().y - lineHeight - itemSpacing.y) : ImVec2(ImGui::GetContentRegionAvail().x - lineHeight - itemSpacing.x, size.y), true, ImGuiWindowFlags_HorizontalScrollbar);
ImGui::TextUnformatted(context.block->name.c_str());
std::string_view typeName = context.block->typeName();
ImGui::TextUnformatted(context.block->blockName.c_str());
std::string_view typeName = context.block->blockTypeName;

ImGui::TextUnformatted("<");
ImGui::SameLine();

// auto types = context.block->type().availableBaseTypes;
// std::vector<std::string> typeNames{types.size()};
// std::ranges::transform(types, typeNames.begin(), [](auto t) { return DataType::name(t); });
#ifdef TODO_PORT
if (ImGui::BeginCombo("##baseTypeCombo", context.block->currentInstantiationName().c_str())) {
for (const auto& [instantiationName, _] : context.block->type().instantiations) {
if (ImGui::Selectable(instantiationName.c_str(), typeName == instantiationName)) {
Expand All @@ -272,6 +280,7 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
}
ImGui::EndCombo();
}
#endif

ImGui::SameLine();
ImGui::TextUnformatted(">");
Expand All @@ -285,10 +294,11 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc

ImGui::SetCursorPos(minpos);

#ifdef TODO_PORT
// draw the button(s) that go to the previous block(s).
const char* nextString = verticalLayout ? "\uf063" : "\uf061";
IMW::Font font(LookAndFeel::instance().fontIconsSolid);
if (context.block->inputs().empty()) {
if (context.block->inputsPorts.empty()) {
auto buttonSize = calcButtonSize(1);
if (verticalLayout) {
ImGui::SetCursorPosY(ImGui::GetContentRegionMax().y - buttonSize.y);
Expand Down Expand Up @@ -324,6 +334,7 @@ void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, Bloc
}
}
}
#endif
}

namespace {
Expand All @@ -335,7 +346,7 @@ template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
} // namespace

void BlockSettingsControls(DigitizerUi::Block* b, bool verticalLayout, const ImVec2& size) {
void BlockSettingsControls(UiGraphBlock* block, bool verticalLayout, const ImVec2& size) {
const auto availableSize = ImGui::GetContentRegionAvail();

auto storage = ImGui::GetStateStorage();
Expand All @@ -346,7 +357,7 @@ void BlockSettingsControls(DigitizerUi::Block* b, bool verticalLayout, const ImV
const auto textColor = ImGui::ColorConvertFloat4ToU32(style.Colors[ImGuiCol_Text]);

int i = 0;
for (const auto& p : b->settings()) {
for (const auto& p : block->blockSettings) {
auto id = ImGui::GetID(p.first.c_str());
ImGui::PushID(int(id));
auto* enabled = storage->GetBoolRef(id, true);
Expand All @@ -362,36 +373,47 @@ void BlockSettingsControls(DigitizerUi::Block* b, bool verticalLayout, const ImV
char label[64];
snprintf(label, sizeof(label), "##parameter_%d", i);

const bool controlDrawn = std::visit(overloaded{[&](float val) {
ImGui::SetCursorPosY(curpos.y + ImGui::GetFrameHeightWithSpacing());
ImGui::SetNextItemWidth(100);
if (InputKeypad<>::edit(label, &val)) {
b->setSetting(p.first, val);
b->update();
}
return true;
},
[&](auto&& val) {
using T = std::decay_t<decltype(val)>;
if constexpr (std::integral<T>) {
auto v = int(val);
ImGui::SetCursorPosY(curpos.y + ImGui::GetFrameHeightWithSpacing());
ImGui::SetNextItemWidth(100);
if (InputKeypad<>::edit(label, &v)) {
b->setSetting(p.first, v);
b->update();
}
return true;
} else if constexpr (std::same_as<T, std::string> || std::same_as<T, std::string_view>) {
ImGui::SetCursorPosY(curpos.y + ImGui::GetFrameHeightWithSpacing());
std::string str(val);
if (ImGui::InputText("##in", &str)) {
b->setSetting(p.first, std::move(str));
}
return true;
}
return false;
}},
auto sendSetSettingMessage = [](auto blockUniqueName, auto key, auto value) {
gr::Message message;
message.serviceName = blockUniqueName;
message.endpoint = gr::block::property::kSetting;
message.cmd = gr::message::Command::Set;
message.data = gr::property_map{{key, value}};
App::instance().sendMessage(std::move(message));
};

const bool controlDrawn = std::visit( //
overloaded{ //
[&](float val) {
ImGui::SetCursorPosY(curpos.y + ImGui::GetFrameHeightWithSpacing());
ImGui::SetNextItemWidth(100);
if (InputKeypad<>::edit(label, &val)) {
fmt::print("Sending set settings message {} {} {}\n", block->blockUniqueName, p.first, val);
sendSetSettingMessage(block->blockUniqueName, p.first, val);
}
return true;
},
[&](auto&& val) {
using T = std::decay_t<decltype(val)>;
if constexpr (std::integral<T>) {
auto v = int(val);
ImGui::SetCursorPosY(curpos.y + ImGui::GetFrameHeightWithSpacing());
ImGui::SetNextItemWidth(100);
if (InputKeypad<>::edit(label, &v)) {
fmt::print("Sending set settings message {} {} {}\n", block->blockUniqueName, p.first, val);
sendSetSettingMessage(block->blockUniqueName, p.first, v);
}
return true;
} else if constexpr (std::same_as<T, std::string> || std::same_as<T, std::string_view>) {
ImGui::SetCursorPosY(curpos.y + ImGui::GetFrameHeightWithSpacing());
std::string str(val);
if (ImGui::InputText("##in", &str)) {
sendSetSettingMessage(block->blockUniqueName, p.first, std::move(str));
}
return true;
}
return false;
}},
p.second);

if (!controlDrawn) {
Expand Down
15 changes: 9 additions & 6 deletions src/ui/components/Block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ class DashboardPage;
namespace DigitizerUi::components {

struct BlockControlsPanelContext {
DigitizerUi::Block* block = {};
UiGraphBlock* block = nullptr;
enum class Mode { None, Insert, AddAndBranch };
Mode mode = Mode::None;
DigitizerUi::Block::Port* insertBefore = nullptr;
DigitizerUi::Block::Port* insertFrom = nullptr;
DigitizerUi::Connection* breakConnection = nullptr;

Mode mode = Mode::None;

// DigitizerUi::Block::Port* insertBefore = nullptr;
// DigitizerUi::Block::Port* insertFrom = nullptr;
// DigitizerUi::Connection* breakConnection = nullptr;

std::chrono::time_point<std::chrono::system_clock> closeTime;
};

void BlockControlsPanel(Dashboard& dashboard, DashboardPage& dashboardPage, BlockControlsPanelContext& context, const ImVec2& pos, const ImVec2& frameSize, bool verticalLayout);
void BlockSettingsControls(DigitizerUi::Block* b, bool verticalLayout, const ImVec2& size = {0.f, 0.f});
void BlockSettingsControls(UiGraphBlock* block, bool verticalLayout, const ImVec2& size = {0.f, 0.f});

} // namespace DigitizerUi::components

Expand Down

0 comments on commit d31b4fe

Please sign in to comment.