BsCmdInputFieldValueChange.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 BansheeEngine
  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. UndoRedo::instance().registerCommand(command);
  34. command->commit();
  35. }
  36. /** @copydoc EditorCommand::commit */
  37. void commit() override
  38. {
  39. mInputField->_setValue(mNewValue, true);
  40. }
  41. /** @copydoc EditorCommand::revert */
  42. void revert() override
  43. {
  44. mInputField->_setValue(mOldValue, true);
  45. }
  46. private:
  47. friend class UndoRedo;
  48. CmdInputFieldValueChange(const WString& description, InputFieldType* inputField, const ValueType& value)
  49. :EditorCommand(description), mOldValue(inputField->getValue()), mNewValue(value), mInputField(inputField)
  50. { }
  51. ValueType mOldValue;
  52. ValueType mNewValue;
  53. InputFieldType* mInputField;
  54. };
  55. /** @} */
  56. }