BsCmdInputFieldValueChange.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 BS_ED_EXPORT 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. * @param description Optional description of what exactly the command does.
  23. */
  24. static void execute(InputFieldType* inputField, const ValueType& value, const WString& description = StringUtil::WBLANK)
  25. {
  26. CmdInputFieldValueChange* command = new (bs_alloc<CmdInputFieldValueChange>()) CmdInputFieldValueChange(description, inputField, value);
  27. UndoRedo::instance().registerCommand(command);
  28. command->commit();
  29. }
  30. /**
  31. * @copydoc EditorCommand::commit
  32. */
  33. void commit() override
  34. {
  35. mInputField->setValue(mNewValue);
  36. }
  37. /**
  38. * @copydoc EditorCommand::revert
  39. */
  40. void revert() override
  41. {
  42. mInputField->setValue(mOldValue);
  43. }
  44. private:
  45. friend class UndoRedo;
  46. CmdInputFieldValueChange(const WString& description, InputFieldType* inputField, const ValueType& value)
  47. :EditorCommand(description), mInputField(inputField), mOldValue(inputField->getValue()), mNewValue(value)
  48. { }
  49. ValueType mOldValue;
  50. ValueType mNewValue;
  51. InputFieldType* mInputField;
  52. };
  53. }