UNPKG

agentscript

Version:

AgentScript Model in Model/View architecture

125 lines (107 loc) 3.78 kB
<!DOCTYPE html> <html> <head> <title>GUI</title> </head> <body> <div id="modelDiv"></div> <script type="module"> import Animator from '../src/Animator.js' import Mouse from '../src/Mouse.js' import Model from '../models/HelloModel.js' import TwoDraw from '../src/TwoDraw.js' import * as util from '../src/utils.js' import GUI from '../src/GUI.js' const model = new Model() await model.startup() model.setup() const view = new TwoDraw(model, { div: 'modelDiv', patchSize: 20, }) const anim = new Animator( () => { model.step() view.draw() }, -1, // run forever 30 // fps ).startStats() util.toWindow({ model, view, anim }) let selectedTurtle const mouse = new Mouse(model, view, mouse => { const { x, y, action } = mouse if (action === "mousedown") { aimator.stop() selectedTurtle = model.turtles.minOneOf(t => t.distanceXY(x, y)) } if (action === "mouseup") { aimator.start() selectedTurtle = null } if (selectedTurtle) selectedTurtle.setxy(x, y) view.draw() }) const gui = new GUI({ fps: { // slider val: [40, [5, 60, 5]], cmd: val => aimator.setFps(val), }, 'Turtles Folder': { turtlesShape: { // chooser val: ['bug', ['bug', 'dart', 'person', 'circle', 'arrow']], cmd: val => view.drawOptions.turtlesShape = val, }, turtlesSize: { // slider val: [1, [0.5, 10, 0.5]], // val: { Small: 1, Medium: 3, Large: 5 }, cmd: val => view.drawOptions.turtlesSize = val, }, 'Test Folder': { turtlesColor: { // color val: '#ff0000', cmd: val => view.drawOptions.turtlesColor = val, }, } }, downloadCanvas: { // button cmd: () => view.downloadCanvas(), }, stop: { // switch/toggle val: false, // bool cmd: val => { if (val) { aimator.stop() } else { aimator.start() } }, }, useMouse: { // switch/toggle val: false, // bool cmd: val => { if (val) { mouse.start() } else { mouse.stop() } }, }, setTitle: { // input val: 'Play', cmd: val => document.title = val }, modelTicks: { // monitor val: [model, 'ticks'], // the object and property to monitor }, }) util.toWindow({ GUI, gui }) // util.toWindow({ model, view, aimator, GUI, gui }) </script> </body> </html> <!-- 'Turtles Folder': { turtlesShape: { // chooser val: ['bug', ['bug', 'dart', 'person', 'circle', 'arrow']], cmd: val => view.drawOptions.turtlesShape = val, }, turtlesSize: { // slider val: [1, [0.5, 10, 0.5]], // val: { Small: 1, Medium: 3, Large: 5 }, cmd: val => view.drawOptions.turtlesSize = val, }, turtlesColor: { // color val: '#ff0000', cmd: val => view.drawOptions.turtlesColor = val, }, }, -->