Changed: #1409 Added hierarchy to the tile model, renamed the base class to 'Node'

This commit is contained in:
sfb 2011-12-22 19:58:59 -06:00
parent 8b9cfe5cd5
commit ba4cc67101
5 changed files with 254 additions and 160 deletions

View file

@ -97,15 +97,11 @@ TileEditorMainWindow::TileEditorMainWindow(QWidget *parent)
// Transition List View // Transition List View
m_ui->listViewTransition->setModel(m_model); m_ui->listViewTransition->setModel(m_model);
//m_ui->listViewTransition->addAction(m_ui->actionAddTile);
//m_ui->listViewTransition->addAction(m_ui->actionDeleteTile);
m_ui->listViewTransition->addAction(m_ui->actionReplaceImage); m_ui->listViewTransition->addAction(m_ui->actionReplaceImage);
m_ui->listViewTransition->addAction(m_ui->actionDeleteImage); m_ui->listViewTransition->addAction(m_ui->actionDeleteImage);
// Displacement List View // Displacement List View
m_ui->listViewDisplacement->setModel(m_model); m_ui->listViewDisplacement->setModel(m_model);
//m_ui->listViewDisplacement->addAction(m_ui->actionAddTile);
//m_ui->listViewDisplacement->addAction(m_ui->actionDeleteTile);
m_ui->listViewDisplacement->addAction(m_ui->actionReplaceImage); m_ui->listViewDisplacement->addAction(m_ui->actionReplaceImage);
m_ui->listViewDisplacement->addAction(m_ui->actionDeleteImage); m_ui->listViewDisplacement->addAction(m_ui->actionDeleteImage);
@ -172,69 +168,11 @@ void TileEditorMainWindow::onTileSetAdd()
//} //}
//else //else
//{ //{
// Create the new tile set.
QVector<QVariant> items;
items.push_back(text);
TileItem *tileSet = new TileItem(items);
// child for 128x128 tiles
QVector<QVariant> tiles128;
tiles128.push_back(QString("128"));
TileTypeTileItem *tile128= new TileTypeTileItem(tiles128);
// child for 256x256 tiles
QVector<QVariant> tiles256;
tiles256.push_back(QString("256"));
TileTypeTileItem *tile256= new TileTypeTileItem(tiles256);
// child for transition tiles. TileSetNode *tileSet = model->createTileSetNode(text);
QVector<QVariant> tilesTrans;
tilesTrans.push_back(QString("Transition"));
TileTypeTileItem *tileTrans= new TileTypeTileItem(tilesTrans);
// Add the default transition tiles.
// TODO tie this to CTileSet::count from NeL
for(int transPos=0; transPos<48; transPos++)
{
QVector<QVariant> tileInfo;
tileInfo.push_back(QString("filename").append(QString::number(transPos+1)));
TileItem *transTile= new TileItem(tileInfo);
tileTrans->appendRow(transTile);
}
// child for displacement tiles
QVector<QVariant> tilesDisp;
tilesDisp.push_back(QString("Displacement"));
TileTypeTileItem *tileDisp= new TileTypeTileItem(tilesDisp);
// Add the default displacement tiles.
// TODO tie this to CTileSet::CountDisplace from NeL
for(int dispPos=0; dispPos<16; dispPos++)
{
QVector<QVariant> tileInfo;
tileInfo.push_back(QString("filename").append(QString::number(dispPos+1)));
TileItem *dispTile= new TileItem(tileInfo);
tileDisp->appendRow(dispTile);
}
// Append them in the correct order to the tile set.
tileSet->appendRow(tile128);
tileSet->appendRow(tile256);
tileSet->appendRow(tileTrans);
tileSet->appendRow(tileDisp);
model->appendRow(tileSet);
m_ui->tileSetLV->reset(); m_ui->tileSetLV->reset();
//updateActions()
//}
// tileBank.addTileSet( text.toStdString() );
// ui.tileSetListWidget->addItem(text);
// ui.tileSetListWidget->setCurrentRow(ui.tileSetListWidget->count() - 1);
//}
} }
} }
@ -256,4 +194,9 @@ void TileEditorMainWindow::changeActiveTileSet(const QModelIndex &newIndex, cons
m_ui->listView256->setRootIndex(tile256Idx); m_ui->listView256->setRootIndex(tile256Idx);
m_ui->listViewTransition->setRootIndex(tileTransIdx); m_ui->listViewTransition->setRootIndex(tileTransIdx);
m_ui->listViewDisplacement->setRootIndex(tileDispIdx); m_ui->listViewDisplacement->setRootIndex(tileDispIdx);
m_ui->listView128->reset();
m_ui->listView256->reset();
m_ui->listViewTransition->reset();
m_ui->listViewDisplacement->reset();
} }

