Magic_Game/core/e2dutil.h

641 lines
10 KiB
C
Raw Normal View History

#pragma once
2018-09-05 13:33:39 +08:00
#include "e2dmacros.h"
namespace e2d
{
2017-10-17 21:22:25 +08:00
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD>
enum class Direction : int
2018-01-30 16:45:38 +08:00
{
2018-09-05 13:33:39 +08:00
Up, /* <20><> */
Down, /* <20><> */
Left, /* <20><> */
Right /* <20><> */
};
2018-01-30 16:45:38 +08:00
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0BDBB>ʽ
enum class Stroke : int
2018-01-30 16:45:38 +08:00
{
2018-09-05 13:33:39 +08:00
Miter = 0, /* б<><D0B1> */
Bevel = 1, /* б<><D0B1> */
Round = 2 /* Բ<><D4B2> */
};
2018-01-30 16:45:38 +08:00
2018-09-05 13:33:39 +08:00
class Size;
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD>
class Point
{
public:
float x; // X <20><><EFBFBD><EFBFBD>
float y; // Y <20><><EFBFBD><EFBFBD>
2018-09-05 13:33:39 +08:00
public:
Point();
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Point(
float x,
float y
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Point(
const Point& other
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Point operator + (const Point & other) const;
Point operator - (const Point & other) const;
Point operator * (float value) const;
Point operator / (float value) const;
Point operator - () const;
bool operator== (const Point& other) const;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
E2D_OP_EXPLICIT operator e2d::Size() const;
2018-09-05 13:33:39 +08:00
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static float Distance(
const Point& p1,
const Point& p2
);
};
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20><>С
class Size
{
public:
float width; // <20><><EFBFBD><EFBFBD>
float height; // <20>߶<EFBFBD>
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
public:
Size();
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Size(
float width,
float height
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Size(
const Size& other
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Size operator + (const Size & other) const;
Size operator - (const Size & other) const;
Size operator * (float value) const;
Size operator / (float value) const;
Size operator - () const;
bool operator== (const Size& other) const;
2018-09-05 13:33:39 +08:00
E2D_OP_EXPLICIT operator e2d::Point() const;
};
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD>
class Rect
{
public:
Point origin; // ԭ<><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Size size; // <20><><EFBFBD>Ⱥ͸߶<CDB8>
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
public:
Rect();
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect(
float x,
float y,
float width,
float height
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect(
const Point& pos,
const Size& size
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect(
const Rect& other
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect& operator= (const Rect& other);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
bool operator== (const Rect& rect) const;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20>жϵ<D0B6><CFB5>Ƿ<EFBFBD><C7B7>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD>
bool ContainsPoint(
const Point& point
) const;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>
bool Intersects(
const Rect& rect
) const;
};
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20>ַ<EFBFBD><D6B7><EFBFBD>
class String
{
public:
String();
2018-09-05 13:33:39 +08:00
String(
const String &
);
2018-09-05 13:33:39 +08:00
String(
const char *
);
String(
const wchar_t *
);
String(
String &&
);
~String();
// <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int GetLength() const;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD> Hash ֵ
size_t GetHash() const;
// <20>ж<EFBFBD><D0B6>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
bool IsEmpty() const;
// <20><>ȡָ<C8A1><D6B8>λ<EFBFBD><CEBB><EFBFBD>ַ<EFBFBD>
const wchar_t& At(
size_t index
) const;
// <20>Ƚ<EFBFBD><C8BD>ַ<EFBFBD><D6B7><EFBFBD>
int Compare(
const String & str
) const;
// <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
String Subtract(
int offset, /* ƫ<><C6AB><EFBFBD><EFBFBD> */
int count = -1 /* <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD> */
) const;
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
void Insert(
const String & str,
int pos
);
// <20><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Replace(
const String & from, /* <20><><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD> */
const String & to /* <20><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
);
// ɾ<><C9BE><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Erase(
int offset, /* ƫ<><C6AB><EFBFBD><EFBFBD> */
int count /* ɾ<><C9BE><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD> */
);
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
int Find(
const String & str, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int offset = 0 /* ƫ<><C6AB><EFBFBD><EFBFBD> */
) const;
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
void Clear();
// <20><>ȡ<EFBFBD><C8A1>д<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
String ToUpper() const;
// <20><>ȡСд<D0A1>ַ<EFBFBD><D6B7><EFBFBD>
String ToLower() const;
// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ int <20><>
int ToInt() const;
// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ float <20><>
float ToFloat() const;
// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ double <20><>
double ToDouble() const;
// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ bool <20><>
bool ToBool() const;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
static String Parse(int value);
static String Parse(unsigned int value);
static String Parse(float value);
static String Parse(double value);
// <20><>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
static String Format(const char * format, ...);
static String Format(const wchar_t * format, ...);
// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String& operator= (const String &);
String& operator= (const char *);
String& operator= (const wchar_t *);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String& operator+= (const String &);
String& operator+= (const char *);
String& operator+= (const wchar_t *);
String operator+ (const String &) const;
String operator+ (const char *) const;
String operator+ (const wchar_t *) const;
// <20><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
friend String operator+ (const char *, const String &);
friend String operator+ (const wchar_t*, const String &);
// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
E2D_OP_EXPLICIT operator const wchar_t* () const;
E2D_OP_EXPLICIT operator wchar_t* () const;
E2D_OP_EXPLICIT operator std::wstring() const;
E2D_OP_EXPLICIT operator std::string() const;
// <20>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool operator== (const String &) const;
bool operator== (const char *) const;
bool operator== (const wchar_t *) const;
bool operator!= (const String &) const;
bool operator!= (const char *) const;
bool operator!= (const wchar_t *) const;
bool operator> (const String &) const;
bool operator>= (const String &) const;
bool operator< (const String &) const;
bool operator<= (const String &) const;
// << <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
String& operator<< (const String &);
String& operator<< (const char *);
String& operator<< (char *);
String& operator<< (const wchar_t *);
String& operator<< (wchar_t *);
String& operator<< (int value);
String& operator<< (unsigned int value);
String& operator<< (float value);
String& operator<< (double value);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
wchar_t& operator[] (size_t);
friend std::ostream& operator<< (std::ostream &, const String &);
friend std::wostream& operator<< (std::wostream &, const String &);
friend std::istream& operator>> (std::istream &, String &);
friend std::wistream& operator>> (std::wistream &, String &);
private:
std::wstring string_;
};
// <20><>ɫ
class Color
{
public:
Color();
Color(
float r,
float g,
float b
);
Color(
float r,
float g,
float b,
float alpha
);
Color(
UINT rgb
);
Color(
UINT rgb,
float alpha
);
Color(
const D2D1_COLOR_F& color
);
E2D_OP_EXPLICIT operator D2D1_COLOR_F() const;
public:
enum Value : UINT
{
Black = 0x000000,
Blue = 0x0000FF,
BlueViolet = 0x8A2BE2,
Brown = 0xA52A2A,
Chocolate = 0xD2691E,
DarkBlue = 0x00008B,
DarkGray = 0xA9A9A9,
DarkGreen = 0x006400,
DarkOrange = 0xFF8C00,
DarkRed = 0x8B0000,
DarkViolet = 0x9400D3,
ForestGreen = 0x228B22,
Gold = 0xFFD700,
Gray = 0x808080,
Green = 0x008000,
GreenYellow = 0xADFF2F,
LightBlue = 0xADD8E6,
LightCyan = 0xE0FFFF,
LightGreen = 0x90EE90,
LightGray = 0xD3D3D3,
LightPink = 0xFFB6C1,
LightSeaGreen = 0x20B2AA,
LightSkyBlue = 0x87CEFA,
LightYellow = 0xFFFFE0,
Orange = 0xFFA500,
OrangeRed = 0xFF4500,
Pink = 0xFFC0CB,
Purple = 0x800080,
Red = 0xFF0000,
Silver = 0xC0C0C0,
SkyBlue = 0x87CEEB,
Snow = 0xFFFAFA,
Violet = 0xEE82EE,
Wheat = 0xF5DEB3,
White = 0xFFFFFF,
WhiteSmoke = 0xF5F5F5,
Wood = 0xDEB887,
Yellow = 0xFFFF00,
Yellow_Green = 0x9ACD32
};
public:
float r;
float g;
float b;
float a;
};
// <20><><EFBFBD>̼<EFBFBD>ֵ
enum class KeyCode : int
{
Unknown = 0,
Up = 0xC8,
Left = 0xCB,
Right = 0xCD,
Down = 0xD0,
Enter = 0x1C,
Space = 0x39,
Esc = 0x01,
Q = 0x10,
W = 0x11,
E = 0x12,
R = 0x13,
T = 0x14,
Y = 0x15,
U = 0x16,
I = 0x17,
O = 0x18,
P = 0x19,
A = 0x1E,
S = 0x1F,
D = 0x20,
F = 0x21,
G = 0x22,
H = 0x23,
J = 0x24,
K = 0x25,
L = 0x26,
Z = 0x2C,
X = 0x2D,
C = 0x2E,
V = 0x2F,
B = 0x30,
N = 0x31,
M = 0x32,
Num1 = 0x02,
Num2 = 0x03,
Num3 = 0x04,
Num4 = 0x05,
Num5 = 0x06,
Num6 = 0x07,
Num7 = 0x08,
Num8 = 0x09,
Num9 = 0x0A,
Num0 = 0x0B,
Numpad7 = 0x47,
Numpad8 = 0x48,
Numpad9 = 0x49,
Numpad4 = 0x4B,
Numpad5 = 0x4C,
Numpad6 = 0x4D,
Numpad1 = 0x4F,
Numpad2 = 0x50,
Numpad3 = 0x51,
Numpad0 = 0x52,
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
enum class MouseCode : int
{
Left, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
Right, /* <20><><EFBFBD><EFBFBD><EFBFBD>Ҽ<EFBFBD> */
Middle /* <20><><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD> */
};
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Function
{
public:
Function();
2018-09-05 13:33:39 +08:00
Function(
std::nullptr_t
);
2018-09-05 13:33:39 +08:00
Function(
std::function<void()> func
);
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
template<typename Func>
2018-09-10 20:55:20 +08:00
Function(Func func)
: func_(func)
{
}
2018-09-05 13:33:39 +08:00
template<typename Func, typename Object>
2018-09-10 20:55:20 +08:00
Function(Func&& func, Object&& obj)
: func_(std::bind(func, obj))
2018-09-05 13:33:39 +08:00
{
}
2018-09-05 13:33:39 +08:00
void operator() (void) const;
2018-09-05 13:33:39 +08:00
E2D_OP_EXPLICIT operator bool() const;
2018-09-05 13:33:39 +08:00
protected:
std::function<void()> func_;
};
2018-09-05 13:33:39 +08:00
// ʱ<><CAB1><EFBFBD><EFBFBD>
class Duration
{
public:
Duration();
2018-09-05 13:33:39 +08:00
explicit Duration(
float seconds
);
2018-04-27 17:07:47 +08:00
2018-09-05 13:33:39 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int Milliseconds() const;
2018-04-27 17:07:47 +08:00
2018-09-05 13:33:39 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
float Seconds() const;
2018-09-05 13:33:39 +08:00
bool operator== (const Duration &) const;
bool operator!= (const Duration &) const;
bool operator> (const Duration &) const;
bool operator>= (const Duration &) const;
bool operator< (const Duration &) const;
bool operator<= (const Duration &) const;
2018-09-05 13:33:39 +08:00
Duration operator + (Duration const &) const;
Duration operator - (Duration const &) const;
2018-09-02 14:30:48 +08:00
2018-09-05 13:33:39 +08:00
Duration& operator += (Duration const &);
Duration& operator -= (Duration const &);
2018-09-02 14:30:48 +08:00
2018-09-05 13:33:39 +08:00
protected:
std::chrono::milliseconds duration_ms_;
};
2018-09-02 14:30:48 +08:00
2018-09-05 13:33:39 +08:00
// ʱ<><CAB1><EFBFBD><EFBFBD>
class Time
{
public:
Time();
2018-01-30 16:45:38 +08:00
2018-09-05 13:33:39 +08:00
// <20><>ȡʱ<C8A1><CAB1><EFBFBD><EFBFBD>
time_t GetTimeStamp() const;
2017-10-14 01:07:34 +08:00
2018-09-05 13:33:39 +08:00
// <20>Ƿ<EFBFBD><C7B7><EFBFBD>
bool IsZero() const;
2018-09-05 13:33:39 +08:00
Time operator + (Duration const &) const;
Time operator - (Duration const &) const;
2018-09-05 13:33:39 +08:00
Time& operator += (Duration const &);
Time& operator -= (Duration const &);
2017-10-17 21:22:25 +08:00
2018-09-05 13:33:39 +08:00
Duration operator - (Time const &) const;
2017-10-17 21:22:25 +08:00
2018-09-05 13:33:39 +08:00
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
static Time Now();
2017-10-21 19:09:31 +08:00
2018-09-05 13:33:39 +08:00
protected:
std::chrono::steady_clock::time_point time_;
};
2017-10-21 19:09:31 +08:00
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD>
class Font
{
public:
String family; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float size; // <20>ֺ<EFBFBD>
UINT weight; // <20><>ϸֵ
bool italic; // <20>Ƿ<EFBFBD>б<EFBFBD><D0B1>
public:
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸֵ
enum Weight : UINT
{
Thin = 100,
ExtraLight = 200,
Light = 300,
Normal = 400,
Medium = 500,
Bold = 700,
ExtraBold = 800,
Black = 900,
ExtraBlack = 950
};
public:
Font();
explicit Font(
const String& family,
float size = 22,
UINT weight = Font::Weight::Normal,
bool italic = false
);
};
// <20><>Դ
class Resource
{
public:
Resource(
int resource_id, /* <20><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD> */
2018-09-05 13:33:39 +08:00
const String& resource_type /* <20><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD> */
);
2018-04-24 09:02:06 +08:00
2018-09-05 13:33:39 +08:00
public:
int id;
2018-09-05 13:33:39 +08:00
String type;
};
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Ref
{
public:
Ref();
2018-09-05 13:33:39 +08:00
virtual ~Ref();
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>
2018-09-07 00:28:54 +08:00
LONG Retain();
2018-09-05 13:33:39 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>
2018-09-07 00:28:54 +08:00
LONG Release();
2018-09-05 13:33:39 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>
2018-09-07 00:28:54 +08:00
LONG GetRefCount() const;
2017-10-14 18:43:32 +08:00
2018-09-05 13:33:39 +08:00
protected:
E2D_DISABLE_COPY(Ref);
2017-10-14 18:43:32 +08:00
2018-09-07 00:28:54 +08:00
protected:
LONG ref_count_;
2018-09-05 13:33:39 +08:00
};
2018-09-07 00:28:54 +08:00
template<class Interface>
inline void SafeRelease(Interface*& p)
{
if (p != nullptr)
{
p->Release();
p = nullptr;
}
}
2018-09-05 13:33:39 +08:00
}