forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editablereg.h
88 lines (76 loc) · 2.05 KB
/
editablereg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#include "robin_hood.h"
typedef IEditable*(*CreateFuncType)();
typedef IEditable*(*CreateAndInitFuncType)(PinTable *pt, float x, float y);
struct EditableInfo
{
ItemTypeEnum type;
int typeNameID;
int toolID;
int cursorID;
unsigned int allowedViews;
CreateFuncType createFunc;
CreateAndInitFuncType createAndInitFunc;
};
class EditableRegistry
{
public:
template <class T>
static void RegisterEditable()
{
EditableInfo ei;
ei.type = T::ItemType;
ei.typeNameID = T::TypeNameID;
ei.toolID = T::ToolID;
ei.cursorID = T::CursorID;
ei.allowedViews = T::AllowedViews;
ei.createFunc = &T::COMCreateEditable;
ei.createAndInitFunc = &T::COMCreateAndInit;
m_map[ei.type] = ei;
}
static IEditable* Create(ItemTypeEnum type)
{
return FindOrFail(type)->createFunc();
}
static IEditable* CreateAndInit(ItemTypeEnum type, PinTable *pt, float x, float y)
{
return FindOrFail(type)->createAndInitFunc(pt, x, y);
}
static int GetTypeNameStringID(ItemTypeEnum type)
{
return FindOrFail(type)->typeNameID;
}
static ItemTypeEnum TypeFromToolID(int toolID)
{
for (robin_hood::unordered_map<ItemTypeEnum, EditableInfo>::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
{
if (it->second.toolID == toolID)
return it->second.type;
}
return eItemInvalid;
}
static int GetCursorID(ItemTypeEnum type)
{
return FindOrFail(type)->cursorID;
}
static unsigned int GetAllowedViews(ItemTypeEnum type)
{
return FindOrFail(type)->allowedViews;
}
private:
static robin_hood::unordered_map<ItemTypeEnum, EditableInfo> m_map;
static EditableInfo* FindOrFail(ItemTypeEnum type)
{
const robin_hood::unordered_map<ItemTypeEnum, EditableInfo>::iterator it = m_map.find(type);
if (it == m_map.end())
{
ShowError("Editable type not found.");
assert(false);
return nullptr;
}
else
{
return &it->second;
}
}
};