Commits

Anonymous committed 8839601

* travail sur l'éditeur
* modification des méthodes d'ajout/suppression de lignes et colonnes
dans Map
* ajout d'un fonction getPointerPosition dans GUI
* ajout de traitement pour les actions dans Editor.

  • Participants
  • Parent commits 2a832fd

Comments (0)

Files changed (3)

File src/editor.d

 import editortools;
 import gui;
 import editoractions;
+import map;
 
 class Editor: public GamePart {
 
 	string mPointerIcon;
 	char mTileCode;
 
+	SDL_Rect mHighlightRect;
+	SDL_Surface* mHighlight;
+
 	void delegate() fMode;
 	void delegate() fPreviousMode;
 
 			return;
 		}
 
+		// highligtht action
+		GUI.instance.getPointerPosition( x, y );
+		int xOffset = cast(int)gView.mX / gMap.TILE_WIDTH;
+		int yOffset = cast(int)gView.mY / gMap.TILE_HEIGHT;
+		int xMap = x / gMap.TILE_WIDTH;
+		int yMap = y / gMap.TILE_HEIGHT;
+		int xHL = xMap * gMap.TILE_WIDTH - (cast(int)gView.mX % gMap.TILE_WIDTH);
+		int yHL = yMap * gMap.TILE_HEIGHT - (cast(int)gView.mY % gMap.TILE_HEIGHT);
+
+
+		switch( mAction ) {
+			case ActionType.DELETE_COLUMN:
+			case ActionType.INSERT_COLUMN:
+				highlight( xHL, 0, gMap.TILE_WIDTH, 480 );
+			break;
+
+			case ActionType.DELETE_LINE:
+			case ActionType.INSERT_LINE:
+				highlight( 0, yHL, 640, gMap.TILE_HEIGHT );
+			break;
+
+			case ActionType.DELETE_TILE:
+			case ActionType.ADD_TILE:
+				highlight( xHL, yHL, gMap.TILE_WIDTH, gMap.TILE_HEIGHT );
+			default:
+			break;
+		}
+
+		// do action if click
 		if( gInputManager.isMouseButtonUp(SDL_BUTTON_LEFT) ) {
-			// switch( action ) ...
+			switch( mAction ) {
+				case ActionType.INSERT_COLUMN:
+					gMap.insertColumn( xMap + xOffset );
+				break;
+
+				case ActionType.DELETE_COLUMN:
+					gMap.insertColumn( xMap + xOffset );
+				break;
+
+				case ActionType.INSERT_LINE:
+					gMap.insertRow( yMap + yOffset );
+				break;
+
+				case ActionType.DELETE_LINE:
+					gMap.deleteRow( yMap + yOffset );
+				break;
+
+				case ActionType.ADD_TILE:
+					//gMap.addTile( xMap+xOffset, yMap+yOffset );
+				break;
+
+				case ActionType.DELETE_TILE:
+					//gMap.removeTile( xMap+xOffset, yMap+yOffset );
+				break;
+
+				default:
+				break;
+			}
 		}
 
 	}
 		return( mState );
 	}
 
-
+	void highlight( int x, int y, int w, int h ) {
+		if( mHighlightRect.w != w || mHighlightRect.h != h || mHighlight == null ) {
+			mHighlightRect.w = cast(ushort)w;
+			mHighlightRect.h = cast(ushort)h;
+			if( mHighlight != null ) SDL_FreeSurface( mHighlight );
+			mHighlight = SDL_CreateRGBSurface( SDL_HWSURFACE, w, h, 32, 0,0,0,0 );
+			SDL_FillRect( mHighlight, null, 0xFFFFFFFF );
+			SDL_SetAlpha( mHighlight, SDL_SRCALPHA, 64 );
+		}
+		mHighlightRect.x = cast(short)x;
+		mHighlightRect.y = cast(short)y;
+		SDL_BlitSurface( mHighlight, null, gScreen.mSurface, &mHighlightRect );
+	}
 }
 		}
 	}
 
