Skip to content

Commit

Permalink
Merge pull request #100 from JuliaRobotics/tk/usernode
Browse files Browse the repository at this point in the history
Add user node to GUI.
  • Loading branch information
tkoolen authored Jan 22, 2019
2 parents b6bb7e5 + 8de1a0f commit 216aef6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/visualization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ using DocStringExtensions
"""

import MeshCatMechanisms
import WebIO

using Printf: @sprintf
using DiffEqBase: DiscreteCallback, ODESolution, CallbackSet, u_modified!, terminate!
using RigidBodyDynamics: Mechanism, MechanismState, normalize_configuration!, configuration
using MeshCatMechanisms: setanimation!
using Observables: Observable
using InteractBase: Widget, button, observe
using WebIO: render, node
using WebIO: render, node, Node
using Blink: Window, body!, title
using CSSUtil: vbox

Expand Down Expand Up @@ -107,7 +108,7 @@ The controls can be displayed in a standalone window using `open(controls, Blink
SimulationControls() = SimulationControls(button(""), button(""), Observable(HTML("0.0")))

# Icons taken from the freely available set at https://icons8.com/color-icons/
function render_default(controls::SimulationControls)
function WebIO.render(controls::SimulationControls)
node(:div,
node(:style, """
.rigidbodysim-controls button {
Expand Down Expand Up @@ -154,7 +155,7 @@ end
function Base.open(controls::SimulationControls, window::Window)
size(window, 300, 55)
title(window, "RigidBodySim controls")
body!(window, render_default(controls))
body!(window, render(controls))
nothing
end

Expand All @@ -172,31 +173,37 @@ CallbackSet(controls::SimulationControls) = CommandHandler(SimulationStatus(cont
struct GUI
visualizer::MechanismVisualizer
controls::SimulationControls
usernode::Union{Node, Nothing}
end

"""
Create a new RigidBodySim graphical user interface from a `MeshCatMechanisms.MechanismVisualizer`.
Use `open(gui)` to open the GUI in a standalone window.
"""
GUI(visualizer::MechanismVisualizer) = GUI(visualizer, SimulationControls())
GUI(visualizer::MechanismVisualizer; usernode=nothing) = GUI(visualizer, SimulationControls(), usernode)

"""
Create a new RigidBodySim graphical user interface for the given Mechanism.
All arguments are passed on to the `MeshCatMechanisms.MechanismVisualizer` constructor.
Use `open(gui)` to open the GUI in a standalone window.
"""
GUI(mechanism::Mechanism, args...) = GUI(MechanismVisualizer(mechanism, args...))
GUI(mechanism::Mechanism, args...; usernode=nothing) = GUI(MechanismVisualizer(mechanism, args...); usernode=usernode)

function Base.open(gui::GUI, window::Window)
title(window, "RigidBodySim")
body!(window, vbox(render_default(gui.controls), gui.visualizer.visualizer.core))
body = vbox(
WebIO.render(gui.controls),
WebIO.iframe(gui.visualizer.visualizer.core),
WebIO.render(gui.usernode)
)
body!(window, body)
wait(gui)
nothing
end

Base.open(gui::GUI) = open(gui, Window())
Base.open(gui::GUI) = (window = Window(); open(gui, window); window)
Base.wait(gui::GUI) = wait(gui.visualizer)

"""
Expand Down

0 comments on commit 216aef6

Please sign in to comment.