1
0

ConcernementMapItem.vue 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. <script>
  2. import Matter from "matter-js";
  3. import MatterAttractors from "matter-attractors";
  4. // Matter.use(MatterAttractors);
  5. // import polydecomp from "poly-decomp";
  6. import paper from 'paper';
  7. // import { easeInOutQuad, easeInOutQuart } from 'easing-utils';
  8. import Tween from "@tweenjs/tween.js";
  9. import { mapState, mapActions } from 'pinia'
  10. import { ConcernementsStore } from '@/stores/concernements'
  11. import { CommonStore } from '@/stores/common'
  12. export default {
  13. inject: ['canvasMap', 'matterEngine'],
  14. data() {
  15. return {
  16. id: null,
  17. cid: null,
  18. concernement: null,
  19. active_revision: null,
  20. entities: null,
  21. superposedEntitesIDsList: [],
  22. canvas: null,
  23. ctx: null,
  24. pos : {
  25. x: 0,
  26. y: 0
  27. },
  28. ray: 100,
  29. time: 0,
  30. salientPoints: [],
  31. scale: 1,
  32. prev_scale: 1,
  33. opacity: 0,
  34. tween: null,
  35. is_open: false,
  36. is_opening: false,
  37. is_closing: false,
  38. is_hover: false,
  39. //
  40. body: null,
  41. body_parts: [],
  42. constraint: null,
  43. //
  44. paper_main_object: {},
  45. paper_groups: {}
  46. }
  47. },
  48. props: ['mapitem'],
  49. computed: {
  50. ...mapState(ConcernementsStore,['map_mode',
  51. 'concernementsByID',
  52. 'allSuperpositions_byid',
  53. 'opened_concernement',
  54. 'opened_entite_id']),
  55. ...mapState(CommonStore,['hover_elmt',
  56. 'map_item_ray',
  57. 'cartouch_width',
  58. 'cartouch_is_open',
  59. 'paper_symbol_definitions'])
  60. },
  61. created () {
  62. console.log('ConcernementMapItem', this.mapitem);
  63. // this.id = this.superposition_id ? `${this.cid}___${this.superposition_id}` : this.cid;
  64. this.id = this.mapitem.id;
  65. this.concernement = this.mapitem.concernement;
  66. this.cid = this.concernement.id;
  67. this.active_revision = this.concernement.active_revision;
  68. // console.log(`ConcernementsMapItem ${this.id} created`);
  69. // this.entites = this.concernement.entites
  70. this.entites = this.concernement.revisions_byid[this.concernement.revision_id].entites;
  71. // disable concernement if less than 3 entite
  72. if(this.entites.length < 3){
  73. this.hideShowConcernement(this.concernement.id, false);
  74. } else{
  75. // record canvas and ctx for rendering (drawing)
  76. this.canvas = this.canvasMap.canvas
  77. this.ctx = this.canvasMap.ctx
  78. // define the ray regarding the width and height of screen
  79. this.ray = this.map_item_ray;
  80. // console.log(`this.ray: ${this.ray}`);
  81. //
  82. this.parseEntityPointsValues()
  83. // this.getSalientPoints()
  84. this.getJarvisEnvelopeConvexe()
  85. // define init position of the item
  86. this.pos = this.getRandomPos();
  87. //
  88. this.getSuperposedEntitesIDsList();
  89. //
  90. this.initMatterBody()
  91. //
  92. this.initPaperObjects()
  93. }
  94. // this.setConcernementMapItem(this.cid, this);
  95. },
  96. // mounted() {
  97. // console.log(`ConcernementsMapItem ${this.concernement.id} mounted`, this.canvasMap.canvas);
  98. // },
  99. // beforeUnmount () {
  100. unmounted () {
  101. // console.log(`mapitem ${this.id} unmounted`);
  102. if(this.mapitem.clone) {
  103. // console.log(`this.paper_main_object ${this.paper_main_object.id}`, this.paper_main_object);
  104. paper.project.getItem({id:this.paper_main_object.id}).remove();
  105. };
  106. if (this.body) {
  107. Matter.Composite.remove(this.matterEngine.world, this.body);
  108. }
  109. },
  110. watch: {
  111. // canvasMap (n, o) {
  112. // console.log("concernementItem watch canvasMap", o, n);
  113. // }
  114. canvasMap: {
  115. handler (n, o){
  116. // console.log("concernementItem watch canvasMap.ctx", typeof this.canvas, o, n);
  117. if (!this.canvas) {
  118. this.initCanvasMap()
  119. }
  120. },
  121. deep: true
  122. },
  123. opened_concernement: {
  124. handler (n, o) {
  125. if (!n) { // if there is no opened concernement
  126. this.is_open = false;
  127. this.applyShuffleForces();
  128. } else {
  129. if(this.cid === n.id && this.id === n.opened_mapitem_id){
  130. this.is_open = true;
  131. }else{
  132. this.is_open = false;
  133. }
  134. }
  135. },
  136. deep: true
  137. },
  138. is_open: {
  139. handler (n, o) {
  140. // console.log('mapItem watch is_open', n, this.is_open);
  141. if(n){ // opened
  142. this.openClose(true);
  143. }else if(o) { // closed if was opened
  144. this.openClose(false);
  145. }
  146. },
  147. deep: true
  148. },
  149. map_mode: {
  150. handler (n, o) {
  151. console.log('concernementMapItem watch map_mode', o, n);
  152. if (n === 'terraindevie' && !this.opened_concernement) {
  153. this.applyShuffleForces(); // apply a little force to check the map when returning to terrain de vie
  154. }
  155. if (this.is_open & n !== o) {
  156. this.setPaperContents();
  157. }
  158. if (n !== o) {
  159. this.handlePaperVisibilityOnMapMode()
  160. }
  161. },
  162. deep: true
  163. },
  164. hover_elmt: {
  165. handler (n, o) {
  166. // console.log(`watch hover_elmt ${this.id}`, o, n);
  167. if (n && (n.type === 'concernement' || n.type === 'superposition') && n.id === this.id) {
  168. this.is_hover = true;
  169. } else {
  170. this.is_hover = false;
  171. }
  172. },
  173. deep: true
  174. },
  175. is_hover: {
  176. handler (n, o) {
  177. this.onIsHover(n);
  178. },
  179. deep: true
  180. },
  181. active_revision: {
  182. handler (n, o) {
  183. console.log(`concernementMapItem watch active_revision o:${o}, n:${n}`);
  184. if(o & n){ // do not trigger on first variable filling (if o is null)
  185. this.resetPaperActiveRevision();
  186. }
  187. },
  188. deep: true
  189. }
  190. },
  191. methods: {
  192. ...mapActions(CommonStore,['setHoverElmt']),
  193. ...mapActions(ConcernementsStore,['openCloseConcernements',
  194. // 'setConcernementMapItem',
  195. 'hideShowConcernement',
  196. 'setBesoinPaperId',
  197. 'setOpenedDoleanceField',
  198. 'setOpenedRecit']),
  199. // getResponsiveRay(){
  200. // return Math.min(this.canvas.width, this.canvas.height) * 0.08;
  201. // },
  202. parseEntityPointsValues (){
  203. // converts data (menace/maintien, actuel/future, prise) into atcual position x,y
  204. for (let r = 0; r < this.concernement.revisions.length; r++) { // loop through all revisions
  205. for (let i = 0; i < this.concernement.revisions[r].entites.length; i++) { // loop through all entite for each revision
  206. let entite = this.concernement.revisions[r].entites[i]
  207. // console.log('entite', entite);
  208. entite.display = {
  209. alpha: null,
  210. ray: null
  211. }
  212. // RAYON
  213. // https://stackoverflow.com/questions/5731863/mapping-a-numeric-range-onto-another
  214. // slope = (output_end - output_start) / (input_end - input_start)
  215. // output = output_start + slope * (input - input_start)
  216. // from range 0 -> 100 to range 0 -> this.ray
  217. let init_max = 100
  218. let slope = this.ray / init_max
  219. entite.display.ray = slope * (init_max - entite.prise);
  220. // if (this.concernement.id === 28) {
  221. // console.log(`entity prise: ${entite.prise} | ray: ${entite.display.ray}`);
  222. // }
  223. // ANGLE
  224. // -90 <= mm <= 90
  225. if (entite.actuelfuture) {
  226. // future en haut : 180 <= a <= 360
  227. // from -90 -> 90 to range 180 -> 360
  228. entite.display.alpha = entite.menacemaintien + 270
  229. } else {
  230. // actuel: en bas : O <= a <= 180
  231. // from -90 -> 90 to range 180 -> 0
  232. entite.display.alpha = -1 * entite.menacemaintien + 90
  233. }
  234. // POSITION X Y (par rapport au centre du concernement)
  235. entite.display.pos = {
  236. x: entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180)),
  237. y: entite.display.ray * Math.sin(entite.display.alpha * (Math.PI/180))
  238. }
  239. // if (entite.entite) {
  240. // this.entites_byid[entite.entite.id].display = entite.display;
  241. // }
  242. }
  243. }
  244. },
  245. getSalientPoints_OLD() {
  246. // debugger
  247. // console.log(this.entites);
  248. let arc = 360/30;
  249. // loop through arcs
  250. // for (let i = 360/arc; i >= 0 ; i--) {
  251. for (let i = 0; i <= 360/arc ; i++) {
  252. // loop through entities to find the farest on the arc
  253. let max_r = 0;
  254. let farest = null;
  255. for (let j = 0; j < this.entites.length; j++) {
  256. let entite = this.entites[j];
  257. if(arc*i <= entite.display.alpha && entite.display.alpha <= arc*i+arc) { // if entity is in arc
  258. if (entite.display.ray > max_r) { // && entite.display.ray > this.ray/2 // and farest from minimu
  259. // if entity is farest from precedent one
  260. max_r = entite.display.ray;
  261. // recalcul x & y to get a little padding between entite and contour by increasing ray
  262. farest = {
  263. alpha: entite.display.alpha,
  264. ray: entite.display.ray,
  265. pos: {
  266. x: (entite.display.ray + 3) * Math.cos(entite.display.alpha * (Math.PI/180)),
  267. y: (entite.display.ray + 3) * Math.sin(entite.display.alpha * (Math.PI/180))
  268. }
  269. };
  270. }
  271. }
  272. }
  273. if (farest) {
  274. this.salientPoints.push(farest)
  275. }
  276. }
  277. // console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints);
  278. },
  279. getJarvisEnvelopeConvexe(){
  280. this.salientPoints = [];
  281. let entites = this.concernement.revisions_byid[this.concernement.active_revision].entites;
  282. // console.log(`getJarvisEnvelopeConvexe ${this.id}`, this.entites.length);
  283. // https://www.geeksforgeeks.org/convex-hull-using-jarvis-algorithm-or-wrapping/
  284. // find the most left point
  285. let l, min_x = null;
  286. for (let i = 0; i < entites.length; i++) {
  287. let entite = entites[i];
  288. let x = entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180));
  289. if(!min_x || min_x > x){
  290. l = i;
  291. min_x = x;
  292. }
  293. }
  294. // Start from leftmost point, keep moving clockwise
  295. // until reach the start point again. This loop runs O(h)
  296. // times where h is number of points in result or output.
  297. let p = l, q;
  298. do {
  299. // console.log(`do while ${this.id}`, p);
  300. // Add current point to result
  301. let entite = entites[p];
  302. let farest = {
  303. alpha: entite.display.alpha,
  304. ray: entite.display.ray,
  305. pos: {
  306. x: (entite.display.ray) * Math.cos(entite.display.alpha * (Math.PI/180)),
  307. y: (entite.display.ray) * Math.sin(entite.display.alpha * (Math.PI/180))
  308. }
  309. };
  310. this.salientPoints.push(farest);
  311. // Search for a point 'q' such that
  312. // orientation(p, q, x) is clockwise
  313. // for all points 'x'. The idea is to keep
  314. // track of last visited most clockwise
  315. // point in q. If any point 'i' is more
  316. // clockwise than q, then update q.
  317. q = (p + 1) % entites.length;
  318. for (let i = 0; i < entites.length; i++) {
  319. let p_x = (entites[p].display.ray) * Math.cos(entites[p].display.alpha * (Math.PI/180));
  320. let p_y = (entites[p].display.ray) * Math.sin(entites[p].display.alpha * (Math.PI/180));
  321. let i_x = (entites[i].display.ray) * Math.cos(entites[i].display.alpha * (Math.PI/180));
  322. let i_y = (entites[i].display.ray) * Math.sin(entites[i].display.alpha * (Math.PI/180));
  323. let q_x = (entites[q].display.ray) * Math.cos(entites[q].display.alpha * (Math.PI/180));
  324. let q_y = (entites[q].display.ray) * Math.sin(entites[q].display.alpha * (Math.PI/180));
  325. let val = (i_y - p_y) * (q_x - i_x) - (i_x - p_x) * (q_y - i_y);
  326. // If i is more clockwise than current q, then update q
  327. if (val > 0){
  328. q = i;
  329. }
  330. }
  331. // Now q is the most clockwise with
  332. // respect to p. Set p as q for next iteration,
  333. // so that q is added to result 'hull'
  334. p = q;
  335. } while (p != l);
  336. },
  337. getRandomPos(){
  338. let pad = 200;
  339. // if (this.concernement.id === 56) {
  340. // return {
  341. // x: Math.random() > 0.5 ? pad : this.canvas.width - pad,
  342. // y: pad + this.ray/2 + Math.random()*(this.canvas.height - this.ray - pad)
  343. // };
  344. // } else {
  345. return {
  346. x: pad + this.getGaussianRandom()*(this.canvas.width - pad*2),
  347. y: pad + this.getGaussianRandom()*(this.canvas.height - pad*2)
  348. };
  349. // }
  350. },
  351. getGaussianRandom(){
  352. // INFO https://stackoverflow.com/a/49434653
  353. let u = 0, v = 0;
  354. while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
  355. while(v === 0) v = Math.random();
  356. let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
  357. num = num / 10.0 + 0.5; // Translate to 0 -> 1
  358. if (num > 1 || num < 0) return this.getGaussianRandom() // resample between 0 and 1
  359. return num
  360. },
  361. getSuperposedEntitesIDsList(){
  362. if (this.concernement.superpositions) {
  363. // loop through all concernement superpositions couples of this concernement
  364. for(let [couple_id, superpositions] of Object.entries(this.concernement.superpositions)){
  365. // loop through all superpositions of each couple
  366. for(let [superposition_id, superposition] of Object.entries(superpositions)){
  367. if (this.mapitem.superposition_ids.indexOf(superposition_id) >= 0) {
  368. switch (this.cid) { // get the right eid regarding the cid
  369. case superposition[0].cid:
  370. this.superposedEntitesIDsList.push(superposition[0].eid);
  371. break;
  372. case superposition[1].cid:
  373. this.superposedEntitesIDsList.push(superposition[1].eid);
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. console.log('superposedEntitesIDsList', this.superposedEntitesIDsList);
  381. },
  382. // MATTER BODY
  383. initMatterBody (){
  384. // MATTER
  385. // create the matter body and add it to the engine
  386. if (!this.body) {
  387. // console.log('concernementItem creating body');
  388. // INFO https://github.com/liabru/matter-attractors/issues/8
  389. // INFO https://github.com/liabru/matter-attractors/blob/master/index.js
  390. // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L180
  391. MatterAttractors.Attractors.gravityConstant = -20;
  392. // Create parts of the body : main big circle & entities
  393. // INFO map a range of numbers to another range of numbers https://stackoverflow.com/a/46462321
  394. let entite_range = [3, 100];
  395. let ray_range = [this.ray*0.8,this.ray*1.2];
  396. let ray = ray_range[0] + (this.entites.length - entite_range[0]) * (ray_range[1] - ray_range[0]) / (entite_range[1] - entite_range[0]);
  397. this.body_parts = [
  398. Matter.Bodies.circle(0, 0, ray, {
  399. // item_type: 'concernement',
  400. // id: this.concernement.id,
  401. })
  402. ];
  403. // INFO map a range of numbers to another range of numbers https://stackoverflow.com/a/46462321
  404. let mass_range = [5,15];
  405. let mass = mass_range[0] + (this.entites.length - entite_range[0]) * (mass_range[1] - mass_range[0]) / (entite_range[1] - entite_range[0]);
  406. // create the body
  407. this.body = Matter.Body.create({
  408. parts: this.body_parts,
  409. item_type: 'concernement',
  410. id: this.id,
  411. frictionAir: 0,
  412. // mass: Math.pow(3, this.entites.length),
  413. // mass: 10,
  414. mass: mass,
  415. restitution: 0.06,
  416. collisionFilter: {
  417. group: -1
  418. },
  419. plugin: {
  420. attractors: [
  421. // // there is a built in helper function for Newtonian gravity!
  422. // // you can find out how it works in index.js
  423. MatterAttractors.Attractors.gravity
  424. // function(bodyA, bodyB) {
  425. // var force = {
  426. // x: (bodyA.position.x - bodyB.position.x) * 1e-6,
  427. // y: (bodyA.position.y - bodyB.position.y) * 1e-6
  428. // }
  429. // // apply force to both bodies
  430. // Matter.Body.applyForce(bodyA, bodyA.position, force);
  431. // Matter.Body.applyForce(bodyB, bodyB.position, Matter.Vector.neg(force));
  432. // }
  433. // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L192
  434. // function (bodyA, bodyB){
  435. // // use Newton's law of gravitation
  436. // var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
  437. // distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
  438. // normal = Matter.Vector.normalise(bToA),
  439. // magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
  440. // force = Matter.Vector.mult(normal, magnitude);
  441. // // to apply forces to both bodies
  442. // Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
  443. // Matter.Body.applyForce(bodyB, bodyB.position, force);
  444. // }
  445. ]
  446. }
  447. });
  448. Matter.Body.setPosition(this.body, this.pos);
  449. // add init velocity
  450. this.setInitBodyVelocity()
  451. // console.log('concernementItem mass', this.body.mass);
  452. Matter.Composite.add(this.matterEngine.world, this.body);
  453. // console.log('concernement body', this.body);
  454. // listen for afterUpdate event from Matter.Engine object
  455. Matter.Events.on(this.matterEngine, "beforeUpdate", this.onBeforeEngineUpdate);
  456. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  457. }
  458. },
  459. setInitBodyVelocity(){
  460. let delta = 50;
  461. Matter.Body.setVelocity(this.body, {
  462. x: -delta + Math.random()*delta*2,
  463. y: -delta + Math.random()*delta*2
  464. });
  465. },
  466. // PAPER OBJECTS
  467. initPaperObjects(){
  468. // the main paper group containing all paper graphical items for one concernement
  469. this.paper_main_object = new paper.Group({
  470. pivot: new paper.Point(this.pos),
  471. name: `main_${this.id}`,
  472. cid: this.cid,
  473. superposition_id: this.mapitem.superposition_ids[0] // TODO what to do with multiples superpositions ids
  474. });
  475. // the sub items for one concernement
  476. this.paper_main_object.addChild(this.setPaperContour());
  477. if (this.concernement.has_superpositions) {
  478. this.paper_main_object.addChild(this.setPaperEntitesSuperposees());
  479. }
  480. if (this.concernement.has_puissancedagir) {
  481. this.addNewPaperSymbolInstance('puissanceagir_icon', false, 0.7);
  482. }
  483. if (this.concernement.has_agissantes) {
  484. this.paper_main_object.addChild(this.setPaperAgissantesIcons());
  485. }
  486. if (this.concernement.has_doleance) {
  487. this.addNewPaperSymbolInstance('doleance_icon', false, 0.7);
  488. }
  489. this.initPaperEvents()
  490. },
  491. setPaperContents(){
  492. // trigered once opening tween is complete
  493. this.clearPaperContents();
  494. switch(this.map_mode){
  495. case 'terraindevie':
  496. this.addNewPaperSymbolInstance('boussole_bg', true);
  497. this.paper_main_object.addChild(this.setPaperEntites());
  498. break;
  499. case 'superposition':
  500. this.addNewPaperSymbolInstance('boussole_bg', true);
  501. break;
  502. case 'puissancedagir':
  503. this.addNewPaperSymbolInstance('puissanceagir_bg', true);
  504. this.paper_main_object.addChild(this.setPaperPuissanceagirBesoins());
  505. break;
  506. case 'action':
  507. this.addNewPaperSymbolInstance('boussole_bg', true);
  508. this.paper_main_object.addChild(this.setPaperAgissantes());
  509. break;
  510. case 'doleancer':
  511. this.addNewPaperSymbolInstance('doleance_bg');
  512. this.paper_main_object.addChild(this.setPaperDoleances());
  513. break;
  514. }
  515. },
  516. resetPaperActiveRevision(){
  517. this.getJarvisEnvelopeConvexe();
  518. // remove contours if already exists
  519. if (this.paper_main_object.children.contours) {
  520. this.paper_main_object.children.contours.remove();
  521. }
  522. this.paper_main_object.addChild(this.setPaperContour());
  523. // remove entites if already exists
  524. if (this.paper_main_object.children.entites) {
  525. this.paper_main_object.children.entites.remove();
  526. }
  527. this.paper_main_object.addChild(this.setPaperEntites());
  528. },
  529. clearPaperContents(){
  530. let clearable_children = ['boussole_bg', 'entites',
  531. 'puissanceagir_bg','puissanceagir_besoins',
  532. 'agissantes',
  533. 'doleance_bg', 'doleances'];
  534. clearable_children.forEach(child_name => {
  535. if (this.paper_main_object.children[child_name]) {
  536. this.paper_main_object.children[child_name].remove();
  537. }
  538. });
  539. },
  540. addNewPaperSymbolInstance(name, back, scale){
  541. let instance = new paper.SymbolItem(this.paper_symbol_definitions[name]); // , {x:0,y:0}
  542. instance.name = name;
  543. // instance.pivot = new paper.Point({x:0,y:0});
  544. instance.position = this.pos;
  545. let s = scale ? this.scale * scale : this.scale;
  546. instance.scale(s);
  547. // instance.locked = true;
  548. this.paper_main_object.addChild(instance);
  549. if (back) {
  550. this.paper_main_object.children[name].sendToBack();
  551. } else {
  552. this.paper_main_object.children[name].bringToFront();
  553. }
  554. },
  555. setPaperContour(){
  556. // console.log(`setPaperContour ${this.concernement.id}`);
  557. let getPaddedRoundedSegments = (b,a,c,d) => {
  558. const ac = { x: c.x - a.x, y: c.y - a.y } // get ac vecteur
  559. const lac = Math.sqrt(Math.pow(ac.x, 2) + Math.pow(ac.y, 2)); // get ac longueur ac
  560. const ab = { x: b.x - a.x, y: b.y - a.y } // get ab vecteur
  561. const lab = Math.sqrt(Math.pow(ab.x, 2) + Math.pow(ab.y, 2)); // get ab longeur
  562. const vab = { x: ab.x/lab, y: ab.y/lab } // get unit vecteur ab
  563. const an = { x: vab.x*lac, y: vab.y*lac } // get an vecteur
  564. const n = { x: a.x + an.x, y: a.y+an.y } // get n point
  565. const m = { x: (c.x + n.x)/2, y: (c.y + n.y)/2 } // get nc midle point
  566. const ma = { x:a.x - m.x, y: a.y - m.y } // get ma vecteur
  567. const lma = Math.sqrt(Math.pow(ma.x, 2)+Math.pow(ma.y, 2)) // get longeur m->a
  568. const vma = { x: ma.x/lma, y: ma.y/lma } // get ma vecteur unitaire
  569. // console.log(`vma x:${vma.x}, y:${vma.y}`);
  570. const pad = 4; // exterior padding
  571. // the final padded point
  572. const pa = [
  573. this.pos.x+(a.x+vma.x*pad)*this.scale,
  574. this.pos.y+(a.y+vma.y*pad)*this.scale
  575. ]
  576. // handles
  577. const delta = 0.05;
  578. // handle IN
  579. const hli = Math.abs(lab)*delta; // handle longeur
  580. const vnai = { x: -vma.y, y: vma.x } // get the ma normal unit vector IN
  581. const hai = [ vnai.x*hli, vnai.y*hli ]; // get the handleIn point
  582. // handle OUT
  583. const hlo = Math.abs(lac)*delta; // handle longeur
  584. const vnao = { x: vma.y, y: -vma.x } // get the ma normal vector Out
  585. const hao = [ vnao.x*hlo, vnao.y*hlo ]; // get the handleOut point
  586. return new paper.Segment({
  587. point: pa,
  588. handleIn: hai,
  589. handleOut: hao
  590. })
  591. }
  592. const first_point = getPaddedRoundedSegments(
  593. this.salientPoints[this.salientPoints.length-1].pos,
  594. this.salientPoints[0].pos,
  595. this.salientPoints[1].pos
  596. );
  597. let segments = [first_point];
  598. for (let j = 1; j < this.salientPoints.length-1; j++) {
  599. // segments.push([this.pos.x+this.salientPoints[j].pos.x*this.scale*gap, this.pos.y+this.salientPoints[j].pos.y*this.scale*gap])
  600. segments.push(getPaddedRoundedSegments(
  601. this.salientPoints[j-1].pos,
  602. this.salientPoints[j].pos,
  603. this.salientPoints[j+1].pos
  604. ))
  605. }
  606. const last_point = getPaddedRoundedSegments(
  607. this.salientPoints[this.salientPoints.length-2].pos,
  608. this.salientPoints[this.salientPoints.length-1].pos,
  609. this.salientPoints[0].pos
  610. );
  611. segments.push(last_point)
  612. segments.push(first_point)
  613. const contrs = new paper.Path({
  614. name: 'contours',
  615. segments: segments,
  616. fillColor: 'rgba(255,255,255,0.4)',
  617. // selected: true,
  618. strokeColor: '#fff',
  619. strokeWidth: 1,
  620. pivot: new paper.Point(this.pos),
  621. cid: this.cid
  622. });
  623. return contrs;
  624. },
  625. setPaperEntites(){
  626. let g = new paper.Group({
  627. pivot: new paper.Point(this.pos),
  628. name: 'entites'
  629. });
  630. // for (let i = 0; i < this.entites.length; i++) {
  631. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  632. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  633. // use paper symbol
  634. let symbol_name = entite.entite ? entite.entite.agissante ? 'entite_action' : 'entite' : 'entite_hidden';
  635. let instance = new paper.SymbolItem(this.paper_symbol_definitions[symbol_name]);
  636. instance.name = 'entite';
  637. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  638. instance.scale(this.scale);
  639. instance.fillColor = '#000';
  640. instance.item_id = entite.entite ? entite.entite.id : null;
  641. instance.item_type = symbol_name;
  642. instance.is_symbol_instance = true;
  643. g.addChild(instance)
  644. }
  645. return g;
  646. },
  647. setPaperEntitesSuperposees(){
  648. // console.log('setPaperSuperpositions, superpositions', this.concernement.superpositions);
  649. let g = new paper.Group({
  650. pivot: new paper.Point(this.pos),
  651. name: 'entites_superposes'
  652. });
  653. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  654. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  655. if (entite.entite // check if we have an entite object with all the contents
  656. && this.superposedEntitesIDsList.indexOf(entite.entite.id) >= 0) // check if entite id is in the list builded above
  657. {
  658. // console.log(`entite ${entite.entite.id}`, entite, entite.entite.superposition);
  659. // use paper symbol
  660. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  661. instance.name = 'entite';
  662. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  663. // instance.scale(this.scale);
  664. instance.scale(3);
  665. instance.fillColor = '#000';
  666. instance.item_id = entite.entite.id;
  667. instance.item_type = 'entite_superpose';
  668. instance.is_symbol_instance = true;
  669. g.addChild(instance)
  670. }
  671. }
  672. return g;
  673. },
  674. setPaperAgissantes(){
  675. console.log('setPaperAgissantes');
  676. let g = new paper.Group({
  677. pivot: new paper.Point(this.pos),
  678. name: 'agissantes'
  679. });
  680. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  681. let entite = this.concernement.revisions_byid[this.concernement.revision_id].entites[i];
  682. console.log('agissantes', entite);
  683. if (entite.entite && entite.entite.agissante) {
  684. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action']);
  685. instance.name = 'entite_action';
  686. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  687. instance.fillColor = '#000';
  688. // instance.scale(0.2);
  689. instance.scale(this.scale);
  690. instance.item_id = entite.entite.id;
  691. instance.item_type = 'entite_action';
  692. instance.is_symbol_instance = true;
  693. g.addChild(instance)
  694. }
  695. }
  696. return g;
  697. },
  698. setPaperAgissantesIcons(){
  699. let g = new paper.Group({
  700. pivot: new paper.Point(this.pos),
  701. name: 'agissantes_icons'
  702. });
  703. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  704. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  705. if (entite.entite && entite.entite.agissante) {
  706. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action_icon']);
  707. instance.name = 'entite_action';
  708. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  709. instance.fillColor = '#000';
  710. // instance.scale(0.2);
  711. instance.scale(this.scale);
  712. instance.item_id = entite.entite.id;
  713. instance.item_type = 'entite_action';
  714. instance.is_symbol_instance = true;
  715. g.addChild(instance)
  716. }
  717. }
  718. return g;
  719. },
  720. setPaperPuissanceagirBesoins(){
  721. let g = new paper.Group({
  722. pivot: new paper.Point(this.pos),
  723. name: 'puissanceagir_besoins'
  724. });
  725. let res_fields = ['qui','quoi','ou','avec'];
  726. let arc = (360 / 16); // unit arc
  727. let r = (this.ray * this.scale)/5; // unit ray
  728. let br = r - r/3; // besoin ray
  729. for (let i = 0; i < this.concernement.besoins.length; i++) {
  730. let start_a = arc * i; // angle depart (for reponses)
  731. let center_a = start_a + arc/2; // angle central
  732. let x = Math.cos(center_a*(Math.PI/180)) * br;
  733. let y = Math.sin(center_a*(Math.PI/180)) * br;
  734. // use paper symbol
  735. let besoin = new paper.SymbolItem(this.paper_symbol_definitions['besoin']);
  736. besoin.position = new paper.Point([this.pos.x + x, this.pos.y + y]);
  737. besoin.scale(this.scale);
  738. besoin.item_id = this.concernement.besoins[i].id;
  739. besoin.item_cid = this.concernement.id;
  740. besoin.item_type = 'besoin';
  741. besoin.is_symbol_instance = true;
  742. console.log(`paper besoin id:${besoin.id}`);
  743. this.setBesoinPaperId(besoin.id, this.cid, this.concernement.besoins[i].id);
  744. g.addChild(besoin)
  745. let res_arc = arc / (1 + this.concernement.besoins[i].reponses.length); // unit arc for responses depending responses number
  746. for (let j = 0; j < this.concernement.besoins[i].reponses.length; j++) {
  747. let res_a = start_a + res_arc * (j+1); // angle for response line
  748. for (let f = 0; f < res_fields.length; f++) { // loop through all 4 fields, keep only the last one filled
  749. if( this.concernement.besoins[i].reponses[j][res_fields[f]] // if field filled
  750. && (f === res_fields.length -1 || !this.concernement.besoins[i].reponses[j][res_fields[f+1]]) // and is last field or last field filled
  751. ){
  752. let rr = this.ray * this.scale - r*f - r/2; // reponse field ray
  753. let rx = Math.cos(res_a*(Math.PI/180)) * rr;
  754. let ry = Math.sin(res_a*(Math.PI/180)) * rr;
  755. // use paper symbol
  756. let reponse = new paper.SymbolItem(this.paper_symbol_definitions['reponse']);
  757. reponse.position = new paper.Point([this.pos.x + rx, this.pos.y + ry]);
  758. reponse.scale(this.scale);
  759. reponse.item_id = this.concernement.besoins[i].reponses[j].id;
  760. reponse.item_bid = this.concernement.besoins[i].id;
  761. reponse.item_cid = this.concernement.id;
  762. reponse.item_type = 'reponse';
  763. reponse.is_symbol_instance = true;
  764. this.setBesoinPaperId(reponse.id, this.cid, this.concernement.besoins[i].id, this.concernement.besoins[i].reponses[j].id);
  765. g.addChild(reponse)
  766. }
  767. }
  768. }
  769. }
  770. return g;
  771. },
  772. setPaperDoleances(){
  773. let g = new paper.Group({
  774. pivot: new paper.Point({x:0,y:0}),
  775. name: `doleances`
  776. });
  777. this.concernement.doleances.forEach((d) => {
  778. g.addChild(this.setPaperDoleanceSteps(d))
  779. });
  780. return g;
  781. },
  782. setPaperDoleanceSteps(doleance){
  783. let g = new paper.Group({
  784. pivot: new paper.Point({x:0,y:0}),
  785. name: `doleance_${doleance.id}`,
  786. doleance_id: doleance.id
  787. });
  788. // let doleance = this.concernement.doleances[0];
  789. let all_fields = [
  790. [
  791. 'leprobleme',
  792. 'lenquete',
  793. {
  794. fieldname: 'groupesinterets',
  795. fields: [
  796. 'groupe_interets',
  797. 'accorder_interets',
  798. 'formuler',
  799. ]
  800. }
  801. ],
  802. [
  803. 'entite_addresse_doleance',
  804. 'comment_ennonce_doleance',
  805. 'aqui_addresse_doleance',
  806. {
  807. fieldname: 'reception_traitement',
  808. fields: [
  809. 'entite_adressee',
  810. 'doleance_formulee',
  811. 'traite_doleance',
  812. 'entite_recoit_doleance',
  813. ]
  814. }
  815. ],
  816. [
  817. 'entites_decisionnaires',
  818. 'decision_formule',
  819. {
  820. fieldname: 'mise_en_oeuvre_decision',
  821. fields: [
  822. 'entite_adresse_decision',
  823. 'formule_decision',
  824. 'entite_metenoeuvre_decisio',
  825. ]
  826. }
  827. ],
  828. [
  829. 'entite_adresse_application',
  830. 'aqui_adresse_decision',
  831. 'comment_formule_decision',
  832. {
  833. fieldname: 'receptions_et_applications',
  834. fields: [
  835. 'applique_decision',
  836. 'formule_decision_applic',
  837. 'entite_recoit_decision',
  838. ]
  839. },
  840. 'probleme_initial_resolu',
  841. 'oui_nouvelle_situation',
  842. 'non_adresse_doleance',
  843. ]
  844. ];
  845. var r = this.ray * this.scale * 0.8; // ray
  846. var dr = r/2; // demi ray
  847. // var pcr = 2*this.scale; // petits cercle rayon
  848. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  849. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  850. // radians = degrees * (pi/180)
  851. // degrees = radians * (180/pi)
  852. // Points for 45° axes
  853. let m = Math.sin(45*(Math.PI/180)) * r; // x = y for rayon
  854. let n = Math.sin(45*(Math.PI/180)) * r/2; // x = y for demi rayon
  855. let o = Math.cos(22.5*(Math.PI/180)) * r; // x @ 22.5° for rayon
  856. let p = Math.sin(22.5*(Math.PI/180)) * r; // y @ 22.5° for rayon
  857. let o_d = Math.cos(22.5*(Math.PI/180)) * r/2; // x @ 22.5° for demi rayon
  858. let p_d = Math.sin(22.5*(Math.PI/180)) * r/2; // y @ 22.5° for demi rayon
  859. // let fontsize = 4;
  860. //
  861. // CAMENBERT STATIQUES
  862. //
  863. // l'enquete
  864. //
  865. // camenbert
  866. let cam = new paper.Path({
  867. strokeColor: '#fff',
  868. strokeWidth: 2,
  869. fillColor: "rgba(255, 255, 255, 0.4)",
  870. item_type: 'doleance_step',
  871. item_id: `lenquete`,
  872. did: doleance.id,
  873. field: 'lenquete',
  874. field_index: null
  875. });
  876. cam.add({x: this.pos.x , y: this.pos.y + dr});
  877. cam.lineTo({x: this.pos.x, y: this.pos.y + r});
  878. cam.arcTo({x: this.pos.x - p, y: this.pos.y + o}, {x: this.pos.x - m, y: this.pos.y + m});
  879. cam.lineTo({x: this.pos.x - n, y: this.pos.y + n});
  880. cam.arcTo({x: this.pos.x - p_d, y: this.pos.y + o_d}, {x: this.pos.x , y: this.pos.y + dr});
  881. // texte
  882. // enquete.addChild(new paper.PointText({
  883. // point: {
  884. // x:this.pos.x + Math.sin(22.5*(Math.PI/180)) * r*0.75,
  885. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  886. // },
  887. // content: doleance['lenquete'],
  888. // fontSize: fontsize,
  889. // fillColor: '#fff',
  890. // justification: 'center',
  891. // locked: true
  892. // }));
  893. g.addChild(cam);
  894. //
  895. // probleme_initial_resolu
  896. //
  897. // camenbert
  898. let rescam = new paper.Path({
  899. strokeColor: '#fff',
  900. strokeWidth: 2,
  901. fillColor: "rgba(255, 255, 255, 0.4)",
  902. closed: true,
  903. item_type: 'doleance_step',
  904. item_id: `probleme_initial_resolu`,
  905. did: doleance.id,
  906. field: 'probleme_initial_resolu',
  907. field_index: null
  908. });
  909. rescam.add({x: this.pos.x + n, y: this.pos.y + n});
  910. rescam.lineTo({x: this.pos.x + m , y: this.pos.y + m});
  911. rescam.arcTo({x: this.pos.x + p, y: this.pos.y + o}, {x: this.pos.x, y: this.pos.y + r});
  912. rescam.lineTo({x: this.pos.x, y: this.pos.y + dr});
  913. rescam.arcTo({x: this.pos.x + p_d, y: this.pos.y + o_d}, {x: this.pos.x + n, y: this.pos.y + n});
  914. // texte
  915. // resolution.addChild(new paper.PointText({
  916. // point: {
  917. // x:this.pos.x - Math.sin(22.5*(Math.PI/180)) * r*0.75,
  918. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  919. // },
  920. // content: doleance['probleme_initial_resolu'] ? doleance['oui_nouvelle_situation'] : doleance['non_adresse_doleance'],
  921. // fontSize: fontsize,
  922. // fillColor: '#fff',
  923. // justification: 'center'
  924. // }));
  925. g.addChild(rescam)
  926. //
  927. // MULTIPLE FIELDS
  928. //
  929. let multiple_fields = [
  930. { field_name: 'groupesinterets', arc: 45, decalage: -45 },
  931. { field_name: 'reception_traitement', arc: 90, decalage: -90 },
  932. { field_name: 'mise_en_oeuvre_decision', arc: 90, decalage: -180 },
  933. { field_name: 'receptions_et_applications', arc: 45, decalage: 90 }
  934. ]
  935. multiple_fields.forEach((mf, j) => {
  936. for (let i = 0, l = doleance[mf.field_name].length, a = mf.arc/l; i < l; i++) {
  937. // let gi = new paper.Group({
  938. // item_type: 'doleance_step',
  939. // item_id: 'lenquete',
  940. // item_field: mf.field_name,
  941. // item_index: i
  942. // });
  943. //camenbert
  944. let x1= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * dr,
  945. y1= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * dr;
  946. let x2= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * r,
  947. y2= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * r;
  948. let x3= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * r,
  949. y3= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * r;
  950. let x3t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r,
  951. y3t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r;
  952. let x4= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * dr,
  953. y4= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * dr;
  954. let x4t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr,
  955. y4t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr;
  956. let x5= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75,
  957. y5= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75;
  958. let p = new paper.Path({
  959. strokeColor: '#fff',
  960. strokeWidth: 2,
  961. fillColor: "rgba(255, 255, 255, 0.4)",
  962. item_type: 'doleance_step',
  963. item_id: `${mf.field_name}-${i}`,
  964. did: doleance.id,
  965. field: mf.field_name,
  966. field_index: i
  967. });
  968. p.add([x1,y1]);
  969. p.lineTo([x2,y2]);
  970. p.arcTo([x3t,y3t], [x3,y3]);
  971. p.lineTo([x4,y4]);
  972. p.arcTo([x4t,y4t], [x1,y1]);
  973. g.addChild(p);
  974. // gi.addChild(p);
  975. // // text
  976. // gi.addChild(new paper.PointText({
  977. // point: {x:x5,y:y5},
  978. // content: `${j}-${i}`,
  979. // fontSize: fontsize,
  980. // fillColor: '#fff',
  981. // justification: 'center'
  982. // }))
  983. // g.addChild(gi)
  984. }
  985. });
  986. // POINTS CARDINAUX
  987. //
  988. // // leprobleme
  989. // g.addChild(new paper.Path.Circle({
  990. // center: [this.pos.x, this.pos.y + r],
  991. // radius: 3,
  992. // style: {
  993. // strokeColor: '#fff',
  994. // strokeWidth: 2,
  995. // fillColor: "rgba(255, 255, 255, 0.4)",
  996. // },
  997. // item_type: 'doleance_step',
  998. // item_id: `leprobleme`,
  999. // did: doleance.id,
  1000. // field: 'leprobleme'
  1001. // }));
  1002. // adresse de la doleance
  1003. g.addChild(new paper.Path.Circle({
  1004. center: [this.pos.x - r, this.pos.y],
  1005. radius: 3 * this.scale,
  1006. style: {
  1007. strokeColor: '#fff',
  1008. strokeWidth: 2,
  1009. fillColor: "rgba(255, 255, 255, 0.9)",
  1010. },
  1011. item_type: 'doleance_step',
  1012. item_id: `adresse_de_la_doleance`,
  1013. did: doleance.id,
  1014. field: 'adresse_de_la_doleance'
  1015. }));
  1016. // decision
  1017. g.addChild(new paper.Path.Circle({
  1018. center: [this.pos.x, this.pos.y - r],
  1019. radius: 3 * this.scale,
  1020. style: {
  1021. strokeColor: '#fff',
  1022. strokeWidth: 2,
  1023. fillColor: "rgba(255, 255, 255, 0.9)",
  1024. },
  1025. item_type: 'doleance_step',
  1026. item_id: `decision`,
  1027. did: doleance.id,
  1028. field: 'decision'
  1029. }));
  1030. // adresse_de_la_decision
  1031. g.addChild(new paper.Path.Circle({
  1032. center: [this.pos.x + r, this.pos.y],
  1033. radius: 3 * this.scale,
  1034. style: {
  1035. strokeColor: '#fff',
  1036. strokeWidth: 2,
  1037. fillColor: "rgba(255, 255, 255, 0.9)",
  1038. },
  1039. item_type: 'doleance_step',
  1040. item_id: `adresse_de_la_decision`,
  1041. did: doleance.id,
  1042. field: 'adresse_de_la_decision'
  1043. }));
  1044. return g;
  1045. },
  1046. // PAPER VISIBILITY
  1047. handlePaperVisibilityOnBeforeOpen(){
  1048. // agissantes
  1049. if (this.concernement.has_agissantes && this.map_mode === "action") {
  1050. if (!this.is_open) {
  1051. this.paper_main_object.children.agissantes_icons.visible = true;
  1052. } else {
  1053. this.paper_main_object.children.agissantes_icons.visible = false;
  1054. }
  1055. }
  1056. // superposition
  1057. // scale down superposed entites on open
  1058. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1059. // console.log('entites_superposes.children', entites_superposes.children);
  1060. if(entites_superposes){
  1061. for(let paper_item of entites_superposes.children) {
  1062. // paper_item.definition = this.paper_symbol_definitions.entite_hover;
  1063. paper_item.scale(0.25)
  1064. }
  1065. }
  1066. },
  1067. handlePaperVisibilityOnOpened(){},
  1068. handlePaperVisibilityOnBeforeClose(){
  1069. // superposition
  1070. // scale up superposed entites on open
  1071. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1072. // console.log('entites_superposes.children', entites_superposes.children);
  1073. if(entites_superposes){
  1074. for(let paper_item of entites_superposes.children) {
  1075. // paper_item.definition = this.paper_symbol_definitions.entite_hover;
  1076. paper_item.scale(4)
  1077. }
  1078. }
  1079. },
  1080. handlePaperVisibilityOnClosed(){
  1081. // agissantes
  1082. if (this.concernement.has_agissantes && this.map_mode === "action") {
  1083. if (!this.is_open) {
  1084. this.paper_main_object.children.agissantes_icons.visible = true;
  1085. } else {
  1086. this.paper_main_object.children.agissantes_icons.visible = false;
  1087. }
  1088. }
  1089. },
  1090. handlePaperVisibilityOnMapMode(){},
  1091. handlePaperVisibilityOnAfterEnginUpdate(){
  1092. // contours focused
  1093. if (!this.isFocused()){
  1094. this.paper_main_object.children['contours'].fillColor = this.mapitem.clone ? "rgba(255,0,0,0.1)" : "rgba(255,255,255,0.1)";
  1095. }else{
  1096. this.paper_main_object.children['contours'].fillColor = this.mapitem.clone ? "rgba(255,0,0,0.4)" : "rgba(255,255,255,0.4)";
  1097. if (this.is_hover) {
  1098. this.paper_main_object.children['contours'].strokeColor = "#01ffe2";
  1099. this.paper_main_object.children['contours'].strokeWidth = 2;
  1100. }else{
  1101. this.paper_main_object.children['contours'].strokeColor = "#fff";
  1102. this.paper_main_object.children['contours'].strokeWidth = 1;
  1103. }
  1104. }
  1105. // contours visibility
  1106. if (!this.is_open
  1107. || (this.is_open && this.map_mode !== "puissancedagir" && this.map_mode !== "doleancer")) {
  1108. this.paper_main_object.children['contours'].visible = true;
  1109. } else {
  1110. this.paper_main_object.children['contours'].visible = false;
  1111. }
  1112. // proximite
  1113. // superposition
  1114. if (this.concernement.has_superpositions) {
  1115. if (this.map_mode === "superposition") {
  1116. if (!this.is_open) {
  1117. this.paper_main_object.children.entites_superposes.visible = true; // if not opened and has_superpositions draw the entites_superposes points
  1118. } else {
  1119. this.paper_main_object.children.entites_superposes.visible = true;
  1120. }
  1121. } else {
  1122. this.paper_main_object.children.entites_superposes.visible = false;
  1123. }
  1124. }
  1125. // puissance d'agir
  1126. if (this.concernement.has_puissancedagir) {
  1127. if (this.map_mode === "puissancedagir") {
  1128. if (!this.is_open) {
  1129. this.paper_main_object.children.puissanceagir_icon.visible = true; // if not opened and has_puissancedagir draw the puissance d'agir icone
  1130. } else {
  1131. this.paper_main_object.children.puissanceagir_icon.visible = false;
  1132. }
  1133. } else {
  1134. this.paper_main_object.children.puissanceagir_icon.visible = false;
  1135. }
  1136. }
  1137. // agissantes
  1138. if (this.concernement.has_agissantes) {
  1139. if (this.map_mode !== "action") {
  1140. this.paper_main_object.children.agissantes_icons.visible = false;
  1141. } else if(!this.is_open && !this.is_closing){
  1142. this.paper_main_object.children.agissantes_icons.visible = true;
  1143. }
  1144. }
  1145. // doleance
  1146. if (this.concernement.has_doleance) {
  1147. if (this.map_mode === "doleancer") {
  1148. if (!this.is_open) {
  1149. this.paper_main_object.children.doleance_icon.visible = true;
  1150. } else {
  1151. this.paper_main_object.children.doleance_icon.visible = false;
  1152. // display the right (opened) doleance
  1153. this.concernement.doleances.forEach((d) => {
  1154. this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible = d.id === this.concernement.opened_doleance.id;
  1155. })
  1156. }
  1157. } else {
  1158. this.paper_main_object.children.doleance_icon.visible = false;
  1159. }
  1160. }
  1161. // focus on opened entite
  1162. if (this.is_open && this.opened_entite_id && (this.map_mode === 'terraindevie' || this.map_mode === 'action')) {
  1163. let group = this.map_mode === 'terraindevie' ? this.paper_main_object.children['entites'] : this.paper_main_object.children['agissantes'];
  1164. if(group){
  1165. group.children.forEach((item) => {
  1166. if (item.item_id === this.opened_entite_id) {
  1167. item.definition = item.item_type === 'entite' ? this.paper_symbol_definitions.entite_hover : this.paper_symbol_definitions.entite_action_hover;
  1168. } else {
  1169. if (!this.hover_elmt || item.item_id !== this.hover_elmt.id) {
  1170. item.definition = item.item_type === 'entite' ? this.paper_symbol_definitions.entite : this.paper_symbol_definitions.entite_action;
  1171. }
  1172. }
  1173. })
  1174. }
  1175. }
  1176. },
  1177. onIsHover(){
  1178. // handle layer z-index
  1179. if (this.is_hover) {
  1180. this.paper_main_object.bringToFront();
  1181. } else {
  1182. this.paper_main_object.sendToBack();
  1183. }
  1184. // hightlight superposed entites on focus
  1185. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1186. // console.log('entites_superposes.children', entites_superposes.children);
  1187. if(entites_superposes){
  1188. for(let paper_item of entites_superposes.children) {
  1189. if (this.is_hover) {
  1190. paper_item.definition = this.paper_symbol_definitions.entite_hover;
  1191. paper_item.scale(2)
  1192. } else {
  1193. paper_item.definition = this.paper_symbol_definitions.entite;
  1194. paper_item.scale(0.5)
  1195. }
  1196. }
  1197. }
  1198. },
  1199. // PAPER EVENTS
  1200. initPaperEvents(){
  1201. this.paper_main_object.onMouseLeave = function(event){
  1202. if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened
  1203. this.resetHoverElmt();
  1204. document.body.style.cursor = "auto";
  1205. }
  1206. }.bind(this);
  1207. this.paper_main_object.onMouseMove = function(event){
  1208. // console.log(`onmousemove ${this.id}`);
  1209. // prevent hover map item mouse event if cartouch is opened
  1210. if (this.cartouch_is_open) return;
  1211. if (!this.is_open) {
  1212. if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened and is this focused
  1213. if (this.map_mode === 'superposition') {
  1214. // get the superposed entite
  1215. this.setHoverElmt({
  1216. type: 'superposition',
  1217. id: this.id,
  1218. cid: this.cid,
  1219. eids: this.superposedEntitesIDsList
  1220. });
  1221. } else {
  1222. this.setHoverElmt({
  1223. type: 'concernement',
  1224. id: this.id,
  1225. cid: this.cid
  1226. });
  1227. }
  1228. document.body.style.cursor = "pointer";
  1229. }
  1230. } else {
  1231. // lets define some options regarding the map_mode
  1232. let paper_group_tohit;
  1233. switch (this.map_mode) {
  1234. case "terraindevie":
  1235. paper_group_tohit = this.paper_main_object.children['entites'];
  1236. break;
  1237. case "superposition":
  1238. paper_group_tohit = this.paper_main_object.children['entites_superposes'];
  1239. break;
  1240. case "action":
  1241. paper_group_tohit = this.paper_main_object.children['agissantes'];
  1242. break;
  1243. case "puissancedagir":
  1244. paper_group_tohit = this.paper_main_object.children['puissanceagir_besoins'];
  1245. break;
  1246. case "doleancer":
  1247. if(this.paper_main_object.children['doleances']){
  1248. paper_group_tohit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1249. }
  1250. break;
  1251. }
  1252. let result = paper_group_tohit ? paper_group_tohit.hitTest(event.point) : null;
  1253. // console.log('move result', result);
  1254. if (result && result.item.item_id) {
  1255. // console.log('move has result', result);
  1256. let new_hover_elmt = {
  1257. paper_id: result.item.id,
  1258. type: result.item.item_type,
  1259. id: result.item.item_id
  1260. };
  1261. switch (result.item.item_type) {
  1262. case "besoin":
  1263. new_hover_elmt.no_popup = true;
  1264. break;
  1265. case "reponse":
  1266. new_hover_elmt.bid = result.item.item_bid;
  1267. new_hover_elmt.cid = result.item.item_cid;
  1268. new_hover_elmt.no_popup = true;
  1269. break;
  1270. }
  1271. if (!this.hover_elmt || new_hover_elmt.paper_id !== this.hover_elmt.paper_id) {
  1272. // console.log(`before setHoverElmt ${this.id}`);
  1273. this.setHoverElmt(new_hover_elmt);
  1274. }
  1275. document.body.style.cursor = "pointer";
  1276. } else {
  1277. // console.log('move no result');
  1278. this.resetHoverElmt();
  1279. document.body.style.cursor = "auto";
  1280. }
  1281. }
  1282. }.bind(this);
  1283. this.paper_main_object.onClick = function(event){
  1284. console.log('paper concernement onClick');
  1285. // prevent hover map item mouse event if cartouch is opened
  1286. if (this.cartouch_is_open) return;
  1287. if (!this.is_open) { // si ce concernement n'est pas ouvet
  1288. console.log('mapitem is NOT opened');
  1289. if (!this.opened_concernement) { // si aucun concernement n'est ouvert
  1290. console.log(`Open me ${this.id}`);
  1291. // open/close all concernements
  1292. this.openCloseConcernements(this.cid, this.id)
  1293. // push route (keep the hash for map_mode)
  1294. this.$router.push({
  1295. name: 'concernement',
  1296. params: {cid: this.cid},
  1297. query: {
  1298. mapitemid: this.id,
  1299. // TODO when mapitem not a clone, there is no superposition_id prop
  1300. superposition_id: this.mapitem.superposition_ids[0]
  1301. },
  1302. hash: `#${this.map_mode}`
  1303. });
  1304. // reset the mousehover
  1305. this.resetHoverElmt();
  1306. }
  1307. } else { // si ce concernement est ouvert
  1308. console.log('mapitem is opened');
  1309. // lets define some options regarding the map_mode
  1310. let group_to_hit = null;
  1311. switch (this.map_mode) {
  1312. case "terraindevie":
  1313. group_to_hit = this.paper_main_object.children['entites'];
  1314. break;
  1315. case "superposition":
  1316. group_to_hit = this.paper_main_object.children['entites_superposes'];
  1317. break;
  1318. case "action":
  1319. group_to_hit = this.paper_main_object.children['agissantes'];
  1320. break;
  1321. case "doleancer":
  1322. group_to_hit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1323. break;
  1324. }
  1325. if (group_to_hit) {
  1326. let result = group_to_hit.hitTest(event.point);
  1327. console.log('click result', result);
  1328. if (result) {
  1329. switch (this.map_mode) {
  1330. case "terraindevie":
  1331. case "superposition":
  1332. case "action":
  1333. // we have clicked on an entite
  1334. this.$router.push({
  1335. name: 'concernement',
  1336. hash: `#${this.map_mode}`,
  1337. params: {id: this.cid, mapitemid: this.id, eid: result.item.item_id}
  1338. });
  1339. break;
  1340. case "doleancer":
  1341. this.setOpenedDoleanceField(this.cid, result.item.did, result.item.field, result.item.field_index);
  1342. break;
  1343. }
  1344. } else {
  1345. // otherwise we close the entite and come back to the concernement
  1346. this.$router.push({
  1347. name: 'concernement',
  1348. hash: `#${this.map_mode}`,
  1349. params: {id: this.cid, mapitemid: this.id}
  1350. });
  1351. // reset the mousehover
  1352. this.resetHoverElmt();
  1353. }
  1354. }
  1355. }
  1356. }.bind(this);
  1357. },
  1358. resetHoverElmt(){
  1359. // console.log('resetHoverElmt');
  1360. setTimeout(()=>{
  1361. this.setHoverElmt(null);
  1362. }, 100);
  1363. },
  1364. // OPEN / CLOSE (with tween)
  1365. openClose(open) {
  1366. console.log(`ConcernementsMapItem ${this.id} openClose: ${open}`);
  1367. if (this.tween) {
  1368. this.tween.stop();
  1369. }
  1370. if (open) {
  1371. this.is_opening = true;
  1372. // create the paper objects to display (like entite, besoin, etc)
  1373. this.setPaperContents();
  1374. this.handlePaperVisibilityOnBeforeOpen();
  1375. // calcul opened size regarding window size
  1376. // let ch = this.canvas.height;
  1377. // let s = this.canvas.height / (this.ray*2.8)
  1378. let s = Math.min(this.canvas.height, this.canvas.width - this.cartouch_width) / (this.ray*2.8)
  1379. // create once the opening tweening
  1380. this.tween = new Tween.Tween({s: this.scale, x: this.pos.x, y: this.pos.y, o: 0})
  1381. .to({
  1382. s: s,
  1383. x: (this.canvas.width - this.cartouch_width) / 2,
  1384. y: this.canvas.height / 2,
  1385. o: 0.8
  1386. }, 800)
  1387. .onUpdate((obj) => {
  1388. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  1389. // revert to the original size (by reverting the previous scale)
  1390. Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  1391. this.paper_main_object.scale(1 / this.scale);
  1392. // then scale again to new scale
  1393. Matter.Body.scale(this.body, obj.s, obj.s)
  1394. this.paper_main_object.scale(obj.s);
  1395. // update superposition constraints points poisitions
  1396. if (this.concernement.superposition_constraints_id
  1397. && this.concernement.superposition_constraints_id[this.id]
  1398. && this.concernement.superposition_constraints_id[this.id].length)
  1399. {
  1400. // get all the constraints of the world
  1401. let all_constrains = Matter.Composite.allConstraints(this.matterEngine.world);
  1402. // loop through all constraint ids recorded in the concernement
  1403. // get only the constraint of this concernement mapitem
  1404. for(let constraint_id of this.concernement.superposition_constraints_id[this.id]) {
  1405. // get the right constraint object from matter
  1406. let constraint;
  1407. for (const c of all_constrains) {
  1408. if (c.id === constraint_id) {
  1409. constraint = c;
  1410. break;
  1411. }
  1412. }
  1413. if (constraint) {
  1414. // tween the constraint point
  1415. let ab = this.cid === constraint.concernementA.id ? 'A' : 'B';
  1416. let point = constraint[`point${ab}`];
  1417. // revert to the original point
  1418. let oripoint = Matter.Vector.create(point.x / this.scale, point.y / this.scale)
  1419. // newpoint from original point
  1420. let newpoint = Matter.Vector.create(oripoint.x * obj.s, oripoint.y * obj.s)
  1421. constraint[`point${ab}`] = newpoint;
  1422. }
  1423. }
  1424. }
  1425. // record new scale
  1426. this.prev_scale = this.scale;
  1427. this.scale = obj.s;
  1428. this.opacity = obj.o;
  1429. // console.log('tween update obj.s', obj.s);
  1430. this.pos = {x:obj.x, y:obj.y};
  1431. Matter.Body.setPosition(this.body, this.pos);
  1432. })
  1433. .onComplete((obj) => {
  1434. // console.log('tween complete obj.s', obj.s);
  1435. // record tween one last time
  1436. this.prev_scale = this.scale = obj.s;
  1437. this.opacity = obj.o;
  1438. this.pos = {x:obj.x, y:obj.y};
  1439. Matter.Body.setPosition(this.body, this.pos);
  1440. this.handlePaperVisibilityOnOpened();
  1441. // fix the concernement position with a constraint
  1442. this.constraint = Matter.Constraint.create({
  1443. pointA: this.pos,
  1444. bodyB: this.body,
  1445. stiffness: 1,
  1446. damping: 0,
  1447. length: 0
  1448. });
  1449. Matter.Composite.add(this.matterEngine.world, [this.body, this.constraint]);
  1450. // paper bring to front
  1451. this.paper_main_object.bringToFront();
  1452. this.is_opening = false;
  1453. });
  1454. // recreate the matter engine event to get it a the end of the events stack
  1455. Matter.Events.off(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  1456. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  1457. } else {
  1458. // closing
  1459. this.is_closing = true;
  1460. if(this.constraint){
  1461. Matter.Composite.remove(this.matterEngine.world, this.constraint);
  1462. }
  1463. this.concernement.active_revision = this.concernement.revision_id;
  1464. this.handlePaperVisibilityOnBeforeClose();
  1465. // this.setOpenedRecit(null);
  1466. // create once the closing tween
  1467. this.tween = new Tween.Tween({s: this.scale, o: 1})
  1468. .to({s: 1, o: 0}, 500)
  1469. .onUpdate((obj) => {
  1470. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  1471. // revert to the original size (by reverting the previous scale)
  1472. Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  1473. this.paper_main_object.scale(1 / this.scale);
  1474. // then scale again to new scale
  1475. Matter.Body.scale(this.body, obj.s, obj.s)
  1476. this.paper_main_object.scale(obj.s);
  1477. // update superposition constraints points positions
  1478. if (this.concernement.superposition_constraints_id
  1479. && this.concernement.superposition_constraints_id[this.id]
  1480. && this.concernement.superposition_constraints_id[this.id].length)
  1481. {
  1482. // get all the constraints of the world
  1483. let all_constrains = Matter.Composite.allConstraints(this.matterEngine.world);
  1484. // loop through all constraint ids recorded in the concernement
  1485. // get only the constraint of this concernement mapitem
  1486. for(let constraint_id of this.concernement.superposition_constraints_id[this.id]) {
  1487. // get the right constraint object from matter
  1488. let constraint;
  1489. for (const c of all_constrains) {
  1490. if (c.id === constraint_id) {
  1491. constraint = c;
  1492. break;
  1493. }
  1494. }
  1495. if (constraint) {
  1496. // tween the constraint point
  1497. let ab = this.cid === constraint.concernementA.id ? 'A' : 'B';
  1498. let point = constraint[`point${ab}`];
  1499. // revert to the original point
  1500. let oripoint = Matter.Vector.create(point.x / this.scale, point.y / this.scale)
  1501. // newpoint from original point
  1502. let newpoint = Matter.Vector.create(oripoint.x * obj.s, oripoint.y * obj.s)
  1503. constraint[`point${ab}`] = newpoint;
  1504. }
  1505. }
  1506. }
  1507. // record new scale
  1508. this.prev_scale = this.scale;
  1509. this.scale = obj.s;
  1510. this.opacity = obj.o;
  1511. })
  1512. .onComplete((obj) => {
  1513. this.prev_scale = this.scale = 1;
  1514. this.handlePaperVisibilityOnClosed();
  1515. this.clearPaperContents();
  1516. this.is_closing = false;
  1517. });
  1518. }
  1519. this.tween.easing(Tween.Easing.Quadratic.InOut).start();
  1520. },
  1521. // ENGINE UPDATE
  1522. onBeforeEngineUpdate (event) {
  1523. // update the opening/closing tweening
  1524. if (this.tween) {
  1525. this.tween.update();
  1526. }
  1527. if (this.opened_concernement) {
  1528. // console.log("try to push aside", this.opened_concernement.opened_mapitem_id, this.id, this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id]);
  1529. // console.log(this.id, this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id].indexOf(this.id));
  1530. if (
  1531. // this.opened_concernement.id !== this.cid &&
  1532. this.opened_concernement.opened_mapitem_id !== this.id
  1533. // && (!this.opened_concernement.all_superposed_concernements_id || this.opened_concernement.all_superposed_concernements_id.indexOf(this.id) < 0)
  1534. && (!this.opened_concernement.superposed_mapitem_id_by_mapitem_id
  1535. || !this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id] // when we are not in superposition mode
  1536. || this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id].indexOf(this.id) < 0) // this mapitem is not superposed to the opened mapitem
  1537. ) {
  1538. this.pushAside()
  1539. }
  1540. } else if (!this.is_hover){
  1541. if (this.map_mode === 'proximite'
  1542. || this.map_mode === 'superposition'
  1543. || this.map_mode === 'puissancedagir'
  1544. || this.map_mode === 'action'
  1545. || this.map_mode === 'doleancer'){ // apply focus forces : move unfocused on the sides and focused on the center
  1546. this.applyFocusForces(); //
  1547. } else {
  1548. this.applyNormalForces(); // if terrain de vie apply force to gently reject items from the sides and so create a continuasly mvmt
  1549. }
  1550. } else {
  1551. // stop movement if is mouse hover
  1552. Matter.Body.setVelocity(this.body, {x:0, y:0})
  1553. }
  1554. // reset all matter rotation forces otherwise items will spin when colide
  1555. Matter.Body.setAngle(this.body, 0);
  1556. Matter.Body.setAngularSpeed(this.body, 0);
  1557. },
  1558. applyFocusForces(){
  1559. if(!this.isFocused()) {
  1560. this.pushAside()
  1561. }else{
  1562. this.bringToCenter()
  1563. }
  1564. },
  1565. isFocused(){
  1566. return this.map_mode === 'terraindevie'
  1567. || (this.map_mode === 'proximite' && this.concernement.has_proximites)
  1568. || (this.map_mode === 'superposition' && this.concernement.has_superpositions)
  1569. || (this.map_mode === 'action' && this.concernement.has_agissantes)
  1570. || (this.map_mode === 'puissancedagir' && this.concernement.has_puissancedagir)
  1571. || (this.map_mode === 'doleancer' && this.concernement.has_doleance);
  1572. },
  1573. pushAside(){
  1574. // console.log('pushAside');
  1575. // apply a force in direction of one side or an other depending of the start position
  1576. // the force is exponentialy proportional to the distance from the side
  1577. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  1578. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1579. let pseudo_center_x = this.opened_concernement
  1580. ? (this.canvas.width - this.cartouch_width) / 2
  1581. : this.canvas.width / 2;
  1582. // get the direction to the closest side
  1583. let dir =
  1584. this.pos.x < pseudo_center_x
  1585. || (this.map_mode === 'superposition'
  1586. && this.concernement.superposition_constraints_id
  1587. && this.concernement.superposition_constraints_id[this.id]
  1588. && this.concernement.superposition_constraints_id[this.id].length) // go to the left if has superposition constraint applied
  1589. ? -1 // to the left
  1590. : -1; // to the right
  1591. // max and min item position
  1592. let minp = 0;
  1593. let maxp = dir < 0
  1594. ? pseudo_center_x
  1595. : this.canvas.width - pseudo_center_x;
  1596. // max and min force
  1597. let minf = 0;
  1598. let maxf = 6;
  1599. // scale factor
  1600. let scale = (maxf-minf) / (maxp-minp);
  1601. // get the inversed distance
  1602. let dist = dir < 0
  1603. ? this.pos.x
  1604. : this.canvas.width - this.pos.x; // get the distance from the side
  1605. // // calculate the force
  1606. // let x_force = Math.pow(dist/700,100) * dir;
  1607. // calculate the logarithmic force
  1608. let x_force = Math.exp(minf + scale*(dist-minp)) * dir;
  1609. let ori_pos = {x:pseudo_center_x, y:this.body.position.y};
  1610. Matter.Body.applyForce(
  1611. this.body,
  1612. ori_pos,
  1613. {
  1614. x: x_force,
  1615. y: 0
  1616. }
  1617. );
  1618. this.body.frictionAir = 0.01;
  1619. },
  1620. bringToCenter(){
  1621. // bring to the centre
  1622. // apply a force in direction of one side or an other depending of the start position
  1623. // the force is exponentialy inversed proportional to the distance from the side
  1624. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  1625. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1626. let pseudo_center_x = this.opened_concernement
  1627. ? (this.canvas.width - this.cartouch_width) / 2
  1628. : this.canvas.width / 2;
  1629. let dir = this.pos.x > pseudo_center_x
  1630. ? -1 // to left
  1631. : 1; // to right
  1632. // get the inversed distance from the side
  1633. let dist = dir < 0
  1634. ? this.pos.x - pseudo_center_x
  1635. : pseudo_center_x - this.pos.x;
  1636. if (dist > this.canvas.width/6) { // apply decreasing forces and increasing friction air only out of center to let the items float in the center zone
  1637. // max & min item position
  1638. let minp = 0;
  1639. let maxp = dir < 0
  1640. ? this.canvas.width - pseudo_center_x
  1641. : pseudo_center_x;
  1642. // X force decrsinsing near the center
  1643. // max and min force
  1644. let xminf = 0;
  1645. let xmaxf = 1 + Math.random()*4;
  1646. // scale factor
  1647. let xscale = (xmaxf-xminf) / (maxp-minp);
  1648. let x_force = Math.exp(xminf + xscale*(dist-minp)) * dir;
  1649. let ori_pos = dir < 0
  1650. ? {x:this.canvas.width, y:this.body.position.y}
  1651. : {x:0, y:this.body.position.y};
  1652. Matter.Body.applyForce(
  1653. this.body,
  1654. ori_pos,
  1655. {
  1656. x: x_force,
  1657. y: 0
  1658. }
  1659. );
  1660. // Friction air fa (increasing near the center)
  1661. let fa_minf = 0;
  1662. let fa_maxf = 6;
  1663. // scale factor
  1664. let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  1665. let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  1666. this.body.frictionAir = fa;
  1667. // console.log(`bringToCenter dist:${dist}, x_force:${x_force}, friction air:${fa}`);
  1668. } else { // in the ceneter zone let items floats freely
  1669. let fa = 0.01;
  1670. let velocity = Matter.Body.getVelocity(this.body);
  1671. if (velocity.x) {
  1672. let velocityx = Math.abs(velocity.x);
  1673. // map a range of numbers to another range of numbers
  1674. // INFO https://stackoverflow.com/a/46462321
  1675. let velocity_range = [50, 1000];
  1676. let fa_range = [0,3];
  1677. fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  1678. // console.log(`bringToCenter velocityx:${velocityx}, fa:${fa}, velocity`, velocity);
  1679. } else {
  1680. console.warn(`bringToCenter no velocity`, velocity);
  1681. }
  1682. this.body.frictionAir = fa;
  1683. }
  1684. },
  1685. applyShuffleForces() {
  1686. // console.log('applyShuffleForces');
  1687. // var dist, dir, x_velocity;
  1688. // dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
  1689. // dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
  1690. // x_velocity = Math.pow(dist/650,10) * dir;
  1691. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1692. let pseudo_center_x = this.opened_concernement
  1693. ? (this.canvas.width - this.cartouch_width) / 2
  1694. : this.canvas.width / 2;
  1695. // get the direction to the centre
  1696. let dir = this.pos.x > pseudo_center_x
  1697. ? -1 // to left
  1698. : 1; // to right
  1699. // get the inversed distance from the side
  1700. let dist = dir < 0
  1701. ? this.pos.x - pseudo_center_x
  1702. : pseudo_center_x - this.pos.x;
  1703. dist = dist > 1000 ? 1000 : dist;
  1704. if (dist > this.canvas.width/6) { // apply decreasing forces and increasing friction air only out of center to let the items float in the center zone
  1705. // max & min item position
  1706. let minp = 0;
  1707. let maxp = dir < 0
  1708. ? this.canvas.width - pseudo_center_x
  1709. : pseudo_center_x;
  1710. // X max and min force
  1711. let xminf = 0;
  1712. let xmaxf = 4 + Math.random()*3;
  1713. // scale factor
  1714. let xscale = (xmaxf-xminf) / (maxp-minp);
  1715. let x_velocity = Math.exp(xminf + xscale*(dist-minp))*dir;
  1716. // let y_velocity = 30 + Math.random() * 50;
  1717. // y_velocity = Math.random() > 0.5 ? -y_velocity : y_velocity;
  1718. // X max and min force
  1719. let yminf = 1;
  1720. let ymaxf = 2 + Math.random()*4;
  1721. // scale factor
  1722. let yscale = (ymaxf-yminf) / (maxp-minp);
  1723. let y_velocity = Math.exp(yminf + yscale*(dist-minp)) * (Math.random() > 0.5 ? 1 : -1); // let ori_pos = dir < 0
  1724. // ? {x:this.canvas.width, y:this.body.position.y}
  1725. // : {x:0, y:this.body.position.y};
  1726. // let x_force = Math.pow(dist/800,10) * dir;
  1727. console.log(`applyShuffleForces dir:${dir}, maxp:${maxp}, dist:${dist}, x_velocity:${x_velocity}, y_velocity:${y_velocity}`);
  1728. Matter.Body.setVelocity(this.body, {x: x_velocity, y: y_velocity});
  1729. } else { // if in center zone real shuffle velocity
  1730. // x_velocity = -50 + Math.random()*100;
  1731. // y_velocity = -50 + Math.random()*100;
  1732. // this.setInitBodyVelocity();
  1733. }
  1734. this.body.frictionAir = 0.01;
  1735. },
  1736. applyNormalForces () {
  1737. // bring gently to the centre
  1738. // apply a force in direction of one side or an other depending of the start position
  1739. // the force is exponentialy inversed proportional to the distance from the side
  1740. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  1741. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1742. // X
  1743. let pseudo_center_x = this.opened_concernement
  1744. ? (this.canvas.width - this.cartouch_width) / 2
  1745. : this.canvas.width / 2;
  1746. let xdir = this.pos.x > pseudo_center_x
  1747. ? -1 // to left
  1748. : 1; // to right
  1749. // get the inversed distance from the side
  1750. let xdist = xdir < 0
  1751. ? this.pos.x - pseudo_center_x
  1752. : pseudo_center_x - this.pos.x;
  1753. // Y
  1754. let pseudo_center_y = this.canvas.height / 2;
  1755. let ydir = this.pos.y > pseudo_center_y
  1756. ? -1 // to top
  1757. : 1; // to bottom
  1758. // get the inversed distance from the side
  1759. let ydist = ydir < 0
  1760. ? this.pos.y - pseudo_center_y
  1761. : pseudo_center_y - this.pos.y;
  1762. let k = 4;
  1763. if (xdist > this.canvas.width/k || ydist > this.canvas.height/k) { // apply decreasing forces and increasing friction air only out of center to let the items float in the center zone
  1764. // max and min force
  1765. let minf = 0;
  1766. let maxf = 0.1;
  1767. // X
  1768. // max & min item position
  1769. let xminp = 0;
  1770. let xmaxp = xdir < 0
  1771. ? this.canvas.width - pseudo_center_x
  1772. : pseudo_center_x;
  1773. // X force decrsinsing near the center
  1774. // scale factor
  1775. let xscale = (maxf-minf) / (xmaxp-xminp);
  1776. let x_force = Math.exp(minf + xscale*(xdist-xminp)) * xdir;
  1777. let xori_pos = xdir < 0
  1778. ? {x:this.canvas.width, y:this.body.position.y}
  1779. : {x:0, y:this.body.position.y};
  1780. Matter.Body.applyForce(
  1781. this.body,
  1782. xori_pos,
  1783. {
  1784. x: x_force,
  1785. y: 0
  1786. }
  1787. );
  1788. // Y
  1789. // max & min item position
  1790. let yminp = 0;
  1791. let ymaxp = ydir < 0
  1792. ? this.canvas.height - pseudo_center_y
  1793. : pseudo_center_y;
  1794. // X force decrsinsing near the center
  1795. // scale factor
  1796. let yscale = (maxf-minf) / (ymaxp-yminp);
  1797. let y_force = Math.exp(minf + yscale*(ydist-xminp)) * ydir;
  1798. let yori_pos = ydir < 0
  1799. ? {x:this.body.position.x, y:this.canvas.height}
  1800. : {x:this.body.position.x, y:0};
  1801. Matter.Body.applyForce(
  1802. this.body,
  1803. yori_pos,
  1804. {
  1805. x: 0,
  1806. y: y_force
  1807. }
  1808. );
  1809. // // Friction air fa (increasing near the center)
  1810. // let fa_minf = 0;
  1811. // let fa_maxf = 6;
  1812. // // scale factor
  1813. // let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  1814. // let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  1815. // this.body.frictionAir = fa;
  1816. // console.log(`bringToCenter dist:${xdist}, x_force:${x_force}`);
  1817. } else { // in the ceneter zone let items floats freely
  1818. // let velocity = Matter.Body.getVelocity(this.body);
  1819. // let velocityx = Math.abs(velocity.x);
  1820. // // map a range of numbers to another range of numbers
  1821. // // INFO https://stackoverflow.com/a/46462321
  1822. // let velocity_range = [50, 1000];
  1823. // let fa_range = [0,3];
  1824. // let fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  1825. // console.log(`bringToCenter velocity.x:${velocity.x}, fa:${fa}`);
  1826. // this.body.frictionAir = fa;
  1827. this.body.frictionAir = 0.1;
  1828. }
  1829. },
  1830. respawn() {
  1831. // respawn element if outside screen
  1832. if(this.pos.x <= 0
  1833. || this.pos.x >= this.canvas.width
  1834. || this.pos.y <= 0
  1835. || this.pos.y >= this.canvas.height){
  1836. this.pos = this.getRandomPos()
  1837. Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
  1838. // this.setInitBodyVelocity();
  1839. Matter.Body.setVelocity(this.body, {x:0,y:0});
  1840. }
  1841. },
  1842. onAfterEngineUpdate (event) {
  1843. this.respawn();
  1844. this.paper_main_object.position = this.pos = this.body.position;
  1845. this.handlePaperVisibilityOnAfterEnginUpdate()
  1846. },
  1847. },
  1848. render() {
  1849. // console.log('render()', this.ctx);
  1850. },
  1851. }
  1852. </script>