BsCmdInputFieldValueChange.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /**
  10. * @brief A command used for undo/redo purposes. It records a value of a GUI input field
  11. * (specified by template type) and allows you to apply or revert a change to that field
  12. * as needed.
  13. */
  14. template <class InputFieldType, class ValueType>
  15. class BS_ED_EXPORT CmdInputFieldValueChange : public EditorCommand
  16. {
  17. public:
  18. /**
  19. * @brief Creates and executes the command on the provided object and field.
  20. * Automatically registers the command with undo/redo system.
  21. *
  22. * @param inputField Input field to modify the value on.
  23. * @param value New value for the field.
  24. * @param description Optional description of what exactly the command does.
  25. */
  26. static void execute(InputFieldType* inputField, const ValueType& value, const WString& description = StringUtil::WBLANK)
  27. {
  28. CmdInputFieldValueChange* command = new (bs_alloc<CmdInputFieldValueChange>()) CmdInputFieldValueChange(description, inputField, value);
  29. UndoRedo::instance().registerCommand(command);
  30. command->commit();
  31. }
  32. /**
  33. * @copydoc EditorCommand::commit
  34. */
  35. void commit() override
  36. {
  37. mInputField->_setValue(mNewValue, true);
  38. }
  39. /**
  40. * @copydoc EditorCommand::revert
  41. */
  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), mInputField(inputField), mOldValue(inputField->getValue()), mNewValue(value)
  50. { }
  51. ValueType mOldValue;
  52. ValueType mNewValue;
  53. InputFieldType* mInputField;
  54. };
  55. }