/*
* File: draw.h - Draw commands
* Project: GED - bitmap editor (ICP)
* Author: Kamil Dudka, xdudka00
* Team: xdudka00, xfilak01, xhefka00, xhradi08
* Created: 2006-04-11
*/
#ifndef DRAW_H
#define DRAW_H
#include <set>
#include "command.h"
/**
* Draw tool enumeration (self-explaining)
*/
enum EDrawTool {
TOOL_NOTOOL = 0,
TOOL_BRUSH,
TOOL_RUBBER,
TOOL_LINE,
TOOL_RECT,
TOOL_CIRCLE,
TOOL_ELLIPSE,
TOOL_TEXT,
TOOL_SELECT
};
/// Count of draw tool enumerators
enum { iDrawToolCnt = 9 };
/**
* Dsecribe draw tool properties.
*/
struct DrawContext {
EDrawTool tool; ///< selected tool
int fgBrushWidth; ///< brush width (in pixels)
int bgBrushWidth; ///< rubber width (in pixels)
Pixel fgColor; ///< forground color
Pixel bgColor; ///< background color
/**
* Draw tool label lookup.
* \param et Tool enumerator.
* \return Label of equipment.
*/
static const char *toolLabel (EDrawTool et);
/**
* Tool enumerator lookup.
* \param szLabel Label of tool to lookup.
* \return Tool enumerator, TOOL_NOTOOL if not found.
*/
static EDrawTool toolByLabel (const char *szLabel);
/**
* Initialize DrawContext structure.
* \param initTool Tool enumerator.
* \param initFgBrushWidht Foreground brush width.
* \param initBgBrushWidth Background brush width.
*/
DrawContext (EDrawTool initTool, int initFgBrushWidht, int initBgBrushWidth):
tool(initTool), fgBrushWidth (initFgBrushWidht), bgBrushWidth(initBgBrushWidth)
{
fgColor.red = 0;
fgColor.green = 0;
fgColor.blue = 0;
fgColor.alpha = 0xFF;
bgColor.red = 0xFF;
bgColor.green = 0xFF;
bgColor.blue = 0xFF;
bgColor.alpha = 0xFF;
}
};
/**
* Brush command class.
* This command is used for TOOL_BRUSH and TOOL_RUBBER.
* Design pattern command.
*/
class BrushCmd: public SlowUndoSelectCmd {
public:
/**
* Construct brush command
* \param pFB Pointer to frame buffer to work with.
* \param dc Current draw context.
* \param bBack Give true to use background brush. (TOOL_RUBBER)
*/
BrushCmd (FrameBuffer *pFB, const DrawContext &dc, bool bBack=false);
/**
* Add pixel position to command
* \param p Pixel position to add.
*/
void add (Point p);
virtual void exec ();
virtual std::string name () { return szCmdName; }
virtual std::string args ();
/**
* Import brush command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by BrushCmd::args() method.
*/
BrushCmd (FrameBuffer *pFB, const std::string &args);
static const char *const szCmdName; ///< Command id string
private:
int _iBrushWidth;
Pixel _brushColor;
std::set<int> _pixSet;
};
/**
* Abstract class PlacementCmd.
* Base for LineCmd, RectCmd, CircleCmd, EllipseCmd.
* Design pattern command.
*/
class PlacementCmd: public SlowUndoSelectCmd {
public:
virtual std::string args ();
protected:
/**
* Initialize base.
* \param pFB Pointer to framebuffer to work with.
* \param placement Command placement.
* \param dc Current draw context.
* \param bBack Give true to use background brush.
*/
PlacementCmd (FrameBuffer *pFB, Rect &placement, DrawContext &dc, bool bBack);
/**
* Import placement command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by PlacementCmd::args() method.
*/
PlacementCmd (FrameBuffer *pFB, const std::string &args);
/**
* Impress round brush at selected positon.
* \param pos Position of up-left corner of brush.
*/
void impressBrush (Point pos);
/*
* Shared data
*/
int _iBrushWidth; ///< Round brush radius
Pixel _brushColor; ///< Brush color
Rect _place; ///< Placement of derived command
};
/**
* Line draw command class.
* Design pattern command.
*/
class LineCmd: public PlacementCmd {
public:
/**
* Construct line command.
* \param pFB Pointer to framebuffer to work with.
* \param placement Command placement.
* \param dc Current draw context.
* \param bBack Give true to use background brush.
*/
LineCmd (FrameBuffer *pFB, Rect placement, DrawContext dc, bool bBack=false):
PlacementCmd (pFB, placement, dc, bBack) { }
virtual void exec ();
virtual std::string name () { return szCmdName; }
static const char *const szCmdName; ///< command id string
/**
* Import line command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by PlacementCmd::args() method.
*/
LineCmd (FrameBuffer *pFB, const std::string &args):
PlacementCmd (pFB, args) { }
};
/**
* Rectangle draw command class.
* Design pattern command.
*/
class RectCmd: public PlacementCmd {
public:
/**
* Construct rectangle command.
* \param pFB Pointer to framebuffer to work with.
* \param placement Command placement.
* \param dc Current draw context.
* \param bBack Give true to use background brush.
*/
RectCmd (FrameBuffer *pFB, Rect placement, DrawContext dc, bool bBack=false):
PlacementCmd (pFB, placement, dc, bBack) { }
virtual void exec ();
virtual std::string name () { return szCmdName; }
static const char *const szCmdName; ///< command id string
/**
* Import rectangle command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by PlacementCmd::args() method.
*/
RectCmd (FrameBuffer *pFB, const std::string &args):
PlacementCmd (pFB, args) { }
};
/**
* Circle draw command class.
* Design pattern command.
*/
class CircleCmd: public PlacementCmd {
public:
/**
* Construct circle command.
* \param pFB Pointer to framebuffer to work with.
* \param placement Command placement.
* \param dc Current draw context.
* \param bBack Give true to use background brush.
*/
CircleCmd (FrameBuffer *pFB, Rect placement, DrawContext dc, bool bBack=false):
PlacementCmd (pFB, placement, dc, bBack) { init(); }
virtual void exec ();
virtual std::string name () { return szCmdName; }
static const char *const szCmdName; ///< command id string
/**
* Import circle command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by PlacementCmd::args() method.
*/
CircleCmd (FrameBuffer *pFB, const std::string &args):
PlacementCmd (pFB, args) { init(); }
private:
void drawCirclePoints (int x, int y);
int _iRadius;
int _iCenterX;
int _iCenterY;
void init () {
_iRadius = (_place.size.width - _iBrushWidth) >> 1;
_iCenterX = _place.at.x + _iRadius;
_iCenterY = _place.at.y + _iRadius;
}
};
/**
* Ellipse draw command class.
* Design pattern command.
*/
class EllipseCmd: public PlacementCmd {
public:
/**
* Construct ellipse command.
* \param pFB Pointer to framebuffer to work with.
* \param placement Command placement.
* \param dc Current draw context.
* \param bBack Give true to use background brush.
*/
EllipseCmd (FrameBuffer *pFB, Rect placement, DrawContext dc, bool bBack=false):
PlacementCmd (pFB, placement, dc, bBack) { init(); }
virtual void exec ();
virtual std::string name () { return szCmdName; }
static const char *const szCmdName; ///< command id string
/**
* Import ellipse command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by PlacementCmd::args() method.
*/
EllipseCmd (FrameBuffer *pFB, const std::string &args):
PlacementCmd (pFB, args) { init(); }
private:
void drawEllipsePoints (int x, int y);
int _iHalfAxisA, _iHalfAxisB;
int _iCenterX, _iCenterY;
void init () {
_iHalfAxisA = (_place.size.width - _iBrushWidth) >> 1;
_iHalfAxisB = (_place.size.height- _iBrushWidth) >> 1;
_iCenterX = _place.at.x + _iHalfAxisA;
_iCenterY = _place.at.y + _iHalfAxisB;
}
};
/**
* Text command class.
* Design pattern command.
*/
class TextCmd: public SlowUndoSelectCmd {
public:
/**
* Construct text command.
* \param pFB Pointer to framebuffer to work with.
* \param position Left-bottom corner of text.
* \param dc Current draw context.
* \param szText Text string to draw.
*/
TextCmd (FrameBuffer *pFB, DrawContext dc, Point position, const char *szText);
virtual void exec ();
virtual std::string name () { return szCmdName; }
virtual std::string args ();
/**
* Import text command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by TextCmd::args() method.
*/
TextCmd (FrameBuffer *pFB, const std::string &args);
/**
* This is an exception thrown if error occur during command execution.
* There can be several reasons -- Font file not found, ...
* Read the program manual for additional information.
*/
class ErrFreeType { };
static const char *const szCmdName; ///< Command id string
private:
void init (); ///< Load font properties
int _iSize;
int _iDpi;
Pixel _fgColor;
Pixel _bgColor;
Point _pos;
std::string _fontFile;
std::string _strText;
};
/**
* Select command class.
* Design pattern command.
*/
class SelectCmd: public Cmd {
public:
/**
* Construct select command using current image size as select size.
* \param pFB Pointer to framebuffer to work with.
*/
SelectCmd (FrameBuffer *pFB);
/**
* Construct select command.
* \param pFB Pointer to framebuffer to work with.
* \param sel Selected area to select on execution.
*/
SelectCmd (FrameBuffer *pFB, const Rect &sel);
virtual void exec ();
virtual void unExec ();
virtual std::string name () { return szCmdName; }
virtual std::string args ();
/**
* Import select command.
* \param pFB Pointer to frame buffer to work with.
* \param args Data to import -- previously returned by SelectCmd::args() method.
*/
SelectCmd (FrameBuffer *pFB, const std::string &args);
static const char *const szCmdName; ///< Command id string
private:
Rect _newSel;
Rect _oldSel;
};
class GrayScaleCmd: public SlowUndoSelectCmd {
public:
/**
* Create filter "Grayscale" command.
* \param pFB Pointer to frame buffer to work with.
*/
GrayScaleCmd (FrameBuffer *pFB):
SlowUndoSelectCmd (pFB)
{
}
virtual void exec ();
virtual std::string name () { return szCmdName; }
virtual std::string args () { return std::string(); }
/**
* Import command.
* \param pFB Pointer to frame buffer to work with.
*/
GrayScaleCmd (FrameBuffer *pFB, const std::string &):
SlowUndoSelectCmd (pFB)
{
}
static const char *const szCmdName; ///< Command id string
};
class InvertCmd: public SlowUndoSelectCmd {
public:
/**
* Create filter "Invert" command.
* \param pFB Pointer to frame buffer to work with.
*/
InvertCmd (FrameBuffer *pFB):
SlowUndoSelectCmd (pFB)
{
}
virtual void exec ();
virtual std::string name () { return szCmdName; }
virtual std::string args () { return std::string(); }
/**
* Import command.
* \param pFB Pointer to frame buffer to work with.
*/
InvertCmd (FrameBuffer *pFB, const std::string &):
SlowUndoSelectCmd (pFB)
{
}
static const char *const szCmdName; ///< Command id string
};
#endif // DRAW_H