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

Predefined objects and :draw action #350

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion src/Javis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ include("structs/Object.jl")
include("structs/Transitions.jl")
include("structs/Action.jl")


"""
Line

Expand Down Expand Up @@ -97,10 +96,12 @@ include("luxor_overrides.jl")
include("backgrounds.jl")
include("svg2luxor.jl")
include("morphs.jl")
include("structs/Creations.jl")
include("action_animations.jl")
include("javis_viewer.jl")
include("latex.jl")
include("object_values.jl")
include("shorthands.jl")

"""
projection(p::Point, l::Line)
Expand Down Expand Up @@ -414,6 +415,7 @@ const LUXOR_DONT_EXPORT = [
:get_fontsize,
:scale,
:text,
:Circle
]

# Export each function from Luxor
Expand Down
5 changes: 5 additions & 0 deletions src/action_animations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ function _appear(video, object, action, rel_frame, symbol::Val{:draw_text})
object.opts[:draw_text_t] = t
end

function _appear(video, object, action, rel_frame, symbol::Val{:draw})
t = get_interpolation(action, rel_frame)
show_creation(object.metadata[1]["shapetype"], object, action, t)
end

"""
disappear(s::Symbol)

Expand Down
84 changes: 84 additions & 0 deletions src/shorthands.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
abstract type Circle end

function _Lineobj(p1, p2, color)
sethue(color)
line(p1, p2,:stroke)
return
end

function Lineobj(p1, p2; color="black")
push!(CURRENT_OBJECT_META, Dict("shapetype"=>Line, "initial_pos" => p1, "final_pos" => p2))
return (args...) -> _Lineobj(p1, p2, color)
end

Lineobj(p2) = Lineobj(O, p2)

function _Circle(center, radius, color, action)
sethue(color)
circle(center, radius, action)
return
end

function Circle(center::Point, radius::Real; color="black", action=:fill)
push!(CURRENT_OBJECT_META, Dict("shapetype"=>Circle, "center" => center, "radius" => radius, "current_angle" => 0.0))
return (args...) -> _Circle(center, radius, color, action)
end

Circle(center_x::Real, center_y::Real, radius::Real; color="black", action=:fill) = Circle(Point(center_x, center_y), radius, color, action)
Circle(p1::Real, p2::Real; color="black", action=:fill) = Circle(midpoint(p1, p2), distance(pt1, pt2)/2, color, action)

#==================#

# macros are difficult to generalize with all the different parameters taken by luxor methods

# macro shapetoobj(name::String, frames, start_pos, func)# todo have some defualt arguments here....defaults in the macro call
# expr = quote
# function ($(Symbol(name)))(frames=$frames, start_pos=$start_pos, func=$func)
# return Object(frames, (args...)->func, start_pos)
# end
# end
# return esc(expr)
# end
# @shapetoobj "JCircle" 1:15 O begin
# circle()
# end

# function Circle(frames::UnitRange=1:15, center::Point=O, radius::Real=40, action::Symbol=:stroke, color::String="black")
# function func(color, center, radius, action)
# sethue(color)
# circle(center, radius ,action)
# end
# return Object(frames, (args...)->func(color, center, radius, action))
# end

# function Circle(frames::UnitRange=1:15, center_x::Float64=0, center_y::Float64=0, radius::Float64=40, action::Symbol=:stroke, color::String="black")
# Circle(frames, Point(center_x, center_y), radius, action, color)
# end

# function Circle(frames::UnitRange=1:15, p1::Point=Point(20, 0), p2::Point=Point(-20, 0), p3::Point=Point(0, 20), action::Symbol=:stroke, color::String="black")
# sethue(color)
# return Object(frames, (args...)->circle(p1, p2, p3,action))
# end

# function Circle(frames::UnitRange=1:15, p1::Point=Point(20, 0), p2::Point=Point(-20, 0), action::Symbol=:stroke, color::String="black")
# sethue(color)
# return Object(frames, (args...)->circle(p1, p2, action))
# end

# function shift!(obj::Object, frames::UnitRange=obj.frames, fp::Union{Object,Point}=O, tp::Union{Object,Point}=O, easing=nothing)
# if easing !== nothing
# act!(obj, Action(frames, easing, anim_translate(fp, tp)))
# else
# act!(obj, Action(frames, anim_translate(fp, tp)))
# end
# end

# function shift!(obj::Object, fp::Union{Object,Point}=O, tp::Union{Object,Point}=O, easing=nothing)
# act!(obj, Action(anim_translate(fp, tp)))
# end

# function shift!(obj::Object, frames::UnitRange=obj.frames, fp::Union{Object,Point}=O, tp::Union{Object,Point}=O, easing=nothing)
# act!(obj, Action(frames, anim_translate(fp, tp)))
# end


36 changes: 36 additions & 0 deletions src/structs/Creations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# to get a flash traversing the path...pass the last position instead of initial

function show_creation(::Type{Line}, obj::Object, action, t)
metadata = obj.metadata[1]
initial_pos = metadata["initial_pos"]
final_pos = metadata["final_pos"]
if t == 1
restore_object(t, obj, action)
else
obj.current_setting.mul_opacity = 0
new_pos = initial_pos + t * (final_pos - initial_pos)
line(initial_pos, new_pos,:stroke)
end
end

function show_creation(::Type{Circle}, obj::Object, action, t)
metadata = obj.metadata[1]
center = metadata["center"]
radius = metadata["radius"]

if t == 1
restore_object(t, obj, action)
else
current_angle = metadata["current_angle"]
obj.current_setting.mul_opacity = 0
new_angle = t * 6
pie(center, radius+10, 0, new_angle, :clip)
circle(center, radius, :stroke)
obj.metadata[1]["current_angle"] = new_angle
end
end

function restore_object(t, obj, action)
obj.current_setting.mul_opacity = t
action.keep = false
end
10 changes: 10 additions & 0 deletions src/structs/Object.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct Object <: AbstractObject
opts::Dict{Symbol,Any}
change_keywords::Dict{Symbol,Any}
result::Vector
metadata::Vector
end

"""
Expand All @@ -33,6 +34,7 @@ The current object can be accessed using CURRENT_OBJECT[1]
"""

const CURRENT_OBJECT = Array{Object,1}()
const CURRENT_OBJECT_META = Array{Any,1}()

Object(func::Function, args...; kwargs...) = Object(:same, func, args...; kwargs...)

Expand Down Expand Up @@ -80,6 +82,12 @@ function Object(frames, func::Function, start_pos::Union{Object,Point}; kwargs..
CURRENT_VIDEO[1].background_frames =
union(CURRENT_VIDEO[1].background_frames, frames)
end

if !isempty(CURRENT_OBJECT_META)
metadata = CURRENT_OBJECT_META[1]
else
metadata = nothing
end
object = Object(
frames,
func,
Expand All @@ -89,7 +97,9 @@ function Object(frames, func::Function, start_pos::Union{Object,Point}; kwargs..
opts,
Dict{Symbol,Any}(),
Any[nothing],
Any[metadata],
)
empty!(CURRENT_OBJECT_META)
push!(CURRENT_VIDEO[1].objects, object)
return object
end
Expand Down