Skip to content

Commit

Permalink
Move 2D viewer to the ViewTree object instead of having it handled by…
Browse files Browse the repository at this point in the history
… the nodes themselves
  • Loading branch information
otto-link committed Sep 21, 2023
1 parent 1ad3277 commit 0c6a71f
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 84 deletions.
2 changes: 1 addition & 1 deletion external/GNode
4 changes: 3 additions & 1 deletion include/hesiod/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ bool drag_float_vector(std::vector<float> &vector,
bool horizontal = false,
float height = 160.f);

bool listbox_map_enum(std::map<std::string, int> &map, int &selected);
bool listbox_map_enum(std::map<std::string, int> &map,
int &selected,
float width = -FLT_MIN);

bool slider_vmin_vmax(float &vmin, float &vmax);

Expand Down
9 changes: 2 additions & 7 deletions include/hesiod/view_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ class ViewNode

gnode::Node *get_p_control_node();

std::string get_view2d_port_id();

void set_p_control_node(gnode::Node *new_p_control_node);

void set_preview_port_id(std::string new_port_id);

void set_preview_type(int new_preview_type);

void set_show_view2d(bool new_show_view2d);

void set_view2d_port_id(std::string new_port_id);

virtual void post_control_node_update();
Expand All @@ -87,8 +87,6 @@ class ViewNode

bool render_settings_footer();

bool render_view2d();

virtual void serialize_load(cereal::JSONInputArchive &);
virtual void serialize_save(cereal::JSONOutputArchive &);

Expand All @@ -100,16 +98,13 @@ class ViewNode
std::string preview_port_id = "";
std::string view2d_port_id = "";
bool show_preview = true;
bool show_view2d = false;
float node_width = 128.f;

private:
gnode::Node *p_control_node;
hmap::Vec2<int> shape_preview = {128, 128};
hmap::Vec2<int> shape_view2d = {128, 128};
int preview_type = preview_type::grayscale;
GLuint image_texture_preview = 0;
GLuint image_texture_view2d = 0;
void init_from_control_node();
};

Expand Down
24 changes: 20 additions & 4 deletions include/hesiod/view_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ViewTree : public gnode::Tree
std::string node_id_to, // in
std::string port_id_to);

void post_update();

void remove_link(int link_id);

void remove_view_node(std::string node_id);
Expand All @@ -99,7 +101,11 @@ class ViewTree : public gnode::Tree

void render_view_nodes();

void render_view2d(std::string node_id);
bool render_view2d();

void update_image_texture_view2d();

// serialization

void load_state(std::string fname);

Expand All @@ -121,9 +127,19 @@ class ViewTree : public gnode::Tree

ax::NodeEditor::EditorContext *p_node_editor_context = nullptr;

bool open_node_list_window = false;
bool open_view2d_window = false;
std::string view2d_node_id = "";
bool open_node_list_window = false;

bool open_view2d_window = false;
std::string view2d_node_id = "";
GLuint image_texture_view2d = 0;
hmap::Vec2<int> shape_view2d = {512, 512};
int cmap_view2d = hmap::cmap::inferno;
bool hillshade_view2d = false;

std::map<std::string, int> cmap_map = {{"gray", hmap::cmap::gray},
{"inferno", hmap::cmap::inferno},
{"terrain", hmap::cmap::terrain}};

