ConcernementMapItem.vue 78 KB

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