void KisColorSelector::mousePressEvent(QMouseEvent* event)
{
    m_clickPos       = mapCoord(event->posF(), m_renderArea);
    m_mouseMoved     = false;
    m_pressedButtons = event->buttons();
    m_clickedRing    = getSaturationIndex(m_clickPos);
    
    qint8 clickedLightPiece = getLightIndex(event->posF());
    
    if (clickedLightPiece >= 0) {
        setLight(getLight(event->posF()), m_relativeLight);
        m_selectedLightPiece = clickedLightPiece;
        setSelectedColor(m_selectedColor, !(m_pressedButtons & Qt::RightButton), true);
        m_mouseMoved   = true;
    }
    else if (m_clickedRing >= 0) {
        if (getNumPieces() > 1) {
            for(int i=0; i<getNumRings(); ++i)
                m_colorRings[i].setTemporaries(m_selectedColor);
        }
        else {
            Radian angle = std::atan2(m_clickPos.x(), m_clickPos.y()) - RAD_90;
            m_selectedColor.setH(angle.scaled(0.0f, 1.0f));
            m_selectedColor.setS(getSaturation(m_clickedRing));
            m_selectedColor.setX(getLight(m_light, m_selectedColor.getH(), m_relativeLight));
            setSelectedColor(m_selectedColor, !(m_pressedButtons & Qt::RightButton), true);
            m_selectedRing = m_clickedRing;
            m_mouseMoved   = true;
            update();
        }
    }
}
void KisColorSelector::selectColor(const KisColor& color)
{
    m_selectedColor      = KisColor(color, m_colorSpace);
    m_selectedPiece      = getHueIndex(m_selectedColor.getH() * PI2);
    m_selectedRing       = getSaturationIndex(m_selectedColor.getS());
    m_selectedLightPiece = getLightIndex(m_selectedColor.getX());
    update();
}
void KisColorSelector::setLight(float light, bool relative)
{
    m_light = qBound(0.0f, light, 1.0f);
    
    m_selectedColor.setX(getLight(m_light, m_selectedColor.getH(), relative));
    m_relativeLight      = relative;
    m_selectedLightPiece = getLightIndex(m_selectedColor.getX());
    update();
}
void KisColorSelector::setNumLightPieces(int num)
{
    num = qBound(MIN_NUM_LIGHT_PIECES, num, MAX_NUM_LIGHT_PIECES);
    
    recalculateAreas(quint8(num));
    
    if (m_selectedLightPiece >= 0)
        m_selectedLightPiece = getLightIndex(m_selectedColor.getX());
    
    update();
}
qreal KisColorSelector::getLight(const QPointF& pt) const
{
    qint8 clickedLightPiece = getLightIndex(pt);
    
    if (clickedLightPiece >= 0) {
        if (getNumLightPieces() > 1) {
            return 1.0 - (qreal(clickedLightPiece) / qreal(getNumLightPieces()-1));
        }
        return 1.0 - (qreal(pt.y()) / qreal(m_lightStripArea.height()));
    }
    
    return qreal(0);
}
void KisColorSelector::mouseMoveEvent(QMouseEvent* event)
{
    QPointF dragPos           = mapCoord(event->posF(), m_renderArea);
    qint8   clickedLightPiece = getLightIndex(event->posF());
    
    if (clickedLightPiece >= 0) {
        setLight(getLight(event->posF()), m_relativeLight);
        m_selectedLightPiece = clickedLightPiece;
        setSelectedColor(m_selectedColor, m_selectedColorIsFgColor, true);
    }
    
    if (m_clickedRing < 0)
        return;
    
    if (getNumPieces() > 1) {
        float angle     = std::atan2(dragPos.x(), dragPos.y()) - std::atan2(m_clickPos.x(), m_clickPos.y());
        float dist      = std::sqrt(dragPos.x()*dragPos.x() + dragPos.y()*dragPos.y()) * 0.80f;
        float threshold = 5.0f * (1.0f-(dist*dist));
        
        if (qAbs(angle * TO_DEG) >= threshold || m_mouseMoved) {
            bool selectedRingMoved = true;
            
            if (m_pressedButtons & Qt::RightButton) {
                selectedRingMoved                 = m_clickedRing == m_selectedRing;
                m_colorRings[m_clickedRing].angle = m_colorRings[m_clickedRing].tmpAngle + angle;
            }
            else for(int i=0; i<getNumRings(); ++i)
                m_colorRings[i].angle = m_colorRings[i].tmpAngle + angle;
            
            if (selectedRingMoved) {
                KisColor color = m_colorRings[m_clickedRing].tmpColor;
                Radian   angle = m_colorRings[m_clickedRing].getMovedAngel() + (color.getH()*PI2);
                color.setH(angle.scaled(0.0f, 1.0f));
                color.setX(getLight(m_light, color.getH(), m_relativeLight));
                
                m_selectedPiece = getHueIndex(angle, m_colorRings[m_clickedRing].getShift());
                setSelectedColor(color, m_selectedColorIsFgColor, true);
            }
            
            m_mouseMoved = true;
        }
    }
    else {
        Radian angle = std::atan2(dragPos.x(), dragPos.y()) - RAD_90;
        m_selectedColor.setH(angle.scaled(0.0f, 1.0f));
        m_selectedColor.setX(getLight(m_light, m_selectedColor.getH(), m_relativeLight));
        setSelectedColor(m_selectedColor, m_selectedColorIsFgColor, true);
    }
    
    update();
}
Exemple #7
0
DirectionalLight::DirectionalLight( const ResourceID& id, const Color& lightColor, const glm::vec3& lightVector, OpenGL& openGL ) :
    Light( id, LightType::DIRECTIONAL_LIGHT, lightColor, "data/system/primitives/directional_light.prim", openGL ), // TODO: Load material from file.
    directionalLightIndex_( lockShaderDirectionalLight( openGL ) )
{
    (void)( lightVector ); // TODO: Remove this argument.

    GLint currentShaderProgram = 0;
    char uniformName[64];

    glGetIntegerv( GL_CURRENT_PROGRAM, &currentShaderProgram );
    assert( currentShaderProgram != 0 );

    // Get the location of this light's isValid in the GLSL shader program.
    sprintf( uniformName, "directionalLights[%u].isValid", directionalLightIndex_ );
    isValidLocation_ = openGL.getShaderVariableLocation( uniformName );
    assert( isValidLocation_ != -1 );

    // Enable this light in shader.
    openGL.setUniformInteger( isValidLocation_, 1 );

    // Get the location of the DirectionalLight::lightIndex variable in shader.
    sprintf( uniformName, "directionalLights[%u].lightIndex", directionalLightIndex_ );
    lightIndexLocation_ = openGL.getShaderVariableLocation( uniformName );
    assert( lightIndexLocation_ != -1 );

    // Get the location of the DirectionalLight::lightVector variable in shader.
    sprintf( uniformName, "directionalLights[%u].lightVector", directionalLightIndex_ );
    lightVectorLocation_ = openGL.getShaderVariableLocation( uniformName );
    assert( lightVectorLocation_ != -1 );

    // TODO: Use halfVector.
    halfVectorLocation_ = -1;

    // Set the light index in shader.
    openGL.setUniformInteger( lightIndexLocation_, getLightIndex() );

    // Initialize light vector in shader.
    update();
}
void KisColorSelector::resetLight()
{
    m_light              = (m_colorSpace == KisColor::HSV) ? 1.0f : 0.5f;
    m_selectedLightPiece = getLightIndex(m_light);
    update();
}