bool show_settings = false;
ax::NodeEditor::NodeId context_menu_node_hid;
std::vector<std::string> key_sort;
Expand Down
6 changes: 4 additions & 2 deletions src/gui/widgets/listbox_map_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace hesiod::gui
{

bool listbox_map_enum(std::map<std::string, int> &map, int &selected)
bool listbox_map_enum(std::map<std::string, int> &map,
int &selected,
float width)
{
bool has_changed = false;

Expand All @@ -17,7 +19,7 @@ bool listbox_map_enum(std::map<std::string, int> &map, int &selected)
float height = ImGui::GetStyle().ItemSpacing.y +
(float)map.size() * ImGui::GetTextLineHeightWithSpacing();

if (ImGui::BeginListBox("##method", ImVec2(-FLT_MIN, height)))
if (ImGui::BeginListBox("##method", ImVec2(width, height)))
for (auto &[cname, k] : map)
{
bool is_selected = false;
Expand Down
4 changes: 0 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ int main()
hmap::Path path = hmap::Path(15, 3);
path.reorder_nns();

tree.add_view_node("Path");

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
Expand Down Expand Up @@ -97,8 +95,6 @@ int main()
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

glfwSwapBuffers(window);

// hesiod::gui::save_screenshot("screenshot.png");
}

// --- Cleanup
Expand Down
10 changes: 5 additions & 5 deletions src/nodes/view_node/view_hydraulic_vpipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ bool ViewHydraulicVpipes::render_settings()
ImGui::InputInt("iterations", &this->iterations);
has_changed |= this->trigger_update_after_edit();

ImGui::SliderFloat("water_height", &this->water_height, 0.f, 0.5f);
ImGui::SliderFloat("water_height", &this->water_height, 0.001f, 0.5f);
has_changed |= this->trigger_update_after_edit();

ImGui::SliderFloat("c_capacity", &this->c_capacity, 0.1f, 0.5f);
ImGui::SliderFloat("c_capacity", &this->c_capacity, 0.001f, 0.5f);
has_changed |= this->trigger_update_after_edit();

ImGui::SliderFloat("c_erosion", &this->c_erosion, 0.001f, 1.f);
ImGui::SliderFloat("c_erosion", &this->c_erosion, 0.f, 0.5f);
has_changed |= this->trigger_update_after_edit();

ImGui::SliderFloat("c_deposition", &this->c_deposition, 0.001f, 1.f);
ImGui::SliderFloat("c_deposition", &this->c_deposition, 0.f, 0.5f);
has_changed |= this->trigger_update_after_edit();

ImGui::SliderFloat("rain_rate", &this->rain_rate, 0.f, 0.1f);
has_changed |= this->trigger_update_after_edit();

ImGui::SliderFloat("evap_rate", &this->evap_rate, 0.f, 0.1f);
ImGui::SliderFloat("evap_rate", &this->evap_rate, 0.001f, 0.1f);
has_changed |= this->trigger_update_after_edit();

has_changed |= this->render_settings_footer();
Expand Down
51 changes: 5 additions & 46 deletions src/nodes/view_node/view_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ gnode::Node *ViewNode::get_p_control_node()
return this->p_control_node;
}

std::string ViewNode::get_view2d_port_id()
{
return this->view2d_port_id;
}

void ViewNode::set_p_control_node(gnode::Node *new_p_control_node)
{
this->p_control_node = new_p_control_node;
Expand Down Expand Up @@ -58,19 +63,10 @@ void ViewNode::set_preview_type(int new_preview_type)
this->update_preview();
}

void ViewNode::set_show_view2d(bool new_show_view2d)
{
this->show_view2d = new_show_view2d;
}

void ViewNode::set_view2d_port_id(std::string new_port_id)
{
if (this->p_control_node->is_port_id_in_keys(new_port_id))
{
this->view2d_port_id = new_port_id;
this->p_control_node->update();
this->update_preview();
}
else
{
LOG_ERROR("port id [%s] not found", new_port_id.c_str());
Expand Down Expand Up @@ -290,24 +286,6 @@ bool ViewNode::render_settings_footer()
return has_changed;
}

bool ViewNode::render_view2d()
{
bool has_changed = false;
ImGui::PushID((void *)this);

ImGui::Text("%s", this->p_control_node->id.c_str());

if (this->view2d_port_id != "" && this->show_view2d)
if (this->p_control_node->get_p_data(this->view2d_port_id))
{
ImGui::Image((void *)(intptr_t)this->image_texture_view2d,
ImVec2(this->shape_view2d.x, this->shape_view2d.y));
}

ImGui::PopID();
return has_changed;
}

void ViewNode::serialize_load(cereal::JSONInputArchive &)
{
LOG_ERROR("serialize (input) not defined for node [%s]",
Expand Down Expand Up @@ -340,7 +318,6 @@ void ViewNode::update_preview()
{
hmap::HeightMap *p_h = (hmap::HeightMap *)this->p_control_node
->get_p_data(this->preview_port_id);

std::vector<uint8_t> img = {};

if (this->preview_type == preview_type::grayscale)
Expand All @@ -350,24 +327,6 @@ void ViewNode::update_preview()

img_to_texture(img, this->shape_preview, this->image_texture_preview);
}

if (this->view2d_port_id != "" && this->show_view2d)
if (this->p_control_node->get_p_data(this->view2d_port_id))
{
hmap::HeightMap *p_h = (hmap::HeightMap *)this->p_control_node
->get_p_data(this->view2d_port_id);

this->shape_view2d = p_h->shape;
hmap::Array array = p_h->to_array(this->shape_view2d);

std::vector<uint8_t> img = hmap::colorize(array,
array.min(),
array.max(),
hmap::cmap::inferno,
false);

img_to_texture_rgb(img, this->shape_view2d, this->image_texture_view2d);
}
}

// HELPERS
Expand Down
77 changes: 75 additions & 2 deletions src/nodes/view_tree/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "macrologger.h"
#include <imgui_node_editor.h>

#include "hesiod/gui.hpp"
#include "hesiod/view_node.hpp"
#include "hesiod/view_tree.hpp"

Expand Down Expand Up @@ -43,16 +44,88 @@ void ViewTree::render_view_nodes()
this->render_view_node(id);
}

void ViewTree::render_view2d(std::string node_id)
bool ViewTree::render_view2d()
{
this->get_view_control_node_ref_by_id(node_id)->render_view2d();
bool has_changed = false;
ImGui::PushID((void *)this);

{
ImGui::Text("%s", this->view2d_node_id.c_str());

// // TODO listbox of output to display
// ImGui::SameLine();
// hesiod::vnode::ViewControlNode *p_vnode =
// this->get_view_control_node_ref_by_id(this->view2d_node_id);
// p_vnode->get_p_data(p_vnode->get_view2d_port_id());
}

{
if (ImGui::Button("256 x 256"))
this->shape_view2d = {256, 256};
ImGui::SameLine();
if (ImGui::Button("512 x 512"))
this->shape_view2d = {512, 512};
ImGui::SameLine();
if (ImGui::Button("1024 x 1024"))
this->shape_view2d = {1024, 1024};
}

if (hesiod::gui::listbox_map_enum(this->cmap_map, this->cmap_view2d, 128.f))
this->update_image_texture_view2d();

ImGui::SameLine();
if (ImGui::Checkbox("Hillshading", &this->hillshade_view2d))
this->update_image_texture_view2d();

if (this->image_texture_view2d != 0)
{
ImGui::Image((void *)(intptr_t)this->image_texture_view2d,
ImVec2(this->shape_view2d.x, this->shape_view2d.y));
}

ImGui::PopID();
return has_changed;
}

void ViewTree::render_settings(std::string node_id)
{
this->get_view_control_node_ref_by_id(node_id)->render_settings();
}

void ViewTree::update_image_texture_view2d()
{
if (this->view2d_node_id != "")
{
hesiod::vnode::ViewControlNode *p_vnode =
this->get_view_control_node_ref_by_id(this->view2d_node_id);

if (p_vnode->get_view2d_port_id() != "")
{
void *p_data = p_vnode->get_p_data(p_vnode->get_view2d_port_id());

if (p_data)
{
hmap::HeightMap *p_h = (hmap::HeightMap *)p_data;
hmap::Array array = p_h->to_array(this->shape_view2d);

std::vector<uint8_t> img = hmap::colorize(array,
array.min(),
array.max(),
this->cmap_view2d,
this->hillshade_view2d);

img_to_texture_rgb(img, this->shape_view2d, this->image_texture_view2d);
}
}
else
{
// 1 pixel black image
std::vector<uint8_t> img = {0, 0, 0};
img_to_texture_rgb(img, {1, 1}, this->image_texture_view2d);
}
}
}

void ViewTree::automatic_node_layout()
{
std::vector<gnode::Point> positions = this->compute_graph_layout_sugiyama();
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/view_tree/render_node_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void ViewTree::render_node_editor()
std::string node_id = this->get_node_id_by_hash_id(
this->selected_node_hid.back().Get());
this->set_view2d_node_id(node_id);
this->render_view2d(node_id);
this->render_view2d();
}
ImGui::End();
}
Expand Down
17 changes: 6 additions & 11 deletions src/nodes/view_tree/view_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,8 @@ void ViewTree::set_view2d_node_id(std::string node_id)
{
if (node_id != this->view2d_node_id)
{
// reset previous node
if (this->is_node_id_in_keys(this->view2d_node_id))
{
this->get_view_control_node_ref_by_id(this->view2d_node_id)
->set_show_view2d(false);
}

this->view2d_node_id = node_id;
this->get_view_control_node_ref_by_id(this->view2d_node_id)
->set_show_view2d(true);
this->get_view_control_node_ref_by_id(this->view2d_node_id)
->update_preview();
this->update_image_texture_view2d();
}
}

Expand Down Expand Up @@ -264,6 +254,11 @@ void ViewTree::new_link(int port_hash_id_from, int port_hash_id_to)
this->new_link(node_id_from, port_id_from, node_id_to, port_id_to);
}

void ViewTree::post_update()
{
this->update_image_texture_view2d();
}

void ViewTree::remove_link(int link_id)
{
Link *p_link = this->get_link_ref_by_id(link_id);
Expand Down

0 comments on commit 0c6a71f

Please sign in to comment.