Esempio n. 1
0
void TextBox::updateCursor(NVGcontext *, float lastx,
                           const NVGglyphPosition *glyphs, int size)
                           {
    // handle mouse cursor events
    if (mMouseDownPos.x() != -1) {
        if (mMouseDownModifier == SDLK_LSHIFT) {
            if (mSelectionPos == -1)
                mSelectionPos = mCursorPos;
        } else
            mSelectionPos = -1;

        mCursorPos =
            position2CursorIndex(mMouseDownPos.x(), lastx, glyphs, size);

        mMouseDownPos = Vector2i(-1, -1);
    } else if (mMouseDragPos.x() != -1) {
        if (mSelectionPos == -1)
            mSelectionPos = mCursorPos;

        mCursorPos =
            position2CursorIndex(mMouseDragPos.x(), lastx, glyphs, size);
    } else {
        // set cursor to last character
        if (mCursorPos == -2)
            mCursorPos = size;
    }

    if (mCursorPos == mSelectionPos)
        mSelectionPos = -1;
}
Esempio n. 2
0
void Caret::onClick(NVGcontext *ctx, float lineh, Vector2i clickPos){
    if (clickPos.x() > -1) {
        mIdx.y() = (int) clickPos.y()/lineh;
        //std::cout << "mPos = " << mConsole->mPos.x() << "," << mConsole->mPos.y() << std::endl;

        if(mIdx.y() < (int)mConsole->mBuffer.size()){
            //std::cout << mBuffer.at(mCursorPosY)<< std::endl;
            double time = glfwGetTime();
            if (time - mLastClick < 0.25) {
                /* Double-click: select a line */
                int linew = mConsole->mSize.x();
                mSelectionIdx = Vector2i(0,(int) clickPos.y()/lineh); 
                mSelectionPos = Vector2i(0,mSelectionIdx.y()*lineh);
                mIdx = Vector2i(mConsole->mBuffer[mSelectionIdx.y()].size(), mSelectionIdx.y());
                mPos = Vector2i(linew, mSelectionPos.y());
                mSelectionState = true;
                std::cout << "Sel : " << mSelectionPos.x() << "," << mSelectionPos.y() << std::endl;
                std::cout << "Pos : " << mPos.x() << "," << mPos.y() << std::endl;
            }
            else { 
                const int maxGlyphs = 1024;
                NVGglyphPosition glyphs[maxGlyphs];
                const std::string& textLine = mConsole->mBuffer[mIdx.y()];
                float textBound[4];

                nvgTextBounds(ctx, 0, 0, textLine.c_str(), nullptr, textBound);
                int nglyphs = nvgTextGlyphPositions(ctx, 0, 0, textLine.c_str(),
                        nullptr, glyphs, maxGlyphs);

                mIdx.x() = position2CursorIndex(clickPos.x(), textBound[2], glyphs, nglyphs);
         
                //update carete position now
                mPos.y() = mIdx.y() * lineh;
                mPos.x() = cursorIndex2Position(mIdx.x(), textBound[2], glyphs, nglyphs);
            }
            mLastClick = time;
        }
    }
}