+	void getPointerPosition( out int x, out int y ) {
+		x = mPx;
+		y = mPy;
+	}
+
 	void update( int relx, int rely, bool click ) {
 
 		mPx += relx;
 	int mWidth;				/** Width of the map in tiles */
 	int mHeight;			/** Height of the map in tiles */
 	string mMapFileName;
+	string[] mMapFile;
 
 	const int TILE_WIDTH = 32;
 	const int TILE_HEIGHT = 32;
     this(string mapFileName) {
         gMap = this;
 
-		char[] file = cast(char[]) read("data/" ~ mapFileName);
-		mMapFileName = mapFileName;
-
-        char[][] mapstring = file.splitlines();
-
-        mHeight = mapstring.length;
-        mWidth = mapstring[0].length;
-
-		// set map dimensions
-		mMap.length = mHeight;
-		for(int i = 0; i<mHeight; i++)
-			mMap[i].length = mWidth;
-
-
-		foreach( uint y, char[] line; mapstring ) {
-
-			foreach( uint x, char c; line ) {
-
-				assert( x < mWidth );
-
-				auto xpix = x*TILE_WIDTH;
-				auto ypix = y*TILE_HEIGHT;
-
-				GameObjectFactory.create( c, xpix, ypix );
-
-			}
-
-		}
+		load( mapFileName );
 
     }
-
+
+	bool load( string mapFileName ) {
+
+		char[] file = cast(char[]) read("data/" ~ mapFileName);
+		mMapFileName = mapFileName;
+
+        mMapFile = file.splitlines();
+
+        mHeight = mMapFile.length;
+        mWidth = mMapFile[0].length;
+
+		// set map dimensions
+		mMap.length = mHeight;
+		for(int i = 0; i<mHeight; i++)
+			mMap[i].length = mWidth;
+
+
+		foreach( uint y, char[] line; mMapFile ) {
+
+			foreach( uint x, char c; line ) {
+
+				assert( x < mWidth );
+
+				auto xpix = x*TILE_WIDTH;
+				auto ypix = y*TILE_HEIGHT;
+
+				GameObjectFactory.create( c, xpix, ypix );
+
+			}
+
+		}
+		return( true );
+	}
+
+	bool save() {
+		return false;
+	}
 
 	/**
 	 * Returns the objects whose AABB overlap the given object's AABB.
 
 		mMap[y][x].sub( obj );
 	}
+
+
+	void insertRow( uint rownum ) {
+
+		// update the mapfile
+		char[] emptyRow;
+		emptyRow = std.string.repeat(" ", mWidth-1 );
+		mMapFile = mMapFile[0 .. rownum] ~ emptyRow ~ mMapFile[rownum .. $];
+
+		// update the map of gameobjects
+		ObjectList[] emptyRow2;
+		emptyRow2.length = mWidth;
+		mMap = mMap[0 .. rownum] ~ emptyRow2 ~ mMap[rownum .. $];
+
+		// move objects
+		foreach( cell; mMap[rownum] ) {
+			foreach( o; cell ) {
+				float oldx = o.mX;
+				float oldy = o.mY; o.mY += TILE_HEIGHT;
+				updateObjectPosition( o, oldx, oldy );
+			}
+		}
+	}
+
+
+	void insertColumn( uint colnum ) {
+		foreach( inout row; mMapFile ) {
+			row = row[0 .. colnum] ~ ' ' ~ row[colnum .. $];
+		}
+
+		foreach( inout row; mMap ) {
+			ObjectList o;
+			row = row[0 .. colnum] ~ o ~ row[colnum .. $];
+
+			// move objects
+			foreach( o; row[colnum] ) {
+				float oldx = o.mX;
+				float oldy = o.mY; o.mY += TILE_HEIGHT;
+				updateObjectPosition( o, oldx, oldy );
+			}
+		}
+	}
+
+
+	void deleteRow( uint row ) {
+		mMapFile = mMapFile[0 .. row] ~ mMapFile[row+1 .. $];
+		// remove objects on this row
+		foreach( cell; mMap[row] )
+			foreach( o; cell ) removeObject(o);
+		// suppress the row
+		mMap = mMap[0 .. row] ~ mMap[row+1 .. $];
+	}
+
+	void deleteColumn( uint col ) {
+		foreach( inout row; mMapFile ) {
+			row = row[0 .. col] ~ row[col+1 .. $];
+		}
+		foreach( inout row; mMap ) {
+			// remove objects on this column for this row
+			foreach( o; row[col] ) removeObject( o );
+			// supress the cell at colum on this row
+			row = row[0 .. col] ~ row[col+1 .. $];
+		}
+	}
 
 }