Skip to content

Commit

Permalink
clean up transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Sep 27, 2023
1 parent ccc4acd commit bf2e25b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
6 changes: 3 additions & 3 deletions MakieCore/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Attributes
end

mutable struct Combined{Typ, T} <: ScenePlot{Typ}
transformation::Transformable
transformation::Union{Nothing, Transformable}

# Unprocessed arguments directly from the user command e.g. `plot(args...; kw...)``
kw::Dict{Symbol,Any}
Expand All @@ -65,8 +65,8 @@ mutable struct Combined{Typ, T} <: ScenePlot{Typ}
deregister_callbacks::Vector{Observables.ObserverFunction}
parent::Union{AbstractScene,Combined}

function Combined{Typ,T}(transformation, kw::Dict{Symbol, Any}, args::Vector{Any}) where {Typ,T}
return new{Typ,T}(transformation, kw, args, (), Attributes(), Combined[],
function Combined{Typ,T}(kw::Dict{Symbol, Any}, args::Vector{Any}, converted::NTuple{N, Observable}) where {Typ,T,N}
return new{Typ,T}(nothing, kw, args, converted, Attributes(), Combined[],
Observables.ObserverFunction[])
end
end
Expand Down
6 changes: 6 additions & 0 deletions src/basic_recipes/plotspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ function apply_convert!(P, ::Attributes, x::AbstractVector{<:PlotSpec})
return (PlotList, (x,))
end

"""
apply for return type
(args...,)
"""
apply_convert!(P, ::Attributes, x::Tuple) = (P, x)

function MakieCore.argtypes(plot::PlotSpec{P}) where {P}
args_converted = convert_arguments(P, plot.args...)
return MakieCore.argtypes(args_converted)
Expand Down
60 changes: 18 additions & 42 deletions src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ function convert_arguments!(plot::Combined{F}) where {F}
return
end


function Combined{Func}(args::Tuple, plot_attributes::Dict) where {Func}
if first(args) isa Attributes
merge!(plot_attributes, attributes(first(args)))
Expand All @@ -133,24 +132,12 @@ function Combined{Func}(args::Tuple, plot_attributes::Dict) where {Func}
kw = [Pair(k, to_value(v)) for (k, v) in plot_attributes if k in used_attrs]
args_converted = convert_arguments(P, map(to_value, args)...; kw...)
PNew, converted = apply_convert!(P, Attributes(), args_converted)
trans = get!(plot_attributes, :transformation, automatic)
transval = to_value(trans)
transformation = if transval isa Automatic
Transformation()
elseif transval isa Transformation
transval
else
t = Transformation()
transform!(t, transval)
t
end

obs_args = Any[convert(Observable, x) for x in args]

ArgTyp = MakieCore.argtypes(converted)
plot = Combined{plotfunc(PNew), ArgTyp}(transformation, plot_attributes, obs_args)
plot.converted = map(Observable, converted)
plot.model = transformationmatrix(transformation)
converted_obs = map(Observable, converted)
plot = Combined{plotfunc(PNew),ArgTyp}(plot_attributes, obs_args, converted_obs)
return plot
end

Expand Down Expand Up @@ -178,23 +165,6 @@ Usage:
"""
used_attributes(PlotType, args...) = ()

"""
apply for return type
(args...,)
"""
apply_convert!(P, ::Attributes, x::Tuple) = (P, x)

function seperate_tuple(args::Observable{<: NTuple{N, Any}}) where N
ntuple(N) do i
lift(args) do x
if i <= length(x)
x[i]
else
error("You changed the number of arguments. This isn't allowed!")
end
end
end
end

## generic definitions
# If the Combined has no plot func, calculate them
Expand Down Expand Up @@ -231,8 +201,8 @@ function convert_arguments(P::PlotFunc, args...)
end
```
"""
plottype(P1::Type{<: Combined{Any}}, P2::Type{<: Combined{T}}) where T = P2
plottype(P1::Type{<: Combined{T}}, P2::Type{<: Combined}) where T = P1
plottype(::Type{<: Combined{Any}}, P::Type{<: Combined{T}}) where T = P
plottype(P::Type{<: Combined{T}}, ::Type{<: Combined}) where T = P

# all the plotting functions that get a plot type
const PlotFunc = Union{Type{Any},Type{<:AbstractPlot}}
Expand All @@ -245,16 +215,22 @@ end

function connect_plot!(scene::SceneLike, plot::Combined{F}) where {F}
plot.parent = scene
# TODO, move transformation into attributes?
# This hacks around transformation being already constructed in the constructor
# So here we don't want to connect to the scene if an explicit Transformation was passed to the plot
kw = getfield(plot, :kw)
attr = getfield(plot, :attributes)
t = to_value(get(() -> get(kw, :transformation, nothing), attr, :transformation))
if t isa Automatic

apply_theme!(parent_scene(scene), plot)
t_user = to_value(get(attributes(plot), :transformation, automatic))
if t_user isa Transformation
plot.transformation = t_user
else
if t_user isa Automatic
plot.transformation = Transformation()
else
t = Transformation()
transform!(t, t_user)
plot.transformation = t
end
connect!(transformation(scene), transformation(plot))
end
apply_theme!(parent_scene(scene), plot)
plot.model = transformationmatrix(plot)
convert_arguments!(plot)
calculated_attributes!(Combined{F}, plot)
plot!(plot)
Expand Down

0 comments on commit bf2e25b

Please sign in to comment.