From 759d3332ad7b69301ff8fcd1a856cfed1be2ffba Mon Sep 17 00:00:00 2001
From: Nick Gasson <nick@nickg.me.uk>
Date: Thu, 9 Apr 2009 14:04:53 +0100
Subject: [PATCH] Draw straight track correctly

---
 src/Editor.cpp        | 17 +++++++++++++----
 src/StraightTrack.cpp | 13 ++++++++++++-
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/Editor.cpp b/src/Editor.cpp
index b9488f9..aff9fdb 100644
--- a/src/Editor.cpp
+++ b/src/Editor.cpp
@@ -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)
diff --git a/src/StraightTrack.cpp b/src/StraightTrack.cpp
index f31b594..d958ae7 100644
--- a/src/StraightTrack.cpp
+++ b/src/StraightTrack.cpp
@@ -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));
 }
-- 
2.39.5