pixelfaucet/examples/cubic_bezier.cr

85 lines
2.2 KiB
Crystal
Raw Normal View History

2022-02-16 06:43:43 +01:00
require "../src/game"
require "../src/bezier"
module PF
class CubicBezier < PF::Game
2022-02-20 06:37:08 +01:00
FONT_COLOR = Pixel.new(0xFFFFFFFF)
POINT_COLOR = Pixel.new(0xFF0000FF)
CTL_COLOR = Pixel.new(0x505050FF)
CURVE_COLOR = Pixel.new(0x0077FFFF)
SEL_COLOR = Pixel.new(0xFFFF00FF)
EXT_X_COLOR = Pixel.new(0xFF00FFFF)
EXT_Y_COLOR = Pixel.new(0x00FF00FF)
2022-02-20 06:37:08 +01:00
@curve : Bezier::Cubic(Float64)
2022-02-16 06:43:43 +01:00
@hover_point : Vector2(Float64)*? = nil
@selected_point : Vector2(Float64)*? = nil
def initialize(*args, **kwargs)
super
2022-02-20 06:37:08 +01:00
@curve = Bezier::Cubic.new(
Vector[width * 0.25, height * 0.7],
Vector[width * 0.33, height * 0.3],
Vector[width * 0.66, height * 0.3],
Vector[width * 0.75, height * 0.7]
2022-02-16 06:43:43 +01:00
)
end
def on_mouse_motion(cursor)
if point = @selected_point
point.value = cursor.to_f
else
@hover_point = @curve.points.find { |p| cursor.distance(p.value) < 4 }
end
end
2022-02-16 06:43:43 +01:00
def on_mouse_button(event)
if event.button == 1
if event.pressed?
@selected_point = @hover_point
2022-02-16 06:43:43 +01:00
else
@selected_point = nil
2022-02-16 06:43:43 +01:00
end
end
end
def update(dt)
end
2022-02-16 06:43:43 +01:00
def draw
clear
2022-02-20 06:37:08 +01:00
draw_line(@curve.p0, @curve.p1, CTL_COLOR)
draw_line(@curve.p3, @curve.p2, CTL_COLOR)
2022-02-16 06:43:43 +01:00
2022-02-20 06:37:08 +01:00
draw_string("Length: " + @curve.length.round(2).to_s, 5, 5, FONT_COLOR)
draw_rect(*@curve.rect.map(&.to_i), CTL_COLOR)
draw_curve(@curve, CURVE_COLOR)
@curve.extremeties.each do |point|
point.try do |p|
draw_circle(p.to_i, 3, EXT_Y_COLOR)
end
end
2022-02-20 06:37:08 +01:00
fill_circle(@curve.p0.to_i, 2, POINT_COLOR)
fill_circle(@curve.p1.to_i, 2, POINT_COLOR)
fill_circle(@curve.p2.to_i, 2, POINT_COLOR)
fill_circle(@curve.p3.to_i, 2, POINT_COLOR)
2022-02-16 06:43:43 +01:00
2022-02-20 06:37:08 +01:00
draw_string("P1", @curve.p0.to_i, color: FONT_COLOR)
draw_string("P2", @curve.p1.to_i, color: FONT_COLOR)
draw_string("P3", @curve.p2.to_i, color: FONT_COLOR)
draw_string("P4", @curve.p3.to_i, color: FONT_COLOR)
2022-02-16 06:43:43 +01:00
if point = @hover_point
2022-02-20 06:37:08 +01:00
draw_circle(point.value.to_i, 5, SEL_COLOR)
2022-02-16 06:43:43 +01:00
end
end
end
end
engine = PF::CubicBezier.new(500, 500, 2).run!