Skip to content

Commit

Permalink
ugh shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
Unbansheee committed Mar 26, 2022
1 parent 8b15efb commit 7965ca0
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 41 deletions.
155 changes: 129 additions & 26 deletions PaintTool/PaintWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ void PaintWindow::WindowLoop()
{
while (window->isOpen())
{

colour = sf::Color(
static_cast<sf::Uint8>(circleColor[0] * 255),
static_cast<sf::Uint8>(circleColor[1] * 255),
static_cast<sf::Uint8>(circleColor[2] * 255),
static_cast<sf::Uint8>(opacity * 255));

sf::Event event{};
while (window->pollEvent(event))
Expand All @@ -72,15 +76,30 @@ void PaintWindow::WindowLoop()
if (event.type == sf::Event::MouseButtonReleased)
{
MouseDown = false;
while (!mouseLocations.empty())
mouseLocations.pop();
while (!lineMouseLocations.empty())
lineMouseLocations.pop();
mouseDoOnce = false;


switch (CurrentTool)
{

case Tools::EFREEDRAW: break;
case Tools::ECIRCLE: Layers[currentLayer]->GetRenderTexture()->draw(circle); break;
case Tools::ERECTANGLE: Layers[currentLayer]->GetRenderTexture()->draw(rectangle); break;
case Tools::ELINE: if (!actionMouseLocations.empty())DrawBetweenPoints(actionMouseLocations.front(), actionMouseLocations.back()); break;
default: ;
}
actionMouseLocations.clear();
}

if (event.type == sf::Event::MouseWheelMoved)
{
freedrawSize += static_cast<float>(event.mouseWheel.delta);
if (freedrawSize < 1) freedrawSize = 1;
dynamic_cast<sf::CircleShape*>(shape)->setRadius(freedrawSize);

freedrawSize += static_cast<float>(event.mouseWheel.delta);
if (freedrawSize < 1) freedrawSize = 1;
dynamic_cast<sf::CircleShape*>(shape)->setRadius(freedrawSize);


}

Expand All @@ -90,10 +109,6 @@ void PaintWindow::WindowLoop()
{
SaveImage();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LControl) && sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{
AtlasTextures(AtlasDimensions[0], AtlasDimensions[1], 823, 1180);
}
}

if (event.type == sf::Event::Resized)
Expand All @@ -113,10 +128,64 @@ void PaintWindow::WindowLoop()
}
else mouseMoved = false;

if (MouseDown && mouseMoved)

if (MouseDown)
{
DrawToLayer(Layers[currentLayer]);
actionMouseLocations.push_back(mousePos);
sf::Vector2<int> scale = (actionMouseLocations.back() - actionMouseLocations.front()) ;
switch (CurrentTool)
{
case Tools::EFREEDRAW:
{
if (mouseMoved) DrawToLayer(Layers[currentLayer]);
break;
}
case Tools::ELINE:
{
/*
float scaleFactor = Length(scale);
line.setSize(sf::Vector2f(10, freedrawSize));
line.setFillColor(colour);
float angle = (Angle( actionMouseLocations.back() - actionMouseLocations.front(), actionMouseLocations.back())) * (180/3.141);
line.setRotation(angle);
std::cout << angle << std::endl;
line.setPosition(actionMouseLocations.front().x, actionMouseLocations.front().y);
*/
// lines(sf::LinesStrip, 2)
// line.Line
shape->setPosition(sf::Vector2f(actionMouseLocations.front().x - freedrawSize, actionMouseLocations.front().y - freedrawSize));
line[0].position = sf::Vector2f(actionMouseLocations.front());
line[1].position = sf::Vector2f(actionMouseLocations.back());
line[0].color = colour;
line[1].color = colour;
break;
}
case Tools::ERECTANGLE:
{
rectangle.setSize(static_cast<sf::Vector2f>(scale));
fillShape ? rectangle.setFillColor(colour) :rectangle.setFillColor(sf::Color::Transparent);
rectangle.setOutlineColor(colour);
rectangle.setOutlineThickness((freedrawSize * 2));
//rectangle.setOutlineThickness(1);
rectangle.setPosition(actionMouseLocations.front().x, actionMouseLocations.front().y);
break;
}
case Tools::ECIRCLE:
{
circle.setRadius(1);
circle.setScale(scale.x * 0.5, scale.y * 0.5);
float scaleFactor = (sqrt(circle.getScale().x * circle.getScale().x + circle.getScale().y * circle.getScale().y));
fillShape ? circle.setFillColor(colour) : circle.setFillColor(sf::Color::Transparent);
circle.setOutlineColor(colour);
circle.setOutlineThickness((freedrawSize * 2) / scaleFactor);
circle.setPointCount(16 + (scaleFactor * 0.2));

circle.setPosition(actionMouseLocations.front().x, actionMouseLocations.front().y);

break;
}
}
mouseDoOnce = true;
}

