Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature xmotion support #7

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/opengl/camera_axes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
if (BUILD_QUICKVIZ_APP)
message(STATUS "Build quickviz application")
add_executable(quickviz main.cpp
# components
quickviz_application.cpp
component/log_processor.cpp
# panels
panels/menu_bar.cpp
panels/main_docking_panel.cpp
Expand Down
20 changes: 5 additions & 15 deletions src/app/panels/console_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,22 @@
#include "panels/console_panel.hpp"

#include "imview/fonts.hpp"
#include "imview/component/logging/app_log_handler.hpp"

namespace quickviz {
ConsolePanel::ConsolePanel(std::string name) : Panel(name) {
this->SetAutoLayout(false);
// this->SetNoResize(true);
this->SetNoResize(true);
this->SetNoMove(true);
this->SetWindowNoMenuButton();

static int counter = 0;
const char* categories[3] = {"info", "warn", "error"};
const char* words[] = {"Bumfuzzled", "Cattywampus", "Snickersnee",
"Abibliophobia", "Absquatulate", "Nincompoop",
"Pauciloquent"};
for (int n = 0; n < 5; n++) {
const char* category = categories[counter % IM_ARRAYSIZE(categories)];
const char* word = words[counter % IM_ARRAYSIZE(words)];
log_.AddLog(
"[%05d] [%s] Hello, current time is %.1f, here's a word: '%s'\n",
ImGui::GetFrameCount(), category, ImGui::GetTime(), word);
counter++;
}
AppLogHandler::GetInstance().Log(LogLevel::kInfo, "app initialized");
}

void ConsolePanel::Draw() {
Begin();
log_.Draw("Example: Log");
// log_.Draw("Example: Log");
AppLogHandler::GetInstance().Draw();
End();
}
} // namespace quickviz
6 changes: 1 addition & 5 deletions src/app/panels/console_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
#define QUICKVIZ_CONSOLE_PANEL_HPP

#include "imview/panel.hpp"
#include "component/log_processor.hpp"

namespace quickviz {
class ConsolePanel : public Panel {
public:
ConsolePanel(std::string name = "Debug");
ConsolePanel(std::string name = "Console");

void Draw() override;

private:
LogProcessor log_;
};
} // namespace quickviz

Expand Down
9 changes: 7 additions & 2 deletions src/app/panels/main_docking_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace quickviz {
MainDockingPanel::MainDockingPanel(std::string name) : Panel(name) {
this->SetAutoLayout(true);
this->SetNoMove(true);
this->SetNoResize(true);
this->SetNoMove(true);
this->SetNoTitleBar(true);
this->SetNoBackground(true);

Expand All @@ -31,9 +31,14 @@ void MainDockingPanel::Draw() {

// set up layout
Begin();

// set up dockspace
dockspace_id_ = ImGui::GetID("MainDockingPanel");
ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_NoResize;
ImGui::DockSpace(dockspace_id_, ImGui::GetWindowSize(), dockspace_flags);
{
if (!layout_initialized_) {
dockspace_id_ = ImGui::DockBuilderAddNode();
dockspace_id_ = ImGui::DockBuilderAddNode(dockspace_id_, dockspace_flags);

ImGui::DockBuilderSplitNode(dockspace_id_, ImGuiDir_Left, 0.2f,
&config_panel_node_, &gl_scene_widget_node_);
Expand Down
31 changes: 26 additions & 5 deletions src/app/panels/scene_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,52 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include "imview/component/logging/app_log_handler.hpp"

namespace quickviz {
ScenePanel::ScenePanel(const std::string& panel_name) : GlWidget(panel_name) {
this->SetNoMove(true);
this->SetNoResize(true);
this->SetNoTitleBar(true);
this->SetNoBackground(true);

camera_ =
std::make_unique<Camera>(glm::vec3(0.0f, 3.0f, 8.0f), -90.0f, -25.0f);
camera_ = std::make_unique<Camera>();
camera_controller_ = std::make_unique<CameraController>(
*camera_, glm::vec3(0.0f, 6.0f, 8.0f), 0.0f, 25.0f);

auto grid = std::make_unique<Grid>(10.0f, 1.0f, glm::vec3(0.7f, 0.7f, 0.7f));
this->AddOpenGLObject("grid", std::move(grid));
}

void ScenePanel::Draw() {
ImVec2 content_size = ImGui::GetContentRegionAvail();
Begin();

// update view according to user input
ImGuiIO& io = ImGui::GetIO();
// only process mouse delta when mouse position is within the scene panel
if (ImGui::IsMousePosValid() && io.WantCaptureMouse &&
ImGui::IsWindowHovered()) {
// track mouse move delta only when the mouse left button is pressed
if (ImGui::IsMouseDown(MouseButton::kLeft)) {
camera_controller_->ProcessMouseMovement(io.MouseDelta.x,
io.MouseDelta.y);
}

// track mouse wheel scroll
camera_controller_->ProcessMouseScroll(io.MouseWheel);
}

// get view matrices from camera
ImVec2 content_size = ImGui::GetContentRegionAvail();
float aspect_ratio =
static_cast<float>(content_size.x) / static_cast<float>(content_size.y);
glm::mat4 projection = camera_->GetProjectionMatrix(aspect_ratio);
glm::mat4 view = camera_->GetViewMatrix();

UpdateView(projection, view);

GlWidget::Draw();
// finally draw the scene
DrawOpenGLObject();

End();
}
} // namespace quickviz
8 changes: 8 additions & 0 deletions src/app/panels/scene_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
#include "imview/widget/gl_widget.hpp"
#include "imview/component/opengl/grid.hpp"
#include "imview/component/opengl/camera.hpp"
#include "imview/component/opengl/camera_controller.hpp"

namespace quickviz {
class ScenePanel : public GlWidget {
enum MouseButton {
kLeft = 0,
kRight = 1,
kMiddle = 2,
};

public:
ScenePanel(const std::string& panel_name);

void Draw() override;

private:
std::unique_ptr<Camera> camera_;
std::unique_ptr<CameraController> camera_controller_;
};
} // namespace quickviz

Expand Down
4 changes: 4 additions & 0 deletions src/imview/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_library(imview
# ui components
src/window.cpp
src/viewer.cpp
src/popup.cpp
src/fonts.cpp
src/scene_object.cpp
src/panel.cpp
Expand All @@ -31,6 +32,7 @@ add_library(imview
src/widget/rt_line_plot_widget.cpp
src/widget/gl_widget.cpp
# components
src/component/popup_manager.cpp
src/component/image_utils.cpp
src/component/cairo_context.cpp
src/component/cairo_draw.cpp
Expand All @@ -45,6 +47,8 @@ add_library(imview
src/component/buffer/scrolling_plot_buffer.cpp
src/component/event/event_dispatcher.cpp
src/component/event/async_event_dispatcher.cpp
src/component/logging/log_processor.cpp
src/component/logging/app_log_handler.cpp
)
target_link_libraries(imview PUBLIC imcore
PkgConfig::Cairo PkgConfig::Fontconfig
Expand Down
78 changes: 78 additions & 0 deletions src/imview/include/imview/component/logging/app_log_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* @file app_log_handler.hpp
* @date 12/17/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef APP_LOG_HANDLER_HPP
#define APP_LOG_HANDLER_HPP

#include "imview/component/logging/log_processor.hpp"

#include <chrono>
#include <sstream>
#include <iomanip>
#include <unordered_map>

namespace quickviz {
enum class LogLevel { kDebug, kInfo, kWarn, kError, kFatal };

class AppLogHandler {
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;

AppLogHandler() { t0_ = Clock::now(); }
~AppLogHandler() = default;

public:
static AppLogHandler& GetInstance() {
static AppLogHandler instance;
return instance;
}

// void Log(LogLevel level, const char* fmt, ...);
template <typename... Args>
void Log(LogLevel level, const char* fmt, Args&&... args) {
// generate timestamp
auto duration = Clock::now() - t0_;
auto seconds =
std::chrono::duration_cast<std::chrono::seconds>(duration).count();
auto microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(duration)
.count() %
1'000'000;
std::ostringstream timestamp;
timestamp << std::setfill('0') << std::setw(10) << seconds
<< "." // Fixed-width seconds
<< std::setfill('0') << std::setw(6)
<< microseconds; // Microseconds

// forward fixed arguments and variable arguments to AddLog
processor_.AddLog("[%s][%s] ", timestamp.str().c_str(),
level_str_map_[level]);
if constexpr (sizeof...(args) > 0) {
processor_.AddLog(fmt, std::forward<Args>(args)...);
} else {
processor_.AddLog("%s", fmt); // Treat fmt as a regular string
}
processor_.AddLog("\n");
}

void Draw();

private:
LogProcessor processor_;
TimePoint t0_;

std::unordered_map<LogLevel, const char*> level_str_map_ = {
{LogLevel::kDebug, "DEBUG"},
{LogLevel::kInfo, "INFO "},
{LogLevel::kWarn, "WARN "},
{LogLevel::kError, "ERROR"},
{LogLevel::kFatal, "FATAL"}};
};
} // namespace quickviz

#endif // APP_LOG_HANDLER_HPP
1 change: 1 addition & 0 deletions src/imview/include/imview/component/opengl/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Camera {
enum class Movement { kForward, kBackward, kLeft, kRight, kUp, kDown };

public:
explicit Camera(float fov = default_fov);
Camera(glm::vec3 position, float yaw, float pitch, float fov = default_fov);

// public methods
Expand Down
10 changes: 7 additions & 3 deletions src/imview/include/imview/component/opengl/camera_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class CameraController {
using CameraMovement = Camera::Movement;

public:
CameraController(Camera& camera);
CameraController(Camera& camera, glm::vec3 position = {0, 0, 0},
float yaw = 0, float pitch = 0);

void Reset();
void SetMode(Mode mode);
Expand All @@ -27,13 +28,16 @@ class CameraController {
void ProcessMouseScroll(float y_offset);

private:
static constexpr float initial_orbit_distance = 10.0f;
static constexpr float initial_top_down_height = 10.0f;

void UpdateOrbitPosition();

Camera& camera_;
Mode mode_ = Mode::kOrbit;
glm::vec3 orbit_target_ = glm::vec3(0.0f, 0.0f, 0.0f);
float orbit_distance_ = 10.0f;
float top_down_height_ = 10.0f;
float orbit_distance_ = initial_orbit_distance;
float top_down_height_ = initial_top_down_height;
};
} // namespace quickviz

Expand Down
57 changes: 57 additions & 0 deletions src/imview/include/imview/component/popup_manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @file popup_manager.hpp
* @date 12/18/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef POPUP_MANAGER_HPP
#define POPUP_MANAGER_HPP

#include <mutex>

#include "imview/popup.hpp"

namespace quickviz {
class PopupManager {
struct PopupDescriptor {
PopupType type;
std::string title;
std::string msg;
float width;
float height;
OnConfirmationCallback callback;
};

struct PopupState {
bool triggered;
PopupDescriptor descriptor;
};

public:
static PopupManager& GetInstance() {
static PopupManager instance;
return instance;
}

void RegisterNotificationPopup(std::string title, std::string msg,
float width = 300, float height = 150);
void RegisterConfirmationPopup(std::string title, std::string msg,
OnConfirmationCallback callback = nullptr,
float width = 300, float height = 150);

void TriggerPopup(std::string title);
void CancelPopup(std::string title);
void UpdatePopups();

private:
PopupManager() = default;
~PopupManager() = default;

std::mutex popup_mtx_;
std::unordered_map<std::string, PopupState> popups_;
};
} // namespace quickviz

#endif // POPUP_MANAGER_HPP
Loading