classdef ScenarioGUIHelper < handle
% helper functionality for build_road_gui scenario builder.
    properties
        frame_center = [0,0]
        
        scenario_file
        cursor_cell
        fig
        scene
        
        start_state_gui_vals
        vehicle_start_state_gui_vals
        vehicle_start_state
        start_state
    end
    
    methods
        function obj = ScenarioGUIHelper(fig, scenario_file)
            obj.fig = fig;
            
            obj.scenario_file = scenario_file;
            obj.scene = ScenarioDescription(scenario_file);
            obj.cursor_cell = [0, 0];
                        
            obj.refresh()
        end
        
        function refresh(obj)
            % Redraws the scenario.
            obj.scene = ScenarioDescription(obj.scenario_file);
            if obj.scene.is_start_set
                obj.scene.drawFrame(obj.fig, obj.frame_center, obj.scene.start_pos, obj.scene.start_angle)
            else
                obj.scene.drawFrame(obj.fig, obj.frame_center)
            end
            
            obj.plotGrid()
            obj.plotCursor()
        end
        
        function plotGrid(obj)
            ax = gca;
            ax.XTick = obj.scene.plot_axis(1)-obj.scene.half_cell_width:obj.scene.cell_width:obj.scene.plot_axis(2);
            ax.YTick = obj.scene.plot_axis(3)-obj.scene.half_cell_width:obj.scene.cell_width:obj.scene.plot_axis(4);
            grid on
        end
        
        function plotCursor(obj)
            cursor_width = obj.scene.cell_width*.1;
            x_lower_left = obj.frame_center(1) + obj.scene.cell_width*obj.cursor_cell(1) - obj.scene.half_cell_width;
            y_lower_left = obj.frame_center(2) + obj.scene.cell_width*obj.cursor_cell(2) - obj.scene.half_cell_width;
            rectangle('Position',[x_lower_left y_lower_left obj.scene.cell_width obj.scene.cell_width],'EdgeColor',[0,0,0],'LineWidth',cursor_width,'FaceColor','none');
        end
        
        function moveCursor(obj,offset)
            obj.cursor_cell = obj.cursor_cell + offset;
            obj.refresh()
        end
        
        function setStartStateVals(obj, vals)
            obj.start_state_gui_vals = vals;
            obj.start_state = vals;
            obj.start_state(1) = obj.scene.cell_width*obj.cursor_cell(1) + vals(1);
            obj.start_state(2) = obj.scene.cell_width*obj.cursor_cell(2) + vals(2);
        end
        
        function setVehicleStartStateVals(obj, vals)
            obj.vehicle_start_state_gui_vals = vals;
            obj.vehicle_start_state = vals;
            obj.vehicle_start_state(1) = obj.scene.cell_width*obj.cursor_cell(1) + vals(1);
            obj.vehicle_start_state(2) = obj.scene.cell_width*obj.cursor_cell(2) + vals(2);
        end
        
    end
    
end