Skip to content

Commit

Permalink
Fixed banner creation for GD
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysinyavsky committed May 5, 2023
1 parent dad257c commit 2e935e7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
32 changes: 21 additions & 11 deletions ads_wrapper/ads_networks/game_distribution.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local helper = require("ads_wrapper.ads_networks.helper")
local M = { NAME = "game_distribution" }
-- Extention: https://github.com/GameDistribution/gd-defold

M.SIZE_NONE = 0
M.SIZE_336x280 = 1
M.SIZE_300x250 = 2
M.SIZE_970x250 = 3
Expand Down Expand Up @@ -107,25 +108,34 @@ local function create_banner(settings)
local baner_data = {
id = settings.banner_id,
wrapper_display = settings.wrapper_display,
wrapper_id = string.format("wr-%s", settings.banner_id)
wrapper_id = settings.wrapper_id
}
banner_ids[#banner_ids + 1] = baner_data
if settings.auto_create then
settings.size = settings.size or banner_default.size
settings.parent_id = settings.parent_id or banner_default.parent_id
settings.wrapper_style = settings.wrapper_style or banner_default.wrapper_style
settings.ad_style = settings.ad_style or banner_default.ad_style
html5.run(
string.format("var canvasContainer = document.getElementById('%s');", settings.parent_id) ..
"var div = document.createElement('div');" ..
string.format("div.id = '%s';", baner_data.wrapper_id) ..
"canvasContainer.appendChild(div);" ..
string.format("div.style = '%s';", settings.wrapper_style) ..

local code = string.format("var canvasContainer = document.getElementById('%s');", settings.parent_id)
if settings.wrapper_id then
code = code .. "var div = document.createElement('div');" ..
string.format("div.id = '%s';", settings.wrapper_id) ..
"canvasContainer.appendChild(div);" ..
string.format("div.style = '%s';", settings.wrapper_style)
else
code = code .. "var div = canvasContainer;"
end
code = code ..
"var ads_div = document.createElement('div');" ..
"div.appendChild(ads_div);" ..
string.format("ads_div.style = '%s %s';", settings.ad_style, get_size_string(settings.size)) ..
string.format("ads_div.id = '%s';", settings.banner_id)
)
"div.appendChild(ads_div);"
if settings.size ~= M.SIZE_NONE then
code = code .. string.format("ads_div.style = '%s %s';", settings.ad_style, get_size_string(settings.size))
else
code = code .. string.format("ads_div.style = '%s';", settings.ad_style)
end
code = code .. string.format("ads_div.id = '%s';", settings.banner_id)
html5.run(code)
end
end
end
Expand Down
14 changes: 9 additions & 5 deletions docs/networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,14 @@ The network uses [this](https://github.com/GameDistribution/gd-defold) extension
You need to set:
* [ads_wrapper.T_BANNER] <kbd>table|table[]</kbd> _optional_ banner options
* banner_id <kbd>string</kbd> _optional_ key for banner. Default `canvas-ad`.
* ad_style <kbd>string</kbd> _optional_ styles for the banner element. Default `margin-left: -50%; display: none;`.
* auto_create <kbd>boolean</kbd> _optional_ Automatically create a `div` element for the banner based on other parameters Default `false`.
* parent_id <kbd>string</kbd> _optional_ element id in which to place the banner. Default `canvas-container`.
* wrapper_id <kbd>string</kbd> _optional_ Id for the wrapper in which the banner element will be inserted
* wrapper_style <kbd>string</kbd> _optional_ styles for the wrapper element. Default `position: absolute; bottom: 0px; left: 50%;`.
* ad_style <kbd>string</kbd> _optional_ styles for the banner element. Default `margin-left: -50%; display: none;`.
* wrapper_display <kbd>string</kbd> _optional_ wrapper `display` style when banners are shown. Not set by default
* size <kbd>number</kbd> _optional_ banner size. Default `game_distribution.SIZE_336x280`. Possible values:
* game_distribution.SIZE_NONE
* game_distribution.SIZE_336x280
* game_distribution.SIZE_300x250
* game_distribution.SIZE_970x250
Expand All @@ -269,11 +271,13 @@ You can create multiple banners that will be displayed at the same time. You jus
local game_distribution_net_id = ads_wrapper.register_network(game_distribution, {
[ads_wrapper.T_BANNER] = {
auto_create = true,
size = game_distribution.SIZE_336x280,
size = game_distribution.SIZE_NONE,
parent_id = "canvas-container",
wrapper_style = "position: absolute; bottom: 0px; left: 50%;",
ad_style = "margin-left: -50%; display: none;",
banner_id = "canvas-ad"
wrapper_id = "wr-canvas-ad-bot",
wrapper_style = "position: absolute; top: 50vh; left: 0px; width: 100%; height: 50%; display: none;align-items: center;justify-content: center;overflow: hidden;flex-direction:column;",
ad_style = "width: 100%; height: 100%;",
banner_id = "canvas-ad-bot",
wrapper_display = "flex"
}
})

Expand Down
18 changes: 8 additions & 10 deletions example/example.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -377,23 +377,21 @@ local function init_game_distribution(self)
[ads_wrapper.T_BANNER] = {
{
auto_create = true,
size = game_distribution.SIZE_336x280,
size = game_distribution.SIZE_NONE,
parent_id = "canvas-container",
wrapper_id = "wr-canvas-ad-bot",
wrapper_style =
"position: absolute; top: 50vh; left: 0px; width: 100%; height: 25vh; display: flex;align-items: center;justify-content: center;overflow: hidden;",
ad_style = "display: none;",
"position: absolute; top: 50vh; left: 0px; width: 100%; height: 50%; display: none;align-items: center;justify-content: center;overflow: hidden;flex-direction:column;",
ad_style = "width: 100%; height: 50%;",
banner_id = "canvas-ad-bot",
wrapper_display = "flex"
},
{
auto_create = true,
size = game_distribution.SIZE_336x280,
parent_id = "canvas-container",
wrapper_style =
"position: absolute; top: 75vh; left: 0px; width: 100%; height: 25vh; display: flex;align-items: center;justify-content: center;overflow: hidden;",
ad_style = " display: none;",
banner_id = "canvas-ad-top",
wrapper_display = "flex"
size = game_distribution.SIZE_NONE,
parent_id = "wr-canvas-ad-bot",
ad_style = "width: 100%; height: 50%;",
banner_id = "canvas-ad-top"
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package = com.defold.adswrapper

[project]
title = Ads Wrapper
version = 1.2.2
version = 1.2.3
dependencies#0 = https://github.com/andsve/dirtylarry/archive/master.zip
dependencies#1 = https://github.com/Insality/druid/archive/refs/tags/0.7.0.zip
dependencies#2 = https://github.com/AGulev/DefVideoAds/archive/refs/tags/4.5.1.zip
Expand Down

0 comments on commit 2e935e7

Please sign in to comment.