def _scrollIntoView(self): """ Makes sure the cursor is visible by applying an appropriate scroll amount. """ hratio = getHPixelScalar() textArea = self.component widthMode = textArea.widthMode textArea.widthMode = 'PIXEL' textAreaWidth = textArea.width / hratio textArea.widthMode = widthMode cursorPosInPixels = textArea.cursor.position.x / hratio relCursorPos = cursorPosInPixels - self.scrollInPixels if relCursorPos > 0 and relCursorPos < textAreaWidth: return textComponent = textArea.text cursorIdx = self.cursorIndex if relCursorPos < 0: startingString = textComponent.text[cursorIdx - SCROLL_SKIP_AMOUNT:cursorIdx + 1] startingWidth = textComponent.stringWidth(startingString) self.scrollInPixels = cursorPosInPixels - startingWidth elif relCursorPos > textAreaWidth - 5: trailingString = textComponent.text[cursorIdx - 1:cursorIdx + SCROLL_SKIP_AMOUNT] trailingWidth = textComponent.stringWidth(trailingString) self.scrollInPixels = cursorPosInPixels - textAreaWidth + trailingWidth self.scrollInPixels = max(self.scrollInPixels, 0) self.scrollInPixels = min( self.scrollInPixels, abs(self._textWidthInPixels() - textAreaWidth) + CURSOR_WIDTH) self._applyScroll()
def _applyScroll(self): """ Takes the internal scrollInPixels and applies it to the textArea. """ scrollX = -self.scrollInPixels / (GUI.screenResolution()[0] * 0.5) scrollX = scrollX * getHPixelScalar() self.component.scroll = (scrollX, 0.0)
def _scrollIntoView(self): """ Makes sure the cursor is visible by applying an appropriate scroll amount. """ hratio = getHPixelScalar() textArea = self.component widthMode = textArea.widthMode textArea.widthMode = 'PIXEL' textAreaWidth = textArea.width / hratio textArea.widthMode = widthMode cursorPosInPixels = textArea.cursor.position.x / hratio relCursorPos = cursorPosInPixels - self.scrollInPixels if relCursorPos > 0 and relCursorPos < textAreaWidth: return textComponent = textArea.text cursorIdx = self.cursorIndex if relCursorPos < 0: startingString = textComponent.text[cursorIdx - SCROLL_SKIP_AMOUNT:cursorIdx + 1] startingWidth = textComponent.stringWidth(startingString) self.scrollInPixels = cursorPosInPixels - startingWidth elif relCursorPos > textAreaWidth - 5: trailingString = textComponent.text[cursorIdx - 1:cursorIdx + SCROLL_SKIP_AMOUNT] trailingWidth = textComponent.stringWidth(trailingString) self.scrollInPixels = cursorPosInPixels - textAreaWidth + trailingWidth self.scrollInPixels = max(self.scrollInPixels, 0) self.scrollInPixels = min(self.scrollInPixels, abs(self._textWidthInPixels() - textAreaWidth) + CURSOR_WIDTH) self._applyScroll()
def _updateCursor(self): """ Updates the cursor position, clamping it to a valid index range. The visual cursor representation is positioned. """ text = self._getText() textLen = len(text.text) self.cursorIndex = max(self.cursorIndex, 0) self.cursorIndex = min(self.cursorIndex, textLen) cursorComp = self.cursor.comp cursorComp.position.x = text.stringWidth(text.text[:self.cursorIndex]) * getHPixelScalar() self._scrollIntoView() self.cursor.enable(self.enabled and not gotComposition() and isFocusedComponent(self.component))
def _updateCursor(self): """ Updates the cursor position, clamping it to a valid index range. The visual cursor representation is positioned. """ text = self._getText() textLen = len(text.text) self.cursorIndex = max(self.cursorIndex, 0) self.cursorIndex = min(self.cursorIndex, textLen) cursorComp = self.cursor.comp cursorComp.position.x = text.stringWidth( text.text[:self.cursorIndex]) * getHPixelScalar() self._scrollIntoView() self.cursor.enable(self.enabled and not gotComposition() and isFocusedComponent(self.component))
def screenToCharacterIndex(self, clipPos): """ Given a screen clip space position, this will calculate the closest index into the edit field text. """ oldWidthMode = self.component.widthMode self.component.widthMode = 'PIXEL' locPos = self.component.screenToLocal(clipPos) self.component.widthMode = oldWidthMode clickedAtPixel = locPos.x / getHPixelScalar() w = 0 idx = 0 text = self.component.text.text for c in text: cw = self.component.text.stringWidth(c) if clickedAtPixel <= w + cw / 2: return idx w += cw idx += 1 return len(text)
def _widthInPixels(self): widthMode = self.component.widthMode self.component.widthMode = 'PIXEL' w = self.component.width self.component.widthMode = widthMode return w / getHPixelScalar()