优化布局语法,消除部分警告
This commit is contained in:
parent
2c794509bc
commit
c322a4b6dd
@ -13,6 +13,13 @@ function(set_cpp_standard standard)
|
|||||||
# 可选:禁用编译器特定的扩展,使用纯粹的标准
|
# 可选:禁用编译器特定的扩展,使用纯粹的标准
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF PARENT_SCOPE)
|
set(CMAKE_CXX_EXTENSIONS OFF PARENT_SCOPE)
|
||||||
|
|
||||||
|
if(WIN32)
|
||||||
|
# 定义Windows版本宏
|
||||||
|
add_definitions(-DUNICODE -D_UNICODE)
|
||||||
|
# 可选:添加 WIN32_LEAN_AND_MEAN 以减少 Windows 头文件包含
|
||||||
|
# add_definitions(-DWIN32_LEAN_AND_MEAN)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(MSVC)
|
if(MSVC)
|
||||||
# 设置utf-8编码
|
# 设置utf-8编码
|
||||||
add_compile_options(/utf-8)
|
add_compile_options(/utf-8)
|
||||||
@ -20,13 +27,8 @@ function(set_cpp_standard standard)
|
|||||||
add_compile_options(/Zc:__cplusplus)
|
add_compile_options(/Zc:__cplusplus)
|
||||||
# 可选:增加 MSVC 警告级别
|
# 可选:增加 MSVC 警告级别
|
||||||
add_compile_options(/W4)
|
add_compile_options(/W4)
|
||||||
endif()
|
# **禁用未使用的形参警告 (C4100)**
|
||||||
|
add_compile_options(/wd4100)
|
||||||
if(WIN32)
|
|
||||||
# 定义Windows版本宏
|
|
||||||
add_definitions(-DUNICODE -D_UNICODE)
|
|
||||||
# 可选:添加 WIN32_LEAN_AND_MEAN 以减少 Windows 头文件包含
|
|
||||||
# add_definitions(-DWIN32_LEAN_AND_MEAN)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# GCC/Clang 特定选项
|
# GCC/Clang 特定选项
|
||||||
|
@ -68,31 +68,28 @@ int main(int argc, char* argv[]) {
|
|||||||
const auto& text_block2 = std::make_shared<mtext_block>();
|
const auto& text_block2 = std::make_shared<mtext_block>();
|
||||||
text_block2->set_text(U"Hello, World!");
|
text_block2->set_text(U"Hello, World!");
|
||||||
|
|
||||||
auto button = mnew(mbutton)
|
|
||||||
[
|
|
||||||
mslot(mbutton)
|
|
||||||
.margin({ 10 })
|
|
||||||
.visibility(visibility_t::visible)(text_block)
|
|
||||||
];
|
|
||||||
|
|
||||||
auto button2 = mnew(mbutton)
|
|
||||||
[
|
|
||||||
mslot(mbutton)
|
|
||||||
.margin({ 10 })
|
|
||||||
.visibility(visibility_t::visible)(text_block2)
|
|
||||||
];
|
|
||||||
|
|
||||||
const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
|
const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
|
||||||
window->set_content(
|
window->set_content(
|
||||||
mnew(mv_box)
|
mnew(mv_box)
|
||||||
[
|
[
|
||||||
mslot(mv_box)
|
mslot(mv_box)
|
||||||
.horizontal_alignment(horizontal_alignment_t::left)
|
.horizontal_alignment(horizontal_alignment_t::left)
|
||||||
(button),
|
+mnew(mbutton)
|
||||||
|
[
|
||||||
|
mslot(mbutton)
|
||||||
|
.margin({ 10 })
|
||||||
|
.visibility(visibility_t::visible)(text_block)
|
||||||
|
],
|
||||||
|
|
||||||
mslot(mv_box)
|
mslot(mv_box)
|
||||||
.horizontal_alignment(horizontal_alignment_t::right)
|
.horizontal_alignment(horizontal_alignment_t::right)
|
||||||
(button2)
|
+mnew(mbutton)
|
||||||
|
[
|
||||||
|
mslot(mbutton)
|
||||||
|
.margin({ 10 })
|
||||||
|
.visibility(visibility_t::visible)
|
||||||
|
(text_block2)
|
||||||
|
]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -117,8 +117,8 @@ std::optional<linear_color> parse_rgb_rgba_color(const std::string& in_str) {
|
|||||||
// 转换为小写以便检查 "rgb(" 和 "rgba("
|
// 转换为小写以便检查 "rgb(" 和 "rgba("
|
||||||
std::ranges::transform(lower_str, lower_str.begin(), ::tolower);
|
std::ranges::transform(lower_str, lower_str.begin(), ::tolower);
|
||||||
|
|
||||||
const size_t open_paren = lower_str.find('(');
|
const auto open_paren = lower_str.find('(');
|
||||||
const size_t close_paren = lower_str.find(')');
|
const auto close_paren = lower_str.find(')');
|
||||||
|
|
||||||
if (open_paren == std::string::npos || close_paren == std::string::npos || close_paren <= open_paren) {
|
if (open_paren == std::string::npos || close_paren == std::string::npos || close_paren <= open_paren) {
|
||||||
return std::nullopt; // 括号不匹配或顺序错误
|
return std::nullopt; // 括号不匹配或顺序错误
|
||||||
@ -164,9 +164,9 @@ std::optional<linear_color> parse_rgb_rgba_color(const std::string& in_str) {
|
|||||||
uses_float = true;
|
uses_float = true;
|
||||||
} else {
|
} else {
|
||||||
// **尝试解析为整数**
|
// **尝试解析为整数**
|
||||||
const auto val_i = std::stoi(comp_str);
|
const auto val_i = std::stoi(comp_str);
|
||||||
components[index] = val_i;
|
components[index] = static_cast<float>(val_i);
|
||||||
uses_int = true;
|
uses_int = true;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
} catch (const std::invalid_argument&) {
|
} catch (const std::invalid_argument&) {
|
||||||
|
@ -93,12 +93,12 @@ font_v_metrics_t stb_font_face_t::get_metrics() const {
|
|||||||
// 如果失败,使用默认的VMetrics
|
// 如果失败,使用默认的VMetrics
|
||||||
stbtt_GetFontVMetrics(&font_info_, &ascent, &descent, &line_gap);
|
stbtt_GetFontVMetrics(&font_info_, &ascent, &descent, &line_gap);
|
||||||
}
|
}
|
||||||
line_gap = line_gap * scale_ + 0.5f;
|
line_gap = static_cast<int32_t>(line_gap * scale_ + 0.5f);
|
||||||
|
|
||||||
font_v_metrics_t metrics{};
|
font_v_metrics_t metrics{};
|
||||||
metrics.ascent = static_cast<int>(ascent * scale_ + 0.5f);
|
metrics.ascent = ascent * scale_ + 0.5f;
|
||||||
metrics.descent = static_cast<int>(descent * scale_ - 0.5f);
|
metrics.descent = descent * scale_ - 0.5f;
|
||||||
metrics.line_height = static_cast<int>(metrics.ascent - metrics.descent + line_gap);
|
metrics.line_height = metrics.ascent - metrics.descent + line_gap;
|
||||||
return metrics;
|
return metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ std::shared_ptr<image_heap_t> stb_font_face_t::get_glyph_image(int32_t in_glyph_
|
|||||||
int32_t width, height, xoff, yoff;
|
int32_t width, height, xoff, yoff;
|
||||||
|
|
||||||
// 获取字形图像
|
// 获取字形图像
|
||||||
const auto bitmap = stbtt_GetGlyphBitmapSubpixel(&font_info_, scale_, scale_, 0.33, 0, in_glyph_id, &width, &height, &xoff, &yoff);
|
const auto bitmap = stbtt_GetGlyphBitmapSubpixel(&font_info_, scale_, scale_, 0.33f, 0, in_glyph_id, &width, &height, &xoff, &yoff);
|
||||||
|
|
||||||
// 创建位图
|
// 创建位图
|
||||||
std::shared_ptr<image_heap_t> image(new image_heap_t(), stb_truetype_deleter);
|
std::shared_ptr<image_heap_t> image(new image_heap_t(), stb_truetype_deleter);
|
||||||
@ -139,20 +139,20 @@ glyph_shaped_t stb_font_face_t::shape_glyph(uint32_t in_glyph_id) const {
|
|||||||
stbtt_GetGlyphHMetrics(&font_info_, in_glyph_id, &advance, &lsb);
|
stbtt_GetGlyphHMetrics(&font_info_, in_glyph_id, &advance, &lsb);
|
||||||
// 获取字形的边界框
|
// 获取字形的边界框
|
||||||
int32_t x0, y0, x1, y1;
|
int32_t x0, y0, x1, y1;
|
||||||
stbtt_GetGlyphBitmapBoxSubpixel(&font_info_, in_glyph_id, scale_, scale_, 0.33, 0, &x0, &y0, &x1, &y1);
|
stbtt_GetGlyphBitmapBoxSubpixel(&font_info_, in_glyph_id, scale_, scale_, 0.33f, 0, &x0, &y0, &x1, &y1);
|
||||||
|
|
||||||
glyph_shaped_t out{};
|
glyph_shaped_t out{};
|
||||||
out.glyph_index = in_glyph_id;
|
out.glyph_index = in_glyph_id;
|
||||||
out.offset.x() = (float)lsb * scale_;
|
out.offset.x() = (float)lsb * scale_;
|
||||||
out.offset.y() = y0;
|
out.offset.y() = (float)y0;
|
||||||
out.advance.x() = (float)advance * scale_;
|
out.advance.x() = (float)advance * scale_;
|
||||||
out.advance.y() = 0.0f;
|
out.advance.y() = 0.0f;
|
||||||
out.rect.set_position({ x0, y0 });
|
out.rect.set_position({ x0, y0 });
|
||||||
out.rect.set_size({ x1 - x0, y1 - y0 });
|
out.rect.set_size({ x1 - x0, y1 - y0 });
|
||||||
out.hori_bearing.x() = (float)lsb * scale_;
|
out.hori_bearing.x() = (float)lsb * scale_;
|
||||||
out.hori_bearing.y() = y0;
|
out.hori_bearing.y() = (float)y0;
|
||||||
out.vert_bearing.x() = 0.0f;
|
out.vert_bearing.x() = 0.0f;
|
||||||
out.vert_bearing.y() = y0;
|
out.vert_bearing.y() = (float)y0;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
@ -182,12 +182,12 @@ text_layout_t font_manager::layout_text(
|
|||||||
current_line.height = std::max(current_line.height, current_metrics.line_height);
|
current_line.height = std::max(current_line.height, current_metrics.line_height);
|
||||||
|
|
||||||
// 计算基线位置 - 基于当前行的上升距离
|
// 计算基线位置 - 基于当前行的上升距离
|
||||||
const float baseline = cursor_y + current_line.ascent;
|
const auto baseline = cursor_y + current_line.ascent;
|
||||||
const int32_t size_y_diff = glyph_metrics.rect.size().y() - region->rect.size().y(); // 计算Y轴差异(字形位图高度与区域高度)
|
const auto size_y_diff = glyph_metrics.rect.size().y() - region->rect.size().y(); // 计算Y轴差异(字形位图高度与区域高度)
|
||||||
|
|
||||||
// 计算字形绘制坐标
|
// 计算字形绘制坐标
|
||||||
float x = cursor_x + glyph_metrics.offset.x();
|
auto x = cursor_x + glyph_metrics.offset.x();
|
||||||
float y = baseline + glyph_metrics.offset.y() + size_y_diff;
|
auto y = baseline + glyph_metrics.offset.y() + size_y_diff;
|
||||||
|
|
||||||
// 添加字形位置信息到布局
|
// 添加字形位置信息到布局
|
||||||
auto& glyph_position = layout.glyphs.emplace_back();
|
auto& glyph_position = layout.glyphs.emplace_back();
|
||||||
|
@ -110,16 +110,16 @@ void render_elements::update_projection_matrix(const Eigen::Vector2i& in_size) {
|
|||||||
// 确保批次兼容性
|
// 确保批次兼容性
|
||||||
void render_elements::ensure_batch_compatibility(const batch_key& key) {
|
void render_elements::ensure_batch_compatibility(const batch_key& key) {
|
||||||
// 如果当前没有批次或者渲染状态改变了,创建新批次
|
// 如果当前没有批次或者渲染状态改变了,创建新批次
|
||||||
if (current_batch_index_ < 0 || current_key_ != key) {
|
if (current_batch_index_ == 0 || current_key_ != key) {
|
||||||
current_key_ = key;
|
current_key_ = key;
|
||||||
|
|
||||||
draw_batch new_batch;
|
draw_batch new_batch;
|
||||||
new_batch.key = key;
|
new_batch.key = key;
|
||||||
new_batch.vertex_start = vertices_.size();
|
new_batch.vertex_start = static_cast<uint32_t>(vertices_.size());
|
||||||
new_batch.index_start = indices_.size();
|
new_batch.index_start = static_cast<uint32_t>(indices_.size());
|
||||||
|
|
||||||
batches_.push_back(new_batch);
|
batches_.push_back(new_batch);
|
||||||
current_batch_index_ = batches_.size() - 1;
|
current_batch_index_ = static_cast<int32_t>(batches_.size() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ void render_elements::add_rect_to_batch(
|
|||||||
compute_rect_vertices(in_pos, in_size, positions, in_rotation_radians, in_scale, in_pivot);
|
compute_rect_vertices(in_pos, in_size, positions, in_rotation_radians, in_scale, in_pivot);
|
||||||
|
|
||||||
// 记录起始顶点索引
|
// 记录起始顶点索引
|
||||||
const auto base_index = vertices_.size();
|
const uint32_t base_index = static_cast<uint32_t>(vertices_.size());
|
||||||
|
|
||||||
// 添加顶点
|
// 添加顶点
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
@ -170,8 +170,8 @@ void render_elements::add_rect_to_batch(
|
|||||||
|
|
||||||
// 更新当前批次的计数
|
// 更新当前批次的计数
|
||||||
draw_batch& batch = batches_[current_batch_index_];
|
draw_batch& batch = batches_[current_batch_index_];
|
||||||
batch.vertex_count = vertices_.size() - batch.vertex_start;
|
batch.vertex_count = static_cast<uint32_t>(vertices_.size() - batch.vertex_start);
|
||||||
batch.index_count = indices_.size() - batch.index_start;
|
batch.index_count = static_cast<uint32_t>(indices_.size() - batch.index_start);
|
||||||
|
|
||||||
// 更新统计信息
|
// 更新统计信息
|
||||||
total_triangles_ += 2;
|
total_triangles_ += 2;
|
||||||
@ -396,7 +396,7 @@ void render_elements::flush_batches() {
|
|||||||
if (vertices_.empty() || indices_.empty() || batches_.empty()) { return; }
|
if (vertices_.empty() || indices_.empty() || batches_.empty()) { return; }
|
||||||
|
|
||||||
// 确保缓冲区足够大
|
// 确保缓冲区足够大
|
||||||
ensure_buffer_capacity(vertices_.size(), indices_.size());
|
ensure_buffer_capacity((uint32_t)vertices_.size(), (uint32_t)indices_.size());
|
||||||
|
|
||||||
// 上传顶点和索引数据
|
// 上传顶点和索引数据
|
||||||
sg_update_buffer(vertex_buffer_, sg_range{ vertices_.data(), vertices_.size() * sizeof(mirage_vertex_t) });
|
sg_update_buffer(vertex_buffer_, sg_range{ vertices_.data(), vertices_.size() * sizeof(mirage_vertex_t) });
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "widget/panel_widget/mbox.h"
|
#include "widget/panel_widget/mbox.h"
|
||||||
|
|
||||||
mbutton::mbutton() {
|
mbutton::mbutton() {
|
||||||
color_ = {0.2, 0.2, 0.2, 1};
|
color_ = { 0.2f, 0.2f, 0.2f, 1 };
|
||||||
slot_.h_alignment(horizontal_alignment_t::center);
|
slot_.h_alignment(horizontal_alignment_t::center);
|
||||||
slot_.v_alignment(vertical_alignment_t::center);
|
slot_.v_alignment(vertical_alignment_t::center);
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ void mbutton::on_paint(mirage_paint_context& in_context) {
|
|||||||
void mbutton::on_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
|
void mbutton::on_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
|
||||||
mborder::on_click(in_position, in_button);
|
mborder::on_click(in_position, in_button);
|
||||||
std::println(std::cout, "点击 Button: {}, {}", in_position.x(), in_position.y());
|
std::println(std::cout, "点击 Button: {}, {}", in_position.x(), in_position.y());
|
||||||
color_ = {0.2, 0.2, 0.2, 1};
|
color_ = {0.2f, 0.2f, 0.2f, 1};
|
||||||
invalidate(invalidate_reason::paint);
|
invalidate(invalidate_reason::paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,21 +43,21 @@ void mbutton::on_double_click(const Eigen::Vector2f& in_position, mouse_button i
|
|||||||
void mbutton::on_mouse_enter() {
|
void mbutton::on_mouse_enter() {
|
||||||
mborder::on_mouse_enter();
|
mborder::on_mouse_enter();
|
||||||
std::println(std::cout, "鼠标进入 Button");
|
std::println(std::cout, "鼠标进入 Button");
|
||||||
color_ = {0.2, 0.2, 0.2, 1};
|
color_ = {0.2f, 0.2f, 0.2f, 1};
|
||||||
invalidate(invalidate_reason::paint);
|
invalidate(invalidate_reason::paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbutton::on_mouse_leave() {
|
void mbutton::on_mouse_leave() {
|
||||||
mborder::on_mouse_leave();
|
mborder::on_mouse_leave();
|
||||||
std::println(std::cout, "鼠标离开 Button");
|
std::println(std::cout, "鼠标离开 Button");
|
||||||
color_ = {0.1, 0.1, 0.1, 1};
|
color_ = {0.1f, 0.1f, 0.1f, 1};
|
||||||
invalidate(invalidate_reason::paint);
|
invalidate(invalidate_reason::paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
hit_test_handle mbutton::on_mouse_button_down(const Eigen::Vector2f& in_position, mouse_button in_button) {
|
hit_test_handle mbutton::on_mouse_button_down(const Eigen::Vector2f& in_position, mouse_button in_button) {
|
||||||
mborder::on_mouse_button_down(in_position, in_button);
|
mborder::on_mouse_button_down(in_position, in_button);
|
||||||
std::println(std::cout, "鼠标按下 Button: {}, {}", in_position.x(), in_position.y());
|
std::println(std::cout, "鼠标按下 Button: {}, {}", in_position.x(), in_position.y());
|
||||||
color_ = {0.1, 0.1, 0.1, 1};
|
color_ = {0.1f, 0.1f, 0.1f, 1};
|
||||||
invalidate(invalidate_reason::paint);
|
invalidate(invalidate_reason::paint);
|
||||||
return hit_test_handle::handled();
|
return hit_test_handle::handled();
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ hit_test_handle mbutton::on_mouse_button_down(const Eigen::Vector2f& in_position
|
|||||||
hit_test_handle mbutton::on_mouse_button_up(const Eigen::Vector2f& in_position, mouse_button in_button) {
|
hit_test_handle mbutton::on_mouse_button_up(const Eigen::Vector2f& in_position, mouse_button in_button) {
|
||||||
mborder::on_mouse_button_up(in_position, in_button);
|
mborder::on_mouse_button_up(in_position, in_button);
|
||||||
std::println(std::cout, "鼠标释放 Button: {}, {}", in_position.x(), in_position.y());
|
std::println(std::cout, "鼠标释放 Button: {}, {}", in_position.x(), in_position.y());
|
||||||
color_ = {0.2, 0.2, 0.2, 1};
|
color_ = {0.2f, 0.2f, 0.2f, 1};
|
||||||
invalidate(invalidate_reason::paint);
|
invalidate(invalidate_reason::paint);
|
||||||
return hit_test_handle::handled();
|
return hit_test_handle::handled();
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,10 @@
|
|||||||
set(in_widget); \
|
set(in_widget); \
|
||||||
return me(); \
|
return me(); \
|
||||||
} \
|
} \
|
||||||
|
auto& operator+(const std::shared_ptr<mwidget>& in_widget) { \
|
||||||
|
set(in_widget); \
|
||||||
|
return me(); \
|
||||||
|
} \
|
||||||
protected: \
|
protected: \
|
||||||
std::shared_ptr<mwidget> widget_{};
|
std::shared_ptr<mwidget> widget_{};
|
||||||
|
|
||||||
|
@ -254,7 +254,7 @@ Eigen::Vector2f compute_box_desired_size(const std::vector<SlotType>& child_slot
|
|||||||
if (widget && has_any_flag(widget->get_visibility(), visibility_t::any_visible)) {
|
if (widget && has_any_flag(widget->get_visibility(), visibility_t::any_visible)) {
|
||||||
widget->cache_desired_size(widget->get_dpi_scale());
|
widget->cache_desired_size(widget->get_dpi_scale());
|
||||||
Eigen::Vector2f widget_desired_size = widget->get_desired_size().value();
|
Eigen::Vector2f widget_desired_size = widget->get_desired_size().value();
|
||||||
if (LayoutOrientation == orientation_t::horizontal) {
|
if constexpr (LayoutOrientation == orientation_t::horizontal) {
|
||||||
desired_size.x() += widget_desired_size.x();
|
desired_size.x() += widget_desired_size.x();
|
||||||
desired_size.y() = std::max(desired_size.y(), widget_desired_size.y());
|
desired_size.y() = std::max(desired_size.y(), widget_desired_size.y());
|
||||||
} else {
|
} else {
|
||||||
|
@ -366,7 +366,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void process_mouse_wheel(const Eigen::Vector2f& window_pos, wheel_event wheel_event) {
|
void process_mouse_wheel(const Eigen::Vector2f& window_pos, wheel_event wheel_event) {
|
||||||
const auto& result = perform_hit_test(window_pos, [&](mwidget* widget, const Eigen::Vector2f& local_pos) {
|
perform_hit_test(window_pos, [&](mwidget* widget, const Eigen::Vector2f& local_pos) {
|
||||||
return widget->on_mouse_wheel(local_pos, wheel_event);
|
return widget->on_mouse_wheel(local_pos, wheel_event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user