forked from jbendig/InvGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptWidget.cpp
131 lines (107 loc) · 3.46 KB
/
ScriptWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ScriptWidget.h"
#include <string>
#include <sstream>
#include <boost/foreach.hpp>
#include "ScriptRunner.h"
#include "Item.h"
#include "FilePath.h"
using std::string;
ScriptWidget::ScriptWidget()
: QWidget(),
inventoryTag(NULL)
{
//Setup widgets.
QPushButton* runButton = new QPushButton("Run");
scriptTextEdit = new QPlainTextEdit();
consoleTextEdit = new QTextEdit();
consoleTextEdit->setReadOnly(true);
QPalette textPalette = palette();
textPalette.setColor(QPalette::Active,QPalette::Base,QColor(0,0,0));
textPalette.setColor(QPalette::Disabled,QPalette::Base,QColor(0,0,0));
textPalette.setColor(QPalette::Active,QPalette::Text,QColor(212,212,212));
textPalette.setColor(QPalette::Disabled,QPalette::Text,QColor(212,212,212));
consoleTextEdit->setPalette(textPalette);
QSplitter* splitter = new QSplitter(Qt::Vertical);
splitter->addWidget(scriptTextEdit);
splitter->addWidget(consoleTextEdit);
splitter->setCollapsible(0,false);
//Layout widgets.
QVBoxLayout* topLayout = new QVBoxLayout();
topLayout->addWidget(runButton);
topLayout->addWidget(splitter,1);
setLayout(topLayout);
setEnabled(false);
//Connect events.
connect(runButton,SIGNAL(pressed()),SLOT(RunScript()));
//Load previous lua script if one is available.
const QString settingsDir = FilePath::GetInvGridSettingsDirectory().c_str();
LoadScript(settingsDir + "auto.lua");
}
ScriptWidget::~ScriptWidget()
{
//Save current script so it can be reloaded on start.
const QString settingsDir = FilePath::GetInvGridSettingsDirectory().c_str();
QDir("").mkpath(settingsDir);
SaveScript(settingsDir + "auto.lua");
}
void ScriptWidget::SetInventoryTag(NBT::Tag* toInventoryTag)
{
inventoryTag = toInventoryTag;
setEnabled(inventoryTag != NULL);
}
void ScriptWidget::RunScript()
{
//Get the current state of the inventory.
emit SaveToInventoryTag();
//Run script on inventory.
consoleTextEdit->clear();
const string scriptString = scriptTextEdit->toPlainText().toStdString();
ScriptRunner scriptRunner;
if(!scriptRunner.Run(inventoryTag,scriptString))
{
consoleTextEdit->append(scriptRunner.Output().c_str());
consoleTextEdit->append(scriptRunner.Error().c_str());
return;
}
consoleTextEdit->append(scriptRunner.Output().c_str());
consoleTextEdit->append("<b>Done.</b>");
//Sync changes to rest of program.
emit ReloadFromInventoryTag();
}
void ScriptWidget::GenerateScriptFromInventory()
{
//Get the current state of the inventory.
emit SaveToInventoryTag();
//Make the inventory easier to work with.
ItemMap itemMap;
InventoryTagToItemMap(inventoryTag,itemMap);
//Generate script that clears the current inventory and sets the items whatever the inventory is when this is run.
std::stringstream scriptStream;
scriptStream << "ClearInventory()\n\n";
BOOST_FOREACH(ItemMap::const_reference itemIter,itemMap)
{
const Item& item = itemIter.second;
scriptStream << "SetItem(" <<
(int)item.slot<< "," <<
(int)item.id << "," <<
(int)item.damage << "," <<
(int)item.count << ")\n";
}
scriptTextEdit->setPlainText(scriptStream.str().c_str());
}
void ScriptWidget::SaveScript(const QString fileName)
{
QFile file(fileName);
if(!file.open(QFile::WriteOnly | QFile::Truncate))
return;
QTextStream textStream(&file);
textStream << scriptTextEdit->toPlainText();
}
void ScriptWidget::LoadScript(const QString fileName)
{
QFile file(fileName);
if(!file.open(QFile::ReadOnly))
return;
QTextStream textStream(&file);
scriptTextEdit->setPlainText(textStream.readAll());
}