View file

@ -18,145 +18,213 @@
#include <nel/misc/debug.h> #include <nel/misc/debug.h>
TileItem::TileItem(const QVector<QVariant> &data, TileItem *parent) Node::Node() : m_parentItem(0)
{ {
parentItem = parent;
itemData = data;
} }
TileItem::~TileItem() Node::Node(const QVector<QVariant> &data, Node *parent)
{ {
qDeleteAll(childItems); m_parentItem = parent;
m_itemData = data;
} }
void TileItem::appendChild(TileItem *item) Node::~Node()
{ {
childItems.append(item); qDeleteAll(m_childItems);
} }
TileItem *TileItem::child(int row) void Node::appendChild(Node *item)
{
m_childItems.append(item);
}
Node *Node::child(int row)
{ {
//nlinfo("row %d and size %d", row, childItems.size()); //nlinfo("row %d and size %d", row, childItems.size());
return childItems.value(row); return m_childItems.value(row);
} }
int TileItem::childCount() const int Node::childCount() const
{ {
return childItems.count(); return m_childItems.count();
} }
int TileItem::childNumber() const int Node::childNumber() const
{ {
if(parentItem) if(m_parentItem)
return parentItem->childItems.indexOf(const_cast<TileItem*>(this)); return m_parentItem->m_childItems.indexOf(const_cast<Node*>(this));
return 0; return 0;
} }
bool TileItem::insertChildren(int position, int count, int columns) bool Node::insertChildren(int position, int count, int columns)
{ {
if(position<0 || position>childItems.size()) if(position<0 || position>m_childItems.size())
return false; return false;
for(int row = 0; row < count; row++) for(int row = 0; row < count; row++)
{ {
QVector<QVariant> data(columns); QVector<QVariant> data(columns);
TileItem *item = new TileItem(data, this); Node *item = new Node(data, this);
childItems.insert(position, item); m_childItems.insert(position, item);
} }
return true; return true;
} }
bool TileItem::removeChildren(int position, int count) bool Node::removeChildren(int position, int count)
{ {
if(position<0 || position+count>childItems.size()) if(position<0 || position+count>m_childItems.size())
return false; return false;
for(int row=0; row<count; row++) for(int row=0; row<count; row++)
delete childItems.takeAt(position); delete m_childItems.takeAt(position);
return true; return true;
} }
bool TileItem::insertColumns(int position, int columns) bool Node::insertColumns(int position, int columns)
{ {
if(position<0 || position > itemData.size()) if(position<0 || position > m_itemData.size())
return false; return false;
for(int column=0; column<columns; column++) for(int column=0; column<columns; column++)
itemData.insert(position, columns); m_itemData.insert(position, columns);
Q_FOREACH(TileItem *child, childItems) Q_FOREACH(Node *child, m_childItems)
child->insertColumns(position, columns); child->insertColumns(position, columns);
return true; return true;
} }
int TileItem::row() const int Node::row() const
{ {
if(parentItem) if(m_parentItem)
return parentItem->childItems.indexOf(const_cast<TileItem*>(this)); return m_parentItem->m_childItems.indexOf(const_cast<Node*>(this));
return 0; return 0;
} }
int TileItem::columnCount() const int Node::columnCount() const
{ {
return itemData.count(); return m_itemData.count();
} }
QVariant TileItem::data(int column) const QVariant Node::data(int column) const
{ {
return itemData.value(column); return m_itemData.value(column);
} }
bool TileItem::setData(int column, const QVariant &value) bool Node::setData(int column, const QVariant &value)
{ {
if(column < 0 || column >= itemData.size()) if(column < 0 || column >= m_itemData.size())
return false; return false;
itemData[column] = value; m_itemData[column] = value;
return true; return true;
} }
TileItem *TileItem::parent() Node *Node::parent()
{ {
return parentItem; return m_parentItem;
} }
void TileItem::setParent(TileItem *parent) void Node::setParent(Node *parent)
{ {
parentItem = parent; m_parentItem = parent;
} }
void TileItem::appendRow(const QList<TileItem*> &items) void Node::appendRow(const QList<Node*> &items)
{ {
Q_FOREACH(TileItem *item, items) Q_FOREACH(Node *item, items)
appendRow(item); appendRow(item);
} }
void TileItem::appendRow(TileItem *item) void Node::appendRow(Node *item)
{ {
item->setParent(this); item->setParent(this);
childItems.append(item); m_childItems.append(item);
} }
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
TileTypeTileItem::TileTypeTileItem(const QVector<QVariant> &data, TileItem *parent) : TileItem(data,parent) TileSetNode::TileSetNode(QString tileSetName, Node *parent) : m_tileSetName(tileSetName)
{ {
m_parentItem = parent;
} }
TileTypeTileItem::~TileTypeTileItem() TileSetNode::~TileSetNode()
{ {
qDeleteAll(childItems); qDeleteAll(m_childItems);
} }
QVariant TileTypeTileItem::data(int column) const QVariant TileSetNode::data(int column) const
{ {
QVariant val = itemData.value(column); return QVariant(m_tileSetName);
}
nlinfo("the column is %d and the value is '%s'. there are %d values",
column, val.toString().toStdString().c_str(), itemData.size()); ///////////////////////////////////////////////////
return itemData.value(column);
TileTypeNode::TileTypeNode(TNodeTileType type, Node *parent) : m_nodeTileType(type)
{
m_parentItem = parent;
}
TileTypeNode::~TileTypeNode()
{
qDeleteAll(m_childItems);
}
QVariant TileTypeNode::data(int column) const
{
return QVariant(getTileTypeName(m_nodeTileType));
}
TileTypeNode::TNodeTileType TileTypeNode::getTileType()
{
return m_nodeTileType;
}
const char *TileTypeNode::getTileTypeName(TNodeTileType type)
{
switch(type)
{
case Tile128:
return "128";
case Tile256:
return "256";
case TileTransition:
return "Transition";
case TileDisplacement:
return "Displacement";
default:
break;
}
return "UNKNOWN";
}
///////////////////////////////////////////////////
TileItemNode::TileItemNode(int tileId, TTileChannel channel, QString filename, Node *parent) : m_tileId(tileId)
{
m_tileFilename[channel] = filename;
m_parentItem = parent;
nlinfo("dispalying tile %d - %s", m_tileId, m_tileFilename[TileDiffuse].toStdString().c_str());
}
TileItemNode::~TileItemNode()
{
qDeleteAll(m_childItems);
}
void TileItemNode::setTileFilename(TTileChannel channel, QString filename)
{
m_tileFilename[channel] = filename;
}
QVariant TileItemNode::data(int column) const
{
nlinfo("dispalying tile %d - %s", m_tileId, m_tileFilename[TileDiffuse].toStdString().c_str());
// find some way to know which file/bitmap to display
return QVariant(m_tileFilename[TileDiffuse]);
} }

