Skip to content
This repository was archived by the owner on Jul 13, 2021. It is now read-only.

Commit ec3deb5

Browse files
Annotations (#268)
* fix annotation pushing * move convert_arguments behind Annotations type definition it depends on * clean up Co-authored-by: jkrumbiegel <[email protected]>
1 parent 9c597b9 commit ec3deb5

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

src/basic_recipes/basic_recipes.jl

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -380,40 +380,37 @@ $(ATTRIBUTES)
380380
default_theme(scene, Text)
381381
end
382382

383-
"""
384-
Pushes an updates to all listeners of `node`
385-
"""
386-
function notify!(node::Node)
387-
node[] = node[]
383+
function convert_arguments(::Type{<: Annotations},
384+
strings::AbstractVector{<: AbstractString},
385+
text_positions::AbstractVector{<: Point{N}}) where N
386+
return (map(strings, text_positions) do str, pos
387+
(String(str), Point{N, Float32}(pos))
388+
end,)
388389
end
389390

390391
function plot!(plot::Annotations)
391-
position = plot[2]
392392
sargs = (
393393
plot[:model], plot[:font],
394-
plot[1], position,
394+
plot[1],
395395
getindex.(plot, (:color, :textsize, :align, :rotation))...,
396396
)
397-
N = to_value(position) |> eltype |> length
398397
atlas = get_texture_atlas()
399-
combinedpos = [Point3f0(0)]; colors = RGBAf0[RGBAf0(0,0,0,0)]
400-
scales = Vec2f0[(0,0)]; fonts = NativeFont[to_font("Dejavu Sans")]
398+
combinedpos = [Point3f0(0)]
399+
colors = RGBAf0[RGBAf0(0,0,0,0)]
400+
scales = Vec2f0[(0,0)]
401+
fonts = NativeFont[to_font("Dejavu Sans")]
401402
rotations = Quaternionf0[Quaternionf0(0,0,0,0)]
402403

403404
tplot = text!(plot, "",
404405
align = Vec2f0(0), model = Mat4f0(I),
405406
position = combinedpos, color = colors,
406407
textsize = scales, font = fonts, rotation = rotations
407408
).plots[end]
408-
409-
onany(sargs...) do model, font, args...
410-
if length(args[1]) != length(args[2])
411-
error("For each text annotation, there needs to be one position. Found: $(length(t)) strings and $(length(p)) positions")
412-
end
409+
onany(sargs...) do model, font, text_pos, args...
413410
io = IOBuffer();
414411
empty!(combinedpos); empty!(colors); empty!(scales); empty!(fonts); empty!(rotations)
415412

416-
broadcast_foreach(1:length(args[1]), args...) do idx, text, startpos, color, tsize, alignment, rotation
413+
broadcast_foreach(1:length(text_pos), text_pos, args...) do idx, (text, startpos), color, tsize, alignment, rotation
417414
# the fact, that Font == Vector{FT_FreeType.Font} is pretty annoying for broadcasting.
418415
# TODO have a better Font type!
419416
f = to_font(font)
@@ -436,7 +433,6 @@ function plot!(plot::Annotations)
436433
tplot[:color] = colors
437434
tplot[:rotation] = rotations
438435
# fonts shouldn't need an update, since it will get udpated when listening on string
439-
#
440436
return
441437
end
442438
# update one time in the beginning, since otherwise the above won't run

src/basic_recipes/buffers.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function TextBuffer(
6565
end
6666

6767
function start!(tb::Annotations)
68-
for key in (1, 2, :color, :rotation, :textsize, :font, :align)
68+
for key in (1, :color, :rotation, :textsize, :font, :align)
6969
resize!(tb[key][], 0)
7070
end
7171
return
@@ -76,28 +76,33 @@ function finish!(tb::Annotations)
7676
# now update all callbacks
7777
# TODO this is a bit shaky, buuuuhut, in theory the whole lift(color, ...)
7878
# in basic_recipes annotations should depend on all signals here, so updating one should be enough
79-
if length(tb[1][]) != length(tb[2][]) || length(tb[1][]) != length(tb[:textsize][])
79+
if length(tb[1][]) != length(tb.textsize[])
8080
error("Inconsistent buffer state for $(tb[1][])")
8181
end
82-
tb[1][] = tb[1][]
82+
notify!(tb[1])
8383
return
8484
end
8585

8686

8787
function push!(tb::Annotations, text::String, position::NVec{N}; kw_args...) where N
88-
append!(tb, [text], Point{N, Float32}[position]; kw_args...)
88+
append!(tb, [(String(text), Point{N, Float32}(position))]; kw_args...)
8989
end
9090

9191
function append!(tb::Annotations, text::Vector{String}, positions::Vector{Point{N, Float32}}; kw_args...) where N
92-
append!(tb[1][], text)
93-
append!(tb[2][], positions)
92+
text_positions = convert_argument(Annotations, text, positions)[1]
93+
append!(tb, text_positions; kw_args...)
94+
return
95+
end
96+
97+
function append!(tb::Annotations, text_positions::Vector{Tuple{String, Point{N, Float32}}}; kw_args...) where N
98+
append!(tb[1][], text_positions)
9499
kw = Dict(kw_args)
95100
for key in (:color, :rotation, :textsize, :font, :align)
96101
val = get(kw, key) do
97102
isempty(tb[key][]) && error("please provide default for $key")
98103
last(tb[key][])
99104
end
100-
val_vec = same_length_array(text, val, Key{key}())
105+
val_vec = same_length_array(text_positions, val, Key{key}())
101106
append!(tb[key][], val_vec)
102107
end
103108
return

src/interaction/nodes.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ function map_once(
8686
lift(f, input, inputrest..., init = init, typ = typ)
8787
end
8888

89+
"""
90+
Pushes an updates to all listeners of `node`
91+
"""
92+
function notify!(node::Node)
93+
node[] = node[]
94+
end
8995
#
9096
# call_count = Ref(1)
9197
# io = IOBuffer()

0 commit comments

Comments
 (0)