Skip to content

Commit

Permalink
minor simplifications - readability
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 18, 2022
1 parent b7f4b35 commit 89fecb0
Showing 1 changed file with 37 additions and 54 deletions.
91 changes: 37 additions & 54 deletions src/makielayout/lineaxis.jl
Expand Up @@ -27,20 +27,19 @@ function calculate_protrusion(

horizontal, labeltext, ticklabel_annotation_obs = closure_args

label_is_empty::Bool = iswhitespace(label) || isempty(label)

local label_is_empty::Bool = iswhitespace(label) || isempty(label)

local real_labelsize::Float32 = if label_is_empty
real_labelsize::Float32 = if label_is_empty
0f0
else
horizontal[] ? boundingbox(labeltext).widths[2] : boundingbox(labeltext).widths[1]
boundingbox(labeltext).widths[horizontal[] ? 2 : 1]
end

local labelspace::Float32 = (labelvisible && !label_is_empty) ? real_labelsize + labelpadding : 0f0
labelspace::Float32 = (labelvisible && !label_is_empty) ? real_labelsize + labelpadding : 0f0

local _tickspace::Float32 = (ticksvisible && !isempty(ticklabel_annotation_obs[])) ? tickspace : 0f0
_tickspace::Float32 = (ticksvisible && !isempty(ticklabel_annotation_obs[])) ? tickspace : 0f0

local ticklabelgap::Float32 = (ticklabelsvisible && actual_ticklabelspace > 0) ? actual_ticklabelspace + ticklabelpad : 0f0
ticklabelgap::Float32 = (ticklabelsvisible && actual_ticklabelspace > 0) ? actual_ticklabelspace + ticklabelpad : 0f0

return _tickspace + ticklabelgap + labelspace
end
Expand All @@ -50,7 +49,7 @@ function create_linepoints(
pos_ext_hor,
flipped::Bool, spine_width::Number, trimspine::Union{Bool, Tuple{Bool, Bool}}, tickpositions::Vector{Point2f}, tickwidth::Number)

(position::Float32, extents::Tuple{Float32, Float32}, horizontal::Bool) = pos_ext_hor
(position::Float32, extents::NTuple{2, Float32}, horizontal::Bool) = pos_ext_hor

if trimspine isa Bool
trimspine = (trimspine, trimspine)
Expand Down Expand Up @@ -123,10 +122,10 @@ function calculate_real_ticklabel_align(al, horizontal, fl::Bool, rot::Number)
(fl ? :left : :right, :center)
end
end
elseif al isa Tuple{Symbol, Symbol}
elseif al isa NTuple{2, Symbol}
return al
else
error("Align needs to be a Tuple{Symbol, Symbol}.")
error("Align needs to be a NTuple{2, Symbol}.")
end
end

Expand All @@ -142,7 +141,7 @@ function update_ticklabel_node(

nticks = length(tickvalues[])

local ticklabelgap::Float32 = spinewidth[] + tickspace[] + ticklabelpad[]
ticklabelgap::Float32 = spinewidth[] + tickspace[] + ticklabelpad[]

shift = if horizontal[]
Point2f(0f0, flipped ? ticklabelgap : -ticklabelgap)
Expand All @@ -165,16 +164,17 @@ end
function update_tick_obs(tick_obs, horizontal::Observable{Bool}, flipped::Observable{Bool}, tickpositions, tickalign, ticksize, spinewidth)
result = tick_obs[]
empty!(result) # re-use allocated array
sign = flipped[] ? -1 : 1
if horizontal[]
for tp in tickpositions
tstart = tp + (flipped[] ? -1f0 : 1f0) * Point2f(0f0, tickalign * ticksize - 0.5f0 * spinewidth)
tend = tstart + (flipped[] ? -1f0 : 1f0) * Point2f(0f0, -ticksize)
tstart = tp + sign * Point2f(0f0, tickalign * ticksize - 0.5f0 * spinewidth)
tend = tstart + sign * Point2f(0f0, -ticksize)
push!(result, tstart, tend)
end
else
for tp in tickpositions
tstart = tp + (flipped[] ? -1f0 : 1f0) * Point2f(tickalign * ticksize - 0.5f0 * spinewidth, 0f0)
tend = tstart + (flipped[] ? -1f0 : 1f0) * Point2f(-ticksize, 0f0)
tstart = tp + sign * Point2f(tickalign * ticksize - 0.5f0 * spinewidth, 0f0)
tend = tstart + sign * Point2f(-ticksize, 0f0)
push!(result, tstart, tend)
end
end
Expand All @@ -185,7 +185,7 @@ end
function update_tickpos_string(closure_args, tickvalues_labels_unfiltered, reversed::Bool, scale)

tickstrings, tickpositions, tickvalues, pos_extents_horizontal, limits_obs = closure_args
limits = limits_obs[]::Tuple{Float32, Float32}
limits = limits_obs[]::NTuple{2, Float32}

tickvalues_unfiltered, tickstrings_unfiltered = tickvalues_labels_unfiltered

Expand Down Expand Up @@ -226,8 +226,7 @@ function update_tickpos_string(closure_args, tickvalues_labels_unfiltered, rever
return
end

function update_minor_ticks(minortickpositions, limits::Tuple{Float32, Float32}, pos_extents_horizontal, minortickvalues, scale, reversed::Bool)

function update_minor_ticks(minortickpositions, limits::NTuple{2, Float32}, pos_extents_horizontal, minortickvalues, scale, reversed::Bool)
position::Float32, extents_uncorrected::NTuple{2, Float32}, horizontal::Bool = pos_extents_horizontal

extents = reversed ? reverse(extents_uncorrected) : extents_uncorrected
Expand All @@ -249,8 +248,8 @@ function update_minor_ticks(minortickpositions, limits::Tuple{Float32, Float32},
else
[Point2f(position, y) for y in tick_scenecoords]
end
return

return
end

function LineAxis(parent::Scene, attrs::Attributes)
Expand All @@ -266,8 +265,8 @@ function LineAxis(parent::Scene, attrs::Attributes)

pos_extents_horizontal = lift(calculate_horizontal_extends, endpoints; ignore_equal_values=true)
horizontal = lift(x-> x[3], pos_extents_horizontal)
# Tuple constructor converts more than `convert(Tuple{Float32, Float32}, x)` but we still need the conversion to Float32 tuple:
limits = lift(x-> convert(Tuple{Float32, Float32}, Tuple(x)), attrs.limits; ignore_equal_values=true)
# Tuple constructor converts more than `convert(NTuple{2, Float32}, x)` but we still need the conversion to Float32 tuple:
limits = lift(x-> convert(NTuple{2, Float32}, Tuple(x)), attrs.limits; ignore_equal_values=true)
flipped = lift(x-> convert(Bool, x), attrs.flipped; ignore_equal_values=true)

ticksnode = Observable(Point2f[]; ignore_equal_values=true)
Expand Down Expand Up @@ -324,9 +323,7 @@ function LineAxis(parent::Scene, attrs::Attributes)
end

tickspace = Observable(0f0; ignore_equal_values=true)
map!(tickspace, ticksvisible, ticksize, tickalign) do ticksvisible,
ticksize, tickalign

map!(tickspace, ticksvisible, ticksize, tickalign) do ticksvisible, ticksize, tickalign
ticksvisible ? max(0f0, ticksize * (1f0 - tickalign)) : 0f0
end

Expand All @@ -348,49 +345,40 @@ function LineAxis(parent::Scene, attrs::Attributes)

x_or_y = flipped ? position + labelgap : position - labelgap

if horizontal
return Point2f(middle, x_or_y)
else
return Point2f(x_or_y, middle)
end
return horizontal ? Point2f(middle, x_or_y) : Point2f(x_or_y, middle)
end

# Initial values should be overwritten by map!. `ignore_equal_values` doesn't work right now without initial values
labelalign = Observable((:none, :none); ignore_equal_values=true)
map!(labelalign, labelrotation, horizontal, flipped, flip_vertical_label) do labelrotation,
horizontal::Bool, flipped::Bool, flip_vertical_label::Bool
if labelrotation isa Automatic
return if labelrotation isa Automatic
if horizontal
return (:center, flipped ? :bottom : :top)
(:center, flipped ? :bottom : :top)
else
return (:center, if flipped
flip_vertical_label ? :bottom : :top
else
flip_vertical_label ? :top : :bottom
end
)
(:center, if flipped
flip_vertical_label ? :bottom : :top
else
flip_vertical_label ? :top : :bottom
end)
end
else
return (:center, :center)
end
(:center, :center)
end::NTuple{2, Symbol}
end

labelrot = Observable(0f0; ignore_equal_values=true)
map!(labelrot, labelrotation, horizontal, flip_vertical_label) do labelrotation,
horizontal::Bool, flip_vertical_label::Bool
if labelrotation isa Automatic
return if labelrotation isa Automatic
if horizontal
return 0f0
0f0
else
if flip_vertical_label
return Float32(-0.5pi)
else
return Float32(0.5pi)
end
(flip_vertical_label ? -0.5f0 : 0.5f0) * π
end
else
return Float32(labelrotation)
end
Float32(labelrotation)
end::Float32
end

labeltext = text!(
Expand Down Expand Up @@ -531,10 +519,7 @@ function tight_ticklabel_spacing!(la::LineAxis)
end


function iswhitespace(str)
match(r"^\s*$", str) !== nothing
end

iswhitespace(str) = match(r"^\s*$", str) !== nothing

function Base.delete!(la::LineAxis)
for (_, d) in la.elements
Expand Down Expand Up @@ -570,8 +555,6 @@ get_tickvalues(::Automatic, F, vmin, vmax) = get_tickvalues(automatic, identity,
# fall back to non-scale aware behavior if no special version is overloaded
get_tickvalues(ticks, scale, vmin, vmax) = get_tickvalues(ticks, vmin, vmax)



function get_ticks(ticks_and_labels::Tuple{Any, Any}, any_scale, ::Automatic, vmin, vmax)
n1 = length(ticks_and_labels[1])
n2 = length(ticks_and_labels[2])
Expand Down

0 comments on commit 89fecb0

Please sign in to comment.