1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include "Canvas.h"
bool Canvas::growHoriztontal()
{
if (getWidth() < MAX_HORI) {
for (unsigned int i = 0; i < getHeight(); i++) {
_canvas[i].push_back(false);
}
return true;
}
return false;
}
bool Canvas::growVertical()
{
if (getHeight() != MAX_VERTI) {
_canvas.push_back(std::vector<bool>(getWidth(), false));
return true;
}
return false;
}
bool Canvas::processDrawAction(PEN_AXIS plane, int extent)
{
if (plane == DEPTH) {
if (_currInstruction != NULL) {
_instructions.push_back(_currInstruction);
}
if (extent == 1) {
_instructions.push_back((new Instruction(SET, _pen._x, _pen._y)));
}
}
else if (isPenDown()) {
INSTRUCTION newInstruction = plane == HORI ? DRH : DRV;
if (_currInstruction != NULL) {
if (_currInstruction->cmd == newInstruction) {
_currInstruction->arg1 += extent;
}
else {
_instructions.push_back(_currInstruction);
_currInstruction = new Instruction(newInstruction, extent);
}
}
else {
_currInstruction = new Instruction(newInstruction, extent);
}
return true;
}
return false;
}
void Canvas::stop()
{
if (_currInstruction != NULL) {
_instructions.push_back(_currInstruction);
_currInstruction = NULL;
}
}
void Canvas::mark()
{
if (isPenDown())
{
_canvas[_pen._y][_pen._x] = true;
}
}
Canvas::Canvas()
{
for (unsigned int i = 0; i < INIT_DIM; i++) {
_canvas.push_back(std::vector<bool>(INIT_DIM, false));
}
}
void Canvas::draw(std::ostream & sout, bool showDetails)
{
if (showDetails) {
sout << "Pen Position: " << _pen._x << "," << _pen._y << std::endl;
sout << "Image Size:" << getWidth() << "," << getHeight() << std::endl;
}
for (unsigned int y = 0; y < getHeight(); y++) {
for (unsigned int x = 0; x < getWidth(); x++) {
if (_pen._show && _pen._x == x && _pen._y == y) {
sout << (_pen._down ? PEN_STATUS_DOWN : PEN_STATUS_UP);
}
else {
sout << (_canvas[y][x] ? '*' : ' ');
}
}
sout << std::endl;
}
}
unsigned int Canvas::getWidth()
{
return _canvas[0].size();
}
unsigned int Canvas::getHeight()
{
return _canvas.size();
}
unsigned int Canvas::getPenX()
{
return _pen._x;
}
unsigned int Canvas::getPenY()
{
return _pen._y;
}
bool Canvas::movePen(PEN_AXIS pa, bool positive)
{
mark();
if (pa == HORI) {
if (positive) {
if (_pen._x + 1 == getWidth())
{
if (!growHoriztontal()) {
return false;
}
}
_pen._x++;
processDrawAction(pa, 1);
return true;
}
else {
if (_pen._x <= 0) {
return false;
}
_pen._x--;
processDrawAction(pa, -1);
return true;
}
}
else {
if (positive) {
if (_pen._y + 1 == getHeight())
{
if (!growVertical()) {
return false;
}
}
_pen._y++;
processDrawAction(pa, 1);
return true;
}
else {
if (_pen._y <= 0) {
return false;
}
_pen._y--;
processDrawAction(pa, -1);
return true;
}
}
return false;
}
void Canvas::togglePen()
{
processDrawAction(DEPTH, _pen._down ? -1 : 1);
_pen._down = !_pen._down;
}
void Canvas::toggleShowPen()
{
_pen._show = !_pen._show;
}
bool Canvas::isPenDown()
{
return _pen._down;
}
bool Canvas::doCommand(ACTION a)
{
switch (a) {
case LEFT:
movePen(HORI, false);
break;
case RIGHT:
movePen(HORI, true);
break;
case DOWN:
movePen(VERTI, true);
break;
case UP:
movePen(VERTI, false);
break;
case PEN_TOGGLE:
togglePen();
break;
case PEN_POSITION:
toggleShowPen();
break;
case STOP:
stop();
break;
}
return false;
}
std::vector<Instruction*> Canvas::getInstructions()
{
return _instructions;
}
|