Files
ViewDesignEngine/src/render/viewport_layout.cpp
T
hm e688c93f80 refactor(batch3): extract render module headers to vde_render/
Moved render_types.h, section_view.h, viewport_layout.h from include/vde/render/ to include/vde_render/

Moved appearance.h from include/vde/brep/ to include/vde_render/ (was orphan file)

Updated all include paths and cross-references
2026-08-06 16:32:36 +08:00

207 lines
7.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file viewport_layout.cpp
* @brief Multi-Viewport Layout API 实现(VDE-211)
*
* 提供视口布局管理、相机状态、像素区域计算。
*/
#include "vde_render/viewport_layout.h"
#include <algorithm>
#include <cmath>
#include <map>
#include <mutex>
#include <stdexcept>
namespace vde::render {
// ════════════════════════════════════════════════════════════
// 内部状态(static lifetime)
// ════════════════════════════════════════════════════════════
namespace {
/// 全局视口布局状态
struct GlobalViewportState {
ViewportLayoutType layout_type = ViewportLayoutType::Single;
double split_ratio = 0.5;
int active_viewport = 0;
int total_width = 1920;
int total_height = 1080;
DividerProperties divider;
std::map<int, CameraState> cameras;
std::vector<std::vector<int>> custom_bounds;
};
static GlobalViewportState g_vp;
static std::mutex g_vp_mutex;
/** 根据布局类型计算视口数量 */
int viewport_count_for_type(ViewportLayoutType type) {
switch (type) {
case ViewportLayoutType::Single: return 1;
case ViewportLayoutType::HorizontalSplit: return 2;
case ViewportLayoutType::VerticalSplit: return 2;
case ViewportLayoutType::Quad: return 4;
case ViewportLayoutType::ThreeLeft: return 3;
case ViewportLayoutType::ThreeRight: return 3;
case ViewportLayoutType::Custom: return static_cast<int>(g_vp.custom_bounds.size());
}
return 1;
}
/** 根据布局类型计算视口 {x, y, w, h} */
std::vector<int> compute_bounds(ViewportLayoutType type, int index,
double ratio, int tw, int th)
{
int x = 0, y = 0, w = tw, h = th;
switch (type) {
case ViewportLayoutType::Single:
// full area
break;
case ViewportLayoutType::HorizontalSplit: {
// top / bottom
int split_y = static_cast<int>(th * ratio);
if (index == 0) { y = 0; h = split_y; }
else { y = split_y; h = th - split_y; }
break;
}
case ViewportLayoutType::VerticalSplit: {
// left / right
int split_x = static_cast<int>(tw * ratio);
if (index == 0) { x = 0; w = split_x; }
else { x = split_x; w = tw - split_x; }
break;
}
case ViewportLayoutType::Quad: {
int split_x = static_cast<int>(tw * ratio);
int split_y = static_cast<int>(th * ratio);
switch (index) {
case 0: x = 0; y = 0; w = split_x; h = split_y; break;
case 1: x = split_x; y = 0; w = tw - split_x; h = split_y; break;
case 2: x = 0; y = split_y; w = split_x; h = th - split_y; break;
case 3: x = split_x; y = split_y; w = tw - split_x; h = th - split_y; break;
}
break;
}
case ViewportLayoutType::ThreeLeft: {
int split_x = static_cast<int>(tw * (1.0 - ratio));
int split_y = static_cast<int>(th * 0.5);
switch (index) {
case 0: x = 0; y = 0; w = split_x; h = th; break;
case 1: x = split_x; y = 0; w = tw - split_x; h = split_y; break;
case 2: x = split_x; y = split_y; w = tw - split_x; h = th - split_y; break;
}
break;
}
case ViewportLayoutType::ThreeRight: {
int split_x = static_cast<int>(tw * ratio);
int split_y = static_cast<int>(th * 0.5);
switch (index) {
case 0: x = 0; y = 0; w = split_x; h = split_y; break;
case 1: x = 0; y = split_y; w = split_x; h = th - split_y; break;
case 2: x = split_x; y = 0; w = tw - split_x; h = th; break;
}
break;
}
case ViewportLayoutType::Custom: {
if (index >= 0 && static_cast<size_t>(index) < g_vp.custom_bounds.size()) {
const auto& b = g_vp.custom_bounds[index];
if (b.size() >= 4) {
x = b[0]; y = b[1]; w = b[2]; h = b[3];
}
}
break;
}
}
return {x, y, w, h};
}
} // anonymous namespace
// ════════════════════════════════════════════════════════════
// API 实现
// ════════════════════════════════════════════════════════════
void set_viewport_layout(ViewportLayoutType type, double split_ratio) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
g_vp.layout_type = type;
g_vp.split_ratio = std::clamp(split_ratio, 0.1, 0.9);
// clamp active viewport if out of range
int count = viewport_count_for_type(type);
if (g_vp.active_viewport >= count) {
g_vp.active_viewport = 0;
}
}
LayoutState get_viewport_layout() {
std::lock_guard<std::mutex> lock(g_vp_mutex);
return LayoutState{g_vp.layout_type, g_vp.split_ratio};
}
CameraState get_sub_viewport_camera(int viewport_index) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
auto it = g_vp.cameras.find(viewport_index);
if (it != g_vp.cameras.end()) return it->second;
return CameraState{}; // default
}
void set_sub_viewport_camera(int viewport_index,
const CameraState& camera) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
g_vp.cameras[viewport_index] = camera;
}
void set_active_viewport(int index) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
int count = viewport_count_for_type(g_vp.layout_type);
if (index >= 0 && index < count) {
g_vp.active_viewport = index;
}
}
int get_active_viewport() {
std::lock_guard<std::mutex> lock(g_vp_mutex);
return g_vp.active_viewport;
}
std::vector<int> viewport_bounds(int index) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
int count = viewport_count_for_type(g_vp.layout_type);
if (index < 0 || index >= count) return {0, 0, 0, 0};
return compute_bounds(g_vp.layout_type, index,
g_vp.split_ratio,
g_vp.total_width, g_vp.total_height);
}
DividerProperties divider_properties() {
std::lock_guard<std::mutex> lock(g_vp_mutex);
return g_vp.divider;
}
void set_divider_properties(const DividerProperties& props) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
g_vp.divider = props;
}
void set_custom_viewport_bounds(
const std::vector<std::vector<int>>& bounds_vec) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
g_vp.custom_bounds = bounds_vec;
}
int viewport_count() {
std::lock_guard<std::mutex> lock(g_vp_mutex);
return viewport_count_for_type(g_vp.layout_type);
}
void set_viewport_total_size(int total_width, int total_height) {
std::lock_guard<std::mutex> lock(g_vp_mutex);
if (total_width > 0) g_vp.total_width = total_width;
if (total_height > 0) g_vp.total_height = total_height;
}
} // namespace vde::render