/* -*- c++ -*- FILE: Macro.cpp RCS REVISION: $Revision: 1.18 $ COPYRIGHT: (c) 1999 -- 2003 Melinda Green, Don Hatch, and Jay Berkenbilt - Superliminal Software LICENSE: Free to use and modify for non-commercial purposes as long as the following conditions are adhered to: 1) Obvious credit for the source of this code and the designs it embodies are clearly made, and 2) Ports and derived versions of 4D Magic Cube programs are not distributed without the express written permission of the authors. DESCRIPTION: Implementation of the Macro class */ #include "Macro.h" #include "History.h" #include "Math4d.h" #include "Polymgr.h" #include "Preferences.h" static char * Strdup(char *s) { char *t; if (!s || !(t = new char[strlen(s) + 1])) return NULL; return strcpy(t, s); } Macro::Macro(char *name, int nrefs, int refs[MAXREFS][4], Preferences& prefs, PolygonManager4D* polymgr) : name(Strdup(name)), polymgr(polymgr) { assert(nrefs <= MAXREFS); this->setRefs(nrefs, refs); this->moves = new History(prefs, polymgr); } Macro::~Macro() { delete this->name; delete this->moves; } void Macro::setUIData(void* ui_data) { this->ui_data = ui_data; } void* Macro::getUIData() { return this->ui_data; } void Macro::setName(char *name) { delete [] this->name; this->name = Strdup(name); } void Macro::goToBeginning() { this->moves->goToBeginning(); } void Macro::goToEnd() { this->moves->goToEnd(); } void Macro::setRefs(int nrefs, int refs[MAXREFS][4]) { assert(nrefs <= MAXREFS); this->nrefs = nrefs; int j; for (j = 0; j < nrefs; ++j) SET4(this->refs[j], refs[j]); } void Macro::addMove(struct stickerspec *sticker, int direction, int slicesmask) { this->moves->apply(sticker, direction, slicesmask); } // Return true on success, false if at end of the macro bool Macro::getMove(int dir_in, int det, int mat[4][4], struct stickerspec* grip, int* dir_out, int* slicesmask) { bool status = false; if (dir_in < 0) status = this->moves->undo(grip, dir_out, slicesmask) == 1; else status = this->moves->redo(grip, dir_out, slicesmask) == 1; if (status) { VXM4i(grip->coords, grip->coords, mat); polymgr->fillStickerspecFromCoordsAndLength(grip, 3); *dir_out *= det; } return status; } char * Macro::getName() { return this->name; } bool Macro::get4dMatThatRotatesThese3ToMy3(int ref0[4], int ref1[4], int ref2[4], int mat[4][4]) { return (Math4d::get4dMatThatRotatesThese3ToThose3 (this->refs[0], this->refs[1], this->refs[2], ref0, ref1, ref2, mat) == 1); } void Macro::dump(FILE *fp) { struct stickerspec sticker; int i; fprintf(fp, "@%s@(", name); for (i = 0; i < nrefs; ++i) { SET4(sticker.coords, refs[i]); polymgr->fillStickerspecFromCoords(&sticker); fprintf(fp, "%d%d", sticker.face, sticker.id_within_face); if (i + 1 < nrefs) fprintf(fp, " "); } fprintf(fp, ") "); moves->dump(fp); } bool Macro::readHistory(FILE* fp) { return this->moves->read(fp) == 1; } // Local Variables: // c-basic-offset: 4 // c-comment-only-line-offset: 0 // c-file-offsets: ((defun-block-intro . +) (block-open . 0) (substatement-open . 0) (statement-cont . +) (statement-case-open . +4) (arglist-intro . +) (arglist-close . +) (inline-open . 0)) // indent-tabs-mode: nil // End: