This project is read-only.

Different "style" for one specific control

Mar 30, 2011 at 6:51 PM

I was wondering about changing skins for one or more controls. I noticed the whole skinning routine is somewhat similar to CSS, can I make somehow a different "style" for specific control(giving an "id" to one labelcontrol).

Jun 24, 2011 at 12:26 AM

I've just started to port my WIP game to Nuclex (mainly for GUI support) and needed this too, so here's how I did it for LabelControl (I'll change the code for each control when I need it):

1. Added the CustomFrame field to LabelControl to allow specifying a frame only for one instance (a label, in this case):

  /// <summary>Control that draws a block of text</summary>
  public class LabelControl : Control {
      /// <summary>If this is non-null, the custom frame will be used for rendering</summary>
      public string CustomFrame;

2. Changed the renderer to use that field, if it exists:

  /// <summary>Renders label controls in a traditional flat style</summary>
  public class FlatLabelControlRenderer :
    IFlatControlRenderer<Controls.LabelControl> {

    public void Render(
      Controls.LabelControl control, IFlatGuiGraphics graphics
    ) {
        if ((null != control.CustomFrame) && (control.CustomFrame.Length > 0))
            graphics.DrawString(control.CustomFrame, control.GetAbsoluteBounds(), control.Text);
            graphics.DrawString("label", control.GetAbsoluteBounds(), control.Text);

3. Add a new frame to the skin XML file:

    <frame name="mgtitle">
      <text font="funnybig" hplacement="left" vplacement="center" yoffset="15" color="#FAD200" />

4. Set your custom frame:

            LabelControl title = new LabelControl("Title");
            title.Bounds = new UniRectangle(new UniScalar(0f, 300f), new UniScalar(0f, 50f), 200, 50);
            title.CustomFrame = "mgtitle";

And that's it. Few new lines of code :)

Don't know how easy is to do the same for each control, I'm just starting to know Nuclex but you can develop from here.