ConcernementMapItem.vue 82 KB

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