예제 #1
0
 public override void Update(GUIControlContext Context, double Time)
 {
     // Handle mouse selection.
     MouseState ms = Context.MouseState;
     if (ms != null)
     {
         if (this._MouseDrag)
         {
             if(ms.IsButtonDown(MouseButton.Left))
             {
                 this._Selection.End = this._SelectedIndex(ms.Position);
             }
             else
             {
                 Context.ReleaseMouse();
                 this._MouseDrag = false;
             }
         }
         if (ms.HasPushedButton(MouseButton.Left))
         {
             Context.CaptureMouse();
             Context.CaptureKeyboard();
             this._Selection = new TextSelection(this._SelectedIndex(ms.Position));
             this._MouseDrag = true;
         }
     }
 }
예제 #2
0
        public override void Update(GUIControlContext Context, double Time)
        {
            // Handle mouse selection.
            MouseState ms = Context.MouseState;
            if (ms != null)
            {
                if (this._MouseDrag)
                {
                    if(ms.IsButtonDown(MouseButton.Left))
                    {
                        this._Selection.End = this._SelectedIndex(ms.Position);
                    }
                    else
                    {
                        Context.ReleaseMouse();
                        this._MouseDrag = false;
                    }
                }
                if (ms.HasPushedButton(MouseButton.Left))
                {
                    Context.CaptureMouse();
                    Context.CaptureKeyboard();
                    this._Selection = new TextSelection(this._SelectedIndex(ms.Position));
                    this._MouseDrag = true;
                }
            }
            TextSelection ts = this._Selection;
            if (ts != null && !Context.HasKeyboard)
            {
                this._Selection = ts = null;
                if (this.TextEntered != null)
                {
                    this.TextEntered(this._Text);
                }
            }

            // Flash the cursor
            this._CursorFlashTime += Time;
            double cfr = this._Style.CursorFlashRate;
            while (this._CursorFlashTime > cfr)
            {
                this._CursorFlashTime -= cfr * 2.0;
            }

            // Update text
            KeyboardState ks = Context.KeyboardState;
            if (ks != null && ts != null)
            {
                bool changed = false;
                foreach (char c in ks.Presses)
                {
                    int starti;
                    int endi;
                    ts.Order(out starti, out endi);

                    if (c == '\b')
                    {
                        if (endi - starti > 0)
                        {
                            if(this._TryChangeText(this._Text.Substring(0, starti) + this._Text.Substring(endi, this._Text.Length - endi)))
                            {
                                this._Selection = new TextSelection(starti);
                                changed = true;
                            }
                        }
                        else
                        {
                            if (starti > 0)
                            {
                                if (this._TryChangeText(this._Text.Substring(0, starti - 1) + this._Text.Substring(starti, this._Text.Length - starti)))
                                {
                                    this._Selection = new TextSelection(starti - 1);
                                    changed = true;
                                }
                            }
                        }
                        continue;
                    }

                    if (ValidChar(c))
                    {
                        if (this._TryChangeText(this._Text.Substring(0, starti) + c + this._Text.Substring(endi, this._Text.Length - endi)))
                        {
                            this._Selection = new TextSelection(starti + 1);
                            changed = true;
                        }
                    }
                }
                if (changed)
                {
                    this._CursorFlashTime = 0.0;
                    this._TextSample.Dispose();
                    this._MakeTextSample();
                }

                // Navigation
                foreach (KeyEvent ke in ks.Events)
                {
                    if (ke.Type == ButtonEventType.Down)
                    {
                        int ss = this._Selection.Start;
                        if (ke.Key == Key.Left)
                        {
                            if (ss > 0)
                            {
                                this._Selection = new TextSelection(ss - 1);
                            }
                        }
                        if (ke.Key == Key.Right)
                        {
                            if (ss < this._Text.Length)
                            {
                                this._Selection = new TextSelection(ss + 1);
                            }
                        }
                    }
                }

                // Enter?
                if (ks.IsKeyDown(Key.Enter))
                {
                    this._Selection = null;
                    Context.ReleaseKeyboard();

                    if (this.TextEntered != null)
                    {
                        this.TextEntered(this._Text);
                    }
                }
            }
        }