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

Commit

Permalink
Annotations (#268)
Browse files Browse the repository at this point in the history
* fix annotation pushing

* move convert_arguments behind Annotations type definition it depends on

* clean up

Co-authored-by: jkrumbiegel <[email protected]>
  • Loading branch information
SimonDanisch and jkrumbiegel authored Dec 23, 2019
1 parent 9c597b9 commit ec3deb5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
30 changes: 13 additions & 17 deletions src/basic_recipes/basic_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,40 +380,37 @@ $(ATTRIBUTES)
default_theme(scene, Text)
end

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

function plot!(plot::Annotations)
position = plot[2]
sargs = (
plot[:model], plot[:font],
plot[1], position,
plot[1],
getindex.(plot, (:color, :textsize, :align, :rotation))...,
)
N = to_value(position) |> eltype |> length
atlas = get_texture_atlas()
combinedpos = [Point3f0(0)]; colors = RGBAf0[RGBAf0(0,0,0,0)]
scales = Vec2f0[(0,0)]; fonts = NativeFont[to_font("Dejavu Sans")]
combinedpos = [Point3f0(0)]
colors = RGBAf0[RGBAf0(0,0,0,0)]
scales = Vec2f0[(0,0)]
fonts = NativeFont[to_font("Dejavu Sans")]
rotations = Quaternionf0[Quaternionf0(0,0,0,0)]

tplot = text!(plot, "",
align = Vec2f0(0), model = Mat4f0(I),
position = combinedpos, color = colors,
textsize = scales, font = fonts, rotation = rotations
).plots[end]

onany(sargs...) do model, font, args...
if length(args[1]) != length(args[2])
error("For each text annotation, there needs to be one position. Found: $(length(t)) strings and $(length(p)) positions")
end
onany(sargs...) do model, font, text_pos, args...
io = IOBuffer();
empty!(combinedpos); empty!(colors); empty!(scales); empty!(fonts); empty!(rotations)

broadcast_foreach(1:length(args[1]), args...) do idx, text, startpos, color, tsize, alignment, rotation
broadcast_foreach(1:length(text_pos), text_pos, args...) do idx, (text, startpos), color, tsize, alignment, rotation
# the fact, that Font == Vector{FT_FreeType.Font} is pretty annoying for broadcasting.
# TODO have a better Font type!
f = to_font(font)
Expand All @@ -436,7 +433,6 @@ function plot!(plot::Annotations)
tplot[:color] = colors
tplot[:rotation] = rotations
# fonts shouldn't need an update, since it will get udpated when listening on string
#
return
end
# update one time in the beginning, since otherwise the above won't run
Expand Down
19 changes: 12 additions & 7 deletions src/basic_recipes/buffers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function TextBuffer(
end

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


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

function append!(tb::Annotations, text::Vector{String}, positions::Vector{Point{N, Float32}}; kw_args...) where N
append!(tb[1][], text)
append!(tb[2][], positions)
text_positions = convert_argument(Annotations, text, positions)[1]
append!(tb, text_positions; kw_args...)
return
end

function append!(tb::Annotations, text_positions::Vector{Tuple{String, Point{N, Float32}}}; kw_args...) where N
append!(tb[1][], text_positions)
kw = Dict(kw_args)
for key in (:color, :rotation, :textsize, :font, :align)
val = get(kw, key) do
isempty(tb[key][]) && error("please provide default for $key")
last(tb[key][])
end
val_vec = same_length_array(text, val, Key{key}())
val_vec = same_length_array(text_positions, val, Key{key}())
append!(tb[key][], val_vec)
end
return
Expand Down
6 changes: 6 additions & 0 deletions src/interaction/nodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ function map_once(
lift(f, input, inputrest..., init = init, typ = typ)
end

"""
Pushes an updates to all listeners of `node`
"""
function notify!(node::Node)
node[] = node[]
end
#
# call_count = Ref(1)
# io = IOBuffer()
Expand Down

0 comments on commit ec3deb5

Please sign in to comment.