MCPcopy Create free account
hub / github.com/WheretIB/nullc / ClientToCursor

Method ClientToCursor

GUI/RichTextarea.cpp:1459–1519  ·  view source on GitHub ↗

Convert local pixel coordinates to cursor position Position X coordinate is clamped when clampX is set

Source from the content-addressed store, hash-verified

1457// Convert local pixel coordinates to cursor position
1458// Position X coordinate is clamped when clampX is set
1459AreaLine* TextareaData::ClientToCursor(int xPos, int yPos, unsigned int &cursorX, unsigned int &cursorY, bool clampX)
1460{
1461 if(yPos > 32768)
1462 {
1463 if(shiftCharY > 0)
1464 shiftCharY--;
1465 yPos = 0;
1466 }
1467 if(xPos > 32768)
1468 {
1469 if(shiftCharX > 0)
1470 shiftCharX--;
1471 xPos = 0;
1472 }
1473 if(yPos > (areaHeight + RichTextarea::charHeight) && shiftCharY < int(lineCount) - (areaHeight / RichTextarea::charHeight) - 1)
1474 shiftCharY++;
1475 int maxShiftX = currLine->length - ((areaWidth - 96) / RichTextarea::charWidth) - 1;
1476 if(xPos > (areaWidth + RichTextarea::charWidth) && shiftCharX < (maxShiftX < 0 ? 0 : maxShiftX))
1477 shiftCharX++;
1478 // Find vertical cursor position
1479 cursorY = yPos / RichTextarea::charHeight + shiftCharY - (yPos < 0 ? 1 : 0);
1480
1481 // Clamp vertical position
1482 if(cursorY >= lineCount)
1483 cursorY = lineCount - 1;
1484 // Change current line
1485 AreaLine *curr = firstLine;
1486 for(unsigned int i = 0; i < cursorY; i++)
1487 curr = curr->next;
1488
1489 // Convert local X coordinate to virtual X coordinate (no padding and scroll shifts)
1490 int vMouseX = xPos - RichTextarea::padLeft + shiftCharX * RichTextarea::charWidth;
1491 // Starting X position in pixels
1492 int vCurrX = 0;
1493
1494 // Starting cursor position
1495 cursorX = 0;
1496 // Starting X position in characters
1497 int posInChars = 0;
1498 // Until we reach virtual X coordinate or line ends
1499 while(cursorX < curr->length)
1500 {
1501 // Find the length of a symbol
1502 int shift = GetCharShift(curr->data[cursorX].ch, posInChars);
1503 // Exit if current position with half of a character if bigger than mouse position
1504 if(vCurrX + shift * RichTextarea::charWidth / 2 > vMouseX)
1505 break;
1506 // Advance cursor, position in pixels and position in characters
1507 cursorX++;
1508 vCurrX += shift * RichTextarea::charWidth;
1509 posInChars += shift;
1510 }
1511 if(clampX)
1512 {
1513 // Clamp horizontal position
1514 if(cursorX > curr->length)
1515 cursorX = curr->length;
1516 }

Callers 1

TextareaProcMethod · 0.80

Calls 1

GetCharShiftFunction · 0.85

Tested by

no test coverage detected