Fix broken preview of team hats in frontend
authorWuzzy <Wuzzy2@mail.ru>
Thu, 08 Feb 2018 13:54:48 +0100
changeset 12931 502d18361c7e
parent 12930 7a54bda7b6d1
child 12932 9400a272c88f
Fix broken preview of team hats in frontend
ChangeLog.txt
QTfrontend/model/HatModel.cpp
--- a/ChangeLog.txt	Thu Feb 08 09:38:35 2018 +0100
+++ b/ChangeLog.txt	Thu Feb 08 13:54:48 2018 +0100
@@ -9,6 +9,7 @@
 
 Frontend:
  + Schemes are now stored in separate files under Schemes
+ * Fix broken preview of team hats (e.g. cap_team)
 
 Content:
  + New flag: uk_scotland
--- a/QTfrontend/model/HatModel.cpp	Thu Feb 08 09:38:35 2018 +0100
+++ b/QTfrontend/model/HatModel.cpp	Thu Feb 08 13:54:48 2018 +0100
@@ -78,7 +78,7 @@
 
         QString str = hatsList.at(i);
         str = str.remove(QRegExp("\\.png$"));
-        QPixmap pix(
+        QPixmap hatpix(
                 "physfs://Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str +
                 ".png"
         );
@@ -87,20 +87,51 @@
         if (isReserved)
             str = "Reserved "+str.remove(0,32);
 
-        QPixmap tmppix(32, 37);
-        tmppix.fill(QColor(Qt::transparent));
+        // Color for team hats. We use the default color of the first team.
+        QColor overlay_color = QColor(colors[0]);
+
+        QPixmap ppix(32, 37);
+        ppix.fill(QColor(Qt::transparent));
+        QPainter painter(&ppix);
+
+        QPixmap opix(32, 37);
+        opix.fill(QColor(Qt::transparent));
+        QPainter overlay_painter(&opix);
+
+        // The hat is drawn in reverse: First the color overlay, then the hat, then the hedgehog.
 
-        QPainter painter(&tmppix);
+        // draw hat's color layer, if present
+        int overlay_offset = -1;
+        if((hatpix.height() == 32) && (hatpix.width() == 64)) {
+            overlay_offset = 32;
+        } else if(hatpix.width() > 64) {
+            overlay_offset = 64;
+        }
+        if(overlay_offset > -1) {
+            // colorized layer
+            overlay_painter.drawPixmap(QPoint(0, 0), hatpix.copy(overlay_offset, 0, 32, 32));
+            overlay_painter.setCompositionMode(QPainter::CompositionMode_Multiply);
+            overlay_painter.fillRect(0, 0, 32, 32, overlay_color);
+
+            // uncolorized layer and combine
+            painter.drawPixmap(QPoint(0, 0), hatpix.copy(overlay_offset, 0, 32, 32));
+            painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
+            painter.drawPixmap(QPoint(0, 0), opix.copy(0, 0, 32, 32));
+        }
+
+        // draw hat below the color layer
+        painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
+        painter.drawPixmap(QPoint(0, 0), hatpix.copy(0, 0, 32, 32));
+
+        // draw hedgehog below the hat
         painter.drawPixmap(QPoint(0, 5), hhpix);
-        painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
-        if(pix.width() > 32)
-            painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
+
         painter.end();
 
         if (str == "NoHat")
-            hats.prepend(new QStandardItem(QIcon(tmppix), str));
+            hats.prepend(new QStandardItem(QIcon(ppix), str));
         else
-            hats.append(new QStandardItem(QIcon(tmppix), str));
+            hats.append(new QStandardItem(QIcon(ppix), str));
     }
 
     QStandardItemModel::appendColumn(hats);