unsafe void SetCaretToCursor(int mouseX, int mouseY) { mouseX -= inputTex.X1; mouseY -= inputTex.Y1; DrawTextArgs args = new DrawTextArgs(null, font, true); IDrawer2D drawer = game.Drawer2D; int offset = 0, elemHeight = defaultHeight; string oneChar = new String('A', 1); for (int y = 0; y < lines; y++) { string line = parts[y]; if (line == null) { continue; } for (int x = 0; x < line.Length; x++) { args.Text = line.Substring(0, x); int trimmedWidth = drawer.MeasureChatSize(ref args).Width; // avoid allocating an unnecessary string fixed(char *ptr = oneChar) ptr[0] = line[x]; args.Text = oneChar; int elemWidth = drawer.MeasureChatSize(ref args).Width; if (Contains(trimmedWidth, y * elemHeight, elemWidth, elemHeight, mouseX, mouseY)) { caretPos = offset + x; CalculateCaretData(); return; } } offset += partLens[y]; } caretPos = -1; CalculateCaretData(); }
public void SetCaretToCursor( int mouseX, int mouseY, IDrawer2D drawer, Font font ) { string text = Text; if( Password ) text = new String( '*', text.Length ); mouseX -= X; mouseY -= Y; DrawTextArgs args = new DrawTextArgs( text, font, true ); Size size = drawer.MeasureSize( ref args ); if( mouseX >= size.Width ) { CaretPos = -1; return; } for( int i = 0; i < Text.Length; i++ ) { args.Text = text.Substring( 0, i ); int trimmedWidth = drawer.MeasureChatSize( ref args ).Width; args.Text = new String( text[i], 1 ); int charWidth = drawer.MeasureChatSize( ref args ).Width; if( mouseX >= trimmedWidth && mouseX < trimmedWidth + charWidth ) { CaretPos = i; return; } } }
public void DrawCaret( IDrawer2D drawer, Font font ) { string text = Text; if( Password ) text = new String( '*', text.Length ); DrawTextArgs args = new DrawTextArgs( text, font, true ); if( CaretPos == -1 ) { Size size = drawer.MeasureSize( ref args ); drawer.Clear( FastColour.White, X + 5 + size.Width, Y + Height - 5, 10, 2 ); } else { args.Text = text.Substring( 0, CaretPos ); int trimmedWidth = drawer.MeasureChatSize( ref args ).Width; args.Text = new String( text[CaretPos], 1 ); int charWidth = drawer.MeasureChatSize( ref args ).Width; drawer.Clear( FastColour.White, X + 5 + trimmedWidth, Y + Height - 5, charWidth, 2 ); } }