Allow to randomize team name, flag, grave, voice and fort seperately
authorWuzzy <almikes@aol.com>
Thu, 13 Apr 2017 17:26:37 +0200
changeset 12245 5206f9a803d1
parent 12244 45c83c88ac4b
child 12246 4c717a69a7a6
Allow to randomize team name, flag, grave, voice and fort seperately
ChangeLog.txt
QTfrontend/ui/page/AbstractPage.cpp
QTfrontend/ui/page/AbstractPage.h
QTfrontend/ui/page/pageeditteam.cpp
QTfrontend/ui/page/pageeditteam.h
QTfrontend/util/namegen.cpp
QTfrontend/util/namegen.h
--- a/ChangeLog.txt	Thu Apr 13 15:18:07 2017 +0200
+++ b/ChangeLog.txt	Thu Apr 13 17:26:37 2017 +0200
@@ -187,6 +187,7 @@
  + Randomizing map/theme in online-mode will not include DLC-content
  + New teams start with a random grave and fort
  + Hedgewars creates 4 teams instead of 1 on its first launch
+ + Allow to randomize team name, flag, grave, voice and fort seperately
  * Campaign screen does no longer show AI-controlled teams
  * Campaign names and campaign mission names can now be translated
  * Creating randomized teams now randomizes (almost) every aspect
--- a/QTfrontend/ui/page/AbstractPage.cpp	Thu Apr 13 15:18:07 2017 +0200
+++ b/QTfrontend/ui/page/AbstractPage.cpp	Thu Apr 13 17:26:37 2017 +0200
@@ -141,6 +141,13 @@
     return btn;
 }
 
