-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtopo_axis_utils.jl
More file actions
784 lines (704 loc) · 27.6 KB
/
topo_axis_utils.jl
File metadata and controls
784 lines (704 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
"""
pixel_to_data(gui::GUI, pixel_size::Real)
Convert `pixel_size` to data widths (in x- and y-direction) in design object `gui`.
"""
function pixel_to_data(gui::GUI, pixel_size::Real)
# Calculate the range in data coordinates
vars = get_vars(gui)
x_range::Float64 = vars[:xlimits][2] - vars[:xlimits][1]
y_range::Float64 = vars[:ylimits][2] - vars[:ylimits][1]
# Get the widths of the axis
plot_widths::Vec2{Int64} = viewport(get_ax(gui, :topo).scene)[].widths
# Calculate the conversion factor
x_factor::Float64 = x_range / plot_widths[1]
y_factor::Float64 = y_range / plot_widths[2]
# Convert pixel size to data coordinates
return (pixel_size * x_factor, pixel_size * y_factor)
end
"""
update_distances!(gui::GUI)
Find the minimum distance between the elements in the design object `gui` and update `Δh` such
that neighbouring icons do not overlap.
"""
function update_distances!(gui::GUI)
min_d::Float64 = Inf
design = get_design(gui)
components = get_components(design)
if length(components) > 1
for component ∈ components
d::Float64 = minimum([
l2_norm(collect(get_xy(component)[] .- get_xy(component2)[])) for
component2 ∈ components if component != component2
])
if d < min_d
min_d = d
end
end
end
get_vars(gui)[:minimum_distance] = min_d
return new_global_delta_h(gui)
end
"""
new_global_delta_h(gui::GUI)
Recalculate the sizes of the boxes in `get_axes(gui)[:topo]` such that their size is independent
of zooming an resizing the window.
"""
function new_global_delta_h(gui::GUI)
vars = get_vars(gui)
axes = get_axes(gui)
xyWidths::Vec = axes[:topo].finallimits[].widths
plot_widths::Vec2{Int64} = viewport(axes[:topo].scene)[].widths
vars[:Δh] = maximum([
maximum(Vector(0.5 * vars[:Δh_px] * xyWidths ./ plot_widths)),
minimum([
minimum(Vector(vars[:Δh_px] * xyWidths ./ plot_widths)),
vars[:minimum_distance] / 2, # Do this to avoid overlapping squares
]),
])
end
"""
get_change(::GUI, ::Val)
Handle different keyboard inputs (events) and return changes in x, y coordinates in the
design object `gui`.
"""
get_change(::GUI, ::Val) = (0.0, 0.0)
get_change(gui::GUI, ::Val{Keyboard.up}) = (0.0, +get_var(gui, :Δh) / 5)
get_change(gui::GUI, ::Val{Keyboard.down}) = (0.0, -get_var(gui, :Δh) / 5)
get_change(gui::GUI, ::Val{Keyboard.left}) = (-get_var(gui, :Δh) / 5, 0.0)
get_change(gui::GUI, ::Val{Keyboard.right}) = (+get_var(gui, :Δh) / 5, 0.0)
"""
align(gui::GUI, align::Symbol)
Align components in `get_selected_systems(gui)` based on the value of Symbol `align`.
The following values are allowed
- `:horizontal` for horizontal alignment.
- `:vertical` for vertical alignment.
"""
function align(gui::GUI, align::Symbol)
xs::Vector{Real} = Real[]
ys::Vector{Real} = Real[]
for sub_design ∈ get_selected_systems(gui)
if isa(sub_design, EnergySystemDesign)
x, y = sub_design.xy[]
push!(xs, x)
push!(ys, y)
end
end
# Use the average of the components as the basis of the translated coordinate
z::Real = if align == :horizontal
sum(ys) / length(ys)
elseif align == :vertical
sum(xs) / length(xs)
end
for sub_design ∈ get_selected_systems(gui)
if isa(sub_design, EnergySystemDesign)
x, y = sub_design.xy[]
if align == :horizontal
sub_design.xy[] = (x, z)
elseif align == :vertical
sub_design.xy[] = (z, y)
end
end
end
end
"""
initialize_plot!(gui::GUI, design::EnergySystemDesign)
Initialize the plot of the topology of design object `gui` given an EnergySystemDesign
`design`.
"""
function initialize_plot!(gui::GUI, design::EnergySystemDesign)
for component ∈ get_components(design)
initialize_plot!(gui, component)
add_component!(gui, component)
end
return connect!(gui, design)
end
"""
plot_design!(
gui::GUI, design::EnergySystemDesign; visible::Bool=true, expand_all::Bool=true
)
Plot the topology of get_design(gui) (only if not already available), and toggle visibility
based on the optional argument `visible`.
"""
function plot_design!(
gui::GUI, design::EnergySystemDesign; visible::Bool = true, expand_all::Bool = true,
)
for component ∈ get_components(design)
component_visibility::Bool = (component == get_design(gui)) || expand_all
plot_design!(gui, component; visible = component_visibility, expand_all)
end
if get_design(gui) == design
update_distances!(gui)
end
for component ∈ get_components(design), plot ∈ component.plots
plot.visible = visible
end
for connection ∈ get_connections(design), plots ∈ get_plots(connection), plot ∈ plots[]
plot.visible = visible
end
end
"""
connect!(gui::GUI, design::EnergySystemDesign)
Draws lines between connected nodes/areas in GUI `gui` using EnergySystemDesign `design`.
"""
function connect!(gui::GUI, design::EnergySystemDesign)
# Find optimal placement of label by finding the wall that has the least number of connections
connections = get_connections(design)
components = get_components(design)
for component ∈ components
linked_to_component::Vector{Connection} = filter(
x -> get_parent(get_system(component)).id == get_element(x).to.id,
connections,
)
linked_from_component::Vector{Connection} = filter(
x -> get_parent(get_system(component)).id == get_element(x).from.id,
connections,
)
on(component.xy; priority = 4) do _
angles::Vector{Float64} = vcat(
[
angle(component, linked_component.from) for
linked_component ∈ linked_to_component
],
[
angle(component, linked_component.to) for
linked_component ∈ linked_from_component
],
)
min_angle_diff::Vector{Float64} = fill(Inf, 4)
for i ∈ eachindex(min_angle_diff)
for angle ∈ angles
Δθ = angle_difference(angle, (i - 1) * π / 2)
if min_angle_diff[i] > Δθ
min_angle_diff[i] = Δθ
end
end
end
walls::Vector{Symbol} = [:E, :N, :W, :S]
component.wall[] = walls[argmax(min_angle_diff)]
end
notify(component.xy)
end
for conn ∈ connections
# Check if link between two elements goes in both directions (two_way)
link = get_element(conn)
two_way::Bool = false
for conn2 ∈ connections
link2 = get_element(conn2)
if link2.to.id == link.from.id && link2.from.id == link.to.id
two_way = true
end
end
# Plot line for connection with decorations
connect!(gui, conn, two_way)
end
end
"""
connect!(gui::GUI, connection::Connection, two_way::Bool)
When a boolean argument `two_way` is specified, draw the lines in both directions.
"""
function connect!(gui::GUI, connection::Connection, two_way::Bool)
colors::Vector{RGB} = get_colors(connection)
no_colors::Int64 = length(colors)
# Create an arrow to highlight the direction of the energy flow
l::Float64 = 1.0 # length of the arrow
t::Float64 = 0.5 # half of the thickness of the arrow
arrow_parts::Vector{Makie.BezierPath} = Vector{Makie.BezierPath}(undef, no_colors)
for i ∈ range(1, no_colors)
arrow_parts[i] = Makie.BezierPath([
Makie.MoveTo(Makie.Point(0, 0)),
Makie.LineTo(Makie.Point(-l, t * (2 * (i - 1) / no_colors - 1))),
Makie.LineTo(Makie.Point(-l, t * (2 * i / no_colors - 1))),
Makie.ClosePath(),
])
end
# Allocate and store objects
line_connections::Observable{Vector{Any}} = Observable(Vector{Any}(undef, 0))
arrow_heads::Observable{Vector{Any}} = Observable(Vector{Any}(undef, 0))
push!(get_plots(connection), line_connections)
push!(get_plots(connection), arrow_heads)
linestyle = get_linestyle(gui, connection)
# Create function to be run on changes in connection.from and connection.to
update =
() -> begin
markersize_lengths::Tuple{Float64,Float64} = pixel_to_data(
gui, get_var(gui, :markersize),
)
xy_1::Vector{Real} = collect(connection.from.xy[])
xy_2::Vector{Real} = collect(connection.to.xy[])
for i ∈ 1:length(line_connections[])
line_connections[][i].visible = false
end
for i ∈ 1:length(arrow_heads[])
arrow_heads[][i].visible = false
end
lines_shift::Tuple{Float64,Float64} =
pixel_to_data(gui, get_var(gui, :connection_linewidth)) .+
pixel_to_data(gui, get_var(gui, :line_sep_px))
two_way_sep::Tuple{Float64,Float64} = pixel_to_data(
gui, get_var(gui, :two_way_sep_px),
)
θ::Float64 = atan(xy_2[2] - xy_1[2], xy_2[1] - xy_1[1])
cosθ::Float64 = cos(θ)
sinθ::Float64 = sin(θ)
cosϕ::Float64 = -sinθ # where ϕ = θ+π/2
sinϕ::Float64 = cosθ
Δ::Float64 = get_var(gui, :Δh) / 2 # half width of a box
if !isempty(get_components(connection.from))
Δ *= get_var(gui, :parent_scaling)
end
for j ∈ 1:no_colors
xy_start::Vector{Float64} = copy(xy_1)
xy_end::Vector{Float64} = copy(xy_2)
xy_midpoint::Vector{Float64} = copy(xy_2) # The midpoint of the end of all lines (for arrow head)
if two_way
xy_start[1] += (two_way_sep[1] / 2 + lines_shift[1] * (j - 1)) * cosϕ
xy_start[2] +=
(two_way_sep[2] / 2 + lines_shift[2] * (j - 1)) * sinϕ
xy_end[1] += (two_way_sep[1] / 2 + lines_shift[1] * (j - 1)) * cosϕ
xy_end[2] += (two_way_sep[2] / 2 + lines_shift[2] * (j - 1)) * sinϕ
xy_midpoint[1] +=
(two_way_sep[1] / 2 + lines_shift[1] * (no_colors - 1) / 2) * cosϕ
xy_midpoint[2] +=
(two_way_sep[2] / 2 + lines_shift[2] * (no_colors - 1) / 2) * sinϕ
else
xy_start[1] += lines_shift[1] * (j - 1) * cosϕ
xy_start[2] += lines_shift[2] * (j - 1) * sinϕ
xy_end[1] += lines_shift[1] * (j - 1) * cosϕ
xy_end[2] += lines_shift[2] * (j - 1) * sinϕ
xy_midpoint[1] += lines_shift[1] * (no_colors - 1) / 2 * cosϕ
xy_midpoint[2] += lines_shift[2] * (no_colors - 1) / 2 * sinϕ
end
xy_start = square_intersection(xy_1, xy_start, θ, Δ)
xy_end = square_intersection(xy_2, xy_end, θ + π, Δ)
xy_midpoint = square_intersection(xy_2, xy_midpoint, θ + π, Δ)
parm::Float64 =
-xy_start[1] * cosθ - xy_start[2] * sinθ +
xy_midpoint[1] * cosθ +
xy_midpoint[2] * sinθ - minimum(markersize_lengths)
xs::Vector{Float64} = [xy_start[1], parm * cosθ + xy_start[1]]
ys::Vector{Float64} = [xy_start[2], parm * sinθ + xy_start[2]]
if length(arrow_heads[]) < j
sctr = scatter!(
get_axes(gui)[:topo],
xy_midpoint[1],
xy_midpoint[2];
marker = arrow_parts[j],
markersize = get_var(gui, :markersize),
rotation = θ,
color = colors[j],
inspectable = false,
)
lns = lines!(
get_axes(gui)[:topo],
xs,
ys;
color = colors[j],
linewidth = get_var(gui, :connection_linewidth),
linestyle = linestyle[j],
inspector_label = (self, i, p) -> get_hover_string(connection),
inspectable = true,
)
Makie.translate!(sctr, 0, 0, get_var(gui, :z_translate_lines))
get_vars(gui)[:z_translate_lines] += 1
Makie.translate!(lns, 0, 0, get_var(gui, :z_translate_lines))
get_vars(gui)[:z_translate_lines] += 1
push!(arrow_heads[], sctr)
push!(line_connections[], lns)
else
arrow_heads[][j][1][] =
[Point{2,Float64}(xy_midpoint[1], xy_midpoint[2])]
arrow_heads[][j][:rotation] = θ
arrow_heads[][j].visible = true
line_connections[][j][1][] = [
Point{2,Float64}(x, y) for (x, y) ∈ zip(xs, ys)
]
line_connections[][j].visible = true
end
line_connections[][j].kw[:EMGUI_obj] = connection
arrow_heads[][j].kw[:EMGUI_obj] = connection
end
end
# If components changes position, so must the connections
for component ∈ [connection.from, connection.to]
on(component.xy; priority = 3) do _
if component.plots[1].visible[]
update()
end
end
end
end
"""
add_component!(gui::GUI, component::EnergySystemDesign)
Draw a box containing the icon and add a label with the id of the EnergySystemDesign
`component` with its type in parantheses.
"""
function add_component!(gui::GUI, component::EnergySystemDesign)
draw_box!(gui, component)
draw_icon!(gui, component)
return draw_label!(gui, component)
end
"""
get_linestyle(gui::GUI, design::EnergySystemDesign)
Get the line style for an EnergySystemDesign `design` based on its properties.
"""
get_linestyle(gui::GUI, design::EnergySystemDesign) = get_linestyle(gui, design.system)
function get_linestyle(gui::GUI, system::AbstractSystem)
node = get_parent(system)
if isa(node, EMB.Node) && EMI.has_investment(node)
return get_var(gui, :investment_lineStyle)
end
return :solid
end
"""
get_linestyle(gui::GUI, connection::Connection)
Get the line style for an Connection `connection` based on its properties.
"""
function get_linestyle(gui::GUI, connection::Connection)
# Check of connection is a transmission
linestyles = get_linestyle(gui, get_element(connection))
if !isempty(linestyles)
return linestyles
end
# For Links, simply use dashed style if from or to node has investments
no_lines = length(get_colors(connection))
linestyle::Union{Symbol,Makie.Linestyle} = get_linestyle(gui, connection.from)
if linestyle == get_var(gui, :investment_lineStyle)
return fill(linestyle, no_lines)
end
linestyle = get_linestyle(gui, connection.to)
if linestyle == get_var(gui, :investment_lineStyle)
return fill(linestyle, no_lines)
end
return fill(:solid, no_lines)
end
"""
get_linestyle(::GUI, ::AbstractElement)
Dispatchable function for the EnergyModelsGeography extension.
"""
function get_linestyle(::GUI, ::AbstractElement)
return []
end
"""
draw_box!(gui::GUI, design::EnergySystemDesign)
Draw a box for EnergySystemDesign `design` and it's appearance, including style, color, size.
"""
function draw_box!(gui::GUI, design::EnergySystemDesign)
linestyle::Union{Symbol,Makie.Linestyle} = get_linestyle(gui, design)
# if the design has components, draw an enlarged box around it.
if !isempty(get_components(design))
xo2::Observable{Vector{Real}} = Observable(zeros(5))
yo2::Observable{Vector{Real}} = Observable(zeros(5))
vertices2::Vector{Tuple{Real,Real}} = [
(x, y) for (x, y) ∈ zip(xo2[][1:(end-1)], yo2[][1:(end-1)])
]
white_rect2 = poly!(
get_axes(gui)[:topo], vertices2; color = :white, strokewidth = 0,
inspectable = false,
) # Create a white background rectangle to hide lines from connections
Makie.translate!(white_rect2, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
push!(design.plots, white_rect2)
# observe changes in design coordinates and update enlarged box position
on(design.xy; priority = 3) do val
x = val[1]
y = val[2]
xo2[], yo2[] = box(x, y, get_var(gui, :Δh) / 2 * get_var(gui, :parent_scaling))
white_rect2[1] = [
(x, y) for (x, y) ∈ zip(xo2[][1:(end-1)], yo2[][1:(end-1)])
]
end
box_boundary2 = lines!(
get_axes(gui)[:topo],
xo2,
yo2;
color = design.color,
linewidth = get_var(gui, :linewidth),
linestyle = :solid,
inspectable = false,
)
Makie.translate!(box_boundary2, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
push!(design.plots, box_boundary2)
box_boundary2.kw[:EMGUI_obj] = design
white_rect2.kw[:EMGUI_obj] = design
end
xo::Observable{Vector{Real}} = Observable(zeros(5))
yo::Observable{Vector{Real}} = Observable(zeros(5))
vertices::Vector{Tuple{Real,Real}} = [
(x, y) for (x, y) ∈ zip(xo[][1:(end-1)], yo[][1:(end-1)])
]
white_rect = poly!(
get_axes(gui)[:topo], vertices; color = :white, strokewidth = 0,
inspectable = false,
) # Create a white background rectangle to hide lines from connections
add_inspector_to_poly!(white_rect, (self, i, p) -> get_hover_string(design))
Makie.translate!(white_rect, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
push!(design.plots, white_rect)
# Observe changes in design coordinates and update box position
on(design.xy; priority = 3) do val
x::Real = val[1]
y::Real = val[2]
xo[], yo[] = box(x, y, get_var(gui, :Δh) / 2)
white_rect[1] = [(x, y) for (x, y) ∈ zip(xo[][1:(end-1)], yo[][1:(end-1)])]
end
box_boundary = lines!(
get_axes(gui)[:topo],
xo,
yo;
color = design.color,
linewidth = get_var(gui, :linewidth),
linestyle = linestyle,
inspectable = false,
)
Makie.translate!(box_boundary, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
push!(design.plots, box_boundary)
box_boundary.kw[:EMGUI_obj] = design
white_rect.kw[:EMGUI_obj] = design
end
"""
draw_icon!(gui::GUI, design::EnergySystemDesign)
Draw an icon for EnergySystemDesign `design`.
"""
function draw_icon!(gui::GUI, design::EnergySystemDesign)
xo::Observable{Vector{Real}} = Observable([0.0, 0.0])
yo::Observable{Vector{Real}} = Observable([0.0, 0.0])
xo_image = Observable(0.0 .. 0.0)
yo_image = Observable(0.0 .. 0.0)
on(design.xy; priority = 3) do val
x::Real = val[1]
y::Real = val[2]
xo[] = [
x - get_var(gui, :Δh) * get_var(gui, :icon_scale) / 2,
x + get_var(gui, :Δh) * get_var(gui, :icon_scale) / 2,
]
yo[] = [
y - get_var(gui, :Δh) * get_var(gui, :icon_scale) / 2,
y + get_var(gui, :Δh) * get_var(gui, :icon_scale) / 2,
]
xo_image[] = xo[][1] .. xo[][2]
yo_image[] = yo[][1] .. yo[][2]
end
if isempty(design.icon) # No path to an icon has been found
node::EMB.Node = get_ref_element(design)
colors_input::Vector{RGB} = get_resource_colors(
inputs(node), design.id_to_color_map,
)
colors_output::Vector{RGB} = get_resource_colors(
outputs(node), design.id_to_color_map,
)
geometry::Symbol = if isa(node, Source)
:rect
elseif isa(node, Sink)
:circle
else # assume NetworkNode
:triangle
end
for (j, colors) ∈ enumerate([colors_input, colors_output])
no_colors::Int64 = length(colors)
for (i, color) ∈ enumerate(colors)
θᵢ::Float64 = 0
θᵢ₊₁::Float64 = 0
# Check if node is a NetworkNode (if so, devide disc into two where
# left side is for input and right side is for output)
if isa(node, NetworkNode)
θᵢ = (-1)^(j + 1) * π / 2 + π * (i - 1) / no_colors
θᵢ₊₁ = (-1)^(j + 1) * π / 2 + π * i / no_colors
else
θᵢ = 2π * (i - 1) / no_colors
θᵢ₊₁ = 2π * i / no_colors
end
sector = get_sector_points()
network_poly = poly!(
get_axes(gui)[:topo], sector; color = color, inspectable = false,
)
if isa(node, Sink) || isa(node, Source)
add_inspector_to_poly!(
network_poly, (self, i, p) -> get_hover_string(design),
)
end
Makie.translate!(network_poly, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
network_poly.kw[:EMGUI_obj] = design
push!(design.plots, network_poly)
on(design.xy; priority = 3) do c
Δ = get_var(gui, :Δh) * get_var(gui, :icon_scale) / 2
sector =
get_sector_points(; c, Δ, θ₁ = θᵢ, θ₂ = θᵢ₊₁, geometry = geometry)
network_poly[1][] = sector
end
end
end
if isa(node, NetworkNode)
# Add a vertical white separation line to distinguis input resources from output resources
center_box = lines!(
get_axes(gui)[:topo],
zeros(4),
zeros(4);
color = :black,
linewidth = get_var(gui, :linewidth),
inspectable = false,
)
Makie.translate!(center_box, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
center_box.kw[:EMGUI_obj] = design
push!(design.plots, center_box)
on(design.xy; priority = 3) do center
radius = get_var(gui, :Δh) * get_var(gui, :icon_scale) / 2
x_coords, y_coords = box(center[1], center[2], radius / 4)
center_box[1][] = Vector{Point{2,Float64}}([
[x, y] for (x, y) ∈ zip(x_coords, y_coords)
])
end
end
else
@debug "$(design.icon)"
icon_image = image!(
get_axes(gui)[:topo],
xo_image,
yo_image,
rotr90(FileIO.load(design.icon));
inspectable = false,
)
Makie.translate!(icon_image, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
icon_image.kw[:EMGUI_obj] = design
push!(design.plots, icon_image)
end
end
"""
draw_label!(gui::GUI, component::EnergySystemDesign)
Add a label to an `EnergySystemDesign` component.
"""
function draw_label!(gui::GUI, component::EnergySystemDesign)
xo = Observable(0.0)
yo = Observable(0.0)
alignment = Observable((:left, :top))
scale = 0.7
on(component.xy; priority = 3) do val
x = val[1]
y = val[2]
if component.wall[] == :E
xo[] = x + get_var(gui, :Δh) * scale
yo[] = y
elseif component.wall[] == :S
xo[] = x
yo[] = y - get_var(gui, :Δh) * scale
elseif component.wall[] == :W
xo[] = x - get_var(gui, :Δh) * scale
yo[] = y
elseif component.wall[] == :N
xo[] = x
yo[] = y + get_var(gui, :Δh) * scale
end
alignment[] = get_text_alignment(component.wall[])
end
node = get_element(component)
if has_invested(component)
font_color = :red
else
font_color = :black
end
label_text = text!(
get_axes(gui)[:topo],
xo,
yo;
text = get_element_label(node),
align = alignment,
fontsize = get_var(gui, :fontsize),
inspectable = false,
color = font_color,
)
Makie.translate!(label_text, 0, 0, get_var(gui, :z_translate_components))
get_vars(gui)[:z_translate_components] += 1
label_text.kw[:EMGUI_obj] = component
push!(get_plots(component), label_text)
end
"""
get_element_label(element)
Get the label of the element based on its `id` field. If the `id` is a number it returns the
built in Base.display() functionality of node, otherwise, the `id` field is converted to a string.
"""
function get_element_label(element::AbstractGUIObj)
return get_element_label(get_element(element))
end
function get_element_label(element::EMB.Node)
return isa(element.id, Number) ? string(element) : string(element.id)
end
function get_element_label(element::EMB.Link)
return get_element_label(element.from) * "-" * get_element_label(element.to)
end
"""
adjust_limits!(gui::GUI)
Adjust the limits of get_axes(gui)[:topo] based on its content.
"""
function adjust_limits!(gui::GUI)
vars = get_vars(gui)
min_x, max_x, min_y, max_y = find_min_max_coordinates(get_design(gui))
Δ_lim_x = max_x - min_x
Δ_lim_y = max_y - min_y
boundary_add = vars[:boundary_add]
min_x -= Δ_lim_x * boundary_add
max_x += Δ_lim_x * boundary_add
min_y -= Δ_lim_y * boundary_add
max_y += Δ_lim_y * boundary_add
Δ_lim_x = max_x - min_x
Δ_lim_y = max_y - min_y
x_center = (min_x + max_x) / 2
y_center = (min_y + max_y) / 2
if Δ_lim_y > Δ_lim_x
Δ_lim_x = Δ_lim_y * vars[:ax_aspect_ratio]
else
Δ_lim_y < Δ_lim_x
Δ_lim_y = Δ_lim_x / vars[:ax_aspect_ratio]
end
min_x = x_center - Δ_lim_x / 2
max_x = x_center + Δ_lim_x / 2
min_y = y_center - Δ_lim_y / 2
max_y = y_center + Δ_lim_y / 2
if min_x ≈ max_x
min_x -= boundary_add
max_x += boundary_add
end
if min_y ≈ max_y
min_y -= boundary_add
max_y += boundary_add
end
vars[:xlimits] = [min_x, max_x]
vars[:ylimits] = [min_y, max_y]
ax = get_ax(gui, :topo)
limits!(ax, vars[:xlimits], vars[:ylimits])
end
"""
update_title!(gui::GUI)
Update the title of `get_axes(gui)[:topo]` based on `get_design(gui)`.
"""
function update_title!(gui::GUI)
design = get_design(gui)
system = get_system(design)
parent = get_parent(system)
get_var(gui, :title)[] = if isa(parent, NothingElement)
"top_level"
else
"top_level.$(parent)"
end
end
"""
get_hover_string(obj::AbstractGUIObj)
Return the string for a EMB.Node/Area/Link/Transmission to be shown on hovering.
"""
function get_hover_string(obj::AbstractGUIObj)
element = get_element(obj)
label = get_element_label(element)
inv_times = get_inv_times(obj)
inv_str = "$label ($(nameof(typeof(element))))"
if !isempty(inv_times)
capex = get_capex(obj)
label = get_element_label(obj)
for (t, capex) ∈ zip(inv_times, capex)
inv_str *= "\n\t$t: $(format_number(capex))"
end
end
return inv_str
end