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
+106
View File
@@ -0,0 +1,106 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include <algorithm>
#include <texture/types.h>
namespace metin2::ui_core {
struct Vec2 {
float x = 0.0f;
float y = 0.0f;
constexpr Vec2() = default;
constexpr Vec2(float in_x, float in_y) : x(in_x), y(in_y) {}
Vec2 operator+(const Vec2& o) const { return {x + o.x, y + o.y}; }
Vec2 operator-(const Vec2& o) const { return {x - o.x, y - o.y}; }
Vec2 operator*(float s) const { return {x * s, y * s}; }
Vec2& operator+=(const Vec2& o) { x += o.x; y += o.y; return *this; }
Vec2& operator-=(const Vec2& o) { x -= o.x; y -= o.y; return *this; }
};
struct Rect {
float x = 0.0f;
float y = 0.0f;
float width = 0.0f;
float height = 0.0f;
constexpr Rect() = default;
constexpr Rect(float in_x, float in_y, float in_w, float in_h)
: x(in_x), y(in_y), width(in_w), height(in_h) {}
float GetLeft() const { return x; }
float GetTop() const { return y; }
float GetRight() const { return x + width; }
float GetBottom() const { return y + height; }
bool Contains(float px, float py) const {
return px >= x && px <= (x + width) && py >= y && py <= (y + height);
}
bool Contains(const Vec2& p) const { return Contains(p.x, p.y); }
bool Intersects(const Rect& o) const {
return !(x + width < o.x || o.x + o.width < x ||
y + height < o.y || o.y + o.height < y);
}
};
enum class UIAnchor : uint8_t {
TopLeft = 0,
TopCenter,
TopRight,
CenterLeft,
Center,
CenterRight,
BottomLeft,
BottomCenter,
BottomRight,
StretchHorizontal,
StretchVertical,
StretchAll
};
enum class UIEventType : uint8_t {
MouseDown = 0,
MouseUp,
MouseMove,
MouseEnter,
MouseLeave,
Click,
DoubleClick,
DragBegin,
DragUpdate,
DragEnd,
Drop,
KeyDown,
KeyUp
};
struct UIEvent {
UIEventType type = UIEventType::MouseMove;
Vec2 mouse_pos{0.0f, 0.0f};
Vec2 delta_pos{0.0f, 0.0f};
int mouse_button = 0; // 0: Left, 1: Right, 2: Middle
int key_code = 0;
void* payload = nullptr;
bool handled = false;
};
struct UIVertex {
float x = 0.0f;
float y = 0.0f;
float u = 0.0f;
float v = 0.0f;
uint32_t color = 0xFFFFFFFF;
};
struct UIRenderBatch {
std::string texture_path;
std::vector<UIVertex> vertices;
std::vector<uint32_t> indices;
};
} // namespace metin2::ui_core
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include "ui_element.h"
#include <functional>
namespace metin2::ui_core {
enum class ButtonState : uint8_t {
Normal = 0,
Hovered,
Pressed,
Disabled
};
class UIButton : public UIElement {
public:
using ClickCallback = std::function<void()>;
UIButton();
void SetText(const std::string& text) { m_text = text; }
const std::string& GetText() const { return m_text; }
void SetOnClick(ClickCallback cb) { m_on_click = std::move(cb); }
ButtonState GetButtonState() const;
void OnMouseDown(UIEvent& event) override;
void OnMouseUp(UIEvent& event) override;
void OnMouseEnter(UIEvent& event) override;
void OnMouseLeave(UIEvent& event) override;
void OnClick(UIEvent& event) override;
void GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) override;
private:
std::string m_text;
ClickCallback m_on_click;
};
} // namespace metin2::ui_core
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "types.h"
#include "ui_element.h"
#include "ui_window.h"
#include "ui_button.h"
#include "ui_text.h"
#include "ui_slot.h"
#include "ui_grid.h"
#include "ui_manager.h"
+114
View File
@@ -0,0 +1,114 @@
#pragma once
#include "types.h"
#include <memory>
#include <vector>
#include <string>
#include <functional>
namespace metin2::ui_core {
class UIElement : public std::enable_shared_from_this<UIElement> {
public:
using EventHandler = std::function<void(UIEvent&)>;
UIElement();
virtual ~UIElement() = default;
// Hierarchy
void AddChild(std::shared_ptr<UIElement> child);
void RemoveChild(std::shared_ptr<UIElement> child);
void RemoveFromParent();
UIElement* GetParent() const { return m_parent; }
const std::vector<std::shared_ptr<UIElement>>& GetChildren() const { return m_children; }
// Identification & Tagging
void SetName(const std::string& name) { m_name = name; }
const std::string& GetName() const { return m_name; }
void SetId(uint32_t id) { m_id = id; }
uint32_t GetId() const { return m_id; }
// Transform & Layout
void SetPosition(float x, float y) { m_local_rect.x = x; m_local_rect.y = y; }
void SetSize(float w, float h) { m_local_rect.width = w; m_local_rect.height = h; }
void SetAnchor(UIAnchor anchor) { m_anchor = anchor; }
UIAnchor GetAnchor() const { return m_anchor; }
const Rect& GetLocalRect() const { return m_local_rect; }
const Rect& GetGlobalRect() const { return m_global_rect; }
virtual void UpdateLayout(const Rect& parent_rect);
// Visibility & Interaction
void SetVisible(bool visible) { m_visible = visible; }
bool IsVisible() const { return m_visible; }
void SetEnabled(bool enabled) { m_enabled = enabled; }
bool IsEnabled() const { return m_enabled; }
// State Tracking
bool IsHovered() const { return m_is_hovered; }
bool IsPressed() const { return m_is_pressed; }
bool IsFocused() const { return m_is_focused; }
bool IsDragging() const { return m_is_dragging; }
void SetHovered(bool hovered) { m_is_hovered = hovered; }
void SetPressed(bool pressed) { m_is_pressed = pressed; }
void SetFocused(bool focused) { m_is_focused = focused; }
void SetDragging(bool dragging) { m_is_dragging = dragging; }
// Nine-Slice Background Support
void SetNineSlice(const std::string& texture, float border_l, float border_t, float border_r, float border_b);
void SetBackgroundColor(uint32_t color) { m_bg_color = color; }
// Hit Testing & Event Dispatch with Bubbling
virtual std::shared_ptr<UIElement> HitTest(float px, float py);
virtual void DispatchEvent(UIEvent& event);
// Event Hooks for Subclasses
virtual void OnMouseDown(UIEvent& event) {}
virtual void OnMouseUp(UIEvent& event) {}
virtual void OnMouseMove(UIEvent& event) {}
virtual void OnMouseEnter(UIEvent& event) {}
virtual void OnMouseLeave(UIEvent& event) {}
virtual void OnClick(UIEvent& event) {}
virtual void OnDragBegin(UIEvent& event) {}
virtual void OnDragUpdate(UIEvent& event) {}
virtual void OnDragEnd(UIEvent& event) {}
virtual void OnDrop(UIEvent& event) {}
// Event Handler Registration
void SetOnEvent(EventHandler handler) { m_event_handler = std::move(handler); }
// Rendering Batch Generation
virtual void GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches);
protected:
void AppendQuad(UIRenderBatch& batch, float x, float y, float w, float h,
float u0, float v0, float u1, float v1, uint32_t color);
std::string m_name;
uint32_t m_id = 0;
UIElement* m_parent = nullptr;
std::vector<std::shared_ptr<UIElement>> m_children;
Rect m_local_rect{0.0f, 0.0f, 0.0f, 0.0f};
Rect m_global_rect{0.0f, 0.0f, 0.0f, 0.0f};
UIAnchor m_anchor = UIAnchor::TopLeft;
bool m_visible = true;
bool m_enabled = true;
bool m_is_hovered = false;
bool m_is_pressed = false;
bool m_is_focused = false;
bool m_is_dragging = false;
// Nine-Slice properties
bool m_has_nine_slice = false;
std::string m_nine_slice_texture;
float m_border_l = 0.0f, m_border_t = 0.0f, m_border_r = 0.0f, m_border_b = 0.0f;
uint32_t m_bg_color = 0xFFFFFFFF;
EventHandler m_event_handler;
};
} // namespace metin2::ui_core
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "ui_element.h"
#include "ui_slot.h"
#include <vector>
namespace metin2::ui_core {
class UIGrid : public UIElement {
public:
UIGrid(uint32_t cols = 5, uint32_t rows = 9, float slot_size = 32.0f, float slot_spacing = 2.0f);
uint32_t GetCols() const { return m_cols; }
uint32_t GetRows() const { return m_rows; }
float GetSlotSize() const { return m_slot_size; }
float GetSlotSpacing() const { return m_slot_spacing; }
std::shared_ptr<UISlot> GetSlot(uint32_t col, uint32_t row) const;
std::shared_ptr<UISlot> GetSlotByIndex(uint32_t index) const;
bool SetItemAt(uint32_t col, uint32_t row, uint32_t item_vnum, uint32_t count = 1);
void ClearItemAt(uint32_t col, uint32_t row);
// Multi-cell footprint check (e.g. 1x2 dagger, 1x3 greatsword)
bool CanPlaceItem(uint32_t col, uint32_t row, uint32_t cell_width, uint32_t cell_height) const;
void UpdateLayout(const Rect& parent_rect) override;
private:
void RebuildSlots();
uint32_t m_cols = 5;
uint32_t m_rows = 9;
float m_slot_size = 32.0f;
float m_slot_spacing = 2.0f;
std::vector<std::shared_ptr<UISlot>> m_slots;
};
} // namespace metin2::ui_core
+55
View File
@@ -0,0 +1,55 @@
#pragma once
#include "ui_element.h"
#include <memory>
#include <vector>
namespace metin2::ui_core {
class UIManager {
public:
UIManager(float screen_width = 1920.0f, float screen_height = 1080.0f);
void SetScreenSize(float width, float height);
Vec2 GetScreenSize() const { return {m_screen_rect.width, m_screen_rect.height}; }
std::shared_ptr<UIElement> GetRoot() const { return m_root; }
// Child Management on Root
void AddElement(std::shared_ptr<UIElement> element);
void RemoveElement(std::shared_ptr<UIElement> element);
// Layout Resolution
void UpdateLayout();
// Input Injection
bool InjectMouseDown(float x, float y, int button);
bool InjectMouseUp(float x, float y, int button);
bool InjectMouseMove(float x, float y);
bool InjectKeyDown(int key_code);
bool InjectKeyUp(int key_code);
// Drag & Drop State
bool IsDragging() const { return m_dragged_element != nullptr; }
std::shared_ptr<UIElement> GetDraggedElement() const { return m_dragged_element; }
void CancelDrag();
// Rendering Pipeline Output
std::vector<UIRenderBatch> GenerateRenderBatches();
private:
Rect m_screen_rect{0.0f, 0.0f, 1920.0f, 1080.0f};
std::shared_ptr<UIElement> m_root;
std::shared_ptr<UIElement> m_hovered_element;
std::shared_ptr<UIElement> m_focused_element;
std::shared_ptr<UIElement> m_pressed_element;
std::shared_ptr<UIElement> m_dragged_element;
Vec2 m_last_mouse_pos{0.0f, 0.0f};
Vec2 m_drag_start_pos{0.0f, 0.0f};
bool m_mouse_down = false;
int m_active_button = 0;
};
} // namespace metin2::ui_core
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include "ui_element.h"
namespace metin2::ui_core {
class UISlot : public UIElement {
public:
using SlotDropCallback = std::function<void(uint32_t from_slot_id, uint32_t item_vnum, uint32_t count)>;
UISlot();
explicit UISlot(uint32_t slot_index);
void SetSlotIndex(uint32_t idx) { m_slot_index = idx; }
uint32_t GetSlotIndex() const { return m_slot_index; }
void SetItem(uint32_t item_vnum, uint32_t count = 1);
void ClearItem();
bool HasItem() const { return m_item_vnum > 0 && m_item_count > 0; }
uint32_t GetItemVnum() const { return m_item_vnum; }
uint32_t GetItemCount() const { return m_item_count; }
// Cooldown progress [0.0f, 1.0f] (0.0: ready, 1.0: full cooldown)
void SetCooldown(float progress) { m_cooldown_progress = std::clamp(progress, 0.0f, 1.0f); }
float GetCooldown() const { return m_cooldown_progress; }
void SetSelected(bool selected) { m_is_selected = selected; }
bool IsSelected() const { return m_is_selected; }
void SetOnDrop(SlotDropCallback cb) { m_on_drop = std::move(cb); }
// Drag and Drop
void OnMouseDown(UIEvent& event) override;
void OnDragBegin(UIEvent& event) override;
void OnDrop(UIEvent& event) override;
void GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) override;
private:
uint32_t m_slot_index = 0;
uint32_t m_item_vnum = 0;
uint32_t m_item_count = 0;
float m_cooldown_progress = 0.0f;
bool m_is_selected = false;
SlotDropCallback m_on_drop;
};
} // namespace metin2::ui_core
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "ui_element.h"
namespace metin2::ui_core {
enum class TextAlignment : uint8_t {
Left = 0,
Center,
Right
};
class UIText : public UIElement {
public:
UIText();
explicit UIText(const std::string& text);
void SetText(const std::string& text) { m_text = text; }
const std::string& GetText() const { return m_text; }
void SetTextColor(uint32_t color) { m_text_color = color; }
uint32_t GetTextColor() const { return m_text_color; }
void SetAlignment(TextAlignment align) { m_alignment = align; }
TextAlignment GetAlignment() const { return m_alignment; }
void SetFontSize(float size) { m_font_size = size; }
float GetFontSize() const { return m_font_size; }
void GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) override;
private:
std::string m_text;
uint32_t m_text_color = 0xFFFFFFFF;
TextAlignment m_alignment = TextAlignment::Left;
float m_font_size = 14.0f;
};
} // namespace metin2::ui_core
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include "ui_element.h"
namespace metin2::ui_core {
class UIWindow : public UIElement {
public:
UIWindow();
void SetTitle(const std::string& title) { m_title = title; }
const std::string& GetTitle() const { return m_title; }
void SetTitleBarHeight(float height) { m_title_bar_height = height; }
float GetTitleBarHeight() const { return m_title_bar_height; }
void SetMovable(bool movable) { m_is_movable = movable; }
bool IsMovable() const { return m_is_movable; }
void OnMouseDown(UIEvent& event) override;
void OnMouseMove(UIEvent& event) override;
void OnMouseUp(UIEvent& event) override;
void GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) override;
private:
std::string m_title;
float m_title_bar_height = 24.0f;
bool m_is_movable = true;
bool m_dragging_title_bar = false;
Vec2 m_drag_start_mouse{0.0f, 0.0f};
Vec2 m_drag_start_pos{0.0f, 0.0f};
};
} // namespace metin2::ui_core