Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ make_args.txt
# Visual Studio Code config
.settings
jsconfig.json
.pshell_info
.vs/

# Webstorm project folder
.idea/

run.bat
run_debug.bat
104 changes: 104 additions & 0 deletions engine/html5_api_lua.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "html5_api.h"
#include "html5_web_view.h"
#include "html5_api_bindings.h"
#include "html5_lua_events.h"

#include <engine_plugin_api/plugin_api.h>
#include <engine_plugin_api/c_api/c_api_window.h>
Expand Down Expand Up @@ -85,6 +86,28 @@ template <typename T> T get_object(lua_State *L, int i)
return (T)p;
}

__forceinline void push(lua_State *L, int n)
{
stingray::api::lua->pushnumber(L, n);
}

__forceinline void push(lua_State *L, const char *s)
{
stingray::api::lua->pushstring(L, s);
}

template<typename T>
__forceinline void set_field(lua_State *L, const char *key, T value)
{
push(L, value);
stingray::api::lua->setfield(L, -2, key);
}

__forceinline void push_new_table(lua_State* L)
{
stingray::api::lua->createtable(L, 0, 0);
}

/* @adoc lua
@obj stingray.WebView : userdata
@grp core
Expand Down Expand Up @@ -146,6 +169,39 @@ void load_lua_api(LuaApi* env)
return 1;
});

/* @adoc lua
@sig stingray.WebApp.emit() : {event_name, event_data}
@des Emit an event to the web app.
*/
env->add_module_function("WebApp", "emit", [](lua_State* L)
{
TempAllocator tempalloc( stingray::api::allocator_api );

if (!web_apps)
return 0;

CefRefPtr<LuaWebApp> web_app = get_web_app(L, 1);
if (!web_app->is_ready())
return 0;

const char* name = stingray::api::lua->tolstring(L, 2, nullptr);
const char* data = stingray::api::lua->tolstring(L, 3, nullptr);

if ( web_app && name && data ) {
unsigned len = (unsigned)strlen(data) +
(unsigned)strlen(name) + 128;
const char *sync = "stingray.WebApp.sync();";
const char *dispatch =
"window.dispatchEvent(new CustomEvent('%s',{detail:%s}));%s";
char *event = (char *)tempalloc.allocate( len );
if ( snprintf( event, len, dispatch, name, data, sync ) > 0 ) {
web_app->execute( event );
}
return 1;
}
return 0;
});

/* @adoc lua
@sig stingray.WebApp.update(dt: number) : nil
@des Update and synchronize the web app with the engine update loop.
Expand All @@ -168,6 +224,54 @@ void load_lua_api(LuaApi* env)
return 0;
});

/* @adoc lua
@sig stingray.WebApp.consume_events() : {event_name, event_data}
@des Consume all events available for processing.
*/
env->add_module_function("WebApp", "consume_events", [](lua_State* L)
{
LuaEventPtr event = event_handler()->consume_one();
push_new_table(L);
unsigned counter = 0;
while ( event ) {
// Specify the table index.
push( L, ++counter );
// Create a new table at the pushed index.
push_new_table(L);
// Now set the appropriate fields.
set_field(L, "name", event->name.c_str());
set_field(L, "data", event->data.c_str());
stingray::api::lua->settable( L, -3 );
event = event_handler()->consume_one();
}
return 1;
});

/* @adoc lua
@sig stingray.WebView.emit() : {event_name, event_data}
@des Emit an event to the WebView.
*/
env->add_module_function("WebView", "emit", [](lua_State* L)
{
TempAllocator tempalloc( stingray::api::allocator_api );
CefRefPtr<WebView> web_view = get_web_view(L, 1);
const char* name = stingray::api::lua->tolstring(L, 2, nullptr);
const char* data = stingray::api::lua->tolstring(L, 3, nullptr);

if ( web_view && name && data ) {
unsigned len = (unsigned)strlen(data) +
(unsigned)strlen(name) + 128;
const char *dispatch =
"window.dispatchEvent(new CustomEvent('%s',{detail:%s}));";
char *event = (char *)tempalloc.allocate( len );
if ( snprintf( event, len, dispatch, name, data ) > 0 ) {
web_view->execute( event );
}
return 1;
}
return 0;
});

