Click here to Skip to main content
15,888,968 members
Articles / Desktop Programming / MFC

Action History - Undo and Redo

Rate me:
Please Sign up or sign in to vote.
3.20/5 (11 votes)
6 Mar 2004MIT2 min read 44.8K   1.2K   18  
A useful undo/redo system that can be implemented into most programs easily.
// ActionDragBox.cpp: implementation file
//

#include "stdafx.h"
#include "ActionDragBox.h"

/***********************************************************************
class CActionDragBox : public IActionBase
	This class is designed to manage user action history. After an
	action has been handled the pointer to the original action should be
	stored in this class for undo and redo functionality.
***********************************************************************/

/***********************************************************************
	CActionDragBox - Constructor/Deconstructor
***********************************************************************/

// CActionDragBox - Constructor
//
CActionDragBox::CActionDragBox()
{
	// initialise the offset amounts
	m_ptOffset.x = m_ptOffset.y = 0;
	m_ptOldOffset.x = m_ptOldOffset.y = 0;
}

// CActionDragBox - Constructor
//
CActionDragBox::CActionDragBox(CRect* NewRect, CUndoRedoView* pParent /*=NULL*/)
	: m_pObject(NewRect), m_pParent(pParent)
{
	// initialise the offset amounts
	m_ptOffset.x = m_ptOffset.y = 0;
	m_ptOldOffset.x = m_ptOldOffset.y = 0;
}

// CActionDragBox - Deconstructor
//
CActionDragBox::~CActionDragBox()
{

}

/***********************************************************************
	CActionDragBox - Member Functions
***********************************************************************/

// void SetOffset(int left, int top)
//		Set the offset value of the drag box.
//
void CActionDragBox::SetOffset(int left, int top)
{
	// set the offset value of the drag box to that specified
	m_ptOffset.x = left;
	m_ptOffset.y = top;
}

// void IncOffset(int left, int top)
//		Increment the offset value of the drag box.
//
void CActionDragBox::IncOffset(int left, int top)
{
	// increment the offset value by the amount specified
	m_ptOffset.x += left;
	m_ptOffset.y += top;
}

/***********************************************************************
	CActionDragBox - Virtual Overrides
***********************************************************************/

// void Run()
//		Automatically runs when the action is to take place.
//
void CActionDragBox::Run()
{
	// create an instance of the client dc object
	CClientDC dc(m_pParent);

	// erase the old drag box
	m_pParent->DrawDragBox(&dc, true);
	// calculate the new rectangle
	m_pObject->left = m_pObject->left - m_ptOldOffset.x + m_ptOffset.x;
	m_pObject->top = m_pObject->top - m_ptOldOffset.y + m_ptOffset.y;
	m_pObject->right = m_pObject->right - m_ptOldOffset.x + m_ptOffset.x;
	m_pObject->bottom = m_pObject->bottom - m_ptOldOffset.y + m_ptOffset.y;
	// draw the new drag box
	m_pParent->DrawDragBox(&dc);

	// store the current offset value
	m_ptOldOffset = m_ptOffset;
}

// void Undo()
//		Automatically runs when the action is to be undone.
//
void CActionDragBox::Undo()
{
	// create an instance of the client dc object
	CClientDC dc(m_pParent);

	// erase the old drag box
	m_pParent->DrawDragBox(&dc, true);
	// calculate the new rectangle
	m_pObject->left = m_pObject->left - m_ptOldOffset.x;
	m_pObject->top = m_pObject->top - m_ptOldOffset.y;
	m_pObject->right = m_pObject->right - m_ptOldOffset.x;
	m_pObject->bottom = m_pObject->bottom - m_ptOldOffset.y;
	// draw the new drag box
	m_pParent->DrawDragBox(&dc);

	// store the current offset value
	m_ptOldOffset.x = m_ptOldOffset.y = 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Rotorz Limited
United Kingdom United Kingdom
I have been fascinated by software and video games since a young age when I was given my first computer, a Dragon 32. Since then I have experimented with numerous methods of development ranging from point-and-click type packages to C++. I soon realized that software development was what I wanted to do.

Having invested a lot of time into programming with various languages and technologies I now find it quite easy to pickup new ideas and methodologies. I relish learning new ideas and concepts.

Throughout my life I have dabbled in game and engine development. I was awarded a first for the degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich. It was good to finally experience video games from a more professional perspective.

Due to various family difficulties I was unable to immediately pursue any sort of software development career. This didn't stop me from dabbling though!

Since then I formed a company to focus upon client projects. Up until now the company has primarily dealt with website design and development. I have since decided that it would be fun to go back to my roots and develop games and tools that other developers can use for their games.

We have recently released our first game on iPhone/iPad called "Munchy Bunny!" (see: http://itunes.apple.com/us/app/munchy-bunny!/id516575993?mt=8). We hope to expand the game and release to additional platforms.

Also, check out our tile system extension for Unity! (see: http://rotorz.com/tilesystem/)

Comments and Discussions