select highlighting

main
Brett 2024-05-14 21:58:05 -04:00
parent 664f553711
commit 7c73318b2a
6 changed files with 82 additions and 38 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(graphs VERSION 0.0.41) project(graphs VERSION 0.0.42)
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

34
include/color_constants.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef GRAPHS_COLOR_CONSTANTS_H
#define GRAPHS_COLOR_CONSTANTS_H
#include <blt/math/vectors.h>
namespace color
{
static inline constexpr blt::color4 POINT_OUTLINE_COLOR = blt::make_color(0, 0.6, 0.6);
static inline constexpr blt::color4 POINT_HIGHLIGHT_COLOR = blt::make_color(0, 1.0, 1.0);
static inline constexpr blt::color4 POINT_SELECT_COLOR = blt::make_color(0, 0.9, 0.7);
static inline constexpr blt::color4 EDGE_COLOR = blt::make_color(0, 0.8, 0);
static inline constexpr blt::color4 EDGE_OUTLINE_COLOR = blt::make_color(0, 1, 0);
}
#endif //GRAPHS_COLOR_CONSTANTS_H

View File

@ -22,6 +22,7 @@
#include <graph_base.h> #include <graph_base.h>
#include <force_algorithms.h> #include <force_algorithms.h>
#include <blt/gfx/window.h> #include <blt/gfx/window.h>
#include <blt/math/interpolation.h>
namespace im = ImGui; namespace im = ImGui;
@ -55,7 +56,8 @@ class graph_t
std::unique_ptr<force_equation> equation; std::unique_ptr<force_equation> equation;
static constexpr float POINT_SIZE = 35; static constexpr float POINT_SIZE = 35;
blt::i32 current_node = -1; blt::i32 selected_node = -1;
blt::quint_easing easing;
void create_random_graph(bounding_box bb, blt::size_t min_nodes, blt::size_t max_nodes, blt::f64 connectivity, void create_random_graph(bounding_box bb, blt::size_t min_nodes, blt::size_t max_nodes, blt::f64 connectivity,
blt::f64 scaling_connectivity, blt::f64 distance_factor); blt::f64 scaling_connectivity, blt::f64 distance_factor);
@ -93,15 +95,19 @@ class graph_t
return edges.contains({e1, e2}); return edges.contains({e1, e2});
} }
void render(double frame_time); void render();
void reset_mouse_drag() void reset_mouse_drag()
{ {
current_node = -1; easing.reset();
nodes[selected_node].setOutlineColor(color::POINT_OUTLINE_COLOR);
selected_node = -1;
} }
void process_mouse_drag(blt::i32 width, blt::i32 height); void process_mouse_drag(blt::i32 width, blt::i32 height);
void handle_mouse();
void use_Eades() void use_Eades()
{ {
equation = std::make_unique<Eades_equation>(); equation = std::make_unique<Eades_equation>();
@ -173,7 +179,7 @@ class engine_t
private: private:
graph_t graph; graph_t graph;
void draw_gui(const blt::gfx::window_data& data, double ft); void draw_gui(const blt::gfx::window_data& data);
public: public:
void init(const blt::gfx::window_data& data) void init(const blt::gfx::window_data& data)
@ -181,19 +187,21 @@ class engine_t
graph.make_new({0, 0, data.width, data.height}, 5, 25, 0.2); graph.make_new({0, 0, data.width, data.height}, 5, 25, 0.2);
} }
void render(const blt::gfx::window_data& data, const double ft) void render(const blt::gfx::window_data& data)
{ {
draw_gui(data, ft); draw_gui(data);
auto& io = ImGui::GetIO(); auto& io = ImGui::GetIO();
if (!io.WantCaptureMouse && blt::gfx::isMousePressed(0)) if (!io.WantCaptureMouse){
if (blt::gfx::isMousePressed(0))
graph.process_mouse_drag(data.width, data.height); graph.process_mouse_drag(data.width, data.height);
else else
graph.reset_mouse_drag(); graph.reset_mouse_drag();
}
graph.render(ft); graph.render();
} }
}; };

View File

