tools/hwmap2txt/hwmapconverter/mainwindow.cpp
changeset 10936 104727dfaa1b
child 11015 7a905f0070ce
equal deleted inserted replaced
10934:d6cc62ef78ef 10936:104727dfaa1b
       
     1 #include <QByteArray>
       
     2 #include <QFile>
       
     3 #include <QFileDialog>
       
     4 #include <QtEndian>
       
     5 #include <QRegExp>
       
     6 
       
     7 #include "mainwindow.h"
       
     8 #include "ui_mainwindow.h"
       
     9 
       
    10 MainWindow::MainWindow(QWidget *parent) :
       
    11     QMainWindow(parent),
       
    12     ui(new Ui::MainWindow)
       
    13 {
       
    14     ui->setupUi(this);
       
    15 }
       
    16 
       
    17 MainWindow::~MainWindow()
       
    18 {
       
    19     delete ui;
       
    20 }
       
    21 
       
    22 void MainWindow::on_pbLoad_clicked()
       
    23 {
       
    24     QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(), "Hedgewars drawn maps (*.hwmap);;All files (*.*)");
       
    25 
       
    26     if(!fileName.isEmpty())
       
    27     {
       
    28         QFile f(fileName);
       
    29 
       
    30         if(f.open(QFile::ReadOnly))
       
    31         {
       
    32             QByteArray data = qUncompress(QByteArray::fromBase64(f.readAll()));
       
    33 
       
    34             QStringList decoded;
       
    35 
       
    36             bool isSpecial = true;
       
    37             while(data.size() >= 5)
       
    38             {
       
    39                 qint16 px = qFromBigEndian(*(qint16 *)data.data());
       
    40                 data.remove(0, 2);
       
    41                 qint16 py = qFromBigEndian(*(qint16 *)data.data());
       
    42                 data.remove(0, 2);
       
    43                 quint8 flags = *(quint8 *)data.data();
       
    44                 data.remove(0, 1);
       
    45 
       
    46                 if(flags & 0x80)
       
    47                 {
       
    48                     if(isSpecial && !decoded.isEmpty())
       
    49                         decoded << "// drawings";
       
    50 
       
    51                     isSpecial = false;
       
    52 
       
    53                     quint8 penWidth = flags & 0x3f;
       
    54                     bool isErasing = flags & 0x40;
       
    55                     decoded << QString("%1 %2 %3 %4")
       
    56                                .arg(px, 5).arg(py, 6)
       
    57                                .arg(isErasing ? "e" : "s")
       
    58                                .arg(penWidth, 2);
       
    59                 } else
       
    60                     if(isSpecial)
       
    61                     {
       
    62                         if(decoded.isEmpty())
       
    63                             decoded << "// special points (these are always before all drawings!)";
       
    64 
       
    65                         decoded << QString("%1 %2 %3")
       
    66                                    .arg(px, 5).arg(py, 6)
       
    67                                    .arg(flags);
       
    68                     } else
       
    69                     {
       
    70                         decoded << QString("%1 %2")
       
    71                                    .arg(px, 5).arg(py, 6);
       
    72                     }
       
    73             }
       
    74 
       
    75             ui->textEdit->setPlainText(decoded.join("\n"));
       
    76             ui->statusBar->showMessage("Load OK");
       
    77         } else
       
    78             ui->statusBar->showMessage(QString("Can't open file %1").arg(fileName));
       
    79     }
       
    80 }
       
    81 
       
    82 void MainWindow::on_pbSave_clicked()
       
    83 {
       
    84     QRegExp rxSP("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*(\\d+)\\s*$");
       
    85     QRegExp rxLS("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*([es])\\s*(\\d+)\\s*$");
       
    86     QRegExp rxP("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*$");
       
    87 
       
    88     QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), "Hedgewars drawn maps (*.hwmap);;All files (*.*)");
       
    89 
       
    90     QFile file(fileName);
       
    91     if(file.open(QFile::WriteOnly))
       
    92     {
       
    93         QByteArray b;
       
    94         QStringList sl = ui->textEdit->toPlainText().split('\n');
       
    95         bool isSpecial = true;
       
    96 
       
    97         foreach(const QString & line, sl)
       
    98             if(!line.startsWith("//"))
       
    99             {
       
   100                 if(rxLS.indexIn(line) != -1)
       
   101                 {
       
   102                     isSpecial = false;
       
   103                     qint16 px = qToBigEndian((qint16)rxLS.cap(1).toInt());
       
   104                     qint16 py = qToBigEndian((qint16)rxLS.cap(2).toInt());
       
   105                     quint8 flags = 0x80;
       
   106                     if(rxLS.cap(3) == "e") flags |= 0x40;
       
   107                     flags = flags + rxLS.cap(4).toUInt();
       
   108                     b.append((const char *)&px, 2);
       
   109                     b.append((const char *)&py, 2);
       
   110                     b.append((const char *)&flags, 1);
       
   111                 } else
       
   112                 if(isSpecial && (rxSP.indexIn(line) != -1))
       
   113                 {
       
   114                     qint16 px = qToBigEndian((qint16)rxSP.cap(1).toInt());
       
   115                     qint16 py = qToBigEndian((qint16)rxSP.cap(2).toInt());
       
   116                     quint8 flags = rxSP.cap(3).toUInt();
       
   117 
       
   118                     b.append((const char *)&px, 2);
       
   119                     b.append((const char *)&py, 2);
       
   120                     b.append((const char *)&flags, 1);
       
   121                 } else
       
   122                 if(rxP.indexIn(line) != -1)
       
   123                 {
       
   124                     isSpecial = false;
       
   125                     qint16 px = qToBigEndian((qint16)rxP.cap(1).toInt());
       
   126                     qint16 py = qToBigEndian((qint16)rxP.cap(2).toInt());
       
   127                     quint8 flags = 0;
       
   128                     b.append((const char *)&px, 2);
       
   129                     b.append((const char *)&py, 2);
       
   130                     b.append((const char *)&flags, 1);
       
   131                 } else
       
   132                     ui->statusBar->showMessage(QString("Can't parse or misplaced special point: %1").arg(line));
       
   133             }
       
   134 
       
   135         file.write(qCompress(b).toBase64());
       
   136     }
       
   137 }