-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocketmenu.cpp
More file actions
171 lines (141 loc) · 4.54 KB
/
rocketmenu.cpp
File metadata and controls
171 lines (141 loc) · 4.54 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "rocketmenu.h"
#include <Rocket/Core.h>
#include <unordered_map>
#include <string>
#define GL_CLAMP_TO_EDGE 0x812F
Rocket::Core::Context* rContext;
std::unordered_map<std::string, LrtClickFunc> listeners;
std::unordered_map<std::string, void* > listener_data;
std::unordered_map<std::string, Rocket::Core::ElementDocument* > documents;
static ShellRenderInterfaceOpenGL opengl_renderer;
static ShellSystemInterface system_interface;
static Rocket::Core::ElementDocument* active_document;
void rocketLoop()
{
rContext->Update();
rContext->Render();
}
void registerListener( std::string ID, LrtClickFunc listener, void* data )
{
listeners[ID] = listener;
listener_data[ID] = data;
}
void registerListener( char* ID, LrtClickFunc listener, void* data )
{
registerListener( std::string( ID ), listener, data );
}
class LrtButtonListener : public Rocket::Core::EventListener
{
public:
void ProcessEvent(Rocket::Core::Event& event)
{
const char* aID = event.GetCurrentElement()->GetAttribute<Rocket::Core::String>("action", "do nothing").CString();
const char* eID = event.GetCurrentElement()->GetId().CString();
dprintf(1, "Processing action \"%s\" in button element \"%s\"\n", aID, eID );
std::string ID( aID );
LrtClickFunc listen = listeners[ ID ];
if( listen == NULL )
{
dprintf( 0, "Invalid listener\n" );
}
else
{
listen( listener_data[ ID ] );
}
}
};
class LrtLinkListener : public Rocket::Core::EventListener
{
public:
void ProcessEvent(Rocket::Core::Event& event)
{
const char* aID = event.GetCurrentElement()->GetAttribute<Rocket::Core::String>("target", "invalid").CString();
const char* eID = event.GetCurrentElement()->GetId().CString();
dprintf(1, "Processing link to \"%s\" in element \"%s\"\n", aID, eID );
std::string ID( aID );
Rocket::Core::ElementDocument* target = documents[ ID ];
if( target == NULL )
{
dprintf( 0, "Invalid target\n" );
}
else
{
active_document->Hide();
active_document = target;
active_document->Show();
}
}
};
static LrtButtonListener* lrtButtonListener;
static LrtLinkListener* lrtLinkListener;
static void loadDocument(const char* name, const char* ID)
{
dprintf( 3, "Loading document \"%s\" with ID \"%s\"\n", name, ID );
Rocket::Core::ElementDocument* document = rContext->LoadDocument(name);
document->SetId(ID);
if (document != NULL)
{
document->GetElementById("title")->SetInnerRML(document->GetTitle());
document->Hide();
document->RemoveReference();
}
Rocket::Core::ElementList links;
Rocket::Core::ElementList buttons;
document->GetElementsByTagName(links, "link");
document->GetElementsByTagName(buttons, "button");
for( auto &link : links )
{
dprintf( 5, "Listening to a link with ID \"%s\"\n", link->GetId().CString() );
link->AddEventListener("click", lrtLinkListener, false);
}
for( auto &button : buttons )
{
dprintf( 5, "Listening to a button with ID \"%s\"\n", button->GetId().CString() );
button->AddEventListener("click", lrtButtonListener, false);
}
documents[ std::string( ID ) ] = document;
//document->GetElementById("main_exit")->AddEventListener("click", lrtButtonListener, false);
}
void initRocketMenu( void )
{
// Generic initialization
if (!Shell::Initialise("./"))
{
Shell::Shutdown();
}
// Librocket requires this, i have no idea what this actually does.
// I presume i'll need to rerun this if i need to change resolution.
glClearColor(0, 0, 0, 1);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1024, 768, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Rocket initialisation.
Rocket::Core::SetRenderInterface(&opengl_renderer);
opengl_renderer.SetViewport(1024, 768);
Rocket::Core::SetSystemInterface(&system_interface);
Rocket::Core::Initialise();
// Create the main Rocket context and set it on the shell's input layer.
rContext = Rocket::Core::CreateContext("soos", Rocket::Core::Vector2i(1024, 768));
if (rContext == NULL)
{
Rocket::Core::Shutdown();
Shell::Shutdown();
}
Rocket::Debugger::Initialise(rContext);
Input::SetContext(rContext);
Shell::LoadFonts("assets/");
// Create the common listener
lrtButtonListener = new LrtButtonListener();
lrtLinkListener = new LrtLinkListener();
// Load and show the tutorial document.
loadDocument( "assets/Main Menu.rml", "main" );
loadDocument( "assets/Map Editor.rml", "maped" );
active_document = documents[ std::string( "main" ) ];
active_document->Show();
}