minirop avatar minirop committed e9d62fe

save/load done but still few bugs

Comments (0)

Files changed (5)

 	m_name = name;
 }
 
-const QString& Character::getName() const
+QString Character::getName() const
 {
 	return m_name;
 }
 
+QString Character::getExpression() const
+{
+	return m_currentExpression;
+}
+
 void Character::addExpression(const QString& name, const QString& image)
 {
 	if(m_defaultExpression.isEmpty())
 
 void Character::setExpression(const QString& name)
 {
+	m_currentExpression = name;
 	setPixmap(m_expressions[name]);
 	setFixedSize(m_expressions[name].size());
 	update();
 	Character();
 	
 	void setName(const QString& pName);
-	const QString& getName() const;
+	QString getName() const;
 	
 	void addExpression(const QString& name, const QString& image);
 	void setDefaultExpression();
 	void setExpression(const QString& name);
+	QString getExpression() const;
 	
 	void setStyle(const QString& style);
 	const QString& getStyle() const;
 private:
 	QString m_name;
 	QString m_style;
+	QString m_currentExpression;
 	QHash<QString, QPixmap> m_expressions;
 	QString m_defaultExpression;
 };
 	loadPlugins();
 	
 	currentSavedPos = 0;
+	
+	saveBtn = 0;
 }
 
 GameWidget::~GameWidget()
 	SoundSystem::release();
 }
 
-void GameWidget::load(const QString& savefile)
+void GameWidget::saveGame(const QString& savefile)
+{
+	QDomDocument saveXML;
+	QDomElement saveNode = saveXML.createElement("save");
+	saveXML.appendChild(saveNode);
+	
+	//add variables
+	QHashIterator<QString, QString> i(m_variables);
+	while(i.hasNext())
+	{
+		i.next();
+		
+		QDomElement variableNode = saveXML.createElement("variable");
+		variableNode.setAttribute("name", i.key());
+		variableNode.setAttribute("value", i.value());
+		
+		saveNode.appendChild(variableNode);
+	}
+	
+	//add characters
+	QHashIterator<QString, Character *> ii(m_characters);
+	while(ii.hasNext())
+	{
+		ii.next();
+		
+		Character * c = ii.value();
+		
+		QDomElement characterNode = saveXML.createElement("character");
+		characterNode.setAttribute("name", c->getName());
+		characterNode.setAttribute("expression", c->getExpression());
+		characterNode.setAttribute("x", QString::number(c->x()));
+		characterNode.setAttribute("y", QString::number(c->y()));
+		characterNode.setAttribute("show", (c->isVisible() ? "1" : "0"));
+		saveNode.appendChild(characterNode);
+	}
+	
+	//
+	QStringList positionString;
+	foreach(int k, savedPositions)
+	{
+		positionString += QString::number(k);
+	}
+	QDomElement variableNode = saveXML.createElement("saved");
+	variableNode.setAttribute("values", positionString.join(","));
+	saveNode.appendChild(variableNode);
+	
+	// background
+	QDomElement backgroundNode = saveXML.createElement("background");
+	backgroundNode.setAttribute("src", lastLoadedBackground);
+	saveNode.appendChild(backgroundNode);
+	
+	// arc
+	QDomElement arcNode = saveXML.createElement("arc");
+	arcNode.setAttribute("name", lastLoadedArc);
+	saveNode.appendChild(arcNode);
+	
+	// check dir and file
+	QDir d(QCoreApplication::applicationDirPath());
+	if(!d.exists("saves"))
+		d.mkdir("saves");
+	
+	QFile saveQFile(QCoreApplication::applicationDirPath() + "/saves/1.xml");
+	if(!saveQFile.open(QFile::WriteOnly | QIODevice::Truncate | QIODevice::Text))
+	{
+		QMessageBox::critical(this, "ERROR", tr("can't open 'saves/1.xml' for writing"));
+		return;
+	}
+	
+	saveQFile.write(saveXML.toByteArray(4));
+	saveQFile.close();
+}
+
+void GameWidget::loadGame(const QString& savefile)
 {
 	QDomDocument doc("savegame");
 	QFile file("saves/"+savefile+".xml");
 		else if(elems.tagName() == "saved")
 		{
 			QStringList pushed = elems.attribute("values").split(",");
+			qDebug() << "saved element" << elems.attribute("values") << pushed;
 			foreach(QString push, pushed)
 			{
 				savedPositions.push_back(push.toInt());
 		
 		elems = elems.nextSiblingElement();
 	}
+	currentSavedPos = 0;
 	
 	/*
 	loadArc();
 		i.value()->setDefaultExpression();
 	}
 	
-	load("1");
+	loadGame("1");
 	
 	loadArc();
 	if(!MainWindow::isClosing)
 			
 			m_textbox->setFont(police);
 		}
+		else if(child.tagName() == "save")
+		{
+			int x = child.attribute("x", "0").toUInt();
+			int y = child.attribute("y", "0").toUInt();
+			
+			if(!saveBtn)
+			{
+				saveBtn = new PushButton(child.attribute("normal"), child.attribute("over"));
+				saveBtn->setParent(m_textbox);
+				saveBtn->move(x, y);
+				
+				connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveGame()));
+			}
+			else
+			{
+				Log("Only one <save> button allowed");
+			}
+		}
 		
 		child = child.nextSiblingElement();
 	}
 void GameWidget::evaluateList(QDomNodeList list, int nested)
 {
 	uint i = 0;
+	qDebug() << savedPositions;
 	if(currentSavedPos < savedPositions.size())
 	{
 		i = savedPositions[ currentSavedPos ];
+		qDebug() << currentSavedPos << "=>" << i;
+		currentSavedPos++;
 	}
 	else
 	{
 #include <QCloseEvent>
 
 #include "PluginInterfaces.h"
+#include "PushButton.h"
 
 class Character;
 class Textbox;
 	GameWidget();
 	~GameWidget();
 	
-	void load(const QString& savefile);
-	
 	void setVariable(const QString& name, const QString& value);
 	QString getVariable(const QString& name) const;
 	
 	void manageCloseEvent();
 
 private slots:
+	void saveGame(const QString& savefile = "1");
+	void loadGame(const QString& savefile);
+	
 	void evaluate();
 	void setTempValue(const QString& value);
 
 	QString parseVariable(QString text);
 	int getPosFromName(QString name, Character * c, bool * ok);
 	
+	// buttons
+	PushButton * saveBtn;
+	
 	// variables
 	QHash<QString, QString> m_variables;
 	QHash<QString, Character*> m_characters;
 	setAlignment(Qt::AlignLeft | Qt::AlignTop);
 	setFocusPolicy(Qt::StrongFocus);
 	
-	QPalette p = palette();
-	p.setColor(QPalette::WindowText, Qt::white);
-	setPalette(p);
-	
 	setObjectName("textbox");
 	setWordWrap(true);
 	
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.