Set cursor position based on mouse click location.

This commit is contained in:
Paul Chote
2010-11-23 21:22:50 +13:00
parent 8085bcb232
commit 3a2279f378

View File

@@ -56,9 +56,33 @@ namespace OpenRA.Widgets
blinkCycle = 10; blinkCycle = 10;
showCursor = true; showCursor = true;
CursorPosition = ClosestCursorPosition(mi.Location.X);
return true; return true;
} }
public int ClosestCursorPosition(int x)
{
var font = (Bold) ? Game.Renderer.BoldFont : Game.Renderer.RegularFont;
var textSize = font.Measure(Text);
var start = RenderOrigin.X + margin;
if (textSize.X > Bounds.Width - 2 * margin && Focused)
start += Bounds.Width - 2 * margin - textSize.X;
int minIndex = -1;
int minValue = int.MaxValue;
for (int i = 0; i <= Text.Length; i++)
{
var dist = Math.Abs(start + font.Measure(Text.Substring(0,i)).X - x);
if (dist > minValue)
break;
minValue = dist;
minIndex = i;
}
return minIndex;
}
public override bool HandleKeyPressInner(KeyInput e) public override bool HandleKeyPressInner(KeyInput e)
{ {
if (e.Event == KeyInputEvent.Up) return false; if (e.Event == KeyInputEvent.Up) return false;
@@ -138,11 +162,11 @@ namespace OpenRA.Widgets
base.Tick(); base.Tick();
} }
int margin = 5;
public virtual void DrawWithString(string text) public virtual void DrawWithString(string text)
{ {
if (text == null) text = ""; if (text == null) text = "";
int margin = 5;
var font = (Bold) ? Game.Renderer.BoldFont : Game.Renderer.RegularFont; var font = (Bold) ? Game.Renderer.BoldFont : Game.Renderer.RegularFont;
var pos = RenderOrigin; var pos = RenderOrigin;