Fattorino / ImNodeFlow

Node based editor/blueprints for ImGui
MIT License
119 stars 15 forks source link

Node ctor params #6

Closed avlec closed 4 months ago

avlec commented 4 months ago

Closes #3

Fattorino commented 4 months ago

This looks good, please note that is also necessary to refactor placeNode (which is the method used to work with screen coordinates). And also if you could provide a simple practical use case, mainly to speed up my testing later, thanks.

avlec commented 4 months ago

Sounds good I can get that done later today

avlec commented 4 months ago

Variable length input average. Doing this via templates is inefficient if you're generating multiple instances of these with varying lengths. Doing it via constructor arguments allows for the code to be re-used between averages that take in differing numbers of pins.

template <typename T> struct InputAveragingNode : ImFlow::BaseNode {
  std::size_t mNumInputs;
  float mAverage;
  explicit InputAveragingNode(const std::string &name, ImVec2 pos,
                              ImFlow::ImNodeFlow *inf, std::size_t numInputs)
      : BaseNode(name, pos, inf), mNumInputs(numInputs) {
    // add pins in reverse order so 1 is at the top and mNumInputs is at the bottom
    for (std::size_t i = mNumInputs; i > 0; i--) {
      addIN_uid<T>(i, std::to_string(i), T{});
    }
    addOUT<T>("OUT")->behaviour([this](){ return mAverage; });
  }

  void draw() override {
    ImGui::SetNextItemWidth(100.0);
    mAverage = 0;
    for (std::size_t i = 1; i <= mNumInputs; i++) {
      const float v = getInVal<T>(i);
      // should line up with pins on RHS
      ImGui::Text(": %0.2f", v);
      mAverage += v;
    }
    mAverage /= static_cast<float>(mNumInputs);
    ImGui::Text("Avg: %0.2f", mAverage);
  }
};

Was also wondering what you think of an examples folder for stuffing flushed out examples like this one.

The below video shows it in action. Note that the "gauge" node on the right scales its input down by 12.

https://github.com/Fattorino/ImNodeFlow/assets/17734191/2aa75313-c67e-468c-a58c-6bf62c3580fb