Skip to content

Commit

Permalink
Move interval functions to screen
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Dec 3, 2018
1 parent dc0337a commit ee81d6d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 126 deletions.
110 changes: 0 additions & 110 deletions src/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,45 +949,6 @@ Actor::UpdateInternal(float delta_time)
delta_time = m_fEffectDelta;
}
this->UpdateTweening(delta_time);

for (auto it = delayedFunctions.begin(); it != delayedFunctions.end();
++it) {
auto& delayedF = *it;
delayedF.second -= delta_time;
if (delayedF.second <= 0) {
delayedF.first();
}
}
// Doing this in place did weird things
delayedFunctions.erase(std::remove_if(delayedFunctions.begin(),
delayedFunctions.end(),
[](pair<function<void()>, float>& x) {
return x.second <= 0;
}),
delayedFunctions.end());
if (!delayedPeriodicFunctionIdsToDelete.empty()) {
auto* L = LUA->Get();
for (auto id : delayedPeriodicFunctionIdsToDelete) {
luaL_unref(L, LUA_REGISTRYINDEX, id);
auto& vec = this->delayedPeriodicFunctions;
vec.erase(std::remove_if(
vec.begin(),
vec.end(),
[id](tuple<function<void()>, float, float, int>& x) {
return std::get<3>(x) == id;
}),
vec.end());
}
LUA->Release(L);
delayedPeriodicFunctionIdsToDelete.clear();
}
for (auto& delayedF : delayedPeriodicFunctions) {
std::get<1>(delayedF) -= delta_time;
if (std::get<1>(delayedF) <= 0) {
std::get<0>(delayedF)();
std::get<1>(delayedF) = std::get<2>(delayedF);
}
}
}

RString
Expand Down Expand Up @@ -1704,20 +1665,6 @@ Actor::TweenInfo::operator=(const TweenInfo& rhs)
return *this;
}

void
Actor::SetTimeout(function<void()> f, float ms)
{
delayedFunctions.emplace_back(make_pair(f, ms));
return;
}

void
Actor::SetInterval(function<void()> f, float ms, int id)
{
delayedPeriodicFunctions.emplace_back(make_tuple(f, ms, ms, id));
return;
}

// lua start
#include "LuaBinding.h"

