Skip to content

Commit

Permalink
i wasnt lucky unlike everyone else
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSillyDoggo committed Dec 16, 2024
1 parent 1598494 commit 719ce2f
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 34 deletions.
Binary file added resources/sprites/draggable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 67 additions & 33 deletions src/Client/Windows/Labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../Labels/LabelLayer.hpp"
#include "../AndroidUI.h"
#include "../../DragDrop.hpp"
#include "../../UI/GrabNodeLayer.hpp"

#define BUTTON_WIDTH 200

Expand Down Expand Up @@ -277,6 +278,7 @@ void Labels::refreshList()
cell->setContentHeight(height);
cell->setContentWidth(scroll->getContentWidth());
cell->setUserData(module);
cell->setAnchorPoint(ccp(0.5f, 0.5f));
y += height + 4;

auto bg = CCScale9Sprite::create("square02b_001.png");
Expand Down Expand Up @@ -318,47 +320,62 @@ void Labels::refreshList()

auto arrowBack = CCScale9Sprite::create("square02_small.png");
arrowBack->setOpacity(100);
arrowBack->setContentSize(ccp(single ? 18 : 36, 18) * 3);
arrowBack->setContentSize(ccp(18, 18) * 3);
arrowBack->setScale(1.0f / 3.0f);
arrowBack->setPositionX(cell->getContentWidth() - (195 - (single ? (18 / 2) / arrowBack->getScale() : 0)) * arrowBack->getScale());
arrowBack->setPositionX(cell->getContentWidth() - (195 - ((18 / 2) / arrowBack->getScale())) * arrowBack->getScale());
arrowBack->setPositionY(cell->getContentHeight() / 2);

auto upSpr = CCSprite::createWithSpriteFrameName("edit_upBtn_001.png");
upSpr->setScale(0.65f);
auto grab = GrabNodeLayer::create();
grab->setNodeToGrab(cell);
grab->setLockedAxis(LockedAxis::Vertical);
grab->setOnStartDrag([cell, bg]
{
cell->runAction(CCEaseInOut::create(CCScaleTo::create(0.2f, 0.95f), 2));

auto moveUp = CCMenuItemSpriteExtra::create(upSpr, this, menu_selector(Labels::onMoveLabelUp));
moveUp->setPositionY(cell->getContentHeight() / 2);
moveUp->setTag(i);
bg->runAction(CCTintTo::create(0.35f, 145, 0, 255));

auto downSpr = CCSprite::createWithSpriteFrameName("edit_downBtn_001.png");
downSpr->setScale(0.65f);
cell->setZOrder(42069);
});
grab->setOnEndDrag([cell, this, i, bg]
{
cell->runAction(CCEaseBackOut::create(CCScaleTo::create(0.35f, 1.0f)));

auto moveDown = CCMenuItemSpriteExtra::create(downSpr, this, menu_selector(Labels::onMoveLabelDown));
moveDown->setPositionY(cell->getContentHeight() / 2);
moveDown->setTag(i);
bg->runAction(CCTintTo::create(0.35f, 0, 0, 0));

if (modules.size() == 1)
{
arrowBack->setVisible(false);
moveUp->setVisible(false);
moveDown->setVisible(false);
}
std::sort(cells.begin(), cells.end(), [](CCNode* a, CCNode* b)
{
return a->getPositionY() > b->getPositionY();
});

if (i == 0)
{
moveUp->setEnabled(false);
as<CCSprite*>(moveUp->getNormalImage())->setOpacity(150);
}
else if (i == modules.size() - 1)
{
moveDown->setEnabled(false);
as<CCSprite*>(moveDown->getNormalImage())->setOpacity(150);
}
int index = 0;

for (size_t i = 0; i < cells.size(); i++)
{
if (cells[i] == cell)
{
index = i;
break;
}
}

#define GAP_ARROWS_BOTH 8
log::info("index: {}", index);

moveUp->setPosition(arrowBack->getPosition() + ccp(GAP_ARROWS_BOTH, 0));
moveDown->setPosition(arrowBack->getPosition() + ccp(-GAP_ARROWS_BOTH, 0));
std::swap(modules[i], modules[index]);

save();

if (LabelLayer::get())
LabelLayer::get()->updateLabels();

dirtyRefreshList(true);
});

grab->setContentSize(ccp(cell->getContentHeight() - 2, cell->getContentHeight() - 2));
grab->setPosition(arrowBack->getPosition());

auto grabSpr = CCSprite::create("draggable.png"_spr);
grabSpr->setPosition(grab->getPosition());
grabSpr->setScale(0.6f);

auto toggleBtn = CCMenuItemToggler::createWithStandardSprites(this, menu_selector(Labels::onToggleVisible), 0.45f);
toggleBtn->setUserData(module);
Expand All @@ -374,8 +391,8 @@ void Labels::refreshList()
m->addChild(options);
m->addChild(deleteBtn);
m->addChild(arrowBack);
m->addChild(moveUp);
m->addChild(moveDown);
m->addChild(grab);
m->addChild(grabSpr);
m->addChild(toggleBG);
m->addChild(toggleBtn);

