BsCmdInputFieldValueChange.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "BsEditorCommand.h"
  6. #include "BsUndoRedo.h"
  7. namespace bs
  8. {
  9. /** @addtogroup UndoRedo
  10. * @{
  11. */
  12. /**
  13. * A command used for undo/redo purposes. It records a value of a GUI input field (specified by template type) and
  14. * allows you to apply or revert a change to that field as needed.
  15. */
  16. template <class InputFieldType, class ValueType>
  17. class BS_ED_EXPORT CmdInputFieldValueChange : public EditorCommand
  18. {
  19. public:
  20. /**
  21. * Creates and executes the command on the provided object and field. Automatically registers the command with
  22. * undo/redo system.
  23. *
  24. * @param[in] inputField Input field to modify the value on.
  25. * @param[in] value New value for the field.
  26. * @param[in] description Optional description of what exactly the command does.
  27. */
  28. static void execute(InputFieldType* inputField, const ValueType& value,
  29. const WString& description = StringUtil::WBLANK)
  30. {
  31. CmdInputFieldValueChange* command =
  32. new (bs_alloc<CmdInputFieldValueChange>()) CmdInputFieldValueChange(description, inputField, value);
  33. SPtr<CmdInputFieldValueChange> commandPtr = bs_shared_ptr(command);
  34. UndoRedo::instance().registerCommand(commandPtr);
  35. commandPtr->commit();
  36. }
  37. /** @copydoc EditorCommand::commit */
  38. void commit() override
  39. {
  40. mInputField->_setValue(mNewValue, true);
  41. }
  42. /** @copydoc EditorCommand::revert */
  43. void revert() override
  44. {
  45. mInputField->_setValue(mOldValue, true);
  46. }
  47. private:
  48. friend class UndoRedo;
  49. CmdInputFieldValueChange(const WString& description, InputFieldType* inputField, const ValueType& value)
  50. :EditorCommand(description), mOldValue(inputField->getValue()), mNewValue(value), mInputField(inputField)
  51. { }
  52. ValueType mOldValue;
  53. ValueType mNewValue;
  54. InputFieldType* mInputField;
  55. };
  56. /** @} */
  57. }