plugin.h 4.29 KB
Newer Older
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
#ifndef _PLUGIN_H_
#define _PLUGIN_H_

#include <QtPlugin>

#include <iostream>
#include <list>

#include "window.h"
#include "system.h"
#include "view.h"
#include "camera.h"
#include "mapHandler.h"
#include "vboHandler.h"
#include "scene.h"

class Plugin
{
public:
	enum { UNLIMITED_NUMBER_OF_MAPS = -1 };
	enum { UNLIMITED_NUMBER_OF_SCENES = -1 };

	virtual ~Plugin()
	{}

	virtual void cb_updateMatrix(View* view) = 0;
	virtual void cb_redraw(Scene* scene) = 0;
	virtual void cb_initGL(Scene* scene) = 0;

	void updateGL()	= 0;
	void updateGL(Scene* scene)	= 0;

	virtual bool cb_keyPress(Scene* scene, int event) = 0;
	virtual bool cb_keyRelease(Scene* scene, int event) = 0;
	virtual bool cb_mousePress(Scene* scene, int button, int x, int y) = 0;
	virtual bool cb_mouseRelease(Scene* scene, int button, int x, int y) = 0;
	virtual bool cb_mouseClick(Scene* scene, int button, int x, int y) = 0;
	virtual bool cb_mouseMove(Scene* scene, int buttons, int x, int y) = 0;
	virtual bool cb_wheelEvent(Scene* scene, int delta, int x, int y) = 0;

	virtual void cb_recievedMap(MapHandler* map) = 0;
	virtual void cb_removingMap(MapHandler* map) = 0;

	virtual bool isWaitingForScene() = 0;
	virtual void recieveScene(Scene* scene) = 0;
	virtual void deleteLinkWithScene(Scene* scene) = 0;
	virtual bool hasManualLinkWithScene(Scene* scene) = 0;

	const QString& getName() { return m_name; }
	void setName(QString name) { m_name = name; }

	const QString& getPluginFilePath() { return m_pluginPathFile; }
	void setPluginFilePath(QString path) { m_pluginPathFile = path; }

	Window* getWindow() { return m_window; }
	void setWindow(Window* window) { m_window = window; }

	virtual bool activate(bool initializing)
	{
		m_windowInitializing = initializing;
		bool r = activate();
		m_windowInitializing = false;
		return r;
	}

	virtual void activate() = 0;
	virtual void disable() = 0;

	void addDependantPlugin(Plugin* p) { l_dependantPlugins.push_back(p); }
	void removeDependantPlugin(Plugin* p) { l_dependantPlugins.removeAll(p); }
	void removeAllDependantPlugins() { l_dependantPlugins.clear(); }
	bool hasDependantPlugins() { return !l_dependantPlugins.isEmpty(); }
	QStringList getDependantPluginNames()
	{
		QStringList l;
		for(QList<Plugin*>::iterator it = l_dependantPlugins.begin(); it != l_dependantPlugins.end(); ++it)
			l.push_back((*it)->getName());
		return l;
	}

	Plugin* addDependency(QString dependency)
	{
		Plugin* plugin;
		if((plugin = m_window->checkPluginDependency(dependency, this)))
		{
			l_dependencies.push_back(plugin);
			return plugin;
		}
		else
			return NULL;
	}

	template<typename T>
	T* getDependency(QString dependency)
	{
		return (T*)(this->addDependency(dependency));
	}

	virtual void removeDependencyLink(Plugin* p)
	{
		p->removeDependantPlugin(this);
		l_dependencies.removeAll(p);
	}
	virtual void removeAllDependencyLinks()
	{
		foreach(Plugin* p, l_dependencies)
			p->removeDependantPlugin(this);
		l_dependencies.clear();
	}
	void removeDependantLink(Plugin* p)
	{
		int i = l_dependantPlugins.indexOf(p);
		if(i >= 0)
			p->removeDependencyLink(this);
	}
	void removeAllDependantLinks()
	{
		foreach(Plugin* p, l_dependantPlugins)
			p->removeDependencyLink(this);
	}

	virtual void unloadDependantPlugins()
	{
		while(!l_dependantPlugins.empty())
		{
			if(l_dependantPlugins.back())
				m_window->unloadPlugin(l_dependantPlugins.back()->getName());
		}
	}

	bool addMap(MapHandler* map)
	{
		if((m_mapNumber == UNLIMITED_NUMBER_OF_MAPS || l_map.size() < m_mapNumber) && map && !l_map.contains(map))
		{
			l_map.push_back(map);
			cb_recievedMap(map);
			return true;
		}
		else
			return false;
	}
	MapHandler* takeMap(MapHandler* map)
	{
		int i = l_map.indexOf(map);
		if(i >= 0)
		{
			cb_removingMap(map);
			return l_map.takeAt(i);
		}
		else
			return NULL;
	}
	QList<MapHandler*> workingMaps() { return l_map; }
	void setMapNumber(int n)
	{
		if(n >= l_map.size() || n == UNLIMITED_NUMBER_OF_MAPS)
			m_mapNumber = n;
	}
	virtual int remainingMapNumber()
	{
		return m_mapNumber != UNLIMITED_NUMBER_OF_MAPS ? m_mapNumber - l_map.size() : m_mapNumber;
	}

protected:
	Window* m_window;
	QString m_name;
	QString m_pluginPathFile;
	QList<Plugin*> l_dependencies;
	QList<Plugin*> l_dependantPlugins;
	bool m_windowInitializing;
	int m_mapNumber;
	QList<MapHandler*> l_map;
};

Q_DECLARE_INTERFACE(Plugin, "Window.Plugin")

#endif