ImGui::SFML::Update(*window, deltaClock.restart());
Expand All @@ -127,32 +196,64 @@ void PaintWindow::WindowLoop()
UI_Debug();


shape->setFillColor(sf::Color(
static_cast<sf::Uint8>(circleColor[0] * 255),
static_cast<sf::Uint8>(circleColor[1] * 255),
static_cast<sf::Uint8>(circleColor[2] * 255),
static_cast<sf::Uint8>(opacity * 255)));
shape->setFillColor(colour);


window->clear(sf::Color(200, 200, 200, 255));

shape->setPosition(static_cast<float>(mousePos.x) - freedrawSize, static_cast<float>(mousePos.y) - freedrawSize);
if ((CurrentTool == Tools::ELINE && !MouseDown) || CurrentTool != Tools::ELINE) shape->setPosition(static_cast<float>(mousePos.x) - freedrawSize, static_cast<float>(mousePos.y) - freedrawSize);


for (RenderLayer* rt : Layers)
{
rt->UpdateTexture();
rt->GetRenderTexture()->display();
window->draw(*rt);
}


window->draw(*shape);
switch (CurrentTool)
{
case Tools::EFREEDRAW: {window->draw(*shape); break;}
case Tools::ECIRCLE: {window->draw(circle); break;}
case Tools::ERECTANGLE: {window->draw(rectangle); break;}
case Tools::ELINE: {window->draw(line); window->draw(*shape); break;}
default: ;
}

ImGui::SFML::Render(*window);
window->display();
}
}

float PaintWindow::Dot(sf::Vector2<int> a, sf::Vector2<int> b)
{
return (a.x * b.x) + (a.y * b.y);
}

float PaintWindow::Angle(sf::Vector2<int> a, sf::Vector2<int> b)
{
// return atan2(Det(b, a), Dot(a, b));
return acos(Dot(a, b) / (Length(a) * Length(b)));
}

float PaintWindow::Length(sf::Vector2<int> a)
{
return sqrt((a.x * a.x) + (a.y * a.y));
}

float PaintWindow::Det(sf::Vector2<int> a, sf::Vector2<int> b)
{
return (a.x * b.y) - (a.y * b.x);
}

/*
bool PaintWindow::Cross(sf::Vector2<int> a, sf::Vector2<int> b)
{
int z = a.x * b.y - a.y * b.y;
}
*/
std::string PaintWindow::GetDateAndTime() const
{
std::time_t t = std::time(0);
Expand Down Expand Up @@ -248,17 +349,17 @@ float PaintWindow::Lerp(float a, float b, float t)
void PaintWindow::DrawToLayer(RenderLayer* layer)
{
layer->GetRenderTexture()->draw(*shape);
mouseLocations.push(mousePos);
lineMouseLocations.push(mousePos);

if (mouseLocations.size() >= 2)
if (lineMouseLocations.size() >= 2)
{
sf::Vector2<int> p1 = mouseLocations.front();
mouseLocations.pop();
sf::Vector2<int> p2 = mouseLocations.front();
sf::Vector2<int> p1 = lineMouseLocations.front();
lineMouseLocations.pop();
sf::Vector2<int> p2 = lineMouseLocations.front();
DrawBetweenPoints(p2, p1);
}

layer->GetRenderTexture()->display();
//layer->GetRenderTexture()->display();
}

bool PaintWindow::SaveImage()
Expand Down Expand Up @@ -401,5 +502,7 @@ void PaintWindow::UI_Tools()
ImGui::SameLine();
if(ImGui::Button("Rect")) SetTool(Tools::ERECTANGLE);
if (update) ImGui::PopStyleColor();

ImGui::Checkbox("Fill Shape", &fillShape);
ImGui::End();
}
20 changes: 18 additions & 2 deletions PaintTool/PaintWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,22 @@ class PaintWindow
private:

