Remove magic number and hard type enums in DrawViewPart.h

- Remove currently present magic numbers
- Hard type enums, so magic numbers can no longer be introduced. We don't want people to introduce magic numbers.
This commit is contained in:
Benjamin Bræstrup Sayoc
2025-02-04 19:32:20 +01:00
parent 738608db07
commit b386e461c2
6 changed files with 90 additions and 144 deletions

View File

@@ -561,12 +561,11 @@ int DrawProjGroup::purgeProjections()
std::pair<Base::Vector3d, Base::Vector3d> DrawProjGroup::getDirsFromFront(DrawProjGroupItem* view)
{
std::pair<Base::Vector3d, Base::Vector3d> result;
std::string viewType = view->Type.getValueAsString();
ProjDirection viewType = static_cast<ProjDirection>(view->Type.getValue());
return getDirsFromFront(viewType);
}
std::pair<Base::Vector3d, Base::Vector3d> DrawProjGroup::getDirsFromFront(std::string viewType)
std::pair<Base::Vector3d, Base::Vector3d> DrawProjGroup::getDirsFromFront(ProjDirection viewType)
{
// Base::Console().Message("DPG::getDirsFromFront(%s)\n", viewType.c_str());
std::pair<Base::Vector3d, Base::Vector3d> result;
@@ -1106,77 +1105,26 @@ void DrawProjGroup::updateSecondaryDirs()
Base::Vector3d anchDir = anchor->Direction.getValue();
Base::Vector3d anchRot = anchor->getXDirection();
std::map<std::string, std::pair<Base::Vector3d, Base::Vector3d>> saveVals;
std::string key;
std::map<ProjDirection, std::pair<Base::Vector3d, Base::Vector3d>> saveVals;
std::pair<Base::Vector3d, Base::Vector3d> data;
for (auto& docObj : Views.getValues()) {
std::pair<Base::Vector3d, Base::Vector3d> newDirs;
std::string pic;
DrawProjGroupItem* v = static_cast<DrawProjGroupItem*>(docObj);
ProjItemType t = static_cast<ProjItemType>(v->Type.getValue());
switch (t) {
case Front:
data.first = anchDir;
data.second = anchRot;
key = "Front";
saveVals[key] = data;
break;
case Rear:
key = "Rear";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case Left:
key = "Left";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case Right:
key = "Right";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case Top:
key = "Top";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case Bottom:
key = "Bottom";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case FrontTopLeft:
key = "FrontTopLeft";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case FrontTopRight:
key = "FrontTopRight";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case FrontBottomLeft:
key = "FrontBottomLeft";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
case FrontBottomRight:
key = "FrontBottomRight";
newDirs = getDirsFromFront(key);
saveVals[key] = newDirs;
break;
default: {
//TARFU invalid secondary type
Base::Console().Message(
"ERROR - DPG::updateSecondaryDirs - invalid projection type\n");
}
ProjDirection t = static_cast<ProjDirection>(v->Type.getValue());
if (t == ProjDirection::Front) {
data.first = anchDir;
data.second = anchRot;
saveVals[ProjDirection::Front] = data;
}
else {
std::pair<Base::Vector3d, Base::Vector3d> newDirs = getDirsFromFront(t);
saveVals[t] = newDirs;
}
}
for (auto& docObj : Views.getValues()) {
DrawProjGroupItem* v = static_cast<DrawProjGroupItem*>(docObj);
std::string type = v->Type.getValueAsString();
ProjDirection type = static_cast<ProjDirection>(v->Type.getValue());
data = saveVals[type];
v->Direction.setValue(data.first);
v->Direction.purgeTouched();
@@ -1186,31 +1134,20 @@ void DrawProjGroup::updateSecondaryDirs()
recomputeChildren();
}
void DrawProjGroup::rotate(const std::string& rotationdirection)
void DrawProjGroup::rotate(const RotationMotion& motion)
{
std::pair<Base::Vector3d, Base::Vector3d> newDirs;
if (rotationdirection == "Right")
newDirs = getDirsFromFront("Left");// Front -> Right -> Rear -> Left -> Front
else if (rotationdirection == "Left")
newDirs = getDirsFromFront("Right");// Front -> Left -> Rear -> Right -> Front
else if (rotationdirection == "Up")
newDirs = getDirsFromFront("Bottom");// Front -> Top -> Rear -> Bottom -> Front
else if (rotationdirection == "Down")
newDirs = getDirsFromFront("Top");// Front -> Bottom -> Rear -> Top -> Front
DrawProjGroupItem* anchor = getAnchor();
anchor->Direction.setValue(newDirs.first);
anchor->XDirection.setValue(newDirs.second);
getAnchor()->rotate(motion);
updateSecondaryDirs();
}
void DrawProjGroup::spin(const std::string& spindirection)
// TODO: should these functions identical to those
// in DrawViewPart be moved to DrawView?
void DrawProjGroup::spin(const SpinDirection& spindirection)
{
double angle;
if (spindirection == "CW")
if (spindirection == SpinDirection::CW)
angle = M_PI / 2.0;// Top -> Right -> Bottom -> Left -> Top
if (spindirection == "CCW")
if (spindirection == SpinDirection::CCW)
angle = -M_PI / 2.0;// Top -> Left -> Bottom -> Right -> Top
spin(angle);