QTfrontend/team.h
changeset 19 09de46a3328c
parent 18 d49c23eba8ba
equal deleted inserted replaced
18:d49c23eba8ba 19:09de46a3328c
    32  */
    32  */
    33 
    33 
    34 #include <qstring.h>
    34 #include <qstring.h>
    35 #include <qfile.h>
    35 #include <qfile.h>
    36 #include <qtextstream.h>
    36 #include <qtextstream.h>
       
    37 #include <qcombobox.h>
    37 #include "hw.h"
    38 #include "hw.h"
    38 
    39 
    39 class QString;
    40 class QString;
    40 class QTextStream;
    41 class QTextStream;
    41 class QFile;
    42 class QFile;
    42 class HWForm;
    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 
    43 
    64 
    44 class HWTeam
    65 class HWTeam
    45 {
    66 {
    46 	public:
    67 	public:
    47 		HWTeam()
    68 		HWTeam(HWForm * hwform)
    48 		{
    69 		{
    49 			TeamName = "unnamed";
    70 			TeamName = "unnamed";
    50 			for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
    71 			for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
    51 			Grave = "Simple";
    72 			Grave = "Simple";
    52 		}
    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];
    53 		QString TeamName;
    87 		QString TeamName;
    54 		QString HHName[8];
    88 		QString HHName[8];
    55 		QString	Grave;
    89 		QString	Grave;
    56 		QString Fort;
    90 		QString Fort;
    57 		
    91 		
    60 			QFile cfgfile(filename);
    94 			QFile cfgfile(filename);
    61 			if (!cfgfile.open(IO_ReadOnly)) return false;
    95 			if (!cfgfile.open(IO_ReadOnly)) return false;
    62 			QTextStream stream(&cfgfile);
    96 			QTextStream stream(&cfgfile);
    63 			stream.setEncoding(QTextStream::Unicode);	
    97 			stream.setEncoding(QTextStream::Unicode);	
    64 			QString str;
    98 			QString str;
       
    99 			QString action;
    65 			
   100 			
    66 			while (!stream.atEnd())
   101 			while (!stream.atEnd())
    67 			{
   102 			{
    68 				str = stream.readLine();
   103 				str = stream.readLine();
    69 				if (str.startsWith(";")) continue;
   104 				if (str.startsWith(";")) continue;
    70 				if (str.startsWith("teamname "))
   105 				if (str.startsWith("name team "))
    71 				{
   106 				{
    72 					str.remove(0, 9);
   107 					str.remove(0, 10);
    73 					TeamName = str;
   108 					TeamName = str;
    74 				} else
   109 				} else
    75 				if (str.startsWith("name"))
   110 				if (str.startsWith("name hh"))
    76 				{
   111 				{
    77 					str.remove(0, 4);
   112 					str.remove(0, 7);
    78 					long i = str.left(1).toLong();
   113 					long i = str.left(1).toLong();
    79 					if ((i < 0) || (i > 7)) continue;
   114 					if ((i < 0) || (i > 7)) continue;
    80 					str.remove(0, 2);
   115 					str.remove(0, 2);
    81 					HHName[i] = str;
   116 					HHName[i] = str;
    82 				} else
   117 				} else
    87 				} else
   122 				} else
    88 				if (str.startsWith("fort "))
   123 				if (str.startsWith("fort "))
    89 				{
   124 				{
    90 					str.remove(0, 5);
   125 					str.remove(0, 5);
    91 					Fort = str;
   126 					Fort = str;
    92 				}				
   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 				}
    93 			}
   141 			}
    94 			cfgfile.close();
   142 			cfgfile.close();
    95 			return true;
   143 			return true;
    96 		}
   144 		}
    97 		
   145 		
   100 			QFile cfgfile(filename);
   148 			QFile cfgfile(filename);
   101 			if (!cfgfile.open(IO_WriteOnly)) return false;
   149 			if (!cfgfile.open(IO_WriteOnly)) return false;
   102 			QTextStream stream(&cfgfile);
   150 			QTextStream stream(&cfgfile);
   103 			stream.setEncoding(QTextStream::Unicode);
   151 			stream.setEncoding(QTextStream::Unicode);
   104 			stream << "; Generated by Hedgewars, do not modify" << endl;
   152 			stream << "; Generated by Hedgewars, do not modify" << endl;
   105 			stream << "teamname " << TeamName << endl;
   153 			stream << "name team " << TeamName << endl;
   106 			for (int i = 0; i < 8; i++)
   154 			for (int i = 0; i < 8; i++)
   107 				stream << "name" << i << " " << HHName[i] << endl;
   155 				stream << "name hh" << i << " " << HHName[i] << endl;
   108 			stream << "grave " << Grave << endl;
   156 			stream << "grave " << Grave << endl;
   109 			stream << "fort " << Fort << 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 			}
   110 			cfgfile.close();
   162 			cfgfile.close();
   111 			return true;
   163 			return true;
   112 		}
   164 		}
   113 		
   165 		
   114 		void ToPage(HWForm * form)
   166 		void ToPage()
   115 		{
   167 		{
   116 			form->EditTeamName->setText(TeamName);
   168 			form->EditTeamName->setText(TeamName);
   117 			form->HHName0->setText(HHName[0]);
   169 			form->HHName0->setText(HHName[0]);
   118 			form->HHName1->setText(HHName[1]);
   170 			form->HHName1->setText(HHName[1]);
   119 			form->HHName2->setText(HHName[2]);
   171 			form->HHName2->setText(HHName[2]);
   126 			const QListBox * lb = form->CBGraves->listBox();
   178 			const QListBox * lb = form->CBGraves->listBox();
   127 			form->CBGraves->setCurrentItem(lb->index(lb->findItem(Grave)));
   179 			form->CBGraves->setCurrentItem(lb->index(lb->findItem(Grave)));
   128 			
   180 			
   129 			lb = form->CBForts->listBox();
   181 			lb = form->CBForts->listBox();
   130 			form->CBForts->setCurrentItem(lb->index(lb->findItem(Fort)));
   182 			form->CBForts->setCurrentItem(lb->index(lb->findItem(Fort)));
   131 		}
   183 			
   132 		
   184 			lb = form->CBindUp->listBox();
   133 		void FromPage(HWForm * form)
   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()
   134 		{
   192 		{
   135 			TeamName  = form->EditTeamName->text();
   193 			TeamName  = form->EditTeamName->text();
   136 			HHName[0] = form->HHName0->text();
   194 			HHName[0] = form->HHName0->text();
   137 			HHName[1] = form->HHName1->text();
   195 			HHName[1] = form->HHName1->text();
   138 			HHName[2] = form->HHName2->text();
   196 			HHName[2] = form->HHName2->text();
   142 			HHName[6] = form->HHName6->text();
   200 			HHName[6] = form->HHName6->text();
   143 			HHName[7] = form->HHName7->text();
   201 			HHName[7] = form->HHName7->text();
   144 			
   202 			
   145 			Grave = form->CBGraves->currentText();
   203 			Grave = form->CBGraves->currentText();
   146 			Fort = form->CBForts->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 			}
   147 		}
   209 		}
   148 	private:
   210 	private:
   149 };
   211 };