Expand All @@ -1730,59 +1677,6 @@ class LunaActor : public Luna<Actor>
p->SetName(SArg(1));
COMMON_RETURN_SELF;
}
static int setTimeout(T* p, lua_State* L)
{
auto f = GetFuncArg(1, L);
std::function<void()> execF = [f]() {
Lua* L = LUA->Get();
f.PushSelf(L);
if (!lua_isnil(L, -1)) {
RString Error =
"Error running RequestChartLeaderBoard Finish Function: ";
LuaHelpers::RunScriptOnStack(
L, Error, 0, 0, true); // 1 args, 0 results
}
LUA->Release(L);
};
p->SetTimeout(execF, FArg(2));
COMMON_RETURN_SELF;
}
static int setInterval(T* p, lua_State* L)
{
lua_pushvalue(L, 1);
auto f = luaL_ref(L, LUA_REGISTRYINDEX);
std::function<void()> execF = [f]() {
Lua* L = LUA->Get();
lua_rawgeti(L, LUA_REGISTRYINDEX, f);
if (!lua_isnil(L, -1)) {
RString Error =
"Error running RequestChartLeaderBoard Finish Function: ";
LuaHelpers::RunScriptOnStack(
L, Error, 0, 0, true); // 1 args, 0 results
}
LUA->Release(L);
};
p->SetInterval(execF, FArg(2), f);
lua_pushnumber(L, f);
return 1;
}
static int clearInterval(T* p, lua_State* L)
{
int r = IArg(1);
auto& vec = p->delayedPeriodicFunctions;
auto it = find_if(vec.begin(),
vec.end(),
[r](tuple<function<void()>, float, float, int>& x) {
return std::get<3>(x) == r;
});
if (it != vec.end()) {
p->delayedPeriodicFunctionIdsToDelete.emplace_back(r);
} else {
LuaHelpers::ReportScriptError(
"Interval function not found (When triying to clearInterval() )");
}
return 0;
}
static int sleep(T* p, lua_State* L)
{
float fTime = FArg(1);
Expand Down Expand Up @@ -2795,10 +2689,6 @@ class LunaActor : public Luna<Actor>
DEFINE_METHOD(IsVisible, IsVisible());
LunaActor()
{
ADD_METHOD(name);
ADD_METHOD(setInterval);
ADD_METHOD(setTimeout);
ADD_METHOD(clearInterval);
ADD_METHOD(name);
ADD_METHOD(sleep);
ADD_METHOD(linear);
Expand Down
7 changes: 0 additions & 7 deletions src/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -749,13 +749,6 @@ class Actor : public MessageSubscriber
virtual void PushSelf(lua_State* L);
virtual void PushContext(lua_State* L);

vector<pair<function<void(void)>, float>> delayedFunctions;
void SetTimeout(function<void()> f, float ms);
std::list<tuple<function<void(void)>, float, float, int>>
delayedPeriodicFunctions; // This is a list to allow safe iterators
vector<int> delayedPeriodicFunctionIdsToDelete;
void SetInterval(function<void()> f, float ms, int fRemove);

// Named commands
void AddCommand(const RString& sCmdName,
apActorCommands apac,
Expand Down
115 changes: 110 additions & 5 deletions src/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
#define GROUPED_SCREENS THEME->GetMetric(m_sName, "GroupedScreens")

static const char* ScreenTypeNames[] = {
"Attract",
"GameMenu",
"Gameplay",
"Evaluation",
"SystemMenu",
"Attract", "GameMenu", "Gameplay", "Evaluation", "SystemMenu",
};
XToString(ScreenType);
LuaXType(ScreenType);
Expand Down Expand Up @@ -175,6 +171,45 @@ Screen::Update(float fDeltaTime)
if (iSize != m_QueuedMessages.size())
i = 0;
}

for (auto it = delayedFunctions.begin(); it != delayedFunctions.end();
++it) {
auto& delayedF = *it;
delayedF.second -= fDeltaTime;
if (delayedF.second <= 0) {
delayedF.first();
}
}
// Doing this in place did weird things
delayedFunctions.erase(std::remove_if(delayedFunctions.begin(),
delayedFunctions.end(),
[](pair<function<void()>, float>& x) {
return x.second <= 0;
}),
delayedFunctions.end());
if (!delayedPeriodicFunctionIdsToDelete.empty()) {
auto* L = LUA->Get();
for (auto id : delayedPeriodicFunctionIdsToDelete) {
luaL_unref(L, LUA_REGISTRYINDEX, id);
auto& vec = this->delayedPeriodicFunctions;
vec.erase(std::remove_if(
vec.begin(),
vec.end(),
[id](tuple<function<void()>, float, float, int>& x) {
return std::get<3>(x) == id;
}),
vec.end());
}
LUA->Release(L);
delayedPeriodicFunctionIdsToDelete.clear();
}
for (auto& delayedF : delayedPeriodicFunctions) {
std::get<1>(delayedF) -= fDeltaTime;
if (std::get<1>(delayedF) <= 0) {
std::get<0>(delayedF)();
std::get<1>(delayedF) = std::get<2>(delayedF);
}
}
}

/* Returns true if the input was handled, or false if not handled. For
Expand Down Expand Up @@ -401,6 +436,20 @@ Screen::PassInputToLua(const InputEventPlus& input)
return handled;
}

void
Screen::SetTimeout(function<void()> f, float ms)
{
delayedFunctions.emplace_back(make_pair(f, ms));
return;
}

void
Screen::SetInterval(function<void()> f, float ms, int id)
{
delayedPeriodicFunctions.emplace_back(make_tuple(f, ms, ms, id));
return;
}

void
Screen::AddInputCallbackFromStack(lua_State* L)
{
Expand Down Expand Up @@ -489,9 +538,65 @@ class LunaScreen : public Luna<Screen>
p->RemoveInputCallback(L);
COMMON_RETURN_SELF;
}
static int setTimeout(T* p, lua_State* L)
{
auto f = GetFuncArg(1, L);
std::function<void()> execF = [f]() {
Lua* L = LUA->Get();
f.PushSelf(L);
if (!lua_isnil(L, -1)) {
RString Error =
"Error running RequestChartLeaderBoard Finish Function: ";
LuaHelpers::RunScriptOnStack(
L, Error, 0, 0, true); // 1 args, 0 results
}
LUA->Release(L);
};
p->SetTimeout(execF, FArg(2));
COMMON_RETURN_SELF;
}
static int setInterval(T* p, lua_State* L)
{
lua_pushvalue(L, 1);
auto f = luaL_ref(L, LUA_REGISTRYINDEX);
std::function<void()> execF = [f]() {
Lua* L = LUA->Get();
lua_rawgeti(L, LUA_REGISTRYINDEX, f);
if (!lua_isnil(L, -1)) {
RString Error =
"Error running RequestChartLeaderBoard Finish Function: ";
LuaHelpers::RunScriptOnStack(
L, Error, 0, 0, true); // 1 args, 0 results
}
LUA->Release(L);
};
p->SetInterval(execF, FArg(2), f);
lua_pushnumber(L, f);
return 1;
}
static int clearInterval(T* p, lua_State* L)
{
int r = IArg(1);
auto& vec = p->delayedPeriodicFunctions;
auto it = find_if(vec.begin(),
vec.end(),
[r](tuple<function<void()>, float, float, int>& x) {
return std::get<3>(x) == r;
});
if (it != vec.end()) {
p->delayedPeriodicFunctionIdsToDelete.emplace_back(r);
} else {
LuaHelpers::ReportScriptError(
"Interval function not found (When triying to clearInterval() )");
}
return 0;
}

LunaScreen()
{
ADD_METHOD(setInterval);
ADD_METHOD(setTimeout);
ADD_METHOD(clearInterval);
ADD_METHOD(GetNextScreenName);
ADD_METHOD(SetNextScreenName);
ADD_METHOD(GetPrevScreenName);
Expand Down
15 changes: 11 additions & 4 deletions src/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ struct RegisterScreenClass
/** @brief The different types of screens available. */
enum ScreenType
{
attract, /**< The attract/demo mode, inviting players to play. */
game_menu, /**< The menu screens, where options can be set before playing.
*/
gameplay, /**< The gameplay screen, where the actual game takes place. */
attract, /**< The attract/demo mode, inviting players to play. */
game_menu, /**< The menu screens, where options can be set before playing.
*/
gameplay, /**< The gameplay screen, where the actual game takes place. */
evaluation,
system_menu, /**< The system/operator menu, where special options are set.
*/
Expand Down Expand Up @@ -103,6 +103,13 @@ class Screen : public ActorFrame
// Lua
void PushSelf(lua_State* L) override;

vector<pair<function<void(void)>, float>> delayedFunctions;
void SetTimeout(function<void()> f, float ms);
std::list<tuple<function<void(void)>, float, float, int>>
delayedPeriodicFunctions; // This is a list to allow safe iterators
vector<int> delayedPeriodicFunctionIdsToDelete;
void SetInterval(function<void()> f, float ms, int fRemove);

protected:
/** @brief Holds the messages sent to a Screen. */
struct QueuedScreenMessage
Expand Down

0 comments on commit ee81d6d

Please sign in to comment.