View file

@ -22,54 +22,90 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QVector> #include <QVector>
class TileItem class Node
{ {
public: public:
Node();
Node(const QVector<QVariant> &data, Node *parent=0);
virtual ~Node();
TileItem(const QVector<QVariant> &data, TileItem *parent=0); void appendChild(Node *child);
virtual ~TileItem();
void appendChild(TileItem *child); Node *child(int row);
TileItem *child(int row);
int childCount() const; int childCount() const;
int childNumber() const; int childNumber() const;
int columnCount() const; int columnCount() const;
bool setData(int column, const QVariant &value); bool setData(int column, const QVariant &value);
QVariant data(int column) const; virtual QVariant data(int column) const;
bool insertChildren(int position, int count, int columns); bool insertChildren(int position, int count, int columns);
bool removeChildren(int position, int count); bool removeChildren(int position, int count);
bool insertColumns(int position, int columns); bool insertColumns(int position, int columns);
int row() const; int row() const;
TileItem *parent(); Node *parent();
void setParent(TileItem *parent); void setParent(Node *parent);
void appendRow(const QList<TileItem*> &items); void appendRow(const QList<Node*> &items);
void appendRow(TileItem *item); void appendRow(Node *item);
//QImage *getTileImageFromChannel(int channel);
//int getTileIndex() { return m_tileIndex; }
//QString getTileFilename() { return m_tileFilename; }
protected: protected:
QList<TileItem*> childItems; QList<Node*> m_childItems;
QVector<QVariant> itemData; QVector<QVariant> m_itemData;
TileItem *parentItem; Node *m_parentItem;
//QMap<int, QImage*> m_tileChannels;
//int m_tileIndex;
//QString m_tileFilename;
}; };
class TileTypeTileItem : public TileItem class TileSetNode : public Node
{ {
public: public:
TileTypeTileItem(const QVector<QVariant> &data, TileItem *parent=0); TileSetNode(QString tileSetName, Node *parent=0);
virtual ~TileTypeTileItem(); virtual ~TileSetNode();
QVariant data(int column) const; QVariant data(int column) const;
const QString &getTileSetName();
private:
QString m_tileSetName;
};
class TileTypeNode : public Node
{
public:
enum TNodeTileType
{
Tile128 = 0,
Tile256 = 1,
TileTransition = 2,
TileDisplacement = 3
};
TileTypeNode(TNodeTileType type, Node *parent=0);
virtual ~TileTypeNode();
QVariant data(int column) const;
TNodeTileType getTileType();
static const char *getTileTypeName(TNodeTileType type);
private:
TNodeTileType m_nodeTileType;
};
class TileItemNode : public Node
{
public:
enum TTileChannel
{
TileDiffuse = 0,
TileAdditive = 1,
TileAlpha = 2,
};
TileItemNode(int tileId, TTileChannel channel, QString filename, Node *parent=0);
virtual ~TileItemNode();
QVariant data(int column) const;
void setTileFilename(TTileChannel channel, QString filename);
private:
int m_tileId;
QMap<TTileChannel, QString> m_tileFilename;
}; };
#endif // TILE_ITEM_H #endif // TILE_ITEM_H

