Commits

Anonymous committed 785f58f

Ajout de slist ( généralisation de la liste chaînée de objectList )
et utilisation dans GUI et Messages

Comments (0)

Files changed (3)

 	bool mPointerHidden;
 	int mPx, mPy;
 
-	Panel[] mPanels;
-	//auto mPanels = new SList!(Panel); // pas utilisable : bug du D ( cas : template d'une sous-classe avec paramètres )
+	alias SList!(Panel) PanelList;
+	PanelList mPanels;
 
 	static GUI mInstance;
 
 	private this() {
 		mPointerHidden = false;
 		mPointer = gTextureManager.get("pointer.png");
+		mPanels = new PanelList;
 	}
 
 	void showPointer() {
 
 	Panel addPanel( int x, int y ) {
 		Panel p = new Panel( null, x, y );
-		mPanels ~= p;
+		//mPanels ~= p;
+		mPanels.add( p );
 		return( p );
 	}
 
 	void removePanel( Panel p ) {
-		foreach( i, panel; mPanels ) {
-			if( panel is p ) {
-				delete( p );
-				if( mPanels.length==1 ) { mPanels.length = 0; break; }
-				if( i == 0 ) { mPanels = mPanels[1..$]; break; }
-				if( i == mPanels.length-1 ) { mPanels = mPanels[0..$-1]; break; }
-				mPanels = mPanels[0..(i-1)] ~ mPanels[(i+1)..$];
-				break;
-			}
-		}
+		mPanels.sub( p );
+		delete(p);
 	}
 
 }
 import textsurface;
 import texturemanager;
 import time;
-
+import slist;
 
 class Messages {
 
 		SDL_Rect pos;
 		float origY;
 	}
-
-	static Mess*[] mMessages;
+
+	alias SList!(Mess*) MessList;
+	static MessList mMessages;
+
+	static this() {
+		mMessages = new MessList;
+	}
 
 	static void add( char[] text ) {
 		Mess* m = new Mess;
 		m.pos.w = cast(ushort) surf.w;
 		m.pos.h = cast(ushort) surf.h;
 		m.origY = m.pos.y;
-		mMessages ~= m;
+		mMessages.add( m );
 		writefln( "add message text=%s", text );
 	}
 
 
 	static void update() {
 		float dt = gTime.getDelta();
-		foreach( i, m ; mMessages ) {
+		foreach( m ; mMessages ) {
 			m.timer += dt;
 
 			float t = m.timer * .75f;
 			m.pos.y = cast(short) (m.origY - t * t * t * t * t);
 
 			if( m.pos.y < - cast(int) m.pos.h ) {
-				// Out of screen, remove message
-				Mess*[] m1;
-				Mess*[] m2;
-				if( i > 0 ) {
-					m1 = mMessages[0..i-1];
-				}
-				if( i < mMessages.length - 1 ) {
-					m2 = mMessages[i+1..$];
-				}
-				mMessages = m1 ~ m2;
-
+				mMessages.sub( m );
 				delete(m);
 			}
 		}
+
+module slist;
+
+
+class SList(T) {
+
+	struct Element {
+		Element* next;
+		T object;
+	}
+
+    Element* first=null;
+	int size;
+
+    void add( T obj ) {
+
+		if( first == null ) {
+			first = new Element;
+			first.object = obj;
+		}
+		else {
+			Element* e = first;
+			while( e.next != null ) {
+				e = e.next;
+			}
+			e.next = new Element;
+			e.next.object = obj;
+		}
+		size++;
+	}
+
+
+    void sub( T obj ) {
+
+		if( first.object == obj ) {
+			first = first.next;
+		}
+		else {
+			Element* e = first.next;
+			Element* prev = first;
+			while( e.object != obj ) {
+				prev = e;
+				e = e.next;
+			}
+			prev.next = e.next;
+		}
+		size--;
+	}
+
+
+	int opApply( int delegate( inout T ) dg ) {
+
+		int done=0;
+
+		if( first != null ) {
+			Element* iterator = first;
+			do {
+				done = dg( iterator.object );
+				if( done ) break;
+				iterator = iterator.next;
+			} while( iterator != null )
+		}
+		return( done );
+	}
+}