Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] General fixes #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,35 @@ function draw(scene::Scene, plot::AbstractPlotting.Text)
@get_attribute(plot, (textsize, color, font, align, rotation, model))
txt = to_value(plot[1])
position = plot.attributes[:position][]

# hack layouting into the process here
if position isa Point
position, _ = AbstractPlotting.layout_text(
txt, position, textsize,
font, align, rotation, model
)
end

# hardcode this against abstractplotting madness
# all textsizes in the vector are different, one for each glyph, which
# has to do with the texture atlas
# 20 is just the default makielayout fontsize
textsize = 20f0

plotwindow_width = topparent(scene).px_area[].widths[1]

N = length(txt)
broadcast_foreach(1:N, position, textsize, color, font, rotation) do i, p, ts, cc, f, r
pos = project_position(scene, p, model)
chup = r * Vec2f0(0, 1)
GR.setcharup(chup[1], chup[2])
GR.settextfontprec(233, 3)
GR.setcharheight(0.018) # ts ???

# helvetica in string mode seems to work relatively well for some reason
GR.settextfontprec(105, 0)
# GR.setcharheight(0.018) # ts ???

# convert textsize to plotwindow_width percentage
GR.setcharheight(textsize / plotwindow_width * 0.666) # why 2/3ds? window size seems always too large
GR.settextcolorind(Int(GR.inqcolorfromrgb(cc.r, cc.g, cc.b)))
GR.settransparency(cc.alpha)
GR.settextalign(1, 4)
Expand Down Expand Up @@ -223,7 +245,7 @@ end
Plots a 3D mesh.
"""
function draw(scene::Scene, plot::Mesh)
@get_attribute(plot, (interpolate,shading,colormap,colorrange))
# @get_attribute(plot, (interpolate,shading,colormap,colorrange))
end


Expand Down Expand Up @@ -258,11 +280,21 @@ function gr_draw(scene::Scene)
end
# The main method - this draws all plots and child scenes
function draw(scene::Scene)
foreach(plot-> draw(scene, plot), scene.plots)
foreach(child-> draw(child), scene.children)
for plot in scene.plots
if plot.visible[]
draw(scene, plot)
end
end
for child in scene.children
draw(child)
end
end
# The lower level method. This dispatches on primitives, meaning that
# we have some guarantees about their attributes.
function draw(scene::Scene, primitive::AbstractPlotting.Combined)
foreach(x-> draw(scene, x), filter(x -> x.visible[], primitive.plots))
for p in primitive.plots
if p.visible[]
draw(scene, p)
end
end
end
28 changes: 22 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
topparent(x::Scene) = isnothing(x.parent) ? x : topparent(x.parent)

function project_position(scene, point, model)
p4d = to_ndim(Vec4f0, to_ndim(Vec3f0, point, 0f0), 1f0)

clip = scene.camera.projectionview[] * model * p4d
p = (clip / clip[4])[Vec(1, 2)]
p = collect((p .+ 1) ./ 2)
w, h = scene.camera.resolution[]

# p_m1_p1 is in scene-relative -1 to 1 space
p_m1_p1 = (clip / clip[4])[Vec(1, 2)]

# p_0_1 is in scene-relative 0 to 1 space
p_0_1 = collect((p_m1_p1 .+ 1) ./ 2)

subarea = scene.px_area[]
rootarea = topparent(scene).px_area[]

p_subscene = p_0_1 .* subarea.widths .+ subarea.origin

@assert rootarea.origin == Point2f0(0, 0)
p_0_1_root = p_subscene ./ rootarea.widths

w, h = rootarea.widths
if w > h
p[2:2:end] .*= (h / w)
p_0_1_root[2:2:end] .*= (h / w)
else
p[1:2:end] .*= (w / h)
p_0_1_root[1:2:end] .*= (w / h)
end
return Vec2f0(p)
return Vec2f0(p_0_1_root)
end

project_scale(scene::Scene, s::Number) = project_scale(scene, Vec2f0(s))
Expand Down