BsCmdInputFieldValueChange.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsEditorCommand.h"
  4. #include "BsUndoRedo.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief A command used for undo/redo purposes. It records a value of a GUI input field
  9. * (specified by template type) and allows you to apply or revert a change to that field
  10. * as needed.
  11. */
  12. template <class InputFieldType, class ValueType>
  13. class CmdInputFieldValueChange : public EditorCommand
  14. {
  15. public:
  16. /**
  17. * @brief Creates and executes the command on the provided object and field.
  18. * Automatically registers the command with undo/redo system.
  19. *
  20. * @param inputField Input field to modify the value on.
  21. * @param value New value for the field.
  22. */
  23. static void execute(InputFieldType* inputField, const ValueType& value)
  24. {
  25. CmdInputFieldValueChange* command = new (bs_alloc<CmdInputFieldValueChange>()) CmdInputFieldValueChange(inputField, value);
  26. UndoRedo::instance().registerCommand(command);
  27. command->commit();
  28. }
  29. /**
  30. * @copydoc EditorCommand::commit
  31. */
  32. void commit() override
  33. {
  34. mInputField->setValue(mNewValue);
  35. }
  36. /**
  37. * @copydoc EditorCommand::revert
  38. */
  39. void revert() override
  40. {
  41. mInputField->setValue(mOldValue);
  42. }
  43. private:
  44. friend class UndoRedo;
  45. CmdInputFieldValueChange(InputFieldType* inputField, const ValueType& value)
  46. :mInputField(inputField), mOldValue(inputField->getValue()), mNewValue(value)
  47. { }
  48. ValueType mOldValue;
  49. ValueType mNewValue;
  50. InputFieldType* mInputField;
  51. };
  52. }