QTfrontend/team.h
changeset 20 ccd2c45f043d
parent 19 09de46a3328c
child 21 dff476dcaaa3
equal deleted inserted replaced
19:09de46a3328c 20:ccd2c45f043d
     1 /*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  */
       
    33 
       
    34 #include <qstring.h>
       
    35 #include <qfile.h>
       
    36 #include <qtextstream.h>
       
    37 #include <qcombobox.h>
       
    38 #include "hw.h"
       
    39 
       
    40 class QString;
       
    41 class QTextStream;
       
    42 class QFile;
       
    43 class QComboBox;
       
    44 
       
    45 struct BindAction
       
    46 {
       
    47 	QComboBox * cbind;
       
    48 	char action[15];
       
    49 	char strbind[15];
       
    50 };
       
    51 
       
    52 const BindAction cbinds[8] =
       
    53 {
       
    54 	{0, "+up", "up"},
       
    55 	{0, "+left", "left"},
       
    56 	{0, "+right", "right"},
       
    57 	{0, "+down", "down"},
       
    58 	{0, "ljump", "return"},
       
    59 	{0, "hjump", "backspace"},
       
    60 	{0, "+attack", "space"},
       
    61 	{0, "switch", "tab"}
       
    62 };
       
    63 
       
    64 
       
    65 class HWTeam
       
    66 {
       
    67 	public:
       
    68 		HWTeam(HWForm * hwform)
       
    69 		{
       
    70 			TeamName = "unnamed";
       
    71 			for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
       
    72 			Grave = "Simple";
       
    73 			form = hwform;
       
    74 			memcpy(binds, cbinds, sizeof(cbinds));
       
    75 			binds[0].cbind = form->CBindUp;
       
    76 			binds[1].cbind = form->CBindLeft;
       
    77 			binds[2].cbind = form->CBindRight;
       
    78 			binds[3].cbind = form->CBindDown;
       
    79 			binds[4].cbind = form->CBindLJump;
       
    80 			binds[5].cbind = form->CBindHJump;
       
    81 			binds[6].cbind = form->CBindAttack;
       
    82 			binds[7].cbind = form->CBindSwitch;
       
    83 		}
       
    84 		
       
    85 		HWForm * form;
       
    86 		BindAction binds[8];
       
    87 		QString TeamName;
       
    88 		QString HHName[8];
       
    89 		QString	Grave;
       
    90 		QString Fort;
       
    91 		
       
    92 		bool LoadFromFile(const QString & filename)
       
    93 		{
       
    94 			QFile cfgfile(filename);
       
    95 			if (!cfgfile.open(IO_ReadOnly)) return false;
       
    96 			QTextStream stream(&cfgfile);
       
    97 			stream.setEncoding(QTextStream::Unicode);	
       
    98 			QString str;
       
    99 			QString action;
       
   100 			
       
   101 			while (!stream.atEnd())
       
   102 			{
       
   103 				str = stream.readLine();
       
   104 				if (str.startsWith(";")) continue;
       
   105 				if (str.startsWith("name team "))
       
   106 				{
       
   107 					str.remove(0, 10);
       
   108 					TeamName = str;
       
   109 				} else
       
   110 				if (str.startsWith("name hh"))
       
   111 				{
       
   112 					str.remove(0, 7);
       
   113 					long i = str.left(1).toLong();
       
   114 					if ((i < 0) || (i > 7)) continue;
       
   115 					str.remove(0, 2);
       
   116 					HHName[i] = str;
       
   117 				} else
       
   118 				if (str.startsWith("grave "))
       
   119 				{
       
   120 					str.remove(0, 6);
       
   121 					Grave = str;
       
   122 				} else
       
   123 				if (str.startsWith("fort "))
       
   124 				{
       
   125 					str.remove(0, 5);
       
   126 					Fort = str;
       
   127 				} else
       
   128 				if (str.startsWith("bind "))
       
   129 				{
       
   130 					str.remove(0, 5);
       
   131 					action = str.section(' ', 1);
       
   132 					str = str.section(' ', 0, 0);
       
   133 					str.truncate(15);
       
   134 					for (int i = 0; i < 8; i++)
       
   135 						if (action == binds[i].action)
       
   136 						{
       
   137 							strcpy((char *)&binds[i].strbind, str.latin1());
       
   138 							break;
       
   139 						}
       
   140 				}
       
   141 			}
       
   142 			cfgfile.close();
       
   143 			return true;
       
   144 		}
       
   145 		
       
   146 		bool SaveToFile(const QString & filename)
       
   147 		{			
       
   148 			QFile cfgfile(filename);
       
   149 			if (!cfgfile.open(IO_WriteOnly)) return false;
       
   150 			QTextStream stream(&cfgfile);
       
   151 			stream.setEncoding(QTextStream::Unicode);
       
   152 			stream << "; Generated by Hedgewars, do not modify" << endl;
       
   153 			stream << "name team " << TeamName << endl;
       
   154 			for (int i = 0; i < 8; i++)
       
   155 				stream << "name hh" << i << " " << HHName[i] << endl;
       
   156 			stream << "grave " << Grave << endl;
       
   157 			stream << "fort " << Fort << endl;
       
   158 			for(int i = 0; i < 8; i++)
       
   159 			{
       
   160 				stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
       
   161 			}
       
   162 			cfgfile.close();
       
   163 			return true;
       
   164 		}
       
   165 		
       
   166 		void ToPage()
       
   167 		{
       
   168 			form->EditTeamName->setText(TeamName);
       
   169 			form->HHName0->setText(HHName[0]);
       
   170 			form->HHName1->setText(HHName[1]);
       
   171 			form->HHName2->setText(HHName[2]);
       
   172 			form->HHName3->setText(HHName[3]);
       
   173 			form->HHName4->setText(HHName[4]);
       
   174 			form->HHName5->setText(HHName[5]);
       
   175 			form->HHName6->setText(HHName[6]);
       
   176 			form->HHName7->setText(HHName[7]);
       
   177 			
       
   178 			const QListBox * lb = form->CBGraves->listBox();
       
   179 			form->CBGraves->setCurrentItem(lb->index(lb->findItem(Grave)));
       
   180 			
       
   181 			lb = form->CBForts->listBox();
       
   182 			form->CBForts->setCurrentItem(lb->index(lb->findItem(Fort)));
       
   183 			
       
   184 			lb = form->CBindUp->listBox();
       
   185 			for(int i = 0; i < 8; i++)
       
   186 			{
       
   187 				binds[i].cbind->setCurrentItem(lb->index(lb->findItem(binds[i].strbind)));
       
   188 			}
       
   189 		}
       
   190 		
       
   191 		void FromPage()
       
   192 		{
       
   193 			TeamName  = form->EditTeamName->text();
       
   194 			HHName[0] = form->HHName0->text();
       
   195 			HHName[1] = form->HHName1->text();
       
   196 			HHName[2] = form->HHName2->text();
       
   197 			HHName[3] = form->HHName3->text();
       
   198 			HHName[4] = form->HHName4->text();
       
   199 			HHName[5] = form->HHName5->text();
       
   200 			HHName[6] = form->HHName6->text();
       
   201 			HHName[7] = form->HHName7->text();
       
   202 			
       
   203 			Grave = form->CBGraves->currentText();
       
   204 			Fort = form->CBForts->currentText();
       
   205 			for(int i = 0; i < 8; i++)
       
   206 			{
       
   207 				strcpy((char *)&binds[i].strbind,  binds[i].cbind->currentText().latin1());
       
   208 			}
       
   209 		}
       
   210 	private:
       
   211 };