diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..038fb68 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.16) +project(myUI LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) +option(BUILD_SHARED_LIBS "Build shared libraries" OFF) + +include(FetchContent) +FetchContent_Declare(SFML + GIT_REPOSITORY https://github.com/SFML/SFML.git + GIT_TAG 2.6.x) +FetchContent_MakeAvailable(SFML) + +add_executable(myUI src/main.cpp + src/data.hpp + src/draw.hpp) +target_link_libraries(myUI PRIVATE sfml-graphics) +target_compile_features(myUI PRIVATE cxx_std_17) + +if(WIN32) + add_custom_command( + TARGET myUI + COMMENT "Copy OpenAL DLL" + PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$,x64,x86>/openal32.dll $ + VERBATIM) +endif() + +install(TARGETS myUI) diff --git a/data-files.hpp b/data-files.hpp new file mode 100644 index 0000000..02fbcef --- /dev/null +++ b/data-files.hpp @@ -0,0 +1,9 @@ +#pragma once +#include + +sf::Font betterVCR; + +void get_files(){ + betterVCR.loadFromFile("../src/BetterVCR/BetterVCR_6.1.ttf"); + // betterVCR.loadFromFile("Arial.ttf"); +} diff --git a/data.hpp b/data.hpp new file mode 100644 index 0000000..cb1179b --- /dev/null +++ b/data.hpp @@ -0,0 +1,257 @@ +#pragma once +#include +#include +#include +#include "data-files.hpp" +#include + +sf::Vector2u old_size_window; + +void old_size(sf::RenderWindow& window){ + old_size_window = window.getSize(); +} + +class object; + +void RoundedRectangle(object& obj,sf::RenderWindow& window); +void rectangle_draw(sf::RenderWindow& window, object& obj); +void text_draw(sf::RenderWindow& window, object& obj); + +class object{ +public: + std::string name; + sf::Color color, outline_col; + + sf::Vector2f position; + sf::Vector2f size; + sf::Vector2f real_size=sf::Vector2f(0, 0), real_pos=sf::Vector2f(0, 0); + std::string type = "sprite"; + sf::String text; + bool isActive, isInput, statusInput, isWisible; + sf::Vector2i stick_pos = sf::Vector2i(0,0); + sf::Vector2i origin_pos = sf::Vector2i(-1,-1); + sf::Vector2i scale_pos = sf::Vector2i(0,0); + float rotate_angle = 0; + int layer; + float radius, outline; + object* stick_object; + + void draw(sf::RenderWindow& window){ + if(type == "sprite") + rectangle_draw(window, *this); + if(type == "round") + RoundedRectangle(*this, window); + if(type == "text") + text_draw(window, *this); + + } +}; + +object* null; + +void rectangle_draw(sf::RenderWindow& window, object& obj){ + sf::RectangleShape shape; + + float psX=obj.position.x; + float psY=obj.position.y; + float spX=obj.size.x; + float spY=obj.size.y; + + if(obj.scale_pos.x == 1 || obj.scale_pos.x == -1){ + spX = obj.size.x+(float)window.getSize().x/2-(float)old_size_window.x/2; + } + if(obj.scale_pos.y == 1 || obj.scale_pos.y == -1){ + spY = obj.size.y+(float)window.getSize().y/2-(float)old_size_window.y/2; + } + if(obj.scale_pos.x == 0){ + spX = obj.size.x+(float)window.getSize().x-(float)old_size_window.x; + } + if(obj.scale_pos.y == 0){ + spY = obj.size.y+(float)window.getSize().y-(float)old_size_window.y; + } + + obj.real_size.x = spX; + obj.real_size.y = spY; + + if(obj.stick_object != null){ + if(obj.stick_pos.x == 0){ + psX = ((float)obj.stick_object->real_size.x)/2-obj.real_size.x/2+obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == 1){ + psX = ((float)obj.stick_object->real_size.x)-obj.real_size.x-obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == -1){ + psX = (float)obj.position.x+obj.stick_object->real_pos.x; + } + + if(obj.stick_pos.y == 0){ + psY = ((float)obj.stick_object->real_size.y)/2-obj.real_size.y/2+obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == 1){ + psY = ((float)obj.stick_object->real_size.y)-obj.real_size.y-obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == -1){ + psY = (float)obj.position.y+obj.stick_object->real_pos.y; + } + } + obj.real_pos.x = psX; + obj.real_pos.y = psY; + + shape.setOutlineThickness(obj.outline); + shape.setOutlineColor(obj.outline_col); + shape.setSize(sf::Vector2f(spX, spY)); + shape.setPosition(sf::Vector2f(psX,psY)); + shape.setFillColor(obj.color); + window.draw(shape); +} + + +void RoundedRectangle(object& obj,sf::RenderWindow& window) +{ + int POINTS = obj.radius; + + float psX=obj.position.x; + float psY=obj.position.y; + float spX=obj.size.x; + float spY=obj.size.y; + + if(obj.scale_pos.x == 1 || obj.scale_pos.x == -1){ + spX = obj.size.x+(float)window.getSize().x/2-(float)old_size_window.x/2; + } + if(obj.scale_pos.y == 1 || obj.scale_pos.y == -1){ + spY = obj.size.y+(float)window.getSize().y/2-(float)old_size_window.y/2; + } + if(obj.scale_pos.x == 0){ + spX = obj.size.x+(float)window.getSize().x-(float)old_size_window.x; + } + if(obj.scale_pos.y == 0){ + spY = obj.size.y+(float)window.getSize().y-(float)old_size_window.y; + } + + obj.real_size.x = spX; + obj.real_size.y = spY; + + if(obj.stick_object != null){ + if(obj.stick_pos.x == 0){ + psX = ((float)obj.stick_object->real_size.x)/2-obj.real_size.x/2+obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == 1){ + psX = ((float)obj.stick_object->real_size.x)-obj.real_size.x-obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == -1){ + psX = (float)obj.position.x+obj.stick_object->real_pos.x; + } + + if(obj.stick_pos.y == 0){ + psY = ((float)obj.stick_object->real_size.y)/2-obj.real_size.y/2+obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == 1){ + psY = ((float)obj.stick_object->real_size.y)-obj.real_size.y-obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == -1){ + psY = (float)obj.position.y+obj.stick_object->real_pos.y; + } + } + obj.real_pos.x = psX; + obj.real_pos.y = psY; + + /* https://en.sfml-dev.org/forums/index.php?topic=973.0 */ + sf::ConvexShape rrect; + rrect.setPointCount(POINTS*4); + rrect.setOutlineThickness(obj.outline); + rrect.setOutlineColor(obj.outline_col); + rrect.setFillColor(obj.color); + float X=0,Y=0; + int a = 0; + for(int i=0; ireal_size.x)/2-obj.real_size.x/2+obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == 1){ + psX = ((float)obj.stick_object->real_size.x)-obj.real_size.x-obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == -1){ + psX = (float)obj.position.x+obj.stick_object->real_pos.x; + } + + if(obj.stick_pos.y == 0){ + psY = ((float)obj.stick_object->real_size.y)/2-obj.real_size.y/2+obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == 1){ + psY = ((float)obj.stick_object->real_size.y)-obj.real_size.y-obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == -1){ + psY = (float)obj.position.y+obj.stick_object->real_pos.y; + } + } + obj.real_pos.x = psX; + obj.real_pos.y = psY; + shape.setPosition(psX, psY); + window.draw(shape); +} + +object* new_object(std::vector>& massive,std::string name, float pos_x, float pos_y, float size_x, float size_y, object* obj_stick = null,int st_x=2,int st_y=0, sf::Color color=sf::Color::White){ + auto obj = std::make_unique(); + obj->name = name; + obj->position.x = pos_x; + obj->position.y = pos_y; + obj->size.x = size_x; + obj->size.y = size_y; + obj->color = color; + obj->stick_pos.x = st_x; + obj->stick_pos.y = st_y; + obj->stick_object = obj_stick; + massive.push_back(std::move(obj)); + return massive.back().get(); +} + + diff --git a/draw.hpp b/draw.hpp new file mode 100644 index 0000000..3c28b50 --- /dev/null +++ b/draw.hpp @@ -0,0 +1,13 @@ +#pragma once +#include +#include "data.hpp" + + +void update_window(sf::RenderWindow& window,std::vector>& objects){ + window.clear(); + for(const auto & i : objects){ + object* obj = i.get(); + obj->draw(window); + } + window.display(); +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cf9920b --- /dev/null +++ b/main.cpp @@ -0,0 +1,41 @@ +#include +#include "data-files.hpp" +#include "draw.hpp" +#include "data.hpp" +#include + + + +std::vector> objects; + +int main(){ + sf::ContextSettings settings; + settings.antialiasingLevel = 5; + sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!", sf::Style::Default, settings); old_size(window); get_files(); + + object* canvas = new_object(objects,"canvas" , 0, 0, 0, 0, null, 10, 10,sf::Color::Black); + canvas->size = sf::Vector2f(200,200); + + object* obj = new_object(objects,"center",0,0,120,50,canvas,-1,-1,sf::Color(120,100,200,100)); + obj->type = "round"; + obj->radius = 20; + + object* text = new_object(objects,"texte",0,0,30,0,obj,0,0,sf::Color::White); + text->type = "text"; + text->text = "UwU"; + + while (window.isOpen()){ + sf::Event event; + while (window.pollEvent(event)){ + if (event.type == sf::Event::Closed) + window.close(); + if (event.type == sf::Event::Resized){ + sf::FloatRect visible(0.f, 0.f, event.size.width, event.size.height); + window.setView(sf::View(visible)); + } + } + update_window(window,objects); + } + + return 0; +}