예제 #1
0
bool ParallelPlane::InRange(const int& row){
    return InFilter(row);
}
예제 #2
0
void g2LabelEdit::KeyEvent(unsigned char key, bool IsSpecial)
{
    // In OSX the backspace maps to DEL while DEL maps to backspace, need to swap
    #if __APPLE__
    if(key == 127)
        key = 8;
    else if(key == 8)
        key = 127;
    #endif
    
    // Ignore all inputs if disabled
    if(GetDisabled())
        return;
    
    /*** User Movement / Editing ***/
    
    // If system key (i.e. left/right)
    else if(IsSpecial)
    {
        // Move far left/right
        if(key == GLUT_KEY_LEFT && glutGetModifiers() == GLUT_ACTIVE_CTRL)
        {
            CursorIndex = 0;
            ViewIndex = 0;
        }
        else if(key == GLUT_KEY_RIGHT && glutGetModifiers() == GLUT_ACTIVE_CTRL)
        {
            // Move the cursor to the far right and calculate
            // what is the best position for the view buffer to be moved to
            CursorIndex = (int)strlen(TextBuffer);
            
            // When something is written on-screen, check to see where our
            // left-view limit should be
            ViewIndex = (int)strlen(TextBuffer) - RenderableLeftChars(CursorIndex);
        }
        
        // If left/right, move the cursor
        if(key == GLUT_KEY_LEFT && CursorIndex > 0)
        {
            // Move cursor to left
            CursorIndex--;
            
            // Move view left if we can
            if(CursorIndex < ViewIndex)
                ViewIndex--;
        }
        else if(key == GLUT_KEY_RIGHT && CursorIndex < (int)strlen(TextBuffer))
        {
            // Move cursor to right
            CursorIndex++;
            
            // If the movement will cause us to be offsreen..
            while(LengthToCursor() > Width)
                ViewIndex++;
        }
    }
    
    // Backspace
    else if(key == 8)
    {
        // Is there anything to delete?
        if(strlen(TextBuffer) <= 0)
            return;
        // Ignore if we are at the 0 position
        else if(CursorIndex <= 0)
            return;
        else
        {
            // Delete this character by shifting everything from right to left by 1
            // Note that this copies the null terminator
            for(size_t i = CursorIndex; i <= strlen(TextBuffer); i++)
                TextBuffer[i - 1] = TextBuffer[i];
            
            // Decrease the cursor position
            CursorIndex--;
            
            // Is the cursor now smaller than the current view index
            // Move view left if we can
            while(CursorIndex < ViewIndex)
            {
                ViewIndex -= 5;
                if(ViewIndex < 0)
                    ViewIndex = 0;
            }
        }
    }
    
    // Delete
    else if(key == 127)
    {
        // Is there anything to delete?
        if(CursorIndex >= (int)strlen(TextBuffer))
            return;
        else
        {
            // Delete this character by shifting everything from right to left by 1
            // Note that this copies the null terminator
            for(size_t i = CursorIndex; i < strlen(TextBuffer); i++)
                TextBuffer[i] = TextBuffer[i + 1];
            
            // Cursor does not move
        }
    }
    
    // Commit / enter
    else if(key == '\r')
    {
        DidUserReturn = true;
    }
    
    /*** Cut, Copy, or Paste ***/
    
    // Note that when doing a ctrl+key event, the given number
    // is offset from 'a', meaning true ascii = 'a' - key + 1
    
    // Cut text
    else if(key == ('x' - 'a' + 1) && glutGetModifiers() == GLUT_ACTIVE_CTRL)
    {
        // Copy into buffer, then set text to empty
        CopyBuffer();
        SetText("");
    }
    
    // Copy text
    else if(key == ('c' - 'a' + 1) && glutGetModifiers() == GLUT_ACTIVE_CTRL)
    {
        // Direct copy
        CopyBuffer();
    }
    
    // Paste text
    else if(key == ('v' - 'a' + 1) && glutGetModifiers() == GLUT_ACTIVE_CTRL)
    {
        // Direct paste
        PasteBuffer();
    }
    
    /*** Regular User Input ***/
    
    // Standard keyboard input; add character
    else if(isprint(key) != 0)
    {
        // Can we actually add anything?
        if(strlen(TextBuffer) < g2LabelEdit_TextBufferLength - 1 && !IsSpecial)
        {
            // Ignore if it isn't a valid character
            if(!InFilter(key))
                return;
            
            // If we are writing to the end, make sure to string-cap
            if(CursorIndex == (int)strlen(TextBuffer))
            {
                // Write to the old string-end and move the terminator a little further
                TextBuffer[CursorIndex + 0] = key;
                TextBuffer[CursorIndex + 1] = '\0';
            }
            // Offset one char to the right, then set
            else
            {
                // Null-terminate the end of the string
                int Length = (int)strlen(TextBuffer);
                for(int i = Length; i > CursorIndex; i--)
                    TextBuffer[i] = TextBuffer[i - 1];
                TextBuffer[CursorIndex] = key;
                TextBuffer[Length + 1] = '\0';
            }
            
            // Grow cursor position to be after the current char
            CursorIndex++;
        }
        
        // If the movement will cause us to be offsreen..
        while(LengthToCursor() > Width)
            ViewIndex++;
    }
    
    // All done for each type of event
}