QTfrontend/team.h
author unc0rr
Wed, 26 Oct 2005 21:15:35 +0000
changeset 19 09de46a3328c
parent 18 d49c23eba8ba
permissions -rw-r--r--
Binds and various fixes

/*
 * Hedgewars, a worms-like game
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * Distributed under the terms of the BSD-modified licence:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * with the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <qstring.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qcombobox.h>
#include "hw.h"

class QString;
class QTextStream;
class QFile;
class QComboBox;

struct BindAction
{
	QComboBox * cbind;
	char action[15];
	char strbind[15];
};

const BindAction cbinds[8] =
{
	{0, "+up", "up"},
	{0, "+left", "left"},
	{0, "+right", "right"},
	{0, "+down", "down"},
	{0, "ljump", "return"},
	{0, "hjump", "backspace"},
	{0, "+attack", "space"},
	{0, "switch", "tab"}
};


class HWTeam
{
	public:
		HWTeam(HWForm * hwform)
		{
			TeamName = "unnamed";
			for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
			Grave = "Simple";
			form = hwform;
			memcpy(binds, cbinds, sizeof(cbinds));
			binds[0].cbind = form->CBindUp;
			binds[1].cbind = form->CBindLeft;
			binds[2].cbind = form->CBindRight;
			binds[3].cbind = form->CBindDown;
			binds[4].cbind = form->CBindLJump;
			binds[5].cbind = form->CBindHJump;
			binds[6].cbind = form->CBindAttack;
			binds[7].cbind = form->CBindSwitch;
		}
		
		HWForm * form;
		BindAction binds[8];
		QString TeamName;
		QString HHName[8];
		QString	Grave;
		QString Fort;
		
		bool LoadFromFile(const QString & filename)
		{
			QFile cfgfile(filename);
			if (!cfgfile.open(IO_ReadOnly)) return false;
			QTextStream stream(&cfgfile);
			stream.setEncoding(QTextStream::Unicode);	
			QString str;
			QString action;
			
			while (!stream.atEnd())
			{
				str = stream.readLine();
				if (str.startsWith(";")) continue;
				if (str.startsWith("name team "))
				{
					str.remove(0, 10);
					TeamName = str;
				} else
				if (str.startsWith("name hh"))
				{
					str.remove(0, 7);
					long i = str.left(1).toLong();
					if ((i < 0) || (i > 7)) continue;
					str.remove(0, 2);
					HHName[i] = str;
				} else
				if (str.startsWith("grave "))
				{
					str.remove(0, 6);
					Grave = str;
				} else
				if (str.startsWith("fort "))
				{
					str.remove(0, 5);
					Fort = str;
				} else
				if (str.startsWith("bind "))
				{
					str.remove(0, 5);
					action = str.section(' ', 1);
					str = str.section(' ', 0, 0);
					str.truncate(15);
					for (int i = 0; i < 8; i++)
						if (action == binds[i].action)
						{
							strcpy((char *)&binds[i].strbind, str.latin1());
							break;
						}
				}
			}
			cfgfile.close();
			return true;
		}
		
		bool SaveToFile(const QString & filename)
		{			
			QFile cfgfile(filename);
			if (!cfgfile.open(IO_WriteOnly)) return false;
			QTextStream stream(&cfgfile);
			stream.setEncoding(QTextStream::Unicode);
			stream << "; Generated by Hedgewars, do not modify" << endl;
			stream << "name team " << TeamName << endl;
			for (int i = 0; i < 8; i++)
				stream << "name hh" << i << " " << HHName[i] << endl;
			stream << "grave " << Grave << endl;
			stream << "fort " << Fort << endl;
			for(int i = 0; i < 8; i++)
			{
				stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
			}
			cfgfile.close();
			return true;
		}
		
		void ToPage()
		{
			form->EditTeamName->setText(TeamName);
			form->HHName0->setText(HHName[0]);
			form->HHName1->setText(HHName[1]);
			form->HHName2->setText(HHName[2]);
			form->HHName3->setText(HHName[3]);
			form->HHName4->setText(HHName[4]);
			form->HHName5->setText(HHName[5]);
			form->HHName6->setText(HHName[6]);
			form->HHName7->setText(HHName[7]);
			
			const QListBox * lb = form->CBGraves->listBox();
			form->CBGraves->setCurrentItem(lb->index(lb->findItem(Grave)));
			
			lb = form->CBForts->listBox();
			form->CBForts->setCurrentItem(lb->index(lb->findItem(Fort)));
			
			lb = form->CBindUp->listBox();
			for(int i = 0; i < 8; i++)
			{
				binds[i].cbind->setCurrentItem(lb->index(lb->findItem(binds[i].strbind)));
			}
		}
		
		void FromPage()
		{
			TeamName  = form->EditTeamName->text();
			HHName[0] = form->HHName0->text();
			HHName[1] = form->HHName1->text();
			HHName[2] = form->HHName2->text();
			HHName[3] = form->HHName3->text();
			HHName[4] = form->HHName4->text();
			HHName[5] = form->HHName5->text();
			HHName[6] = form->HHName6->text();
			HHName[7] = form->HHName7->text();
			
			Grave = form->CBGraves->currentText();
			Fort = form->CBForts->currentText();
			for(int i = 0; i < 8; i++)
			{
				strcpy((char *)&binds[i].strbind,  binds[i].cbind->currentText().latin1());
			}
		}
	private:
};