forked from MakieOrg/Makie.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.jl
281 lines (245 loc) · 9.08 KB
/
interfaces.jl
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
function add_cycle_attribute!(plot::Plot, scene::Scene, cycle=get_cycle_for_plottype(plot.cycle[]))
cycler = scene.cycler
palette = scene.theme.palette
add_cycle_attributes!(plot, cycle, cycler, palette)
return
end
function color_and_colormap!(plot, colors = plot.color)
scene = parent_scene(plot)
if !isnothing(scene) && haskey(plot, :cycle)
add_cycle_attribute!(plot, scene)
end
colors = assemble_colors(colors[], colors, plot)
attributes(plot.attributes)[:calculated_colors] = colors
end
function calculated_attributes!(::Type{<: AbstractPlot}, plot)
scene = parent_scene(plot)
if !isnothing(scene) && haskey(plot, :cycle)
add_cycle_attribute!(plot, scene)
end
end
function calculated_attributes!(::Type{<: Mesh}, plot)
mesha = lift(GeometryBasics.attributes, plot, plot.mesh)
color = haskey(mesha[], :color) ? lift(x-> x[:color], plot, mesha) : plot.color
color_and_colormap!(plot, color)
return
end
function calculated_attributes!(::Type{<: Union{Heatmap, Image}}, plot)
color_and_colormap!(plot, plot[3])
end
function calculated_attributes!(::Type{<: Surface}, plot)
colors = plot[3]
if haskey(plot, :color)
color = plot[:color][]
if isa(color, AbstractMatrix) && !(color === to_value(colors))
colors = plot[:color]
end
end
color_and_colormap!(plot, colors)
end
function calculated_attributes!(::Type{<: MeshScatter}, plot)
color_and_colormap!(plot)
return
end
function calculated_attributes!(::Type{<:Volume}, plot)
color_and_colormap!(plot, plot[4])
return
end
function calculated_attributes!(::Type{<:Text}, plot)
color_and_colormap!(plot)
return
end
function calculated_attributes!(::Type{<: Scatter}, plot)
# calculate base case
color_and_colormap!(plot)
replace_automatic!(plot, :marker_offset) do
# default to middle
return lift(plot, plot[:markersize]) do msize
return to_2d_scale(map(x -> x .* -0.5f0, msize))
end
end
replace_automatic!(plot, :markerspace) do
lift(plot, plot.markersize) do ms
if ms isa Pixel || (ms isa AbstractVector && all(x-> ms isa Pixel, ms))
return :pixel
else
return :data
end
end
end
end
function calculated_attributes!(::Type{T}, plot) where {T<:Union{Lines, LineSegments}}
pos = plot[1][]
# extend one color/linewidth per linesegment to be one (the same) color/linewidth per vertex
if T <: LineSegments
for attr in [:color, :linewidth]
# taken from @edljk in PR #77
if haskey(plot, attr) && isa(plot[attr][], AbstractVector) && (length(pos) ÷ 2) == length(plot[attr][])
# TODO, this is actually buggy for `plot.color = new_colors`, since we're overwriting the origin color input
attributes(plot.attributes)[attr] = lift(plot, plot[attr]) do cols
map(i -> cols[(i + 1) ÷ 2], 1:(length(cols) * 2))
end
end
end
end
color_and_colormap!(plot)
return
end
const atomic_functions = (
text, meshscatter, scatter, mesh, linesegments,
lines, surface, volume, heatmap, image
)
const Atomic{Arg} = Union{map(x-> Plot{x, Arg}, atomic_functions)...}
function convert_arguments!(plot::Plot{F}) where {F}
P = Plot{F,Any}
function on_update(kw, args...)
nt = convert_arguments(P, args...; kw...)
pnew, converted = apply_convert!(P, plot.attributes, nt)
@assert plotfunc(pnew) === F "Changed the plot type in convert_arguments. This isn't allowed!"
for (obs, new_val) in zip(plot.converted, converted)
obs[] = new_val
end
end
used_attrs = used_attributes(P, to_value.(plot.args)...)
convert_keys = intersect(used_attrs, keys(plot.attributes))
kw_signal = if isempty(convert_keys)
# lift(f) isn't supported so we need to catch the empty case
Observable(())
else
# Remove used attributes from `attributes` and collect them in a `Tuple` to pass them more easily
lift((args...) -> Pair.(convert_keys, args), plot, pop!.(plot.attributes, convert_keys)...)
end
onany(on_update, plot, kw_signal, plot.args...)
return
end
function Plot{Func}(args::Tuple, plot_attributes::Dict) where {Func}
if !isempty(args) && first(args) isa Attributes
merge!(plot_attributes, attributes(first(args)))
return Plot{Func}(Base.tail(args), plot_attributes)
end
P = Plot{Func}
used_attrs = used_attributes(P, to_value.(args)...)
if used_attrs === ()
args_converted = convert_arguments(P, map(to_value, args)...)
else
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...)
end
PNew, converted = apply_convert!(P, Attributes(), args_converted)
obs_args = Any[convert(Observable, x) for x in args]
ArgTyp = MakieCore.argtypes(converted)
converted_obs = map(Observable, converted)
FinalPlotFunc = plotfunc(plottype(PNew, converted...))
return Plot{FinalPlotFunc,ArgTyp}(plot_attributes, obs_args, converted_obs)
end
"""
used_attributes(args...) = ()
Function used to indicate what keyword args one wants to get passed in `convert_arguments`.
Those attributes will not be forwarded to the backend, but only used during the
conversion pipeline.
Usage:
```julia
struct MyType end
used_attributes(::MyType) = (:attribute,)
function convert_arguments(x::MyType; attribute = 1)
...
end
# attribute will get passed to convert_arguments
# without keyword_verload, this wouldn't happen
plot(MyType, attribute = 2)
#You can also use the convenience macro, to overload convert_arguments in one step:
@keywords convert_arguments(x::MyType; attribute = 1)
...
end
```
"""
used_attributes(::Type{<:Plot}, args...) = used_attributes(args...)
used_attributes(args...) = ()
## generic definitions
# Chose the more specific plot type from arguments or input type
# Note the plottype(Scatter, Plot{plot}) will return Scatter
# And plottype(args...) falls back to Plot{plot}
plottype(P::Type{<: Plot{T}}, argvalues...) where T = plottype(P, plottype(argvalues...))
plottype(P::Type{<:Plot{T}}) where {T} = P
plottype(P1::Type{<:Plot{T1}}, ::Type{<:Plot{T2}}) where {T1, T2} = P1
plottype(::Type{Plot{plot}}, ::Type{Plot{plot}}) = Plot{plot}
"""
plottype(P1::Type{<: Plot{T1}}, P2::Type{<: Plot{T2}})
Chooses the more concrete plot type
```julia
function convert_arguments(P::PlotFunc, args...)
ptype = plottype(P, Lines)
...
end
```
"""
plottype(::Type{Plot{plot}}, P::Type{<:Plot{T}}) where {T} = P
plottype(P::Type{<:Plot{T}}, ::Type{Plot{plot}}) where {T} = P
## specialized definitions for types
plottype(::AbstractVector, ::AbstractVector, ::AbstractVector) = Scatter
plottype(::AbstractVector, ::AbstractVector) = Scatter
plottype(::AbstractVector) = Scatter
plottype(::AbstractMatrix{<: Real}) = Heatmap
plottype(::Array{<: AbstractFloat, 3}) = Volume
plottype(::AbstractString) = Text
plottype(::LineString) = Lines
plottype(::AbstractVector{<:LineString}) = Lines
plottype(::MultiLineString) = Lines
plottype(::Polygon) = Poly
plottype(::GeometryBasics.AbstractPolygon) = Poly
plottype(::AbstractVector{<:GeometryBasics.AbstractPolygon}) = Poly
plottype(::MultiPolygon) = Lines
# all the plotting functions that get a plot type
const PlotFunc = Union{Type{Any},Type{<:AbstractPlot}}
function plot!(::Plot{F}) where {F}
if !(F in atomic_functions)
error("No recipe for $(F)")
end
end
function connect_plot!(parent::SceneLike, plot::Plot{F}) where {F}
plot.parent = parent
apply_theme!(parent_scene(parent), 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
if is_space_compatible(plot, parent)
obsfunc = connect!(transformation(parent), transformation(plot))
append!(plot.deregister_callbacks, obsfunc)
end
end
plot.model = transformationmatrix(plot)
convert_arguments!(plot)
calculated_attributes!(Plot{F}, plot)
default_shading!(plot, parent_scene(parent))
plot!(plot)
return plot
end
function plot!(scene::SceneLike, plot::Plot)
connect_plot!(scene, plot)
push!(scene, plot)
return plot
end
function apply_theme!(scene::Scene, plot::P) where {P<: Plot}
raw_attr = attributes(plot.attributes)
plot_theme = default_theme(scene, P)
plot_sym = plotsym(P)
if haskey(theme(scene), plot_sym)
merge_without_obs_reverse!(plot_theme, theme(scene, plot_sym))
end
for (k, v) in plot.kw
if v isa NamedTuple
raw_attr[k] = Attributes(v)
else
raw_attr[k] = convert(Observable{Any}, v)
end
end
return merge!(plot.attributes, plot_theme)
end