Expand All @@ -397,6 +414,7 @@ void Labels::refreshList()
for (auto cell : cells)
{
cell->setPositionY(scroll->m_contentLayer->getContentHeight() - cell->getPositionY() - 4 - cell->getContentHeight());
cell->setPosition(cell->getPosition() + cell->getContentSize() / 2);
}

if (cells.size() == 0)
Expand All @@ -410,6 +428,22 @@ void Labels::refreshList()
}
}

void Labels::dirtyRefreshList(bool smooth)
{
for (size_t i = 0; i < modules.size(); i++)
{
// im going to kill myself very soon
auto pos = ccp(cells[i]->getContentWidth() / 2, (scroll->m_contentLayer->getContentHeight() - ((23 + 4) * i) - cells[i]->getContentHeight() / 2) - 4);

if (smooth)
cells[i]->runAction(CCEaseInOut::create(CCMoveTo::create(0.35f, pos), 2));
else
cells[i]->setPosition(pos);

cells[i]->setZOrder(i);
}
}

void Labels::save()
{
Mod::get()->setSavedValue<float>("safe-zone.x", safeZone.origin.x);
Expand Down
1 change: 1 addition & 0 deletions src/Client/Windows/Labels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Labels : public Window
void cocosCreate(CCMenu* menu);

void refreshList();
void dirtyRefreshList(bool smooth);

void save();
void load();
Expand Down
108 changes: 108 additions & 0 deletions src/UI/GrabNodeLayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "GrabNodeLayer.hpp"

bool GrabNodeLayer::init()
{
if (!CCLayer::init())
return false;

this->ignoreAnchorPointForPosition(false);
this->setPosition(CCPointZero);
this->setContentSize(CCPointZero);
this->setAnchorPoint(ccp(0.5f, 0.5f));

this->setTouchEnabled(true);

return true;
}

CCPoint GrabNodeLayer::getAxisForLocked(LockedAxis axis)
{
switch (axis)
{
case LockedAxis::Horizontal:
return ccp(1, 0);

case LockedAxis::Vertical:
return ccp(0, 1);

default:
return ccp(1, 1);
}
}

void GrabNodeLayer::setLockedAxis(LockedAxis axis)
{
this->axis = axis;
}

LockedAxis GrabNodeLayer::getLockedAxis()
{
return axis;
}

void GrabNodeLayer::setNodeToGrab(CCNode* node)
{
nodeToGrab = node;
}

CCNode* GrabNodeLayer::getNodeToGrab()
{
return nodeToGrab;
}

void GrabNodeLayer::setOnStartDrag(std::function<void()> callback)
{
this->onStartDrag = callback;
}

void GrabNodeLayer::setOnEndDrag(std::function<void()> callback)
{
this->onEndDrag = callback;
}

void GrabNodeLayer::registerWithTouchDispatcher(void)
{
CCTouchDispatcher::get()->addTargetedDelegate(this, INT_MIN + 1, true);
}

bool GrabNodeLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if (nodeToGrab && cocos::nodeIsVisible(this))
{
auto bbox = CCRect(convertToWorldSpace(CCPointZero), convertToWorldSpace(getScaledContentSize()) - convertToWorldSpace(CCPointZero));

if (pTouch->getLocation() > bbox.origin && pTouch->getLocation() < bbox.origin + bbox.size)
{
isDragging = true;
startPos = nodeToGrab->getPosition();

if (onStartDrag)
onStartDrag();

return true;
}
}

return false;
}

void GrabNodeLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
if (isDragging)
{
nodeToGrab->setPosition(startPos + (pTouch->getLocation() - pTouch->getStartLocation()) * getAxisForLocked(axis));
}
}

void GrabNodeLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
isDragging = false;

if (onEndDrag)
onEndDrag();
}

void GrabNodeLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
ccTouchEnded(pTouch, pEvent);
}
46 changes: 46 additions & 0 deletions src/UI/GrabNodeLayer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <Geode/Geode.hpp>

using namespace geode::prelude;

enum class LockedAxis
{
Any,
Horizontal,
Vertical,
};

class GrabNodeLayer : public CCLayer
{
private:
bool isDragging = false;
CCNode* nodeToGrab = nullptr;
CCPoint startPos = CCPointZero;
std::function<void()> onStartDrag = nullptr;
std::function<void()> onEndDrag = nullptr;
LockedAxis axis = LockedAxis::Any;

CCPoint getAxisForLocked(LockedAxis axis);

public:
virtual bool init();

virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

virtual void registerWithTouchDispatcher(void);

void setNodeToGrab(CCNode* node);
CCNode* getNodeToGrab();

void setLockedAxis(LockedAxis axis);
LockedAxis getLockedAxis();

void setOnStartDrag(std::function<void()> callback);
void setOnEndDrag(std::function<void()> callback);

CREATE_FUNC(GrabNodeLayer);
};
Loading

0 comments on commit 719ce2f

Please sign in to comment.