Initialize shared Metin2 SDK with rendering optimizations and tests

This commit is contained in:
shen
2026-10-11 17:52:55 -07:00
commit 86759bb726
147 changed files with 27272 additions and 0 deletions
+180
View File
@@ -0,0 +1,180 @@
#include "ui_core/ui_core.h"
#include <iostream>
#include <cassert>
using namespace metin2::ui_core;
void TestHierarchyAndLayout() {
std::cout << "[Test] UI Hierarchy & Anchor Layout...\n";
UIManager mgr(1920.0f, 1080.0f);
// Create a centered window (400x300)
auto window = std::make_shared<UIWindow>();
window->SetName("InventoryWindow");
window->SetSize(400.0f, 300.0f);
window->SetAnchor(UIAnchor::Center);
window->SetPosition(0.0f, 0.0f); // Exactly centered
// Add a child button inside window anchored at BottomCenter
auto btn = std::make_shared<UIButton>();
btn->SetName("CloseButton");
btn->SetSize(100.0f, 30.0f);
btn->SetAnchor(UIAnchor::BottomCenter);
btn->SetPosition(0.0f, -10.0f); // 10 px above bottom
window->AddChild(btn);
mgr.AddElement(window);
// Initial Layout check
mgr.UpdateLayout();
const auto& win_rect = window->GetGlobalRect();
assert(win_rect.x == (1920.0f - 400.0f) * 0.5f);
assert(win_rect.y == (1080.0f - 300.0f) * 0.5f);
assert(win_rect.width == 400.0f && win_rect.height == 300.0f);
const auto& btn_rect = btn->GetGlobalRect();
assert(btn_rect.x == win_rect.x + (400.0f - 100.0f) * 0.5f);
assert(btn_rect.y == win_rect.y + 300.0f - 30.0f - 10.0f);
// Resize screen to 1280x720 and verify responsive recalculation
mgr.SetScreenSize(1280.0f, 720.0f);
const auto& win_rect2 = window->GetGlobalRect();
assert(win_rect2.x == (1280.0f - 400.0f) * 0.5f);
assert(win_rect2.y == (720.0f - 300.0f) * 0.5f);
std::cout << " -> UI Hierarchy and responsive layout passed!\n";
}
void TestEventBubblingAndButton() {
std::cout << "[Test] Event Bubbling & Button Interaction...\n";
UIManager mgr(800.0f, 600.0f);
auto window = std::make_shared<UIWindow>();
window->SetPosition(100.0f, 100.0f);
window->SetSize(300.0f, 200.0f);
bool window_caught_event = false;
window->SetOnEvent([&](UIEvent& ev) {
if (ev.type == UIEventType::MouseDown) {
window_caught_event = true;
}
});
auto button = std::make_shared<UIButton>();
button->SetPosition(20.0f, 50.0f);
button->SetSize(80.0f, 30.0f);
bool clicked = false;
button->SetOnClick([&]() {
clicked = true;
});
window->AddChild(button);
mgr.AddElement(window);
mgr.UpdateLayout();
// 1. Click button inside window (global coords: 100+20=120, 100+50=150)
mgr.InjectMouseMove(130.0f, 160.0f);
assert(button->GetButtonState() == ButtonState::Hovered);
mgr.InjectMouseDown(130.0f, 160.0f, 0);
assert(button->GetButtonState() == ButtonState::Pressed);
mgr.InjectMouseUp(130.0f, 160.0f, 0);
assert(clicked);
// 2. Click outside button but inside window: verify event bubbling to window
mgr.InjectMouseDown(250.0f, 180.0f, 0);
assert(window_caught_event);
std::cout << " -> Event bubbling and button click passed!\n";
}
void TestInventoryGridAndDragDrop() {
std::cout << "[Test] Inventory Grid & Slot Drag-and-Drop...\n";
UIManager mgr(1000.0f, 800.0f);
// 5 cols x 9 rows grid (Metin2 standard inventory page)
auto grid = std::make_shared<UIGrid>(5, 9, 32.0f, 2.0f);
grid->SetPosition(50.0f, 50.0f);
mgr.AddElement(grid);
mgr.UpdateLayout();
// Put a sword (vnum 10) in slot (0, 0)
assert(grid->SetItemAt(0, 0, 10, 1));
auto slot0 = grid->GetSlot(0, 0);
assert(slot0->HasItem());
assert(slot0->GetItemVnum() == 10);
// Multi-cell footprint check
// Weapon takes 1x3 cells; can we place at (1, 0)? Yes, empty!
assert(grid->CanPlaceItem(1, 0, 1, 3));
// At (0, 0) it is already occupied, should fail
assert(!grid->CanPlaceItem(0, 0, 1, 3));
// Out of bound check (at col 4, width 2 exceeds cols=5)
assert(!grid->CanPlaceItem(4, 0, 2, 1));
// Drag-and-drop: drag item from Slot (0,0) to Slot (1,1)
auto slot_target = grid->GetSlot(1, 1);
assert(!slot_target->HasItem());
const auto& s0_rect = slot0->GetGlobalRect();
const auto& st_rect = slot_target->GetGlobalRect();
// Mouse down at slot0 center
mgr.InjectMouseDown(s0_rect.x + 16.0f, s0_rect.y + 16.0f, 0);
// Move mouse > 4px to initiate drag
mgr.InjectMouseMove(s0_rect.x + 25.0f, s0_rect.y + 25.0f);
assert(mgr.IsDragging());
// Drag towards target slot and release
mgr.InjectMouseMove(st_rect.x + 16.0f, st_rect.y + 16.0f);
mgr.InjectMouseUp(st_rect.x + 16.0f, st_rect.y + 16.0f, 0);
assert(!mgr.IsDragging());
assert(!slot0->HasItem()); // Moved from slot 0
assert(slot_target->HasItem()); // Landed in slot target
assert(slot_target->GetItemVnum() == 10);
std::cout << " -> Inventory grid and drag-drop transfer passed!\n";
}
void TestNineSliceRenderBatches() {
std::cout << "[Test] Nine-Slice UI Render Batches Output...\n";
UIManager mgr(800.0f, 600.0f);
auto window = std::make_shared<UIWindow>();
window->SetPosition(50.0f, 50.0f);
window->SetSize(200.0f, 150.0f);
window->SetNineSlice("ui/frame_skin.sub", 10.0f, 10.0f, 10.0f, 10.0f);
mgr.AddElement(window);
mgr.UpdateLayout();
auto batches = mgr.GenerateRenderBatches();
assert(!batches.empty());
// Nine slice produces 9 quads (36 vertices, 54 indices)
bool found_nine_slice = false;
for (const auto& b : batches) {
if (b.texture_path == "ui/frame_skin.sub") {
assert(b.vertices.size() == 36);
assert(b.indices.size() == 54);
found_nine_slice = true;
break;
}
}
assert(found_nine_slice);
std::cout << " -> Nine-slice batch generation verified!\n";
}
int main() {
std::cout << "=== Running Metin2 UI Core SDK Tests ===\n";
TestHierarchyAndLayout();
TestEventBubblingAndButton();
TestInventoryGridAndDragDrop();
TestNineSliceRenderBatches();
std::cout << "All UI Core SDK Tests PASSED!\n";
return 0;
}