Waypoint.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. /******************************************************************************/
  4. /******************************************************************************/
  5. void EditWaypointPoint::setPos(C Vec &pos ) {T.pos=pos; pos_time.getUTC();}
  6. void EditWaypointPoint::setRemoved(bool removed) {T.removed=removed; removed_time.getUTC();}
  7. bool EditWaypointPoint::newer(C EditWaypointPoint &src)C
  8. {
  9. return removed_time>src.removed_time || pos_time>src.pos_time || order_time>src.order_time;
  10. }
  11. bool EditWaypointPoint::equal(C EditWaypointPoint &src)C
  12. {
  13. return removed_time==src.removed_time && pos_time==src.pos_time && order_time==src.order_time;
  14. }
  15. bool EditWaypointPoint::sync(C EditWaypointPoint &src)
  16. {
  17. bool changed=false;
  18. // 'order_time' is synced in 'EditWaypoint.sync'
  19. changed|=Sync(removed_time, src.removed_time, removed, src.removed);
  20. changed|=Sync( pos_time, src. pos_time, pos, src. pos);
  21. return changed;
  22. }
  23. bool EditWaypointPoint::undo(C EditWaypointPoint &src)
  24. {
  25. bool changed=false;
  26. changed|=Undo(removed_time, src.removed_time, removed, src.removed);
  27. changed|=Undo( pos_time, src. pos_time, pos, src. pos);
  28. changed|=Undo( order_time, src. order_time);
  29. return changed;
  30. }
  31. bool EditWaypointPoint::save(File &f)C
  32. {
  33. f.cmpUIntV(0);
  34. f<<id<<removed<<pos<<removed_time<<pos_time<<order_time;
  35. return f.ok();
  36. }
  37. bool EditWaypointPoint::load(File &f)
  38. {
  39. switch(f.decUIntV())
  40. {
  41. case 0:
  42. {
  43. f>>id>>removed>>pos>>removed_time>>pos_time>>order_time;
  44. if(f.ok())return true;
  45. }break;
  46. }
  47. return false;
  48. }
  49. int EditWaypoint::FindI(C Memc<EditWaypointPoint> &waypoints, C UID &waypoint_id) {REPA(waypoints)if(waypoints[i].id==waypoint_id)return i; return -1;}
  50. EditWaypointPoint* EditWaypoint::Find( Memc<EditWaypointPoint> &waypoints, C UID &waypoint_id) {int i=FindI(waypoints, waypoint_id); return (i>=0) ? &waypoints[i] : null;}
  51. void EditWaypoint::del() {clear();}
  52. int EditWaypoint::findI(C UID &waypoint_id)C {return FindI(T, waypoint_id);}
  53. EditWaypointPoint* EditWaypoint::find(C UID &waypoint_id) {return Find (T, waypoint_id);}
  54. C EditWaypointPoint* EditWaypoint::find(C UID &waypoint_id)C {return ConstCast(T).find(waypoint_id);}
  55. int EditWaypoint::liveIndex(C UID &waypoint_id) {int n=0; FREPA(T)if(!T[i].removed){if(T[i].id==waypoint_id)return n; n++;} return -1;}
  56. bool EditWaypoint::newer(C EditWaypoint &src)C
  57. {
  58. if(loop_mode_time>src.loop_mode_time)return true;
  59. if( removed_time>src. removed_time)return true;
  60. if( name_time>src. name_time)return true;
  61. REPA(T){C EditWaypointPoint &e=T[i], *s=src.find(e.id); if(!s || e.newer(*s))return true;}
  62. return false;
  63. }
  64. bool EditWaypoint::equal(C EditWaypoint &src)C
  65. {
  66. if(loop_mode_time!=src.loop_mode_time)return false;
  67. if( removed_time!=src. removed_time)return false;
  68. if( name_time!=src. name_time)return false;
  69. if(elms()!=src.elms())return false;
  70. REPA(T){C EditWaypointPoint &e=T[i], *s=src.find(e.id); if(!s || !e.equal(*s))return false;}
  71. return true;
  72. }
  73. bool EditWaypoint::getRect(Rect &rect)C
  74. {
  75. bool found=false; REPA(T)if(!T[i].removed)Include(rect, found, T[i].pos.xz());
  76. return found;
  77. }
  78. bool EditWaypoint::hasExisting()C { REPA(T)if(!T[i].removed)return true; return false;}
  79. UID EditWaypoint::firstExisting()C {FREPA(T)if(!T[i].removed)return T[i].id; return UIDZero;}
  80. UID EditWaypoint::existingNear(C UID &point_id)C
  81. {
  82. REPA(T)if(T[i].id==point_id)
  83. {
  84. REPD(j, i)if(!T[j].removed)return T[j].id; // try before
  85. for(int j=i+1; j<elms(); j++)if(!T[j].removed)return T[j].id; // try after
  86. break;
  87. }
  88. return UIDZero;
  89. }
  90. Vec EditWaypoint::pos()C
  91. {
  92. FREPA(T)if(!T[i].removed)return T[i].pos;
  93. if(elms())return T[0].pos;
  94. return Vec(0);
  95. }
  96. int EditWaypoint::existingPoints()C
  97. {
  98. int n=0; REPA(T)if(!T[i].removed)n++;
  99. return n;
  100. }
  101. EditWaypoint& EditWaypoint::setRemoved(bool removed ) {T.removed =removed ; removed_time.getUTC(); return T;}
  102. EditWaypoint& EditWaypoint::setLoopMode(Game::Waypoint::LOOP_MODE loop_mode) {T.loop_mode=loop_mode; loop_mode_time.getUTC(); return T;}
  103. EditWaypoint& EditWaypoint::setName(C Str &name ) {T.name =name ; name_time.getUTC(); return T;}
  104. bool EditWaypoint::sync(C EditWaypoint &src)
  105. {
  106. bool changed=false;
  107. changed|=Sync(loop_mode_time, src.loop_mode_time, loop_mode, src.loop_mode);
  108. changed|=Sync( removed_time, src. removed_time, removed, src. removed);
  109. changed|=Sync( name_time, src. name_time, name, src. name);
  110. Memc<EditWaypointPoint> this_waypoints, src_waypoints=src;
  111. Swap(SCAST(Memc<EditWaypointPoint>, T), this_waypoints);
  112. for(; this_waypoints.elms() || src_waypoints.elms(); )
  113. {
  114. EditWaypointPoint *t=this_waypoints.addr(0), // this elm
  115. *s= src_waypoints.addr(0), // src elm
  116. *t_in_s=(t ? Find( src_waypoints, t->id) : null), // this in src
  117. *s_in_t=(s ? Find(this_waypoints, s->id) : null); // src in this
  118. if(t && t_in_s && t_in_s->order_time>t->order_time) // this elm is present in src and there it has newer order
  119. {
  120. changed=true; t_in_s->sync(*t); this_waypoints.removeData(t, true);
  121. }else
  122. if(s && s_in_t && s_in_t->order_time>s->order_time) // src elm is present in this and there it has newer order
  123. {
  124. changed=true; s_in_t->sync(*s); src_waypoints.removeData(s, true);
  125. }else
  126. if(t && (!s || t->order_time>=s->order_time)) // there is this elm which is same or newer than src elm
  127. {
  128. if(t_in_s){changed|=t->sync(*t_in_s); src_waypoints.removeData(t_in_s, true);}
  129. Swap(*t, New()); this_waypoints.removeData(t, true);
  130. }else
  131. if(s)
  132. {
  133. changed=true; // always changed because we're taking src element instead of this elm
  134. if(s_in_t){changed|=s->sync(*s_in_t); this_waypoints.removeData(s_in_t, true);}
  135. Swap(*s, New()); src_waypoints.removeData(s, true);
  136. }else Exit("EditWaypoint::sync");
  137. }
  138. return changed;
  139. }
  140. void EditWaypoint::undo(C EditWaypoint &src)
  141. {
  142. bool changed=false;
  143. changed|=Undo(loop_mode_time, src.loop_mode_time, loop_mode, src.loop_mode);
  144. changed|=Undo( removed_time, src. removed_time, removed, src. removed);
  145. changed|=Undo( name_time, src. name_time, name, src. name);
  146. Memc<EditWaypointPoint> this_waypoints, src_waypoints=src;
  147. Swap(SCAST(Memc<EditWaypointPoint>, T), this_waypoints);
  148. for(; this_waypoints.elms() || src_waypoints.elms(); )
  149. {
  150. EditWaypointPoint *t=this_waypoints.addr(0), // this elm
  151. *s= src_waypoints.addr(0), // src elm
  152. *t_in_s=(t ? Find( src_waypoints, t->id) : null), // this in src
  153. *s_in_t=(s ? Find(this_waypoints, s->id) : null); // src in this
  154. if(t && !t_in_s) // this elm is present and it is not found in src (which means that it was created later, so add it first as removed)
  155. {
  156. if(!t->removed){t->removed=true; t->removed_time++;} // mark as removed if necessary
  157. Swap(*t, New()); this_waypoints.removeData(t, true);
  158. }else
  159. if(s)
  160. {
  161. if(s_in_t) // 's_in_t' is newer, so undo and insert this one
  162. {
  163. s_in_t->undo(*s); Swap(*s_in_t, New());
  164. }else
  165. {
  166. Swap(*s, New());
  167. }
  168. this_waypoints.removeData(s_in_t, true);
  169. src_waypoints.removeData(s , true);
  170. }else Exit("EditWaypoint::undo");
  171. }
  172. }
  173. int EditWaypoint::move(C UID &point_id, int index)
  174. {
  175. int elm=FindI(T, point_id);
  176. if(InRange(elm, T))
  177. {
  178. T[elm].order_time.getUTC();
  179. moveElm(elm, index);
  180. if(elm>=index)index++;
  181. }
  182. return index;
  183. }
  184. EditWaypoint& EditWaypoint::reverse()
  185. {
  186. reverseOrder(); REPAO(T).order_time.getUTC(); return T;
  187. }
  188. EditWaypoint& EditWaypoint::rotateLeft()
  189. {
  190. if(elms()){T[0].order_time.getUTC(); rotateOrder(-1);} return T; // old 1st is now new last
  191. }
  192. EditWaypoint& EditWaypoint::rotateRight()
  193. {
  194. if(elms()){rotateOrder(1); T[0].order_time.getUTC();} return T; // old last is now new 1st
  195. }
  196. void EditWaypoint::create(C Game::Waypoint &src, C Str &name)
  197. {
  198. setName(name).setRemoved(false).setLoopMode(src.loop_mode);
  199. FREPA(src)New().setPos(src.points[i].pos);
  200. }
  201. bool EditWaypoint::copyTo(Game::Waypoint &w)
  202. {
  203. w.del();
  204. if(!removed)
  205. {
  206. w.loop_mode=loop_mode;
  207. FREPA(T)if(!T[i].removed)w.points.New().pos=T[i].pos;
  208. w.updateTotalLengths();
  209. }
  210. return w.points.elms()>0;
  211. }
  212. void EditWaypoint::draw(C Color &color, int edge_smooth_steps) {Game::Waypoint w; copyTo(w); w.draw(color, color, WaypointRadius, edge_smooth_steps);}
  213. void EditWaypoint::drawText(C UID &sel, C UID &lit)
  214. {
  215. int n=0; TextStyleParams ts; ts.size=0.055f; Vec2 screen; FREPA(T)if(!T[i].removed){if(PosToScreen(T[i].pos, screen)){ts.color=((T[i].id==sel) ? LitSelColor : (T[i].id==lit) ? LitColor : DefColor); D.text(ts, screen, S+n);} n++;}
  216. }
  217. bool EditWaypoint::save(File &f)C
  218. {
  219. f.cmpUIntV(2);
  220. f<<removed<<loop_mode<<name<<removed_time<<loop_mode_time<<name_time;
  221. ::EE::Memc< ::EditWaypointPoint>::save(f);
  222. return f.ok();
  223. }
  224. bool EditWaypoint::load(File &f)
  225. {
  226. del();
  227. switch(f.decUIntV())
  228. {
  229. case 2:
  230. {
  231. f>>removed>>loop_mode>>name>>removed_time>>loop_mode_time>>name_time;
  232. if(::EE::Memc< ::EditWaypointPoint>::load(f))
  233. if(f.ok())return true;
  234. }break;
  235. case 1:
  236. {
  237. f>>removed>>loop_mode; GetStr2(f, name); f>>removed_time>>loop_mode_time>>name_time;
  238. if(::EE::Memc< ::EditWaypointPoint>::load(f))
  239. if(f.ok())return true;
  240. }break;
  241. case 0:
  242. {
  243. f>>removed>>loop_mode>>removed_time>>loop_mode_time>>name_time; GetStr(f, name);
  244. if(::EE::Memc< ::EditWaypointPoint>::load(f))
  245. if(f.ok())return true;
  246. }break;
  247. }
  248. return false;
  249. }
  250. bool EditWaypoint::load(C Str &name)
  251. {
  252. File f; if(f.readTry(name))return load(f);
  253. del(); return false;
  254. }
  255. EditWaypointPoint::EditWaypointPoint() : removed(false), id(UID().randomizeValid()), pos(0) {}
  256. EditWaypoint::EditWaypoint() : removed(false), loop_mode(Game::Waypoint::SINGLE) {}
  257. /******************************************************************************/