ConcernementMapItem.vue 80 KB

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