From 8baee19fa3d938fba3ededbc16f1797ec0584488 Mon Sep 17 00:00:00 2001 From: luckyt Date: Wed, 20 Aug 2014 12:16:37 -0600 Subject: [PATCH] Allowing choice between 3 pathing modes: go outside, prefer indoors within reason, prefer indoors at any cost --- .../src/com/lucky/watisrain/backend/Util.java | 10 ++++ .../lucky/watisrain/backend/data/Path.java | 4 +- .../WATisRain/gen/com/lucky/watisrain/R.java | 1 + workspace/WATisRain/res/menu/main.xml | 4 ++ .../src/com/lucky/watisrain/Global.java | 49 +++++++++++++++++++ .../src/com/lucky/watisrain/MainActivity.java | 10 +++- .../src/com/lucky/watisrain/map/MapView.java | 14 +++++- 7 files changed, 89 insertions(+), 3 deletions(-) diff --git a/workspace/RainBackend/src/com/lucky/watisrain/backend/Util.java b/workspace/RainBackend/src/com/lucky/watisrain/backend/Util.java index ddacfc9..26b2e45 100644 --- a/workspace/RainBackend/src/com/lucky/watisrain/backend/Util.java +++ b/workspace/RainBackend/src/com/lucky/watisrain/backend/Util.java @@ -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) diff --git a/workspace/RainBackend/src/com/lucky/watisrain/backend/data/Path.java b/workspace/RainBackend/src/com/lucky/watisrain/backend/data/Path.java index 62dcd04..e6660da 100644 --- a/workspace/RainBackend/src/com/lucky/watisrain/backend/data/Path.java +++ b/workspace/RainBackend/src/com/lucky/watisrain/backend/data/Path.java @@ -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. * @@ -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; } diff --git a/workspace/WATisRain/gen/com/lucky/watisrain/R.java b/workspace/WATisRain/gen/com/lucky/watisrain/R.java index 341330a..e48b587 100644 --- a/workspace/WATisRain/gen/com/lucky/watisrain/R.java +++ b/workspace/WATisRain/gen/com/lucky/watisrain/R.java @@ -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; } diff --git a/workspace/WATisRain/res/menu/main.xml b/workspace/WATisRain/res/menu/main.xml index 97ebcef..863d02f 100644 --- a/workspace/WATisRain/res/menu/main.xml +++ b/workspace/WATisRain/res/menu/main.xml @@ -5,4 +5,8 @@ android:icon="@android:drawable/ic_menu_close_clear_cancel" android:showAsAction="ifRoom|withText"/> + + diff --git a/workspace/WATisRain/src/com/lucky/watisrain/Global.java b/workspace/WATisRain/src/com/lucky/watisrain/Global.java index 5f73ae3..35f8b2a 100644 --- a/workspace/WATisRain/src/com/lucky/watisrain/Global.java +++ b/workspace/WATisRain/src/com/lucky/watisrain/Global.java @@ -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 { @@ -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(); + } } diff --git a/workspace/WATisRain/src/com/lucky/watisrain/MainActivity.java b/workspace/WATisRain/src/com/lucky/watisrain/MainActivity.java index 40db5dc..a0ae715 100644 --- a/workspace/WATisRain/src/com/lucky/watisrain/MainActivity.java +++ b/workspace/WATisRain/src/com/lucky/watisrain/MainActivity.java @@ -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; } diff --git a/workspace/WATisRain/src/com/lucky/watisrain/map/MapView.java b/workspace/WATisRain/src/com/lucky/watisrain/map/MapView.java index 40d4431..3ff5172 100644 --- a/workspace/WATisRain/src/com/lucky/watisrain/map/MapView.java +++ b/workspace/WATisRain/src/com/lucky/watisrain/map/MapView.java @@ -236,7 +236,6 @@ else if(selectedBuilding1 == null){ selectedBuilding2 = closestBuilding.getName(); updateRoute(); - directionsView.generateDirectionsFromRoute(route); } } @@ -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(); }