/* @adoc lua
@sig stingray.WebApp.render() : nil
@des Render and synchronize the web app with the engine render frame.
Expand Down
50 changes: 50 additions & 0 deletions engine/html5_lua_events.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "stingray_api.h"

#include "html5_lua_events.h"
#include <plugin_foundation/string.h>
#include <plugin_foundation/vector.h>

namespace PLUGIN_NAMESPACE {

LuaEventPtr LuaEventHandler::add_event(const char *name, const char *data)
{
LuaEventPtr event = MAKE_NEW(allocator, LuaEvent, allocator);
event->name = name;
event->data = data;

events.push_back( event );

return event;
}

LuaEventPtr LuaEventHandler::consume_one()
{
if ( stack_idx < events.size() ) {
return events[stack_idx++];
}

stack_idx = 0;
events.clear();
return nullptr;
}

LuaEventHandler *event_handler()
{
return LuaEventHandler::instance;
}

void setup_lua_event_table()
{
LuaEventHandler::instance =
MAKE_NEW(allocator, LuaEventHandler, allocator);
}

void shutdown_lua_event_table()
{
MAKE_DELETE_TYPE(LuaEventHandler::instance->allocator,
LuaEventHandler,
LuaEventHandler::instance);
}

LuaEventHandler* LuaEventHandler::instance = nullptr;
};
54 changes: 54 additions & 0 deletions engine/html5_lua_events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "shared_ptr.h"

#include <engine_plugin_api/plugin_api.h>

#include <plugin_foundation/allocator.h>
#include <plugin_foundation/string.h>
#include <plugin_foundation/vector.h>

namespace PLUGIN_NAMESPACE {

using namespace stingray_plugin_foundation;

struct LuaEvent {
ALLOCATOR_AWARE;

explicit LuaEvent( Allocator &a )
: allocator(a),
name(a),
data(a)
{
}

Allocator &allocator;
DynamicString name;
DynamicString data;
};
typedef shared_ptr<LuaEvent> LuaEventPtr;

struct LuaEventHandler {

ALLOCATOR_AWARE;
explicit LuaEventHandler(Allocator &a)
: allocator(a),
events(a),
stack_idx(0)
{
}

LuaEventPtr add_event(const char *name, const char *data);
LuaEventPtr consume_one();

Allocator &allocator;
Vector<LuaEventPtr> events;
unsigned stack_idx;
static LuaEventHandler* instance;
};

LuaEventHandler * event_handler();
void setup_lua_event_table();
void shutdown_lua_event_table();

} // end namespace
3 changes: 3 additions & 0 deletions engine/html5_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "html5_api.h"
#include "html5_web_browser.h"
#include "html5_web_page.h"
#include "html5_lua_events.h"

#include <engine_plugin_api/plugin_api.h>
#include <plugin_foundation/platform.h>
Expand Down Expand Up @@ -83,6 +84,7 @@ void setup_runtime_plugin(GetApiFunction get_engine_api)
load_lua_api(stingray::api::lua);

setup_web_page_database();
setup_lua_event_table();

// Initialize modules
browser::init();
Expand Down Expand Up @@ -165,6 +167,7 @@ void unload_plugin()
browser::shutdown();
unload_lua_api(stingray::api::lua);
shutdown_web_page_database();
shutdown_lua_event_table();
WebApp::shutdown();

unload_common_plugin_resources();
Expand Down
10 changes: 10 additions & 0 deletions engine/html5_web_api.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "shared_ptr.h"
#include "html5_web_browser.h"
#include "html5_web_view.h"
#include "html5_api_bindings.h"
#include "html5_lua_events.h"

#include <include/cef_app.h>

Expand All @@ -20,6 +22,14 @@ void bind_api_web_app(CefRefPtr<CefV8Value> stingray_ns)
sync_signal();
return CefV8Value::CreateUndefined();
});

bind_api(ns, "emit", [](const CefV8ValueList& args)
{
const char *messageName = get_arg<const char*>( args, 0 );
const char *messageData = get_arg<const char*>( args, 1 );
event_handler()->add_event( messageName, messageData );
return CefV8Value::CreateUndefined();
});
}

void bind_api_web_view(CefRefPtr<CefV8Value> stingray_ns)
Expand Down
24 changes: 22 additions & 2 deletions plugin/html5_resources/web_app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

local web_app

local test_events = false

local app_settings = stingray.Application.settings() or {}
local web_app_url = app_settings.web_app_url or "stingray://app/main.html"
if not stingray.Script.exists(web_app_url) then
Expand All @@ -23,8 +25,26 @@ function shutdown()
stingray.WebApp.destroy(web_app)
end

function update(dt)
stingray.WebApp.update(web_app, dt)
if test_events then
function update(dt)
stingray.WebApp.update(web_app, dt)
local events = stingray.WebApp.consume_events()
for i=1,#events,1 do
print( 'name: ' .. events[i].name )
print( 'data: ' .. events[i].data )
end
index = stingray.Keyboard.button_id("x")
local x_pressed = index and stingray.Keyboard.pressed( index )
if x_pressed then
-- Send an event to the web app
--
stingray.WebApp.emit(web_app, "eventFire", '"sayhello"' )
end
end
else
function update(dt)
stingray.WebApp.update(web_app, dt)
end
end

function render()
Expand Down
4 changes: 4 additions & 0 deletions plugin/sample_project/app.html5/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ define(require => {
Vector2.create(0,0), 2,
Vector2.create(res[0],res[1]))
}
stingray.WebApp.emit("worldCreated", '{"value":true}');
}

/**
Expand All @@ -158,6 +159,9 @@ define(require => {
Camera.set_local_rotation(app.camera.instance, app.camera.unit, orientation);

console.info('Camera created');
window.addEventListener("eventFire", function( e ) {
console.info( e.detail );
});
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tools/makeall.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ruby make.rb --config debug
ruby make.rb --config dev
ruby make.rb --config release