-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathluaengine.cpp
156 lines (130 loc) · 3.55 KB
/
luaengine.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "luaengine.h"
#include "lguilib.h"
#include "propertyeditor/luakvmodel.h"
#include "zlgcan/lzlgcanlib.h"
#include <QApplication>
#include <QClipboard>
LuaEngine::LuaEngine(QObject *parent) : QObject(parent)
{
this->m_L = NULL;
this->m_lua_thread = NULL;
this->m_msg_type_count = 0;
connect(this, SIGNAL(ToClipboradEvent(QString)), this, SLOT(onToClipborad(QString)));
connect(this, SIGNAL(PlayMediaEvent(QString)), this, SLOT(onPlayMedia(QString)));
connect(this, SIGNAL(StopMediaEvent()), this, SLOT(onStopMedia()));
}
LuaEngine *LuaEngine::Instance()
{
static LuaEngine *res = new LuaEngine();
return res;
}
lua_State *LuaEngine::newLuaThread()
{
lua_State *res = luaL_newstate();
luaL_openlibs(res);
luaL_requiref(res, LUA_ZLGCANNAME, luaopen_zlgcan, 0);
lua_pop(res, 1);
luaL_requiref(res, LUA_GUINAME, luaopen_gui, 1);
lua_pop(res, 1);
this->m_L = res;
return res;
}
void LuaEngine::triggerEvent(LuaEvent &event)
{
emit GetLuaEvent(event);
}
QThread *LuaEngine::runLuaThread(QString filename)
{
if (this->m_lua_thread == NULL)
{
this->m_script_name = filename;
this->m_events.clear();
this->newLuaThread();
this->m_lua_thread = new LuaThread(this->m_L, filename);
this->m_lua_thread->start(QThread::HighestPriority);
this->m_msg_type_count = 0;
connect(this->m_lua_thread, SIGNAL(finished()), this, SLOT(onLuaThreadFinished()));
emit LuaStateChange(this->m_script_name, true);
}
return this->m_lua_thread;
}
void LuaEngine::onLuaThreadFinished()
{
this->m_lua_thread->deleteLater();
this->m_lua_thread = NULL;
this->closeLuaThread();
emit LuaStateChange(this->m_script_name, false);
}
void LuaEngine::stopScript()
{
static LuaEvent event;
event.Type = LuaEvent::EVENT_EXIT;
event.ExtraType = LuaEvent::EXTRA_NONE;
gL->putEvent(event);
}
void LuaEngine::pauseScript()
{
static LuaEvent event;
event.Type = LuaEvent::EVENT_PAUSE;
event.ExtraType = LuaEvent::EXTRA_INT;
event.ExtraInt = 1;
gL->putEvent(event);
}
void LuaEngine::resumeScript()
{
static LuaEvent event;
event.Type = LuaEvent::EVENT_PAUSE;
event.ExtraType = LuaEvent::EXTRA_INT;
event.ExtraInt = 0;
gL->putEvent(event);
}
void LuaEngine::triggerPushMsg(int type, const char *msg)
{
emit PushMsg(type, QString::fromUtf8(msg));
}
void LuaEngine::triggerAddMsgType(const char *name)
{
emit AddMsgType(QString::fromUtf8(name));
}
void LuaEngine::triggerLuaScriptError(const char *msg)
{
emit LuaScriptError(QString::fromUtf8(msg));
}
void LuaEngine::triggerSetKV(const char *key, const char *value, const char *color)
{
emit SetKVEvent(QString::fromUtf8(key), QString::fromUtf8(value), QString::fromUtf8(color));
}
int LuaEngine::getNextMsgTypeCount()
{
++this->m_msg_type_count;
return this->m_msg_type_count;
}
void LuaEngine::triggerToClipboard(const char *text)
{
emit ToClipboradEvent(QString::fromUtf8(text));
}
void LuaEngine::onToClipborad(QString text)
{
QApplication::clipboard()->setText(text);
}
void LuaEngine::triggerPlayMedia(const char *file)
{
emit PlayMediaEvent(QString::fromUtf8(file));
}
void LuaEngine::triggerStopMedia()
{
emit StopMediaEvent();
}
void LuaEngine::onPlayMedia(QString file)
{
if (this->m_player.state() == QMediaPlayer::StoppedState)
{
this->m_player.setMedia(QUrl::fromLocalFile(file));
this->m_player.setVolume(50);
this->m_player.play();
}
}
void LuaEngine::onStopMedia()
{
this->m_player.stop();
}