mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2024-11-16 07:47:36 +01:00
86 lines
2.3 KiB
Crystal
86 lines
2.3 KiB
Crystal
require "../src/game"
|
|
require "../src/bezier"
|
|
|
|
module PF
|
|
class CubicBezier < PF::Game
|
|
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)
|
|
|
|
@curve : Bezier::Cubic(Float64)
|
|
|
|
@hover_point : Vector2(Float64)*? = nil
|
|
@selected_point : Vector2(Float64)*? = nil
|
|
|
|
@mouse_pos : Vector2(Int32) = Vector[0, 0]
|
|
|
|
def initialize(*args, **kwargs)
|
|
super
|
|
@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]
|
|
)
|
|
end
|
|
|
|
def update(dt, event)
|
|
case event
|
|
when SDL::Event::MouseButton
|
|
if event.button == 1
|
|
if event.pressed?
|
|
@selected_point = @hover_point
|
|
else
|
|
@selected_point = nil
|
|
end
|
|
end
|
|
when SDL::Event::MouseMotion
|
|
@mouse_pos = Vector[event.x, event.y] // scale
|
|
|
|
unless point = @selected_point
|
|
@hover_point = @curve.points.find { |p| @mouse_pos.distance(p.value) < 4 }
|
|
else
|
|
point.value = @mouse_pos.to_f
|
|
end
|
|
end
|
|
end
|
|
|
|
def draw
|
|
clear
|
|
|
|
draw_line(@curve.p0, @curve.p1, CTL_COLOR)
|
|
draw_line(@curve.p3, @curve.p2, CTL_COLOR)
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
if point = @hover_point
|
|
draw_circle(point.value.to_i, 5, SEL_COLOR)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
engine = PF::CubicBezier.new(500, 500, 2).run!
|