[.net] [MDX] Writing a GUI using linked list

Started by
4 comments, last by remigius 16 years, 11 months ago
I have just bought a book called "DirectX 9 User Interfaces: Design and Implementation". It's got code examples in C++, so I was hoping I could just port them over to C#. The problem is with the base class called "CXControl". It uses a linked list type implementation and a bunch of methods for adding and removing child controls etc. I'm having difficulty translating it to C#. C++
class CXControl
{
	CXControl* m_Parent;
	CXControl* m_FirstChild;
	CXControl* m_NextSibling;
	CXControl* m_PreviousSibling;
}
My first attempt started along the lines of C#
class Control
{
	Control Parent;
	Control FirstChild;
	Control NextSibling;
	Control PreviousSibling;
}
I have my reservations about going this way because of having all these referenced objects could mess with Garbage Collection, and also performance issues. Am I in the right ballpark in thinking that? So next I had a look at using System.Collections.Generic.LinkedList<T> and System.Collections.Generic.LinkedListNode<T> to implement this. But I just can't wrap my head around it. Do I inherit LinkedList<Control>? Do I change all the pointers to be nodes instead?
public LinkedListNode<Control> Parent = null;
public LinkedListNode<Control> FirstChild = null;
public LinkedListNode<Control> NextSibling = null;
public LinkedListNode<Control> PreviousSibling = null;
But then I'm having issues converting over some of the methods this way..
public Control AddChildControl(Control ctrl)
{
	ctrl.Parent = this;

	if (FirstChild == null)
		FirstChild = ctrl;
	else
	{
		LinkedListNode<Control> temp = this.FirstChild;

		while (temp.Next != null)
			temp = temp.Next;

		temp.Next = ctrl;
		ctrl.PreviousSibling = temp;
	}

	return ctrl;
}
It complains that ctrl.Parent "Cannot implicitly convert Control to LinkedListNode<Control>". And it also complains that temp.Next is read-only. It's really confusing me, in C++ it is such a simple task to have an internal linked list to manage all the controls and child controls for a GUI. Is this a bad way to design a GUI for use in C#? I like the ideas presented in the book but are they only suited to C++? This book is available on Google if you want to see more of it's implementation of the CXControl class on Chapter 7 (Page 135).. http://books.google.com/books?id=j2N4jrL7jFkC&printsec=frontcover#PPA135,M1 Any help or recommendations would be greatly appreciated!
Advertisement
I'm just taking a guess at how the rest of that widget system works, but I don't think there's any reason not to duplicate that architecture in C#. Garbage Collector will do fine.
I think you might be better off by just using some container as a class member to hold the child controls. All the methods for adding, removing and iterating over the elements in such a container are already implemented in the C# class library, so by taking this route you might save yourself some trouble.

The code would look a little something like this:

class Control{  private List<Control> childControls = new List<Control>();  public List<Control> Controls  {     get { return childControls; }  }  public void Render()  {     // render control here     // recursively render child controls     foreach(Control c in this.Controls)     {        c.Render();     }  }}// in other codeControl c = new Control();// add a new child controlc.Controls.Add(new Control());// remove first child controlc.Controls.RemoveAt(0);


Assuming you won't be adding and/or removing tens of controls each frame, this approach should work just fine. The LinkedList might be better suited for more dynamic lists, but I don't think that's really needed in this scenario. I haven't worked with the C# LinkedList implementation yet but I'm pretty sure the LinkedListNodes aren't replacements for pointers, but rather internal nodes used in the LinkedList container class. The docs can probably tell you more about that.
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
While this is on the XNA Creators Club web site, it's still dealing with the different data structures and their performance in real-time applications.
It's a series and should give you an idea of what container could work best in this situation.

For you, using the LinkedList<> generic container would require a separate list if you were wanting to index directly into the list of UI components you've added, because you'd have to iterate through each LinkedListNode<> and perform some sort of comparison on an ID property/field.

http://creators.xna.com/Headlines/whitepapers/archive/2007/04/26/Data-Structures-Series.aspx
Thanks for the info guys. I successfully ported the code from C++ but I'm not sure if I like the way it works.

I usually use a List<T> or ArrayList() when I deal with collections of things like controls. But I thought this was an interesting approach to have the base control class handle all the message dispatchment and filter it down through each object using pointers to linked objects in a heirarchy. But now I think I'll go for an external GUIManager approach and handle messages that way.

I don't recommend this book "DirectX 9 User Interfaces: Design and Implementation"., it only starts getting into the GUI stuff half way through the book, and at the end you feel he's hardly touched on much at all. There are better GUI systems and tutorials on the Net like at Chad's C-Unit website (http://www.c-unit.com/tutorials/mdirectx/?t=47).

I'm also looking at the CustomUI example in the SDK which looks pretty good and hopefully I'll come up with a sort of hybrid that suits my tastes.

If anyone else has some good GUI design philosophy or principles to share, I'd like to hear them.
Quote:Original post by Headkaze
I don't recommend this book "DirectX 9 User Interfaces: Design and Implementation"., it only starts getting into the GUI stuff half way through the book, and at the end you feel he's hardly touched on much at all. There are better GUI systems and tutorials on the Net like at Chad's C-Unit website (http://www.c-unit.com/tutorials/mdirectx/?t=47).


Aye, I recall I did read some comments on this particular book leaving off just where things got interesting. But Chad's tutorials are very good indeed and the CustomUI/DXUT framework also is a good place to start, though it is a bit messy. I mostly try to stick close to the Windows Forms way of doing things. Not necessarily how it works under the hood, but rather API-wise so its behaviour is familiar to most C# coders.
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!

This topic is closed to new replies.

Advertisement