Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(ui_core LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(ui_core STATIC
|
||||
src/ui_element.cpp
|
||||
src/ui_window.cpp
|
||||
src/ui_button.cpp
|
||||
src/ui_text.cpp
|
||||
src/ui_slot.cpp
|
||||
src/ui_grid.cpp
|
||||
src/ui_manager.cpp
|
||||
)
|
||||
|
||||
target_include_directories(ui_core PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
target_link_libraries(ui_core PUBLIC texture)
|
||||
|
||||
add_library(ui_core::ui_core ALIAS ui_core)
|
||||
|
||||
option(UI_CORE_BUILD_TESTS "Build UI Core SDK tests" ON)
|
||||
if(UI_CORE_BUILD_TESTS)
|
||||
add_executable(ui_core_test tests/ui_core_test.cpp)
|
||||
target_link_libraries(ui_core_test PRIVATE ui_core)
|
||||
add_test(NAME ui_core_test COMMAND ui_core_test)
|
||||
endif()
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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"
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "ui_core/ui_button.h"
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UIButton::UIButton() {
|
||||
m_bg_color = 0xFF3D3D3D;
|
||||
m_local_rect.width = 80.0f;
|
||||
m_local_rect.height = 28.0f;
|
||||
}
|
||||
|
||||
ButtonState UIButton::GetButtonState() const {
|
||||
if (!m_enabled) return ButtonState::Disabled;
|
||||
if (m_is_pressed) return ButtonState::Pressed;
|
||||
if (m_is_hovered) return ButtonState::Hovered;
|
||||
return ButtonState::Normal;
|
||||
}
|
||||
|
||||
void UIButton::OnMouseEnter(UIEvent& event) {
|
||||
m_is_hovered = true;
|
||||
}
|
||||
|
||||
void UIButton::OnMouseLeave(UIEvent& event) {
|
||||
m_is_hovered = false;
|
||||
m_is_pressed = false;
|
||||
}
|
||||
|
||||
void UIButton::OnMouseDown(UIEvent& event) {
|
||||
if (event.mouse_button == 0 && m_enabled) {
|
||||
m_is_pressed = true;
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UIButton::OnMouseUp(UIEvent& event) {
|
||||
if (event.mouse_button == 0 && m_is_pressed) {
|
||||
m_is_pressed = false;
|
||||
if (m_global_rect.Contains(event.mouse_pos.x, event.mouse_pos.y)) {
|
||||
OnClick(event);
|
||||
}
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UIButton::OnClick(UIEvent& event) {
|
||||
if (m_enabled && m_on_click) {
|
||||
m_on_click();
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UIButton::GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) {
|
||||
if (!m_visible) return;
|
||||
|
||||
uint32_t current_color = m_bg_color;
|
||||
switch (GetButtonState()) {
|
||||
case ButtonState::Normal: current_color = 0xFF4A4A4A; break;
|
||||
case ButtonState::Hovered: current_color = 0xFF606060; break;
|
||||
case ButtonState::Pressed: current_color = 0xFF2A2A2A; break;
|
||||
case ButtonState::Disabled: current_color = 0xFF252525; break;
|
||||
}
|
||||
|
||||
UIRenderBatch batch;
|
||||
AppendQuad(batch, m_global_rect.x, m_global_rect.y, m_global_rect.width, m_global_rect.height,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, current_color);
|
||||
out_batches.push_back(std::move(batch));
|
||||
|
||||
// Base children
|
||||
for (auto& child : m_children) {
|
||||
if (child) child->GenerateRenderBatches(out_batches);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -0,0 +1,236 @@
|
||||
#include "ui_core/ui_element.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UIElement::UIElement() = default;
|
||||
|
||||
void UIElement::AddChild(std::shared_ptr<UIElement> child) {
|
||||
if (!child || child.get() == this) return;
|
||||
|
||||
if (child->m_parent) {
|
||||
child->RemoveFromParent();
|
||||
}
|
||||
|
||||
child->m_parent = this;
|
||||
m_children.push_back(child);
|
||||
}
|
||||
|
||||
void UIElement::RemoveChild(std::shared_ptr<UIElement> child) {
|
||||
auto it = std::find(m_children.begin(), m_children.end(), child);
|
||||
if (it != m_children.end()) {
|
||||
(*it)->m_parent = nullptr;
|
||||
m_children.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void UIElement::RemoveFromParent() {
|
||||
if (m_parent) {
|
||||
auto shared_this = shared_from_this();
|
||||
m_parent->RemoveChild(shared_this);
|
||||
}
|
||||
}
|
||||
|
||||
void UIElement::SetNineSlice(const std::string& texture, float border_l, float border_t, float border_r, float border_b) {
|
||||
m_has_nine_slice = true;
|
||||
m_nine_slice_texture = texture;
|
||||
m_border_l = border_l;
|
||||
m_border_t = border_t;
|
||||
m_border_r = border_r;
|
||||
m_border_b = border_b;
|
||||
}
|
||||
|
||||
void UIElement::UpdateLayout(const Rect& parent_rect) {
|
||||
float gx = 0.0f;
|
||||
float gy = 0.0f;
|
||||
float gw = m_local_rect.width;
|
||||
float gh = m_local_rect.height;
|
||||
|
||||
switch (m_anchor) {
|
||||
case UIAnchor::TopLeft:
|
||||
gx = parent_rect.x + m_local_rect.x;
|
||||
gy = parent_rect.y + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::TopCenter:
|
||||
gx = parent_rect.x + (parent_rect.width - gw) * 0.5f + m_local_rect.x;
|
||||
gy = parent_rect.y + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::TopRight:
|
||||
gx = parent_rect.x + parent_rect.width - gw + m_local_rect.x;
|
||||
gy = parent_rect.y + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::CenterLeft:
|
||||
gx = parent_rect.x + m_local_rect.x;
|
||||
gy = parent_rect.y + (parent_rect.height - gh) * 0.5f + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::Center:
|
||||
gx = parent_rect.x + (parent_rect.width - gw) * 0.5f + m_local_rect.x;
|
||||
gy = parent_rect.y + (parent_rect.height - gh) * 0.5f + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::CenterRight:
|
||||
gx = parent_rect.x + parent_rect.width - gw + m_local_rect.x;
|
||||
gy = parent_rect.y + (parent_rect.height - gh) * 0.5f + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::BottomLeft:
|
||||
gx = parent_rect.x + m_local_rect.x;
|
||||
gy = parent_rect.y + parent_rect.height - gh + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::BottomCenter:
|
||||
gx = parent_rect.x + (parent_rect.width - gw) * 0.5f + m_local_rect.x;
|
||||
gy = parent_rect.y + parent_rect.height - gh + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::BottomRight:
|
||||
gx = parent_rect.x + parent_rect.width - gw + m_local_rect.x;
|
||||
gy = parent_rect.y + parent_rect.height - gh + m_local_rect.y;
|
||||
break;
|
||||
case UIAnchor::StretchHorizontal:
|
||||
gx = parent_rect.x + m_local_rect.x;
|
||||
gy = parent_rect.y + m_local_rect.y;
|
||||
gw = parent_rect.width - m_local_rect.x * 2.0f;
|
||||
break;
|
||||
case UIAnchor::StretchVertical:
|
||||
gx = parent_rect.x + m_local_rect.x;
|
||||
gy = parent_rect.y + m_local_rect.y;
|
||||
gh = parent_rect.height - m_local_rect.y * 2.0f;
|
||||
break;
|
||||
case UIAnchor::StretchAll:
|
||||
gx = parent_rect.x + m_local_rect.x;
|
||||
gy = parent_rect.y + m_local_rect.y;
|
||||
gw = parent_rect.width - m_local_rect.x * 2.0f;
|
||||
gh = parent_rect.height - m_local_rect.y * 2.0f;
|
||||
break;
|
||||
}
|
||||
|
||||
m_global_rect = Rect(gx, gy, gw, gh);
|
||||
|
||||
// Recursively update all children
|
||||
for (auto& child : m_children) {
|
||||
if (child) {
|
||||
child->UpdateLayout(m_global_rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<UIElement> UIElement::HitTest(float px, float py) {
|
||||
if (!m_visible || !m_enabled) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!m_global_rect.Contains(px, py)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Children tested in reverse order (topmost first)
|
||||
for (auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
|
||||
if (*it) {
|
||||
auto hit = (*it)->HitTest(px, py);
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
void UIElement::DispatchEvent(UIEvent& event) {
|
||||
// 1. Invoke element specific virtual hook
|
||||
switch (event.type) {
|
||||
case UIEventType::MouseDown: OnMouseDown(event); break;
|
||||
case UIEventType::MouseUp: OnMouseUp(event); break;
|
||||
case UIEventType::MouseMove: OnMouseMove(event); break;
|
||||
case UIEventType::MouseEnter: OnMouseEnter(event); break;
|
||||
case UIEventType::MouseLeave: OnMouseLeave(event); break;
|
||||
case UIEventType::Click: OnClick(event); break;
|
||||
case UIEventType::DragBegin: OnDragBegin(event); break;
|
||||
case UIEventType::DragUpdate: OnDragUpdate(event); break;
|
||||
case UIEventType::DragEnd: OnDragEnd(event); break;
|
||||
case UIEventType::Drop: OnDrop(event); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
// 2. Invoke custom event callback
|
||||
if (m_event_handler) {
|
||||
m_event_handler(event);
|
||||
}
|
||||
|
||||
// 3. Event Bubbling: if unhandled, bubble up to parent
|
||||
if (!event.handled && m_parent) {
|
||||
m_parent->DispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void UIElement::AppendQuad(UIRenderBatch& batch, float x, float y, float w, float h,
|
||||
float u0, float v0, float u1, float v1, uint32_t color) {
|
||||
uint32_t base_index = static_cast<uint32_t>(batch.vertices.size());
|
||||
|
||||
batch.vertices.push_back({x, y, u0, v0, color});
|
||||
batch.vertices.push_back({x + w, y, u1, v0, color});
|
||||
batch.vertices.push_back({x + w, y + h, u1, v1, color});
|
||||
batch.vertices.push_back({x, y + h, u0, v1, color});
|
||||
|
||||
batch.indices.push_back(base_index + 0);
|
||||
batch.indices.push_back(base_index + 1);
|
||||
batch.indices.push_back(base_index + 2);
|
||||
batch.indices.push_back(base_index + 0);
|
||||
batch.indices.push_back(base_index + 2);
|
||||
batch.indices.push_back(base_index + 3);
|
||||
}
|
||||
|
||||
void UIElement::GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) {
|
||||
if (!m_visible) return;
|
||||
|
||||
if (m_has_nine_slice && !m_nine_slice_texture.empty()) {
|
||||
UIRenderBatch batch;
|
||||
batch.texture_path = m_nine_slice_texture;
|
||||
|
||||
float x = m_global_rect.x;
|
||||
float y = m_global_rect.y;
|
||||
float w = m_global_rect.width;
|
||||
float h = m_global_rect.height;
|
||||
|
||||
float bl = m_border_l;
|
||||
float bt = m_border_t;
|
||||
float br = m_border_r;
|
||||
float bb = m_border_b;
|
||||
|
||||
float center_w = std::max(0.0f, w - bl - br);
|
||||
float center_h = std::max(0.0f, h - bt - bb);
|
||||
|
||||
// UV borders (assuming normalized 1.0 atlas or 9-slice)
|
||||
float u_l = 0.25f, u_r = 0.75f;
|
||||
float v_t = 0.25f, v_b = 0.75f;
|
||||
|
||||
// 3x3 Grid patches:
|
||||
// Top row
|
||||
AppendQuad(batch, x, y, bl, bt, 0.0f, 0.0f, u_l, v_t, m_bg_color);
|
||||
AppendQuad(batch, x + bl, y, center_w, bt, u_l, 0.0f, u_r, v_t, m_bg_color);
|
||||
AppendQuad(batch, x + bl + center_w, y, br, bt, u_r, 0.0f, 1.0f, v_t, m_bg_color);
|
||||
|
||||
// Middle row
|
||||
AppendQuad(batch, x, y + bt, bl, center_h, 0.0f, v_t, u_l, v_b, m_bg_color);
|
||||
AppendQuad(batch, x + bl, y + bt, center_w, center_h, u_l, v_t, u_r, v_b, m_bg_color);
|
||||
AppendQuad(batch, x + bl + center_w, y + bt, br, center_h, u_r, v_t, 1.0f, v_b, m_bg_color);
|
||||
|
||||
// Bottom row
|
||||
AppendQuad(batch, x, y + bt + center_h, bl, bb, 0.0f, v_b, u_l, 1.0f, m_bg_color);
|
||||
AppendQuad(batch, x + bl, y + bt + center_h, center_w, bb, u_l, v_b, u_r, 1.0f, m_bg_color);
|
||||
AppendQuad(batch, x + bl + center_w, y + bt + center_h, br, bb, u_r, v_b, 1.0f, 1.0f, m_bg_color);
|
||||
|
||||
out_batches.push_back(std::move(batch));
|
||||
} else if (m_bg_color != 0) {
|
||||
UIRenderBatch batch;
|
||||
AppendQuad(batch, m_global_rect.x, m_global_rect.y, m_global_rect.width, m_global_rect.height,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, m_bg_color);
|
||||
out_batches.push_back(std::move(batch));
|
||||
}
|
||||
|
||||
// Children batches
|
||||
for (auto& child : m_children) {
|
||||
if (child) {
|
||||
child->GenerateRenderBatches(out_batches);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "ui_core/ui_grid.h"
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UIGrid::UIGrid(uint32_t cols, uint32_t rows, float slot_size, float slot_spacing)
|
||||
: m_cols(cols)
|
||||
, m_rows(rows)
|
||||
, m_slot_size(slot_size)
|
||||
, m_slot_spacing(slot_spacing) {
|
||||
m_local_rect.width = m_cols * m_slot_size + (m_cols > 1 ? (m_cols - 1) * m_slot_spacing : 0);
|
||||
m_local_rect.height = m_rows * m_slot_size + (m_rows > 1 ? (m_rows - 1) * m_slot_spacing : 0);
|
||||
RebuildSlots();
|
||||
}
|
||||
|
||||
void UIGrid::RebuildSlots() {
|
||||
m_slots.clear();
|
||||
m_children.clear();
|
||||
|
||||
uint32_t total = m_cols * m_rows;
|
||||
m_slots.reserve(total);
|
||||
|
||||
for (uint32_t r = 0; r < m_rows; ++r) {
|
||||
for (uint32_t c = 0; c < m_cols; ++c) {
|
||||
uint32_t idx = r * m_cols + c;
|
||||
auto slot = std::make_shared<UISlot>(idx);
|
||||
float lx = c * (m_slot_size + m_slot_spacing);
|
||||
float ly = r * (m_slot_size + m_slot_spacing);
|
||||
slot->SetPosition(lx, ly);
|
||||
slot->SetSize(m_slot_size, m_slot_size);
|
||||
m_slots.push_back(slot);
|
||||
AddChild(slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<UISlot> UIGrid::GetSlot(uint32_t col, uint32_t row) const {
|
||||
if (col >= m_cols || row >= m_rows) return nullptr;
|
||||
uint32_t idx = row * m_cols + col;
|
||||
return m_slots[idx];
|
||||
}
|
||||
|
||||
std::shared_ptr<UISlot> UIGrid::GetSlotByIndex(uint32_t index) const {
|
||||
if (index >= m_slots.size()) return nullptr;
|
||||
return m_slots[index];
|
||||
}
|
||||
|
||||
bool UIGrid::SetItemAt(uint32_t col, uint32_t row, uint32_t item_vnum, uint32_t count) {
|
||||
auto slot = GetSlot(col, row);
|
||||
if (!slot) return false;
|
||||
slot->SetItem(item_vnum, count);
|
||||
return true;
|
||||
}
|
||||
|
||||
void UIGrid::ClearItemAt(uint32_t col, uint32_t row) {
|
||||
auto slot = GetSlot(col, row);
|
||||
if (slot) {
|
||||
slot->ClearItem();
|
||||
}
|
||||
}
|
||||
|
||||
bool UIGrid::CanPlaceItem(uint32_t col, uint32_t row, uint32_t cell_width, uint32_t cell_height) const {
|
||||
if (col + cell_width > m_cols || row + cell_height > m_rows) {
|
||||
return false; // Out of bounds
|
||||
}
|
||||
|
||||
for (uint32_t r = 0; r < cell_height; ++r) {
|
||||
for (uint32_t c = 0; c < cell_width; ++c) {
|
||||
auto s = GetSlot(col + c, row + r);
|
||||
if (!s || s->HasItem()) {
|
||||
return false; // Collides with occupied slot
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void UIGrid::UpdateLayout(const Rect& parent_rect) {
|
||||
UIElement::UpdateLayout(parent_rect);
|
||||
// Children positions were set relative to grid local origin
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -0,0 +1,201 @@
|
||||
#include "ui_core/ui_manager.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UIManager::UIManager(float screen_width, float screen_height)
|
||||
: m_screen_rect(0.0f, 0.0f, screen_width, screen_height) {
|
||||
m_root = std::make_shared<UIElement>();
|
||||
m_root->SetName("Root");
|
||||
m_root->SetPosition(0.0f, 0.0f);
|
||||
m_root->SetSize(screen_width, screen_height);
|
||||
m_root->SetBackgroundColor(0); // Transparent root
|
||||
}
|
||||
|
||||
void UIManager::SetScreenSize(float width, float height) {
|
||||
m_screen_rect = Rect(0.0f, 0.0f, width, height);
|
||||
if (m_root) {
|
||||
m_root->SetSize(width, height);
|
||||
UpdateLayout();
|
||||
}
|
||||
}
|
||||
|
||||
void UIManager::AddElement(std::shared_ptr<UIElement> element) {
|
||||
if (m_root && element) {
|
||||
m_root->AddChild(element);
|
||||
UpdateLayout();
|
||||
}
|
||||
}
|
||||
|
||||
void UIManager::RemoveElement(std::shared_ptr<UIElement> element) {
|
||||
if (m_root && element) {
|
||||
m_root->RemoveChild(element);
|
||||
}
|
||||
}
|
||||
|
||||
void UIManager::UpdateLayout() {
|
||||
if (m_root) {
|
||||
m_root->UpdateLayout(m_screen_rect);
|
||||
}
|
||||
}
|
||||
|
||||
bool UIManager::InjectMouseMove(float x, float y) {
|
||||
Vec2 current_pos{x, y};
|
||||
Vec2 delta = current_pos - m_last_mouse_pos;
|
||||
m_last_mouse_pos = current_pos;
|
||||
|
||||
// Check Drag threshold (e.g., > 4 pixels)
|
||||
if (m_mouse_down && !m_dragged_element && m_pressed_element) {
|
||||
if ((current_pos - m_drag_start_pos).x * (current_pos - m_drag_start_pos).x +
|
||||
(current_pos - m_drag_start_pos).y * (current_pos - m_drag_start_pos).y > 16.0f) {
|
||||
UIEvent drag_event;
|
||||
drag_event.type = UIEventType::DragBegin;
|
||||
drag_event.mouse_pos = current_pos;
|
||||
drag_event.delta_pos = delta;
|
||||
drag_event.mouse_button = m_active_button;
|
||||
|
||||
m_pressed_element->DispatchEvent(drag_event);
|
||||
if (drag_event.handled) {
|
||||
m_dragged_element = m_pressed_element;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_dragged_element) {
|
||||
UIEvent drag_update;
|
||||
drag_update.type = UIEventType::DragUpdate;
|
||||
drag_update.mouse_pos = current_pos;
|
||||
drag_update.delta_pos = delta;
|
||||
drag_update.mouse_button = m_active_button;
|
||||
m_dragged_element->DispatchEvent(drag_update);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Hover state management
|
||||
auto hit = m_root ? m_root->HitTest(x, y) : nullptr;
|
||||
if (hit != m_hovered_element) {
|
||||
if (m_hovered_element) {
|
||||
UIEvent leave_event;
|
||||
leave_event.type = UIEventType::MouseLeave;
|
||||
leave_event.mouse_pos = current_pos;
|
||||
m_hovered_element->DispatchEvent(leave_event);
|
||||
}
|
||||
m_hovered_element = hit;
|
||||
if (m_hovered_element) {
|
||||
UIEvent enter_event;
|
||||
enter_event.type = UIEventType::MouseEnter;
|
||||
enter_event.mouse_pos = current_pos;
|
||||
m_hovered_element->DispatchEvent(enter_event);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_hovered_element) {
|
||||
UIEvent move_event;
|
||||
move_event.type = UIEventType::MouseMove;
|
||||
move_event.mouse_pos = current_pos;
|
||||
move_event.delta_pos = delta;
|
||||
m_hovered_element->DispatchEvent(move_event);
|
||||
return move_event.handled;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UIManager::InjectMouseDown(float x, float y, int button) {
|
||||
m_mouse_down = true;
|
||||
m_active_button = button;
|
||||
m_drag_start_pos = {x, y};
|
||||
m_last_mouse_pos = {x, y};
|
||||
|
||||
auto hit = m_root ? m_root->HitTest(x, y) : nullptr;
|
||||
m_pressed_element = hit;
|
||||
m_focused_element = hit;
|
||||
|
||||
if (hit) {
|
||||
UIEvent down_event;
|
||||
down_event.type = UIEventType::MouseDown;
|
||||
down_event.mouse_pos = {x, y};
|
||||
down_event.mouse_button = button;
|
||||
hit->DispatchEvent(down_event);
|
||||
return down_event.handled;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UIManager::InjectMouseUp(float x, float y, int button) {
|
||||
m_mouse_down = false;
|
||||
Vec2 pos{x, y};
|
||||
|
||||
if (m_dragged_element) {
|
||||
auto drop_target = m_root ? m_root->HitTest(x, y) : nullptr;
|
||||
if (drop_target && drop_target != m_dragged_element) {
|
||||
UIEvent drop_event;
|
||||
drop_event.type = UIEventType::Drop;
|
||||
drop_event.mouse_pos = pos;
|
||||
drop_event.mouse_button = button;
|
||||
drop_event.payload = m_dragged_element.get();
|
||||
drop_target->DispatchEvent(drop_event);
|
||||
}
|
||||
|
||||
UIEvent end_event;
|
||||
end_event.type = UIEventType::DragEnd;
|
||||
end_event.mouse_pos = pos;
|
||||
end_event.mouse_button = button;
|
||||
m_dragged_element->DispatchEvent(end_event);
|
||||
m_dragged_element->SetDragging(false);
|
||||
m_dragged_element = nullptr;
|
||||
m_pressed_element = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_pressed_element) {
|
||||
UIEvent up_event;
|
||||
up_event.type = UIEventType::MouseUp;
|
||||
up_event.mouse_pos = pos;
|
||||
up_event.mouse_button = button;
|
||||
m_pressed_element->DispatchEvent(up_event);
|
||||
m_pressed_element = nullptr;
|
||||
return up_event.handled;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UIManager::InjectKeyDown(int key_code) {
|
||||
if (m_focused_element) {
|
||||
UIEvent event;
|
||||
event.type = UIEventType::KeyDown;
|
||||
event.key_code = key_code;
|
||||
m_focused_element->DispatchEvent(event);
|
||||
return event.handled;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UIManager::InjectKeyUp(int key_code) {
|
||||
if (m_focused_element) {
|
||||
UIEvent event;
|
||||
event.type = UIEventType::KeyUp;
|
||||
event.key_code = key_code;
|
||||
m_focused_element->DispatchEvent(event);
|
||||
return event.handled;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void UIManager::CancelDrag() {
|
||||
if (m_dragged_element) {
|
||||
m_dragged_element->SetDragging(false);
|
||||
m_dragged_element = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<UIRenderBatch> UIManager::GenerateRenderBatches() {
|
||||
std::vector<UIRenderBatch> batches;
|
||||
if (m_root) {
|
||||
m_root->GenerateRenderBatches(batches);
|
||||
}
|
||||
return batches;
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "ui_core/ui_slot.h"
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UISlot::UISlot() {
|
||||
m_local_rect.width = 32.0f;
|
||||
m_local_rect.height = 32.0f;
|
||||
m_bg_color = 0xFF1E1E1E;
|
||||
}
|
||||
|
||||
UISlot::UISlot(uint32_t slot_index)
|
||||
: m_slot_index(slot_index) {
|
||||
m_local_rect.width = 32.0f;
|
||||
m_local_rect.height = 32.0f;
|
||||
m_bg_color = 0xFF1E1E1E;
|
||||
}
|
||||
|
||||
void UISlot::SetItem(uint32_t item_vnum, uint32_t count) {
|
||||
m_item_vnum = item_vnum;
|
||||
m_item_count = count;
|
||||
}
|
||||
|
||||
void UISlot::ClearItem() {
|
||||
m_item_vnum = 0;
|
||||
m_item_count = 0;
|
||||
m_cooldown_progress = 0.0f;
|
||||
m_is_selected = false;
|
||||
}
|
||||
|
||||
void UISlot::OnMouseDown(UIEvent& event) {
|
||||
if (event.mouse_button == 0 && HasItem()) {
|
||||
m_is_pressed = true;
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UISlot::OnDragBegin(UIEvent& event) {
|
||||
if (HasItem()) {
|
||||
m_is_dragging = true;
|
||||
event.payload = this;
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UISlot::OnDrop(UIEvent& event) {
|
||||
if (event.payload) {
|
||||
auto* src_slot = static_cast<UISlot*>(event.payload);
|
||||
if (src_slot && src_slot != this) {
|
||||
if (m_on_drop) {
|
||||
m_on_drop(src_slot->GetSlotIndex(), src_slot->GetItemVnum(), src_slot->GetItemCount());
|
||||
}
|
||||
// Swap / Transfer
|
||||
uint32_t old_vnum = m_item_vnum;
|
||||
uint32_t old_cnt = m_item_count;
|
||||
|
||||
SetItem(src_slot->GetItemVnum(), src_slot->GetItemCount());
|
||||
if (old_vnum > 0) {
|
||||
src_slot->SetItem(old_vnum, old_cnt);
|
||||
} else {
|
||||
src_slot->ClearItem();
|
||||
}
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UISlot::GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) {
|
||||
if (!m_visible) return;
|
||||
|
||||
// 1. Slot frame
|
||||
uint32_t frame_color = m_is_selected ? 0xFFFFD700 : (m_is_hovered ? 0xFF888888 : 0xFF333333);
|
||||
UIRenderBatch batch;
|
||||
AppendQuad(batch, m_global_rect.x, m_global_rect.y, m_global_rect.width, m_global_rect.height,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, frame_color);
|
||||
|
||||
// Inner background
|
||||
AppendQuad(batch, m_global_rect.x + 1, m_global_rect.y + 1, m_global_rect.width - 2, m_global_rect.height - 2,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, m_bg_color);
|
||||
out_batches.push_back(std::move(batch));
|
||||
|
||||
// 2. Item Icon (if item exists)
|
||||
if (HasItem()) {
|
||||
UIRenderBatch icon_batch;
|
||||
icon_batch.texture_path = "icon/item_" + std::to_string(m_item_vnum) + ".tga";
|
||||
AppendQuad(icon_batch, m_global_rect.x + 2, m_global_rect.y + 2,
|
||||
m_global_rect.width - 4, m_global_rect.height - 4,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, 0xFFFFFFFF);
|
||||
out_batches.push_back(std::move(icon_batch));
|
||||
}
|
||||
|
||||
// 3. Cooldown Sweep Mask (if cooling down)
|
||||
if (m_cooldown_progress > 0.001f) {
|
||||
UIRenderBatch cd_batch;
|
||||
float cd_height = m_global_rect.height * m_cooldown_progress;
|
||||
// Semi-transparent dark overlay sweep
|
||||
AppendQuad(cd_batch, m_global_rect.x, m_global_rect.y + (m_global_rect.height - cd_height),
|
||||
m_global_rect.width, cd_height,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, 0xAA000000);
|
||||
out_batches.push_back(std::move(cd_batch));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "ui_core/ui_text.h"
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UIText::UIText() {
|
||||
m_bg_color = 0; // Transparent
|
||||
}
|
||||
|
||||
UIText::UIText(const std::string& text)
|
||||
: m_text(text) {
|
||||
m_bg_color = 0;
|
||||
}
|
||||
|
||||
void UIText::GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) {
|
||||
if (!m_visible || m_text.empty()) return;
|
||||
|
||||
// In zero-API core, Text emits placeholder glyph quads or metadata batch
|
||||
UIRenderBatch batch;
|
||||
batch.texture_path = "font/default.fnt";
|
||||
|
||||
float char_w = m_font_size * 0.6f;
|
||||
float char_h = m_font_size;
|
||||
float total_w = char_w * m_text.size();
|
||||
|
||||
float start_x = m_global_rect.x;
|
||||
if (m_alignment == TextAlignment::Center) {
|
||||
start_x += (m_global_rect.width - total_w) * 0.5f;
|
||||
} else if (m_alignment == TextAlignment::Right) {
|
||||
start_x += m_global_rect.width - total_w;
|
||||
}
|
||||
|
||||
float start_y = m_global_rect.y + (m_global_rect.height - char_h) * 0.5f;
|
||||
|
||||
for (size_t i = 0; i < m_text.size(); ++i) {
|
||||
AppendQuad(batch, start_x + i * char_w, start_y, char_w, char_h,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, m_text_color);
|
||||
}
|
||||
|
||||
out_batches.push_back(std::move(batch));
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "ui_core/ui_window.h"
|
||||
|
||||
namespace metin2::ui_core {
|
||||
|
||||
UIWindow::UIWindow() {
|
||||
m_bg_color = 0xEE1A1A1A; // Dark translucent default
|
||||
}
|
||||
|
||||
void UIWindow::OnMouseDown(UIEvent& event) {
|
||||
if (event.mouse_button == 0 && m_is_movable) {
|
||||
// Check if mouse clicked inside title bar
|
||||
Rect title_rect(m_global_rect.x, m_global_rect.y, m_global_rect.width, m_title_bar_height);
|
||||
if (title_rect.Contains(event.mouse_pos.x, event.mouse_pos.y)) {
|
||||
m_dragging_title_bar = true;
|
||||
m_drag_start_mouse = event.mouse_pos;
|
||||
m_drag_start_pos = {m_local_rect.x, m_local_rect.y};
|
||||
event.handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UIWindow::OnMouseMove(UIEvent& event) {
|
||||
if (m_dragging_title_bar) {
|
||||
float dx = event.mouse_pos.x - m_drag_start_mouse.x;
|
||||
float dy = event.mouse_pos.y - m_drag_start_mouse.y;
|
||||
m_local_rect.x = m_drag_start_pos.x + dx;
|
||||
m_local_rect.y = m_drag_start_pos.y + dy;
|
||||
|
||||
// If has parent, trigger update
|
||||
if (m_parent) {
|
||||
UpdateLayout(m_parent->GetGlobalRect());
|
||||
}
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UIWindow::OnMouseUp(UIEvent& event) {
|
||||
if (m_dragging_title_bar && event.mouse_button == 0) {
|
||||
m_dragging_title_bar = false;
|
||||
event.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UIWindow::GenerateRenderBatches(std::vector<UIRenderBatch>& out_batches) {
|
||||
if (!m_visible) return;
|
||||
|
||||
// 1. Draw base window background (Nine-Slice or quad)
|
||||
UIElement::GenerateRenderBatches(out_batches);
|
||||
|
||||
// 2. Draw Title Bar Header
|
||||
if (m_title_bar_height > 0.0f) {
|
||||
UIRenderBatch title_batch;
|
||||
uint32_t header_color = 0xFF2A2A2A;
|
||||
AppendQuad(title_batch, m_global_rect.x, m_global_rect.y, m_global_rect.width, m_title_bar_height,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, header_color);
|
||||
out_batches.push_back(std::move(title_batch));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metin2::ui_core
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user