1+
2+ <!DOCTYPE html>
3+ < html lang ="en ">
4+ < head >
5+ < meta charset ="utf-8 ">
6+ < title > NetTangoizer</ title >
7+ < link href ="https://fonts.googleapis.com/css?family=Poppins " rel ="stylesheet ">
8+ < link href ="../css/ntango.css " rel ="stylesheet ">
9+ < link href ="css/codefirst.css " rel ="stylesheet ">
10+ < style >
11+ body {
12+ margin : 40px ;
13+ font-family : Poppins, sans-serif;
14+ font-size : 18px ;
15+ }
16+ .netlogo-container {
17+ position : relative;
18+ margin : 20px ;
19+ }
20+ .nt-canvas {
21+ background-color : # fef9f6 ;
22+ margin : 20px 0 0 40px ;
23+ border : 1px solid # aaa ;
24+ }
25+ .overlay {
26+ position : absolute;
27+ left : 0 ;
28+ right : 0 ;
29+ top : 0 ;
30+ bottom : 0 ;
31+ background-color : rgba (0 , 0 , 0 , 0.4 );
32+ text-align : center;
33+ display : none;
34+ }
35+ .nt-container {
36+ display : inline-block;
37+ }
38+
39+ # recompile-button {
40+ margin : 150px auto;
41+ width : 200px ;
42+ height : 55px ;
43+ border-radius : 8px ;
44+ outline : none;
45+ border : none;
46+ background-color : # 6b9bc3 ;
47+ font-family : Poppins, sans-serif;
48+ font-size : 20px ;
49+ color : white;
50+ text-align : center;
51+ }
52+
53+ # recompile-button : active {
54+ background-color : # 9bd ;
55+ }
56+
57+ # netlogo-code {
58+ margin : 40px ;
59+ widows : 500px ;
60+ min-height : 200px ;
61+ border : 1px solid # 555 ;
62+ background-color : # efefef ;
63+ padding : 30px ;
64+ font-size : 15px ;
65+ overflow-y : scroll;
66+ }
67+ </ style >
68+ </ head >
69+
70+ < body >
71+
72+ < h3 > Wolves and Sheep</ h3 >
73+
74+ < div class ="netlogo-container ">
75+ < div class ="overlay " id ="recompile-overlay ">
76+ < button id ="recompile-button " onclick ="recompile() "> Recompile</ button >
77+ </ div >
78+ < iframe id ="netlogo " frameborder ="0 " height ="430 " scrolling ="yes " width ="100% " src ="WSP1.html "> </ iframe >
79+ </ div >
80+
81+
82+ < h3 > NetTango Code</ h3 >
83+
84+ < div class ="nt-container ">
85+ < canvas id ="nt-wolf-code " class ="nt-canvas " width ="470 " height ="500 "> </ canvas >
86+ </ div >
87+
88+
89+ < div class ="nt-container ">
90+ < canvas id ="nt-sheep-code " class ="nt-canvas " width ="470 " height ="500 "> </ canvas >
91+ </ div >
92+ < br >
93+ < br >
94+
95+
96+ < h3 > NetLogo Code</ h3 >
97+ < pre id ="netlogo-code ">
98+ </ pre >
99+
100+ < script src ="../lib/ntango.js "> </ script >
101+
102+ < script type ="text/javascript ">
103+
104+
105+ // -------------------------------------------------------------
106+ // update NetLogo code and recompile
107+ // -------------------------------------------------------------
108+ function recompile ( ) {
109+ var nl = document . getElementById ( "netlogo" ) . contentWindow ;
110+ nl . session . widgetController . setCode ( buildCode ( ) ) ;
111+ nl . session . recompile ( ) ;
112+ document . getElementById ( "recompile-overlay" ) . style . display = "none" ;
113+ }
114+
115+
116+ // -------------------------------------------------------------
117+ // merge existing NetLogo code and current NetTango code
118+ // -------------------------------------------------------------
119+ function buildCode ( ) {
120+ var nl = document . getElementById ( "netlogo" ) . contentWindow ;
121+
122+ var nlcode = nl . session . widgetController . code ( ) ;
123+ var ntcode = "; --- NETTANGO BEGIN ---" ;
124+ ntcode += "\n\n" + NetTango . exportCode ( "nt-wolf-code" , "NetLogo" ) ;
125+ ntcode += "\n\n" + NetTango . exportCode ( "nt-sheep-code" , "NetLogo" ) ;
126+ ntcode += "\n\n; --- NETTANGO END ---" ;
127+
128+ // merge NetTango and NetLogo
129+ var merged = nlcode ;
130+ if ( nlcode . indexOf ( "; --- NETTANGO BEGIN ---" ) >= 0 ) {
131+ merged = nlcode . replace ( new RegExp ( "((?:^|\n); --- NETTANGO BEGIN ---\n)([^]*)(\n; --- NETTANGO END ---)" ) , "\n" + ntcode + "\n" ) ;
132+ } else {
133+ merged = nlcode + "\n\n" + ntcode ;
134+ }
135+
136+ return merged ;
137+ }
138+
139+
140+ // -------------------------------------------------------------
141+ // initialize NetTango workspaces
142+ // -------------------------------------------------------------
143+ document . body . onload = function ( ) {
144+ console . log ( "hello" ) ;
145+ NetTango . init ( "nt-wolf-code" , wolfDefinition ) ;
146+ NetTango . init ( "nt-sheep-code" , sheepDefinition ) ;
147+
148+ NetTango . onProgramChanged ( "nt-wolf-code" , function ( canvasId ) {
149+ document . getElementById ( "recompile-overlay" ) . style . display = "block" ;
150+ document . getElementById ( "netlogo-code" ) . innerHTML = buildCode ( ) ;
151+ } ) ;
152+
153+ NetTango . onProgramChanged ( "nt-sheep-code" , function ( canvasId ) {
154+ document . getElementById ( "recompile-overlay" ) . style . display = "block" ;
155+ document . getElementById ( "netlogo-code" ) . innerHTML = buildCode ( ) ;
156+ } ) ;
157+ console . log ( "goodbye" ) ;
158+ }
159+
160+
161+ // -------------------------------------------------------------
162+ // NetTango block definitions
163+ // -------------------------------------------------------------
164+ var wolfDefinition = {
165+ "blocks" : [
166+ {
167+ "action" : "\uD83D\uDC3A wolf actions " ,
168+ "type" : "nlogo:procedure" ,
169+ "start" : true ,
170+ "limit" : 1 ,
171+ "format" : "to wolf-actions" ,
172+ "blockColor" : "#b55" ,
173+ "required" : true
174+ } ,
175+
176+ {
177+ "action" : "wolf meets sheep " ,
178+ "type" : "nlogo:procedure" ,
179+ "start" : true ,
180+ "limit" : 1 ,
181+ "format" : "to wolf-meets-sheep" ,
182+ "blockColor" : "#b55" ,
183+ "required" : true
184+ } ,
185+
186+ {
187+ "action" : "forward" ,
188+ "format" : "forward {0}" ,
189+ "params" : [
190+ {
191+ "type" : "range" ,
192+ "min" : 0 ,
193+ "max" : 3 ,
194+ "step" : 0.1 ,
195+ "default" : 1 ,
196+ "name" : "steps"
197+ }
198+ ]
199+ } ,
200+
201+ {
202+ "action" : "left" ,
203+ "format" : "left random {0}" ,
204+ "params" : [
205+ {
206+ "type" : "range" ,
207+ "min" : 0 ,
208+ "max" : 90 ,
209+ "step" : 1 ,
210+ "default" : 50 ,
211+ "random" : true ,
212+ "name" : "amount" ,
213+ "unit" : "\u00B0"
214+ }
215+ ]
216+ } ,
217+
218+
219+ {
220+ "action" : "right" ,
221+ "format" : "right random {0}" ,
222+ "params" : [
223+ {
224+ "type" : "range" ,
225+ "min" : 0 ,
226+ "max" : 90 ,
227+ "step" : 1 ,
228+ "default" : 50 ,
229+ "random" : true ,
230+ "name" : "amount" ,
231+ "unit" : "\u00B0"
232+ }
233+ ]
234+ } ,
235+
236+ {
237+ "action" : "hatch" ,
238+ "format" : "hatch 1 [ rt random-float 360 fd 1 ]" ,
239+ "blockColor" : "#916da0"
240+ } ,
241+
242+ {
243+ "action" : "die" ,
244+ "blockColor" : "#916da0" ,
245+ } ,
246+
247+ {
248+ "action" : "\uD83D\uDC11 ask nearby sheep" ,
249+ "blockColor" : "#89a" ,
250+ "format" : "ask sheep in-radius 3" ,
251+ "clauses" : [ ]
252+ } ,
253+
254+
255+ {
256+ "action" : "chance" ,
257+ "blockColor" : "#89a" ,
258+ "format" : "if random 100 < {0}" ,
259+ "clauses" : [
260+ /*
261+ {
262+ "name" : "else",
263+ "action" : "chance-else",
264+ "format" : ""
265+ }
266+ */
267+ ] ,
268+ "params" : [
269+ {
270+ "type" : "range" ,
271+ "min" : 0 ,
272+ "max" : 100 ,
273+ "step" : 0.5 ,
274+ "default" : 20 ,
275+ "unit" : "%" ,
276+ "name" : "percent"
277+ }
278+ ]
279+ }
280+ ]
281+ } ;
282+
283+ var sheepDefinition = {
284+ "blocks" : [
285+
286+ {
287+ "action" : "\uD83D\uDC11 sheep actions " ,
288+ "type" : "nlogo:procedure" ,
289+ "start" : true ,
290+ "limit" : 1 ,
291+ "format" : "to sheep-actions" ,
292+ "blockColor" : "#b55" ,
293+ "required" : true
294+ } ,
295+
296+ {
297+ "action" : "forward" ,
298+ "format" : "forward {0}" ,
299+ "params" : [
300+ {
301+ "type" : "range" ,
302+ "min" : 0 ,
303+ "max" : 3 ,
304+ "step" : 0.1 ,
305+ "default" : 1 ,
306+ "name" : "steps"
307+ }
308+ ]
309+ } ,
310+
311+ {
312+ "action" : "left" ,
313+ "format" : "left random {0}" ,
314+ "params" : [
315+ {
316+ "type" : "range" ,
317+ "min" : 0 ,
318+ "max" : 90 ,
319+ "step" : 1 ,
320+ "default" : 50 ,
321+ "random" : true ,
322+ "name" : "amount" ,
323+ "unit" : "\u00B0"
324+ }
325+ ]
326+ } ,
327+
328+
329+ {
330+ "action" : "right" ,
331+ "format" : "right random {0}" ,
332+ "params" : [
333+ {
334+ "type" : "range" ,
335+ "min" : 0 ,
336+ "max" : 90 ,
337+ "step" : 1 ,
338+ "default" : 50 ,
339+ "random" : true ,
340+ "name" : "amount" ,
341+ "unit" : "\u00B0"
342+ }
343+ ]
344+ } ,
345+
346+ {
347+ "action" : "hatch" ,
348+ "format" : "hatch 1 [ rt random-float 360 fd 1 ]" ,
349+ "blockColor" : "#916da0"
350+ } ,
351+
352+ {
353+ "action" : "die" ,
354+ "blockColor" : "#916da0" ,
355+ } ,
356+
357+ {
358+ "action" : "chance" ,
359+ "blockColor" : "#89a" ,
360+ "format" : "if random 100 < {0}" ,
361+ "clauses" : [
362+ /*
363+ {
364+ "name" : "else",
365+ "action" : "chance-else",
366+ "format" : ""
367+ }
368+ */
369+ ] ,
370+ "params" : [
371+ {
372+ "type" : "range" ,
373+ "min" : 0 ,
374+ "max" : 100 ,
375+ "step" : 0.5 ,
376+ "default" : 20 ,
377+ "unit" : "%" ,
378+ "name" : "percent"
379+ }
380+ ]
381+ }
382+ ]
383+ } ;
384+ </ script >
385+ </ body >
386+ </ html >
0 commit comments