Skip to content

Commit 2d8969d

Browse files
author
Brad Carman
committed
working move node feature
1 parent 9c13ba2 commit 2d8969d

File tree

1 file changed

+41
-99
lines changed

1 file changed

+41
-99
lines changed

src/ModelingToolkitDesigner.jl

Lines changed: 41 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,6 @@ using ModelingToolkit
44
using FilterHelpers
55
using FileIO
66

7-
#=
8-
TODO:
9-
10-
- scale the labels
11-
- add this repo to my GitHub account
12-
=#
13-
14-
15-
# struct Parameter
16-
17-
# end
18-
19-
# struct State
20-
21-
# end
22-
23-
# struct Connector
24-
# type::Symbol
25-
# color::Symbol
26-
# wall::Symbol
27-
# end
287

298
const Δh = 0.05
309

@@ -152,10 +131,10 @@ function Base.isapprox(a::Tuple{Float64, Float64}, b::Tuple{Float64, Float64}; a
152131
end
153132

154133
get_change(::Val) = (0.0, 0.0)
155-
get_change(::Val{Keyboard.up}) = (0.0, +0.01)
156-
get_change(::Val{Keyboard.down}) = (0.0, -0.01)
157-
get_change(::Val{Keyboard.left}) = (-0.01, 0.0)
158-
get_change(::Val{Keyboard.right}) = (+0.01, 0.0)
134+
get_change(::Val{Keyboard.up}) = (0.0, +Δh/5)
135+
get_change(::Val{Keyboard.down}) = (0.0, -Δh/5)
136+
get_change(::Val{Keyboard.left}) = (-Δh/5, 0.0)
137+
get_change(::Val{Keyboard.right}) = (+Δh/5, 0.0)
159138

160139
function view(model::ODESystemDesign)
161140

@@ -178,10 +157,10 @@ function view(model::ODESystemDesign)
178157

179158
for system in model.systems
180159
add_system(system)
181-
notify(system.xy)
182160
for connector in system.connectors
183161
notify(connector.wall)
184162
end
163+
notify(system.xy)
185164
end
186165

187166
for connection in model.connections
@@ -397,7 +376,7 @@ get_text_alignment(wall::Symbol) = get_text_alignment(Val(wall))
397376
get_text_alignment(::Val{:E}) = (:left, :top)
398377
get_text_alignment(::Val{:W}) = (:right, :top)
399378
get_text_alignment(::Val{:S}) = (:left, :top)
400-
get_text_alignment(::Val{:N}) = (:right, :top)
379+
get_text_alignment(::Val{:N}) = (:right, :bottom)
401380

402381

403382
get_color(system_design::ODESystemDesign) = get_color(system_design.system)
@@ -469,19 +448,41 @@ end
469448

470449
function draw_nodes(system::ODESystemDesign)
471450

451+
xo = Observable(0.0)
452+
yo = Observable(0.0)
453+
454+
on(system.xy) do val
455+
456+
x = val[1]
457+
y = val[2]
458+
459+
xo[] = x
460+
yo[] = y
461+
462+
end
463+
464+
update = (connector) -> begin
465+
println("updating node: $(system.system.name) $(connector.system.name)")
466+
connectors_on_wall = filter(x->x.wall[] == connector.wall[], system.connectors)
467+
468+
n_items = length(connectors_on_wall)
469+
delta = 2*Δh/(n_items+1)
470+
471+
for i=1:n_items
472+
x,y = get_node_position(connector.wall[], delta, i)
473+
connectors_on_wall[i].xy[] = (x+xo[],y+yo[])
474+
end
475+
end
476+
477+
472478
for connector in system.connectors
473479

474480
on(connector.wall) do val
475-
println("updating node: $(system.system.name) $(connector.system.name)")
476-
connectors_on_wall = filter(x->x.wall[] == val, system.connectors)
477-
478-
n_items = length(connectors_on_wall)
479-
delta = 2*Δh/(n_items+1)
481+
update(connector)
482+
end
480483

481-
for i=1:n_items
482-
x,y = get_node_position(val, delta, i)
483-
connectors_on_wall[i].xy[] = (x,y)
484-
end
484+
on(system.xy) do val
485+
update(connector)
485486
end
486487

487488
draw_node(connector)
@@ -532,16 +533,16 @@ get_node_position(w::Symbol, delta, i) = get_node_position(Val(w), delta, i)
532533
get_node_label_position(w::Symbol, x, y) = get_node_label_position(Val(w), x, y)
533534

534535
get_node_position(::Val{:N}, delta, i) = (delta*i - Δh, +Δh)
535-
get_node_label_position(::Val{:N}, x, y) = (x, y*0.6)
536+
get_node_label_position(::Val{:N}, x, y) = (x, y+Δh/5)
536537

537538
get_node_position(::Val{:S}, delta, i) = (delta*i - Δh, -Δh)
538-
get_node_label_position(::Val{:S}, x, y) = (x, y*0.6)
539+
get_node_label_position(::Val{:S}, x, y) = (x, y-Δh/5)
539540

540541
get_node_position(::Val{:E}, delta, i) = (+Δh, delta*i - Δh)
541-
get_node_label_position(::Val{:E}, x, y) = (x*1.4, y)
542+
get_node_label_position(::Val{:E}, x, y) = (x+Δh/5, y)
542543

543544
get_node_position(::Val{:W}, delta, i) = (-Δh, delta*i - Δh)
544-
get_node_label_position(::Val{:W}, x, y) = (x*1.4, y)
545+
get_node_label_position(::Val{:W}, x, y) = (x-Δh/5, y)
545546

546547

547548

@@ -556,65 +557,6 @@ function add_system(system::ODESystemDesign)
556557

557558
end
558559

559-
function draw_node(connector::ODESystemDesign, delta, i, w, xo, yo)
560-
if w == :N
561-
x = delta*i - Δh
562-
xt = x
563-
y = +Δh
564-
yt = y*0.6
565-
end
566-
567-
if w == :S
568-
x = delta*i - Δh
569-
xt = x
570-
y = -Δh
571-
yt = y*0.6
572-
end
573-
574-
if w == :E
575-
x = +Δh
576-
xt = x*1.4
577-
y = delta*i - Δh
578-
yt = y
579-
end
580-
581-
if w == :W
582-
x = -Δh
583-
xt = x*1.4
584-
y = delta*i - Δh
585-
yt = y
586-
end
587-
588-
xpo = Observable(0.0)
589-
on(xo) do val
590-
xpo[] = val + x
591-
connector.xy[] = (xpo[], connector.xy[][2])
592-
end
593-
594-
ypo = Observable(0.0)
595-
on(yo) do val
596-
ypo[] = val + y
597-
connector.xy[] = (connector.xy[][1], ypo[])
598-
end
599-
600-
xto = Observable(0.0)
601-
on(xo) do val
602-
xto[] = val + xt
603-
end
604-
605-
yto = Observable(0.0)
606-
on(yo) do val
607-
yto[] = val + yt
608-
end
609-
610-
611-
612-
scatter!(ax[], xpo, ypo; marker=:rect, color = connector.color, markersize=15)
613-
text!(ax[], xto, yto; text=string(connector.system.name), color = get_color(connector), align=get_text_alignment(w), fontsize=10)
614-
615-
end
616-
617-
618560
# box(model::ODESystemDesign, Δh = 0.05) = box(model.xy[][1], model.xy[],[2] Δh)
619561

620562
function box(x, y, Δh = 0.05)

0 commit comments

Comments
 (0)