/// <summary>Creates a new edit box control</summary> public EditBox(Dialog parent) : base(parent) { controlType = ControlType.EditBox; parentDialog = parent; border = 5; // Default border spacing = 4; // default spacing isCaretOn = true; textData = new System.Windows.Forms.RichTextBox(); // Create the control textData.Visible = true; textData.Font = new System.Drawing.Font("Arial", 8.0f); textData.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None; textData.Multiline = false; textData.Text = string.Empty; textData.MaxLength = ushort.MaxValue; // 65k characters should be plenty textData.WordWrap = false; // Now create the control textData.CreateControl(); isHidingCaret = false; firstVisible = 0; blinkTime = NativeMethods.GetCaretBlinkTime() * 0.001f; lastBlink = FrameworkTimer.GetAbsoluteTime(); textColor = new ColorValue(0.06f, 0.06f, 0.06f, 1.0f); selectedTextColor = new ColorValue(1.0f, 1.0f, 1.0f, 1.0f); selectedBackColor = new ColorValue(0.15f, 0.196f, 0.36f, 1.0f); caretColor = new ColorValue(0, 0, 0, 1.0f); caretPosition = textData.SelectionStart = 0; isInsertMode = true; isMouseDragging = false; }
/// <summary>Render the control</summary> public override void Render(Device device, float elapsedTime) { if (!IsVisible) { return; // Nothing to render } // Render the control graphics for (int i = 0; i <= LowerRightBorder; ++i) { Element e = elementList[i] as Element; e.TextureColor.Blend(ControlState.Normal, elapsedTime); parentDialog.DrawSprite(e, elementRects[i]); } // // Compute the X coordinates of the first visible character. // int xFirst = textData.GetPositionFromCharIndex(firstVisible).X; int xCaret = textData.GetPositionFromCharIndex(caretPosition).X; int xSel; if (caretPosition != textData.SelectionStart) { xSel = textData.GetPositionFromCharIndex(textData.SelectionStart).X; } else { xSel = xCaret; } // Render the selection rectangle System.Drawing.Rectangle selRect = System.Drawing.Rectangle.Empty; if (caretPosition != textData.SelectionStart) { int selLeft = xCaret, selRight = xSel; // Swap if left is beigger than right if (selLeft > selRight) { int temp = selLeft; selLeft = selRight; selRight = temp; } selRect = System.Drawing.Rectangle.FromLTRB( selLeft, textRect.Top, selRight, textRect.Bottom); selRect.Offset(textRect.Left - xFirst, 0); selRect.Intersect(textRect); Parent.DrawRectangle(selRect, selectedBackColor); } // Render the text Element textElement = elementList[TextLayer] as Element; textElement.FontColor.Current = textColor; parentDialog.DrawText(textData.Text.Substring(firstVisible), textElement, textRect); // Render the selected text if (caretPosition != textData.SelectionStart) { int firstToRender = Math.Max(firstVisible, Math.Min(textData.SelectionStart, caretPosition)); int numToRender = Math.Max(textData.SelectionStart, caretPosition) - firstToRender; textElement.FontColor.Current = selectedTextColor; parentDialog.DrawText(textData.Text.Substring(firstToRender, numToRender), textElement, selRect); } // // Blink the caret // if (FrameworkTimer.GetAbsoluteTime() - lastBlink >= blinkTime) { isCaretOn = !isCaretOn; lastBlink = FrameworkTimer.GetAbsoluteTime(); } // // Render the caret if this control has the focus // if (hasFocus && isCaretOn && !isHidingCaret) { // Start the rectangle with insert mode caret System.Drawing.Rectangle caretRect = textRect; caretRect.Width = 2; caretRect.Location = new System.Drawing.Point( caretRect.Left - xFirst + xCaret - 1, caretRect.Top); // If we are in overwrite mode, adjust the caret rectangle // to fill the entire character. if (!isInsertMode) { // Obtain the X coord of the current character caretRect.Width = 4; } parentDialog.DrawRectangle(caretRect, caretColor); } }
/// <summary>Reset's the caret blink time</summary> protected void ResetCaretBlink() { isCaretOn = true; lastBlink = FrameworkTimer.GetAbsoluteTime(); }