|
4 | 4 | <meta charset="utf-8">
|
5 | 5 | <link rel="stylesheet" href="style.css" />
|
6 | 6 | <title>DragonRuby Game Toolkit Tutorial</title>
|
| 7 | + <script src="//cdnjs.cloudflare.com/ajax/libs/reqwest/2.0.5/reqwest.js"></script> |
7 | 8 | </head>
|
8 | 9 | <body id='toplevel'>
|
9 | 10 | <script>
|
|
16 | 17 |
|
17 | 18 | <a id="tutorial-prev" href="#" onclick="tutorial_prev()">prev<br/>step</a>
|
18 | 19 | <a id="tutorial-next" href="#" onclick="tutorial_next()">next<br/>step</a>
|
| 20 | + <span id="tutorial-link-pointer">Use these links to<br/> navigate the tutorial →</span> |
19 | 21 |
|
20 |
| - <div id="tutorial" style="width: 960px; height: 270px;"> |
21 |
| - </div> |
| 22 | + <div id="editor"></div> |
22 | 23 |
|
23 |
| - <!-- <div id="reference-code" style="width: 480px; height: 270px;"> |
24 |
| - </div> |
25 |
| - --> |
26 |
| - <div id="editor" style="width: 480px; height: 270px;"> |
| 24 | + <div id="tutorial"> |
27 | 25 | </div>
|
28 | 26 |
|
29 |
| - <iframe src="game.html" width="480" height="270"></iframe> |
| 27 | + |
| 28 | + <iframe src="game.html"></iframe> |
30 | 29 |
|
31 | 30 | <script>
|
32 | 31 | function hotload() {
|
|
40 | 39 |
|
41 | 40 | window.addEventListener('load', function() {
|
42 | 41 | setTimeout(function() {
|
43 |
| - editor.setValue(document.getElementById('main.rb').innerHTML.trim()); |
44 | 42 | editor.focus();
|
45 | 43 | editor.gotoLine(0);
|
46 | 44 | editor.clearSelection();
|
47 |
| - tutorial_start(); |
| 45 | + tutorial_retrieve(); |
48 | 46 | }, 1000);
|
49 | 47 | });
|
50 | 48 |
|
|
109 | 107 | var newHeight =
|
110 | 108 | editor.getSession().getScreenLength() * editor.renderer.lineHeight + editor.renderer.scrollBar.getWidth();
|
111 | 109 |
|
112 |
| - $('#editor').height("270px"); |
113 |
| - $('#editor-section').height("270px"); |
| 110 | + $('#editor').height("540px"); |
| 111 | + $('#editor-section').height("540px"); |
114 | 112 |
|
115 | 113 | // This call is required for the editor to fix all of
|
116 | 114 | // its inner structure for adapting to a change in size
|
|
132 | 130 | */
|
133 | 131 | </script>
|
134 | 132 |
|
135 |
| - <script id="main.rb" type='text/ruby'> |
136 |
| -def tick args |
137 |
| -end |
138 |
| - </script> |
139 |
| - |
140 |
| - <script class="tutorial-step" type="text/html" data-tutorial-step="1"> |
141 |
| - <h1>DragonRuby Primer</h1> |
142 |
| - <p> |
143 |
| - This tutorial is going to give you a crash course of DragonRuby Game Toolkit. Let's get started shall we? |
144 |
| - <br/> |
145 |
| - <br/> |
146 |
| - Click the link in the top right corner that says <b>"next step"</b> to continue. |
147 |
| - </p> |
148 |
| - </script> |
149 |
| - |
150 |
| - <script class="tutorial-step" type="text/html" data-tutorial-step="2"> |
151 |
| - <h1>Rendering a Label</h1> |
152 |
| - <p> |
153 |
| - Do you see the code editor directly <b>below</b> this box? Click in there and type the following: |
154 |
| - <br/> |
155 |
| - </p> |
156 |
| - |
157 |
| - <pre>def tick args |
158 |
| - args.outputs.labels << [30, 30, "hello world"] |
159 |
| -end</pre> |
160 |
| - |
161 |
| - <p> |
162 |
| - Once you've typed the code above. Press "Ctrl + S" (or "Cmd + S" if you're on a Mac) to run the game. Play around with the numbers and see how the label moves around. Click <b>"next step"</b> whenever you're ready. |
163 |
| - </p> |
164 |
| - </script> |
165 |
| - |
166 | 133 | <script>
|
| 134 | + var tutorial = { steps: { } }; |
167 | 135 | var current_tutorial_step = 1;
|
168 |
| - var tutorial = {}; |
169 |
| - function tutorial_start() { |
170 |
| - var tutorial_elements = document.getElementsByClassName("tutorial-step"); |
171 |
| - for (var i = 0; i < tutorial_elements.length; i++) { |
172 |
| - var element = tutorial_elements[i]; |
173 |
| - var step = element.getAttribute("data-tutorial-step"); |
174 |
| - if (!tutorial[step]) { |
175 |
| - tutorial[step] = { |
176 |
| - text: "", |
177 |
| - code: "" |
178 |
| - } |
179 |
| - } |
180 |
| - |
181 |
| - if (element.type == "text/html") { |
182 |
| - tutorial[step].text = element.innerHTML; |
183 |
| - } |
184 |
| - } |
185 | 136 |
|
| 137 | + function tutorial_start() { |
186 | 138 | tutorial_load_step(current_tutorial_step);
|
187 | 139 | }
|
188 | 140 |
|
189 | 141 | function tutorial_prev() {
|
| 142 | + document.getElementById("tutorial-link-pointer").style.display = 'none'; |
190 | 143 | var next_step = current_tutorial_step - 1;
|
191 |
| - if (tutorial[next_step.toString()]) { |
| 144 | + if (tutorial.steps[next_step.toString()]) { |
192 | 145 | current_tutorial_step -= 1;
|
193 | 146 | tutorial_load_step(current_tutorial_step);
|
194 | 147 | }
|
195 | 148 | }
|
196 | 149 |
|
197 | 150 | function tutorial_next() {
|
| 151 | + document.getElementById("tutorial-link-pointer").style.display = 'none'; |
198 | 152 | var next_step = current_tutorial_step + 1;
|
199 |
| - if (tutorial[next_step.toString()]) { |
| 153 | + if (tutorial.steps[next_step.toString()]) { |
200 | 154 | current_tutorial_step += 1;
|
201 | 155 | tutorial_load_step(current_tutorial_step);
|
202 | 156 | }
|
203 | 157 | }
|
204 | 158 |
|
205 | 159 | function tutorial_load_step(number) {
|
206 |
| - var tutorial_step = tutorial[number.toString()]; |
207 |
| - document.getElementById("tutorial").innerHTML = tutorial_step.text; |
208 |
| - /* reference_code.setValue(tutorial_step.code); |
209 |
| - * reference_code.gotoLine(0); |
210 |
| - * reference_code.clearSelection(); */ |
| 160 | + var tutorial_step = tutorial.steps[number.toString()]; |
| 161 | + document.getElementById("tutorial").innerHTML = tutorial_step.text.outerHTML; |
| 162 | + editor.setValue("# Ctrl|Cmd + S for save.\n# Ctrl|Cmd + N for next step in tutorial.\n# Ctrl|Cmd + P for previous step in tutorial.\n\n" + tutorial_step.code); |
| 163 | + editor.gotoLine(0); |
| 164 | + editor.clearSelection(); |
| 165 | + } |
| 166 | + |
| 167 | + function tutorial_retrieve() { |
| 168 | + reqwest('warpdrive-tutorial.html', function (resp) { |
| 169 | + var script = document.createElement("script"); |
| 170 | + script.id = "retrieved-tutorial"; |
| 171 | + script.type = "text/tutorial"; |
| 172 | + script.innerHTML = resp; |
| 173 | + document.body.appendChild(script); |
| 174 | + var div = document.createElement("div"); |
| 175 | + div.innerHTML = document.getElementById("retrieved-tutorial").innerHTML; |
| 176 | + |
| 177 | + Array.from(div.querySelectorAll("[itemscope='itemscope'][itemtype='tutorial-step']")) |
| 178 | + .sort((a, b) => parseInt(a.dataset.stepNumber) - parseInt(b.dataset.stepNumber)) |
| 179 | + .forEach(e => { |
| 180 | + var stepNumber = parseInt(e.dataset.stepNumber); |
| 181 | + var textElement = e.querySelector("[itemscope='itemscope'][itemtype='tutorial-text']"); |
| 182 | + var codeElement = e.querySelector("[itemscope='itemscope'][itemtype='tutorial-code']"); |
| 183 | + var leadingWhiteSpace = codeElement.innerText.split('\n').filter(l => l.trim().length != 0)[0].match(/^[ ]*/)[0] || ""; |
| 184 | + var code = leadingWhiteSpace + codeElement.innerText.trim(); |
| 185 | + code = code.split('\n').map(l => l.replace(new RegExp('^' + leadingWhiteSpace + ''), '')).join('\n'); |
| 186 | + tutorial.steps[stepNumber] = { text: textElement, code: code }; |
| 187 | + }); |
| 188 | + |
| 189 | + tutorial_start(); |
| 190 | + }); |
211 | 191 | }
|
212 | 192 | </script>
|
213 | 193 | </body>
|
|
0 commit comments