1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
26 #include "vogleditor_statetreeitem.h"
27 #include "vogleditor_qstatetreemodel.h"
29 //=============================================================================
31 vogleditor_stateTreeItem::vogleditor_stateTreeItem(QList<QVariant> columnTitles, vogleditor_QStateTreeModel* pModel)
32 : m_columnData(columnTitles),
39 vogleditor_stateTreeItem::vogleditor_stateTreeItem(QString name, QString value, vogleditor_stateTreeItem* parent)
40 : m_parentItem(parent),
44 m_columnData << value;
46 if (m_parentItem != NULL)
48 m_pModel = m_parentItem->m_pModel;
52 vogleditor_stateTreeItem::~vogleditor_stateTreeItem()
54 for (int i = 0; i < m_childItems.size(); i++)
56 delete m_childItems[i];
57 m_childItems[i] = NULL;
63 vogleditor_stateTreeItem* vogleditor_stateTreeItem::parent() const
68 void vogleditor_stateTreeItem::setValue(QString value)
70 if (m_columnData.size() >= 2)
72 m_columnData.removeAt(1);
73 m_columnData.insert(1, value);
77 QString vogleditor_stateTreeItem::getValueFromBools(const bool* values, uint count) const
79 if (count == 0 || values == NULL)
85 return (values[0]) ? "GL_TRUE" : "GL_FALSE";
89 tmp = tmp.sprintf("%s, %s", getValueFromBools(values, 1).toStdString().c_str(), getValueFromBools(&(values[1]), count-1).toStdString().c_str());
93 QString vogleditor_stateTreeItem::getValueFromInts(const int* values, uint count) const
96 if (count == 0 || values == NULL)
102 tmp = tmp.sprintf("%d", *values);
106 tmp = tmp.sprintf("%s, %s", getValueFromInts(values, 1).toStdString().c_str(), getValueFromInts(&(values[1]), count-1).toStdString().c_str());
112 QString vogleditor_stateTreeItem::getValueFromUints(const uint* values, uint count) const
115 if (count == 0 || values == NULL)
121 tmp = tmp.sprintf("%d", *values);
125 tmp = tmp.sprintf("%s, %s", getValueFromUints(values, 1).toStdString().c_str(), getValueFromUints(&(values[1]), count-1).toStdString().c_str());
131 QString vogleditor_stateTreeItem::getValueFromFloats(const float* values, uint count) const
134 if (count == 0 || values == NULL)
140 tmp = tmp.sprintf("%f", *values);
144 tmp = tmp.sprintf("%s, %s", getValueFromFloats(values, 1).toStdString().c_str(), getValueFromFloats(&(values[1]), count-1).toStdString().c_str());
150 QString vogleditor_stateTreeItem::getValueFromDoubles(const double* values, uint count) const
153 if (count == 0 || values == NULL)
159 tmp = tmp.sprintf("%f", *values);
163 tmp = tmp.sprintf("%s, %s", getValueFromDoubles(values, 1).toStdString().c_str(), getValueFromDoubles(&(values[1]), count-1).toStdString().c_str());
169 QString vogleditor_stateTreeItem::getValueFromEnums(const int* values, uint count) const
172 if (count == 0 || values == NULL)
178 tmp = enum_to_string(*values);
182 tmp = tmp.sprintf("%s, %s", getValueFromEnums(values, 1).toStdString().c_str(), getValueFromEnums(&(values[1]), count-1).toStdString().c_str());
188 QString vogleditor_stateTreeItem::getValueFromPtrs(const int* values, uint count) const
191 if (count == 0 || values == NULL)
197 tmp = tmp.sprintf("%p", (void*)(*values));
201 tmp = tmp.sprintf("%s, %s", getValueFromPtrs(values, 1).toStdString().c_str(), getValueFromPtrs(&(values[1]), count-1).toStdString().c_str());
207 void vogleditor_stateTreeItem::appendChild(vogleditor_stateTreeItem* pChild)
209 m_childItems.append(pChild);
212 int vogleditor_stateTreeItem::childCount() const
214 return m_childItems.size();
217 vogleditor_stateTreeItem* vogleditor_stateTreeItem::child(int index) const
219 if (index < 0 || index >= childCount())
224 return m_childItems[index];
227 int vogleditor_stateTreeItem::columnCount() const
230 if (m_parentItem == NULL)
232 // this must be the root node
233 count = m_columnData.size();
237 m_pModel->columnCount();
243 QVariant vogleditor_stateTreeItem::columnData(int column, int role) const
245 if (column >= m_columnData.size())
250 if (role == Qt::ForegroundRole && parent() != NULL && hasChanged())
252 return QVariant(Qt::red);
255 if (role == Qt::ToolTipRole)
259 QString prevValue = getDiffedValue();
260 if (prevValue.isEmpty())
263 return "Previous value was: " + prevValue;
267 // catch any other roles that we don't specifically handle
268 if (role != Qt::DisplayRole)
273 // return the data since the only role left is the DisplayRole
274 return m_columnData[column];
277 int vogleditor_stateTreeItem::row() const
279 // note, this is just the row within the current level of the hierarchy
281 return m_parentItem->m_childItems.indexOf(const_cast<vogleditor_stateTreeItem*>(this));
286 bool vogleditor_stateTreeItem::hasChanged() const
288 for (int i = 0; i < m_childItems.size(); i++)
290 if (m_childItems[i]->hasChanged())
299 //=============================================================================
301 template <typename T> vogleditor_stateTreeDatatypeItem<T>::vogleditor_stateTreeDatatypeItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
302 : vogleditor_stateTreeStateVecDiffableItem(glenumName, "", parent),
305 m_numComponents(numComponents),
306 m_isIndexed(isIndexed),
307 m_pStateVec(&stateVec)
311 template <typename T> bool vogleditor_stateTreeDatatypeItem<T>::hasChanged() const
313 if (m_pDiffBaseState == NULL)
316 static T baseValues[4];
317 static T curValues[4];
318 VOGL_ASSERT(m_numComponents <= 4);
320 bool bFoundInBase = m_pDiffBaseState->get<T>(m_name, m_index, baseValues, m_numComponents, m_isIndexed);
321 bool bFoundInCurrent = m_pStateVec->get<T>(m_name, m_index, curValues, m_numComponents, m_isIndexed);
323 if (bFoundInBase && bFoundInCurrent)
325 for (unsigned int i = 0; i < m_numComponents; i++)
327 if (baseValues[i] != curValues[i])
329 // one of the values has changed, so return early
335 if (bFoundInCurrent && !bFoundInBase)
337 // the enum must have been added, so it is different
344 template <typename T> QString vogleditor_stateTreeDatatypeItem<T>::getDiffedValue() const
346 static T baseValues[4];
347 VOGL_ASSERT(m_numComponents <= 4);
349 bool bFoundInBase = m_pDiffBaseState->get<T>(m_name, m_index, baseValues, m_numComponents, m_isIndexed);
354 result = "TODO: An actual diff'ed value";
361 //=============================================================================
362 vogleditor_stateTreeStateVecBoolItem::vogleditor_stateTreeStateVecBoolItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, bool* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
363 : vogleditor_stateTreeDatatypeItem<bool>(glenumName, name, index, stateVec, numComponents, isIndexed, parent)
365 setValue(getValueFromBools(values, numComponents));
368 vogleditor_stateTreeStateVecBoolItem::vogleditor_stateTreeStateVecBoolItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, int* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
369 : vogleditor_stateTreeDatatypeItem<bool>(glenumName, name, index, stateVec, numComponents, isIndexed, parent)
371 bool bVals[4] = {values[0] != 0, values[1] != 0, values[2] != 0, values[3] != 0 };
372 setValue(getValueFromBools(bVals, numComponents));
375 QString vogleditor_stateTreeStateVecBoolItem::getDiffedValue() const
377 static bool baseValues[4];
378 VOGL_ASSERT(m_numComponents <= 4);
381 if (m_pDiffBaseState->get<bool>(m_name, m_index, baseValues, m_numComponents, m_isIndexed))
383 result = getValueFromBools(baseValues, m_numComponents);
389 //=============================================================================
391 vogleditor_stateTreeStateVecIntItem::vogleditor_stateTreeStateVecIntItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, int* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
392 : vogleditor_stateTreeDatatypeItem<int>(glenumName, name, index, stateVec, numComponents, isIndexed, parent)
394 setValue(getValueFromInts(values, numComponents));
397 QString vogleditor_stateTreeStateVecIntItem::getDiffedValue() const
399 static int baseValues[4];
400 VOGL_ASSERT(m_numComponents <= 4);
403 if (m_pDiffBaseState->get<int>(m_name, m_index, baseValues, m_numComponents, m_isIndexed))
405 result = getValueFromInts(baseValues, m_numComponents);
411 //=============================================================================
413 vogleditor_stateTreeStateVecPtrItem::vogleditor_stateTreeStateVecPtrItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, int* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
414 : vogleditor_stateTreeDatatypeItem<int>(glenumName, name, index, stateVec, numComponents, isIndexed, parent)
416 setValue(getValueFromPtrs(values, numComponents));
419 QString vogleditor_stateTreeStateVecPtrItem::getDiffedValue() const
421 static int baseValues[4];
422 VOGL_ASSERT(m_numComponents <= 4);
425 if (m_pDiffBaseState->get<int>(m_name, m_index, baseValues, m_numComponents, m_isIndexed))
427 result = getValueFromPtrs(baseValues, m_numComponents);
433 //=============================================================================
435 vogleditor_stateTreeStateVecFloatItem::vogleditor_stateTreeStateVecFloatItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, float* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
436 : vogleditor_stateTreeDatatypeItem<float>(glenumName, name, index, stateVec, numComponents, isIndexed, parent)
438 setValue(getValueFromFloats(values, numComponents));
441 QString vogleditor_stateTreeStateVecFloatItem::getDiffedValue() const
443 static float baseValues[4];
444 VOGL_ASSERT(m_numComponents <= 4);
447 if (m_pDiffBaseState->get<float>(m_name, m_index, baseValues, m_numComponents, m_isIndexed))
449 result = getValueFromFloats(baseValues, m_numComponents);
455 //=============================================================================
457 vogleditor_stateTreeStateVecEnumItem::vogleditor_stateTreeStateVecEnumItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, int* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
458 : vogleditor_stateTreeDatatypeItem<int>(glenumName, name, index, stateVec, numComponents, isIndexed, parent)
460 setValue(getValueFromEnums(values, numComponents));
463 QString vogleditor_stateTreeStateVecEnumItem::getDiffedValue() const
465 static int baseValues[4];
466 VOGL_ASSERT(m_numComponents <= 4);
469 if (m_pDiffBaseState->get<int>(m_name, m_index, baseValues, m_numComponents, m_isIndexed))
471 result = getValueFromEnums(baseValues, m_numComponents);
477 //=============================================================================
479 vogleditor_stateTreeStateVecMatrixRowItem::vogleditor_stateTreeStateVecMatrixRowItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, float* values, unsigned int numComponents, unsigned int rowIndex, bool isIndexed, vogleditor_stateTreeItem* parent)
480 : vogleditor_stateTreeStateVecDiffableItem(glenumName, "", parent),
483 m_numComponents(numComponents),
484 m_isIndexed(isIndexed),
488 setValue(getValueFromFloats(values, 4));
491 bool vogleditor_stateTreeStateVecMatrixRowItem::hasChanged() const
493 if (m_pDiffBaseState == NULL)
498 static float baseValues[16];
499 static float curValues[16];
500 VOGL_ASSERT(m_numComponents <= 16);
502 if (m_pDiffBaseState->get<float>(m_name, m_index, baseValues, m_numComponents, m_isIndexed) &&
503 m_pState->get<float>(m_name, m_index, curValues, m_numComponents, m_isIndexed))
505 for(unsigned int i = 0; i < 4; i++)
507 if (baseValues[i + (m_rowIndex*4)] != curValues[i + (m_rowIndex*4)])
509 // one of the values has changed, so return early
516 // the enum must have been added, so it is different
522 QString vogleditor_stateTreeStateVecMatrixRowItem::getDiffedValue() const
524 static float baseValues[16];
525 VOGL_ASSERT(m_numComponents <= 16);
528 if (m_pDiffBaseState->get<float>(m_name, m_index, baseValues, m_numComponents, m_isIndexed))
530 result = getValueFromFloats(&(baseValues[m_rowIndex*4]), 4);
536 vogleditor_stateTreeStateVecMatrixItem::vogleditor_stateTreeStateVecMatrixItem(QString glenumName, GLenum name, unsigned int index, const vogl_state_vector& stateVec, float* values, unsigned int numComponents, bool isIndexed, vogleditor_stateTreeItem* parent)
537 : vogleditor_stateTreeStateVecDiffableItem(glenumName, "", parent),
540 vogleditor_stateTreeStateVecMatrixRowItem* pRow1 = new vogleditor_stateTreeStateVecMatrixRowItem("row 0", name, index, stateVec, &(values[0]), numComponents, 0, isIndexed, this); m_rowItems.push_back(pRow1); this->appendChild(pRow1);
541 vogleditor_stateTreeStateVecMatrixRowItem* pRow2 = new vogleditor_stateTreeStateVecMatrixRowItem("row 1", name, index, stateVec, &(values[4]), numComponents, 1, isIndexed, this); m_rowItems.push_back(pRow2); this->appendChild(pRow2);
542 vogleditor_stateTreeStateVecMatrixRowItem* pRow3 = new vogleditor_stateTreeStateVecMatrixRowItem("row 2", name, index, stateVec, &(values[8]), numComponents, 2, isIndexed, this); m_rowItems.push_back(pRow3); this->appendChild(pRow3);
543 vogleditor_stateTreeStateVecMatrixRowItem* pRow4 = new vogleditor_stateTreeStateVecMatrixRowItem("row 3", name, index, stateVec, &(values[12]), numComponents, 3, isIndexed, this); m_rowItems.push_back(pRow4); this->appendChild(pRow4);