map choose from files added
authordisplacer
Sat, 18 Nov 2006 23:48:24 +0000
changeset 249 ff85fa029541
parent 248 aab204fe5061
child 250 d7f744a9bf28
map choose from files added
QTfrontend/game.cpp
QTfrontend/gamecfgwidget.cpp
QTfrontend/gamecfgwidget.h
QTfrontend/mapContainer.cpp
QTfrontend/mapContainer.h
--- a/QTfrontend/game.cpp	Sat Nov 18 18:17:03 2006 +0000
+++ b/QTfrontend/game.cpp	Sat Nov 18 23:48:24 2006 +0000
@@ -54,8 +54,14 @@
 void HWGame::SendConfig()
 {
 	SendIPC(QString("eseed %1").arg(seed).toAscii());
-//	SendIPC(QString("emap %1").arg("mushrooms").toAscii());
-	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
+	try {
+	  QString currentMap=gamecfg->getCurrentMap();
+	  SendIPC((QString("emap ")+currentMap).toAscii());
+	  SendIPC(QString("etheme %1").arg(gamecfg->getCurrentTheme()).toAscii());
+	}
+	catch(const MapFileErrorException& e) {
+	  SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
+	}
 	SendIPC("TL");
 	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
 
--- a/QTfrontend/gamecfgwidget.cpp	Sat Nov 18 18:17:03 2006 +0000
+++ b/QTfrontend/gamecfgwidget.cpp	Sat Nov 18 23:48:24 2006 +0000
@@ -62,3 +62,13 @@
 {
   return pMapContainer->getCurrentSeed();
 }
+
+QString GameCFGWidget::getCurrentMap() const
+{
+  return pMapContainer->getCurrentMap();
+}
+
+QString GameCFGWidget::getCurrentTheme() const
+{
+  return pMapContainer->getCurrentTheme();
+}
--- a/QTfrontend/gamecfgwidget.h	Sat Nov 18 18:17:03 2006 +0000
+++ b/QTfrontend/gamecfgwidget.h	Sat Nov 18 23:48:24 2006 +0000
@@ -33,6 +33,8 @@
 	GameCFGWidget(QWidget* parent=0);
 	quint32 getGameFlags();
 	QString getCurrentSeed() const;
+	QString getCurrentMap() const;
+	QString getCurrentTheme() const;
 
 private slots:
 
--- a/QTfrontend/mapContainer.cpp	Sat Nov 18 18:17:03 2006 +0000
+++ b/QTfrontend/mapContainer.cpp	Sat Nov 18 23:48:24 2006 +0000
@@ -25,6 +25,9 @@
 #include <QPainter>
 #include <QLinearGradient>
 #include <QColor>
+#include <QTextStream>
+
+#include "hwconsts.h"
 
 HWMapContainer::HWMapContainer(QWidget * parent) :
   QWidget(parent), mainLayout(this)
@@ -36,6 +39,18 @@
   mainLayout.addWidget(imageButt);
   connect(imageButt, SIGNAL(clicked()), this, SLOT(changeImage()));
   changeImage();
+
+  chooseMap=new QComboBox(this);
+  QDir tmpdir;
+  tmpdir.cd(datadir->absolutePath());
+  tmpdir.cd("Maps");
+  tmpdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
+  QStringList mapList=tmpdir.entryList(QStringList("*"));
+  mapList.push_front("generated map...");
+  chooseMap->addItems(mapList);
+  connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
+
+  mainLayout.addWidget(chooseMap);
 }
 
 void HWMapContainer::setImage(const QImage newImage)
@@ -54,9 +69,25 @@
   p.fillRect(QRect(0, 0, 256, 128), linearGrad);
   p.drawPixmap(QPoint(0, 0), px);
 
-
   imageButt->setIcon(pxres);
   imageButt->setIconSize(QSize(256, 128));
+  chooseMap->setCurrentIndex(0);
+}
+
+void HWMapContainer::mapChanged(int index)
+{
+  if(!index) {
+    changeImage();
+    return;
+  }
+
+  QPixmap mapImage;
+  if(!mapImage.load(datadir->absolutePath()+"/Maps/"+chooseMap->currentText()+"/map.png")) {
+    changeImage();
+    chooseMap->setCurrentIndex(0);
+    return;
+  }
+  imageButt->setIcon(mapImage.scaled(256,128));
 }
 
 void HWMapContainer::changeImage()
@@ -72,6 +103,28 @@
   return m_seed;
 }
 
+QString HWMapContainer::getCurrentMap() const
+{
+  if(!chooseMap->currentIndex()) throw MapFileErrorException();
+  return chooseMap->currentText();
+}
+
+QString HWMapContainer::getCurrentTheme() const
+{
+  if(!chooseMap->currentIndex()) throw MapFileErrorException();
+  QFile mapCfgFile(datadir->absolutePath()+"/Maps/"+chooseMap->currentText()+"/map.cfg");
+  if (mapCfgFile.open(QFile::ReadOnly)) {
+    QTextStream input(&mapCfgFile);
+    QString theme;
+    input >> theme;
+    if(theme.length()>256) throw MapFileErrorException(); // theme name too long
+    return theme;
+    mapCfgFile.close();
+  } else {
+    throw MapFileErrorException();
+  }
+}
+
 void HWMapContainer::resizeEvent ( QResizeEvent * event )
 {
   //imageButt->setIconSize(imageButt->size());
--- a/QTfrontend/mapContainer.h	Sat Nov 18 18:17:03 2006 +0000
+++ b/QTfrontend/mapContainer.h	Sat Nov 18 23:48:24 2006 +0000
@@ -23,9 +23,14 @@
 
 #include <QWidget>
 #include <QVBoxLayout>
+#include <QComboBox>
 
 class QPushButton;
 
+class MapFileErrorException
+{
+};
+
 class HWMapContainer : public QWidget
 {
   Q_OBJECT
@@ -33,12 +38,15 @@
  public:
   HWMapContainer(QWidget * parent=0);
   QString getCurrentSeed() const;
+  QString getCurrentMap() const;
+  QString getCurrentTheme() const;
 
  public slots:
   void changeImage();
 
  private slots:
   void setImage(const QImage newImage);
+  void mapChanged(int index);
 
  protected:
   virtual void resizeEvent ( QResizeEvent * event );
@@ -46,6 +54,7 @@
  private:
   QVBoxLayout mainLayout;
   QPushButton* imageButt;
+  QComboBox* chooseMap;
   HWMap* pMap;
   QString m_seed;
 };