From c7378907285c10fadde01a41bc8a4bfff52a97fb Mon Sep 17 00:00:00 2001 From: Akiko Date: Thu, 10 Oct 2013 11:00:59 +0200 Subject: [PATCH] - bsp_viewer cleanup (just removed some old code) - added syntax highlighting for def, fx and ini text files in TextWindow - updated the makefile - fixed some minor bugs --- editor/CMakeLists.txt | 5 +- editor/DefSyntax.cxx | 114 ++++++++++++++++++++++++++++++++++++++++++ editor/DefSyntax.hxx | 27 ++++++++++ editor/FxSyntax.cxx | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ editor/FxSyntax.hxx | 29 +++++++++++ editor/IniSyntax.cxx | 99 +++++++++++++++++++++++++++++++++++++ editor/IniSyntax.hxx | 25 ++++++++++ editor/MainWindow.cxx | 3 +- editor/TextWindow.cxx | 19 ++++++- editor/TextWindow.hxx | 6 +++ tools/bsp_viewer.cxx | 50 +------------------ 11 files changed, 458 insertions(+), 53 deletions(-) create mode 100644 editor/DefSyntax.cxx create mode 100644 editor/DefSyntax.hxx create mode 100644 editor/FxSyntax.cxx create mode 100644 editor/FxSyntax.hxx create mode 100644 editor/IniSyntax.cxx create mode 100644 editor/IniSyntax.hxx diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt index 080a8c6..c0dd324 100644 --- a/editor/CMakeLists.txt +++ b/editor/CMakeLists.txt @@ -1,7 +1,8 @@ # Qt sources set (ED_SOURCES Main.cxx MainWindow.cxx ImageWindow.cxx TextWindow.cxx DatWindow.cxx - ObjectWindow.cxx) -set (ED_HEADERS MainWindow.hxx ImageWindow.hxx TextWindow.hxx DatWindow.hxx ObjectWindow.hxx) + ObjectWindow.cxx DefSyntax.cxx FxSyntax.cxx IniSyntax.cxx) +set (ED_HEADERS MainWindow.hxx ImageWindow.hxx TextWindow.hxx DatWindow.hxx ObjectWindow.hxx + DefSyntax.hxx FxSyntax.hxx IniSyntax.hxx) set (ED_UI_FILES MainWindow.ui ImageWindow.ui TextWindow.ui DatWindow.ui ObjectWindow.ui) set (ED_RES_FILES Resources.qrc) set (ED_TS_FILES i18n_english.ts i18n_french.ts i18n_german.ts) diff --git a/editor/DefSyntax.cxx b/editor/DefSyntax.cxx new file mode 100644 index 0000000..78852f9 --- /dev/null +++ b/editor/DefSyntax.cxx @@ -0,0 +1,114 @@ +#include "DefSyntax.hxx" + +// --- internal constants --- + +const QString keywords("setentry"); + +// --- constructors and deconstructors --- + +DefSyntax::DefSyntax(QObject *parent) +: QSyntaxHighlighter(parent) +{ + setup(); +} + +DefSyntax::DefSyntax(QTextDocument *parent) +: QSyntaxHighlighter(parent) +{ + setup(); +} + +DefSyntax::~DefSyntax() +{ +} + +// --- protected methods --- + +void DefSyntax::highlightBlock(const QString& text) +{ + qint32 index = 0; + + setCurrentBlockState(0); + + // keywords + index = text.indexOf(_keywords_re); + while (index >= 0) + { + auto len = _keywords_re.matchedLength(); + + setFormat(index, len, _keywords_f); + index = text.indexOf(_keywords_re, index + len); + } + + // ints + index = text.indexOf(_ints_re); + while (index >= 0) + { + auto len = _ints_re.matchedLength(); + + setFormat(index, len, _ints_f); + index = text.indexOf(_ints_re, index + len); + } + + // floats + index = text.indexOf(_floats_re); + while (index >= 0) + { + auto len = _floats_re.matchedLength(); + + setFormat(index, len, _floats_f); + index = text.indexOf(_floats_re, index + len); + } + + // strings + index = text.indexOf(_strings_re); + while (index >= 0) + { + auto len = _strings_re.matchedLength(); + + setFormat(index, len, _strings_f); + index = text.indexOf(_strings_re, index + len); + } + + // single line comments + index = text.indexOf(_comment_re); + if (index >= 0) + setFormat(index, _comment_re.matchedLength(), _comment_f); +} + +void DefSyntax::setup() +{ + QString pattern; + + // format + _keywords_f.setFontWeight(QFont::Bold); + _keywords_f.setForeground(Qt::green); + + _ints_f.setFontWeight(QFont::Bold); + _ints_f.setForeground(Qt::darkBlue); + + _floats_f.setFontWeight(QFont::Bold); + _floats_f.setForeground(Qt::darkRed); + + _strings_f.setFontWeight(QFont::Bold); + _strings_f.setFontItalic(true); + _strings_f.setForeground(Qt::darkYellow); + + _comment_f.setFontItalic(true); + _comment_f.setForeground(Qt::darkGray); + + + // regexp + pattern.append("\\b("); + pattern.append(keywords.split(" ").join("|")); + pattern.append(")\\b"); + _keywords_re.setPattern(pattern); + + _ints_re.setPattern("\\b[0-9]+\\b"); + + _floats_re.setPattern("\\b[0-9]+\\.[0-9]+\\b"); + + _strings_re.setPattern("(\"[^\"]*)\""); + + _comment_re.setPattern("//[^\n]*"); +} diff --git a/editor/DefSyntax.hxx b/editor/DefSyntax.hxx new file mode 100644 index 0000000..a51e9fe --- /dev/null +++ b/editor/DefSyntax.hxx @@ -0,0 +1,27 @@ +#pragma once + +#include + +class DefSyntax : public QSyntaxHighlighter { + Q_OBJECT +public: + explicit DefSyntax(QObject *parent = 0); + explicit DefSyntax(QTextDocument *parent = 0); + virtual ~DefSyntax(); + +protected: + virtual void highlightBlock(const QString& text) override; + void setup(); + +private: + QTextCharFormat _keywords_f; + QTextCharFormat _ints_f; + QTextCharFormat _floats_f; + QTextCharFormat _strings_f; + QTextCharFormat _comment_f; + QRegExp _keywords_re; + QRegExp _ints_re; + QRegExp _floats_re; + QRegExp _strings_re; + QRegExp _comment_re; +}; diff --git a/editor/FxSyntax.cxx b/editor/FxSyntax.cxx new file mode 100644 index 0000000..52fb94b --- /dev/null +++ b/editor/FxSyntax.cxx @@ -0,0 +1,134 @@ +#include "FxSyntax.hxx" + +// --- internal constants --- + +const QString structs("PARTICLE POS MOVEMENT COLORRGBA SIZE LIFESPAN BITMAP GLOW"); +const QString functions("StartPosX StartPosY StartPosZ Random"); + +// --- constructors and deconstructors --- + +FxSyntax::FxSyntax(QObject *parent) +: QSyntaxHighlighter(parent) +{ + setup(); +} + +FxSyntax::FxSyntax(QTextDocument *parent) +: QSyntaxHighlighter(parent) +{ + setup(); +} + +FxSyntax::~FxSyntax() +{ +} + +// --- protected methods --- + +void FxSyntax::highlightBlock(const QString& text) +{ + qint32 index = 0; + + setCurrentBlockState(0); + + // structs + index = text.indexOf(_structs_re); + while (index >= 0) + { + auto len = _structs_re.matchedLength(); + + setFormat(index, len, _structs_f); + index = text.indexOf(_structs_re, index + len); + } + + // functions + index = text.indexOf(_functions_re); + while (index >= 0) + { + auto len = _functions_re.matchedLength(); + + setFormat(index, len, _functions_f); + index = text.indexOf(_functions_re, index + len); + } + + // ints + index = text.indexOf(_ints_re); + while (index >= 0) + { + auto len = _ints_re.matchedLength(); + + setFormat(index, len, _ints_f); + index = text.indexOf(_ints_re, index + len); + } + + // floats + index = text.indexOf(_floats_re); + while (index >= 0) + { + auto len = _floats_re.matchedLength(); + + setFormat(index, len, _floats_f); + index = text.indexOf(_floats_re, index + len); + } + + // strings + index = text.indexOf(_strings_re); + while (index >= 0) + { + auto len = _strings_re.matchedLength(); + + setFormat(index, len, _strings_f); + index = text.indexOf(_strings_re, index + len); + } + + // single line comments + index = text.indexOf(_comment_re); + if (index >= 0) + setFormat(index, _comment_re.matchedLength(), _comment_f); +} + +void FxSyntax::setup() +{ + QString pattern; + + // format + _structs_f.setFontWeight(QFont::Bold); + _structs_f.setForeground(Qt::green); + + _functions_f.setFontWeight(QFont::Bold); + _functions_f.setForeground(Qt::darkGreen); + + _ints_f.setFontWeight(QFont::Bold); + _ints_f.setForeground(Qt::darkBlue); + + _floats_f.setFontWeight(QFont::Bold); + _floats_f.setForeground(Qt::darkRed); + + _strings_f.setFontWeight(QFont::Bold); + _strings_f.setFontItalic(true); + _strings_f.setForeground(Qt::darkYellow); + + _comment_f.setFontItalic(true); + _comment_f.setForeground(Qt::darkGray); + + + // regexp + pattern.append("\\b("); + pattern.append(structs.split(" ").join("|")); + pattern.append(")\\b"); + _structs_re.setPattern(pattern); + + pattern.clear(); + pattern.append("\\b("); + pattern.append(functions.split(" ").join("|")); + pattern.append(")\\b"); + _functions_re.setPattern(pattern); + + _ints_re.setPattern("\\b[0-9]+\\b"); + + _floats_re.setPattern("\\b[0-9]+\\.[0-9]+\\b"); + + _strings_re.setPattern("(\"[^\"]*)\""); + + _comment_re.setPattern("//[^\n]*"); +} diff --git a/editor/FxSyntax.hxx b/editor/FxSyntax.hxx new file mode 100644 index 0000000..b8b6e59 --- /dev/null +++ b/editor/FxSyntax.hxx @@ -0,0 +1,29 @@ +#pragma once + +#include + +class FxSyntax : public QSyntaxHighlighter { + Q_OBJECT +public: + explicit FxSyntax(QObject *parent = 0); + explicit FxSyntax(QTextDocument *parent = 0); + virtual ~FxSyntax(); + +protected: + virtual void highlightBlock(const QString& text) override; + void setup(); + +private: + QTextCharFormat _structs_f; + QTextCharFormat _functions_f; + QTextCharFormat _ints_f; + QTextCharFormat _floats_f; + QTextCharFormat _strings_f; + QTextCharFormat _comment_f; + QRegExp _structs_re; + QRegExp _functions_re; + QRegExp _ints_re; + QRegExp _floats_re; + QRegExp _strings_re; + QRegExp _comment_re; +}; diff --git a/editor/IniSyntax.cxx b/editor/IniSyntax.cxx new file mode 100644 index 0000000..7b2e547 --- /dev/null +++ b/editor/IniSyntax.cxx @@ -0,0 +1,99 @@ +#include "IniSyntax.hxx" + +// --- internal constants --- + +const QString keywords("set"); + +// --- constructors and deconstructors --- + +IniSyntax::IniSyntax(QObject *parent) +: QSyntaxHighlighter(parent) +{ + setup(); +} + +IniSyntax::IniSyntax(QTextDocument *parent) +: QSyntaxHighlighter(parent) +{ + setup(); +} + +IniSyntax::~IniSyntax() +{ +} + +// --- protected methods --- + +void IniSyntax::highlightBlock(const QString& text) +{ + qint32 index = 0; + + setCurrentBlockState(0); + + // keywords + index = text.indexOf(_keywords_re); + while (index >= 0) + { + auto len = _keywords_re.matchedLength(); + + setFormat(index, len, _keywords_f); + index = text.indexOf(_keywords_re, index + len); + } + + // ints + index = text.indexOf(_ints_re); + while (index >= 0) + { + auto len = _ints_re.matchedLength(); + + setFormat(index, len, _ints_f); + index = text.indexOf(_ints_re, index + len); + } + + // strings + index = text.indexOf(_strings_re); + while (index >= 0) + { + auto len = _strings_re.matchedLength(); + + setFormat(index, len, _strings_f); + index = text.indexOf(_strings_re, index + len); + } + + // single line comments + index = text.indexOf(_comment_re); + if (index >= 0) + setFormat(index, _comment_re.matchedLength(), _comment_f); +} + +void IniSyntax::setup() +{ + QString pattern; + + // format + _keywords_f.setFontWeight(QFont::Bold); + _keywords_f.setForeground(Qt::green); + + _ints_f.setFontWeight(QFont::Bold); + _ints_f.setForeground(Qt::darkBlue); + + _strings_f.setFontWeight(QFont::Bold); + _strings_f.setFontItalic(true); + _strings_f.setForeground(Qt::darkYellow); + + _comment_f.setFontItalic(true); + _comment_f.setForeground(Qt::darkGray); + + + // regexp + pattern.append("\\b("); + pattern.append(keywords.split(" ").join("|")); + pattern.append(")\\b"); + _keywords_re.setPattern(pattern); + + _ints_re.setPattern("\\b[0-9]+\\b"); + + _strings_re.setPattern("(\"[^\"]*)\""); + + _comment_re.setPattern("//[^\n]*"); +} diff --git a/editor/IniSyntax.hxx b/editor/IniSyntax.hxx new file mode 100644 index 0000000..82472de --- /dev/null +++ b/editor/IniSyntax.hxx @@ -0,0 +1,25 @@ +#pragma once + +#include + +class IniSyntax : public QSyntaxHighlighter { + Q_OBJECT +public: + explicit IniSyntax(QObject *parent = 0); + explicit IniSyntax(QTextDocument *parent = 0); + virtual ~IniSyntax(); + +protected: + virtual void highlightBlock(const QString& text) override; + void setup(); + +private: + QTextCharFormat _keywords_f; + QTextCharFormat _ints_f; + QTextCharFormat _strings_f; + QTextCharFormat _comment_f; + QRegExp _keywords_re; + QRegExp _ints_re; + QRegExp _strings_re; + QRegExp _comment_re; +}; diff --git a/editor/MainWindow.cxx b/editor/MainWindow.cxx index a50fc06..b21bb5e 100644 --- a/editor/MainWindow.cxx +++ b/editor/MainWindow.cxx @@ -30,6 +30,7 @@ const QColor Green(180, 255, 180); const QColor Grey(180, 180, 180); const QColor Red(255, 180, 180); const QString TableHeader("header size;position;compressed size;original size;name length;name;SHA-1"); +const QRegExp TextFiles("\\b(*.def|*.fx|*.ini|*.txt|.tsc|*.lua)\\b"); // --- constructors and deconstructors --- @@ -341,7 +342,7 @@ void MainWindow::slotTableSelected() if (text.contains(".def", Qt::CaseInsensitive) || text.contains(".fx", Qt::CaseInsensitive) || text.contains(".ini", Qt::CaseInsensitive) || text.contains(".txt", Qt::CaseInsensitive) - || text.contains(".tsc", Qt::CaseInsensitive)) + || text.contains(".tsc", Qt::CaseInsensitive) || text.contains(".lua", Qt::CaseInsensitive)) _ui->act_file_text->setEnabled(true); else _ui->act_file_image->setEnabled(false); diff --git a/editor/TextWindow.cxx b/editor/TextWindow.cxx index fc74001..22e1bbf 100644 --- a/editor/TextWindow.cxx +++ b/editor/TextWindow.cxx @@ -1,11 +1,15 @@ #include #include "TextWindow.hxx" #include "ui_TextWindow.h" +#include "DefSyntax.hxx" +#include "FxSyntax.hxx" +#include "IniSyntax.hxx" // --- constructors and deconstructors --- TextWindow::TextWindow(QWidget *parent) -: QMainWindow(parent), _ui(new Ui::TextWindow), _num(0) +: QMainWindow(parent), _ui(new Ui::TextWindow), _def(new DefSyntax(this)), _fx(new FxSyntax(this)), + _ini(new IniSyntax(this)), _num(0) { _ui->setupUi(this); @@ -15,6 +19,9 @@ TextWindow::TextWindow(QWidget *parent) TextWindow::~TextWindow() { + delete _ini; + delete _fx; + delete _def; delete _ui; } @@ -45,10 +52,20 @@ void TextWindow::slotTextView(NCFile& entry, quint32 row) { _entry = entry; _num = row; + _def->setDocument(0); _ui->ed_text->setPlainText(entry.oData()); _ui->ed_name->setText(entry.name()); _ui->ed_lines->setText(QString::number(_ui->ed_text->document()->lineCount())); _ui->ed_size->setText(QString::number(_ui->ed_text->toPlainText().size())); + + if (entry.name().contains(".def", Qt::CaseInsensitive)) + _def->setDocument(_ui->ed_text->document()); + + if (entry.name().contains(".fx", Qt::CaseInsensitive)) + _fx->setDocument(_ui->ed_text->document()); + + if (entry.name().contains(".ini", Qt::CaseInsensitive)) + _ini->setDocument(_ui->ed_text->document()); } // --- protected methods --- diff --git a/editor/TextWindow.hxx b/editor/TextWindow.hxx index a5b0845..e4823f8 100644 --- a/editor/TextWindow.hxx +++ b/editor/TextWindow.hxx @@ -7,6 +7,9 @@ namespace Ui { class TextWindow; } +class DefSyntax; +class FxSyntax; +class IniSyntax; class TextWindow : public QMainWindow { Q_OBJECT @@ -38,6 +41,9 @@ protected: private: Ui::TextWindow *_ui; + DefSyntax *_def; + FxSyntax *_fx; + IniSyntax *_ini; NCFile _entry; quint32 _num; }; diff --git a/tools/bsp_viewer.cxx b/tools/bsp_viewer.cxx index 4eeb168..ebf6bdb 100644 --- a/tools/bsp_viewer.cxx +++ b/tools/bsp_viewer.cxx @@ -21,13 +21,6 @@ namespace Data }; } -struct BSPHeader { - uint32_t magic; - uint32_t unknown; - uint32_t version; - uint16_t systime[8]; -}; - struct Chunk { uint32_t type; uint32_t size; @@ -74,23 +67,6 @@ void display() } glEnd(); } - /*for (Vertex& vert : vertices) - { - glBegin(GL_POINTS); - glColor3f(1.0, 1.0, 1.0); - glVertex3f(vert.x / 10.0, vert.y / 10.0, vert.z / 10.0); - glEnd(); - }*/ - /*glBegin(GL_POINTS); - glColor3f(1.0, 1.0, 1.0); - glVertex3f(-496.0, -512.0, -233.0); - glVertex3f(-496.0, -512.0, -322.0); - glVertex3f(-496.0, -512.0, -452.0); - glVertex3f(-304.0, -512.0, -452.0); - glVertex3f(-304.0, -512.0, -357.0); - glVertex3f(-304.0, -512.0, -304.0); - glVertex3f(-304.0, -512.0, -233.0); - glEnd();*/ glutSwapBuffers(); } @@ -108,7 +84,6 @@ int main(int argc, char **argv) { std::string filename = "test.bsp"; std::ifstream file; - struct BSPHeader header; if (argc == 2) filename = argv[1]; @@ -123,7 +98,7 @@ int main(int argc, char **argv) if (file.is_open() && file.good()) { Chunk chunk; - size_t size = file.seekg(0, std::ios_base::end).tellg(); + //size_t size = file.seekg(0, std::ios_base::end).tellg(); file.seekg(0, std::ios_base::beg); @@ -192,29 +167,6 @@ int main(int argc, char **argv) file.close(); } - //for (Vertex& vert : vertices) - // std::cout << "vertex " << vert.x << " " << vert.y << " " << vert.z << std::endl; - - //for (uint32_t& ind : indexes) - // std::cout << "index " << ind << std::endl; - - /*for (Face& face : faces) - { - std::cout << "Face: " << face.vertex_count << " (" << face.first << ") ->"; - for (size_t i = 0; i < face.vertex_count; ++i) - { - std::cout << " " << indexes[i + face.first]; - } - std::cout << std::endl; - std::cout << " "; - for (size_t i = 0; i < face.vertex_count; ++i) - { - Vertex& vert = vertices[indexes[i + face.first]]; - std::cout << " (" << vert.x << ", " << vert.y << ", " << vert.z << ")"; - } - std::cout << std::endl << std::endl; - }*/ - glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(800, 600); -- 2.15.1