/*global browser:true */ var applet = document.getElementById("applet"), run_button = document.getElementById("run-button"), reset_button = document.getElementById("reset-button"), watch_sunray_button = document.getElementById("watch-sunray-button"), nl_obj_panel, // org.nlogo.lite.Applet object nl_obj_workspace, // org.nlogo.lite.LiteWorkspace nl_obj_world, // org.nlogo.agent.World nl_obj_program, // org.nlogo.api.Program nl_obj_state, nl_obj_observer, nl_obj_globals, sw, pw, nlogo_elements, globals = [], i, data_array = [], graph; window.onload=function() { disable_nlogo_elements(); dgApi.initGame(); dgApi.beginRun(); // // NetLogo Applet Loading Handler // // Wait until the applet is loaded and initialized before enabling buttons // and creating JavaScript variables for Java objects in the applet. // applet.ready = false; applet.checked_more_than_once = false; window.setTimeout (function() { isAppletReady(); }, 250); function isAppletReady() { try { applet.ready = applet.panel(); } catch (e) { // Do nothing--we'll try again in the next timer interval. } if(applet.ready) { nl_setup_objects(); nl_obj_panel.commandLater("set done true"); sw = new applet.Packages.java.io.StringWriter(); pw = new applet.Packages.java.io.PrintWriter(sw); enable_nlogo_elements(); if(applet.checked_more_than_once) { clearInterval(applet.checked_more_than_once); applet.checked_more_than_once = false; } } else { if(!applet.checked_more_than_once) { applet.checked_more_than_once = window.setInterval(function() { isAppletReady(); }, 250); } } } // // Create these JavaScript objects to provide access to the // corresponding Java objects in NetLogo. // function nl_setup_objects() { nl_obj_panel = applet.panel(); nl_obj_workspace = nl_obj_panel.workspace(); nl_obj_world = nl_obj_workspace.org$nlogo$lite$LiteWorkspace$$world; nl_obj_program = nl_obj_world.program(); nl_obj_observer = nl_obj_world.observer(); nl_obj_globals = nl_obj_program.globals(); } // // NetLogo command interface // function nl_cmd_start() { nl_obj_panel.commandLater("set done false while [not done] [ execute ]"); } function nl_cmd_stop() { nl_obj_panel.commandLater("set done true"); } function nl_cmd_execute(cmd) { nl_obj_panel.commandLater(cmd); } function nl_cmd_save_state() { nl_obj_world.exportWorld(pw, true); nl_obj_state = sw.toString(); } function nl_cmd_restore_state() { if (nl_obj_state) { var sr = new applet.Packages.java.io.StringReader(nl_obj_state); nl_obj_workspace.importWorld(sr); nl_obj_panel.commandLater("display"); } } function nl_cmd_reset() { nl_cmd_stop(); nl_obj_panel.commandLater("startup"); } // // Managing the NetLogo data polling system // function startNLDataPoller() { applet.data_poller = window.setInterval(function() { nlDataPoller(); }, 200); } function stopNLDataPoller() { if (applet.data_poller) { clearInterval(applet.data_poller); applet.data_poller = false; } } function nlDataPoller() { data_array.push(nl_obj_observer.getVariable(3)); dgApi.addTick(data_array); } // // button handlers // run_button.onclick = function() { if (run_button.textContent == "Run") { run_button_run(); } else { run_button_stop(); } }; reset_button.onclick = function() { run_button_stop(); nl_cmd_reset(); data_array.length = 0; dgApi.endRun(); dgApi.beginRun(); }; watch_sunray_button.onclick = function() { nl_cmd_execute("watch one-of sunrays with [ycor > (max-pycor / 2 ) and heading > 90 ]"); }; // // button/view helpers // function run_button_run() { nl_cmd_start(); startNLDataPoller(); run_button.textContent = "Stop"; } function run_button_stop() { nl_cmd_stop(); stopNLDataPoller(); run_button.textContent = "Run"; } // // add the css class "inactive" to all dom elements that include the class "nlogo" // function disable_nlogo_elements() { nlogo_elements = document.getElementsByClassName("nlogo"); for (i=0; i < nlogo_elements.length; i++) { nlogo_elements[i].classList.add("inactive"); } } // // add the css class "active" to all dom elements that include the class "nlogo" // function enable_nlogo_elements() { nlogo_elements = document.getElementsByClassName("nlogo"); for (i=0; i < nlogo_elements.length; i++) { nlogo_elements[i].classList.remove("inactive"); nlogo_elements[i].classList.add("active"); } } };