Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good way to expose sf::Event to Lua with Luabridge?

According to the LuaBridge readme, LuaBridge does not support "Enumerated constants", which I assume is just enums. Since sf::Event is almost entirely enums, is there any way I can expose the class? Currently the only other solution I can come up with is detect key presses in C++, then send a string to Lua, that describes the event. Obviously, there are around 100+ keys on a modern keyboard, which would cause a massive, ugly segment of just if statements.

For those who haven't used SFML: Link to sf::Event class source code


UPDATE:

After attempting to create the function outlined in my question, I discovered that it don't work anyway, because you can't return more than one string in C++, so most events are ignored.

Example Source (doesn't work):

std::string getEvent()
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed) {window.close(); return "";}
        else if (event.type == sf::Event::GainedFocus) {return "GainedFocus";}
        else if (event.type == sf::Event::LostFocus) {return "LostFocus";}
        else if (event.type == sf::Event::Resized) {return "Resized";}
        else if (event.type == sf::Event::TextEntered)
        {
            if ((event.text.unicode < 128) && (event.text.unicode > 0)) {return "" + static_cast<char>(event.text.unicode);}
        }
        else if (event.type == sf::Event::KeyPressed)
        {
            //If else for all keys on keyboard
        }
        else if (event.type == sf::Event::KeyReleased)
        {
            //If else for all keys on keyboard
        }
        else {return "";}
    }
    return "";
}

UPDATE UPDATE:

Since this question has received zero comments or answers, I've decided not to rule out other libraries. So, if there is a C++ library that supports enums, I will accept it

like image 550
Orfby Avatar asked Jul 19 '15 18:07

Orfby


2 Answers

if you only want enum to number, consider this. <luabridge/detail/Vector.h> methods.

#include <LuaBridge/detail/Stack.h>
enum class LogLevels { LOG_1, LOG_2 }  

namespace luabridge
{
    template <>
    struct Stack<LogLevels>
    {
        static void push(lua_State* L, LogLevels const& v) { lua_pushnumber( L, static_cast<int>(v) ); }
        static LogLevels get(lua_State* L, int index) { return LuaRef::fromStack(L, index); }
    };
}
like image 21
HaruGakka Avatar answered Nov 05 '22 14:11

HaruGakka


Since this question has received zero comments or answers, I've decided not to rule out other libraries. So, if there is a C++ library that supports enums, I will accept it

The Thor library, an SFML extension, supports conversions between SFML key types and strings. This would help you serialize enumerators and pass them as strings to Lua -- and back if you need.

like image 61
TheOperator Avatar answered Nov 05 '22 15:11

TheOperator