Skip to content

Commit

Permalink
free brush drawer & cleanup of the brush code
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Jul 18, 2024
1 parent 9513bd1 commit e3fbe6a
Show file tree
Hide file tree
Showing 20 changed files with 643 additions and 360 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ add_library(${PROJECT_NAME} SHARED
src/ui/AlliumPopup.cpp
src/util/BrushDrawer.cpp
src/util/CurveBrushDrawer.cpp
src/util/EditorUtilities.cpp
src/util/FreeBrushDrawer.cpp
src/util/LineBrushDrawer.cpp
src/util/PolylineConverter.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC
Expand Down
15 changes: 15 additions & 0 deletions include/Allium/manager/BrushManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ namespace allium {
BrushType m_currentBrush = BrushType::None;
BrushDrawer* m_currentDrawer = nullptr;

cocos2d::ccColor3B getColor();

int getColorID();
float getLineWidth();
float getCurveRoughness();
float getFreeThreshold();

void setColorID(int id);
void setLineWidth(float width);
void setCurveRoughness(float roughness);
void setFreeThreshold(float threshold);

bool m_panEditorInBrush = false;
bool m_tempPanEditorInBrush = false;

bool panEditorInBrush();
};
}
4 changes: 4 additions & 0 deletions include/Allium/util/BrushDrawer.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <Geode/Geode.hpp>
#include "PolylineConverter.hpp"

