Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RageEye · Aug 17, 2020 at 04:17 PM · inputfieldundo

Possible Undo/Redo class as Input Field?

Hello, Could someone help me convert this class to Unity Input Field or just Text?

 public partial class Form1 : Form
 {
     Stack<Func<object>> undoStack = new Stack<Func<object>>();
     Stack<Func<object>> redoStack = new Stack<Func<object>>();
 
     public Form1()
     {
         InitializeComponent();
         textBox1.KeyDown += TextBox1_KeyDown;
     }
 
     private void TextBox1_KeyDown(object sender, KeyEventArgs e)
     {
         if (e.KeyCode == Keys.ControlKey && ModifierKeys == Keys.Control) { }
         else if (e.KeyCode == Keys.U && ModifierKeys == Keys.Control)
         {
             if(undoStack.Count > 0)
             {
                 StackPush(sender, redoStack);
                 undoStack.Pop()();
             }
         }
         else if (e.KeyCode == Keys.R && ModifierKeys == Keys.Control)
         {
             if(redoStack.Count > 0)
             {
                 StackPush(sender, undoStack);
                 redoStack.Pop()();
             }
         }
         else
         {
             redoStack.Clear();
             StackPush(sender, undoStack);
         }
     }
 
     private void StackPush(object sender, Stack<Func<object>> stack)
     {
         TextBox textBox = (TextBox)sender;
         var tBT = textBox.Text(textBox.Text, textBox.SelectionStart);
         stack.Push(tBT);
     }
 }
 
 public static class Extensions
 {
     public static Func<TextBox> Text(this TextBox textBox, string text, int sel)
     {
         return () => 
         {
             textBox.Text = text;
             textBox.SelectionStart = sel;
             return textBox;
         };
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Nov 19, 2020 at 10:32 PM

Well, with the "new" UI system uGUI we have a little issue. The InputField class does not have a event / callback when a key is pressed. However it actually has a protected method "KeyPressed" which is called when a key event happens. Unfortunately it is not marked virtual so we can not override it in our own subclass. That's a really bad shortcomming of Unity's class design and others have already noticed that 3 years ago.


Your only option here would be to create a subclass and override the only alternative we have which is OnUpdateSelected. It actually grabs the queued events from Unity's event system and processes them with the KeyPressed method. Since we want our own handler inside that loop your only option is to copy the whole content of that method into your overridden method and add your special handling right in place.


Though regardless of this workaround I would recommend you file a bug report pointing out that KeyPressed should be a virtual method.


Anyways here's an example implementation I quickly implemented:

 // InputFieldUndo.cs
 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class InputFieldUndo : InputField
 {
     protected Stack<Action> undoStack = new Stack<Action>();
     protected Stack<Action> redoStack = new Stack<Action>();
 
     protected Event m_ProcessingEvent = new Event();
 
     public override void OnUpdateSelected(BaseEventData eventData)
     {
         if (!isFocused)
             return;
 
         bool consumedEvent = false;
         while (Event.PopEvent(m_ProcessingEvent))
         {
             if (m_ProcessingEvent.rawType == EventType.KeyDown)
             {
                 consumedEvent = true;
                 if (ProcessUndo(m_ProcessingEvent))
                 {
                     var shouldContinue = KeyPressed(m_ProcessingEvent);
 
                     if (shouldContinue == EditState.Finish)
                     {
                         DeactivateInputField();
                         break;
                     }
                 }
             }
 
             switch (m_ProcessingEvent.type)
             {
                 case EventType.ValidateCommand:
                 case EventType.ExecuteCommand:
                     switch (m_ProcessingEvent.commandName)
                     {
                         case "SelectAll":
                             SelectAll();
                             consumedEvent = true;
                             break;
                     }
                     break;
             }
         }
 
         if (consumedEvent)
             UpdateLabel();
 
         eventData.Use();
     }
 
     private bool ProcessUndo(Event e)
     {
         if (e.keyCode == KeyCode.U && e.control)
         {
             if (undoStack.Count > 0)
             {
                 StackPush(redoStack);
                 undoStack.Pop()();
             }
             return false;
         }
         else if (e.keyCode == KeyCode.R && e.control)
         {
             if (redoStack.Count > 0)
             {
                 StackPush(undoStack);
                 redoStack.Pop()();
             }
             return false;
         }
         else if (ShouldRecord(e))
         {
             redoStack.Clear();
             StackPush(undoStack);
         }
         return true;
     }
     // restrict undo recording to relevant key presses
     private bool ShouldRecord(Event e)
     {
         switch (e.keyCode)
         {
             case KeyCode.None:
             case KeyCode.LeftShift:
             case KeyCode.RightShift:
             case KeyCode.LeftControl:
             case KeyCode.RightControl:
             case KeyCode.LeftAlt:
             case KeyCode.RightAlt:
                 return false;
         }
         return true;
     }
 
     private void StackPush(Stack<Action> stack)
     {
         var oldText = text;
         var oldPos = caretPosition;
         stack.Push(() =>
         {
             text = oldText;
             caretPosition = oldPos;
         });
     }
 }

Of course you have to use this class instead of the old InputField class. The easiest way to replace the class is:

  • select your InputField gameobject in the inspector

  • switch the inspector to debug mode through the top right context menu

  • drag our new script file onto the "script" field of the old InputField component

  • switch the inspector back to normal mode

That way the InputField will keep all its current settings and references but the actual class is replaced. The alternative way is to manually remove the oldInputField component, add the InputFieldUndo component and setup all the required references and settings. Unfortunately we can't easily add our new input field to the create UI menu.


Note I had to apply a "filter" for what keycodes actually trigger a "queueing action". Otherwise it would queue way too many undo actions because Unity had some keydown events with "KeyCode.None" (usually happens for char events). If you want to exclude other keys as well you can add them to the "ShouldRecord" method.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI InputField Undo Redo 0 Answers

Is there a notepad-like (preferably a notepad++-like) in-game text editor for use in Unity5? 0 Answers

How to get InputField Text from prefabClone? 2 Answers

How to move text in input field? 0 Answers

Can you use a more performant undo function than RegisterSceneUndo with DestroyImmediate? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges