Cleanup view_object functions 60/88660/1
authorVarun <tallytalwar@gmail.com>
Thu, 15 Sep 2016 21:07:20 +0000 (17:07 -0400)
committerVarun <tallytalwar@gmail.com>
Tue, 20 Sep 2016 06:58:23 +0000 (02:58 -0400)
Change-Id: I3d29c0b9ab1b4fbd4d54225cf1842d53e1d5c7ea

src/mapzen/tangram_view.cpp
src/mapzen/tangram_view.hpp

index 497e96b..12472c9 100644 (file)
@@ -413,11 +413,6 @@ mapzen_error_e TangramView::onViewObject(maps_view_h view, const maps_view_objec
                return MAPZEN_ERROR_SERVICE_NOT_AVAILABLE;
        }
 
-       return processViewObject(view, object, operation);
-}
-
-mapzen_error_e TangramView::processViewObject(maps_view_h view, const maps_view_object_h object, maps_view_object_operation_e operation)
-{
        mapzen_error_e error = MAPZEN_ERROR_NONE;
 
        TangramView *tv = nullptr;
@@ -488,31 +483,23 @@ mapzen_error_e TangramView::addObject(maps_view_object_h object)
        MapViewObjects::iterator iter;
        MapViewObjects::iterator end;
 
-       switch(type) {
-               case MAPS_VIEW_OBJECT_MARKER:
-               case MAPS_VIEW_OBJECT_POLYGON:
-               case MAPS_VIEW_OBJECT_POLYLINE:
-                       {
+       {
+               std::lock_guard<std::mutex> lock(m_viewObjectMutex);
+               iter = m_mapViewObjs.find(object);
+               end = m_mapViewObjs.end();
+       }
+       if (iter == end) {
+               tvMarker = m_map->markerAdd();
+               if (tvMarker) {
+                       error = updateObject(object, tvMarker);
+                       if (error == MAPZEN_ERROR_NONE) {
                                std::lock_guard<std::mutex> lock(m_viewObjectMutex);
-                               iter = m_mapViewObjs.find(object);
-                               end = m_mapViewObjs.end();
+                               m_mapViewObjs.insert(std::make_pair(object, tvMarker));
+                       } else {
+                               m_map->markerRemove(tvMarker);
+                               tvMarker = 0;
                        }
-                       if (iter == end) {
-                               tvMarker = m_map->markerAdd();
-                               if (tvMarker) {
-                                       error = updateObject(object, tvMarker);
-                                       if (error == MAPZEN_ERROR_NONE) {
-                                               std::lock_guard<std::mutex> lock(m_viewObjectMutex);
-                                               m_mapViewObjs.insert(std::make_pair(object, tvMarker));
-                                       } else {
-                                               m_map->markerRemove(tvMarker);
-                                               tvMarker = 0;
-                                       }
-                               }
-                       }
-                       break;
-               default:
-                       break;
+               }
        }
 
        return error;
@@ -533,110 +520,136 @@ mapzen_error_e TangramView::updateObject(maps_view_object_h object, Tangram::Mar
 {
        if (!object || !tvMarker || !m_map) { return MAPZEN_ERROR_INVALID_PARAMETER; }
 
-       int error = MAPS_ERROR_NONE;
+       mapzen_error_e error = MAPZEN_ERROR_NONE;
        maps_view_object_type_e type = MAPS_VIEW_OBJECT_POLYLINE;
 
        maps_view_object_get_type(object, &type);
 
-       std::string styleString = "";
-
-       Tangram::Coordinates coords = {};
-       unsigned char r = 0, g = 0, b = 0, a = 0;
-
        switch(type) {
                case MAPS_VIEW_OBJECT_MARKER:
-                       {
-                               // TODO: use image passed with the view object, not available in tangram yet!
-                               maps_view_marker_type_e markerType;
-                               error = maps_view_object_marker_get_type(object, &markerType);
-                               if (error != MAPS_ERROR_NONE || markerType < MAPS_VIEW_MARKER_PIN || markerType > MAPS_VIEW_MARKER_STICKER) { break; }
-
-                               maps_coordinates_h mapsCoord = nullptr;
-                               double lat = 0.0, lng = 0.0;
-                               int markerWidth = 0, markerHeight = 0;
-
-                               const char* styleFormat = "{ style: 'ux-icons-overlay', sprite: 'ux-search-active', size: [%dpx, %dpx], collide: false, anchor: %s, transition: { [show, hide]: { time: 0s } } }";
-                               static std::string anchor;
-
-                               anchor = "center";
-                               if (markerType == MAPS_VIEW_MARKER_PIN) {
-                                       anchor = "top";
-                               }
-
-                               error = maps_view_object_marker_get_size(object, &markerWidth, &markerHeight);
-                               if (error != MAPS_ERROR_NONE) { break; }
-
-                               int sz = std::snprintf(nullptr, 0, styleFormat, markerWidth, markerHeight, anchor.c_str());
-                               styleString.resize(sz+1);
-                               std::snprintf(&styleString[0], styleString.size(), styleFormat, markerWidth, markerHeight, anchor.c_str());
-
-                               m_map->markerSetStyling(tvMarker, styleString.c_str());
-
-                               error = maps_view_object_marker_get_coordinates(object, &mapsCoord);
-                               if (error != MAPS_ERROR_NONE) { break; }
-
-                               error = maps_coordinates_get_longitude(mapsCoord, &lng);
-                               if (error != MAPS_ERROR_NONE) {
-                                       maps_coordinates_destroy(mapsCoord);
-                                       break;
-                               }
-
-                               error = maps_coordinates_get_latitude(mapsCoord, &lat);
-                               if (error != MAPS_ERROR_NONE) {
-                                       maps_coordinates_destroy(mapsCoord);
-                                       break;
-                               }
-
-                               maps_coordinates_destroy(mapsCoord);
-                               m_map->markerSetPoint(tvMarker, Tangram::LngLat(lng, lat));
-                       }
+                       error = updateMarker(object, tvMarker);
                        break;
                case MAPS_VIEW_OBJECT_POLYLINE:
-                       {
-                               int width = 0;
-                               const char* styleFormat = "{ style: 'ux-route-line-overlay', color: [%d, %d, %d, %d], width: %dpx, order: 500 }";
-
-                               error = maps_view_object_polyline_get_color(object, &r, &g, &b, &a);
-                               if (error != MAPS_ERROR_NONE) { break; }
+                       error = updatePolyline(object, tvMarker);
+                       break;
+               case MAPS_VIEW_OBJECT_POLYGON:
+                       error = updatePolygon(object, tvMarker);
+                       break;
+               default:
+                       break;
+       }
 
-                               error = maps_view_object_polyline_get_width(object, &width);
-                               if (error != MAPS_ERROR_NONE) { break; }
+       return (mapzen_error_e)convert_maps_error_to_mapzen_error(error);
+}
 
-                               int sz = std::snprintf(nullptr, 0, styleFormat, r, g, b, a, width);
-                               styleString.resize(sz+1);
-                               std::snprintf(&styleString[0], styleString.size(), styleFormat, r, g, b, a, width);
+mapzen_error_e TangramView::updateMarker(maps_view_object_h object, Tangram::MarkerID tvMarker)
+{
+       std::string styleString = "";
+       maps_coordinates_h mapsCoord = nullptr;
+       double lat = 0.0, lng = 0.0;
+       int markerWidth = 0, markerHeight = 0;
+       int error = MAPS_ERROR_NONE;
+       static std::string anchor;
+       const char* styleFormat = "{ style: 'ux-icons-overlay', sprite: 'ux-search-active', size: [%dpx, %dpx], collide: false, anchor: %s, transition: { [show, hide]: { time: 0s } } }";
 
-                               m_map->markerSetStyling(tvMarker, styleString.c_str());
+       maps_view_marker_type_e type;
+       error = maps_view_object_marker_get_type(object, &type);
 
-                               error = maps_view_object_polyline_foreach_point(object, emplaceCoord, &coords);
-                               if (error != MAPS_ERROR_NONE) { break; }
+       do {
+               if (error != MAPS_ERROR_NONE || type < MAPS_VIEW_MARKER_PIN || type > MAPS_VIEW_MARKER_STICKER) { break; }
 
-                               m_map->markerSetPolyline(tvMarker, coords.data(), static_cast<int>(coords.size()));
-                       }
-                       break;
-               case MAPS_VIEW_OBJECT_POLYGON:
-                       {
-                               const char* styleFormat = "{ style: 'polygons', color: [%d, %d, %d, %d] }";
+               anchor = "center";
+               if (type == MAPS_VIEW_MARKER_PIN) {
+                       anchor = "top";
+               }
 
-                               error = maps_view_object_polygon_get_fill_color(object, &r, &g, &b, &a);
-                               if (error != MAPS_ERROR_NONE) { break; }
+               error = maps_view_object_marker_get_size(object, &markerWidth, &markerHeight);
+               if (error != MAPS_ERROR_NONE) { break; }
 
-                               int sz = std::snprintf(nullptr, 0, styleFormat, r, g, b, a);
-                               styleString.resize(sz+1);
-                               std::snprintf(&styleString[0], styleString.size(), styleFormat, r, g, b, a);
+               int sz = std::snprintf(nullptr, 0, styleFormat, markerWidth, markerHeight, anchor.c_str());
+               styleString.resize(sz+1);
+               std::snprintf(&styleString[0], styleString.size(), styleFormat, markerWidth, markerHeight, anchor.c_str());
 
-                               m_map->markerSetStyling(tvMarker, styleString.c_str());
+               m_map->markerSetStyling(tvMarker, styleString.c_str());
 
-                               error = maps_view_object_polygon_foreach_point(object, emplaceCoord, &coords);
-                               if (error != MAPS_ERROR_NONE) { break; }
+               error = maps_view_object_marker_get_coordinates(object, &mapsCoord);
+               if (error != MAPS_ERROR_NONE) { break; }
 
-                               int count = (int)coords.size();
-                               m_map->markerSetPolygon(tvMarker, coords.data(), &count, 1);
-                       }
+               error = maps_coordinates_get_longitude(mapsCoord, &lng);
+               if (error != MAPS_ERROR_NONE) {
+                       maps_coordinates_destroy(mapsCoord);
                        break;
-               default:
+               }
+
+               error = maps_coordinates_get_latitude(mapsCoord, &lat);
+               if (error != MAPS_ERROR_NONE) {
+                       maps_coordinates_destroy(mapsCoord);
                        break;
-       }
+               }
+
+               maps_coordinates_destroy(mapsCoord);
+               m_map->markerSetPoint(tvMarker, Tangram::LngLat(lng, lat));
+       } while (0);
+
+       return (mapzen_error_e)convert_maps_error_to_mapzen_error(error);
+}
+
+mapzen_error_e TangramView::updatePolyline(maps_view_object_h object, Tangram::MarkerID tvMarker)
+{
+       std::string styleString = "";
+       int error = MAPS_ERROR_NONE;
+       Tangram::Coordinates coords = {};
+       unsigned char r = 0, g = 0, b = 0, a = 0;
+       int width = 0;
+
+       const char* styleFormat = "{ style: 'ux-route-line-overlay', color: [%d, %d, %d, %d], width: %dpx, order: 500 }";
+
+       do {
+               error = maps_view_object_polyline_get_color(object, &r, &g, &b, &a);
+               if (error != MAPS_ERROR_NONE) { break; }
+
+               error = maps_view_object_polyline_get_width(object, &width);
+               if (error != MAPS_ERROR_NONE) { break; }
+
+               int sz = std::snprintf(nullptr, 0, styleFormat, r, g, b, a, width);
+               styleString.resize(sz+1);
+               std::snprintf(&styleString[0], styleString.size(), styleFormat, r, g, b, a, width);
+
+               m_map->markerSetStyling(tvMarker, styleString.c_str());
+
+               error = maps_view_object_polyline_foreach_point(object, emplaceCoord, &coords);
+               if (error != MAPS_ERROR_NONE) { break; }
+
+               m_map->markerSetPolyline(tvMarker, coords.data(), static_cast<int>(coords.size()));
+       } while (0);
+
+       return (mapzen_error_e)convert_maps_error_to_mapzen_error(error);
+}
+
+mapzen_error_e TangramView::updatePolygon(maps_view_object_h object, Tangram::MarkerID tvMarker)
+{
+       std::string styleString = "";
+       int error = MAPS_ERROR_NONE;
+       Tangram::Coordinates coords = {};
+       unsigned char r = 0, g = 0, b = 0, a = 0;
+       const char* styleFormat = "{ style: 'polygons', color: [%d, %d, %d, %d] }";
+
+       do {
+               error = maps_view_object_polygon_get_fill_color(object, &r, &g, &b, &a);
+               if (error != MAPS_ERROR_NONE) { break; }
+
+               int sz = std::snprintf(nullptr, 0, styleFormat, r, g, b, a);
+               styleString.resize(sz+1);
+               std::snprintf(&styleString[0], styleString.size(), styleFormat, r, g, b, a);
+
+               m_map->markerSetStyling(tvMarker, styleString.c_str());
+
+               error = maps_view_object_polygon_foreach_point(object, emplaceCoord, &coords);
+               if (error != MAPS_ERROR_NONE) { break; }
+
+               int count = (int)coords.size();
+               m_map->markerSetPolygon(tvMarker, coords.data(), &count, 1);
+       } while (0);
 
        return (mapzen_error_e)convert_maps_error_to_mapzen_error(error);
 }
@@ -820,7 +833,6 @@ void TangramView::pixelGetCb(void *data, Evas_Object *obj)
        } else {
                MAPS_LOGD("tv->m_map is null, in pixelGelCb");
        }
-
 }
 
 void TangramView::renderingCb(void *data) {
index b059538..c3a9103 100644 (file)
@@ -65,6 +65,9 @@ private:
        mapzen_error_e addObject(maps_view_object_h object);
        mapzen_error_e setObjectVisible(maps_view_object_h object, Tangram::MarkerID tvMarker);
        mapzen_error_e updateObject(maps_view_object_h object, Tangram::MarkerID tvMarker);
+       mapzen_error_e updateMarker(maps_view_object_h object, Tangram::MarkerID tvMarker);
+       mapzen_error_e updatePolyline(maps_view_object_h object, Tangram::MarkerID tvMarker);
+       mapzen_error_e updatePolygon(maps_view_object_h object, Tangram::MarkerID tvMarker);
        mapzen_error_e removeObject(maps_view_object_h object);
        MapViewObjects& mapViewObjects() { return m_mapViewObjs; }
        std::mutex& viewObjectMutex() { return m_viewObjectMutex; }
@@ -73,7 +76,6 @@ private:
        static void readyMapCb(void *view);
        static void renderingCb(void *data);
        static void pixelGetCb(void *data, Evas_Object *obj);
-       static mapzen_error_e processViewObject(maps_view_h view, const maps_view_object_h object, maps_view_object_operation_e operation);
        // Emplaces the lng/lat from `coordinate` to `user_data` (Tangram::Coordinates)
        static bool emplaceCoord(int index, maps_coordinates_h coordinate, void *user_data);