File indexing completed on 2024-05-12 05:39:28
0001 /*************************************************************************** 0002 * Copyright (C) 2015 by Renaud Guezennec * 0003 * http:://www.rolisteam.org/contact * 0004 * * 0005 * rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "paintnode.h" 0021 0022 ColorItem::ColorItem(QString str, int val) : m_colorNumber(val), m_color(str) {} 0023 0024 int ColorItem::colorNumber() const 0025 { 0026 return m_colorNumber; 0027 } 0028 0029 void ColorItem::setColorNumber(int colorNumber) 0030 { 0031 m_colorNumber= colorNumber; 0032 } 0033 0034 QString ColorItem::color() const 0035 { 0036 return m_color; 0037 } 0038 0039 void ColorItem::setColor(const QString& color) 0040 { 0041 m_color= color; 0042 } 0043 0044 /////////////////////////////////// 0045 /// @brief PainterNode::PainterNode 0046 /////////////////////////////////// 0047 0048 PainterNode::PainterNode() : ExecutionNode() 0049 { 0050 m_nextNode= nullptr; 0051 } 0052 0053 PainterNode::~PainterNode() 0054 { 0055 m_result= nullptr; 0056 } 0057 0058 void PainterNode::run(ExecutionNode* previous) 0059 { 0060 m_previousNode= previous; 0061 if(nullptr == previous) 0062 { 0063 m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Paint operator")); 0064 return; 0065 } 0066 Result* previousResult= previous->getResult(); 0067 if(nullptr == previousResult) 0068 return; 0069 0070 m_diceResult= dynamic_cast<DiceResult*>(previousResult->getCopy()); 0071 if(nullptr != m_diceResult) 0072 { 0073 QList<Die*> diceList= m_diceResult->getResultList(); 0074 int pastDice= 0; 0075 for(ColorItem& item : m_colors) 0076 { 0077 int current= item.colorNumber(); 0078 QList<Die*>::iterator it; 0079 for(it= diceList.begin() + pastDice; it != diceList.end() && current > 0; ++it) 0080 { 0081 (*it)->setColor(item.color()); 0082 --current; 0083 ++pastDice; 0084 } 0085 } 0086 m_diceResult->setPrevious(previousResult); 0087 m_result= m_diceResult; 0088 } 0089 if(nullptr != m_nextNode) 0090 { 0091 m_nextNode->run(this); 0092 } 0093 } 0094 0095 QString PainterNode::toString(bool wl) const 0096 { 0097 if(wl) 0098 { 0099 return QString("%1 [label=\"PainterNode\"]").arg(m_id); 0100 } 0101 else 0102 { 0103 return m_id; 0104 } 0105 } 0106 0107 qint64 PainterNode::getPriority() const 0108 { 0109 return 4; 0110 } 0111 0112 void PainterNode::insertColorItem(QString color, int value) 0113 { 0114 ColorItem item(color, value); 0115 m_colors.append(item); 0116 } 0117 ExecutionNode* PainterNode::getCopy() const 0118 { 0119 PainterNode* node= new PainterNode(); 0120 if(nullptr != m_nextNode) 0121 { 0122 node->setNextNode(m_nextNode->getCopy()); 0123 } 0124 return node; 0125 }