View file

@ -25,7 +25,7 @@ TileModel::TileModel(const QStringList &headers, QObject *parent) : QAbstractIte
Q_FOREACH(QString header, headers) Q_FOREACH(QString header, headers)
rootData << header; rootData << header;
rootItem = new TileItem(rootData); rootItem = new Node(rootData);
} }
TileModel::~TileModel() TileModel::~TileModel()
@ -33,11 +33,11 @@ TileModel::~TileModel()
delete rootItem; delete rootItem;
} }
TileItem *TileModel::getItem(const QModelIndex &index) const Node *TileModel::getItem(const QModelIndex &index) const
{ {
if(index.isValid()) if(index.isValid())
{ {
TileItem *item = static_cast<TileItem*>(index.internalPointer()); Node *item = static_cast<Node*>(index.internalPointer());
if(item) return item; if(item) return item;
} }
return rootItem; return rootItem;
@ -48,9 +48,9 @@ QModelIndex TileModel::index(int row, int column, const QModelIndex &parent) con
if(parent.isValid() && parent.column() != 0) if(parent.isValid() && parent.column() != 0)
return QModelIndex(); return QModelIndex();
TileItem *parentItem = getItem(parent); Node *parentItem = getItem(parent);
TileItem *childItem = parentItem->child(row); Node *childItem = parentItem->child(row);
if(childItem) if(childItem)
return createIndex(row, column, childItem); return createIndex(row, column, childItem);
else else
@ -62,8 +62,8 @@ QModelIndex TileModel::parent(const QModelIndex &index) const
if(!index.isValid()) if(!index.isValid())
return QModelIndex(); return QModelIndex();
TileItem *childItem = getItem(index); Node *childItem = getItem(index);
TileItem *parentItem = childItem->parent(); Node *parentItem = childItem->parent();
if(parentItem == rootItem) if(parentItem == rootItem)
return QModelIndex(); return QModelIndex();
@ -73,13 +73,13 @@ QModelIndex TileModel::parent(const QModelIndex &index) const
int TileModel::rowCount(const QModelIndex &parent) const int TileModel::rowCount(const QModelIndex &parent) const
{ {
TileItem *parentItem = getItem(parent); Node *parentItem = getItem(parent);
return parentItem->childCount(); return parentItem->childCount();
} }
int TileModel::columnCount(const QModelIndex &parent) const int TileModel::columnCount(const QModelIndex &parent) const
{ {
TileItem *parentItem = getItem(parent); Node *parentItem = getItem(parent);
return parentItem->columnCount(); return parentItem->columnCount();
} }
@ -91,7 +91,7 @@ QVariant TileModel::data(const QModelIndex &index, int role) const
if(role != Qt::DisplayRole) if(role != Qt::DisplayRole)
return QVariant(); return QVariant();
TileItem *item = static_cast<TileItem*>(index.internalPointer()); Node *item = static_cast<Node*>(index.internalPointer());
return item->data(index.column()); return item->data(index.column());
} }
@ -111,13 +111,57 @@ QVariant TileModel::headerData(int section, Qt::Orientation orientation, int rol
return QVariant(); return QVariant();
} }
void TileModel::appendRow(const QList<TileItem*> &items) void TileModel::appendRow(const QList<Node*> &items)
{ {
rootItem->appendRow(items); rootItem->appendRow(items);
} }
void TileModel::appendRow(TileItem *item) void TileModel::appendRow(Node *item)
{ {
rootItem->appendRow(item); rootItem->appendRow(item);
} }
TileSetNode *TileModel::createTileSetNode(QString tileSetName)
{
// Create the new tile set.
TileSetNode *tileSet = new TileSetNode(tileSetName);
// child for 128x128 tiles
TileTypeNode *tile128= new TileTypeNode(TileTypeNode::Tile128);
// child for 256x256 tiles
TileTypeNode *tile256= new TileTypeNode(TileTypeNode::Tile256);
// child for transition tiles.
TileTypeNode *tileTrans= new TileTypeNode(TileTypeNode::TileTransition);
// Add the default transition tiles.
// TODO tie this to CTileSet::count from NeL
for(int transPos=0; transPos<48; transPos++)
{
TileItemNode *transTile= new TileItemNode(transPos, TileItemNode::TileDiffuse, QString("filename").append(QString::number(transPos+1)));
tileTrans->appendRow(transTile);
}
// child for displacement tiles
TileTypeNode *tileDisp= new TileTypeNode(TileTypeNode::TileDisplacement);
// Add the default displacement tiles.
// TODO tie this to CTileSet::CountDisplace from NeL
for(int dispPos=0; dispPos<16; dispPos++)
{
TileItemNode *dispTile= new TileItemNode(dispPos, TileItemNode::TileDiffuse, QString("filename").append(QString::number(dispPos+1)));
tileDisp->appendRow(dispTile);
}
// Append them in the correct order to the tile set.
tileSet->appendRow(tile128);
tileSet->appendRow(tile256);
tileSet->appendRow(tileTrans);
tileSet->appendRow(tileDisp);
this->appendRow(tileSet);
return tileSet;
}

View file

@ -21,7 +21,8 @@
#include <QAbstractListModel> #include <QAbstractListModel>
class TileItem; class Node;
class TileSetNode;
class TileModel : public QAbstractItemModel class TileModel : public QAbstractItemModel
{ {
@ -40,15 +41,17 @@ public:
int columnCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const;
// Tile Model specific functions // Tile Model specific functions
void appendRow(const QList<TileItem*> &items); void appendRow(const QList<Node*> &items);
void appendRow(TileItem *item); void appendRow(Node *item);
TileSetNode *createTileSetNode(QString tileSetName);
private: private:
TileItem *getItem(const QModelIndex &index) const; Node *getItem(const QModelIndex &index) const;
//QList<TileItem*> m_tiles; //QList<TileItem*> m_tiles;
//int m_activeEditChannel; //int m_activeEditChannel;
TileItem *rootItem; Node *rootItem;
}; };
#endif // TILE_128_MODEL_H #endif // TILE_MODEL_H