sf::RenderTexture renderTexture;
sf::CircleShape circle;
sf::RectangleShape rectangle;
//sf::RectangleShape line;
sf::Sprite sprite;
sf::Vector2<int> mousePos;
sf::Shape* shape;
sf::RenderWindow* window;
std::queue<sf::Vector2<int>> mouseLocations;
std::queue<sf::Vector2<int>> lineMouseLocations;
std::vector<sf::Vector2<int>> actionMouseLocations;
sf::VertexArray line = sf::VertexArray(sf::Lines, 2);
sf::Clock deltaClock;
ImGuiIO* io;
Tools CurrentTool = Tools::EFREEDRAW;
ImVec4 ActiveButtonColour = ImColor(27, 54, 74);
bool mouseDoOnce;
bool fillShape;

int Width = 1600;
int Height = 900;
Expand All @@ -62,13 +69,22 @@ class PaintWindow
char AtlasOutput[128] = "Saved/Out";
float AtlasResolutionScale = 1.f;

sf::Color colour;

void AtlasTextures(int rows, int columns, int imageWidth, int imageHeight);
void UI_Debug();
void UI_Tools();
void UI_ColorPicker();
void UI_Atlas();
void WindowLoop();


float Dot(sf::Vector2<int> a, sf::Vector2<int> b);

float Angle(sf::Vector2<int> a, sf::Vector2<int> b);

float Length(sf::Vector2<int> a);
float Det(sf::Vector2<int> a, sf::Vector2<int> b);

RenderLayer* NewLayer(int width, int height);

static float Lerp(float a, float b, float t);
Expand Down
26 changes: 13 additions & 13 deletions PaintTool/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Size=291,102
Collapsed=0

[Window][Colour Picker]
Pos=1297,9
Size=288,299
Pos=1247,286
Size=288,295
Collapsed=0
DockId=0x00000004,0

[Window][Debug Window]
Pos=1297,415
Size=288,132
Pos=1247,687
Size=288,137
Collapsed=0
DockId=0x00000003,0

Expand All @@ -41,21 +41,21 @@ Size=398,345
Collapsed=0

[Window][Texture Atlas]
Pos=1297,9
Size=288,299
Pos=1247,286
Size=288,295
Collapsed=0
DockId=0x00000004,1

[Window][Tools]
Pos=1297,310
Size=288,103
Pos=1247,583
Size=288,102
Collapsed=0
DockId=0x00000005,0

[Docking][Data]
DockNode ID=0x00000001 Pos=1297,9 Size=288,538 Split=Y
DockNode ID=0x00000002 Parent=0x00000001 SizeRef=238,404 Split=Y Selected=0xA9845671
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=298,299 Selected=0xA9845671
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=298,103 Selected=0xD44407B5
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=238,132 Selected=0x7C31ADA6
DockNode ID=0x00000001 Pos=1247,286 Size=288,538 Split=Y
DockNode ID=0x00000002 Parent=0x00000001 SizeRef=288,399 Split=Y
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=288,299 Selected=0xA9845671
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=288,103 Selected=0xD44407B5
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=288,137 Selected=0x7C31ADA6

0 comments on commit 7965ca0

Please sign in to comment.