Draw straight track correctly
authorNick Gasson <nick@nickg.me.uk>
Thu, 9 Apr 2009 13:04:53 +0000 (14:04 +0100)
committerNick Gasson <nick@nickg.me.uk>
Thu, 9 Apr 2009 13:04:53 +0000 (14:04 +0100)
src/Editor.cpp
src/StraightTrack.cpp

index b9488f9a2c754a9099de60f78db18209d8dd65a5..aff9fdbdf2cd3ddad1e75b1a4d83b4fd07dfb19c 100644 (file)
@@ -169,7 +169,16 @@ void Editor::drawDraggedTile()
 // This just draws straight track along the rectangle
 void Editor::drawDraggedStraight(const ITrackSegment::Direction& anAxis, int aLength)
 {
-   //for (int i = 0; i < 
+   Point<int> where = myDragBegin;
+
+   log() << "drawDraggedStraight " << anAxis << " len=" << aLength;
+   
+   for (int i = 0; i < aLength; i++) {
+      myMap->setTrackAt(where, makeStraightTrack(anAxis));
+      
+      where.x += anAxis.x;
+      where.y += anAxis.z;
+   }
 }
 
 // Called when the user has finished dragging a rectangle for track
@@ -183,13 +192,13 @@ void Editor::drawDraggedTrack()
    
    int xlen = abs(xmax - xmin) + 1;
    int ylen = abs(ymax - ymin) + 1;
-
+log() << "xlen=" << xlen << ", ylen=" << ylen;
    if (xlen == 1 && ylen == 1)
       drawDraggedTile();
    else if (xlen == 1)
-      drawDraggedStraight(Axis::Y, ylen);
+      drawDraggedStraight(myDragBegin.y < myDragEnd.y ? Axis::Y : -Axis::Y, ylen);
    else if (ylen == 1)
-      drawDraggedStraight(Axis::X, xlen);
+      drawDraggedStraight(myDragBegin.x < myDragEnd.x ? Axis::X : -Axis::X, xlen);
    /*
    // Try to guess the initial orientation from a nearby track segment
    if (canConnect(myDragBegin.left(), myDragBegin)
index f31b5941812f5d859dc4cb6f0fc9377380d9ed7d..d958ae791ab77e90377ebee86bd4420a4a5dacf2 100644 (file)
@@ -142,5 +142,16 @@ void StraightTrack::render() const
 
 ITrackSegmentPtr makeStraightTrack(const ITrackSegment::Direction& aDirection)
 {
-   return ITrackSegmentPtr(new StraightTrack(aDirection));
+   ITrackSegment::Direction realDir(aDirection);
+   
+   // Direction must either be along Axis::X or Axis::Y but we
+   // allow the opositite direction here too
+   if (realDir == -Axis::X || realDir == -Axis::Y)
+      realDir = -realDir;
+
+   if (realDir != Axis::X && realDir != Axis::Y)
+      throw runtime_error("Illegal straight track direction: "
+                          + lexical_cast<string>(aDirection));
+   
+   return ITrackSegmentPtr(new StraightTrack(realDir));
 }