Skip to content

Commit

Permalink
Allowing choice between 3 pathing modes: go outside, prefer indoors w…
Browse files Browse the repository at this point in the history
…ithin reason, prefer indoors at any cost
  • Loading branch information
lucky-bai committed Aug 20, 2014
1 parent ea95952 commit 8baee19
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
10 changes: 10 additions & 0 deletions workspace/RainBackend/src/com/lucky/watisrain/backend/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public static int getFloor(String combinedID){
}


/**
* HACK: This is a global variable to make the pathing weight adjustable from
* outside the backend module.
*
* This number represents the "unwillingness" ratio of going outside. For example,
* 3.0 means that travelling 1m outside is equal to travelling 3m outside.
*/
public static double GLOBAL_PATHING_WEIGHT = 3.0;


/**
* Given two vectors a and b, return the unit vector that goes in the opposite direction
* of them. For example, if a = (1,0) and b = (0,1), the return (-sqrt 2, -sqrt 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.List;

import com.lucky.watisrain.backend.Util;

/**
* A path is an undirected edge in the graph.
*
Expand Down Expand Up @@ -118,7 +120,7 @@ public double getCost(){
distance += waypoints.get(i).distanceTo(waypoints.get(i+1));
}

if(!isIndoors()) return 3 * distance;
if(!isIndoors()) return Util.GLOBAL_PATHING_WEIGHT * distance;
else return distance;
}

Expand Down
1 change: 1 addition & 0 deletions workspace/WATisRain/gen/com/lucky/watisrain/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static final class drawable {
}
public static final class id {
public static final int action_clear=0x7f080002;
public static final int action_settings=0x7f080003;
public static final int directions_view=0x7f080001;
public static final int mapImageView=0x7f080000;
}
Expand Down
4 changes: 4 additions & 0 deletions workspace/WATisRain/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:showAsAction="ifRoom|withText"/>

<item android:id="@+id/action_settings"
android:title="Settings"
android:showAsAction="never"/>

</menu>
49 changes: 49 additions & 0 deletions workspace/WATisRain/src/com/lucky/watisrain/Global.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.lucky.watisrain;

import com.lucky.watisrain.backend.Util;
import com.lucky.watisrain.map.MapView;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;

public class Global {
Expand All @@ -22,4 +29,46 @@ public static void println(Object s){
Log.d("DEBUG_MSG", s.toString());
}



static String[] pathing_choices = new String[]{"None","Within reason","At any cost"};
static int pathing_selected = 1;

/**
* Show the settings dialog (to select pathing mode)
* Automatically recalculate the route when settings change.
*/
public static void showSettings(Context context, final MapView mapview){

AlertDialog.Builder db = new AlertDialog.Builder(context);
db.setTitle("Prefer indoors:");
db.setSingleChoiceItems(pathing_choices, pathing_selected, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

// no change
if(which == pathing_selected) return;

pathing_selected = which;

switch(which){
case 0:
Util.GLOBAL_PATHING_WEIGHT = 1.0;
break;
case 1:
Util.GLOBAL_PATHING_WEIGHT = 3.0;
break;
case 2:
Util.GLOBAL_PATHING_WEIGHT = 100.0;
break;
}

mapview.recalculateRoute();
}
});

db.setPositiveButton("OK", null);
db.create().show();
}
}
10 changes: 9 additions & 1 deletion workspace/WATisRain/src/com/lucky/watisrain/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ protected void onStart(){

// Handle action bar
public boolean onOptionsItemSelected(MenuItem item){
mapView.clearRoute();
switch(item.getItemId()){
case R.id.action_clear:
mapView.clearRoute();
break;
case R.id.action_settings:
Global.showSettings(this, mapView);
break;
}

return true;
}

Expand Down
14 changes: 13 additions & 1 deletion workspace/WATisRain/src/com/lucky/watisrain/map/MapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ else if(selectedBuilding1 == null){
selectedBuilding2 = closestBuilding.getName();

updateRoute();
directionsView.generateDirectionsFromRoute(route);
}

}
Expand All @@ -262,6 +261,19 @@ private void updateRoute(){
route = routefinder.findRoute(map.getBuildingByID(selectedBuilding1),
map.getBuildingByID(selectedBuilding2)).getContractedRoute();

directionsView.generateDirectionsFromRoute(route);
}


/**
* Recalculate the route if applicable, possibly with a different global pathing
* value. If no route is selected, do nothing.
*/
public void recalculateRoute(){
if(selectedBuilding1 == null || selectedBuilding2 == null) return;

updateRoute();
invalidate();
}


Expand Down

0 comments on commit 8baee19

Please sign in to comment.