about summary refs log tree commit diff
path: root/src/game
diff options
context:
space:
mode:
authoroy <Tom_Adams@web.de>2010-10-16 11:05:42 +0200
committeroy <Tom_Adams@web.de>2010-10-16 11:05:42 +0200
commit9c256f69d8f8f2aa726f8b9ab0792dfb62069e57 (patch)
treec28e18f89a50dd77e66f5f10817cae48c8e9829e /src/game
parent5f7efb465e21ec65b8f026a5d68559f7fce60a21 (diff)
downloadzcatch-9c256f69d8f8f2aa726f8b9ab0792dfb62069e57.tar.gz
zcatch-9c256f69d8f8f2aa726f8b9ab0792dfb62069e57.zip
fixed issues with adding and replacing images in the editor. Closes #216
Diffstat (limited to 'src/game')
-rw-r--r--src/game/editor/ed_editor.cpp72
1 files changed, 38 insertions, 34 deletions
diff --git a/src/game/editor/ed_editor.cpp b/src/game/editor/ed_editor.cpp
index 287a77e9..1c6ad225 100644
--- a/src/game/editor/ed_editor.cpp
+++ b/src/game/editor/ed_editor.cpp
@@ -1839,36 +1839,20 @@ void CEditor::RenderLayers(CUIRect ToolBox, CUIRect ToolBar, CUIRect View)
 	}
 }
 
-static void ExtractName(const char *pFileName, char *pName)
+static void ExtractName(const char *pFileName, char *pName, int BufferSize)
 {
-	int Len = str_length(pFileName);
-	int Start = Len;
-	int End = Len;
-
-	while(Start > 0)
-	{
-		Start--;
-		if(pFileName[Start] == '/' || pFileName[Start] == '\\')
-		{
-			Start++;
-			break;
-		}
-	}
-
-	End = Start;
-	for(int i = Start; i < Len; i++)
+	const char *pExtractedName = pFileName;
+	const char *pEnd = 0;
+	for(; *pFileName; ++pFileName)
 	{
-		if(pFileName[i] == '.')
-			End = i;
+		if(*pFileName == '/' || *pFileName == '\\')
+			pExtractedName = pFileName+1;
+		else if(*pFileName == '.')
+			pEnd = pFileName;
 	}
 
-	if(End == Start)
-		End = Len;
-
-	int FinalLen = End-Start;
-	mem_copy(pName, &pFileName[Start], FinalLen);
-	pName[FinalLen] = 0;
-	//dbg_msg("", "%s %s %d %d", pFileName, pName, Start, End);
+	int Length = pEnd > pExtractedName ? min(BufferSize, pEnd-pExtractedName+1) : BufferSize;
+	str_copy(pName, pExtractedName, Length);
 }
 
 void CEditor::ReplaceImage(const char *pFileName, int StorageType, void *pUser)
@@ -1879,11 +1863,19 @@ void CEditor::ReplaceImage(const char *pFileName, int StorageType, void *pUser)
 		return;
 
 	CEditorImage *pImg = pEditor->m_Map.m_lImages[pEditor->m_SelectedImage];
+	int External = pImg->m_External;
 	pEditor->Graphics()->UnloadTexture(pImg->m_TexId);
 	*pImg = ImgInfo;
-	ExtractName(pFileName, pImg->m_aName);
+	pImg->m_External = External;
+	ExtractName(pFileName, pImg->m_aName, sizeof(pImg->m_aName));
 	pImg->m_TexId = pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0);
 	pEditor->SortImages();
+	for(int i = 0; i < pEditor->m_Map.m_lImages.size(); ++i)
+	{
+	    if(!str_comp(pEditor->m_Map.m_lImages[i]->m_aName, pImg->m_aName))
+           pEditor->m_SelectedImage = i;
+	}
+	pEditor->m_Dialog = DIALOG_NONE;
 }
 
 void CEditor::AddImage(const char *pFileName, int StorageType, void *pUser)
@@ -1893,20 +1885,32 @@ void CEditor::AddImage(const char *pFileName, int StorageType, void *pUser)
 	if(!pEditor->Graphics()->LoadPNG(&ImgInfo, pFileName, StorageType))
 		return;
 
-	CEditorImage *pImg = new CEditorImage(pEditor);
-	*pImg = ImgInfo;
-	pImg->m_TexId = pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0);
-	pImg->m_External = 1; // external by default
-	ExtractName(pFileName, pImg->m_aName);
-
+	// check if we have that image already
+	char aBuf[128];
+	ExtractName(pFileName, aBuf, sizeof(aBuf));
 	for(int i = 0; i < pEditor->m_Map.m_lImages.size(); ++i)
 	{
-	    if(!str_comp(pEditor->m_Map.m_lImages[i]->m_aName, pImg->m_aName))
+	    if(!str_comp(pEditor->m_Map.m_lImages[i]->m_aName, aBuf))
             return;
 	}
 
+	CEditorImage *pImg = new CEditorImage(pEditor);
+	*pImg = ImgInfo;
+	pImg->m_TexId = pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0);
+	pImg->m_External = 1;	// external by default
+	str_copy(pImg->m_aName, aBuf, sizeof(pImg->m_aName));
 	pEditor->m_Map.m_lImages.add(pImg);
 	pEditor->SortImages();
+	if(pEditor->m_SelectedImage > -1 && pEditor->m_SelectedImage < pEditor->m_Map.m_lImages.size())
+	{
+		for(int i = 0; i <= pEditor->m_SelectedImage; ++i)
+			if(!str_comp(pEditor->m_Map.m_lImages[i]->m_aName, aBuf))
+			{
+				pEditor->m_SelectedImage++;
+				break;
+			}
+	}
+	pEditor->m_Dialog = DIALOG_NONE;
 }