Fix RequestButton clearing
[cs356-p1-elevator.git] / elevator.cpp
index d4e7cfb..62dfb19 100644 (file)
@@ -1,21 +1,24 @@
 #include "elevator.hpp"
+#include "main.hpp"
 
-Elevator::Elevator ()
+Elevator::Elevator (int elevator_number)
        : state_(STATE_IDLE)
        , wait_(0)
        , direction_(IDLE)
        , position_()
        , stops_()
+       , number_(elevator_number)
 {
        // Intentionally Left Empty
 }
 
-Elevator::Elevator (int starting_floor)
+Elevator::Elevator (int starting_floor, int elevator_number)
        : state_(STATE_IDLE)
        , wait_(0)
        , direction_(IDLE)
        , position_(starting_floor)
        , stops_()
+       , number_(elevator_number)
 {
        // Intentionally Left Empty
 }
@@ -59,6 +62,14 @@ bool Elevator::currently_at_stop () const
                if (*it == current)
                        return true;
 
+       /* Check if we are IDLE. If so, only the position needs to match */
+       if (direction_ == IDLE)
+       {
+               for (it = stops_.begin (); it != stops_.end (); it++)
+                       if (it->getPosition() == position_)
+                               return true;
+       }
+
        /* No match */
        return false;
 }
@@ -115,6 +126,7 @@ void Elevator::transition_move_up ()
        position_ += ELEVATOR_STEP;
 
        // TODO: Call into the GUI to update the position
+       gui_update_position_label (number_, (float)position_);
        std::cout << "Updating the GUI with our position: " << position_ << std::endl;
 }
 
@@ -124,6 +136,7 @@ void Elevator::transition_move_down ()
        position_ -= ELEVATOR_STEP;
 
        // TODO: Call into the GUI to update the position
+       gui_update_position_label (number_, (float)position_);
        std::cout << "Updating the GUI with our position: " << position_ << std::endl;
 }
 
@@ -156,19 +169,42 @@ void Elevator::transition_open_door ()
         *
         * Otherwise, just clear this stop */
        if      (direction_ == UP && stops_above == 0)
+       {
                stops_.remove (Stop(position_, ALL));
+               gui_unpress_request_button (number_, (int)position_);
+               gui_unpress_call_button ((int)position_, UP);
+               gui_unpress_call_button ((int)position_, DOWN);
+       }
        else if (direction_ == DOWN && stops_below == 0)
+       {
+               stops_.remove (Stop(position_, ALL));
+               gui_unpress_request_button (number_, (int)position_);
+               gui_unpress_call_button ((int)position_, UP);
+               gui_unpress_call_button ((int)position_, DOWN);
+       }
+       else if (direction_ == IDLE)
+       {
                stops_.remove (Stop(position_, ALL));
+               gui_unpress_request_button (number_, (int)position_);
+               gui_unpress_call_button ((int)position_, UP);
+               gui_unpress_call_button ((int)position_, DOWN);
+       }
        else
+       {
                stops_.remove (Stop(position_, direction_));
+               gui_unpress_call_button ((int)position_, direction_);
+               gui_unpress_request_button (number_, (int)position_);
+       }
 
        // TODO: Call into the GUI to open the door
+       gui_open_door (number_, (int)position_);
        std::cout << "Opening Door" << std::endl;
 }
 
 void Elevator::transition_close_door ()
 {
        // TODO: Call into the GUI to close the door
+       gui_close_door (number_, (int)position_);
        std::cout << "Closing Door" << std::endl;
 }