File: Plugin.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (409 lines) | stat: -rw-r--r-- 10,748 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include "Plugin.h"
#include "../../Include/Rocket/Core/Types.h"
#include "../../Include/Rocket/Core.h"
#include "ElementContextHook.h"
#include "ElementInfo.h"
#include "ElementLog.h"
#include "FontSource.h"
#include "Geometry.h"
#include "MenuSource.h"
#include "SystemInterface.h"
#include <stack>

namespace Rocket {
namespace Debugger {

Plugin* Plugin::instance = NULL;

Plugin::Plugin()
{
	ROCKET_ASSERT(instance == NULL);
	instance = this;
	host_context = NULL;
	debug_context = NULL;
	log_hook = NULL;

	menu_element = NULL;
	info_element = NULL;
	log_element = NULL;

	render_outlines = false;
}

Plugin::~Plugin()
{
	instance = NULL;
}

// Initialises the debugging tools into the given context.
bool Plugin::Initialise(Core::Context* context)
{
	host_context = context;
	Geometry::SetContext(context);

	if (!LoadFont())
	{
		Core::Log::Message(Core::Log::LT_ERROR, "Failed to initialise debugger, unable to load font.");
		return false;
	}

	if (!LoadMenuElement() ||
		!LoadInfoElement() ||
		!LoadLogElement())
	{
		Core::Log::Message(Core::Log::LT_ERROR, "Failed to initialise debugger, error while load debugger elements.");
		return false;
	}

	Core::Factory::RegisterElementInstancer("debug-hook", new Core::ElementInstancerGeneric< ElementContextHook >())->RemoveReference();

	return true;
}

// Sets the context to be debugged.
bool Plugin::SetContext(Core::Context* context)
{
	// Remove the debug hook from the old context.
	if (debug_context != NULL &&
		hook_element != NULL)
	{
		debug_context->UnloadDocument(hook_element);
		hook_element->RemoveReference();
		hook_element = NULL;
	}

	// Add the debug hook into the new context.
	if (context != NULL)
	{
		Core::ElementDocument* element = context->CreateDocument("debug-hook");
		if (element == NULL)
			return false;

		hook_element = dynamic_cast< ElementContextHook* >(element);
		if (hook_element == NULL)
		{
			element->RemoveReference();
			context->UnloadDocument(element);
			return false;
		}

		hook_element->Initialise(this);
	}

	// Attach the info element to the new context.
	if (info_element != NULL)
	{
		if (debug_context != NULL)
		{
			debug_context->RemoveEventListener("click", info_element, true);
			debug_context->RemoveEventListener("mouseover", info_element, true);
		}

		if (context != NULL)
		{
			context->AddEventListener("click", info_element, true);
			context->AddEventListener("mouseover", info_element, true);
		}

		info_element->Reset();
	}

	debug_context = context;
	return true;
}

// Sets the visibility of the debugger.
void Plugin::SetVisible(bool visibility)
{
	if (visibility)
		menu_element->SetProperty("visibility", "visible");
	else
		menu_element->SetProperty("visibility", "hidden");
}

// Returns the visibility of the debugger.
bool Plugin::IsVisible()
{
	return menu_element->IsVisible();
}

// Renders any debug elements in the debug context.
void Plugin::Render()
{
	// Render the outlines of the debug context's elements.
	if (render_outlines &&
		debug_context != NULL)
	{
		for (int i = 0; i < debug_context->GetNumDocuments(); ++i)
		{
			Core::ElementDocument* document = debug_context->GetDocument(i);
			if (document->GetId().Find("rkt-debug-") == 0)
				continue;

			std::stack< Core::Element* > element_stack;
			element_stack.push(document);

			while (!element_stack.empty())
			{
				Core::Element* element = element_stack.top();
				element_stack.pop();
				if (element->IsVisible())
				{
					for (int j = 0; j < element->GetNumBoxes(); ++j)
					{
						const Core::Box& box = element->GetBox(j);
						Geometry::RenderOutline(element->GetAbsoluteOffset(Core::Box::BORDER) + box.GetPosition(Core::Box::BORDER), box.GetSize(Core::Box::BORDER), Core::Colourb(255, 0, 0, 128), 1);
					}

					for (int j = 0; j < element->GetNumChildren(); ++j)
						element_stack.push(element->GetChild(j));
				}
			}
		}
	}

	// Render the info element's boxes.
	if (info_element != NULL &&
		info_element->IsVisible())
	{
		info_element->RenderHoverElement();
		info_element->RenderSourceElement();
	}
}

// Called when Rocket shuts down.
void Plugin::OnShutdown()
{
	// Release the elements before we leak track, this ensures the debugger hook has been cleared
	// and that we don't try send the messages to the debug log window
	ReleaseElements();

	if (!elements.empty())
	{
		Core::Log::Message(Core::Log::LT_WARNING, "%u leaked elements detected.", elements.size());

		int count = 0;
		for (ElementInstanceMap::iterator i = elements.begin(); i != elements.end(); ++i)
			Core::Log::Message(Core::Log::LT_WARNING, "\t(%d) %s -> %s", count++, (*i)->GetTagName().CString(), (*i)->GetAddress().CString());
	}

	delete this;
}

// Called whenever a Rocket context is destroyed.
void Plugin::OnContextDestroy(Core::Context* context)
{
	if (context == debug_context)
	{
		// The context we're debugging is being destroyed, so we need to remove our debug hook elements.
		SetContext(NULL);
	}

	if (context == host_context)
	{
		// Our host is being destroyed, so we need to shut down the debugger.

		ReleaseElements();

		Geometry::SetContext(NULL);
		context = NULL;
	}
}

// Called whenever an element is created.
void Plugin::OnElementCreate(Core::Element* element)
{
	// Store the stack addresses for this frame.
	elements.insert(element);
}

// Called whenever an element is destroyed.
void Plugin::OnElementDestroy(Core::Element* element)
{
	elements.erase(element);

	if (info_element != NULL)
		info_element->OnElementDestroy(element);
}

// Event handler for events from the debugger elements.
void Plugin::ProcessEvent(Core::Event& event)
{
	if (event == "click")
	{
		if (event.GetTargetElement()->GetId() == "event-log-button")
		{
			if (log_element->IsVisible())
				log_element->SetProperty("visibility", "hidden");
			else
				log_element->SetProperty("visibility", "visible");
		}
		else if (event.GetTargetElement()->GetId() == "debug-info-button")
		{
			if (info_element->IsVisible())
				info_element->SetProperty("visibility", "hidden");
			else
				info_element->SetProperty("visibility", "visible");
		}
		else if (event.GetTargetElement()->GetId() == "outlines-button")
		{
			render_outlines = !render_outlines;
		}
	}
}

Plugin* Plugin::GetInstance()
{
	return instance;
}

bool Plugin::LoadFont()
{
	return (Core::FontDatabase::LoadFontFace(Core::FontDatabase::FreeType, lacuna_regular, sizeof(lacuna_regular) / sizeof(unsigned char), "Lacuna", Core::Font::STYLE_NORMAL, Core::Font::WEIGHT_NORMAL) &&
			Core::FontDatabase::LoadFontFace(Core::FontDatabase::FreeType, lacuna_italic, sizeof(lacuna_italic) / sizeof(unsigned char), "Lacuna", Core::Font::STYLE_ITALIC, Core::Font::WEIGHT_NORMAL));
}

bool Plugin::LoadMenuElement()
{
	menu_element = host_context->CreateDocument();
	if (menu_element == NULL)
		return false;

	menu_element->SetId("rkt-debug-menu");
	menu_element->SetProperty("visibility", "hidden");
	menu_element->SetInnerRML(menu_rml);

	// Remove our reference on the document.
	menu_element->RemoveReference();

	Core::StyleSheet* style_sheet = Core::Factory::InstanceStyleSheetString(menu_rcss);
	if (style_sheet == NULL)
	{
		host_context->UnloadDocument(menu_element);
		menu_element = NULL;

		return false;
	}

	menu_element->SetStyleSheet(style_sheet);
	style_sheet->RemoveReference();
	menu_element->AddReference();

	// Set the version info in the menu.
	menu_element->GetElementById("version-number")->SetInnerRML("v" + Rocket::Core::GetVersion());

	// Attach to the buttons.
	Core::Element* event_log_button = menu_element->GetElementById("event-log-button");
	event_log_button->AddEventListener("click", this);

	Core::Element* element_info_button = menu_element->GetElementById("debug-info-button");
	element_info_button->AddEventListener("click", this);

	Core::Element* outlines_button = menu_element->GetElementById("outlines-button");
	outlines_button->AddEventListener("click", this);

	return true;
}

bool Plugin::LoadInfoElement()
{
	Core::Factory::RegisterElementInstancer("debug-info", new Core::ElementInstancerGeneric< ElementInfo >())->RemoveReference();
	info_element = dynamic_cast< ElementInfo* >(host_context->CreateDocument("debug-info"));
	if (info_element == NULL)
		return false;

	info_element->SetProperty("visibility", "hidden");

	if (!info_element->Initialise())
	{
		info_element->RemoveReference();
		host_context->UnloadDocument(info_element);
		info_element = NULL;

		return false;
	}

	return true;
}

bool Plugin::LoadLogElement()
{
	Core::Factory::RegisterElementInstancer("debug-log", new Core::ElementInstancerGeneric< ElementLog >())->RemoveReference();
	log_element = dynamic_cast< ElementLog* >(host_context->CreateDocument("debug-log"));
	if (log_element == NULL)
		return false;

	log_element->SetProperty("visibility", "hidden");

	if (!log_element->Initialise())
	{
		log_element->RemoveReference();
		host_context->UnloadDocument(log_element);
		log_element = NULL;

		return false;
	}

	// Make the system interface; this will trap the log messages for us.
	log_hook = new SystemInterface(log_element);

	return true;
}

void Plugin::ReleaseElements()
{
	if (menu_element)
	{
		menu_element->RemoveReference();
		menu_element = NULL;
	}

	if (info_element)
	{
		info_element->RemoveReference();
		info_element = NULL;
	}

	if (log_element)
	{
		log_element->RemoveReference();
		log_element = NULL;
		delete log_hook;
	}

	if (hook_element)
	{
		hook_element->RemoveReference();
		hook_element = NULL;
	}
}

}
}