+QPushButton* AbstractPage::addSoundlessButton(const QString & name, QGridLayout * grid, int row, int column, int rowSpan, int columnSpan, bool hasIcon, Qt::Alignment alignment)
+{
+    QPushButton * btn = formattedSoundlessButton(name, hasIcon);
+    grid->addWidget(btn, row, column, rowSpan, columnSpan, alignment);
+    return btn;
+}
+
 QPushButton* AbstractPage::addSoundlessButton(const QString & name, QBoxLayout * box, int where, bool hasIcon, Qt::Alignment alignment)
 {
     QPushButton* btn = formattedSoundlessButton(name, hasIcon);
--- a/QTfrontend/ui/page/AbstractPage.h	Thu Apr 13 15:18:07 2017 +0200
+++ b/QTfrontend/ui/page/AbstractPage.h	Thu Apr 13 17:26:37 2017 +0200
@@ -190,6 +190,7 @@
          * @return the button.
          */
         QPushButtonWithSound * addButton(const QString & name, QGridLayout * grid, int row, int column, int rowSpan = 1, int columnSpan = 1, bool hasIcon = false, Qt::Alignment alignment = 0);
+        QPushButton * addSoundlessButton(const QString & name, QGridLayout * grid, int row, int column, int rowSpan = 1, int columnSpan = 1, bool hasIcon = false, Qt::Alignment alignment = 0);
 
         /**
          * @brief Creates a default formatted button and adds it to a
--- a/QTfrontend/ui/page/pageeditteam.cpp	Thu Apr 13 15:18:07 2017 +0200
+++ b/QTfrontend/ui/page/pageeditteam.cpp	Thu Apr 13 17:26:37 2017 +0200
@@ -114,10 +114,12 @@
 
     TeamNameEdit = new QLineEdit(GBoxTeam);
     TeamNameEdit->setMaxLength(64);
-    GBTLayout->addWidget(TeamNameEdit, 0, 1);
+    TeamNameEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
+    GBTLayout->addWidget(TeamNameEdit, 0, 1, 1, 2);
     vbox2->addWidget(GBoxTeam);
 
     CBTeamLvl = new QComboBox(GBoxTeam);
+    CBTeamLvl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     CBTeamLvl->setIconSize(QSize(32, 32));
     CBTeamLvl->addItem(QIcon(":/res/botlevels/small0.png"), QComboBox::tr("Human"));
     for(int i = 5; i > 0; i--)
@@ -126,46 +128,64 @@
             QComboBox::tr("Computer (Level %1)").arg(i)
         );
     CBTeamLvl->setFixedHeight(38);
-    GBTLayout->addWidget(CBTeamLvl, 1, 1);
+    GBTLayout->addWidget(CBTeamLvl, 1, 1, 1, 2);
 
     CBGrave = new QComboBox(GBoxTeam);
+    CBGrave->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     CBGrave->setMaxCount(65535);
     CBGrave->setMaxVisibleItems(20);
     CBGrave->setIconSize(QSize(32, 32));
     CBGrave->setFixedHeight(44);
-    GBTLayout->addWidget(CBGrave, 2, 1);
+    GBTLayout->addWidget(CBGrave, 2, 1, 1, 2);
 
     CBFlag = new QComboBox(GBoxTeam);
     CBFlag->setMaxCount(65535);
     CBFlag->setMaxVisibleItems(50);
     CBFlag->setIconSize(QSize(22, 15));
-    GBTLayout->addWidget(CBFlag, 3, 1);
+    CBFlag->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
+    GBTLayout->addWidget(CBFlag, 3, 1, 1, 2);
+
+    btnRandomTeamName = addButton(":/res/dice.png", GBTLayout, 0, 3, 1, 1, true);
+    btnRandomTeamName->setWhatsThis(tr("Randomize the team name"));
 
-    QHBoxLayout * hbox = new QHBoxLayout();
+    btnRandomGrave = addButton(":/res/dice.png", GBTLayout, 2, 3, 1, 1, true);
+    btnRandomGrave->setWhatsThis(tr("Randomize the grave"));
+
+    btnRandomFlag = addButton(":/res/dice.png", GBTLayout, 3, 3, 1, 1, true);
+    btnRandomFlag->setWhatsThis(tr("Randomize the flag"));
+
     CBVoicepack = new QComboBox(GBoxTeam);
     CBVoicepack->setMaxVisibleItems(50);
+    CBVoicepack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
 
-    hbox->addWidget(CBVoicepack, 100);
-    btnTestSound = addSoundlessButton(":/res/PlaySound.png", hbox, 1, true);
+    GBTLayout->addWidget(CBVoicepack, 4, 1, 1, 1);
+
+    btnTestSound = addSoundlessButton(":/res/PlaySound.png", GBTLayout, 4, 2, 1, 1, true);
     btnTestSound->setWhatsThis(tr("Play a random example of this voice"));
-    hbox->setStretchFactor(btnTestSound, 1);
 
-    GBTLayout->addLayout(hbox, 4, 1);
+    btnRandomVoice = addButton(":/res/dice.png", GBTLayout, 4, 3, 1, 1, true);
+    btnRandomVoice->setWhatsThis(tr("Randomize the voice"));
 
     GBoxFort = new QGroupBox(this);
     GBoxFort->setTitle(QGroupBox::tr("Fort"));
     QGridLayout * GBFLayout = new QGridLayout(GBoxFort);
     CBFort = new QComboBox(GBoxFort);
+    CBFort->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     CBFort->setMaxVisibleItems(25);
     CBFort->setMaxCount(65535);
+
     GBFLayout->addWidget(CBFort, 0, 0);
+
+    btnRandomFort = addButton(":/res/dice.png", GBFLayout, 0, 2, 1, 1, true);
+    btnRandomFort->setWhatsThis(tr("Randomize the fort"));
+
     FortPreview = new SquareLabel(GBoxFort);
     FortPreview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
     FortPreview->setMinimumSize(128, 128);
     FortPreview->setPixmap(QPixmap());
     // perhaps due to handling its own paintevents, SquareLabel doesn't play nice with the stars
     //FortPreview->setAttribute(Qt::WA_PaintOnScreen, true);
-    GBFLayout->addWidget(FortPreview, 1, 0);
+    GBFLayout->addWidget(FortPreview, 1, 0, 1, 2);
     vbox2->addWidget(GBoxFort);
 
     vbox1->addStretch();
@@ -199,6 +219,11 @@
     }
 
     connect(btnRandomTeam, SIGNAL(clicked()), this, SLOT(setRandomTeam()));
+    connect(btnRandomTeamName, SIGNAL(clicked()), this, SLOT(setRandomTeamName()));
+    connect(btnRandomGrave, SIGNAL(clicked()), this, SLOT(setRandomGrave()));
+    connect(btnRandomFlag, SIGNAL(clicked()), this, SLOT(setRandomFlag()));
+    connect(btnRandomVoice, SIGNAL(clicked()), this, SLOT(setRandomVoice()));
+    connect(btnRandomFort, SIGNAL(clicked()), this, SLOT(setRandomFort()));
 
     connect(btnTestSound, SIGNAL(clicked()), this, SLOT(testSound()));
 
@@ -393,6 +418,41 @@
     loadTeam(team);
 }
 
+void PageEditTeam::setRandomTeamName()
+{
+    HWTeam team = data();
+    HWNamegen::teamRandomTeamName(team);
+    loadTeam(team);
+}
+
+void PageEditTeam::setRandomGrave()
+{
+    HWTeam team = data();
+    HWNamegen::teamRandomGrave(team);
+    loadTeam(team);
+}
+
+void PageEditTeam::setRandomFlag()
+{
+    HWTeam team = data();
+    HWNamegen::teamRandomFlag(team);
+    loadTeam(team);
+}
+
+void PageEditTeam::setRandomVoice()
+{
+    HWTeam team = data();
+    HWNamegen::teamRandomVoice(team);
+    loadTeam(team);
+}
+
+void PageEditTeam::setRandomFort()
+{
+    HWTeam team = data();
+    HWNamegen::teamRandomFort(team);
+    loadTeam(team);
+}
+
 void PageEditTeam::loadTeam(const HWTeam & team)
 {
     tbw->setCurrentIndex(0);
--- a/QTfrontend/ui/page/pageeditteam.h	Thu Apr 13 15:18:07 2017 +0200
+++ b/QTfrontend/ui/page/pageeditteam.h	Thu Apr 13 17:26:37 2017 +0200
@@ -76,6 +76,11 @@
 
         // page 1
         QPushButton * btnRandomHogName[HEDGEHOGS_PER_TEAM];
+        QPushButton * btnRandomTeamName;
+        QPushButton * btnRandomGrave;
+        QPushButton * btnRandomFlag;
+        QPushButton * btnRandomVoice;
+        QPushButton * btnRandomFort;
         QPushButton * btnRandomTeam;
         QPushButton * btnTestSound;
 
@@ -84,6 +89,11 @@
     private slots:
         void saveTeam();
         void setRandomTeam();
+        void setRandomTeamName();
+        void setRandomGrave();
+        void setRandomFlag();
+        void setRandomVoice();
+        void setRandomFort();
 
         void setRandomHogName(int hh_index);
 
--- a/QTfrontend/util/namegen.cpp	Thu Apr 13 15:18:07 2017 +0200
+++ b/QTfrontend/util/namegen.cpp	Thu Apr 13 17:26:37 2017 +0200
@@ -33,6 +33,13 @@
 QList<QStringList> HWNamegen::TypesHatnames;
 bool HWNamegen::typesAvailable = false;
 
+void HWNamegen::teamRandomTeamName(HWTeam & team)
+{
+    QString newName = getRandomTeamName();
+    if(!newName.isNull())
+        team.setName(newName);
+}
+
 void HWNamegen::teamRandomFlag(HWTeam & team)
 {
     team.setFlag(getRandomFlag());
@@ -70,9 +77,7 @@
     // pick team name based on hat
     if (changeteamname)
     {
-        if (TypesTeamnames[kind].size() > 0)
-            team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
-
+        team.setName(getRandomTeamName());
         team.setGrave(getRandomGrave());
         team.setFort(getRandomFort());
         team.setFlag(getRandomFlag());
@@ -251,7 +256,23 @@
     return typesAvailable;
 }
 
+QString HWNamegen::getRandomTeamName()
+{
+    // load types if not already loaded
+    if (!typesAvailable)
+        if (!loadTypes())
+            return QString(); // abort if loading failed
 
+    // abort if there are no hat types
+    if (TypesHatnames.size() <= 0)
+        return QString();
+
+    int kind = (rand()%(TypesHatnames.size()));
+    if (TypesTeamnames[kind].size() > 0)
+        return TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
+    else
+        return QString();
+}
 
 QString HWNamegen::getRandomGrave()
 {
--- a/QTfrontend/util/namegen.h	Thu Apr 13 15:18:07 2017 +0200
+++ b/QTfrontend/util/namegen.h	Thu Apr 13 17:26:37 2017 +0200
@@ -29,6 +29,7 @@
 {
     public:
 
+        static void teamRandomTeamName(HWTeam & team);
         static void teamRandomGrave(HWTeam & team);
         static void teamRandomFort(HWTeam & team);
         static void teamRandomFlag(HWTeam & team);
@@ -39,6 +40,7 @@
     private:
         HWNamegen();
 
+        static QString getRandomTeamName();
         static QString getRandomGrave();
         static QString getRandomFort();
         static QString getRandomFlag();