namespace allium {
class BrushDrawer : public cocos2d::CCNode {
protected:
cocos2d::CCDrawNode* m_overlay = nullptr;
bool m_canUpdateLine = false;

public:
bool init() override;

Expand All @@ -18,5 +20,7 @@ namespace allium {
void clearOverlay();

virtual void updateLine();

virtual PolylineConverter initializeConverter() = 0;
};
}
10 changes: 2 additions & 8 deletions include/Allium/util/CurveBrushDrawer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@ namespace allium {
// Maximum size of 4 points
std::vector<cocos2d::CCPoint> m_points;

// Todo: Implement adjustable line width
float m_lineWidth = 5.0f;
// Todo: Implement adjustable line color
cocos2d::ccColor3B m_lineColor = cocos2d::ccc3(255, 255, 255);
// Todo: Implement adjustable curve roughness
float m_curveRoughness = 0.4f;

// Generated points for all of the curves
std::vector<std::array<double, 2>> m_previousPoints;
std::vector<std::array<double, 2>> m_currentPoints;

private:
std::vector<std::array<double, 2>> getGeneratedPoints();
CurveOptimizer initalizeOptimizer();

public:
static CurveBrushDrawer* create();
Expand All @@ -36,5 +28,7 @@ namespace allium {
void updateOverlay() override;

void updateLine() override;

PolylineConverter initializeConverter() override;
};
}
25 changes: 25 additions & 0 deletions include/Allium/util/EditorUtilities.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <Geode/Geode.hpp>

namespace allium {
class EditorUtilities {
protected:
std::vector<GameObject*> m_objects;
int m_colorID = 0;
public:
static EditorUtilities* get();

void setColor(int colorID);

void addRectangle(
cocos2d::CCPoint const& p1, cocos2d::CCPoint const& p2, cocos2d::CCPoint const& p3, cocos2d::CCPoint const& p4
);

void addCircle(
cocos2d::CCPoint const& center, float radius
);

void finish();
};
}
27 changes: 27 additions & 0 deletions include/Allium/util/FreeBrushDrawer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <Geode/Geode.hpp>
#include "BrushDrawer.hpp"

namespace allium {
class FreeBrushDrawer : public BrushDrawer {
protected:
std::vector<cocos2d::CCPoint> m_points;
private:

std::vector<cocos2d::CCPoint> simplify(std::vector<cocos2d::CCPoint> const& points);
public:
static FreeBrushDrawer* create();
bool init() override;

bool handleTouchStart(cocos2d::CCPoint const& point) override;
void handleTouchMove(cocos2d::CCPoint const& point) override;
void handleTouchEnd(cocos2d::CCPoint const& point) override;

void updateOverlay() override;

void updateLine() override;

PolylineConverter initializeConverter() override;
};
}
8 changes: 2 additions & 6 deletions include/Allium/util/LineBrushDrawer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ namespace allium {
protected:
cocos2d::CCPoint m_firstPoint = ccp(0, 0);
cocos2d::CCPoint m_lastPoint = ccp(0, 0);

// Todo: Implement adjustable line width
float m_lineWidth = 5.0f;
// Todo: Implement adjustable line color
cocos2d::ccColor3B m_lineColor = cocos2d::ccc3(255, 255, 255);

public:
static LineBrushDrawer* create();
bool init() override;
Expand All @@ -24,5 +18,7 @@ namespace allium {
void updateOverlay() override;

void updateLine() override;

PolylineConverter initializeConverter() override;
};
}
86 changes: 86 additions & 0 deletions include/Allium/util/PolylineConverter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#pragma once

#include <stdint.h>
#include <vector>
#include <cmath>
#include <numbers>
#include <Geode/Geode.hpp>

namespace allium {
class PolylineConverter {
public:
struct Point {
double x;
double y;

Point operator+(Point const& other) const {
return Point{x + other.x, y + other.y};
}

Point operator-(Point const& other) const {
return Point{x - other.x, y - other.y};
}

Point operator*(double const& scalar) const {
return Point{x * scalar, y * scalar};
}

Point operator/(double const& scalar) const {
return Point{x / scalar, y / scalar};
}
};

struct Rect {
Point p1;
Point p2;
Point p3;
Point p4;
};

struct Circle {
Point center;
float radius;
};

private:
float m_lineWidth;
std::vector<Point> m_points;

// Returns the a coefficient of the line equation of the line passing through p1 and p2
double a(Point p1, Point p2);

// Returns the b coefficient of the line equation of the line passing through p1 and p2
double b(Point p1, Point p2);

// Returns the c coefficient of the line equation of the line passing through p1 and p2
double c(Point p1, Point p2);

// Returns the intersection point of the lines passing through p1 and p2 and r1 and r2
Point intersect(Point p1, Point p2, Point r1, Point r2);

// Returns the angle between the lines passing through p1, p2, and p2, p3
double angle(Point p1, Point p2, Point p3);

// Returns the distance between two points
double distance(Point p1, Point p2);

// Checks if the angle between the lines passing through p1, p2, and p2, p3 is valid
bool hasAngle(Point p1, Point p2, Point p3);

// Returns the offset point of p2 based on the angle between the lines passing through p1, p2, and p2, p3
// This is the corner of the rectangle that is wanted to be extended to intersection point
Point offset(Point p1, Point p2, Point p3);

// Extends the corner based on the angle between the lines passing through p1, p2, and p2, p3
auto extend(Point p1, Point p2, Point p3);

// Checks if the points are colinear
bool checkColinear(Point p1, Point p2, Point p3);

public:

PolylineConverter(float lineWidth, std::vector<Point>&& points);

void handleExtension(std::vector<PolylineConverter::Rect>& rects, std::vector<PolylineConverter::Circle>& circles);
};
}
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"geode": "3.1.1",
"geode": "3.2.0",
"gd": {
"win": "2.206",
"android": "2.206"
Expand Down
Binary file added resources/SupportPopup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 8 additions & 10 deletions src/hooks/EditorUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ struct EditorUIHook : Modify<EditorUIHook, EditorUI> {
using namespace keybinds;

this->template addEventListener<InvokeBindFilter>([=](InvokeBindEvent* event) {
if (!BrushManager::get()->m_panEditorInBrush) {
if (event->isDown()) {
BrushManager::get()->m_panEditorInBrush = true;
}
else {
BrushManager::get()->m_panEditorInBrush = false;
}
if (event->isDown()) {
BrushManager::get()->m_tempPanEditorInBrush = true;
}
else {
BrushManager::get()->m_tempPanEditorInBrush = false;
}
return ListenerResult::Propagate;
}, "pan-editor-in-brush"_spr);
Expand All @@ -75,7 +73,7 @@ struct EditorUIHook : Modify<EditorUIHook, EditorUI> {

$override
bool ccTouchBegan(CCTouch* touch, CCEvent* event) {
if (!BrushManager::get()->m_panEditorInBrush && BrushManager::get()->m_currentDrawer) {
if (!BrushManager::get()->panEditorInBrush() && BrushManager::get()->m_currentDrawer) {
auto layerPosition = this->getLayerPosition(touch);
BrushManager::get()->m_currentDrawer->handleTouchStart(layerPosition);

Expand All @@ -86,7 +84,7 @@ struct EditorUIHook : Modify<EditorUIHook, EditorUI> {

$override
void ccTouchMoved(CCTouch* touch, CCEvent* event) {
if (!BrushManager::get()->m_panEditorInBrush && BrushManager::get()->m_currentDrawer) {
if (!BrushManager::get()->panEditorInBrush() && BrushManager::get()->m_currentDrawer) {
auto layerPosition = this->getLayerPosition(touch);
BrushManager::get()->m_currentDrawer->handleTouchMove(layerPosition);

Expand All @@ -97,7 +95,7 @@ struct EditorUIHook : Modify<EditorUIHook, EditorUI> {

$override
void ccTouchEnded(CCTouch* touch, CCEvent* event) {
if (!BrushManager::get()->m_panEditorInBrush && BrushManager::get()->m_currentDrawer) {
if (!BrushManager::get()->panEditorInBrush() && BrushManager::get()->m_currentDrawer) {
auto layerPosition = this->getLayerPosition(touch);
BrushManager::get()->m_currentDrawer->handleTouchEnd(layerPosition);

Expand Down
34 changes: 34 additions & 0 deletions src/manager/BrushManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,37 @@ BrushManager* BrushManager::get() {
}
return instance;
}

bool BrushManager::panEditorInBrush() {
return m_panEditorInBrush || m_tempPanEditorInBrush;
}

cocos2d::ccColor3B BrushManager::getColor() {
return LevelEditorLayer::get()->m_levelSettings->m_effectManager->getColorSprite(this->getColorID())->m_color;
}

int BrushManager::getColorID() {
return Mod::get()->getSavedValue<int>("brush-color-id", 1011);
}
float BrushManager::getLineWidth() {
return Mod::get()->getSavedValue<float>("brush-line-width", 5.0f);
}
float BrushManager::getCurveRoughness() {
return Mod::get()->getSavedValue<float>("brush-curve-roughness", 0.4f);
}
float BrushManager::getFreeThreshold() {
return Mod::get()->getSavedValue<float>("brush-free-threshold", 0.4f);
}

void BrushManager::setColorID(int id) {
Mod::get()->setSavedValue("brush-color-id", id);
}
void BrushManager::setLineWidth(float width) {
Mod::get()->setSavedValue("brush-line-width", width);
}
void BrushManager::setCurveRoughness(float roughness) {
Mod::get()->setSavedValue("brush-curve-roughness", roughness);
}
void BrushManager::setFreeThreshold(float threshold) {
Mod::get()->setSavedValue("brush-free-threshold", threshold);
}
7 changes: 7 additions & 0 deletions src/ui/AlliumPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ui/AlliumPopup.hpp>
#include <util/BrushDrawer.hpp>
#include <util/CurveBrushDrawer.hpp>
#include <util/FreeBrushDrawer.hpp>
#include <util/LineBrushDrawer.hpp>

using namespace geode::prelude;
Expand Down Expand Up @@ -121,6 +122,12 @@ void AlliumPopup::brushToggleCallback(CCMenuItemToggler* toggle) {
this->createPanButton();
this->createFinalizeButton();
}
else if (toggle == m_freeBrushToggle) {
brushDrawer = FreeBrushDrawer::create();

BrushManager::get()->m_currentBrush = BrushType::Free;
this->createPanButton();
}
else {
BrushManager::get()->m_currentBrush = BrushType::None;

Expand Down
Loading

0 comments on commit e3fbe6a

Please sign in to comment.