Skip to content

Commit 0a34153

Browse files
authored
A Simple Game: Add width and height parameters to embed-gwt.html and make it responsive (#256)
1 parent 9c7440a commit 0a34153

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

_includes/embed-gwt.html

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
<style>
22
.embed-container {
3-
position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%;
3+
position: relative;
4+
max-width: 100%;
5+
margin: 1em auto; /* auto left and right to center div in case width is smaller than available space */
6+
7+
{% if include.width and include.height %}
8+
/* set width and height explicitly if they are given */
9+
width: {{include.width}}px;
10+
height: {{include.height}}px;
11+
{% else %}
12+
/* otherwise default to 16:9 ratio */
13+
height: 0;
14+
padding-bottom: 56.25%;
15+
{% endif %}
416
}
5-
.embed-container iframe , .embed-container object, .embed-container embed {
6-
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
17+
18+
/* set height based on ratio if screen width is smaller than given width to avoid black bars */
19+
{% if include.width and include.height %}
20+
@media only screen and (max-width: {{include.width}}px) {
21+
.embed-container {
22+
height: 0;
23+
padding-bottom: calc({{include.height}} / {{include.width}} * 100%);
24+
}
25+
}
26+
{% endif %}
27+
28+
.embed-container iframe, .embed-container object, .embed-container embed {
29+
position: absolute;
30+
top: 0;
31+
left: 0;
32+
width: 100%;
33+
height: 100%;
734
}
35+
836
.embed-container iframe {
9-
border:0;
37+
border: 0;
1038
}
1139
</style>
1240

wiki/misc/wiki-style-guide.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ Videos can be included like this:
8181

8282
Actual libGDX examples can be embedded via GWT as iframes. To do this, use the `embed-gwt` element on a wiki page:
8383
```yml
84-
{% raw %}{% include embed-gwt.html dir='viewport-example' %}{% endraw %}
84+
{% raw %}{% include embed-gwt.html dir='viewport-example' width="800" height="500" %}{% endraw %}
8585
```
8686

8787
`dir` refers to the directory in the [libgdx-wiki-examples](https://github.com/libgdx/libgdx-wiki-examples) repo, where the source code of the examples is located. In particular, it denotes the path to the root folder of the example's Gradle project (without leading and trailing slashes; in this case, `viewport-example` refers to the `/viewport-example/` folder which contains `/html/build.gradle`). The examples are automatically built via GH Actions (by calling `./gradlew html:dist`) and then deployed through GH Pages.
8888

89+
If `width` and `height` are **both** set they are used for the container dimensions. They represent the dimensions in pixels and must be given as raw numbers without units.
90+
8991
To style the embedded content, use the `containerstyle` and `iframestyle` attributes.
9092

9193
## Renaming pages

wiki/start/a-simple-game.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ redirect_from:
77

88
Let's make a game! Game design is hard, but if you break up the process into small, achievable goals, you'll be able to produce wonders. In this simple game tutorial, you will learn how to make a basic game from scratch. These are the essential skills that you will build on in future projects. You may watch the [video tutorial](https://youtu.be/aipDYyh1Mlc), however you should come back here for the code examples.
99

10-
{% include embed-gwt.html dir='a-simple-game' %}
10+
{% include embed-gwt.html dir='a-simple-game' width="800" height="500" %}
1111

1212
As you can see with the live demo, we're going to make a basic game where you control a bucket to collect water droplets falling from the sky. There is no score or end goal. Just enjoy the experience! Here are the steps that we will use to split up the game design process:
1313

0 commit comments

Comments
 (0)