ConcernementMapItem.vue 61 KB

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