Skip to content

Commit

Permalink
Add toolbar showing toolbar blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Waqar144 committed Feb 22, 2024
1 parent 02f3a36 commit b54d779
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FetchContent_Declare(
FetchContent_Declare(
graph-prototype
GIT_REPOSITORY https://github.com/fair-acc/graph-prototype.git
GIT_TAG c9b2dd33dcfaf7ab56dc6b1c42b73f46e7dfc001 # main as of 2024-02-01
GIT_TAG ed9738d809d4f89ddbbee49fd9a3bbd87c27bfab # main as of 2024-02-21
)

FetchContent_Declare(
Expand Down
2 changes: 1 addition & 1 deletion src/service/cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(FetchContent)
FetchContent_Declare(
gr-digitizers
GIT_REPOSITORY https://github.com/fair-acc/gr-digitizers.git
GIT_TAG aff5eae3f55eb9a9fcfbf841b08921d5530078b2 # dev-prototype as of 2024-02-01
GIT_TAG 8b261c133f9560f326537fc794641aa9ee4119c5 # dev-prototype as of 2024-02-21
)

FetchContent_MakeAvailable(gr-digitizers)
5 changes: 3 additions & 2 deletions src/ui/blocks/RemoteSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ struct RemoteSource : public gr::Block<RemoteSource<T>> {
auto processBulk(gr::PublishableSpan auto &output) noexcept {
std::size_t written = 0;
std::lock_guard lock(_queue->mutex);
while (written < output.size() && !_queue->data.empty()) {
const auto outputSize = output.end() - output.begin();
while (written < outputSize && !_queue->data.empty()) {
auto &d = _queue->data.front();
auto in = std::span<const float>(d.data.channelValue.begin(), d.data.channelValue.end());
in = in.subspan(d.read, std::min(output.size() - written, in.size() - d.read));
in = in.subspan(d.read, std::min(outputSize - written, in.size() - d.read));

std::copy(in.begin(), in.end(), output.begin() + written);
written += in.size();
Expand Down
3 changes: 2 additions & 1 deletion src/ui/blocks/SineSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ struct SineSource : public gr::Block<SineSource<T>, gr::BlockingIO<true>> {
conditionvar.wait(guard);
}

const auto n = std::min(output.size(), samples.size());
const size_t outputSize = output.end() - output.begin();
const auto n = std::min(outputSize, samples.size());
if (n == 0) {
output.publish(0);
return gr::work::Status::OK;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ FetchContent_Declare(
FetchContent_Declare(
graph-prototype
GIT_REPOSITORY https://github.com/fair-acc/graph-prototype.git
GIT_TAG c9b2dd33dcfaf7ab56dc6b1c42b73f46e7dfc001 # main as of 2024-02-01
GIT_TAG 8fc3c2d5ac3bd09f7cf07212c2eb4fb15ef46371 # main as of 2024-02-01
)

FetchContent_MakeAvailable(imgui implot imgui-node-editor yaml-cpp stb opencmw-cpp plf_colony graph-prototype)
Expand Down
11 changes: 11 additions & 0 deletions src/ui/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ void Block::update() {
}
}

bool Block::isToolbarBlock() const {
const auto &meta = m_node->metaInformation();
auto it = meta.find("Drawable");
if (it != meta.end() && std::holds_alternative<gr::property_map>(it->second)) {
const auto &drawableMap = std::get<gr::property_map>(it->second);
auto catIt = drawableMap.find("Category");
return catIt != drawableMap.end() && std::holds_alternative<std::string>(catIt->second) && std::get<std::string>(catIt->second) == "Toolbar";
}
return false;
}

std::string Block::EnumParameter::toString() const {
return definition.optionsLabels[optionIndex];
}
Expand Down
8 changes: 7 additions & 1 deletion src/ui/flowgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ struct DataType {

const std::string &toString() const;

inline operator Id() const { return m_id; }
inline operator Id() const { return m_id; }

private:
Id m_id = Id::Untyped;
Expand Down Expand Up @@ -328,6 +328,12 @@ class Block {
const std::string name;
const std::string id;

bool isToolbarBlock() const;
void draw() {
// TODO: handle return value
std::ignore = m_node->draw();
}

// protected:
auto &inputs() { return m_inputs; }
auto &outputs() { return m_outputs; }
Expand Down
8 changes: 8 additions & 0 deletions src/ui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "flowgraph.h"
#include "flowgraph/datasink.h"
#include "flowgraphitem.h"
#include "toolbar.h"
#include "toolbar_block.h"
#include "utils/TouchHandler.hpp"

CMRC_DECLARE(ui_assets);
Expand Down Expand Up @@ -246,6 +248,8 @@ int main(int argc, char **argv) {
DigitizerUi::BlockType::registry().addBlockType<opendigitizer::RemoteSource>("opendigitizer::RemoteSource");
DigitizerUi::BlockType::registry().addBlockType<opendigitizer::Arithmetic>("opendigitizer::Arithmetic");
DigitizerUi::BlockType::registry().addBlockType<SpecFFT>("FFT");
DigitizerUi::BlockType::registry().addBlockType<DigitizerUi::GRPlayStopToolbarBlock>("toolbar_playstop_block");
DigitizerUi::BlockType::registry().addBlockType<DigitizerUi::GRLabelToolbarBlock>("toolbar_label_block");

loadFonts(app);

Expand Down Expand Up @@ -360,6 +364,10 @@ static void main_loop(void *arg) {
ImGui::BeginDisabled();
}

if (app->mainViewMode == "View" || app->mainViewMode == "Layout" || app->mainViewMode == "FlowGraph" || app->mainViewMode.empty()) {
DigitizerUi::drawToolbar();
}

ImGuiID viewId = 0;
if (app->mainViewMode == "View" || app->mainViewMode.empty()) {
if (app->dashboard != nullptr) {
Expand Down
57 changes: 57 additions & 0 deletions src/ui/toolbar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef DIGITIZER_TOOLBAR_H
#define DIGITIZER_TOOLBAR_H

#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS true
#endif

#include <imgui.h>

#include "dashboard.h"
#include "flowgraph.h"

#include "app.h"
#include "toolbar_block.h"

namespace DigitizerUi {
namespace detail {
inline bool beginToolbar(const char *id) {
const ImVec2 size = ImGui::GetContentRegionAvail();
const auto width = size.x;
constexpr auto height = 36;
auto ret = ImGui::BeginChild(id, ImVec2(width, height));
auto currentX = ImGui::GetCursorPosX();
ImGui::SetCursorPosX(currentX + 16);

return ret;
}

inline void endToolbar() {
const float width = ImGui::GetWindowWidth();
const float y = (ImGui::GetWindowPos().y + ImGui::GetWindowHeight()) - 1;
const float x = ImGui::GetWindowPos().x;
const uint32_t lineColor = DigitizerUi::App::instance().style() == DigitizerUi::Style::Light ? 0x40000000 : 0x40ffffff;
ImGui::GetWindowDrawList()->AddLine(ImVec2(x, y), ImVec2(width, y), lineColor);

ImGui::EndChild();
}

} // namespace detail

inline void drawToolbar() {
detail::beginToolbar("##Toolbar");

const auto &blocks = App::instance().dashboard->localFlowGraph.blocks();
for (const auto &b : blocks) {
if (b->isToolbarBlock()) {
b->draw();
ImGui::SameLine();
}
}

detail::endToolbar();
}

} // namespace DigitizerUi

#endif
118 changes: 118 additions & 0 deletions src/ui/toolbar_block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#ifndef OPENDIGITIZER_TOOLBAR_BLOCK_H
#define OPENDIGITIZER_TOOLBAR_BLOCK_H

#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS true
#endif

#include "app.h"
#include "imgui.h"

#include <gnuradio-4.0/Block.hpp>

namespace DigitizerUi {

inline bool toolbarButton(const char *label, bool disabled) {
ImGui::BeginDisabled(disabled);
ImGui::PushFont(DigitizerUi::App::instance().fontIconsSolid);
const bool clicked = ImGui::Button(label, ImVec2(28, 28));
ImGui::PopFont();
ImGui::SameLine();
ImGui::EndDisabled();
return clicked;
}

template<typename T>
struct GRPlayStopToolbarBlock : public gr::Block<GRPlayStopToolbarBlock<T>, gr::BlockingIO<false>, gr::Drawable<gr::UICategory::Toolbar, "Dear ImGui">> {
using super_t = gr::Block<GRPlayStopToolbarBlock<T>, gr::BlockingIO<false>, gr::Drawable<gr::UICategory::Toolbar, "Dear ImGui">>;

enum State {
Initial,
PlayStop,
Play,
Stream,
Pause
};

State m_state = Initial;

GRPlayStopToolbarBlock() {}

void playStop() {
// TODO: needs proper impl
m_state = PlayStop;
}

void play() {
// TODO: needs proper impl
m_state = Play;
}

void stream() {
// TODO: needs proper impl
m_state = Stream;
}

void stop() {
// TODO: needs proper impl
m_state = Initial;
}

void pause() {
// TODO: needs proper impl
m_state == Pause;
}

gr::work::Status
draw() noexcept {
if (DigitizerUi::toolbarButton("\uf051", m_state != Initial)) { // play-stop
playStop();
}

if (DigitizerUi::toolbarButton("\uf04b", isPlayDisabled())) { // play
play();
}

if (DigitizerUi::toolbarButton("\uf04e", isStreamDisabled())) { // forward
stream();
}

if (DigitizerUi::toolbarButton("\uf04c", isPauseDisabled())) { // pause
pause();
}

if (DigitizerUi::toolbarButton("\uf04d", isStopDisabled())) { // stop
stop();
}

return gr::work::Status::DONE;
}

bool isStreamDisabled() const { return m_state != Initial && m_state != Pause; }
bool isPlayDisabled() const { return m_state != Initial && m_state != Pause; }
bool isStopDisabled() const { return m_state == Initial || m_state == PlayStop; }
bool isPauseDisabled() const { return m_state == Initial || m_state == PlayStop; }
};

template<typename T>
struct GRLabelToolbarBlock : public gr::Block<GRLabelToolbarBlock<T>, gr::BlockingIO<false>, gr::Drawable<gr::UICategory::Toolbar, "Dear ImGui">> {
GRLabelToolbarBlock() {}

void
processMessages(auto &, std::span<const gr::Message>) {
//
}

gr::work::Status
draw() noexcept {
ImGui::Text("Text block");
return gr::work::Status::DONE;
}
};

} // namespace DigitizerUi

ENABLE_REFLECTION_FOR_TEMPLATE(DigitizerUi::GRPlayStopToolbarBlock, msgOut)
ENABLE_REFLECTION_FOR_TEMPLATE(DigitizerUi::GRLabelToolbarBlock, msgIn)

#endif

0 comments on commit b54d779

Please sign in to comment.