#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nuclex.Graphics.Batching;
namespace Nuclex.Fonts {
/// Drawing context for drawing text with vector fonts
internal class TextDrawContext : DrawContext {
/// Initializes a new text draw context
/// Effect that will be used to render the text
/// Transformation matrix for the text
/// Drawing color of the text
public TextDrawContext(Effect effect, Matrix transform, Color textColor) {
this.effect = effect;
this.transform = transform;
this.textColor = textColor;
}
/// Number of passes this draw context requires for rendering
public override int Passes {
get { return this.effect.CurrentTechnique.Passes.Count; }
}
#if XNA_3
/// Begins the drawing cycle
public override void Begin() {
this.effect.Parameters["ViewProjection"].SetValue(this.transform);
this.effect.Parameters["TextColor"].SetValue(this.textColor.ToVector4());
this.effect.Begin(SaveStateMode.SaveState);
}
/// Ends the drawing cycle
public override void End() {
this.effect.End();
}
/// Prepares the graphics device for drawing
/// Index of the pass to begin rendering
///
/// Should only be called between the normal Begin() and End() methods.
///
public override void BeginPass(int pass) {
this.effect.CurrentTechnique.Passes[pass].Begin();
this.currentPass = pass;
}
/// Restores the graphics device after drawing has finished
///
/// Should only be called between the normal Begin() and End() methods.
///
public override void EndPass() {
this.effect.CurrentTechnique.Passes[this.currentPass].End();
}
#else
/// Prepares the graphics device for drawing
/// Index of the pass to begin rendering
public override void Apply(int pass) {
#if WINDOWS_PHONE
BasicEffect basicEffect = this.effect as BasicEffect;
//basicEffect.World = Matrix.Identity;
//basicEffect.View = Matrix.Identity;
basicEffect.Projection = this.transform;
basicEffect.Alpha = (float)this.textColor.A / 255.0f;
basicEffect.DiffuseColor = this.textColor.ToVector3();
#else
this.effect.Parameters["ViewProjection"].SetValue(this.transform);
this.effect.Parameters["TextColor"].SetValue(this.textColor.ToVector4());
#endif
this.effect.CurrentTechnique.Passes[pass].Apply();
}
#endif
/// Tests whether another draw context is identical to this one
/// Other context to check for equality
/// True if the other context is identical to this one
public override bool Equals(DrawContext otherContext) {
TextDrawContext other = otherContext as TextDrawContext;
if(other == null)
return false;
Effect thisEffect = this.effect;
Effect otherEffect = other.effect;
// If the same effect instances are different, we stop comparing right here.
// This context is specialized to run the same effect in multiple configurations,
// different effects with identical settings will not happen due to its usage.
if(!ReferenceEquals(thisEffect, otherEffect))
return false;
// It's the same effect instance, so compare the configuration we're assigning
// to the effect before each drawing cycle
return
(this.textColor == other.textColor) &&
(this.transform == other.transform);
}
/// The draw context's effect used for rendering
private Effect effect;
#if XNA_3
/// Pass being currently rendered
private int currentPass;
#endif
/// Transformation matrix controlling the text's placement
private Matrix transform;
/// Drawing color of the text
private Color textColor;
}
} // namespace Nuclex.Fonts