From 3fe4a536c5bb7439ae893dfaa0fb397076dc9b24 Mon Sep 17 00:00:00 2001 From: Epicalert Date: Tue, 10 Sep 2024 05:12:05 +0800 Subject: [PATCH] Initial commit it works!! --- .gitignore | 76 ++++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 44 +++++++++++++++++++++++++++ Main.qml | 73 ++++++++++++++++++++++++++++++++++++++++++++ TexSlot.qml | 58 +++++++++++++++++++++++++++++++++++ main.cpp | 22 ++++++++++++++ texturecrafter.cpp | 49 ++++++++++++++++++++++++++++++ texturecrafter.h | 21 +++++++++++++ 7 files changed, 343 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Main.qml create mode 100644 TexSlot.qml create mode 100644 main.cpp create mode 100644 texturecrafter.cpp create mode 100644 texturecrafter.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fb8642 --- /dev/null +++ b/.gitignore @@ -0,0 +1,76 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + +# build dir +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0e5ddfe --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.16) + +project(texcrafting VERSION 0.1 LANGUAGES CXX) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Qt6 6.2 REQUIRED COMPONENTS Quick QuickControls2) + + +qt_add_executable(apptexcrafting + main.cpp +) + +qt_add_qml_module(apptexcrafting + URI texcrafting + VERSION 1.0 + QML_FILES + Main.qml + TexSlot.qml + SOURCES texturecrafter.h texturecrafter.cpp +) + +# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. +# If you are developing for iOS or macOS you should consider setting an +# explicit, fixed bundle identifier manually though. +set_target_properties(apptexcrafting PROPERTIES +# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.apptexcrafting + MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} + MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} + MACOSX_BUNDLE TRUE + WIN32_EXECUTABLE TRUE +) + +target_link_libraries(apptexcrafting + PRIVATE Qt6::Quick +) + +include(GNUInstallDirs) +install(TARGETS apptexcrafting + BUNDLE DESTINATION . + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) diff --git a/Main.qml b/Main.qml new file mode 100644 index 0000000..5752306 --- /dev/null +++ b/Main.qml @@ -0,0 +1,73 @@ +import QtQuick +import QtQuick.Layouts +import QtQuick.Controls + +import texcrafting + +ApplicationWindow { + width: 640 + height: 480 + visible: true + title: qsTr("Hello World") + + TextureCrafter { + id: crafter + } + + RowLayout { + anchors.fill: parent + + ColumnLayout { + Layout.fillWidth: true + + Layout.alignment: Qt.AlignHCenter + + TexSlot { + id: texSlotR + + rectColor: "red" + } + + TexSlot { + id: texSlotG + + rectColor: "green" + } + + TexSlot { + id: texSlotB + + rectColor: "blue" + } + } + + ColumnLayout { + Layout.fillWidth: true + + Layout.alignment: Qt.AlignHCenter + + ProgressBar { + value: 0.5 + } + + Button { + Layout.alignment: Qt.AlignHCenter + text: qsTr("Craft!") + + onClicked: crafter.packChannels([texSlotR.filePath, texSlotG.filePath, texSlotB.filePath]) + } + } + + ColumnLayout { + Layout.fillWidth: true + + Layout.alignment: Qt.AlignHCenter + + TexSlot { + + + rectColor: "green" + } + } + } +} diff --git a/TexSlot.qml b/TexSlot.qml new file mode 100644 index 0000000..2ba4eb4 --- /dev/null +++ b/TexSlot.qml @@ -0,0 +1,58 @@ +import QtQuick +import QtQuick.Layouts +import QtQuick.Controls + +DropArea { + property string rectColor: "white" + property url filePath: "" + + Layout.alignment: Qt.AlignCenter + + width: 128 + height: 128 + + + Rectangle { + id: rectangle + anchors.fill: parent + color: rectColor + + Image { + id: image + + anchors.centerIn: parent + + width: 96 + height: 96 + + + } + } + + Label { + id: huh + + text: "無" + } + + onEntered: { + rectangle.color = "cyan"; + drag.accept (Qt.LinkAction); + } + onDropped: { + // TODO: handle dragging all channels at once + var firstUrl = new URL(drop.urls[0]); + if (firstUrl.protocol === "file:") + { + console.log(drop.urls); + + filePath = drop.urls[0]; + huh.text = drop.urls[0]; + image.source = drop.urls[0]; + } + rectangle.color = rectColor; + } + onExited: { + rectangle.color = rectColor; + } +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..de5d5c4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + const QUrl url(QStringLiteral("qrc:/texcrafting/Main.qml")); + QObject::connect( + &engine, + &QQmlApplicationEngine::objectCreated, + &app, + [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, + Qt::QueuedConnection); + engine.load(url); + + return app.exec(); +} diff --git a/texturecrafter.cpp b/texturecrafter.cpp new file mode 100644 index 0000000..1a72ab9 --- /dev/null +++ b/texturecrafter.cpp @@ -0,0 +1,49 @@ +#include "texturecrafter.h" + +TextureCrafter::TextureCrafter(QObject *parent) + : QObject{parent} +{} + +void TextureCrafter::packChannels(QVector imagePaths) { + QVector sourceImages; + int width = 0, height = 0; + QImage::Format format = QImage::Format_RGB888; + + for (int i = 0; i < imagePaths.length(); i++) { + // if the ui layer did its job this will always be a file:// url + QString rawPath = imagePaths[i].path(); + QImage newImage(rawPath); // would this cause a use after free idk + + // we just use the first image's dimensions and force the others to conform hehe + if (width == 0) { + width = newImage.width(); + } + if (height == 0) { + height = newImage.height(); + } + + sourceImages.append(newImage); + } + + QImage outImage(width, height, format); + + // theres probably a faster way to do this but eh + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + int r, g, b; + + // too eepy for dry + r = sourceImages.at(0).pixelColor(i, j).red(); + g = sourceImages.at(1).pixelColor(i, j).green(); + b = sourceImages.at(2).pixelColor(i, j).blue(); + + QColor newColor(r, g, b); + + outImage.setPixelColor(i, j, newColor); + } + } + + outImage.save("/tmp/out.png"); + + //do we need to close these??? idk lets hope qt handles it +} diff --git a/texturecrafter.h b/texturecrafter.h new file mode 100644 index 0000000..8631ca6 --- /dev/null +++ b/texturecrafter.h @@ -0,0 +1,21 @@ +#ifndef TEXTURECRAFTER_H +#define TEXTURECRAFTER_H + +#include +#include +#include + +class TextureCrafter : public QObject +{ + Q_OBJECT + QML_ELEMENT +public: + explicit TextureCrafter(QObject *parent = nullptr); + + Q_INVOKABLE void packChannels(QVector images); + +signals: +private: +}; + +#endif // TEXTURECRAFTER_H