///
/// This file is part of ILNumerics Community Edition.
///
/// ILNumerics Community Edition - high performance computing for applications.
/// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
///
/// ILNumerics Community Edition is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License version 3 as published by
/// the Free Software Foundation.
///
/// ILNumerics Community Edition is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with ILNumerics Community Edition. See the file License.txt in the root
/// of your distribution package. If not, see .
///
/// In addition this software uses the following components and/or licenses:
///
/// =================================================================================
/// The Open Toolkit Library License
///
/// Copyright (c) 2006 - 2009 the Open Toolkit library.
///
/// 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.
///
/// =================================================================================
///
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using ILNumerics.Drawing.Interfaces;
using ILNumerics.Exceptions;
using ILNumerics.Drawing;
namespace ILNumerics.Drawing.Labeling {
///
/// Provides access to all (device dependent) ILRenderer available
///
public class ILRendererManager {
#region members / properties
Dictionary m_rendererCollection;
Dictionary m_rendererCache;
GraphicDeviceType m_graphicsDevice;
string m_defaultRendererScreen;
string m_defaultRendererWorld;
ILPanel m_panel;
#endregion
#region constructor
///
/// create manager instance, device dependent
///
/// the hosting panel, will give access to this manager instance
/// While creating ILTextRendereManager instances, the executing assembly will be queried for
/// available classes matching the device. Corresponding types are than provided by calling the GetRenderer() method.
public ILRendererManager (ILPanel panel)
: this(panel,new Assembly[] { Assembly.GetExecutingAssembly()} ) {
}
///
/// create manager instance, device dependent
///
/// The hosting panel, giving access to his manager instance
/// assemblies to query for matching ILRenderer types
/// While creating ILRendererManager instances, the given assemblies will be queried for
/// available classes matching the device. Corresponding types are than provided by calling the
/// GetRenderer() method.
public ILRendererManager (ILPanel panel, Assembly[] assemblies) {
m_graphicsDevice = panel.GraphicDeviceType;
m_panel = panel;
m_rendererCollection = new Dictionary();
m_rendererCache = new Dictionary();
foreach (Assembly ass in assemblies) {
AddAssemblyTypes(ass);
}
if (m_rendererCollection.Count == 0)
throw new InvalidOperationException("No valid ILRenderer found!");
}
#endregion
#region public interface
///
/// Create a new ILRenderer instance, matching the current graphics device
///
/// full class name of the new text renderer
/// assembly hosting the textrenderer, null for executing assembly
/// newly created ILRenderer instance from assembly
public IILTextRenderer CreateInstance (string typeName, Assembly assembly) {
if (typeName == null)
throw new ILArgumentException ("The type must be a valid ILTextRenderer!");
string key = (assembly==null)
? typeName : assembly.FullName + "|" + typeName;
if (m_rendererCache.ContainsKey(key))
return m_rendererCache[key];
if (assembly == null)
assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType(typeName);
if (type == null || type.GetInterface("IILTextRenderer") == null)
throw new ILArgumentException (String.Format(
"The type '{0}' was not found in assembly {1} or is not a valid IILTextRenderer!"
,typeName,assembly));
queryAddRenderer(type);
if (!m_rendererCollection.ContainsKey(key))
throw new ILArgumentException("The renderer could not be loaded. Make sure, to specify the correct assembly and the type is properly decorated by the ILRendererAttribute!");
IILTextRenderer ret = (IILTextRenderer)type.InvokeMember(
typeName, BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null,
new object[] {m_panel});
if (!m_rendererCache.ContainsKey(typeName)) {
m_rendererCache[typeName] = ret;
}
return ret;
}
///
/// Create the default instance of IILTextRenderer for
/// the current graphics device and a coord system
///
/// coord system, the renderer is
/// specialized and the default renderer for
/// default IILTextRenderer object
public IILTextRenderer GetDefault(CoordSystem coords) {
string key = (coords == CoordSystem.World3D)
? m_defaultRendererWorld : m_defaultRendererScreen;
if (m_rendererCache.ContainsKey(key) && m_rendererCache[key] != null)
return m_rendererCache[key];
// must create a new instance
IILTextRenderer ret;
switch (coords) {
case CoordSystem.World3D:
ret = CreateInstance(m_defaultRendererWorld,null);
break;
default:
ret = CreateInstance(m_defaultRendererScreen,null);
break;
}
m_rendererCache[key] = ret;
return ret;
}
///
/// Get a collection of all IILTextRenderer types and corresponding displayNames available
///
public IDictionary RendererNames {
get {
IDictionary ret = new Dictionary();
foreach (KeyValuePair v in m_rendererCollection) {
ret.Add(v);
}
return ret;
}
}
#endregion
#region private helper
///
/// iterate over types attribute, select IILTextRenderer attribute,
/// register for later reference, add/overwrite default renderers
///
/// IILTextRenderer type
private void queryAddRenderer(Type t)
{
foreach (object att in t.GetCustomAttributes(false)) {
if (att is ILRendererAttribute) {
ILRendererAttribute trAttr = (ILRendererAttribute)att;
if (trAttr.GraphicDeviceType == m_graphicsDevice ||
trAttr.GraphicDeviceType == GraphicDeviceType.GDI) {
if (t.GetInterface("IILTextRenderer") == null)
continue;
if (m_rendererCollection.ContainsKey(t.FullName))
m_rendererCollection[t.FullName] = trAttr.Name;
else
m_rendererCollection.Add(t.FullName,trAttr.Name);
if (trAttr.IsDefault) {
if (trAttr.Coords == CoordSystem.Screen)
m_defaultRendererScreen = t.FullName;
else if (trAttr.Coords == CoordSystem.World3D)
m_defaultRendererWorld = t.FullName;
}
break;
}
}
}
}
private void AddAssemblyTypes (Assembly assembly) {
System.Diagnostics.Trace.Write("Entering ILTextRendererManager.AddAssemblyTypes...");
m_rendererCollection.Add("ILNumerics.Drawing.Platform.OpenGL.ILOGLRenderer", "OpenGL Renderer");
m_defaultRendererScreen = "ILNumerics.Drawing.Platform.OpenGL.ILOGLRenderer";
m_defaultRendererWorld = "ILNumerics.Drawing.Platform.OpenGL.ILOGLRenderer";
try {
Type[] types = assembly.GetTypes();
foreach (Type t in types) {
queryAddRenderer(t);
}
System.Diagnostics.Trace.WriteLine("success. " + m_rendererCollection.Count.ToString() + " renderer found.");
System.Diagnostics.Trace.Indent();
foreach (var i in m_rendererCollection) {
System.Diagnostics.Trace.WriteLine(i.Key.ToString() + " -- " + i.Value);
}
System.Diagnostics.Trace.Unindent();
} catch (Exception exc) {
System.Diagnostics.Trace.WriteLine("failure. Exception information:");
System.Diagnostics.Trace.WriteLine(exc.ToString());
}
}
#endregion
}
}