@ -22,6 +22,8 @@
#include <blt/gfx/renderer/batch_2d_renderer.h> #include <blt/gfx/renderer/batch_2d_renderer.h>
#include <blt/std/types.h> #include <blt/std/types.h>
#include <blt/std/assert.h> #include <blt/std/assert.h>
#include <color_constants.h>
class node class node
{ {
@ -29,7 +31,7 @@ class node
blt::gfx::point2d_t point; blt::gfx::point2d_t point;
float outline_scale = 1.25f; float outline_scale = 1.25f;
blt::vec2 velocity; blt::vec2 velocity;
blt::color4 outline_color = blt::make_color(0.0, 1.0, 1.0); blt::color4 outline_color = color::POINT_OUTLINE_COLOR;
public: public:
explicit node(const blt::gfx::point2d_t& point): point(point) explicit node(const blt::gfx::point2d_t& point): point(point)
{} {}
@ -82,8 +84,8 @@ class edge
blt::u64 i1, i2; blt::u64 i1, i2;
float outline_scale = 2.0f; float outline_scale = 2.0f;
float thickness = 2.0f; float thickness = 2.0f;
blt::color4 color = blt::make_color(0, 1, 0); blt::color4 color = color::EDGE_COLOR;
blt::color4 outline_color = blt::make_color(1, 0, 0); blt::color4 outline_color = color::EDGE_OUTLINE_COLOR;
public: public:
edge(blt::u64 i1, blt::u64 i2): i1(i1), i2(i2) edge(blt::u64 i1, blt::u64 i2): i1(i1), i2(i2)
{ {

View File

@ -19,14 +19,17 @@
#include <random> #include <random>
#include <blt/gfx/raycast.h> #include <blt/gfx/raycast.h>
#include <blt/std/ranges.h> #include <blt/std/ranges.h>
#include <blt/math/interpolation.h>
#include <color_constants.h>
extern blt::gfx::batch_renderer_2d renderer_2d; extern blt::gfx::batch_renderer_2d renderer_2d;
extern blt::gfx::matrix_state_manager global_matrices; extern blt::gfx::matrix_state_manager global_matrices;
int sub_ticks = 1; int sub_ticks = 1;
void graph_t::render(const double frame_time) void graph_t::render()
{ {
const double frame_time = blt::gfx::getFrameDeltaSeconds();
if (sim && (current_iterations < max_iterations || run_infinitely) && max_force_last > threshold) if (sim && (current_iterations < max_iterations || run_infinitely) && max_force_last > threshold)
{ {
for (int _ = 0; _ < sub_ticks; _++) for (int _ = 0; _ < sub_ticks; _++)
@ -81,7 +84,7 @@ void graph_t::process_mouse_drag(const blt::i32 width, const blt::i32 height)
const auto mouse_pos = blt::make_vec2(blt::gfx::calculateRay2D(width, height, global_matrices.getScale2D(), global_matrices.getView2D(), const auto mouse_pos = blt::make_vec2(blt::gfx::calculateRay2D(width, height, global_matrices.getScale2D(), global_matrices.getView2D(),
global_matrices.getOrtho())); global_matrices.getOrtho()));
if (current_node < 0) if (selected_node < 0)
{ {
for (const auto& [index, node] : blt::enumerate(nodes)) for (const auto& [index, node] : blt::enumerate(nodes))
{ {
@ -90,12 +93,25 @@ void graph_t::process_mouse_drag(const blt::i32 width, const blt::i32 height)
if (const auto mag = dist.magnitude(); mag < POINT_SIZE) if (const auto mag = dist.magnitude(); mag < POINT_SIZE)
{ {
current_node = static_cast<blt::i32>(index); selected_node = static_cast<blt::i32>(index);
break; break;
} }
} }
} else } else
nodes[current_node].getPositionRef() = mouse_pos; {
auto& node = nodes[selected_node];
easing.progress(5 * static_cast<float>(blt::gfx::getFrameDeltaSeconds()));
node.setOutlineColor(easing.apply(color::POINT_OUTLINE_COLOR, color::POINT_SELECT_COLOR));
node.getPositionRef() = mouse_pos;
}
}
void graph_t::handle_mouse()
{
for (const auto& node : nodes)
{
}
} }
void graph_t::create_random_graph(bounding_box bb, const blt::size_t min_nodes, const blt::size_t max_nodes, const blt::f64 connectivity, void graph_t::create_random_graph(bounding_box bb, const blt::size_t min_nodes, const blt::size_t max_nodes, const blt::f64 connectivity,
@ -182,8 +198,9 @@ void graph_t::create_random_graph(bounding_box bb, const blt::size_t min_nodes,
} }
} }
void engine_t::draw_gui(const blt::gfx::window_data& data, const double ft) void engine_t::draw_gui(const blt::gfx::window_data& data)
{ {
double ft = blt::gfx::getFrameDeltaSeconds();
if (im::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) if (im::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
{ {
static int min_nodes = 5; static int min_nodes = 5;

View File

@ -19,25 +19,15 @@
#include "blt/gfx/renderer/resource_manager.h" #include "blt/gfx/renderer/resource_manager.h"
#include "blt/gfx/renderer/batch_2d_renderer.h" #include "blt/gfx/renderer/batch_2d_renderer.h"
#include "blt/gfx/renderer/camera.h" #include "blt/gfx/renderer/camera.h"
#include <blt/gfx/framebuffer.h>
#include <blt/gfx/raycast.h> #include <blt/gfx/raycast.h>
#include <imgui.h>
#include <memory>
#include <random>
#include <blt/std/ranges.h>
#include <blt/std/time.h> #include <blt/std/time.h>
#include <blt/math/log_util.h> #include <blt/math/log_util.h>
#include <blt/gfx/input.h>
#include <blt/parse/templating.h>
#include <graph.h> #include <graph.h>
#include <blt/gfx/renderer/shaders/pp_screen.frag>
blt::gfx::matrix_state_manager global_matrices; blt::gfx::matrix_state_manager global_matrices;
blt::gfx::resource_manager resources; blt::gfx::resource_manager resources;
blt::gfx::batch_renderer_2d renderer_2d(resources, global_matrices); blt::gfx::batch_renderer_2d renderer_2d(resources, global_matrices);
blt::gfx::first_person_camera_2d camera; blt::gfx::first_person_camera_2d camera;
blt::u64 lastTime;
double ft = 0;
namespace im = ImGui; namespace im = ImGui;
@ -58,8 +48,6 @@ void init(const blt::gfx::window_data& data)
renderer_2d.create(); renderer_2d.create();
engine.init(data); engine.init(data);
lastTime = blt::system::nanoTime();
} }
void update(const blt::gfx::window_data& data) void update(const blt::gfx::window_data& data)
@ -68,18 +56,13 @@ void update(const blt::gfx::window_data& data)
//im::ShowDemoWindow(); //im::ShowDemoWindow();
engine.render(data, ft); engine.render(data);
camera.update(); camera.update();
camera.update_view(global_matrices); camera.update_view(global_matrices);
global_matrices.update(); global_matrices.update();
renderer_2d.render(data.width, data.height); renderer_2d.render(data.width, data.height);
const auto currentTime = blt::system::nanoTime();
const auto diff = currentTime - lastTime;
lastTime = currentTime;
ft = static_cast<double>(diff) / 1000000000.0;
} }
int main(int, const char**) int main(int, const char**)