Skip to content

Commit

Permalink
Improved example. Fixed invalid callback error.
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Oct 20, 2020
1 parent 36a2037 commit 1a472f4
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 21 deletions.
20 changes: 19 additions & 1 deletion example/example.collection
@@ -1,7 +1,7 @@
name: "example"
scale_along_z: 0
embedded_instances {
id: "go"
id: "loader"
data: "components {\n"
" id: \"example\"\n"
" component: \"/example/example.script\"\n"
Expand All @@ -17,6 +17,24 @@ embedded_instances {
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/example/webmonetization.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
Expand Down
33 changes: 14 additions & 19 deletions example/example.script
@@ -1,21 +1,16 @@
function init(self)
local monetized = webmonetization.is_monetized()
if monetized then
print("The user has an active payment stream")
end

webmonetization.set_listener(function(self, event, details)
if event == webmonetization.EVENT_PENDING then
print("The user is trying to make a first payment")
elseif event == webmonetization.EVENT_START then
print("The user has started paying")
elseif event == webmonetization.EVENT_PROGRESS then
print("The user is still paying")
elseif event == webmonetization.EVENT_STOP then
print("The user has stopped paying")
else
print("Unknown event", event)
end
pprint(details)
end)
msg.post(".", "acquire_input_focus")
msg.post("#", "load_example")
end


function on_message(self, message_id, message, sender)
print(message_id)
if message_id == hash("load_example") then
msg.post("#collectionproxy", "async_load")
elseif message_id == hash("unload_example") then
msg.post("#collectionproxy", "unload")
elseif message_id == hash("proxy_loaded") then
msg.post(sender, "enable")
end
end
91 changes: 91 additions & 0 deletions example/webmonetization.collection
@@ -0,0 +1,91 @@
name: "webmonetization"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"webmonetization\"\n"
" component: \"/example/webmonetization.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"label\"\n"
" type: \"label\"\n"
" data: \"size {\\n"
" x: 128.0\\n"
" y: 32.0\\n"
" z: 0.0\\n"
" w: 0.0\\n"
"}\\n"
"scale {\\n"
" x: 1.0\\n"
" y: 1.0\\n"
" z: 1.0\\n"
" w: 0.0\\n"
"}\\n"
"color {\\n"
" x: 1.0\\n"
" y: 1.0\\n"
" z: 1.0\\n"
" w: 1.0\\n"
"}\\n"
"outline {\\n"
" x: 0.0\\n"
" y: 0.0\\n"
" z: 0.0\\n"
" w: 1.0\\n"
"}\\n"
"shadow {\\n"
" x: 0.0\\n"
" y: 0.0\\n"
" z: 0.0\\n"
" w: 1.0\\n"
"}\\n"
"leading: 1.0\\n"
"tracking: 0.0\\n"
"pivot: PIVOT_CENTER\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"line_break: false\\n"
"text: \\\"WEB MONETIZATION\\\"\\n"
"font: \\\"/builtins/fonts/system_font.font\\\"\\n"
"material: \\\"/builtins/fonts/label.material\\\"\\n"
"\"\n"
" position {\n"
" x: 320.0\n"
" y: 568.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
38 changes: 38 additions & 0 deletions example/webmonetization.script
@@ -0,0 +1,38 @@
local function log(message)
print(message)
label.set_text("#label", message)
end

function init(self)
msg.post(".", "acquire_input_focus")

if webmonetization then
local monetized = webmonetization.is_monetized()
if monetized then
log("The user has an active payment stream")
end

webmonetization.set_listener(function(self, event, details)
if event == webmonetization.EVENT_PENDING then
log("The user is trying to make a first payment")
elseif event == webmonetization.EVENT_START then
log("The user has started paying")
elseif event == webmonetization.EVENT_PROGRESS then
log("The user is still paying")
elseif event == webmonetization.EVENT_STOP then
log("The user has stopped paying")
else
log("Unknown event", event)
end
pprint(details)
end)
else
log("Not supported")
end
end

function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed then
msg.post("example:/loader", "unload_example")
end
end
5 changes: 4 additions & 1 deletion webmonetization/src/webmonetization.cpp
Expand Up @@ -29,14 +29,17 @@ static void lua_setfieldstringstring(lua_State* L, const char* key, const char*

static void WebMonetization_OnEventListener(const char* event, const char* details)
{
if (g_WebMonetization.m_Callback == 0)
{
return;
}

lua_State* L = dmScript::GetCallbackLuaContext(g_WebMonetization.m_Callback);

DM_LUA_STACK_CHECK(L, 0);

if (!dmScript::IsCallbackValid(g_WebMonetization.m_Callback))
{
dmScript::DestroyCallback(g_WebMonetization.m_Callback);
g_WebMonetization.m_Callback = 0;
return;
}
Expand Down

0 comments on commit 1a472f4

Please sign in to comment.