diff --git a/3DEditor/CMakeLists.txt b/3DEditor/CMakeLists.txt deleted file mode 100644 index e11e387..0000000 --- a/3DEditor/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -project(3DEditor) - -### ---------------------- Externals --------------------- ### -set(BINDINGS_INCLUDE ../Externals/glfw/include ../Externals) -set(BINDINGS_LIBS glfw Imgui) - -### ---------------------- Static Library --------------------- ### -file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp") -file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp") - -add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) - -target_include_directories(${PROJECT_NAME} PUBLIC ./public/ ${BINDINGS_INCLUDE} ./ext/) -target_link_libraries(${PROJECT_NAME} PUBLIC Graphics Connection Widgets Math RasterRender) -target_link_libraries(${PROJECT_NAME} PUBLIC ${BINDINGS_LIBS}) - -### -------------------------- Applications -------------------------- ### -add_executable(3DEditorApp ./applications/Entry.cpp ./applications/SceneLoad.cpp) -target_link_libraries(3DEditorApp ${PROJECT_NAME} Lua ImageIO) - -file(COPY "rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") -file(COPY "rsc/Font.ttf" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") diff --git a/3DEditor/applications/Entry.cpp b/3DEditor/applications/Entry.cpp deleted file mode 100644 index fb1e37e..0000000 --- a/3DEditor/applications/Entry.cpp +++ /dev/null @@ -1,42 +0,0 @@ - -#include "EditorWidget.hpp" - -#include "GraphicApplication.hpp" - -using namespace tp; - -bool loadMeshes(tp::Scene& scene, const std::string& objetsPath); - -class EditorGUI : public Application { -public: - EditorGUI() { - Vec2F renderResolution = { 1000, 1000 }; - auto canvas = this->mGraphics->getCanvas(); - // mGui = new EditorWidget(canvas, &geometry, renderResolution); - - mGui = new ShortcutsTest(); - - loadMeshes(geometry, "rsc/scene.obj"); - - geometry.mCamera.lookAtPoint({ 0, 0, 0 }, { 3, 3, 2 }, { 0, 0, 1 }); - } - - ~EditorGUI() override { delete mGui; } - - void processFrame(EventHandler* eventHandler) override { - - auto rec = RectF({ 0, 0 }, mWindow->getSize()); - mGui->proc(*eventHandler, rec, rec); - } - - void drawFrame(Canvas* canvas) override { mGui->draw(*canvas); } - -private: - Scene geometry; - ShortcutsTest* mGui; -}; - -int main() { - EditorGUI gui; - gui.run(); -} diff --git a/3DEditor/applications/SceneLoad.cpp b/3DEditor/applications/SceneLoad.cpp deleted file mode 100644 index bc03e0c..0000000 --- a/3DEditor/applications/SceneLoad.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "Scene.hpp" - -#include "obj/OBJ_Loader.h" -#include - -bool loadMeshes(tp::Scene& scene, const std::string& objetsPath) { - using namespace tp; - - objl::Loader Loader; - - if (!Loader.LoadFile(objetsPath.c_str())) { - std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n"; - return false; - } - - for (auto& curMesh : Loader.LoadedMeshes) { - scene.mObjects.append(Object()); - - auto object = &scene.mObjects.last(); - - for (auto& vertex : curMesh.Vertices) { - // printf("{ %f, %f, %f }, \n", vertex.Position.X, vertex.Position.Y, vertex.Position.Z); - object->mTopology.Points.append(Vec3F{ vertex.Position.X, vertex.Position.Y, vertex.Position.Z }); - object->mTopology.Normals.append(Vec3F{ vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z }); - } - - for (int j = 0; j < curMesh.Indices.size(); j += 3) { - uint idx1 = (int) curMesh.Indices[j]; - uint idx2 = (int) curMesh.Indices[j + 1]; - uint idx3 = (int) curMesh.Indices[j + 2]; - // printf("{ %i, %i, %i },\n", idx1, idx2, idx3); - object->mTopology.Indexes.append({ idx1, idx2, idx3 }); - } - - if (object->mTopology.Normals.size() != object->mTopology.Points.size()) { - printf("Logic error loading normals\n"); - } - } - - return scene.mObjects.size(); -} diff --git a/3DEditor/private/Render.cpp b/3DEditor/private/Render.cpp deleted file mode 100644 index ec98be5..0000000 --- a/3DEditor/private/Render.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include "Render.hpp" - -#include "GraphicsApi.hpp" - -using namespace tp; - -class ObjectBuffers { -public: - ObjectBuffers(Object* object) { - mObject = (object); - - auto& buff = mObject->mTopology.Points; - auto& indices = mObject->mTopology.Indexes; - - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3F) * buff.size(), buff.getBuff(), GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Vec3) * indices.size(), indices.getBuff(), GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr); - glEnableVertexAttribArray(0); - - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - - Object* mObject = nullptr; - - GLuint VAO = 0; - GLuint VBO = 0; - GLuint EBO = 0; - - void drawCall() { - auto& indices = mObject->mTopology.Indexes; - - glBindVertexArray(VAO); - - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, nullptr); - - // glDrawArrays(GL_TRIANGLES, 0, sizeof(buffer) / (2 * sizeof(float))); - - // glBindVertexArray(0); - } - - ~ObjectBuffers() { - glDeleteBuffers(1, &VBO); - glDeleteVertexArrays(1, &VAO); - } -}; - -Render::Render(Vec2F renderResolution) : - mRenderBuffer(renderResolution) { - - mDefaultShader.load("rsc/shaders/default.vert", nullptr, "rsc/shaders/default.frag", true); -} - -Render::~Render() {} - -uint4 Render::getRenderBuffer() { return mRenderBuffer.texId(); } - -Vec2F Render::getBufferSize() { return mRenderBuffer.getSize(); } - -void Render::render(const Scene& geometry, Vec2F size) { - - for (auto object : geometry.mObjects) { - if (!object->mBuffers) { - object->mBuffers = std::make_shared(&object.data()); - } - } - - mRenderBuffer.mClearCol = { 0.0f, 0.0f, 0.0f, 0.f }; - - mRenderBuffer.beginDraw(); - mRenderBuffer.clear(); - - mDefaultShader.bind(); - - Mat4F cameraMat = geometry.mCamera.calculateTransformationMatrix(); - - glEnable(GL_DEPTH_TEST); - - for (auto object : geometry.mObjects) { - - static auto origin = (GLint) mDefaultShader.getu("Origin"); - static auto basis = (GLint) mDefaultShader.getu("Basis"); - static auto camera = (GLint) mDefaultShader.getu("Camera"); - - Mat4F basisMat; - Vec4F originPoint; - - glUniform4fv(origin, 1, &originPoint[0]); - glUniformMatrix4fv(basis, 1, false, &basisMat[0][0]); - glUniformMatrix4fv(camera, 1, true, &cameraMat[0][0]); - - object->mBuffers->drawCall(); - } - - mDefaultShader.unbind(); - - mRenderBuffer.endDraw(); -} diff --git a/3DEditor/public/EditorWidget.hpp b/3DEditor/public/EditorWidget.hpp deleted file mode 100644 index cabd207..0000000 --- a/3DEditor/public/EditorWidget.hpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "Widgets.hpp" -#include "Render.hpp" - -namespace tp { - - template - class ShortcutsTest : public Widget { - public: - ShortcutsTest() { this->createConfig("ShortcutsTest"); } - - void action(const Events&) { - // - } - - void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { - this->mArea = aArea; - this->mVisible = areaParent.isOverlap(aArea); - if (!this->mVisible) return; - } - - void draw(Canvas& canvas) override { - if (!this->mVisible) return; - canvas.rect(this->mArea, this->getColor("Base")); - } - - void populateConfig() override { - this->addColor("Base", "Base"); - - this->addOperator("OperatorName", { this, [](void* self, const Events& events) { - ((ShortcutsTest*) self)->action(events); - } }); - - this->getShortcuts("OperatorName").append({ { "Alt", "Hold" }, { "Mouse1", "Hold" } }); - this->getShortcuts("OperatorName").append({ { "Alt", "Hold" }, { "Mouse1", "Hold" } }); - } - }; - - template - class ViewportWidget : public Widget { - public: - explicit ViewportWidget(Canvas* canvas, Scene* geometry, Vec2F renderResolution) : - mRender(renderResolution) { - this->createConfig("ViewportWidget"); - - mImage = canvas->createImageFromTextId(mRender.getRenderBuffer(), mRender.getBufferSize()); - mGeometry = geometry; - mCanvas = canvas; - } - - ~ViewportWidget() { mCanvas->deleteImageHandle(mImage); } - - void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { - this->mArea = aArea; - this->mVisible = areaParent.isOverlap(aArea); - if (!this->mVisible) return; - - mGeometry->mCamera.rotate(0.01f, 0.0); - } - - void draw(Canvas& canvas) override { - if (!this->mVisible) return; - - mRender.render(*mGeometry, this->mArea.size); - canvas.drawImage(this->mArea, &mImage, PI); - } - - public: - Render mRender; - Scene* mGeometry = nullptr; - Canvas* mCanvas = nullptr; - Canvas::ImageHandle mImage; - }; - - template - class EditorWidget : public Widget { - public: - EditorWidget(Canvas* canvas, Scene* geometry, Vec2F renderResolution) : - mViewport(canvas, geometry, renderResolution) { - this->createConfig("EditorWidget"); - this->addColor("Base", "Base"); - } - - void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { - this->mArea = aArea; - this->mVisible = areaParent.isOverlap(aArea); - if (!this->mVisible) return; - - mSplitView.proc(events, aArea, aArea); - mViewport.proc(events, aArea, mSplitView.getFirst()); - } - - void draw(Canvas& canvas) override { - if (!this->mVisible) return; - - canvas.rect(this->mArea, this->getColor("Base")); - mSplitView.draw(canvas); - mViewport.draw(canvas); - } - - public: - ViewportWidget mViewport; - SplitView mSplitView; - }; -} \ No newline at end of file diff --git a/3DEditor/public/Render.hpp b/3DEditor/public/Render.hpp deleted file mode 100644 index 818ffe8..0000000 --- a/3DEditor/public/Render.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "Scene.hpp" -#include "Rect.hpp" - -#include "FrameBuffer.hpp" -#include "Shader.hpp" - -namespace tp { - class Render { - public: - explicit Render(Vec2F renderResolution); - ~Render(); - - void render(const Scene& geometry, Vec2F size); - uint4 getRenderBuffer(); - Vec2F getBufferSize(); - - private: - RenderBuffer mRenderBuffer; - RenderShader mDefaultShader; - }; -} \ No newline at end of file diff --git a/3DEditor/public/Scene.hpp b/3DEditor/public/Scene.hpp deleted file mode 100644 index 9b995ba..0000000 --- a/3DEditor/public/Scene.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "Topology.hpp" -#include - -class ObjectBuffers; - -namespace tp { - - class Object { - public: - Object() = default; - - public: - Topology mTopology; - std::shared_ptr mBuffers; - }; - - struct PointLight { - Vec3F pos; - halnf fallOut = 1.f; - halnf intensity = 1.f; - }; - - class Scene { - public: - Scene() = default; - - public: - Buffer mObjects; - Buffer mLights; - Camera mCamera; - }; -} \ No newline at end of file diff --git a/3DEditor/rsc/Font.ttf b/3DEditor/rsc/Font.ttf deleted file mode 100644 index 8a63054..0000000 Binary files a/3DEditor/rsc/Font.ttf and /dev/null differ diff --git a/3DEditor/rsc/scene.obj b/3DEditor/rsc/scene.obj deleted file mode 100644 index 4368e9e..0000000 --- a/3DEditor/rsc/scene.obj +++ /dev/null @@ -1,112 +0,0 @@ -# Blender v3.0.1 OBJ File: 'scene.blend' -# www.blender.org -mtllib meshes.mtl -o Cube -v -1.039076 1.039076 1.792917 -v -1.039076 1.039076 -1.285234 -v 1.039076 1.039076 1.792917 -v 1.039076 1.039076 -1.285234 -v -1.039076 -1.039076 1.792917 -v -1.039076 -1.039076 -1.285234 -v 1.039076 -1.039076 1.792917 -v 1.039076 -1.039076 -1.285234 -vt 0.625000 0.500000 -vt 0.625000 0.750000 -vt 0.875000 0.750000 -vt 0.875000 0.500000 -vt 0.375000 0.750000 -vt 0.375000 1.000000 -vt 0.625000 1.000000 -vt 0.375000 0.000000 -vt 0.375000 0.250000 -vt 0.625000 0.250000 -vt 0.625000 0.000000 -vt 0.125000 0.500000 -vt 0.125000 0.750000 -vt 0.375000 0.500000 -vn 0.0000 0.0000 -1.0000 -vn -1.0000 0.0000 0.0000 -vn 0.0000 1.0000 0.0000 -vn 0.0000 0.0000 1.0000 -vn 1.0000 0.0000 0.0000 -usemtl Material -s off -f 1/1/1 3/2/1 7/3/1 5/4/1 -f 4/5/2 8/6/2 7/7/2 3/2/2 -f 8/8/3 6/9/3 5/10/3 7/11/3 -f 6/12/4 8/13/4 4/5/4 2/14/4 -f 6/9/5 2/14/5 1/1/5 5/10/5 -o Cube.001 -v 0.411013 -0.208216 -1.264212 -v 0.411013 -0.208216 -0.612621 -v -0.208216 -0.411013 -1.264212 -v -0.208216 -0.411013 -0.612621 -v 0.208216 0.411013 -1.264212 -v 0.208216 0.411013 -0.612621 -v -0.411013 0.208216 -1.264212 -v -0.411013 0.208216 -0.612621 -vt 0.375000 0.000000 -vt 0.625000 0.000000 -vt 0.625000 0.250000 -vt 0.375000 0.250000 -vt 0.625000 0.500000 -vt 0.375000 0.500000 -vt 0.625000 0.750000 -vt 0.375000 0.750000 -vt 0.625000 1.000000 -vt 0.375000 1.000000 -vt 0.125000 0.500000 -vt 0.125000 0.750000 -vt 0.875000 0.500000 -vt 0.875000 0.750000 -vn 0.3112 -0.9503 0.0000 -vn -0.9503 -0.3112 0.0000 -vn -0.3112 0.9503 0.0000 -vn 0.9503 0.3112 0.0000 -vn 0.0000 0.0000 -1.0000 -vn 0.0000 0.0000 1.0000 -usemtl None -s off -f 9/15/6 10/16/6 12/17/6 11/18/6 -f 11/18/7 12/17/7 16/19/7 15/20/7 -f 15/20/8 16/19/8 14/21/8 13/22/8 -f 13/22/9 14/21/9 10/23/9 9/24/9 -f 11/25/10 15/20/10 13/22/10 9/26/10 -f 16/19/11 12/27/11 10/28/11 14/21/11 -o Cube.002 -v -0.351718 -0.467100 -0.602050 -v -0.351718 -0.467100 0.049542 -v -0.800825 0.004996 -0.602050 -v -0.800825 0.004996 0.049542 -v 0.120377 -0.017993 -0.602050 -v 0.120377 -0.017993 0.049542 -v -0.328730 0.454103 -0.602050 -v -0.328730 0.454103 0.049542 -vt 0.375000 0.000000 -vt 0.625000 0.000000 -vt 0.625000 0.250000 -vt 0.375000 0.250000 -vt 0.625000 0.500000 -vt 0.375000 0.500000 -vt 0.625000 0.750000 -vt 0.375000 0.750000 -vt 0.625000 1.000000 -vt 0.375000 1.000000 -vt 0.125000 0.500000 -vt 0.125000 0.750000 -vt 0.875000 0.500000 -vt 0.875000 0.750000 -vn -0.7245 -0.6892 0.0000 -vn -0.6892 0.7245 0.0000 -vn 0.7245 0.6892 0.0000 -vn 0.6892 -0.7245 0.0000 -vn 0.0000 0.0000 -1.0000 -vn 0.0000 0.0000 1.0000 -usemtl None -s off -f 17/29/12 18/30/12 20/31/12 19/32/12 -f 19/32/13 20/31/13 24/33/13 23/34/13 -f 23/34/14 24/33/14 22/35/14 21/36/14 -f 21/36/15 22/35/15 18/37/15 17/38/15 -f 19/39/16 23/34/16 21/36/16 17/40/16 -f 24/33/17 20/41/17 18/42/17 22/35/17 diff --git a/3DEditor/rsc/shaders/default.frag b/3DEditor/rsc/shaders/default.frag deleted file mode 100644 index e142a37..0000000 --- a/3DEditor/rsc/shaders/default.frag +++ /dev/null @@ -1,7 +0,0 @@ -#version 330 core - -out vec4 FragColor; - -void main() { - FragColor = vec4(gl_FragCoord.z, gl_FragCoord.z, gl_FragCoord.z, 1.f); -} \ No newline at end of file diff --git a/3DEditor/rsc/shaders/default.vert b/3DEditor/rsc/shaders/default.vert deleted file mode 100644 index 7d1fd94..0000000 --- a/3DEditor/rsc/shaders/default.vert +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 Point; - -uniform vec4 Origin; -uniform mat4 Basis; -uniform mat4 Camera; - -void main() { - gl_Position = Camera * vec4(Point.xyz, 1.0); -} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index c211e67..61301a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,4 @@ add_subdirectory(DataAnalysis) add_subdirectory(Objects) add_subdirectory(Widgets) add_subdirectory(LibraryViewer) -add_subdirectory(RasterRender) -add_subdirectory(Sketch3D) -add_subdirectory(3DEditor) +add_subdirectory(Sketch3D) \ No newline at end of file diff --git a/Containers/public/Map.hpp b/Containers/public/Map.hpp index fd50491..642a6d9 100644 --- a/Containers/public/Map.hpp +++ b/Containers/public/Map.hpp @@ -11,12 +11,7 @@ namespace tp { return hash(key); } - template < - typename tKey, - typename tVal, - class tAllocator = DefaultAllocator, - ualni (*tHashFunc)(SelectValueOrReference) = DefaultHashFunc, - int tTableInitialSize = 4> + template ) = DefaultHashFunc, int tTableInitialSize = 4> class Map { enum { @@ -228,13 +223,6 @@ namespace tp { return mTable[slot]->val; } - tVal& operator[](KeyArg key) { - auto idx = presents(key); - if (idx.isValid()) return getSlotVal(idx); - put(key, {}); - return get(key); - } - tVal& getSlotVal(ualni slot) { DEBUG_ASSERT(slot < mNSlots && (mTable[slot] && !isDeletedNode(mTable[slot])) && "Key Error") return mTable[slot]->val; @@ -254,7 +242,7 @@ namespace tp { if (this == &in) { return *this; } - + for (ualni i = 0; i < mNSlots; i++) { if (mTable[i] && !isDeletedNode(mTable[i])) { deleteNode(mTable[i]); diff --git a/Graphics/public/Graphics.hpp b/Graphics/public/Graphics.hpp index 4d35595..6eb5aa6 100644 --- a/Graphics/public/Graphics.hpp +++ b/Graphics/public/Graphics.hpp @@ -62,7 +62,7 @@ namespace tp { ImageHandle createImageFromTextId(ualni id, Vec2F size); void deleteImageHandle(ImageHandle image); - void drawImage(const RectF& rec, ImageHandle* image, halnf angle = 0, halnf alpha = 1.f, halnf rounding = 0.f); + void drawImage(const RectF& rec, ImageHandle* image, halnf angle, halnf alpha, halnf rounding); private: Buffer mScissors; @@ -71,9 +71,7 @@ namespace tp { class Graphics { public: - explicit Graphics(Window* window) : - mGui(window), - mCanvas(window) {} + explicit Graphics(Window* window) : mGui(window), mCanvas(window) {} void procBegin() { mCanvas.procBegin(); @@ -90,12 +88,14 @@ namespace tp { mGui.drawBegin(); } - void drawEnd() { + void drawEnd() { mCanvas.drawEnd(); mGui.drawEnd(); } - Canvas* getCanvas() { return &mCanvas; } + Canvas* getCanvas() { + return &mCanvas; + } private: Canvas mCanvas; diff --git a/Math/public/Topology.hpp b/Math/public/Topology.hpp index 92cb1a0..f29fb36 100644 --- a/Math/public/Topology.hpp +++ b/Math/public/Topology.hpp @@ -34,7 +34,7 @@ namespace tp { Buffer Points; Buffer Normals; - Buffer> Indexes; + Buffer Indexes; }; struct TopologyCache { diff --git a/Objects/applications/GUIEntry.cpp b/Objects/applications/GUIEntry.cpp index b5c518e..5b0c4e8 100644 --- a/Objects/applications/GUIEntry.cpp +++ b/Objects/applications/GUIEntry.cpp @@ -6,9 +6,9 @@ using namespace tp; using namespace obj; -class SimpleGUI : public Application { +class ExampleGUI : public Application { public: - SimpleGUI() { gui.cd(objects_api::create(), "root"); } + ExampleGUI() { gui.cd(objects_api::create(), "root"); } void processFrame(EventHandler* eventHandler) override {} @@ -28,7 +28,7 @@ int main() { if (module.initialize()) { { - SimpleGUI gui; + ExampleGUI gui; gui.run(); } module.deinitialize(); diff --git a/RasterRender/CMakeLists.txt b/RasterRender/CMakeLists.txt deleted file mode 100644 index fd8c89c..0000000 --- a/RasterRender/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -project(RasterRender) - -### ---------------------- Externals --------------------- ### -set(BINDINGS_INCLUDE ${GLEW_INCLUDE_DIR}) -set(BINDINGS_LIBS ${GLEW_LIB}) - -### ---------------------- Static Library --------------------- ### -file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp") -file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp") - -add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) - -target_include_directories(${PROJECT_NAME} PUBLIC ./public/ ${BINDINGS_INCLUDE} ./ext/) -target_link_libraries(${PROJECT_NAME} PUBLIC Math Connection) -target_link_libraries(${PROJECT_NAME} PUBLIC ${BINDINGS_LIBS}) \ No newline at end of file diff --git a/RayTracer/CMakeLists.txt b/RayTracer/CMakeLists.txt index ae8c52b..d40c663 100644 --- a/RayTracer/CMakeLists.txt +++ b/RayTracer/CMakeLists.txt @@ -4,7 +4,7 @@ project(RayTracer) file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp") file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp" "./applications/*.hpp") add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) -target_include_directories(${PROJECT_NAME} PUBLIC ./public/ ../Externals/) +target_include_directories(${PROJECT_NAME} PUBLIC ./public/) target_link_libraries(${PROJECT_NAME} PUBLIC Math Connection) ### -------------------------- Applications -------------------------- ### diff --git a/Externals/obj/OBJ_LOADER_LICENSE.txt b/RayTracer/applications/OBJ_LOADER_LICENSE.txt similarity index 100% rename from Externals/obj/OBJ_LOADER_LICENSE.txt rename to RayTracer/applications/OBJ_LOADER_LICENSE.txt diff --git a/Externals/obj/OBJ_Loader.h b/RayTracer/applications/OBJ_Loader.h similarity index 100% rename from Externals/obj/OBJ_Loader.h rename to RayTracer/applications/OBJ_Loader.h diff --git a/RayTracer/applications/SceneLoad.cpp b/RayTracer/applications/SceneLoad.cpp index a1f53da..724e62b 100644 --- a/RayTracer/applications/SceneLoad.cpp +++ b/RayTracer/applications/SceneLoad.cpp @@ -6,7 +6,7 @@ extern "C" { #include "lualib.h" } -#include "obj/OBJ_Loader.h" +#include "OBJ_Loader.h" #include @@ -139,6 +139,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re // Remove the filename from the path fs::path directoryPath = fullPath.remove_filename(); + if (luaL_dofile(L, scenePath.c_str()) != 0) { lua_close(L); printf("Cant open scene script.\n"); diff --git a/Sketch3D/CMakeLists.txt b/Sketch3D/CMakeLists.txt index 248c80d..21fef33 100644 --- a/Sketch3D/CMakeLists.txt +++ b/Sketch3D/CMakeLists.txt @@ -1,8 +1,8 @@ project(Sketch3D) ### ---------------------- Externals --------------------- ### -set(BINDINGS_INCLUDE ../Externals/glfw/include) -set(BINDINGS_LIBS glfw Imgui) +set(BINDINGS_INCLUDE ../Externals/glfw/include ${GLEW_INCLUDE_DIR}) +set(BINDINGS_LIBS glfw Imgui ${GLEW_LIB}) ### ---------------------- Static Library --------------------- ### file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp") @@ -11,12 +11,12 @@ file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp") add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) target_include_directories(${PROJECT_NAME} PUBLIC ./public/ ${BINDINGS_INCLUDE} ./ext/) -target_link_libraries(${PROJECT_NAME} PUBLIC Graphics Connection Widgets Math RasterRender) +target_link_libraries(${PROJECT_NAME} PUBLIC Graphics Connection Widgets Math) target_link_libraries(${PROJECT_NAME} PUBLIC ${BINDINGS_LIBS}) ### -------------------------- Applications -------------------------- ### add_executable(Sketch3DApp ./applications/Entry.cpp) target_link_libraries(Sketch3DApp ${PROJECT_NAME}) - + file(COPY "rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") file(COPY "applications/Font.ttf" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") diff --git a/RasterRender/private/FrameBuffer.cpp b/Sketch3D/private/FrameBuffer.cpp similarity index 71% rename from RasterRender/private/FrameBuffer.cpp rename to Sketch3D/private/FrameBuffer.cpp index a500e5e..0804fae 100644 --- a/RasterRender/private/FrameBuffer.cpp +++ b/Sketch3D/private/FrameBuffer.cpp @@ -5,26 +5,20 @@ #include void glerr(GLenum type) { printf("GL ERROR\n"); } -#define AssertGL(x) \ - { \ - x; \ - GLenum __gle = glGetError(); \ - if (__gle != GL_NO_ERROR) glerr(__gle); \ - } +#define AssertGL(x) { x; GLenum __gle = glGetError(); if (__gle != GL_NO_ERROR) glerr(__gle); } using namespace tp; RenderBuffer::RenderBuffer(const Vec2F& size) : mSize(size) { - mDrawBuffers[0] = { GL_COLOR_ATTACHMENT0 }; + mDrawBuffers[0] = {GL_COLOR_ATTACHMENT0}; // --------- texture --------- AssertGL(glGenTextures(1, &mTextureId)); AssertGL(glBindTexture(GL_TEXTURE_2D, mTextureId)); // Give an empty image to OpenGL ( the last "0" ) - AssertGL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) size.x, (GLsizei) size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0) - ); + AssertGL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)size.x, (GLsizei)size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)); // Poor filtering. Needed AssertGL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); AssertGL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); @@ -32,7 +26,7 @@ RenderBuffer::RenderBuffer(const Vec2F& size) : // --------- depth --------- AssertGL(glGenRenderbuffers(1, &mDepthBufferID)); AssertGL(glBindRenderbuffer(GL_RENDERBUFFER, mDepthBufferID)); - AssertGL(glRenderbufferStorage(GL_RENDERBUFFER, GLW_CONTEXT_DEPTH_COMPONENT, (GLsizei) size.x, (GLsizei) size.y)); + AssertGL(glRenderbufferStorage(GL_RENDERBUFFER, GLW_CONTEXT_DEPTH_COMPONENT, (GLsizei)size.x, (GLsizei)size.y)); // ------------ framebuffer ------------ AssertGL(glGenFramebuffers(1, &mFrameBufferID)); @@ -55,24 +49,18 @@ RenderBuffer::RenderBuffer(const Vec2F& size, tp::uint1 samples) : AssertGL(glGenTextures(1, &mTextureId)); AssertGL(glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mTextureId)); #ifdef ENV_OS_ANDROID - AssertGL( - glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, (GLsizei) size.x, (GLsizei) size.y, GL_TRUE) - ); + AssertGL(glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, (GLsizei)size.x, (GLsizei)size.y, GL_TRUE)); #else - AssertGL( - glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, (GLsizei) size.x, (GLsizei) size.y, GL_TRUE) - ); + AssertGL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, (GLsizei)size.x, (GLsizei)size.y, GL_TRUE)); #endif // !? - // AssertGL(glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); - // AssertGL(glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + //AssertGL(glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + //AssertGL(glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); // ------- depth ------- AssertGL(glGenRenderbuffers(1, &mDepthBufferID)); AssertGL(glBindRenderbuffer(GL_RENDERBUFFER, mDepthBufferID)); - AssertGL(glRenderbufferStorageMultisample( - GL_RENDERBUFFER, samples, GLW_CONTEXT_DEPTH_COMPONENT, (GLsizei) size.x, (GLsizei) size.y - )); + AssertGL(glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GLW_CONTEXT_DEPTH_COMPONENT, (GLsizei)size.x, (GLsizei)size.y)); // ------- fbuff ------- AssertGL(glGenFramebuffers(1, &mFrameBufferID)); @@ -84,7 +72,9 @@ RenderBuffer::RenderBuffer(const Vec2F& size, tp::uint1 samples) : glBindFramebuffer(GL_FRAMEBUFFER, 0); } -uint4 RenderBuffer::buffId() const { return mFrameBufferID; } +uint4 RenderBuffer::buffId() const { + return mFrameBufferID; +} void RenderBuffer::beginDraw() { AssertGL(glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID)); @@ -92,7 +82,7 @@ void RenderBuffer::beginDraw() { } void RenderBuffer::setViewport(const RectF& viewport) { - AssertGL(glViewport((GLsizei) viewport.x, (GLsizei) viewport.y, (GLsizei) viewport.z, (GLsizei) viewport.w)); + AssertGL(glViewport((GLsizei)viewport.x, (GLsizei)viewport.y, (GLsizei)viewport.z, (GLsizei)viewport.w)); } void RenderBuffer::clear() { @@ -112,6 +102,9 @@ RenderBuffer::~RenderBuffer() { glDeleteRenderbuffers(1, &mDepthBufferID); } -uint4 RenderBuffer::texId() const { return mTextureId; } +uint4 RenderBuffer::texId() const { + return mTextureId; +} const Vec2F& RenderBuffer::getSize() const { return mSize; } + diff --git a/RasterRender/private/Shader.cpp b/Sketch3D/private/Shader.cpp similarity index 100% rename from RasterRender/private/Shader.cpp rename to Sketch3D/private/Shader.cpp diff --git a/RasterRender/private/Texture.cpp b/Sketch3D/private/Texture.cpp similarity index 100% rename from RasterRender/private/Texture.cpp rename to Sketch3D/private/Texture.cpp diff --git a/RasterRender/public/FrameBuffer.hpp b/Sketch3D/public/FrameBuffer.hpp similarity index 100% rename from RasterRender/public/FrameBuffer.hpp rename to Sketch3D/public/FrameBuffer.hpp diff --git a/RasterRender/public/GraphicsApi.hpp b/Sketch3D/public/GraphicsApi.hpp similarity index 100% rename from RasterRender/public/GraphicsApi.hpp rename to Sketch3D/public/GraphicsApi.hpp diff --git a/RasterRender/public/Shader.hpp b/Sketch3D/public/Shader.hpp similarity index 99% rename from RasterRender/public/Shader.hpp rename to Sketch3D/public/Shader.hpp index 253cdba..8442e4d 100644 --- a/RasterRender/public/Shader.hpp +++ b/Sketch3D/public/Shader.hpp @@ -13,15 +13,14 @@ namespace tp { void geom_bind_source(const char* geom_src); void compile(); - + void bind(); uint4 getu(const char* uid); void unbind(); - void load(const char* vert, const char* geom, const char* frag, bool paths); - private: bool compile_shader(const char* ShaderCode, uint4 ShaderID); + void load(const char* vert, const char* geom, const char* frag, bool paths); private: uint4 programm; diff --git a/RasterRender/public/Texture.hpp b/Sketch3D/public/Texture.hpp similarity index 100% rename from RasterRender/public/Texture.hpp rename to Sketch3D/public/Texture.hpp diff --git a/Widgets/CMakeLists.txt b/Widgets/CMakeLists.txt index dc1b591..5047dae 100644 --- a/Widgets/CMakeLists.txt +++ b/Widgets/CMakeLists.txt @@ -10,12 +10,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Math Graphics Imgui) ### -------------------------- Applications -------------------------- ### -add_executable(SimpleGui examples/SimpleGUI.cpp) -target_link_libraries(SimpleGui ${PROJECT_NAME} ${GLEW_LIB}) -target_include_directories(SimpleGui PUBLIC ../Externals/glfw/include ${GLEW_INCLUDE_DIR}) - -add_executable(ChatGui examples/ChatGUI.cpp) -target_link_libraries(ChatGui ${PROJECT_NAME} ${GLEW_LIB}) -target_include_directories(ChatGui PUBLIC ../Externals/glfw/include ${GLEW_INCLUDE_DIR}) - +add_executable(ExampleGui examples/Entry.cpp) +target_link_libraries(ExampleGui ${PROJECT_NAME} ${GLEW_LIB}) +target_include_directories(ExampleGui PUBLIC ../Externals/glfw/include ${GLEW_INCLUDE_DIR}) file(COPY "examples/Font.ttf" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") \ No newline at end of file diff --git a/Widgets/examples/ChatGUI.cpp b/Widgets/examples/ChatGUI.cpp deleted file mode 100644 index 580b979..0000000 --- a/Widgets/examples/ChatGUI.cpp +++ /dev/null @@ -1,32 +0,0 @@ - -#include "ChatGUI.hpp" - -#include "GraphicApplication.hpp" - -using namespace tp; - -class ExampleGUI : public Application { -public: - ExampleGUI() { mGui.setupConfig(mWidgetManager); } - - void processFrame(EventHandler* eventHandler) override { - auto rec = RectF({ 0, 0 }, mWindow->getSize()); - - mGui.updateConfigCache(mWidgetManager); - - mGui.proc(*eventHandler, rec, rec); - } - - void drawFrame(Canvas* canvas) override { mGui.draw(*canvas); } - -private: - WidgetManager mWidgetManager; - ComplexWidget mGui; -}; - -int main() { - { - ExampleGUI gui; - gui.run(); - } -} diff --git a/Widgets/examples/Entry.cpp b/Widgets/examples/Entry.cpp new file mode 100644 index 0000000..c7fc1d1 --- /dev/null +++ b/Widgets/examples/Entry.cpp @@ -0,0 +1,33 @@ + +#include "ExampleGUI.hpp" + +#include "GraphicApplication.hpp" + +using namespace tp; + +class ExampleGUI : public Application { +public: + ExampleGUI() = default; + + void processFrame(EventHandler* eventHandler) override { + auto rec = RectF( { 0, 0 }, mWindow->getSize() ); + mGui.proc(*eventHandler, rec, rec); + } + + void drawFrame(Canvas* canvas) override { + mGui.draw(*canvas); + } + +private: + ComplexWidget mGui; +}; + +int main() { + GlobalGUIConfig mConfig; + gGlobalGUIConfig = &mConfig; + + { + ExampleGUI gui; + gui.run(); + } +} diff --git a/Widgets/examples/ChatGUI.hpp b/Widgets/examples/ExampleGUI.hpp similarity index 51% rename from Widgets/examples/ChatGUI.hpp rename to Widgets/examples/ExampleGUI.hpp index 805cd4c..474151e 100644 --- a/Widgets/examples/ChatGUI.hpp +++ b/Widgets/examples/ExampleGUI.hpp @@ -7,7 +7,15 @@ namespace tp { template class UserWidget : public Widget { public: - UserWidget() { this->mId = "UserWidget"; } + UserWidget() { + this->createConfig("User"); + this->addColor("Base", "Base"); + this->addValue("Size", "FontSize"); + this->addValue("Padding", "Padding"); + this->addColor("ColUser", "Front"); + this->addColor("Accent", "Accent"); + this->addValue("Rounding", "Rounding"); + } void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { this->mArea = aArea; @@ -20,48 +28,34 @@ namespace tp { void draw(Canvas& canvas) override { if (!this->mVisible) return; - if (mIsHover) canvas.rect(this->mArea, mAccentColor, mRounding); - else canvas.rect(this->mArea, mBaseColor, mRounding); + if (mIsHover) canvas.rect(this->mArea, this->getColor("Accent"), this->getValue("Rounding")); + else canvas.rect(this->mArea, this->getColor("Base"), this->getValue("Rounding")); - canvas.text(mUser.c_str(), this->mArea, mFontSize, Canvas::CC, mPadding, mUserColor); - } + const auto padding = this->getValue("Padding"); + const auto size = this->getValue("Size"); + const auto colUser = this->getColor("ColUser"); - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Base", "Base"); - wm.addReference(this->mId, "Size", "FontSize"); - wm.addReference(this->mId, "Padding", "Padding"); - wm.addReference(this->mId, "ColUser", "Front"); - wm.addReference(this->mId, "Accent", "Accent"); - wm.addReference(this->mId, "Rounding", "Rounding"); - } - - void updateConfigCache(const WidgetManager& wm) override { - mBaseColor = wm.getColor(this->mId, "Base"); - mFontSize = wm.getNumber(this->mId, "Size"); - mPadding = wm.getNumber(this->mId, "Padding"); - mUserColor = wm.getColor(this->mId, "ColUser"); - mAccentColor = wm.getColor(this->mId, "Accent"); - mRounding = wm.getNumber(this->mId, "Rounding"); + canvas.text(mUser.c_str(), this->mArea, size, Canvas::CC, padding, colUser); } public: std::string mUser = "UserName"; bool mIsHover = false; - - RGBA mBaseColor; - RGBA mUserColor; - RGBA mAccentColor; - halnf mPadding = 0; - halnf mFontSize = 0; - halnf mRounding = 0; }; template class MessageWidget : public Widget { public: - MessageWidget() { this->mId = "MessageWidget"; } + MessageWidget() { + this->createConfig("Message"); + this->addColor("Base", "Base"); + this->addValue("Size", "FontSize"); + this->addValue("SizeUser", "FontSizeDim"); + this->addValue("Padding", "Padding"); + this->addColor("ColUser", "Front"); + this->addColor("Col", "FrontDim"); + this->addValue("Rounding", "Rounding"); + } void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { this->mArea = aArea; @@ -73,7 +67,14 @@ namespace tp { void draw(Canvas& canvas) override { if (!this->mVisible) return; - if (mIsHover) canvas.rect(this->mArea, mBaseColor, mRounding); + + if (mIsHover) canvas.rect(this->mArea, this->getColor("Base"), this->getValue("Rounding")); + + const auto padding = this->getValue("Padding"); + const auto size = this->getValue("Size"); + const auto sizeUser = this->getValue("SizeUser"); + const auto col = this->getColor("Col"); + const auto colUser = this->getColor("ColUser"); auto userName = this->mArea; userName.w = 25; @@ -82,51 +83,22 @@ namespace tp { content.y = userName.y + userName.w; content.w = this->mArea.w - userName.w; - canvas.text(mContent.c_str(), content, mFontSize, Canvas::LC, mPadding, mUserColorDim); - canvas.text(mUser.c_str(), userName, mFontSizeDim, Canvas::LC, mPadding, mUserColor); - } - - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Base", "Base"); - wm.addReference(this->mId, "Size", "FontSize"); - wm.addReference(this->mId, "SizeUser", "FontSizeDim"); - wm.addReference(this->mId, "Padding", "Padding"); - wm.addReference(this->mId, "UserColor", "Front"); - wm.addReference(this->mId, "UserColorDim", "FrontDim"); - wm.addReference(this->mId, "Rounding", "Rounding"); - } - - void updateConfigCache(const WidgetManager& wm) override { - mBaseColor = wm.getColor(this->mId, "Base"); - mFontSize = wm.getNumber(this->mId, "Size"); - mFontSizeDim = wm.getNumber(this->mId, "SizeUser"); - mPadding = wm.getNumber(this->mId, "Padding"); - mUserColor = wm.getColor(this->mId, "UserColor"); - mUserColorDim = wm.getColor(this->mId, "UserColorDim"); - mRounding = wm.getNumber(this->mId, "Rounding"); + canvas.text(mContent.c_str(), content, size, Canvas::LC, padding, col); + canvas.text(mUser.c_str(), userName, sizeUser, Canvas::LC, padding, colUser); } public: std::string mContent = "Message Content"; std::string mUser = "UserName"; bool mIsHover = false; - - RGBA mBaseColor; - RGBA mUserColor; - RGBA mUserColorDim; - halnf mPadding = 0; - halnf mFontSize = 0; - halnf mFontSizeDim = 0; - halnf mRounding = 0; }; template class LoginWidget : public Widget { public: explicit LoginWidget() { - this->mId = "Login"; - + this->createConfig("Login"); + this->addColor("Back", "Background"); mPass.mId = "pass"; mUser.mId = "user"; mButton.mLabel.mLabel = "Login"; @@ -148,45 +120,26 @@ namespace tp { } void draw(Canvas& canvas) override { - canvas.rect(this->mArea, mBGColor); + canvas.rect(this->mArea, this->getColor("Back")); mButton.draw(canvas); mUser.draw(canvas); mPass.draw(canvas); } - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Back", "Base"); - - mUser.setupConfig(wm); - mPass.setupConfig(wm); - mButton.setupConfig(wm); - } - - void updateConfigCache(const WidgetManager& wm) override { - mBGColor = wm.getColor(this->mId, "Back"); - - mUser.updateConfigCache(wm); - mPass.updateConfigCache(wm); - mButton.updateConfigCache(wm); - } - public: TextInputWidget mUser; TextInputWidget mPass; ButtonWidget mButton; bool mLogged = false; - - RGBA mBGColor; }; template class ActiveChatWidget : public Widget { public: ActiveChatWidget() { - this->mId = "ActiveWidget"; - + this->createConfig("ActiveChat"); + this->addColor("Back", "Background"); + this->addValue("Padding", "Padding"); mSend.mLabel.mLabel = "Send"; mMessage.mId = "Message"; } @@ -199,16 +152,16 @@ namespace tp { auto input = this->mArea; input.y = history.w + 10; - input.w = 40 - mPadding; - input.x += mPadding; - input.z -= mPadding; + input.w = 40 - this->getValue("Padding"); + input.x += this->getValue("Padding"); + input.z -= this->getValue("Padding"); auto inputMessage = input; inputMessage.z -= 100; auto inputSend = input; - inputSend.x = inputMessage.x + inputMessage.z + mPadding; - inputSend.z = 100 - mPadding * 2; + inputSend.x = inputMessage.x + inputMessage.z + this->getValue("Padding"); + inputSend.z = 100 - this->getValue("Padding") * 2; mSend.proc(events, this->mArea, inputSend); mMessage.proc(events, this->mArea, inputMessage); @@ -221,53 +174,26 @@ namespace tp { } void draw(Canvas& canvas) override { - canvas.rect(this->mArea, mBGColor); + canvas.rect(this->mArea, this->getColor("Back")); mHistoryView.draw(canvas); mMessage.draw(canvas); mSend.draw(canvas); } - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - - wm.addReference(this->mId, "Back", "Background"); - wm.addReference(this->mId, "Padding", "Padding"); - - mHistoryView.setupConfig(wm); - mMessage.setupConfig(wm); - mSend.setupConfig(wm); - - MessageWidget().setupConfig(wm); - } - - void updateConfigCache(const WidgetManager& wm) override { - mBGColor = wm.getColor(this->mId, "Back"); - mPadding = wm.getNumber(this->mId, "Padding"); - - mHistoryView.updateConfigCache(wm); - mMessage.updateConfigCache(wm); - mSend.updateConfigCache(wm); - - for (auto message : mMessages) { - message->updateConfigCache(wm); - } - } - public: Buffer> mMessages; ScrollableWindow mHistoryView; TextInputWidget mMessage; ButtonWidget mSend; - - RGBA mBGColor; - halnf mPadding = 0; }; template class ChattingWidget : public Widget { public: ChattingWidget() { - this->mId = "Chatting"; + this->createConfig("Chatting"); + this->addColor("Back", "Background"); + this->addValue("Padding", "Padding"); // todo : fetch code mUsers.append(UserWidget()); @@ -299,54 +225,34 @@ namespace tp { this->mArea = aArea; mSplitView.proc(events, aArea, aArea); + + const auto padding = this->getValue("Padding"); mSideView.proc(events, this->mArea, mSplitView.getSecond()); + mActive.proc(events, aArea, mSplitView.getFirst()); } void draw(Canvas& canvas) override { - canvas.rect(this->mArea, mBGColor); + canvas.rect(this->mArea, this->getColor("Back")); mSplitView.draw(canvas); mSideView.draw(canvas); mActive.draw(canvas); } - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - - wm.addReference(this->mId, "Back", "Background"); - - mSideView.setupConfig(wm); - mActive.setupConfig(wm); - mSplitView.setupConfig(wm); - - UserWidget().setupConfig(wm); - } - - void updateConfigCache(const WidgetManager& wm) override { - mBGColor = wm.getColor(this->mId, "Back"); - - mSideView.updateConfigCache(wm); - mActive.updateConfigCache(wm); - mSplitView.updateConfigCache(wm); - - for (auto user : mUsers) { - user->updateConfigCache(wm); - } - } - public: Buffer> mUsers; ScrollableWindow mSideView; ActiveChatWidget mActive; SplitView mSplitView; - - RGBA mBGColor; }; template class ComplexWidget : public Widget { public: - ComplexWidget() { this->mId = "Chat"; } + ComplexWidget() { + this->createConfig("chat"); + this->addColor("Back", "Background"); + } void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { this->mArea = aArea; @@ -362,32 +268,14 @@ namespace tp { } void draw(Canvas& canvas) override { - canvas.rect(this->mArea, mBGColor); + canvas.rect(this->mArea, this->getColor("Back")); if (mLogged) mChatting.draw(canvas); else mLogin.draw(canvas); } - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - - wm.addReference(this->mId, "Back", "Background"); - - mLogin.setupConfig(wm); - mChatting.setupConfig(wm); - } - - void updateConfigCache(const WidgetManager& wm) override { - mBGColor = wm.getColor(this->mId, "Back"); - - mLogin.updateConfigCache(wm); - mChatting.updateConfigCache(wm); - } - private: bool mLogged = false; LoginWidget mLogin; ChattingWidget mChatting; - - RGBA mBGColor; }; } \ No newline at end of file diff --git a/Widgets/examples/SimpleGUI.cpp b/Widgets/examples/SimpleGUI.cpp deleted file mode 100644 index fd4ec5e..0000000 --- a/Widgets/examples/SimpleGUI.cpp +++ /dev/null @@ -1,50 +0,0 @@ - -#include "ChatGUI.hpp" - -#include "GraphicApplication.hpp" - -using namespace tp; - -class SimpleGUI : public Application { -public: - SimpleGUI() { - mGui.setupConfig(mWidgetManager); - - mButton.setupConfig(mWidgetManager); - mSlider.setupConfig(mWidgetManager); - mLabel.setupConfig(mWidgetManager); - - mGui.mContents.append(&mButton); - mGui.mContents.append(&mSlider); - mGui.mContents.append(&mLabel); - } - - void processFrame(EventHandler* eventHandler) override { - - mGui.updateConfigCache(mWidgetManager); - - mSlider.updateConfigCache(mWidgetManager); - mLabel.updateConfigCache(mWidgetManager); - mButton.updateConfigCache(mWidgetManager); - - const auto rec = RectF({ 0, 0 }, mWindow->getSize()); - mGui.proc(*eventHandler, rec, rec); - } - - void drawFrame(Canvas* canvas) override { mGui.draw(*canvas); } - -private: - WidgetManager mWidgetManager; - ScrollableWindow mGui; - - ButtonWidget mButton; - LabelWidget mLabel; - SliderWidget mSlider; -}; - -int main() { - { - SimpleGUI gui; - gui.run(); - } -} diff --git a/Widgets/private/WidgetConfig.cpp b/Widgets/private/WidgetConfig.cpp index 5a2cf39..8984b72 100644 --- a/Widgets/private/WidgetConfig.cpp +++ b/Widgets/private/WidgetConfig.cpp @@ -3,4 +3,6 @@ #include "Graphics.hpp" -namespace tp {} \ No newline at end of file +namespace tp { + GlobalGUIConfig* gGlobalGUIConfig = nullptr; +} \ No newline at end of file diff --git a/Widgets/public/ButtonWidget.hpp b/Widgets/public/ButtonWidget.hpp index 4eb748c..8df41bc 100644 --- a/Widgets/public/ButtonWidget.hpp +++ b/Widgets/public/ButtonWidget.hpp @@ -9,13 +9,22 @@ namespace tp { public: ButtonWidget() { this->mArea = { 0, 0, 100, 100 }; - this->mId = "Button"; + this->createConfig("Button"); + this->addColor("Pressed", "Action"); + this->addColor("Hovered", "Interaction"); + this->addColor("Default", "Accent"); + this->addValue("Rounding", "Rounding"); } ButtonWidget(const std::string& label, const tp::RectF& aArea) { - this->mId = "Button"; this->mArea = aArea; this->mLabel.mLabel = label; + + this->createConfig("Button"); + this->addColor("Pressed", "Action"); + this->addColor("Hovered", "Interaction"); + this->addColor("Default", "Accent"); + this->addValue("Rounding", "Rounding"); } void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { @@ -51,45 +60,19 @@ namespace tp { if (!this->mVisible) return; if (mIsPressed) { - canvas.rect(this->mArea, pressedColor, rounding); + canvas.rect(this->mArea, this->getColor("Pressed"), this->getValue("Rounding")); } else if (mIsHover) { - canvas.rect(this->mArea, hoveredColor, rounding); + canvas.rect(this->mArea, this->getColor("Hovered"), this->getValue("Rounding")); } else { - canvas.rect(this->mArea, accentColor, rounding); + canvas.rect(this->mArea, this->getColor("Default"), this->getValue("Rounding")); } mLabel.draw(canvas); } - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Pressed", "Action"); - wm.addReference(this->mId, "Hovered", "Interaction"); - wm.addReference(this->mId, "Default", "Accent"); - wm.addReference(this->mId, "Rounding", "Rounding"); - - mLabel.setupConfig(wm); - } - - void updateConfigCache(const WidgetManager& wm) override { - pressedColor = wm.getColor(this->mId, "Pressed"); - hoveredColor = wm.getColor(this->mId, "Hovered"); - accentColor = wm.getColor(this->mId, "Default"); - rounding = wm.getNumber(this->mId, "Rounding"); - - mLabel.updateConfigCache(wm); - } - public: LabelWidget mLabel; - bool mIsHover = false; bool mIsPressed = false; bool mIsReleased = false; - - RGBA pressedColor; - RGBA hoveredColor; - RGBA accentColor; - halnf rounding = 0; }; } \ No newline at end of file diff --git a/Widgets/public/LabelWidget.hpp b/Widgets/public/LabelWidget.hpp index 5fba4d1..945436e 100644 --- a/Widgets/public/LabelWidget.hpp +++ b/Widgets/public/LabelWidget.hpp @@ -6,9 +6,14 @@ namespace tp { template class LabelWidget : public Widget { public: - LabelWidget() { this->mId = "Label"; } + LabelWidget() { + this->createConfig("Label"); + this->addValue("Size", "FontSize"); + this->addValue("Padding", "Padding"); + this->addColor("Default", "Front"); + } - void proc(const Events&, const tp::RectF& areaParent, const tp::RectF& aArea) override { + void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { this->mArea = aArea; this->mVisible = areaParent.isOverlap(aArea); if (!this->mVisible) return; @@ -16,28 +21,18 @@ namespace tp { void draw(Canvas& canvas) override { if (!this->mVisible) return; - canvas.text(mLabel.c_str(), this->mArea, fontSize, Canvas::CC, padding, fontColor); - } - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Size", "FontSize"); - wm.addReference(this->mId, "Padding", "Padding"); - wm.addReference(this->mId, "Default", "Front"); - } - - void updateConfigCache(const WidgetManager& wm) override { - fontSize = wm.getNumber(this->mId, "Size"); - padding = wm.getNumber(this->mId, "Padding"); - fontColor = wm.getColor(this->mId, "Default"); + canvas.text( + mLabel.c_str(), + this->mArea, + this->getValue("Size"), + Canvas::CC, + this->getValue("Padding"), + this->getColor("Default") + ); } public: std::string mLabel = "Label"; - - halnf fontSize = 10; - halnf padding = 0; - RGBA fontColor = { 1, 1, 1, 1 }; }; } \ No newline at end of file diff --git a/Widgets/public/ScrollableWidget.hpp b/Widgets/public/ScrollableWidget.hpp index da93640..d955825 100644 --- a/Widgets/public/ScrollableWidget.hpp +++ b/Widgets/public/ScrollableWidget.hpp @@ -8,7 +8,17 @@ namespace tp { template class ScrollBarWidget : public Widget { public: - ScrollBarWidget() { this->mId = "ScrollBar"; } + ScrollBarWidget() { + this->createConfig("ScrollBar"); + this->addColor("Default", "Base"); + this->addColor("Handle", "Accent"); + this->addColor("Hovered", "Interaction"); + this->addColor("Scrolling", "Action"); + this->addValue("Padding", "Padding"); + this->addValue("HandleSize", 20.f); + this->addValue("MinSize", 20.f); + this->addValue("Rounding", "Rounding"); + } // takes whole area void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { @@ -69,22 +79,28 @@ namespace tp { if (mSizeFraction > 1.f) return; // if (!areaParent.isOverlap(getHandle())) return; - tp::RGBA col = mHandleColor; + tp::RGBA col = this->getColor("Handle"); if (mIsScrolling) { - col = mScrollingColor; + col = this->getColor("Scrolling"); } else if (mHovered) { - col = mHoveredColor; + col = this->getColor("Hovered"); } - canvas.rect(area, mDefaultColor, mRounding); + canvas.rect(area, this->getColor("Default"), this->getValue("Rounding")); - canvas.rect(getHandleHandle(), col, mRounding); + canvas.rect(getHandleHandle(), col, this->getValue("Rounding")); } RectF getHandleHandle() const { + auto minSize = this->getValue("MinSize"); + if (mIsScrolling) { + minSize = this->getValue("MinSize") * 2; + } else if (mHovered) { + minSize = this->getValue("MinSize") * 2; + } auto area = getHandle(); - auto sliderSize = tp::clamp(area.w * mSizeFraction, mMinSize * 2, area.w); + auto sliderSize = tp::clamp(area.w * mSizeFraction, minSize, area.w); auto diffSize = sliderSize - area.w * mSizeFraction; return { area.x, area.y + (area.w - diffSize) * mPositionFraction, area.z, sliderSize }; } @@ -93,39 +109,14 @@ namespace tp { if (mSizeFraction > 1.f) { return this->mArea; } - return { this->mArea.x, this->mArea.y, this->mArea.z - mHandleSize, this->mArea.w }; + return { this->mArea.x, this->mArea.y, this->mArea.z - this->getValue("HandleSize"), this->mArea.w }; } RectF getHandle() const { - return { this->mArea.x + this->mArea.z - mHandleSize + mPadding, - this->mArea.y + mPadding, - mHandleSize - mPadding * 2, - this->mArea.w - mPadding * 2 }; - } - - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - - wm.addReference(this->mId, "Default", "Base"); - wm.addReference(this->mId, "Handle", "Accent"); - wm.addReference(this->mId, "Hovered", "Interaction"); - wm.addReference(this->mId, "Scrolling", "Action"); - wm.addReference(this->mId, "Padding", "Padding"); - wm.addNumber(this->mId, "HandleSize", 20.f); - wm.addNumber(this->mId, "MinSize", 20.f); - wm.addReference(this->mId, "Rounding", "Rounding"); - } - - void updateConfigCache(const WidgetManager& wm) override { - mDefaultColor = wm.getColor(this->mId, "Default"); - mHandleColor = wm.getColor(this->mId, "Handle"); - mHoveredColor = wm.getColor(this->mId, "Hovered"); - mScrollingColor = wm.getColor(this->mId, "Scrolling"); - mPadding = wm.getNumber(this->mId, "Padding"); - mHandleSize = wm.getNumber(this->mId, "HandleSize"); - mMinSize = wm.getNumber(this->mId, "MinSize"); - mRounding = wm.getNumber(this->mId, "Rounding"); + return { this->mArea.x + this->mArea.z - this->getValue("HandleSize") + this->getValue("Padding"), + this->mArea.y + this->getValue("Padding"), + this->getValue("HandleSize") - this->getValue("Padding") * 2, + this->mArea.w - this->getValue("Padding") * 2 }; } public: @@ -135,21 +126,16 @@ namespace tp { halnf mSizeFraction = 1.f; halnf mPositionFraction = 0.f; bool mHovered = false; - - RGBA mDefaultColor; - RGBA mHandleColor; - RGBA mHoveredColor; - RGBA mScrollingColor; - halnf mPadding = 0; - halnf mHandleSize = 10; - halnf mMinSize = 10; - halnf mRounding = 10; }; template class ScrollableWindow : public Widget { public: - ScrollableWindow() { this->mId = "ScrollableWindow"; } + ScrollableWindow() { + this->createConfig("ScrollableWindow"); + this->addColor("Default", "Base"); + this->addValue("Padding", "Padding"); + } ~ScrollableWindow() = default; @@ -162,7 +148,7 @@ namespace tp { updateContents(); updateContentSize(); - const auto padding = mPadding; + const auto padding = this->getValue("Padding"); mScroller.mSizeFraction = this->mArea.w / mContentSize; mScroller.proc(events, this->mArea, this->mArea); @@ -194,37 +180,16 @@ namespace tp { canvas.popClamp(); } - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - - wm.addReference(this->mId, "Default", "Base"); - wm.addReference(this->mId, "Padding", "Padding"); - wm.addReference(this->mId, "Rounding", "Rounding"); - - mScroller.setupConfig(wm); - } - - void updateConfigCache(const WidgetManager& wm) override { - mDefaultColor = wm.getColor(this->mId, "Default"); - mPadding = wm.getNumber(this->mId, "Padding"); - - mScroller.updateConfigCache(wm); - - for (auto item : mContents) { - item->updateConfigCache(wm); - } - } - private: void updateContents() { if (mContents.size()) { - const halnf offset = mContents.first()->mArea.y + mPadding; + const auto padding = this->getValue("Padding"); + const halnf offset = mContents.first()->mArea.y + padding; halnf start = 0; for (auto widget : mContents) { widget->mArea.y = start; - start += widget->mArea.w + mPadding; + start += widget->mArea.w + padding; } for (auto widget : mContents) { @@ -236,15 +201,17 @@ namespace tp { void updateContentSize() { mContentSize = 0; if (mContents.size()) { + const auto padding = this->getValue("Padding"); mContentSize = mContents.last()->mArea.y - mContents.first()->mArea.y; mContentSize += mContents.last()->mArea.w; - mContentSize += 2 * mPadding; + mContentSize += 2 * padding; } } void setOffset(const halnf offset) { if (!mContents.size()) return; - auto newOffset = offset - mContents.first()->mArea.y + mPadding; + const auto padding = this->getValue("Padding"); + auto newOffset = offset - mContents.first()->mArea.y + padding; for (auto widget : mContents) { widget->mArea.y += newOffset; } @@ -252,11 +219,7 @@ namespace tp { public: halnf mContentSize = 0; - Buffer*> mContents; ScrollBarWidget mScroller; - - RGBA mDefaultColor; - halnf mPadding = 0; }; } \ No newline at end of file diff --git a/Widgets/public/SliderWidget.hpp b/Widgets/public/SliderWidget.hpp index 70aa657..098d44e 100644 --- a/Widgets/public/SliderWidget.hpp +++ b/Widgets/public/SliderWidget.hpp @@ -7,7 +7,17 @@ namespace tp { template class SliderWidget : public Widget { public: - SliderWidget() { this->mId = "SliderWidget"; } + SliderWidget() { + this->createConfig("SliderWidget"); + this->addColor("Default", "Base"); + this->addColor("Handle", "Accent"); + this->addColor("Hovered", "Interaction"); + this->addColor("Scrolling", "Action"); + this->addValue("Padding", "Padding"); + this->addValue("HandleSize", 20.f); + this->addValue("MinSize", 20.f); + this->addValue("Rounding", "Rounding"); + } void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { this->mArea = aArea; @@ -21,6 +31,7 @@ namespace tp { } if (mIsSliding) { + const auto handleSize = this->getValue("HandleSize"); mFactor = (events.getPointer().x - this->mArea.x - handleSize / 2.f) / (this->mArea.z - handleSize); } @@ -29,47 +40,26 @@ namespace tp { void draw(Canvas& canvas) override { if (!this->mVisible) return; - canvas.rect(this->mArea, defaultColor, rounding); - canvas.rect(getHandle(), handleColor, rounding); + canvas.rect(this->mArea, this->getColor("Default"), this->getValue("Rounding")); + canvas.rect(getHandle(), this->getColor("Handle"), this->getValue("Rounding")); } RectF getHandle() const { - const auto halfHandle = handleSize / 2.f; - const auto left = this->mArea.x + (this->mArea.z - handleSize) * mFactor; - return { left, this->mArea.y, handleSize, this->mArea.w }; - } - - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Default", "Base"); - wm.addReference(this->mId, "Handle", "Accent"); - wm.addNumber(this->mId, "HandleSize", 20.f); - wm.addReference(this->mId, "Rounding", "Rounding"); - } - - void updateConfigCache(const WidgetManager& wm) override { - defaultColor = wm.getColor(this->mId, "Default"); - handleColor = wm.getColor(this->mId, "Handle"); - handleSize = wm.getNumber(this->mId, "HandleSize"); - rounding = wm.getNumber(this->mId, "Rounding"); + const auto handle = this->getValue("HandleSize"); + const auto halfHandle = handle / 2.f; + const auto left = this->mArea.x + (this->mArea.z - handle) * mFactor; + return { left, this->mArea.y, handle, this->mArea.w }; } public: halnf mFactor = 0.f; bool mIsSliding = false; - - RGBA defaultColor; - RGBA handleColor; - halnf handleSize = 0; - halnf rounding = 0; }; template class NamedSliderWidget : public Widget { public: - explicit NamedSliderWidget(const char* name = "Value") { - this->mId = "NamedSliderWidget"; + NamedSliderWidget(const char* name = "Value") { mLabel.mLabel = name; this->mArea = { 0, 0, 100, 30 }; } @@ -99,7 +89,6 @@ namespace tp { mLabel.draw(canvas); } - public: public: SliderWidget mSlider; LabelWidget mLabel; diff --git a/Widgets/public/SplitViewWidget.hpp b/Widgets/public/SplitViewWidget.hpp index 6731efa..2ee1ad8 100644 --- a/Widgets/public/SplitViewWidget.hpp +++ b/Widgets/public/SplitViewWidget.hpp @@ -6,7 +6,14 @@ namespace tp { template class SplitView : public Widget { public: - SplitView() { this->mId = "SplitView"; } + SplitView() { + this->createConfig("SplitView"); + this->addColor("Handle", "Accent"); + this->addColor("Hovered", "Interaction"); + this->addColor("Resizing", "Action"); + this->addValue("Min", 200.f); + this->addValue("HandleSize", 7.f); + } void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { this->mArea = aArea; @@ -31,9 +38,9 @@ namespace tp { mFactor += diff / this->mArea.z; } - mFactor = tp::clamp(mFactor, mMinSize / this->mArea.z, 1 - mMinSize / this->mArea.z); + mFactor = tp::clamp(mFactor, this->getValue("Min") / this->mArea.z, 1 - this->getValue("Min") / this->mArea.z); - if (mMinSize * 2.f > this->mArea.z) { + if (this->getValue("Min") * 2.f > this->mArea.z) { mFactor = 0.5f; } } @@ -42,52 +49,34 @@ namespace tp { void draw(Canvas& canvas) override { if (!this->mVisible) return; - if (mResizeInProcess) canvas.rect(getHandle(), mResizingColor); - else if (mIsHover) canvas.rect(getHandle(), mHoveredColor); - else canvas.rect(getHandle(), mHandleColor); + if (mResizeInProcess) canvas.rect(getHandle(), this->getColor("Resizing")); + else if (mIsHover) canvas.rect(getHandle(), this->getColor("Hovered")); + else canvas.rect(getHandle(), this->getColor("Handle")); } RectF getFirst() const { - return { this->mArea.x, this->mArea.y, mFactor * this->mArea.z - mHandleSize / 2.f, this->mArea.w }; + return { + this->mArea.x, this->mArea.y, mFactor * this->mArea.z - this->getValue("HandleSize") / 2.f, this->mArea.w + }; } RectF getSecond() const { - return { this->mArea.x + mFactor * this->mArea.z + mHandleSize / 2.f, + return { this->mArea.x + mFactor * this->mArea.z + this->getValue("HandleSize") / 2.f, this->mArea.y, - (1.f - mFactor) * this->mArea.z - mHandleSize / 2.f, + (1.f - mFactor) * this->mArea.z - this->getValue("HandleSize") / 2.f, this->mArea.w }; } RectF getHandle() const { - return { this->mArea.x + mFactor * this->mArea.z - mHandleSize / 2.f, this->mArea.y, mHandleSize, this->mArea.w }; - } - - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Handle", "Accent"); - wm.addReference(this->mId, "Hovered", "Interaction"); - wm.addReference(this->mId, "Resizing", "Action"); - wm.addNumber(this->mId, "Min", 200.f); - wm.addNumber(this->mId, "HandleSize", 7.f); - } - - void updateConfigCache(const WidgetManager& wm) override { - mHandleColor = wm.getColor(this->mId, "Handle"); - mHoveredColor = wm.getColor(this->mId, "Hovered"); - mResizingColor = wm.getColor(this->mId, "Resizing"); - mMinSize = wm.getNumber(this->mId, "Min"); - mHandleSize = wm.getNumber(this->mId, "HandleSize"); + return { this->mArea.x + mFactor * this->mArea.z - this->getValue("HandleSize") / 2.f, + this->mArea.y, + this->getValue("HandleSize"), + this->mArea.w }; } public: halnf mFactor = 0.7f; bool mResizeInProcess = false; bool mIsHover = false; - - RGBA mHandleColor; - RGBA mHoveredColor; - RGBA mResizingColor; - halnf mMinSize = 0; - halnf mHandleSize = 0; }; } \ No newline at end of file diff --git a/Widgets/public/TextInputWidget.hpp b/Widgets/public/TextInputWidget.hpp index 2dfd40b..11089cb 100644 --- a/Widgets/public/TextInputWidget.hpp +++ b/Widgets/public/TextInputWidget.hpp @@ -9,7 +9,14 @@ namespace tp { template class TextInputWidget : public Widget { public: - TextInputWidget() { this->mId = "TextInput"; } + TextInputWidget() { + this->createConfig("TextInput"); + this->addColor("Accent", "Accent"); + this->addColor("Base", "Base"); + this->addValue("Rounding", "Rounding"); + this->addColor("Hovered", "Interaction"); + this->addValue("Padding", "Padding"); + } void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { this->mArea = aArea; @@ -22,8 +29,8 @@ namespace tp { nChanged = false; - const auto col = mAccentColor; - const auto colSel = mHoveredColor; + const auto col = this->getColor("Accent"); + const auto colSel = this->getColor("Hovered"); ImGui::GetStyle().Colors[ImGuiCol_FrameBg] = { col.r, col.g, col.b, col.a }; ImGui::GetStyle().Colors[ImGuiCol_TextSelectedBg] = { colSel.r, colSel.g, colSel.b, colSel.a }; @@ -31,8 +38,8 @@ namespace tp { ImGui::SetNextWindowPos({ this->mArea.x, this->mArea.y }); ImGui::SetNextWindowSize({ this->mArea.z, this->mArea.w }); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0, 0 }); - ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, mRounding * 1.5f); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { mPadding, mPadding }); + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, this->getValue("Rounding") * 1.5f); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { this->getValue("Padding"), this->getValue("Padding") }); // ImGui::PushID((int) alni(this)); ImGui::Begin( @@ -58,36 +65,11 @@ namespace tp { ImGui::PopStyleVar(3); } - public: - void setupConfig(WidgetManager& wm) { - if (!wm.createWidgetConfig(this->mId)) return; - wm.addReference(this->mId, "Accent", "Accent"); - wm.addReference(this->mId, "Base", "Base"); - wm.addReference(this->mId, "Rounding", "Rounding"); - wm.addReference(this->mId, "Hovered", "Accent"); - wm.addReference(this->mId, "Padding", "Padding"); - } - - void updateConfigCache(const WidgetManager& wm) override { - mAccentColor = wm.getColor(this->mId, "Accent"); - mBaseColor = wm.getColor(this->mId, "Base"); - mHoveredColor = wm.getColor(this->mId, "Hovered"); - mRounding = wm.getNumber(this->mId, "Rounding"); - mPadding = wm.getNumber(this->mId, "Padding"); - } - - public: enum { mMaxBufferSize = 512 }; char mBuff[mMaxBufferSize] = ""; bool nChanged = false; std::string mValue; std::string mId = "id"; bool mMultiline = false; - - RGBA mAccentColor; - RGBA mHoveredColor; - RGBA mBaseColor; - halnf mRounding = 0; - halnf mPadding = 0; }; } \ No newline at end of file diff --git a/Widgets/public/WidgetBase.hpp b/Widgets/public/WidgetBase.hpp index 8eae05f..3fcd190 100644 --- a/Widgets/public/WidgetBase.hpp +++ b/Widgets/public/WidgetBase.hpp @@ -1,13 +1,46 @@ #pragma once -#include "WidgetManager.hpp" +#include "WidgetConfig.hpp" namespace tp { template class Widget { public: - Widget() { this->mArea = { 0, 0, 100, 100 }; } + Widget() { + this->mArea = { 0, 0, 100, 100 }; + } + + [[nodiscard]] const RGBA& getColor(const std::string& name) const { + const auto& node = cfg->values.get(name); + if (node.type == WidgetConfig::Node::REF) { + return globalCfg->configs.get(node.refGroup).values.get(node.refName).color; + } + return cfg->values.get(name).color; + } + + [[nodiscard]] halnf getValue(const std::string& name) const { + const auto& node = cfg->values.get(name); + if (node.type == WidgetConfig::Node::REF) { + return globalCfg->configs.get(node.refGroup).values.get(node.refName).value; + } + return cfg->values.get(name).value; + } + + void addColor(const std::string& name, const RGBA& val) { cfg->values.put(name, WidgetConfig::Node(val)); } + void addValue(const std::string& name, halnf val) { cfg->values.put(name, WidgetConfig::Node(val)); } + + void addColor(const std::string& name, const std::string& presetName) { + cfg->values.put(name, WidgetConfig::Node(presetName)); + } + void addValue(const std::string& name, const std::string& presetName) { + cfg->values.put(name, WidgetConfig::Node(presetName)); + } + + void createConfig(const std::string& name) { + globalCfg->configs.put(name, {}); + cfg = &globalCfg->configs.get(name); + } virtual void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) { mVisible = areaParent.isOverlap(aArea); @@ -24,10 +57,9 @@ namespace tp { } } - virtual void updateConfigCache(const WidgetManager& wm) = 0; - public: - std::string mId; + GlobalGUIConfig* globalCfg = gGlobalGUIConfig; + WidgetConfig* cfg = nullptr; RectF mArea; bool mVisible = false; }; diff --git a/Widgets/public/WidgetConfig.hpp b/Widgets/public/WidgetConfig.hpp new file mode 100644 index 0000000..3455c48 --- /dev/null +++ b/Widgets/public/WidgetConfig.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include "Animations.hpp" +#include "Map.hpp" +#include "Rect.hpp" + +#include "InputCodes.hpp" + +namespace tp { + + struct WidgetConfig { + struct Node { + enum Type { NONE, VAL, COL, REF }; + + halnf value = 0.f; + RGBA color = {}; + + Type type = NONE; + + std::string refGroup; + std::string refName; + + Node() = default; + + explicit Node(halnf val) { + type = VAL; + value = val; + } + + explicit Node(const RGBA& val) { + type = COL; + color = val; + } + + explicit Node(const std::string& val) { + refGroup = "Presets"; + refName = val; + type = REF; + } + }; + + Map values; + }; + + struct GlobalGUIConfig { + GlobalGUIConfig() { + configs.put("Presets", {}); + auto& presets = configs.get("Presets"); + + presets.values.put("FontSize", WidgetConfig::Node(15.f)); + presets.values.put("FontSizeDim", WidgetConfig::Node(12.f)); + presets.values.put("Rounding", WidgetConfig::Node(5.f)); + presets.values.put("Padding", WidgetConfig::Node(5.f)); + presets.values.put("HandleSize", WidgetConfig::Node(5.f)); + + presets.values.put("Background", WidgetConfig::Node(RGBA{ 0.03f, 0.03f, 0.03f, 1.f })); + presets.values.put("Base", WidgetConfig::Node(RGBA{ 0.07f, 0.07f, 0.07f, 1.f })); + presets.values.put("Accent", WidgetConfig::Node(RGBA{ 0.13f, 0.13f, 0.13f, 1.f })); + presets.values.put("Interaction", WidgetConfig::Node(RGBA{ 0.33f, 0.33f, 0.3f, 1.f })); + presets.values.put("Action", WidgetConfig::Node(RGBA{ 0.44f, 0.44f, 0.4f, 1.f })); + presets.values.put("Front", WidgetConfig::Node(RGBA{ 1.f, 1.f, 1.f, 1.f })); + presets.values.put("FrontDim", WidgetConfig::Node(RGBA{ 0.7f, 0.7f, 0.7f, 1.f })); + } + + Map configs; + + ~GlobalGUIConfig() { + configs.removeAll(); + } + }; + + extern GlobalGUIConfig* gGlobalGUIConfig; +} \ No newline at end of file diff --git a/Widgets/public/WidgetManager.hpp b/Widgets/public/WidgetManager.hpp deleted file mode 100644 index e254a7d..0000000 --- a/Widgets/public/WidgetManager.hpp +++ /dev/null @@ -1,131 +0,0 @@ -#pragma once - -#include "Animations.hpp" -#include "Map.hpp" -#include "Rect.hpp" - -#include "InputCodes.hpp" -#include "Buffer.hpp" - -namespace tp { - - struct WidgetConfig { - - struct WidgetShortcut { - struct Condition { - std::string name; - std::string state; - }; - - std::string callbackName; - - WidgetShortcut() = default; - WidgetShortcut(const InitialierList&) {} - }; - - struct WidgetParameter { - enum Type { NONE, VAL, COL, REF }; - - halnf value = 0.f; - RGBA color = {}; - - std::string refName; - std::string refWidgetId; - - Type type = NONE; - - WidgetParameter() = default; - - explicit WidgetParameter(halnf val) { - type = VAL; - value = val; - } - - explicit WidgetParameter(const RGBA& val) { - type = COL; - color = val; - } - - explicit WidgetParameter(const std::string& widgetId, const std::string& val) { - type = REF; - refName = val; - refWidgetId = widgetId; - } - }; - - Map mParameters; - Buffer mShortcuts; - }; - - class WidgetManager { - public: - WidgetManager() { - createWidgetConfig("Default"); - - addNumber("Default", "FontSize", 15.f); - addNumber("Default", "FontSizeDim", 12.f); - addNumber("Default", "Rounding", 5.f); - addNumber("Default", "Padding", 5.f); - addNumber("Default", "HandleSize", 5.f); - - addColor("Default", "Background", RGBA{ 0.03f, 0.03f, 0.03f, 1.f }); - addColor("Default", "Base", RGBA{ 0.07f, 0.07f, 0.07f, 1.f }); - addColor("Default", "Accent", RGBA{ 0.13f, 0.13f, 0.13f, 1.f }); - addColor("Default", "Interaction", RGBA{ 0.33f, 0.33f, 0.3f, 1.f }); - addColor("Default", "Action", RGBA{ 0.44f, 0.44f, 0.4f, 1.f }); - addColor("Default", "Front", RGBA{ 1.f, 1.f, 1.f, 1.f }); - addColor("Default", "FrontDim", RGBA{ 0.7f, 0.7f, 0.7f, 1.f }); - } - - ~WidgetManager() { mConfigurations.removeAll(); } - - bool createWidgetConfig(const std::string& widgetId) { - auto idx = mConfigurations.presents(widgetId); - if (idx) return false; - mConfigurations.put(widgetId, {}); - return true; - } - - [[nodiscard]] const RGBA& getColor(const std::string& widgetId, const std::string& name) const { - const WidgetConfig& config = mConfigurations.get(widgetId); - const WidgetConfig::WidgetParameter& parameter = config.mParameters.get(name); - - if (parameter.type == WidgetConfig::WidgetParameter::REF) { - return mConfigurations.get(parameter.refWidgetId).mParameters.get(parameter.refName).color; - } else { - return parameter.color; - } - } - - [[nodiscard]] halnf getNumber(const std::string& widgetId, const std::string& name) const { - const WidgetConfig& config = mConfigurations.get(widgetId); - const WidgetConfig::WidgetParameter& parameter = config.mParameters.get(name); - - if (parameter.type == WidgetConfig::WidgetParameter::REF) { - return mConfigurations.get(parameter.refWidgetId).mParameters.get(parameter.refName).value; - } else { - return parameter.value; - } - } - - void addColor(const std::string& widgetId, const std::string& name, const RGBA& val) { - WidgetConfig& config = mConfigurations.get(widgetId); - config.mParameters.put(name, WidgetConfig::WidgetParameter(val)); - } - - void addNumber(const std::string& widgetId, const std::string& name, halnf val) { - WidgetConfig& config = mConfigurations.get(widgetId); - config.mParameters.put(name, WidgetConfig::WidgetParameter(val)); - } - - void addReference(const std::string& widgetId, const std::string& refName, const std::string& name) { - WidgetConfig& config = mConfigurations.get(widgetId); - config.mParameters.put(refName, WidgetConfig::WidgetParameter("Default", name)); - } - - private: - Map mConfigurations; - RGBA mErrorColor = { 0, 0, 0, 1 }; - halnf mErrorNumber = 0; - }; -} \ No newline at end of file