void PhysicsFrameSplineEditor::addControlPoint() {
    if (m_spline.control.size() == 0) {
        m_spline.append(CFrame());
    } else if (m_spline.control.size() == 1) {
        // Adding the 2nd point
        CFrame f = m_spline.control.last();
        f.translation += f.lookVector();
        m_spline.append(f);

        resizeControlPointDropDown(m_spline.control.size());
        // Select the new point
        setSelectedControlPointIndex(m_selectedControlPointIndex + 1);
    } else {
        // Adding between two points
        float t0 = m_spline.time[m_selectedControlPointIndex];
        float newT = t0;
        float evalT = 0;
        if (m_selectedControlPointIndex < m_spline.control.size() - 1) {
            // Normal interval
            newT = m_spline.time[m_selectedControlPointIndex + 1];
            evalT = (t0 + newT) / 2;
        } else if (m_spline.extrapolationMode == SplineExtrapolationMode::CYCLIC) {
            // After the end on a cyclic spline
            newT = m_spline.getFinalInterval() + t0;
            evalT = (t0 + newT) / 2;
        } else {
            // After the end on a non-cyclic spline of length at least
            // 2; assume that we want to step the distance of the previous
            // interval
            newT = evalT = 2.0f * t0 - m_spline.time[m_selectedControlPointIndex - 1];
        }

        const PhysicsFrame f = m_spline.evaluate(evalT);
        m_spline.control.insert(m_selectedControlPointIndex + 1, f);
        m_spline.time.insert(m_selectedControlPointIndex + 1, newT);

        // Select the new point
        resizeControlPointDropDown(m_spline.control.size());
        setSelectedControlPointIndex(m_selectedControlPointIndex + 1);

        // Fix the rest of the times to be offset by the inserted duration
        float shift = newT - t0;
        for (int i = m_selectedControlPointIndex + 1; i < m_spline.time.size(); ++i) {
            m_spline.time[i] += shift;
        }
        
    }

}
bool PhysicsFrameSplineEditor::hitsSpline(const Ray& r) {
    int j = m_spline.control.size();
    for (int i = 0; i < j; ++i) {
        const CFrame& c = m_spline.control[i];
        if(r.intersectionTime(Sphere(c.translation, CONTROL_POINT_RADIUS), true) != finf()) {
            setSelectedControlPointIndex(i);
            return true;
        }
    }
    return false;
}
void PhysicsFrameSplineEditor::removeSelectedControlPoint() {
    if (m_spline.control.size() <= 1) {
        // Can't delete!
        return;
    }

    // TODO: Should we fix the times?  Maybe
    m_spline.time.remove(m_selectedControlPointIndex);
    m_spline.control.remove(m_selectedControlPointIndex);
    setSelectedControlPointIndex(m_selectedControlPointIndex - 1);
}
void PhysicsFrameSplineEditor::controlPointDropDownCallback() {
    setSelectedControlPointIndex(m_selectedControlPointIndex);
}