summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorleo2005-07-14 05:10:57 +0000
committerleo2005-07-14 05:10:57 +0000
commite3b7ec77734b8f415f9c756f0d7bf90ff202e71e (patch)
tree741fd8c82177a499308de47bd94453d9bdc5e229 /common
parent7c16eadee05cfe3115ac0a69a822c66f1b3e292f (diff)
Changed the pieces list to a tree sorted by categories.
git-svn-id: http://svn.leocad.org/trunk@414 c7d43263-9d01-0410-8a33-9dba5d9f93d6
Diffstat (limited to 'common')
-rwxr-xr-xcommon/array.cpp143
-rwxr-xr-xcommon/array.h40
-rwxr-xr-xcommon/library.cpp136
-rwxr-xr-xcommon/library.h35
-rwxr-xr-xcommon/system.h1
5 files changed, 280 insertions, 75 deletions
diff --git a/common/array.cpp b/common/array.cpp
index 3d58c55..76c9a30 100755
--- a/common/array.cpp
+++ b/common/array.cpp
@@ -1,105 +1,132 @@
//
-// Simple array class
+// Simple array classes
//
#include <stdlib.h>
#include <string.h>
template <class T>
-PtrArray<T>::PtrArray (int nSize)
+PtrArray<T>::PtrArray(int nSize)
{
- m_pData = NULL;
- m_nLength = 0;
- m_nAlloc = 0;
+ m_pData = NULL;
+ m_nLength = 0;
+ m_nAlloc = 0;
- if (nSize != 0)
- Expand (nSize);
+ if(nSize != 0)
+ Expand(nSize);
}
template <class T>
-PtrArray<T>::~PtrArray ()
+PtrArray<T>::~PtrArray()
{
- free (m_pData);
+ free(m_pData);
}
template <class T>
-void PtrArray<T>::Expand (int nGrow)
+void PtrArray<T>::Expand(int nGrow)
{
- if ((m_nLength + nGrow) > m_nAlloc)
- {
- m_pData = (T**)realloc (m_pData, (m_nLength + nGrow) * sizeof (T*));
- memset (m_pData + m_nLength, 0, nGrow * sizeof (T*));
- m_nAlloc = m_nLength + nGrow;
- }
+ if((m_nLength + nGrow) > m_nAlloc)
+ {
+ m_pData =(T**)realloc(m_pData,(m_nLength + nGrow) * sizeof(T*));
+ memset(m_pData + m_nLength, 0, nGrow * sizeof(T*));
+ m_nAlloc = m_nLength + nGrow;
+ }
}
template <class T>
-T* PtrArray<T>::RemoveIndex (int nIndex)
+void PtrArray<T>::RemoveAll()
{
- T* ret = NULL;
+ m_nLength = 0;
+}
- if (nIndex < m_nLength)
- {
- ret = m_pData[nIndex];
+template <class T>
+T* PtrArray<T>::RemoveIndex(int nIndex)
+{
+ T* ret = NULL;
- if (nIndex != m_nLength - 1)
- memmove (m_pData + nIndex, m_pData + nIndex + 1,
- sizeof (T*) * (m_nLength - nIndex - 1));
-
- m_nLength--;
- m_pData[m_nLength] = NULL;
- }
+ if(nIndex < m_nLength)
+ {
+ ret = m_pData[nIndex];
- return ret;
+ if(nIndex != m_nLength - 1)
+ memmove(m_pData + nIndex, m_pData + nIndex + 1, sizeof(T*) *(m_nLength - nIndex - 1));
+
+ m_nLength--;
+ m_pData[m_nLength] = NULL;
+ }
+
+ return ret;
}
template <class T>
-T* PtrArray<T>::RemovePointer (T* pObj)
+T* PtrArray<T>::RemovePointer(T* pObj)
{
- int i;
+ int i;
- for (i = 0; i < m_nLength; i++)
- if (m_pData[i] == pObj)
- return RemoveIndex (i);
+ for(i = 0; i < m_nLength; i++)
+ if(m_pData[i] == pObj)
+ return RemoveIndex(i);
- return NULL;
+ return NULL;
}
template <class T>
-void PtrArray<T>::Add (T* pObj)
+void PtrArray<T>::Add(T* pObj)
{
- Expand (1);
- m_pData[m_nLength++] = pObj;
+ Expand(1);
+ m_pData[m_nLength] = pObj;
+ m_nLength++;
}
template <class T>
-void PtrArray<T>::AddSorted (T* pObj, LC_PTRARRAY_COMPARE_FUNC pFunc, void* pData)
+void PtrArray<T>::AddSorted(T* pObj, LC_PTRARRAY_COMPARE_FUNC pFunc, void* pData)
{
- int i;
+ int i;
- for (i = 0; i < GetSize (); i++)
- if (pFunc (pObj, m_pData[i], pData) < 0)
- {
- InsertAt (i, pObj);
- return;
- }
+ for(i = 0; i < GetSize(); i++)
+ {
+ if(pFunc(pObj, m_pData[i], pData) < 0)
+ {
+ InsertAt(i, pObj);
+ return;
+ }
+ }
- Add (pObj);
+ Add(pObj);
}
template <class T>
-void PtrArray<T>::InsertAt (int nIndex, T* pObj)
+void PtrArray<T>::InsertAt(int nIndex, T* pObj)
{
- if (nIndex >= m_nLength)
- Expand (nIndex - m_nLength + 1);
- else
- Expand (1);
+ if(nIndex >= m_nLength)
+ Expand(nIndex - m_nLength + 1);
+ else
+ Expand(1);
m_nLength++;
- for (int i = m_nLength - 1; i > nIndex; i--)
- m_pData[i] = m_pData[i-1];
+ for(int i = m_nLength - 1; i > nIndex; i--)
+ m_pData[i] = m_pData[i-1];
+
+ m_pData[nIndex] = pObj;
+}
+
+template <class T>
+int PtrArray<T>::FindIndex(T* Obj) const
+{
+ for (int i = 0; i < m_nLength; i++)
+ if (m_pData[i] == Obj)
+ return i;
- m_pData[nIndex] = pObj;
+ return -1;
+}
+
+template <class T>
+PtrArray<T>& PtrArray<T>::operator=(const PtrArray<T>& Array)
+{
+ m_nLength = Array.m_nLength;
+ m_nAlloc = Array.m_nAlloc;
+ m_pData =(T**)realloc(m_pData, (m_nAlloc) * sizeof(T*));
+ memcpy(m_pData, Array.m_pData, (m_nAlloc) * sizeof(T*));
}
// ============================================================================
@@ -141,6 +168,12 @@ void ObjArray<T>::Expand(int Grow)
}
template <class T>
+void ObjArray<T>::RemoveAll()
+{
+ m_Length = 0;
+}
+
+template <class T>
void ObjArray<T>::RemoveIndex(int Index)
{
m_Length--;
diff --git a/common/array.h b/common/array.h
index c8adfbf..64f82f9 100755
--- a/common/array.h
+++ b/common/array.h
@@ -4,30 +4,33 @@
template <class T>
class PtrArray
{
- public:
- PtrArray (int nSize = 0);
- ~PtrArray ();
+public:
+ PtrArray(int nSize = 0);
+ ~PtrArray();
- typedef int (*LC_PTRARRAY_COMPARE_FUNC) (T* a, T* b, void* data);
+ typedef int (*LC_PTRARRAY_COMPARE_FUNC)(T* a, T* b, void* data);
- int GetSize () const
- { return m_nLength; }
+ int GetSize() const
+ { return m_nLength; }
- T* RemoveIndex (int nIndex);
- T* RemovePointer (T* pObj);
- void Add (T* pObj);
- void AddSorted (T* pObj, LC_PTRARRAY_COMPARE_FUNC pFunc, void* pData);
- void InsertAt (int nIndex, T* pObj);
+ T* RemoveIndex(int nIndex);
+ T* RemovePointer(T* pObj);
+ void RemoveAll();
+ void Add(T* pObj);
+ void AddSorted(T* pObj, LC_PTRARRAY_COMPARE_FUNC pFunc, void* pData);
+ void InsertAt(int nIndex, T* pObj);
+ int FindIndex(T* Obj) const;
- T* operator [](int nIndex) const
- { return m_pData[nIndex]; }
+ PtrArray<T>& operator=(const PtrArray<T>& Array);
+ T* operator [](int nIndex) const
+ { return m_pData[nIndex]; }
- protected:
- void Expand (int nGrow);
+protected:
+ void Expand(int nGrow);
- T** m_pData;
- int m_nLength;
- int m_nAlloc;
+ T** m_pData;
+ int m_nLength;
+ int m_nAlloc;
};
template <class T>
@@ -43,6 +46,7 @@ public:
{ return m_Length; }
void RemoveIndex(int Index);
+ void RemoveAll();
void Add(const T& Obj);
void AddSorted(const T& Obj, LC_OBJARRAY_COMPARE_FUNC Func, void* SortData);
void InsertAt(int Index, const T& Obj);
diff --git a/common/library.cpp b/common/library.cpp
index 3da1053..370b6df 100755
--- a/common/library.cpp
+++ b/common/library.cpp
@@ -198,6 +198,30 @@ bool PiecesLibrary::Load (const char *libpath)
idx.Close();
bin.Close();
+ // TODO: Load categories.
+
+ const int NumCategories = 34;
+ const char* DefaultCategories[] =
+ {
+ "Animal", "Antenna", "Arm", "Bar", "Baseplate", "Belville", "Boat", "Bracket", "Brick", "Car",
+ "Cone", "Container", "Crane", "Cylinder", "Door", "Flag", "Hinge", "Hose", "Magnet", "Minifig",
+ "Minifig Accessory", "Plane", "Plant", "Plate", "Propellor", "Rock", "Round", "Scala",
+ "Slope", "Space", "Support", "Technic", "Wheel", "Windscreen"
+ };
+
+
+ for (i = 0; i < NumCategories; i++)
+ {
+ PiecesLibraryCategory Cat;
+
+ Cat.Name = DefaultCategories[i];
+ Cat.Keywords = DefaultCategories[i];
+
+ m_Categories.Add(Cat);
+ }
+
+ SystemUpdateCategories(false);
+
m_bNeedsReload = false;
return true;
@@ -366,6 +390,118 @@ Texture* PiecesLibrary::GetTexture (int index) const
// =============================================================================
+void PiecesLibrary::GetCategoryEntries(int CategoryIndex, PtrArray<PieceInfo>& SinglePieces, PtrArray<PieceInfo>& GroupedPieces)
+{
+ bool m_bSubParts = false;
+
+ String& SearchString = m_Categories[CategoryIndex].Keywords;
+
+ SinglePieces.RemoveAll();
+ GroupedPieces.RemoveAll();
+
+ for (int i = 0; i < m_nPieceCount; i++)
+ {
+ PieceInfo* Info = &m_pPieceIdx[i];
+
+ // Skip subparts if the user doesn't want to see them.
+ if ((Info->m_strDescription[0] == '~') && !m_bSubParts)
+ continue;
+
+ // Check if the piece belongs to this category.
+ if (strncmp(Info->m_strDescription, (const char*)SearchString, strlen(SearchString)) != 0)
+ continue;
+
+ // Check if it's a patterned piece.
+ const char* Name = Info->m_strName;
+ while (*Name)
+ {
+ if (!*Name || *Name < '0' || *Name > '9')
+ break;
+
+ if (*Name == 'P')
+ break;
+
+ Name++;
+ }
+
+ if (*Name == 'P')
+ {
+ PieceInfo* Parent;
+
+ // Find the parent of this patterned piece.
+ char ParentName[9];
+ strcpy(ParentName, Info->m_strName);
+ ParentName[Name - Info->m_strName] = '\0';
+
+ Parent = FindPieceInfo(ParentName);
+
+ if (Parent)
+ {
+ // Check if the parent was added as a single piece.
+ int Index = SinglePieces.FindIndex(Parent);
+
+ if (Index != -1)
+ SinglePieces.RemoveIndex(Index);
+
+ Index = GroupedPieces.FindIndex(Parent);
+
+ if (Index == -1)
+ GroupedPieces.Add(Parent);
+ }
+ else
+ {
+ // Patterned pieces should have a parent but in case they don't just add them anyway.
+ SinglePieces.Add(Info);
+ }
+ }
+ else
+ {
+ // Check if this piece has already been added to this category by one of its children.
+ int Index = GroupedPieces.FindIndex(Info);
+
+ if (Index == -1)
+ SinglePieces.Add(Info);
+ }
+ }
+}
+
+void PiecesLibrary::GetPatternedPieces(PieceInfo* Parent, PtrArray<PieceInfo>& Pieces)
+{
+ char Name[9];
+ strcpy(Name, Parent->m_strName);
+ strcat(Name, "P");
+
+ Pieces.RemoveAll();
+
+ for (int i = 0; i < m_nPieceCount; i++)
+ {
+ PieceInfo* Info = &m_pPieceIdx[i];
+
+ if (strncmp(Name, Info->m_strName, strlen(Name)) == 0)
+ Pieces.Add(Info);
+ }
+}
+
+void PiecesLibrary::SetCategory(int Index, const String& Name, const String& Keywords)
+{
+ m_Categories[Index].Name = Name;
+ m_Categories[Index].Keywords = Keywords;
+
+ SystemUpdateCategories(true);
+}
+
+void PiecesLibrary::AddCategory(const String& Name, const String& Keywords)
+{
+ PiecesLibraryCategory Cat;
+
+ Cat.Name = Name;
+ Cat.Keywords = Keywords;
+
+ m_Categories.Add(Cat);
+
+ SystemUpdateCategories(true);
+}
+
unsigned long PiecesLibrary::GetDefaultPieceGroup (const char* name)
{
char tmp[9];
diff --git a/common/library.h b/common/library.h
index 54fd137..3ecdde9 100755
--- a/common/library.h
+++ b/common/library.h
@@ -3,6 +3,7 @@
#include "defines.h"
#include "str.h"
+#include "array.h"
class File;
class Texture;
@@ -10,6 +11,12 @@ class PieceInfo;
#define LC_PIECESLIB_MAXGROUPS 32
+typedef struct
+{
+ String Name;
+ String Keywords;
+} PiecesLibraryCategory;
+
class PiecesLibrary
{
public:
@@ -29,19 +36,40 @@ public:
bool NeedsReload () const
{ return m_bNeedsReload; }
+ // Categories.
+ void GetCategoryEntries(int CategoryIndex, PtrArray<PieceInfo>& SinglePieces, PtrArray<PieceInfo>& GroupedPieces);
+ void GetPatternedPieces(PieceInfo* Parent, PtrArray<PieceInfo>& Pieces);
+ void SetCategory(int Index, const String& Name, const String& Keywords);
+ void AddCategory(const String& Name, const String& Keywords);
+
+ const char* GetCategoryName(int Index) const
+ { return m_Categories[Index].Name; }
+
+ int GetNumCategories() const
+ { return m_Categories.GetSize(); }
+
+ int FindCategoryIndex(const String& CategoryName) const
+ {
+ for (int i = 0; i < m_Categories.GetSize(); i++)
+ if (m_Categories[i].Name == CategoryName)
+ return i;
+
+ return -1;
+ }
+
void CheckReload ();
bool Load (const char* libpath);
void Unload ();
bool LoadGroupConfig (const char* Filename);
- // Search for stuff
+ // Search for pieces.
PieceInfo* FindPieceInfo (const char* name) const;
PieceInfo* GetPieceInfo (int index) const;
int GetPieceIndex (PieceInfo *pInfo) const;
Texture* FindTexture (const char* name) const;
Texture* GetTexture (int index) const;
- // File operations
+ // File operations.
bool DeletePieces (char** names, int numpieces);
bool LoadUpdate (const char* update);
bool DeleteTextures (char** Names, int NumTextures);
@@ -60,6 +88,9 @@ protected:
int m_nTextureCount; // number of textures
Texture* m_pTextures; // textures array
+ // Categories.
+ ObjArray<PiecesLibraryCategory> m_Categories;
+
// Groups stuff
int m_nGroupCount;
String m_strGroups[LC_PIECESLIB_MAXGROUPS];
diff --git a/common/system.h b/common/system.h
index 415b30a..f8161ae 100755
--- a/common/system.h
+++ b/common/system.h
@@ -75,6 +75,7 @@ void SystemUpdateSnap(unsigned short MoveSnap, unsigned short RotateSnap);
void SystemUpdateSelected(unsigned long flags);
void SystemUpdatePaste(bool enable);
void SystemUpdatePlay(bool play, bool stop);
+void SystemUpdateCategories(bool SearchOnly);
void SystemInit();
void SystemFinish();