OSDN Git Service

b343545efaa24e876df1088e67d19a21bb75a5d1
[gvonavish/GVONavish.git] / GVONavish / GVONavish / GVOShipRouteList.cpp
1 #include "stdafx.h"
2 #include "GVOShipRouteList.h"
3
4
5
6
7 void GVOShipRouteList::closeRoute()
8 {
9         if ( !m_shipRouteList.empty() ) {
10                 m_shipRouteList.back()->setFix( true );
11         }
12 }
13
14
15 void GVOShipRouteList::addRoutePoint( const GVONormalizedPoint point )
16 {
17         if ( m_shipRouteList.empty() || m_shipRouteList.back()->isFixed() ) {
18                 addRoute();
19         }
20         m_shipRouteList.back()->addRoutePoint( point );
21         if ( m_observer ) {
22                 m_observer->onShipRouteListUpdateRoute( m_shipRouteList.back() );
23         }
24 }
25
26
27 void GVOShipRouteList::removeShipRoute( GVOShipRoutePtr shipRoute )
28 {
29         _ASSERT( shipRoute != nullptr );
30         auto it = std::find( m_shipRouteList.begin(), m_shipRouteList.end(), shipRoute );
31         if ( it == m_shipRouteList.end() ) {
32                 return;
33         }
34         GVOShipRoutePtr removeTarget = shipRoute;
35         m_shipRouteList.erase( it );
36         if ( m_observer ) {
37                 m_observer->onShipRouteListRemoveItem( removeTarget );
38         }
39 }
40
41
42 void GVOShipRouteList::clearAllItems()
43 {
44         m_shipRouteList.clear();
45         if ( m_observer ) {
46                 m_observer->onShipRouteListRemoveAllItems();
47         }
48 }
49
50
51 void GVOShipRouteList::addRoute()
52 {
53         // \92Ç\89Á\82Æ\92Ê\92m
54         m_shipRouteList.push_back( GVOShipRoutePtr( new GVOShipRoute() ) );
55         if ( m_observer ) {
56                 m_observer->onShipRouteListAddRoute( m_shipRouteList.back() );
57         }
58
59         // \88ì\82ê\82½\95ª\82ð\8dí\8f\9c
60         // \82Æ\82è\82 \82¦\82¸\83^\83R\83R\81[\83h\82Å\8bð\92¼\82É\8f\91\82¢\82Ä\82¨\82­\81B
61         int favorits = 0;
62         for ( auto route : m_shipRouteList ) {
63                 if ( route->isFavorite() ) {
64                         ++favorits;
65                 }
66         }
67         const int overCount = m_shipRouteList.size() - (m_maxRouteCountWithoutFavorits + favorits);
68         if ( 0 < overCount ) {
69                 int removeCount = 0;
70                 auto it = m_shipRouteList.begin();
71                 while ( it != m_shipRouteList.end() && removeCount < overCount) {
72                         if ( (*it)->isFavorite() ) {
73                                 ++it;
74                                 continue;
75                         }
76                         auto itNext = std::next( it, 1 );
77                         m_shipRouteList.erase( it );
78                         ++removeCount;
79                         it = itNext;
80                 }
81         }
82 }
83
84
85 void GVOShipRouteList::joinPreviousRouteAtReverseIndex( int reverseIndex )
86 {
87         _ASSERT( 0 <= reverseIndex );
88         _ASSERT( reverseIndex < (int)m_shipRouteList.size() );
89
90         RouteList::iterator itBase;
91         itBase = m_shipRouteList.begin();
92         std::advance( itBase, indexFromReverseIndex( reverseIndex ) );
93         RouteList::iterator itPrev = std::prev( itBase );
94         _ASSERT( itPrev != m_shipRouteList.end() );
95         GVOShipRoutePtr baseRoute = *itBase;
96         GVOShipRoutePtr prevRoute = *itPrev;
97
98         m_shipRouteList.erase( itPrev );
99
100         // remove\82µ\82½\8e\9e\82É\92Ê\92m\83n\83\93\83h\83\89\82Å\91®\90«\82ª\95Ï\8dX\82³\82ê\82Ä\82µ\82Ü\82¤\82Ì\82Å\81A
101         // \82±\82Ì\8e\9e\93_\82Å\8b­\92²\91®\90«\82ð\95Û\91\82µ\82Ä\82¨\82­\81B
102         bool isHilight = prevRoute->isHilight() | baseRoute->isHilight();
103         if ( m_observer ) {
104                 m_observer->onShipRouteListRemoveItem( prevRoute );
105         }
106
107         baseRoute->jointPreviousLinesWithRoute( *prevRoute );
108         baseRoute->setHilight( isHilight );
109
110         // \92Ê\92m
111         if ( m_observer ) {
112                 m_observer->onShipRouteListUpdateRoute( baseRoute );
113         }
114 }