/// <summary> /// Renders this InputField to the screen /// </summary> /// <param name="_value">The string to be rendered in this InputField</param> public void Render(InputFieldStyle _style = default(InputFieldStyle)) { /* * if (!string.IsNullOrEmpty(mInputString)) * mcaretPosition = (mInputString.Length - 1); * * if (_style == null) * _style = new InputFieldStyle(); * * TackGUI.Box(mShape, _style.GetBoxStyle()); * TackGUI.TextArea(mShape, mInputString, _style.GetTextStyle()); * * if (mReceivingInput) * { * if (mcaretDisplayStopwatch.ElapsedMilliseconds >= mcaretDisplaySpeed) * { * mdisplayCaret = !mdisplayCaret; * mcaretDisplayStopwatch.Restart(); * } * * if (mdisplayCaret) * { * if (!string.IsNullOrEmpty(mInputString)) * { * mcaretPosition = mInputString.Length; * string selectedInputStr = mInputString.Substring(0, mcaretPosition); * SizeF stringLengthPx = mstringMeasurer.MeasureString(selectedInputStr, new Font(TackGUI.GetFontFamily(_style.FontFamilyId), _style.FontSize)); * * int totalPadding = (int)(mShape.Height - stringLengthPx.Height); * TackGUI.Box(new RectangleShape(stringLengthPx.Width - 3.0f, (TackEngine.ScreenHeight * 0.70f) + (totalPadding / 2), 1, (stringLengthPx.Height)), mCaretStyle); * } * else * { * SizeF stringLengthPx = mstringMeasurer.MeasureString("l", new Font(TackGUI.GetFontFamily(_style.FontFamilyId), _style.FontSize)); * * int totalPadding = (int)(mShape.Height - stringLengthPx.Height); * TackGUI.Box(new RectangleShape(3.0f, (TackEngine.ScreenHeight * 0.70f) + (totalPadding / 2), 1, (stringLengthPx.Height)), mCaretStyle); * } * } * * } */ }
public static string InputField(RectangleShape rect, string textToRender, ref InputFieldStyle style) { if (style == null) { style = new InputFieldStyle(); } // Instead of calling TextArea(), just run the dup code from the method, cause lazy //TextArea(rect, textToRender, style.GetTextStyle()); Bitmap textBitmap = new Bitmap((int)rect.Width, (int)rect.Height); Graphics g = Graphics.FromImage(textBitmap); g.FillRectangle(ActiveInstance.GetColouredBrush(style.BackgroundColour), 0, 0, rect.Width, rect.Height); g.DrawString(textToRender, new Font(GetFontFamily(style.FontFamilyId), style.FontSize, FontStyle.Regular), ActiveInstance.GetColouredBrush(style.FontColour), new Rectangle(0, 0, (int)rect.Width, (int)rect.Height), ActiveInstance.GenerateTextFormat(style.HorizontalAlignment, style.VerticalAlignment)); Sprite textSprite = Sprite.LoadFromBitmap(textBitmap); textSprite.Create(false); GUIOperation operation = new GUIOperation(1, 2); operation.Bounds = rect; operation.DrawLevel = 1; operation.Sprite = textSprite; operation.Colour = Colour4b.White; g.Dispose(); textBitmap.Dispose(); ActiveInstance.m_guiOperations.Add(operation); // Finished creating text operation bool registeredADownClick = false; //Console.WriteLine("{0} mouse events on ({1})", ActiveInstance.m_currentMouseEvents.Count, TackEngine.RenderCycleCount); for (int i = 0; i < ActiveInstance.m_currentMouseEvents.Count; i++) { //Console.WriteLine("Pos: {0}, X: {1}, X + Width: {2}", ActiveInstance.m_currentMouseEvents[i].Position.X, rect.X, (rect.X + rect.Width)); if (ActiveInstance.m_currentMouseEvents[i].Position.X >= rect.X && ActiveInstance.m_currentMouseEvents[i].Position.X <= (rect.X + rect.Width)) { //Console.WriteLine("Is X"); if (ActiveInstance.m_currentMouseEvents[i].Position.Y >= rect.Y && ActiveInstance.m_currentMouseEvents[i].Position.Y <= (rect.Y + rect.Height)) { if (ActiveInstance.m_currentMouseEvents[i].EventType == 0) { registeredADownClick = true; Console.WriteLine("Found a GUI mouse event of type: {0} involving the InputField", ActiveInstance.m_currentMouseEvents[i].EventType); } } } } if (registeredADownClick) { TackInput.GUIInputRequired = true; } else if (!registeredADownClick && ActiveInstance.m_currentMouseEvents.Count(x => x.EventType == 0) > 0) { Console.WriteLine("Found that a mouse down happened outside this inputfield"); TackInput.GUIInputRequired = false; } KeyboardKey[] bufferOperations = TackInput.GetInputBufferArray(); string newString; if (textToRender == null) { newString = ""; } else { newString = textToRender; } for (int i = 0; i < bufferOperations.Length; i++) { if (bufferOperations[i] == KeyboardKey.Left) { if (style.CaretPosition > 0) { style.CaretPosition -= 1; } } else if (bufferOperations[i] == KeyboardKey.Right) { if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } else if (bufferOperations[i] == KeyboardKey.BackSpace) { if (style.CaretPosition > 0) { newString = newString.Remove((int)style.CaretPosition - 1, 1); } if (style.CaretPosition > 0) { style.CaretPosition -= 1; } } else if (bufferOperations[i] == KeyboardKey.Delete) { if (style.CaretPosition < newString.Length) { newString = newString.Remove((int)style.CaretPosition, 1); } } else if (bufferOperations[i] == KeyboardKey.Space) { newString = newString.Insert((int)style.CaretPosition, " "); if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } else if (bufferOperations[i] == KeyboardKey.Period) { newString = newString.Insert((int)style.CaretPosition, "."); if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } else if (bufferOperations[i] == KeyboardKey.Quote) { newString = newString.Insert((int)style.CaretPosition, "\""); if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } else if (bufferOperations[i] == KeyboardKey.Minus) { if (TackInput.InputBufferShift) { newString = newString.Insert((int)style.CaretPosition, "_"); } else { newString = newString.Insert((int)style.CaretPosition, "-"); } if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } else if (bufferOperations[i] >= KeyboardKey.Number0 && bufferOperations[i] <= KeyboardKey.Number9) { newString = newString.Insert((int)style.CaretPosition, ((char)((int)bufferOperations[i] - 61)).ToString()); if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } else if (bufferOperations[i] >= KeyboardKey.A && bufferOperations[i] <= KeyboardKey.Z) { if (TackInput.InputBufferCapsLock || TackInput.InputBufferShift) { newString = newString.Insert((int)style.CaretPosition, ((char)((int)bufferOperations[i] - 18)).ToString()); } else { newString = newString.Insert((int)style.CaretPosition, ((char)((int)bufferOperations[i] + 14)).ToString()); } if (style.CaretPosition < newString.Length) { style.CaretPosition += 1; } } } TackInput.ClearInputBuffer(); return(newString); }