Skip to content

Commit

Permalink
improve drag and drop, allow it by default inTreeViewMemoryFields
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Auto committed May 19, 2024
1 parent 58e5784 commit 57c0249
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 71 deletions.
8 changes: 2 additions & 6 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ namespace S2Plugin
constexpr uint16_t gsRoleSize = Qt::UserRole + 10;
constexpr uint16_t gsRoleEntityAddress = Qt::UserRole + 11; // for entity uid to not look for the uid twice

constexpr char* gsJSONDragDropMemoryField_UID = "uid";
constexpr char* gsJSONDragDropMemoryField_Address = "addr";
constexpr char* gsJSONDragDropMemoryField_Type = "type";

// new types need to be added to
// - the MemoryFieldType enum
// - gsMemoryFieldType in Configuration.cpp
Expand Down Expand Up @@ -177,8 +173,8 @@ namespace S2Plugin
RoomCode(uint16_t _id, std::string _name, QColor _color) : id(_id), name(_name), color(_color){};
};

Q_DECLARE_METATYPE(S2Plugin::MemoryFieldType)
Q_DECLARE_METATYPE(std::string)
Q_DECLARE_METATYPE(S2Plugin::MemoryFieldType);
Q_DECLARE_METATYPE(std::string);

class Configuration
{
Expand Down
8 changes: 7 additions & 1 deletion include/QtHelpers/TreeViewMemoryFields.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace S2Plugin
{
struct MemoryField;

constexpr char* gsDragDropMemoryField_UID = "uid";
constexpr char* gsDragDropMemoryField_Address = "addr";
constexpr char* gsDragDropMemoryField_Type = "type";
constexpr char* gsDragDropMemoryField_IsPointer = "pointer";
constexpr char* gsDragDropMemoryField_RefName = "ref";

struct ColumnFilter
{
constexpr ColumnFilter& enable(uint8_t h)
Expand Down Expand Up @@ -84,7 +90,7 @@ namespace S2Plugin
signals:
void memoryFieldValueUpdated(int row, QStandardItem* parrent);
void levelGenRoomsPointerClicked();
void entityOffsetDropped(size_t offset);
void offsetDropped(uintptr_t offset);

private slots:
void cellClicked(const QModelIndex& index);
Expand Down
5 changes: 5 additions & 0 deletions include/Spelunky2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Data/CharacterDB.h"
#include "Data/EntityDB.h"
#include "Data/ParticleDB.h"
#include "Data/State.h"
#include "Data/StringsTable.h"
#include "Data/TextureDB.h"
#include "Data/VirtualTableLookup.h"
Expand Down Expand Up @@ -44,6 +45,10 @@ namespace S2Plugin
const EntityDB& get_EntityDB();
const StringsTable& get_StringsTable();
const VirtualTableLookup& get_VirtualTableLookup();
State get_State() const
{
return State{get_StatePtr()};
}
//

uintptr_t find(const char* pattern, uintptr_t start = 0, size_t size = 0) const;
Expand Down
2 changes: 1 addition & 1 deletion include/Views/ViewEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace S2Plugin
void refreshEntity();
void interpretAsChanged(const QString& text);
void label();
void entityOffsetDropped(size_t entityOffset);
void entityOffsetDropped(uintptr_t entityOffset);
void tabChanged();

private:
Expand Down
22 changes: 6 additions & 16 deletions src/QtHelpers/TableViewLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Data/Logger.h"
#include "QtHelpers/ItemModelLoggerFields.h"
#include "QtHelpers/StyledItemDelegateColorPicker.h"
#include "QtHelpers/TreeViewMemoryFields.h"
#include "QtPlugin.h"
#include <QColorDialog>
#include <QFont>
Expand All @@ -13,7 +14,6 @@
#include <QPainter>
#include <QTextCodec>
#include <QUuid>
#include <nlohmann/json.hpp>

S2Plugin::TableViewLogger::TableViewLogger(Logger* logger, QWidget* parent) : QTableView(parent), mLogger(logger)
{
Expand All @@ -30,18 +30,14 @@ S2Plugin::TableViewLogger::TableViewLogger(Logger* logger, QWidget* parent) : QT

void S2Plugin::TableViewLogger::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasFormat("spelunky/memoryfield"))
{
if (event->mimeData()->property(gsDragDropMemoryField_UID).isValid())
event->acceptProposedAction();
}
}

void S2Plugin::TableViewLogger::dragMoveEvent(QDragMoveEvent* event)
{
if (event->mimeData()->hasFormat("spelunky/memoryfield"))
{
if (event->mimeData()->property(gsDragDropMemoryField_UID).isValid())
event->accept();
}
}

void S2Plugin::TableViewLogger::dropEvent(QDropEvent* event)
Expand All @@ -50,14 +46,8 @@ void S2Plugin::TableViewLogger::dropEvent(QDropEvent* event)

auto fieldsModel = qobject_cast<ItemModelLoggerFields*>(model());

auto dropData = event->mimeData()->data("spelunky/memoryfield");
auto codec = QTextCodec::codecForName("UTF-8");
auto str = codec->toUnicode(dropData);

auto j = nlohmann::json::parse(str.toStdString());

LoggerField field;
field.type = static_cast<MemoryFieldType>(j[gsJSONDragDropMemoryField_Type].get<uint64_t>());
field.type = event->mimeData()->property(gsDragDropMemoryField_Type).value<MemoryFieldType>();
switch (field.type)
{
// List allowed types
Expand Down Expand Up @@ -97,8 +87,8 @@ void S2Plugin::TableViewLogger::dropEvent(QDropEvent* event)
}
}
field.uuid = QUuid::createUuid().toString().toStdString();
field.memoryAddr = j[gsJSONDragDropMemoryField_Address].get<uint64_t>();
field.name = j[gsJSONDragDropMemoryField_UID].get<std::string>();
field.memoryAddr = event->mimeData()->property(gsDragDropMemoryField_Address).toULongLong();
field.name = event->mimeData()->property(gsDragDropMemoryField_UID).toString().toStdString();
field.color = defaultColors[fieldsModel->rowCount() % defaultColors.size()];

mLogger->addField(field);
Expand Down
Loading

0 comments on commit 57c0249

Please sign in to comment.