ConcernementMapItem.vue 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721
  1. <script>
  2. import { nextTick } from 'vue'
  3. import Matter from "matter-js";
  4. import MatterAttractors from "matter-attractors";
  5. // Matter.use(MatterAttractors);
  6. // import polydecomp from "poly-decomp";
  7. import paper from 'paper';
  8. // import { easeInOutQuad, easeInOutQuart } from 'easing-utils';
  9. import Tween from "@tweenjs/tween.js";
  10. import { mapState, mapActions } from 'pinia'
  11. import { ConcernementsStore } from '@/stores/concernements'
  12. import { CommonStore } from '@/stores/common'
  13. import { mdiPictureInPictureBottomRight } from '@mdi/js';
  14. export default {
  15. inject: ['canvasMap', 'matterEngine'],
  16. data() {
  17. return {
  18. id: null,
  19. cid: null,
  20. concernement: null,
  21. active_revision: null,
  22. entities: null,
  23. superposedEntiteID: null,
  24. // superposedEntitesIDsList: [],
  25. canvas: null,
  26. ctx: null,
  27. pos : {
  28. x: 0,
  29. y: 0
  30. },
  31. ray: 100,
  32. time: 0,
  33. salientPoints: [],
  34. scale: 1,
  35. prev_scale: 1,
  36. opacity: 0,
  37. tween: null,
  38. is_visible: true,
  39. is_open: false,
  40. is_opening: false,
  41. is_closing: false,
  42. is_hover: false,
  43. //
  44. body: null,
  45. body_parts: [],
  46. constraint: null,
  47. //
  48. paper_main_object: {},
  49. paper_groups: {},
  50. //
  51. doleance_transition: false,
  52. //
  53. }
  54. },
  55. props: ['mapitem'],
  56. computed: {
  57. ...mapState(ConcernementsStore,['map_mode',
  58. 'concernementsByID',
  59. 'allEntitesById',
  60. 'allSuperpositions_bycids',
  61. 'allSuperpositions_clustered',
  62. 'allMapItems_byid',
  63. 'opened_concernement',
  64. 'opened_entite_id']),
  65. ...mapState(CommonStore,['hover_elmt',
  66. 'map_item_ray',
  67. 'cartouch_width',
  68. 'cartouch_is_opened',
  69. 'paper_symbol_definitions'])
  70. },
  71. created () {
  72. // console.log('ConcernementMapItem created', this.mapitem);
  73. // this.id = this.superposition_id ? `${this.cid}___${this.superposition_id}` : this.cid;
  74. // console.log(`this.mapitem.id`, this.mapitem.id);
  75. this.id = this.mapitem.id;
  76. this.concernement = this.mapitem.concernement;
  77. this.cid = this.concernement.id;
  78. // console.log('mapitem this.cid', this.cid);
  79. this.active_revision = this.concernement.active_revision;
  80. // console.log(`ConcernementsMapItem ${this.id} created`);
  81. // this.entites = this.concernement.entites
  82. this.entites = this.concernement.revisions_byid[this.concernement.revision_id].entites;
  83. // disable concernement if less than 3 entite
  84. if(this.entites.length < 3){
  85. this.hideShowConcernement(this.concernement.id, false);
  86. } else{
  87. // record canvas and ctx for rendering (drawing)
  88. this.canvas = this.canvasMap.canvas
  89. this.ctx = this.canvasMap.ctx
  90. // define the ray regarding the width and height of screen
  91. this.ray = this.map_item_ray;
  92. // console.log(`this.ray: ${this.ray}`);
  93. //
  94. this.parseEntityPointsValues()
  95. // this.getSalientPoints()
  96. this.sailentEntites = this.concernement.sailentEntites = this.getJarvisEnvelopeConvexeEntites(this.entites)
  97. // define init position of the item
  98. this.pos = this.getRandomPos();
  99. //
  100. this.getSuperposedEntiteID();
  101. //
  102. this.initMatterBody()
  103. //
  104. this.initPaperObjects()
  105. //
  106. if(this.mapitem.clone){
  107. if(this.map_mode === 'superposition'){
  108. this.handleMapitemVisibility(true);
  109. }else{
  110. this.handleMapitemVisibility(false);
  111. }
  112. }
  113. }
  114. // this.setConcernementMapItem(this.cid, this);
  115. // this.setConcernementScale(this.cid, this.scale);
  116. this.mapitem.scale = this.scale;
  117. // window.addEventListener('resize', this.onWindowResize.bind(this));
  118. },
  119. // mounted() {
  120. // console.log(`ConcernementsMapItem ${this.concernement.id} mounted`, this.canvasMap.canvas);
  121. // },
  122. // beforeUnmount () {
  123. unmounted () {
  124. // console.log(`mapitem ${this.id} unmounted`);
  125. if(this.mapitem.clone) {
  126. // console.log(`this.paper_main_object ${this.paper_main_object.id}`, this.paper_main_object);
  127. paper.project.getItem({id:this.paper_main_object.id}).remove();
  128. };
  129. if (this.body) {
  130. Matter.Composite.remove(this.matterEngine.world, this.body);
  131. }
  132. },
  133. watch: {
  134. // canvasMap (n, o) {
  135. // console.log("concernementItem watch canvasMap", o, n);
  136. // }
  137. canvasMap: {
  138. handler (n, o){
  139. // console.log("concernementItem watch canvasMap.ctx", typeof this.canvas, o, n);
  140. if (!this.canvas) {
  141. this.initCanvasMap()
  142. }
  143. },
  144. deep: true
  145. },
  146. opened_concernement: {
  147. handler (n, o) {
  148. if (!n) { // if there is no opened concernement
  149. this.is_open = false;
  150. this.applyShuffleForces();
  151. } else {
  152. if(this.cid === n.id && this.id === n.opened_mapitem_id){
  153. this.is_open = this.mapitem.is_open = true;
  154. }else{
  155. this.is_open = this.mapitem.is_open = false;
  156. }
  157. }
  158. },
  159. deep: true
  160. },
  161. is_open: {
  162. handler (n, o) {
  163. // console.log('mapItem watch is_open', n, this.is_open);
  164. if(n){ // opened
  165. this.openClose(true);
  166. }else if(o) { // closed if was opened
  167. this.openClose(false);
  168. }
  169. },
  170. deep: true
  171. },
  172. map_mode: {
  173. handler (n, o) {
  174. console.log('concernementMapItem watch map_mode', o, n);
  175. // apply a little force to check the map when returning to terrain de vie
  176. if (n === 'terraindevie' && !this.opened_concernement) {
  177. this.applyShuffleForces();
  178. }
  179. // if map_item is open and we change the mad mode, redefined what is displayed in the "boussole"
  180. if (this.is_open & n !== o) {
  181. this.setPaperContents();
  182. }
  183. // Clones visibility
  184. if(this.mapitem.clone){
  185. let is_visible = false;
  186. if(n === 'superposition'){
  187. // superposition mapemode all clones are visible
  188. is_visible = true;
  189. }else{
  190. if(this.opened_concernement){
  191. if(this.mapitem.is_open){
  192. is_visible = true;
  193. }else{
  194. // keep open the clones that are superposed to the opened map_item
  195. let opened_mapitem_id = this.opened_concernement.opened_mapitem_id;
  196. if (this.opened_concernement.superposed_mapitem_id_by_mapitem_id && this.opened_concernement.superposed_mapitem_id_by_mapitem_id[opened_mapitem_id]) {
  197. if(this.opened_concernement.superposed_mapitem_id_by_mapitem_id[opened_mapitem_id].indexOf(this.mapitem.id) !== -1){
  198. is_visible = true;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. this.handleMapitemVisibility(is_visible);
  205. }
  206. },
  207. deep: true
  208. },
  209. hover_elmt: {
  210. handler (n, o) {
  211. // console.log(`watch hover_elmt ${this.id}`, o, n);
  212. if (n && (n.type === 'concernement' || n.type === 'superposition') && n.id === this.id) {
  213. this.is_hover = true;
  214. } else {
  215. this.is_hover = false;
  216. }
  217. },
  218. deep: true
  219. },
  220. is_hover: {
  221. handler (n, o) {
  222. this.onIsHover(n);
  223. },
  224. deep: true
  225. },
  226. concernement: {
  227. handler (n, o){
  228. // check for active_revision changes
  229. // do not trigger on first variable filling (if o is null)
  230. if(this.is_open && o && o.active_revision && n.active_revision && this.map_mode === "terraindevie"){
  231. // be aware that here o & n active_revision are already the same :( !?
  232. console.log(`concernementMapItem watch concernement o.active_revision:${o.active_revision}, n.active_revision:${n.active_revision}`);
  233. this.resetPaperActiveRevision();
  234. }
  235. // // check for doleance change
  236. // if (this.is_open && o && o.opened_doleance && n.opened_doleance && this.map_mode === "doleancer") {
  237. // // be aware that here o & n opened_doleance are already the same :( !?
  238. // console.log(`concernementMapItem watch concernement o.opened_doleance:${o.opened_doleance.id}, n.opened_doleance:${n.opened_doleance.id}`);
  239. // }
  240. },
  241. deep: true
  242. },
  243. // window as been resized
  244. map_item_ray: {
  245. handler (n, o) {
  246. console.log(`map_item_ray updated o: ${o}, n: ${n}`);
  247. this.ray = n;
  248. this.updateMatterBodyRay();
  249. this.updatePaperObjectSize(n,o);
  250. },
  251. deep: true
  252. }
  253. },
  254. methods: {
  255. ...mapActions(CommonStore,['setHoverElmt']),
  256. ...mapActions(ConcernementsStore,['openCloseConcernements',
  257. // 'setConcernementMapItem',
  258. 'hideShowConcernement',
  259. 'setOpenedEntiteId',
  260. 'setActiveRevision',
  261. 'setBesoinPaperId',
  262. 'setOpenedDoleanceField',
  263. 'setOpenedRecit',
  264. // 'setConcernementScale',
  265. ]),
  266. // getResponsiveRay(){
  267. // return Math.min(this.canvas.width, this.canvas.height) * 0.08;
  268. // },
  269. parseEntityPointsValues (){
  270. // converts data (menace/maintien, actuel/future, prise) into atcual position x,y
  271. for (let r = 0; r < this.concernement.revisions.length; r++) { // loop through all revisions
  272. for (let i = 0; i < this.concernement.revisions[r].entites.length; i++) { // loop through all entite for each revision
  273. let entite = this.concernement.revisions[r].entites[i]
  274. // console.log('entite', entite);
  275. entite.display = {
  276. alpha: null,
  277. ray: null
  278. }
  279. // RAYON
  280. // https://stackoverflow.com/questions/5731863/mapping-a-numeric-range-onto-another
  281. // slope = (output_end - output_start) / (input_end - input_start)
  282. // output = output_start + slope * (input - input_start)
  283. // from range 0 -> 100 to range 0 -> this.ray
  284. let init_max = 100
  285. let slope = this.ray / init_max
  286. entite.display.ray = slope * (init_max - entite.prise);
  287. // if (this.concernement.id === 28) {
  288. // console.log(`entity prise: ${entite.prise} | ray: ${entite.display.ray}`);
  289. // }
  290. // ANGLE
  291. // -90 <= mm <= 90
  292. if (entite.actuelfuture) {
  293. // future en haut : 180 <= a <= 360
  294. // from -90 -> 90 to range 180 -> 360
  295. entite.display.alpha = entite.menacemaintien + 270
  296. } else {
  297. // actuel: en bas : O <= a <= 180
  298. // from -90 -> 90 to range 180 -> 0
  299. entite.display.alpha = -1 * entite.menacemaintien + 90
  300. }
  301. // POSITION X Y (par rapport au centre du concernement)
  302. entite.display.pos = {
  303. x: entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180)),
  304. y: entite.display.ray * Math.sin(entite.display.alpha * (Math.PI/180))
  305. }
  306. // if (entite.entite) {
  307. // this.entites_byid[entite.entite.id].display = entite.display;
  308. // }
  309. }
  310. }
  311. },
  312. getSalientPoints_OLD() {
  313. // debugger
  314. // console.log(this.entites);
  315. let arc = 360/30;
  316. // loop through arcs
  317. // for (let i = 360/arc; i >= 0 ; i--) {
  318. for (let i = 0; i <= 360/arc ; i++) {
  319. // loop through entities to find the farest on the arc
  320. let max_r = 0;
  321. let farest = null;
  322. for (let j = 0; j < this.entites.length; j++) {
  323. let entite = this.entites[j];
  324. if(arc*i <= entite.display.alpha && entite.display.alpha <= arc*i+arc) { // if entity is in arc
  325. if (entite.display.ray > max_r) { // && entite.display.ray > this.ray/2 // and farest from minimu
  326. // if entity is farest from precedent one
  327. max_r = entite.display.ray;
  328. // recalcul x & y to get a little padding between entite and contour by increasing ray
  329. farest = {
  330. alpha: entite.display.alpha,
  331. ray: entite.display.ray,
  332. pos: {
  333. x: (entite.display.ray + 3) * Math.cos(entite.display.alpha * (Math.PI/180)),
  334. y: (entite.display.ray + 3) * Math.sin(entite.display.alpha * (Math.PI/180))
  335. }
  336. };
  337. }
  338. }
  339. }
  340. if (farest) {
  341. this.salientPoints.push(farest)
  342. }
  343. }
  344. // console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints);
  345. },
  346. getJarvisEnvelopeConvexeEntites(entites){
  347. let sailentEntites = [];
  348. // https://www.geeksforgeeks.org/convex-hull-using-jarvis-algorithm-or-wrapping/
  349. // find the most left point
  350. let l, min_x = null;
  351. for (let i = 0; i < entites.length; i++) {
  352. let entite = entites[i];
  353. let x = entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180));
  354. if(!min_x || min_x > x){
  355. l = i;
  356. min_x = x;
  357. }
  358. }
  359. // Start from leftmost point, keep moving clockwise
  360. // until reach the start point again. This loop runs O(h)
  361. // times where h is number of points in result or output.
  362. let p = l, q;
  363. do {
  364. // console.log(`do while ${this.id}`, p);
  365. // Add current point to result
  366. let entite = entites[p];
  367. sailentEntites.push(entite);
  368. // Search for a point 'q' such that
  369. // orientation(p, q, x) is clockwise
  370. // for all points 'x'. The idea is to keep
  371. // track of last visited most clockwise
  372. // point in q. If any point 'i' is more
  373. // clockwise than q, then update q.
  374. q = (p + 1) % entites.length;
  375. for (let i = 0; i < entites.length; i++) {
  376. let p_x = (entites[p].display.ray) * Math.cos(entites[p].display.alpha * (Math.PI/180));
  377. let p_y = (entites[p].display.ray) * Math.sin(entites[p].display.alpha * (Math.PI/180));
  378. let i_x = (entites[i].display.ray) * Math.cos(entites[i].display.alpha * (Math.PI/180));
  379. let i_y = (entites[i].display.ray) * Math.sin(entites[i].display.alpha * (Math.PI/180));
  380. let q_x = (entites[q].display.ray) * Math.cos(entites[q].display.alpha * (Math.PI/180));
  381. let q_y = (entites[q].display.ray) * Math.sin(entites[q].display.alpha * (Math.PI/180));
  382. let val = (i_y - p_y) * (q_x - i_x) - (i_x - p_x) * (q_y - i_y);
  383. // If i is more clockwise than current q, then update q
  384. if (val > 0){
  385. q = i;
  386. }
  387. }
  388. // Now q is the most clockwise with
  389. // respect to p. Set p as q for next iteration,
  390. // so that q is added to result 'hull'
  391. p = q;
  392. } while (p != l);
  393. return sailentEntites;
  394. },
  395. getRandomPos(){
  396. let pad = 200;
  397. // if (this.concernement.id === 56) {
  398. // return {
  399. // x: Math.random() > 0.5 ? pad : this.canvas.width - pad,
  400. // y: pad + this.ray/2 + Math.random()*(this.canvas.height - this.ray - pad)
  401. // };
  402. // } else {
  403. return {
  404. x: pad + this.getGaussianRandom()*(this.canvas.width - pad*2),
  405. y: pad + this.getGaussianRandom()*(this.canvas.height - pad*2)
  406. };
  407. // }
  408. },
  409. getGaussianRandom(){
  410. // INFO https://stackoverflow.com/a/49434653
  411. let u = 0, v = 0;
  412. while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
  413. while(v === 0) v = Math.random();
  414. let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
  415. num = num / 10.0 + 0.5; // Translate to 0 -> 1
  416. if (num > 1 || num < 0) return this.getGaussianRandom() // resample between 0 and 1
  417. return num
  418. },
  419. getSuperposedEntiteID(){
  420. if (this.concernement.has_superpositions) {
  421. let ids = this.id.match(/(\d+)___(\d+)/i);
  422. if (ids) {
  423. let cluster_index = ids[2];
  424. // console.log(ids, cluster_index);
  425. for(let cid_eid of this.allSuperpositions_clustered[cluster_index] ){
  426. if(this.cid === cid_eid.cid){
  427. this.superposedEntiteID = cid_eid.eid;
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. },
  434. // MATTER BODY
  435. initMatterBody (){
  436. // MATTER
  437. // create the matter body and add it to the engine
  438. if (!this.body) {
  439. // console.log('concernementItem creating body');
  440. // INFO https://github.com/liabru/matter-attractors/issues/8
  441. // INFO https://github.com/liabru/matter-attractors/blob/master/index.js
  442. // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L180
  443. MatterAttractors.Attractors.gravityConstant = -20;
  444. // Create parts of the body : main big circle & entities
  445. // INFO map a range of numbers to another range of numbers https://stackoverflow.com/a/46462321
  446. let entite_range = [3, 100];
  447. let ray_range = [this.ray*0.8,this.ray*1.2];
  448. let ray = ray_range[0] + (this.entites.length - entite_range[0]) * (ray_range[1] - ray_range[0]) / (entite_range[1] - entite_range[0]);
  449. // this.body_parts = [
  450. // Matter.Bodies.circle(0, 0, ray)
  451. // ];
  452. // INFO map a range of numbers to another range of numbers https://stackoverflow.com/a/46462321
  453. let mass_range = [5,15];
  454. let mass = mass_range[0] + (this.entites.length - entite_range[0]) * (mass_range[1] - mass_range[0]) / (entite_range[1] - entite_range[0]);
  455. // create the body
  456. this.body = Matter.Bodies.circle(0, 0, ray, {
  457. item_type: 'concernement',
  458. id: this.id,
  459. frictionAir: 0,
  460. mass: mass,
  461. restitution: 0.06,
  462. collisionFilter: {
  463. group: -1
  464. },
  465. plugin: {
  466. attractors: [
  467. // // there is a built in helper function for Newtonian gravity!
  468. // // you can find out how it works in index.js
  469. MatterAttractors.Attractors.gravity
  470. // function(bodyA, bodyB) {
  471. // var force = {
  472. // x: (bodyA.position.x - bodyB.position.x) * 1e-6,
  473. // y: (bodyA.position.y - bodyB.position.y) * 1e-6
  474. // }
  475. // // apply force to both bodies
  476. // Matter.Body.applyForce(bodyA, bodyA.position, force);
  477. // Matter.Body.applyForce(bodyB, bodyB.position, Matter.Vector.neg(force));
  478. // }
  479. // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L192
  480. // function (bodyA, bodyB){
  481. // // use Newton's law of gravitation
  482. // var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
  483. // distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
  484. // normal = Matter.Vector.normalise(bToA),
  485. // magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
  486. // force = Matter.Vector.mult(normal, magnitude);
  487. // // to apply forces to both bodies
  488. // Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
  489. // Matter.Body.applyForce(bodyB, bodyB.position, force);
  490. // }
  491. ]
  492. }
  493. })
  494. // this.body = Matter.Body.create({
  495. // parts: this.body_parts,
  496. // item_type: 'concernement',
  497. // id: this.id,
  498. // frictionAir: 0,
  499. // // mass: Math.pow(3, this.entites.length),
  500. // // mass: 10,
  501. // mass: mass,
  502. // restitution: 0.06,
  503. // collisionFilter: {
  504. // group: -1
  505. // },
  506. // plugin: {
  507. // attractors: [
  508. // // // there is a built in helper function for Newtonian gravity!
  509. // // // you can find out how it works in index.js
  510. // MatterAttractors.Attractors.gravity
  511. // // function(bodyA, bodyB) {
  512. // // var force = {
  513. // // x: (bodyA.position.x - bodyB.position.x) * 1e-6,
  514. // // y: (bodyA.position.y - bodyB.position.y) * 1e-6
  515. // // }
  516. // // // apply force to both bodies
  517. // // Matter.Body.applyForce(bodyA, bodyA.position, force);
  518. // // Matter.Body.applyForce(bodyB, bodyB.position, Matter.Vector.neg(force));
  519. // // }
  520. // // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L192
  521. // // function (bodyA, bodyB){
  522. // // // use Newton's law of gravitation
  523. // // var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
  524. // // distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
  525. // // normal = Matter.Vector.normalise(bToA),
  526. // // magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
  527. // // force = Matter.Vector.mult(normal, magnitude);
  528. // // // to apply forces to both bodies
  529. // // Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
  530. // // Matter.Body.applyForce(bodyB, bodyB.position, force);
  531. // // }
  532. // ]
  533. // }
  534. // });
  535. Matter.Body.setPosition(this.body, this.pos);
  536. // add init velocity
  537. // INFO no need of init velocity as each mapitems are repeling the each others
  538. // this.setInitBodyVelocity()
  539. // console.log('concernementItem mass', this.body.mass);
  540. Matter.Composite.add(this.matterEngine.world, this.body);
  541. // console.log('concernement body', this.body);
  542. // listen for afterUpdate event from Matter.Engine object
  543. Matter.Events.on(this.matterEngine, "beforeUpdate", this.onBeforeEngineUpdate);
  544. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  545. }
  546. },
  547. setInitBodyVelocity(){
  548. let delta = 5;
  549. Matter.Body.setVelocity(this.body, {
  550. x: -delta + Math.random()*delta*2,
  551. y: -delta + Math.random()*delta*2
  552. });
  553. },
  554. updateMatterBodyRay(){
  555. console.log('updateMatterBodyRay',this.ray, this.body);
  556. let entite_range = [3, 100];
  557. let ray_range = [this.ray*0.8,this.ray*1.2];
  558. let new_ray = ray_range[0] + (this.entites.length - entite_range[0]) * (ray_range[1] - ray_range[0]) / (entite_range[1] - entite_range[0]);
  559. let old_ray = this.body.circleRadius;
  560. let scale = new_ray/old_ray;
  561. Matter.Body.scale(this.body, scale, scale);
  562. // Matter.Body.set(this.body, 'circleRadius', ray);
  563. console.log(`this.body circleRadius ${this.body.circleRadius}`);
  564. },
  565. // PAPER OBJECTS
  566. initPaperObjects(){
  567. // the main paper group containing all paper graphical items for one concernement
  568. this.paper_main_object = new paper.Group({
  569. pivot: new paper.Point(this.pos),
  570. name: `main_${this.id}`,
  571. cid: this.cid,
  572. // superposition_id: this.mapitem.superposition_ids[0]
  573. });
  574. // fadein intro
  575. // if(!this.mapitem.clone){
  576. this.paper_main_object.opacity = (1 + Math.random())*0.001;
  577. // }
  578. // the sub items for one concernement
  579. this.paper_main_object.addChild(this.setPaperContour());
  580. if (this.concernement.has_superpositions) {
  581. this.paper_main_object.addChild(this.setPaperEntitesSuperposees());
  582. }
  583. if (this.concernement.has_proximites) {
  584. this.paper_main_object.addChild(this.setPaperEntitesProximite());
  585. }
  586. // if (this.concernement.has_puissancedagir) {
  587. // this.addNewPaperSymbolInstance('puissanceagir_icon', false, 0.7);
  588. // }
  589. // if (this.concernement.has_agissantes) {
  590. // this.paper_main_object.addChild(this.setPaperAgissantesIcons());
  591. // }
  592. // if (this.concernement.has_doleance) {
  593. // this.addNewPaperSymbolInstance('doleance_icon', false, 0.7);
  594. // }
  595. this.initPaperEvents()
  596. },
  597. async updatePaperObjectSize(n,o){
  598. await nextTick();
  599. // INFO we redraw the points with the new ray AND sacle the backgrounds ... would it be simplier to only resize the whole mapitem ???
  600. // window has been resized
  601. // if open
  602. if (this.is_open) {
  603. // move to the new position
  604. // INFO we need to change position first for the contours to be redraw in the right place
  605. this.pos = this.paper_main_object.position = {
  606. x: (this.canvas.width - this.cartouch_width) / 2,
  607. y: this.canvas.height / 2
  608. };
  609. this.constraint.pointA = this.pos;
  610. Matter.Body.setPosition(this.body, this.pos);
  611. // reset the paper contents with paper symbols redrew by MapConcernements.vue
  612. this.setPaperContents();
  613. // scale
  614. // let s = this.getOpeningAvailableScale();
  615. // this.paper_main_object.scale(1 / this.scale);
  616. // this.paper_main_object.scale(s);
  617. }
  618. // in any case
  619. // delet the old contours
  620. let clearables = ['contours'];
  621. clearables.forEach(child_name => {
  622. if (this.paper_main_object.children[child_name]) {
  623. this.paper_main_object.children[child_name].remove();
  624. }
  625. });
  626. // rebuild the contours with the new ray
  627. this.parseEntityPointsValues()
  628. this.sailentEntites = this.concernement.sailentEntites = this.getJarvisEnvelopeConvexeEntites(this.entites)
  629. // redraw the contours
  630. this.paper_main_object.addChild(this.setPaperContour());
  631. },
  632. /*
  633. * called by openClose() function
  634. */
  635. setPaperContents(){
  636. // trigered once opening tween is complete
  637. // trigered once mapmode changed
  638. this.clearPaperContents();
  639. switch(this.map_mode){
  640. case 'terraindevie':
  641. this.addNewPaperSymbolInstance('boussole_bg', true);
  642. this.paper_main_object.addChild(this.setPaperEntites());
  643. break;
  644. case 'superposition':
  645. this.addNewPaperSymbolInstance('boussole_bg', true);
  646. break;
  647. case 'proximite':
  648. this.addNewPaperSymbolInstance('boussole_bg', true);
  649. this.paper_main_object.addChild(this.setPaperEntitesProximiteReferences());
  650. break;
  651. case 'puissancedagir':
  652. this.addNewPaperSymbolInstance('puissanceagir_bg', true);
  653. this.paper_main_object.addChild(this.setPaperPuissanceagirBesoins());
  654. break;
  655. case 'action':
  656. this.addNewPaperSymbolInstance('boussole_bg', true);
  657. this.paper_main_object.addChild(this.setPaperAgissantes());
  658. break;
  659. case 'doleancer':
  660. this.addNewPaperSymbolInstance('doleance_bg');
  661. this.paper_main_object.addChild(this.setPaperDoleances());
  662. break;
  663. }
  664. },
  665. resetPaperActiveRevision(){
  666. console.log('resetPaperActiveRevision');
  667. this.sailentEntites = this.getJarvisEnvelopeConvexeEntites(this.concernement.revisions_byid[this.concernement.active_revision].entites);
  668. // remove contours if already exists
  669. if (this.paper_main_object.children.contours) {
  670. this.paper_main_object.children.contours.remove();
  671. }
  672. // redraw contours
  673. this.paper_main_object.addChild(this.setPaperContour());
  674. // remove entites if already exists
  675. if (this.paper_main_object.children.entites) {
  676. this.paper_main_object.children.entites.remove();
  677. }
  678. // redraw entites
  679. this.paper_main_object.addChild(this.setPaperEntites());
  680. },
  681. clearPaperContents(){
  682. let clearable_children = ['boussole_bg', 'entites',
  683. 'entites_proximites_references',
  684. 'puissanceagir_bg','puissanceagir_besoins',
  685. 'agissantes',
  686. 'doleance_bg', 'doleances'];
  687. clearable_children.forEach(child_name => {
  688. if (this.paper_main_object.children[child_name]) {
  689. this.paper_main_object.children[child_name].remove();
  690. }
  691. });
  692. },
  693. addNewPaperSymbolInstance(name, back, scale){
  694. let instance = new paper.SymbolItem(this.paper_symbol_definitions[name]); // , {x:0,y:0}
  695. instance.name = name;
  696. instance.position = this.pos;
  697. let s = scale ? this.scale * scale : this.scale;
  698. instance.scale(s);
  699. // instance.locked = true;
  700. this.paper_main_object.addChild(instance);
  701. if (back) {
  702. this.paper_main_object.children[name].sendToBack();
  703. } else {
  704. this.paper_main_object.children[name].bringToFront();
  705. }
  706. },
  707. getPaddedRoundedSegments(points, scale){
  708. // console.log(`setPaperContour ${this.concernement.id}`);
  709. let getSegmentProps = (b,a,c,d) => {
  710. const ac = { x: c.x - a.x, y: c.y - a.y } // get ac vecteur
  711. const lac = Math.sqrt(Math.pow(ac.x, 2) + Math.pow(ac.y, 2)); // get ac longueur ac
  712. const ab = { x: b.x - a.x, y: b.y - a.y } // get ab vecteur
  713. const lab = Math.sqrt(Math.pow(ab.x, 2) + Math.pow(ab.y, 2)); // get ab longeur
  714. const vab = { x: ab.x/lab, y: ab.y/lab } // get unit vecteur ab
  715. const an = { x: vab.x*lac, y: vab.y*lac } // get an vecteur
  716. const n = { x: a.x + an.x, y: a.y+an.y } // get n point
  717. const m = { x: (c.x + n.x)/2, y: (c.y + n.y)/2 } // get nc midle point
  718. const ma = { x:a.x - m.x, y: a.y - m.y } // get ma vecteur
  719. const lma = Math.sqrt(Math.pow(ma.x, 2)+Math.pow(ma.y, 2)) // get longeur m->a
  720. const vma = { x: ma.x/lma, y: ma.y/lma } // get ma vecteur unitaire
  721. // console.log(`vma x:${vma.x}, y:${vma.y}`);
  722. const pad = 4; // exterior padding
  723. // the final padded point
  724. const pa = [
  725. a.x+vma.x*pad,
  726. a.y+vma.y*pad
  727. ]
  728. // handles
  729. const delta = 0.05 * scale;
  730. // handle IN
  731. const hli = Math.abs(lab)*delta; // handle longeur
  732. const vnai = { x: -vma.y, y: vma.x } // get the ma normal unit vector IN
  733. const hai = [ vnai.x*hli, vnai.y*hli ]; // get the handleIn point
  734. // handle OUT
  735. const hlo = Math.abs(lac)*delta; // handle longeur
  736. const vnao = { x: vma.y, y: -vma.x } // get the ma normal vector Out
  737. const hao = [ vnao.x*hlo, vnao.y*hlo ]; // get the handleOut point
  738. return {
  739. point: pa,
  740. handleIn: hai,
  741. handleOut: hao
  742. }
  743. }
  744. const first_point = getSegmentProps(
  745. points[points.length-1],
  746. points[0],
  747. points[1]
  748. );
  749. let segments = [first_point];
  750. for (let j = 1; j < points.length-1; j++) {
  751. segments.push(getSegmentProps(
  752. points[j-1],
  753. points[j],
  754. points[j+1]
  755. ))
  756. }
  757. const last_point = getSegmentProps(
  758. points[points.length-2],
  759. points[points.length-1],
  760. points[0]
  761. );
  762. segments.push(last_point)
  763. segments.push(first_point)
  764. return segments;
  765. },
  766. setPaperContour(){
  767. // console.log('setPaperContour, this.sailentEntites', this.sailentEntites);
  768. // convert sailent entites to x,y points
  769. let points = [];
  770. this.sailentEntites.forEach(entite => {
  771. points.push({
  772. x: (entite.display.ray) * Math.cos(entite.display.alpha * (Math.PI/180)),
  773. y: (entite.display.ray) * Math.sin(entite.display.alpha * (Math.PI/180))
  774. })
  775. })
  776. // convert points to rouded and padded segments props
  777. let segments = this.getPaddedRoundedSegments(points, this.scale)
  778. // create "real" Paper Segments from previous segments props
  779. let paper_segments = [];
  780. segments.forEach(seg => {
  781. paper_segments.push(new paper.Segment({
  782. point: [this.pos.x+seg.point[0]*this.scale, this.pos.y+seg.point[1]*this.scale],
  783. handleIn: seg.handleIn,
  784. handleout: seg.handleOut
  785. }))
  786. });
  787. // create the paper path with previous segments
  788. const contrs = new paper.Path({
  789. name: 'contours',
  790. segments: paper_segments,
  791. fillColor: 'rgba(255,255,255,0.4)',
  792. // selected: true,
  793. strokeColor: this.mapitem.clone ? '#f00' : '#fff',
  794. strokeWidth: 1,
  795. pivot: new paper.Point(this.pos),
  796. cid: this.cid
  797. });
  798. // return the paper path
  799. return contrs;
  800. },
  801. setPaperEntites(){
  802. let g = new paper.Group({
  803. pivot: new paper.Point(this.pos),
  804. name: 'entites'
  805. });
  806. // for (let i = 0; i < this.entites.length; i++) {
  807. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  808. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  809. // item type
  810. let item_type = entite.entite ? entite.entite.agissante ? 'entite_action' : 'entite' : 'entite_hidden';
  811. // use paper symbol
  812. let symbol_name = item_type;
  813. switch (item_type) {
  814. case 'entite':
  815. if(parseInt(this.opened_entite_id) === entite.entite.id){
  816. symbol_name = 'entite_hover';
  817. }
  818. break;
  819. case 'entite_action':
  820. if(parseInt(this.opened_entite_id) === entite.entite.id){
  821. symbol_name = 'entite_action_hover';
  822. }
  823. break;
  824. }
  825. // console.log(`item_type: ${item_type}, symbol_name: ${symbol_name}`);
  826. let instance = new paper.SymbolItem(this.paper_symbol_definitions[symbol_name]);
  827. instance.name = 'entite';
  828. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  829. instance.scale(this.scale);
  830. instance.fillColor = '#000';
  831. instance.item_id = entite.entite ? entite.entite.id : null;
  832. instance.item_type = item_type;
  833. instance.is_symbol_instance = true;
  834. g.addChild(instance)
  835. }
  836. return g;
  837. },
  838. setPaperEntitesSuperposees(){
  839. // console.log('setPaperSuperpositions, superpositions', this.concernement.superpositions);
  840. let g = new paper.Group({
  841. pivot: new paper.Point(this.pos),
  842. name: 'entites_superposes'
  843. });
  844. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  845. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  846. if (entite.entite // check if we have an entite object with all the contents
  847. && this.superposedEntiteID === entite.entite.id) // check if entite id
  848. {
  849. // console.log(`entite ${entite.entite.id}`, entite, entite.entite.superposition);
  850. // use paper symbol
  851. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  852. instance.name = 'entite';
  853. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  854. // instance.scale(this.scale);
  855. instance.scale(3);
  856. instance.fillColor = '#000';
  857. instance.item_id = entite.entite.id;
  858. instance.item_type = 'entite_superpose';
  859. instance.is_symbol_instance = true;
  860. g.addChild(instance)
  861. break; // there is only one superposed entite by mapitem
  862. }
  863. }
  864. return g;
  865. },
  866. setPaperEntitesProximite(){
  867. let g = new paper.Group({
  868. pivot: new paper.Point(this.pos),
  869. name: 'entites_proximites'
  870. });
  871. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  872. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  873. if (entite.entite // check if we have an entite object with all the contents
  874. && entite.entite.proximite.length ) // check if entite id is in the list builded above
  875. {
  876. // console.log(`entite ${entite.entite.id}`, entite, entite.entite.superposition);
  877. // use paper symbol
  878. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  879. instance.name = 'entite';
  880. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  881. // instance.scale(this.scale);
  882. instance.scale(3);
  883. instance.fillColor = '#000';
  884. instance.item_id = entite.entite.id;
  885. instance.item_type = 'entite_proximite';
  886. instance.is_symbol_instance = true;
  887. g.addChild(instance)
  888. }
  889. }
  890. return g;
  891. },
  892. setPaperEntitesProximiteReferences(){
  893. // console.log('_ _ _ _ setPaperEntitesProximiteReferences')
  894. let g = new paper.Group({
  895. pivot: new paper.Point(this.pos),
  896. name: 'entites_proximites_references'
  897. });
  898. // loop through all concernement's entites
  899. // keeping only those who have proximite
  900. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  901. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  902. if (entite.entite // check if we have an entite object with all the contents
  903. && entite.entite.proximite.length ) // check if entite id is in the list builded above
  904. {
  905. // console.log(`PROXIMITE entite ${entite.entite.id}`, entite, entite.entite.proximite);
  906. // create the main entite paper point object
  907. let entite_pos = {
  908. x: this.pos.x + entite.display.pos.x * this.scale,
  909. y: this.pos.y + entite.display.pos.y * this.scale
  910. }
  911. // console.log(`entite_pos x:${entite_pos.x}, y:${entite_pos.y}`);
  912. // use paper symbol
  913. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  914. instance.name = 'entite';
  915. instance.position = new paper.Point([entite_pos.x, entite_pos.y]);
  916. // instance.scale(this.scale);
  917. instance.scale(this.scale);
  918. instance.fillColor = '#000';
  919. instance.item_id = entite.entite.id;
  920. instance.item_type = 'entite_proximite';
  921. instance.is_symbol_instance = true;
  922. g.addChild(instance)
  923. // create the proximite reference of the main entite
  924. // a paper group wich contains the ref entite point and the ref concernement's contour
  925. let ref_g = new paper.Group({
  926. pivot: new paper.Point(this.pos),
  927. name: 'ref_entite_proximite'
  928. });
  929. let allEntiteRefConcernementContourEntites = [];
  930. let beta = 360 / entite.entite.proximite.length;
  931. let ray = 5;
  932. let e=0;
  933. // define the right scaling down of the big original concernement contours
  934. let scaledown = 0.05;
  935. entite.entite.proximite.forEach(entite_ref => {
  936. // console.log(`${entite_ref.id}, ${entite_ref.title}`, this.allEntitesById[entite_ref.id]);
  937. // get the concernement parents of the proximite reference
  938. let ref_cid = this.allEntitesById[entite_ref.id].cid;
  939. // console.log('PROXIMITE ref concernement jarvis_envelope_convexe', this.concernementsByID[ref_cid].sailentEntites);
  940. let ref_concernement = this.concernementsByID[ref_cid];
  941. // create the entite ref paper point
  942. let ref_instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  943. ref_instance.name = 'entite_ref';
  944. let entite_ref_pos = {
  945. x: entite_pos.x + ray * Math.cos((beta*e) * (Math.PI/180)) * this.scale, // met a l'echelle la position du point de l'entite en proximite
  946. y: entite_pos.y + ray * Math.sin((beta*e) * (Math.PI/180)) * this.scale, // met a l'echelle la position du point de l'entite en proximite
  947. }
  948. // console.log(`entite_ref_pos x:${entite_ref_pos.x}, y:${entite_ref_pos.y}`);
  949. ref_instance.position = new paper.Point(entite_ref_pos);
  950. // ref_instance.scale(this.scale);
  951. ref_instance.scale(this.scale*0.75); // met a l'échelle la taille du point et la diminue
  952. ref_instance.fillColor = '#000';
  953. ref_instance.item_id = entite_ref.id;
  954. ref_instance.item_type = 'entite_proximite_reference';
  955. ref_instance.ref_cid = ref_cid;
  956. ref_instance.parent_eid = entite.entite.id;
  957. ref_instance.is_symbol_instance = true;
  958. ref_g.addChild(ref_instance)
  959. // create the original concernement contours arround the entite_ref
  960. let points = [];
  961. ref_concernement.sailentEntites.forEach(ent => {
  962. points.push({
  963. x: (ent.display.ray) * Math.cos(ent.display.alpha * (Math.PI/180)) * this.scale, // met a l'echelle la position du point par rapport a l'entite e proximite
  964. y: (ent.display.ray) * Math.sin(ent.display.alpha * (Math.PI/180)) * this.scale // met a l'echelle la position du point par rapport a l'entite e proximite
  965. })
  966. // - - //
  967. // concat all the contours points for general contours arround all contours :/ :( (that's a very clear description)
  968. let p = {
  969. x: entite_ref_pos.x - entite_pos.x + (ent.display.ray * Math.cos(ent.display.alpha * (Math.PI/180))) * this.scale,
  970. y: entite_ref_pos.y - entite_pos.y + (ent.display.ray * Math.sin(ent.display.alpha * (Math.PI/180))) * this.scale
  971. }
  972. // console.log(`p x:${p.x}, y:${p.y}`);
  973. allEntiteRefConcernementContourEntites.push({
  974. display:{
  975. ray: Math.sqrt(Math.pow(p.x,2)+Math.pow(p.y,2)),
  976. alpha: parseFloat(Math.atan2(p.y,p.x) * (180/Math.PI)).toFixed(2)
  977. }
  978. })
  979. // - - //
  980. })
  981. // convert points to rouded and padded segments props
  982. let segments = this.getPaddedRoundedSegments(points, scaledown)
  983. // create "real" Paper Segments from previous segments props
  984. let paper_segments = [];
  985. segments.forEach(seg => {
  986. let paddedSegPoint = {
  987. x: entite_ref_pos.x+seg.point[0]*scaledown,
  988. y: entite_ref_pos.y+seg.point[1]*scaledown
  989. }
  990. console.log('paddedSegPoint', paddedSegPoint);
  991. paper_segments.push(new paper.Segment({
  992. point: paddedSegPoint,
  993. handleIn: seg.handleIn,
  994. handleout: seg.handleOut
  995. }))
  996. // - - //
  997. // concat all the contours points for general contours arround all contours :/ :( (that's a very clear description)
  998. // let p = {
  999. // x: (entite_ref_pos.x - entite_pos.x + (ent.display.ray * Math.cos(ent.display.alpha * (Math.PI/180)))),
  1000. // y: (entite_ref_pos.y - entite_pos.y + (ent.display.ray * Math.sin(ent.display.alpha * (Math.PI/180))))
  1001. // }
  1002. // console.log(`p x:${p.x}, y:${p.y}`);
  1003. // allEntiteRefConcernementContourEntites.push({
  1004. // display:{
  1005. // ray: Math.sqrt(Math.pow(paddedSegPoint.x,2)+Math.pow(paddedSegPoint.y,2)),
  1006. // alpha: parseFloat(Math.atan2(paddedSegPoint.y,paddedSegPoint.x) * (180/Math.PI)).toFixed(2)
  1007. // }
  1008. // })
  1009. // - - //
  1010. });
  1011. // create the paper path with previous segments
  1012. const contrs = new paper.Path({
  1013. name: 'contours',
  1014. segments: paper_segments,
  1015. fillColor: 'rgba(255,255,255,0.4)',
  1016. // selected: true,
  1017. strokeColor: '#fff',
  1018. strokeWidth: 1,
  1019. pivot: new paper.Point(this.pos),
  1020. cid: ref_concernement.id
  1021. });
  1022. ref_g.addChild(contrs);
  1023. contrs.sendToBack();
  1024. ref_instance.contour_paper_item_id = contrs.id;
  1025. e++;
  1026. }); // end of loop on proximite for each main entite
  1027. // general contour arround proximité
  1028. // console.log('allEntiteRefConcernementContourEntites', allEntiteRefConcernementContourEntites);
  1029. let genContoursEntite = this.getJarvisEnvelopeConvexeEntites(allEntiteRefConcernementContourEntites);
  1030. // console.log('genContoursEntite', genContoursEntite);
  1031. let points = [];
  1032. genContoursEntite.forEach(ent => {
  1033. points.push({
  1034. x: ent.display.ray * Math.cos(ent.display.alpha * (Math.PI/180)),
  1035. y: ent.display.ray * Math.sin(ent.display.alpha * (Math.PI/180))
  1036. })
  1037. })
  1038. console.log('points', points);
  1039. let gen_scaledown = 0.16;
  1040. // scaledown = 1;
  1041. // convert points to rouded and padded segments props
  1042. let segments = this.getPaddedRoundedSegments(points, gen_scaledown)
  1043. // create "real" Paper Segments from previous segments props
  1044. let paper_segments = [];
  1045. segments.forEach(seg => {
  1046. paper_segments.push(new paper.Segment({
  1047. point: [entite_pos.x+seg.point[0]*gen_scaledown, entite_pos.y+seg.point[1]*gen_scaledown],
  1048. // point: [seg.point[0], seg.point[1]],
  1049. handleIn: seg.handleIn,
  1050. handleout: seg.handleOut
  1051. }))
  1052. });
  1053. // create the paper path with previous segments
  1054. const genContrs = new paper.Path({
  1055. name: 'gen_contours',
  1056. segments: paper_segments,
  1057. fillColor: 'rgba(255,255,255,0.4)',
  1058. // selected: true,
  1059. strokeColor: '#fff',
  1060. strokeWidth: 1,
  1061. pivot: new paper.Point(this.pos),
  1062. // cid: this.cid
  1063. });
  1064. ref_g.addChild(genContrs);
  1065. genContrs.sendToBack();
  1066. g.addChild(ref_g);
  1067. ref_g.sendToBack();
  1068. } // end of loop on entite wich have proximite
  1069. }
  1070. return g;
  1071. },
  1072. setPaperAgissantes(){
  1073. console.log('setPaperAgissantes');
  1074. let g = new paper.Group({
  1075. pivot: new paper.Point(this.pos),
  1076. name: 'agissantes'
  1077. });
  1078. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  1079. let entite = this.concernement.revisions_byid[this.concernement.revision_id].entites[i];
  1080. console.log('agissantes', entite);
  1081. if (entite.entite && entite.entite.agissante) {
  1082. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action']);
  1083. instance.name = 'entite_action';
  1084. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  1085. instance.fillColor = '#000';
  1086. // instance.scale(0.2);
  1087. instance.scale(this.scale);
  1088. instance.item_id = entite.entite.id;
  1089. instance.item_type = 'entite_action';
  1090. instance.is_symbol_instance = true;
  1091. g.addChild(instance)
  1092. }
  1093. }
  1094. return g;
  1095. },
  1096. // setPaperAgissantesIcons(){
  1097. // let g = new paper.Group({
  1098. // pivot: new paper.Point(this.pos),
  1099. // name: 'agissantes_icons'
  1100. // });
  1101. // for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  1102. // let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  1103. // if (entite.entite && entite.entite.agissante) {
  1104. // let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action_icon']);
  1105. // instance.name = 'entite_action';
  1106. // instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  1107. // instance.fillColor = '#000';
  1108. // // instance.scale(0.2);
  1109. // instance.scale(this.scale);
  1110. // instance.item_id = entite.entite.id;
  1111. // instance.item_type = 'entite_action';
  1112. // instance.is_symbol_instance = true;
  1113. // g.addChild(instance)
  1114. // }
  1115. // }
  1116. // return g;
  1117. // },
  1118. setPaperPuissanceagirBesoins(){
  1119. let g = new paper.Group({
  1120. pivot: new paper.Point(this.pos),
  1121. name: 'puissanceagir_besoins'
  1122. });
  1123. let res_fields = ['qui','quoi','ou','avec'];
  1124. let arc = (360 / 16); // unit arc
  1125. let r = (this.ray * this.scale)/5; // unit ray
  1126. let br = r - r/3; // besoin ray
  1127. for (let i = 0; i < this.concernement.besoins.length; i++) {
  1128. let start_a = arc * i; // angle depart (for reponses)
  1129. let center_a = start_a + arc/2; // angle central
  1130. let x = Math.cos(center_a*(Math.PI/180)) * br;
  1131. let y = Math.sin(center_a*(Math.PI/180)) * br;
  1132. // use paper symbol
  1133. let besoin = new paper.SymbolItem(this.paper_symbol_definitions['besoin']);
  1134. besoin.position = new paper.Point([this.pos.x + x, this.pos.y + y]);
  1135. besoin.scale(this.scale);
  1136. besoin.item_id = this.concernement.besoins[i].id;
  1137. besoin.item_cid = this.concernement.id;
  1138. besoin.item_type = 'besoin';
  1139. besoin.is_symbol_instance = true;
  1140. console.log(`paper besoin id:${besoin.id}`);
  1141. this.setBesoinPaperId(besoin.id, this.cid, this.concernement.besoins[i].id);
  1142. g.addChild(besoin)
  1143. let res_arc = arc / (1 + this.concernement.besoins[i].reponses.length); // unit arc for responses depending responses number
  1144. for (let j = 0; j < this.concernement.besoins[i].reponses.length; j++) {
  1145. let res_a = start_a + res_arc * (j+1); // angle for response line
  1146. for (let f = 0; f < res_fields.length; f++) { // loop through all 4 fields, keep only the last one filled
  1147. if( this.concernement.besoins[i].reponses[j][res_fields[f]] // if field filled
  1148. && (f === res_fields.length -1 || !this.concernement.besoins[i].reponses[j][res_fields[f+1]]) // and is last field or last field filled
  1149. ){
  1150. let rr = this.ray * this.scale - r*f - r/2; // reponse field ray
  1151. let rx = Math.cos(res_a*(Math.PI/180)) * rr;
  1152. let ry = Math.sin(res_a*(Math.PI/180)) * rr;
  1153. // use paper symbol
  1154. let reponse = new paper.SymbolItem(this.paper_symbol_definitions['reponse']);
  1155. reponse.position = new paper.Point([this.pos.x + rx, this.pos.y + ry]);
  1156. reponse.scale(this.scale);
  1157. reponse.item_id = this.concernement.besoins[i].reponses[j].id;
  1158. reponse.item_bid = this.concernement.besoins[i].id;
  1159. reponse.item_cid = this.concernement.id;
  1160. reponse.item_type = 'reponse';
  1161. reponse.is_symbol_instance = true;
  1162. this.setBesoinPaperId(reponse.id, this.cid, this.concernement.besoins[i].id, this.concernement.besoins[i].reponses[j].id);
  1163. g.addChild(reponse)
  1164. }
  1165. }
  1166. }
  1167. }
  1168. return g;
  1169. },
  1170. setPaperDoleances(){
  1171. let g = new paper.Group({
  1172. pivot: new paper.Point({x:0,y:0}),
  1173. name: `doleances`
  1174. });
  1175. this.concernement.doleances.forEach((d) => {
  1176. g.addChild(this.setPaperDoleanceSteps(d))
  1177. });
  1178. return g;
  1179. },
  1180. setPaperDoleanceSteps(doleance){
  1181. let g = new paper.Group({
  1182. pivot: new paper.Point({x:0,y:0}),
  1183. name: `doleance_${doleance.id}`,
  1184. doleance_id: doleance.id,
  1185. visible: false
  1186. });
  1187. // let doleance = this.concernement.doleances[0];
  1188. let all_fields = [
  1189. [
  1190. 'leprobleme',
  1191. 'lenquete',
  1192. {
  1193. fieldname: 'groupesinterets',
  1194. fields: [
  1195. 'groupe_interets',
  1196. 'accorder_interets',
  1197. 'formuler',
  1198. ]
  1199. }
  1200. ],
  1201. [
  1202. 'entite_addresse_doleance',
  1203. 'comment_ennonce_doleance',
  1204. 'aqui_addresse_doleance',
  1205. {
  1206. fieldname: 'reception_traitement',
  1207. fields: [
  1208. 'entite_adressee',
  1209. 'doleance_formulee',
  1210. 'traite_doleance',
  1211. 'entite_recoit_doleance',
  1212. ]
  1213. }
  1214. ],
  1215. [
  1216. 'entites_decisionnaires',
  1217. 'decision_formule',
  1218. {
  1219. fieldname: 'mise_en_oeuvre_decision',
  1220. fields: [
  1221. 'entite_adresse_decision',
  1222. 'formule_decision',
  1223. 'entite_metenoeuvre_decisio',
  1224. ]
  1225. }
  1226. ],
  1227. [
  1228. 'entite_adresse_application',
  1229. 'aqui_adresse_decision',
  1230. 'comment_formule_decision',
  1231. {
  1232. fieldname: 'receptions_et_applications',
  1233. fields: [
  1234. 'applique_decision',
  1235. 'formule_decision_applic',
  1236. 'entite_recoit_decision',
  1237. ]
  1238. },
  1239. 'probleme_initial_resolu',
  1240. 'oui_nouvelle_situation',
  1241. 'non_adresse_doleance',
  1242. ]
  1243. ];
  1244. var r = this.ray * this.scale * 0.8; // ray
  1245. var dr = r/2; // demi ray
  1246. // var pcr = 2*this.scale; // petits cercle rayon
  1247. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  1248. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  1249. // radians = degrees * (pi/180)
  1250. // degrees = radians * (180/pi)
  1251. // Points for 45° axes
  1252. let m = Math.sin(45*(Math.PI/180)) * r; // x = y for rayon
  1253. let n = Math.sin(45*(Math.PI/180)) * r/2; // x = y for demi rayon
  1254. let o = Math.cos(22.5*(Math.PI/180)) * r; // x @ 22.5° for rayon
  1255. let p = Math.sin(22.5*(Math.PI/180)) * r; // y @ 22.5° for rayon
  1256. let o_d = Math.cos(22.5*(Math.PI/180)) * r/2; // x @ 22.5° for demi rayon
  1257. let p_d = Math.sin(22.5*(Math.PI/180)) * r/2; // y @ 22.5° for demi rayon
  1258. let step_Style = {
  1259. strokeColor: '#fff',
  1260. strokeWidth: 2,
  1261. fillColor: "rgba(255, 255, 255, 0.3)"
  1262. }
  1263. // let fontsize = 4;
  1264. //
  1265. // CAMENBERT STATIQUES
  1266. //
  1267. // l'enquete
  1268. //
  1269. // camenbert
  1270. if (doleance.lenquete) {
  1271. let cam = new paper.Path({...step_Style, ...{
  1272. item_type: 'doleance_step',
  1273. item_id: `lenquete`,
  1274. did: doleance.id,
  1275. field: 'lenquete',
  1276. field_index: null
  1277. }
  1278. });
  1279. cam.add({x: this.pos.x , y: this.pos.y + dr});
  1280. cam.lineTo({x: this.pos.x, y: this.pos.y + r});
  1281. cam.arcTo({x: this.pos.x - p, y: this.pos.y + o}, {x: this.pos.x - m, y: this.pos.y + m});
  1282. cam.lineTo({x: this.pos.x - n, y: this.pos.y + n});
  1283. cam.arcTo({x: this.pos.x - p_d, y: this.pos.y + o_d}, {x: this.pos.x , y: this.pos.y + dr});
  1284. // texte
  1285. // enquete.addChild(new paper.PointText({
  1286. // point: {
  1287. // x:this.pos.x + Math.sin(22.5*(Math.PI/180)) * r*0.75,
  1288. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  1289. // },
  1290. // content: doleance['lenquete'],
  1291. // fontSize: fontsize,
  1292. // fillColor: '#fff',
  1293. // justification: 'center',
  1294. // locked: true
  1295. // }));
  1296. g.addChild(cam);
  1297. }
  1298. //
  1299. // probleme_initial_resolu
  1300. //
  1301. // camenbert
  1302. if(doleance.probleme_initial_resolu === 0 || doleance.probleme_initial_resolu === 1){
  1303. let rescam = new paper.Path({...step_Style, ...{
  1304. closed: true,
  1305. item_type: 'doleance_step',
  1306. item_id: `probleme_initial_resolu`,
  1307. did: doleance.id,
  1308. field: 'probleme_initial_resolu',
  1309. field_index: null
  1310. }
  1311. });
  1312. rescam.add({x: this.pos.x + n, y: this.pos.y + n});
  1313. rescam.lineTo({x: this.pos.x + m , y: this.pos.y + m});
  1314. rescam.arcTo({x: this.pos.x + p, y: this.pos.y + o}, {x: this.pos.x, y: this.pos.y + r});
  1315. rescam.lineTo({x: this.pos.x, y: this.pos.y + dr});
  1316. rescam.arcTo({x: this.pos.x + p_d, y: this.pos.y + o_d}, {x: this.pos.x + n, y: this.pos.y + n});
  1317. // texte
  1318. // resolution.addChild(new paper.PointText({
  1319. // point: {
  1320. // x:this.pos.x - Math.sin(22.5*(Math.PI/180)) * r*0.75,
  1321. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  1322. // },
  1323. // content: doleance['probleme_initial_resolu'] ? doleance['oui_nouvelle_situation'] : doleance['non_adresse_doleance'],
  1324. // fontSize: fontsize,
  1325. // fillColor: '#fff',
  1326. // justification: 'center'
  1327. // }));
  1328. g.addChild(rescam)
  1329. }
  1330. //
  1331. // MULTIPLE FIELDS
  1332. //
  1333. let multiple_fields = [
  1334. { field_name: 'groupesinterets', arc: 45, decalage: -45 },
  1335. { field_name: 'reception_traitement', arc: 90, decalage: -90 },
  1336. { field_name: 'mise_en_oeuvre_decision', arc: 90, decalage: -180 },
  1337. { field_name: 'receptions_et_applications', arc: 45, decalage: 90 }
  1338. ]
  1339. multiple_fields.forEach((mf, j) => {
  1340. for (let i = 0, l = doleance[mf.field_name].length, a = mf.arc/l; i < l; i++) {
  1341. // let gi = new paper.Group({
  1342. // item_type: 'doleance_step',
  1343. // item_id: 'lenquete',
  1344. // item_field: mf.field_name,
  1345. // item_index: i
  1346. // });
  1347. //camenbert
  1348. let x1= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * dr,
  1349. y1= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * dr;
  1350. let x2= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * r,
  1351. y2= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * r;
  1352. let x3= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * r,
  1353. y3= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * r;
  1354. let x3t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r,
  1355. y3t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r;
  1356. let x4= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * dr,
  1357. y4= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * dr;
  1358. let x4t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr,
  1359. y4t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr;
  1360. let x5= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75,
  1361. y5= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75;
  1362. let p = new paper.Path({...step_Style, ...{
  1363. item_type: 'doleance_step',
  1364. item_id: `${mf.field_name}-${i}`,
  1365. did: doleance.id,
  1366. field: mf.field_name,
  1367. field_index: i
  1368. }
  1369. });
  1370. p.add([x1,y1]);
  1371. p.lineTo([x2,y2]);
  1372. p.arcTo([x3t,y3t], [x3,y3]);
  1373. p.lineTo([x4,y4]);
  1374. p.arcTo([x4t,y4t], [x1,y1]);
  1375. g.addChild(p);
  1376. // gi.addChild(p);
  1377. // // text
  1378. // gi.addChild(new paper.PointText({
  1379. // point: {x:x5,y:y5},
  1380. // content: `${j}-${i}`,
  1381. // fontSize: fontsize,
  1382. // fillColor: '#fff',
  1383. // justification: 'center'
  1384. // }))
  1385. // g.addChild(gi)
  1386. }
  1387. });
  1388. // POINTS CARDINAUX
  1389. let cardinal_Style = {
  1390. strokeColor: '#fff',
  1391. strokeWidth: 2,
  1392. fillColor: "rgba(255, 255, 255, 0.9)"
  1393. }
  1394. // leprobleme
  1395. if (doleance.leprobleme) {
  1396. g.addChild(new paper.Path.Circle({
  1397. center: [this.pos.x, this.pos.y + r],
  1398. radius: 3 * this.scale,
  1399. style: cardinal_Style,
  1400. item_type: 'doleance_step',
  1401. item_id: `leprobleme`,
  1402. did: doleance.id,
  1403. field: 'leprobleme'
  1404. }));
  1405. }
  1406. // adresse de la doleance
  1407. if (doleance.entite_addresse_doleance || doleance.aqui_addresse_doleance || doleance.comment_ennonce_doleance) {
  1408. g.addChild(new paper.Path.Circle({
  1409. center: [this.pos.x - r, this.pos.y],
  1410. radius: 3 * this.scale,
  1411. style: cardinal_Style,
  1412. item_type: 'doleance_step',
  1413. item_id: `adresse_de_la_doleance`,
  1414. did: doleance.id,
  1415. field: 'adresse_de_la_doleance'
  1416. }));
  1417. }
  1418. // decision
  1419. if (doleance.entites_decisionnaires || doleance.decision_formule) {
  1420. g.addChild(new paper.Path.Circle({
  1421. center: [this.pos.x, this.pos.y - r],
  1422. radius: 3 * this.scale,
  1423. style: cardinal_Style,
  1424. item_type: 'doleance_step',
  1425. item_id: `decision`,
  1426. did: doleance.id,
  1427. field: 'decision'
  1428. }));
  1429. }
  1430. // adresse_de_la_decision
  1431. if (doleance.entite_adresse_application || doleance.aqui_adresse_decision || doleance.comment_formule_decision) {
  1432. g.addChild(new paper.Path.Circle({
  1433. center: [this.pos.x + r, this.pos.y],
  1434. radius: 3 * this.scale,
  1435. style: cardinal_Style,
  1436. item_type: 'doleance_step',
  1437. item_id: `adresse_de_la_decision`,
  1438. did: doleance.id,
  1439. field: 'adresse_de_la_decision'
  1440. }));
  1441. }
  1442. return g;
  1443. },
  1444. // mapitem visibility
  1445. handleMapitemVisibility(state){
  1446. this.is_visible = state;
  1447. this.paper_main_object.visible = this.is_visible;
  1448. },
  1449. // PAPER VISIBILITY
  1450. handlePaperVisibilityOnBeforeOpen(){
  1451. // // agissantes
  1452. // if (this.concernement.has_agissantes && this.map_mode === "action") {
  1453. // if (!this.is_open) {
  1454. // this.paper_main_object.children.agissantes_icons.visible = true;
  1455. // } else {
  1456. // this.paper_main_object.children.agissantes_icons.visible = false;
  1457. // }
  1458. // }
  1459. // superposition
  1460. // scale down superposed entites on open
  1461. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1462. if(entites_superposes){
  1463. for(let paper_item of entites_superposes.children) {
  1464. paper_item.scale(0.25)
  1465. }
  1466. }
  1467. // proximite
  1468. // scale down proximite entites on open
  1469. let entites_proximites = this.paper_main_object.children['entites_proximites'];
  1470. if(entites_proximites){
  1471. for(let paper_item of entites_proximites.children) {
  1472. paper_item.scale(0.25)
  1473. }
  1474. }
  1475. },
  1476. handlePaperVisibilityOnOpened(){},
  1477. handlePaperVisibilityOnBeforeClose(){
  1478. // superposition
  1479. // scale up superposed entites on open
  1480. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1481. if(entites_superposes){
  1482. for(let paper_item of entites_superposes.children) {
  1483. paper_item.scale(4)
  1484. }
  1485. }
  1486. // proximites
  1487. // scale up proximites entites on open
  1488. let entites_proximites = this.paper_main_object.children['entites_proximites'];
  1489. if(entites_proximites){
  1490. for(let paper_item of entites_proximites.children) {
  1491. paper_item.scale(4)
  1492. }
  1493. }
  1494. },
  1495. handlePaperVisibilityOnClosed(){
  1496. // // agissantes
  1497. // if (this.concernement.has_agissantes && this.map_mode === "action") {
  1498. // if (!this.is_open) {
  1499. // this.paper_main_object.children.agissantes_icons.visible = true;
  1500. // } else {
  1501. // this.paper_main_object.children.agissantes_icons.visible = false;
  1502. // }
  1503. // }
  1504. },
  1505. handlePaperVisibilityOnAfterEnginUpdate(){
  1506. // contours focused
  1507. if (!this.isFocused()){
  1508. this.paper_main_object.children['contours'].fillColor = "rgba(255,255,255,0.1)"; // this.mapitem.clone ? "rgba(255,0,0,0.1)" : "rgba(255,255,255,0.1)"; //
  1509. }else{
  1510. this.paper_main_object.children['contours'].fillColor = "rgba(255,255,255,0.4)"; // this.mapitem.clone ? "rgba(255,0,0,0.4)" : "rgba(255,255,255,0.4)"; //
  1511. if (this.is_hover) {
  1512. this.paper_main_object.children['contours'].strokeColor = "#01ffe2";
  1513. this.paper_main_object.children['contours'].strokeWidth = 2;
  1514. }else{
  1515. this.paper_main_object.children['contours'].strokeColor = "#fff";
  1516. this.paper_main_object.children['contours'].strokeWidth = 1;
  1517. }
  1518. }
  1519. // contours visibility
  1520. if (!this.is_open
  1521. || (this.is_open && this.map_mode !== "puissancedagir" && this.map_mode !== "doleancer")) {
  1522. this.paper_main_object.children['contours'].visible = true;
  1523. } else {
  1524. this.paper_main_object.children['contours'].visible = false;
  1525. }
  1526. // proximite
  1527. if (this.concernement.has_proximites) {
  1528. if (this.map_mode === "proximite") {
  1529. this.paper_main_object.children.entites_proximites.visible = true;
  1530. }else{
  1531. this.paper_main_object.children.entites_proximites.visible = false;
  1532. }
  1533. }
  1534. // superposition
  1535. if (this.concernement.has_superpositions) {
  1536. if (this.map_mode === "superposition") {
  1537. this.paper_main_object.children.entites_superposes.visible = true;
  1538. // highlight of entite part of opened superposition
  1539. let group = this.paper_main_object.children['entites_superposes'];
  1540. if(group){
  1541. if (this.$route.query.superposition_id) {
  1542. let superposition_id = this.$route.query.superposition_id;
  1543. if(superposition_id){
  1544. // let ids = superposition_id.match(/(\d+)_(\d+)__(\d+)_(\d+)/i)
  1545. // group.children.forEach((item) => {
  1546. // if ( (this.cid === parseInt(ids[1]) && item.item_id === parseInt(ids[2]))
  1547. // || (this.cid === parseInt(ids[3]) && item.item_id === parseInt(ids[4])) ) {
  1548. // item.definition = this.paper_symbol_definitions.entite_hover;
  1549. // if(!item.focused){
  1550. // item.scale(2)
  1551. // item.focused = true;
  1552. // }
  1553. // } else if(!this.is_hover) {
  1554. // item.definition = this.paper_symbol_definitions.entite;
  1555. // if (item.focused) {
  1556. // item.scale(0.5)
  1557. // item.focused = false;
  1558. // }
  1559. // }
  1560. // })
  1561. }
  1562. }else{
  1563. group.children.forEach((item) => {
  1564. if(!this.is_hover) {
  1565. item.definition = this.paper_symbol_definitions.entite;
  1566. if (item.focused) {
  1567. item.scale(0.5)
  1568. item.focused = false;
  1569. }
  1570. }
  1571. })
  1572. }
  1573. }
  1574. } else {
  1575. this.paper_main_object.children.entites_superposes.visible = false;
  1576. }
  1577. }
  1578. // // puissance d'agir
  1579. // if (this.concernement.has_puissancedagir) {
  1580. // if (this.map_mode === "puissancedagir") {
  1581. // if (!this.is_open) {
  1582. // this.paper_main_object.children.puissanceagir_icon.visible = true; // if not opened and has_puissancedagir draw the puissance d'agir icone
  1583. // } else {
  1584. // this.paper_main_object.children.puissanceagir_icon.visible = false;
  1585. // }
  1586. // } else {
  1587. // this.paper_main_object.children.puissanceagir_icon.visible = false;
  1588. // }
  1589. // }
  1590. // agissantes
  1591. // if (this.concernement.has_agissantes) {
  1592. // if (this.map_mode !== "action") {
  1593. // this.paper_main_object.children.agissantes_icons.visible = false;
  1594. // } else if(!this.is_open && !this.is_closing){
  1595. // this.paper_main_object.children.agissantes_icons.visible = true;
  1596. // }
  1597. // }
  1598. // doleance
  1599. if (this.concernement.has_doleance) {
  1600. if (this.map_mode === "doleancer") {
  1601. if (!this.is_open) {
  1602. // this.paper_main_object.children.doleance_icon.visible = true;
  1603. } else {
  1604. // this.paper_main_object.children.doleance_icon.visible = false;
  1605. // display the right (opened) doleance
  1606. let from = null
  1607. let to = this.concernement.opened_doleance.id;
  1608. this.concernement.doleances.forEach((d) => {
  1609. // this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible = d.id === this.concernement.opened_doleance.id;
  1610. if (this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible) {
  1611. from = d.id
  1612. }
  1613. })
  1614. // console.log(`opened_doleance from:${from}, to:${to}`);
  1615. if (from && from !== to) {
  1616. this.initDoleanceTransition(from, to);
  1617. } else if(!this.doleance_transition) {
  1618. this.concernement.doleances.forEach((d) => {
  1619. this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible = d.id === this.concernement.opened_doleance.id;
  1620. })
  1621. }
  1622. }
  1623. } else {
  1624. // this.paper_main_object.children.doleance_icon.visible = false;
  1625. }
  1626. }
  1627. // focus on opened entite
  1628. if (this.is_open && (this.map_mode === 'terraindevie' || this.map_mode === 'action')) {
  1629. let group = this.map_mode === 'terraindevie' ? this.paper_main_object.children['entites'] : this.paper_main_object.children['agissantes'];
  1630. if(group){
  1631. group.children.forEach((item) => {
  1632. if (this.opened_entite_id && item.item_id === parseInt(this.opened_entite_id) && item.item_type !== 'entite_hidden') {
  1633. item.definition = item.item_type === 'entite' ? this.paper_symbol_definitions.entite_hover : this.paper_symbol_definitions.entite_action_hover;
  1634. } else {
  1635. if ((!this.hover_elmt || item.item_id !== this.hover_elmt.id) && item.item_type !== 'entite_hidden') {
  1636. item.definition = item.item_type === 'entite' ? this.paper_symbol_definitions.entite : this.paper_symbol_definitions.entite_action;
  1637. }
  1638. }
  1639. })
  1640. }
  1641. }
  1642. },
  1643. initDoleanceTransition(from, to){
  1644. if (!this.doleance_transition) {
  1645. this.doleance_transition = true
  1646. // console.log(`initDoleanceTransition from:${from}, to:${to}`);
  1647. let from_pg = this.paper_main_object.children.doleances.children[`doleance_${from}`]
  1648. from_pg.visible = true;
  1649. from_pg.opacity = 1;
  1650. let to_pg = this.paper_main_object.children.doleances.children[`doleance_${to}`]
  1651. to_pg.visible = true;
  1652. to_pg.opacity = 0;
  1653. let bg = this.paper_main_object.children.doleance_bg;
  1654. // // fade out the current visible cercle politique then fade in the new one
  1655. new Tween.Tween({o: 1})
  1656. .to({
  1657. o: 0
  1658. }, 600)
  1659. .onUpdate((obj) => {
  1660. console.log(`tween update. o=${obj.o}`);
  1661. from_pg.opacity = bg.opacity = obj.o;
  1662. })
  1663. .onComplete((obj) => {
  1664. new Tween.Tween({o: 0})
  1665. .to({
  1666. o: 1
  1667. }, 900)
  1668. .onUpdate((objb) => {
  1669. to_pg.opacity = bg.opacity = objb.o;
  1670. })
  1671. .onComplete((obj) => {
  1672. from_pg.visible = false;
  1673. from_pg.opacity = 1;
  1674. to_pg.opacity = 1;
  1675. this.doleance_transition = false
  1676. })
  1677. .delay(500)
  1678. .easing(Tween.Easing.Quadratic.InOut)
  1679. .start();
  1680. })
  1681. .easing(Tween.Easing.Quadratic.Out)
  1682. .start();
  1683. }
  1684. },
  1685. onIsHover(){
  1686. // handle layer z-index
  1687. if (this.is_hover) {
  1688. this.paper_main_object.bringToFront();
  1689. } else {
  1690. // mapitem can be hover while an other mapitem is open if it is superposed (superposition) to the currently opened mapitem
  1691. // in this case don't send it back
  1692. if (!this.opened_concernement) {
  1693. this.paper_main_object.sendToBack();
  1694. }
  1695. }
  1696. // hightlight superposed entites on focus
  1697. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1698. // console.log('entites_superposes.children', entites_superposes.children);
  1699. if(entites_superposes){
  1700. for(let paper_item of entites_superposes.children) {
  1701. if (this.is_hover) {
  1702. paper_item.definition = this.paper_symbol_definitions.entite_hover;
  1703. // paper_item.scale(2)
  1704. } else {
  1705. paper_item.definition = this.paper_symbol_definitions.entite;
  1706. // paper_item.scale(0.5)
  1707. }
  1708. }
  1709. }
  1710. },
  1711. /*
  1712. * PAPER EVENTS
  1713. */
  1714. initPaperEvents(){
  1715. this.paper_main_object.onMouseLeave = function(event){
  1716. if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened
  1717. this.resetHoverElmt();
  1718. document.body.style.cursor = "auto";
  1719. }
  1720. }.bind(this);
  1721. this.paper_main_object.onMouseMove = function(event){
  1722. console.log(`onmousemove ${this.id}`, this.cartouch_is_opened);
  1723. // prevent hover map item mouse event if cartouch is opened
  1724. if (this.cartouch_is_opened && this.map_mode !== "superposition") return;
  1725. if (!this.is_open) {
  1726. if (!this.opened_concernement) { // only if no concernement is opened and is this focused
  1727. if(this.isFocused()){
  1728. if (this.map_mode === 'superposition') {
  1729. // get the superposed entite
  1730. this.setHoverElmt({
  1731. type: 'superposition',
  1732. id: this.id,
  1733. cid: this.cid,
  1734. eid: this.superposedEntiteID
  1735. });
  1736. } else{
  1737. this.setHoverElmt({
  1738. type: 'concernement',
  1739. id: this.id,
  1740. cid: this.cid
  1741. });
  1742. }
  1743. document.body.style.cursor = "pointer";
  1744. }
  1745. } else if (this.map_mode === 'superposition'){
  1746. // if this mapitem is superposed to the opened mapitem
  1747. if(this.opened_concernement.all_superposed_concernements_id.indexOf(this.cid) >= 0){
  1748. this.setHoverElmt({
  1749. type: 'superposition',
  1750. id: this.id,
  1751. cid: this.cid,
  1752. eid: this.superposedEntiteID
  1753. });
  1754. document.body.style.cursor = "pointer";
  1755. }
  1756. }
  1757. } else {
  1758. // reset all proximity contrs stroke to white
  1759. if (this.paper_main_object.children['entites_proximites_references']) {
  1760. this.paper_main_object.children['entites_proximites_references'].getItems({name:'contours'}).forEach((contr) => {
  1761. contr.strokeColor = '#fff';
  1762. })
  1763. }
  1764. // lets define some options regarding the map_mode
  1765. let paper_group_tohit;
  1766. switch (this.map_mode) {
  1767. case "terraindevie":
  1768. paper_group_tohit = this.paper_main_object.children['entites'];
  1769. break;
  1770. case "proximite":
  1771. paper_group_tohit = this.paper_main_object.children['entites_proximites_references'];
  1772. break;
  1773. case "superposition":
  1774. paper_group_tohit = this.paper_main_object.children['entites_superposes'];
  1775. break;
  1776. case "action":
  1777. paper_group_tohit = this.paper_main_object.children['agissantes'];
  1778. break;
  1779. case "puissancedagir":
  1780. paper_group_tohit = this.paper_main_object.children['puissanceagir_besoins'];
  1781. break;
  1782. case "doleancer":
  1783. if(this.paper_main_object.children['doleances']){
  1784. paper_group_tohit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1785. }
  1786. break;
  1787. }
  1788. let result = paper_group_tohit ? paper_group_tohit.hitTest(event.point) : null;
  1789. // console.log('move result', result);
  1790. if (result && result.item.item_id) {
  1791. // console.log('move has result', result);
  1792. let new_hover_elmt = {
  1793. paper_id: result.item.id,
  1794. type: result.item.item_type,
  1795. id: result.item.item_id
  1796. };
  1797. switch (result.item.item_type) {
  1798. case "entite_proximite_reference":
  1799. new_hover_elmt.ref_cid = result.item.ref_cid;
  1800. new_hover_elmt.parent_eid = result.item.parent_eid;
  1801. // set the mouse overed contrs to blue
  1802. // contour_paper_item_id is recorded when creating the full paper objects in setPaperEntitesProximiteReferences()
  1803. let contrs_paper_id = result.item.contour_paper_item_id;
  1804. let contrs_paper_obj = result.item.parent.getItem({id: contrs_paper_id});
  1805. contrs_paper_obj.strokeColor = "#01ffe2";
  1806. break;
  1807. case "besoin":
  1808. new_hover_elmt.no_popup = true;
  1809. break;
  1810. case "reponse":
  1811. new_hover_elmt.bid = result.item.item_bid;
  1812. new_hover_elmt.cid = result.item.item_cid;
  1813. new_hover_elmt.no_popup = true;
  1814. break;
  1815. }
  1816. if (!this.hover_elmt || new_hover_elmt.paper_id !== this.hover_elmt.paper_id) {
  1817. // console.log(`before setHoverElmt ${this.id}`);
  1818. this.setHoverElmt(new_hover_elmt);
  1819. }
  1820. document.body.style.cursor = "pointer";
  1821. } else {
  1822. // console.log('move no result');
  1823. this.resetHoverElmt();
  1824. document.body.style.cursor = "auto";
  1825. }
  1826. }
  1827. }.bind(this);
  1828. this.paper_main_object.onClick = async function(event){
  1829. console.log('paper concernement onClick');
  1830. // prevent hover map item mouse event if cartouch is opened
  1831. if (this.cartouch_is_opened) return;
  1832. if (!this.is_open) { // si ce concernement n'est pas ouvet
  1833. console.log('mapitem is NOT opened');
  1834. if (!this.opened_concernement) { // si aucun concernement n'est ouvert
  1835. console.log(`Open me ${this.id}`);
  1836. // push route (keep the hash for map_mode)
  1837. // wait for routing to be finished before opening the mapItem
  1838. let params = {cid: parseInt(this.cid)}
  1839. let query = {mapitemid: this.id};
  1840. if (this.map_mode === "superposition") {
  1841. params.eid = this.superposedEntiteID
  1842. query.superposition_cluster_index = this.mapitem.superposition_cluster_index;
  1843. }
  1844. await this.$router.push({
  1845. name: 'concernement',
  1846. params: params,
  1847. query: query,
  1848. hash: `#${this.map_mode}`
  1849. });
  1850. // open/close all concernements
  1851. this.openCloseConcernements(this.cid, this.id)
  1852. // reset the mousehover
  1853. this.resetHoverElmt();
  1854. } else if (this.map_mode === 'superposition'){ // si un concernement est ouvert et qu'on est en mode superposition
  1855. // if this mapitem is superposed to the opened mapitem
  1856. if(this.opened_concernement.all_superposed_concernements_id.indexOf(this.cid) >= 0){
  1857. // push route (keep the hash for map_mode)
  1858. // wait for routing to be finished before opening the mapItem
  1859. console.log('click on superposed this.mapitem.superposition_cluster_index', this.mapitem.superposition_cluster_index);
  1860. await this.$router.push({
  1861. name: 'concernement',
  1862. params: {cid: this.opened_concernement.cid, eid: this.$route.params.eid},
  1863. query: {
  1864. mapitemid: this.opened_concernement.opened_mapitem_id,
  1865. superposition_cluster_index: this.mapitem.superposition_cluster_index,
  1866. superposed_cid: this.cid
  1867. },
  1868. hash: `#${this.map_mode}`
  1869. });
  1870. }
  1871. }
  1872. } else { // si ce concernement est ouvert
  1873. console.log('mapitem is opened');
  1874. // lets define some options regarding the map_mode
  1875. let group_to_hit = null;
  1876. switch (this.map_mode) {
  1877. case "terraindevie":
  1878. group_to_hit = this.paper_main_object.children['entites'];
  1879. break;
  1880. case "proximite":
  1881. group_to_hit = this.paper_main_object.children['entites_proximites_references'];
  1882. break;
  1883. case "superposition":
  1884. group_to_hit = this.paper_main_object.children['entites_superposes'];
  1885. break;
  1886. case "action":
  1887. group_to_hit = this.paper_main_object.children['agissantes'];
  1888. break;
  1889. case "doleancer":
  1890. group_to_hit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1891. break;
  1892. }
  1893. if (group_to_hit) {
  1894. let result = group_to_hit.hitTest(event.point);
  1895. console.log('click result', result);
  1896. if (result) {
  1897. switch (this.map_mode) {
  1898. case "terraindevie":
  1899. case "superposition":
  1900. case "action":
  1901. // we have clicked on an entite
  1902. this.$router.push({
  1903. name: 'concernement',
  1904. params: {cid: this.cid, eid: result.item.item_id},
  1905. query: {
  1906. mapitemid: this.id,
  1907. // superposition_id: this.mapitem.superposition_ids[0]
  1908. },
  1909. hash: `#${this.map_mode}`,
  1910. });
  1911. break;
  1912. case "proximite":
  1913. switch (result.item.item_type) {
  1914. case "entite_proximite":
  1915. this.$router.push({
  1916. name: 'concernement',
  1917. params: {cid: this.cid, eid: result.item.item_id},
  1918. hash: `#${this.map_mode}`,
  1919. });
  1920. break;
  1921. case "entite_proximite_reference":
  1922. this.$router.push({
  1923. name: 'concernement',
  1924. params: {cid: this.cid, eid: result.item.parent_eid},
  1925. query: {
  1926. proximite_cid: result.item.ref_cid,
  1927. proximite_eid: result.item.item_id
  1928. },
  1929. hash: `#${this.map_mode}`,
  1930. });
  1931. break;
  1932. }
  1933. case "doleancer":
  1934. this.setOpenedDoleanceField(this.cid, result.item.did, result.item.field, result.item.field_index);
  1935. break;
  1936. }
  1937. } else {
  1938. // otherwise we close the entite and come back to the concernement
  1939. this.$router.push({
  1940. name: 'concernement',
  1941. hash: `#${this.map_mode}`,
  1942. params: {id: this.cid, mapitemid: this.id}
  1943. });
  1944. // reset the mousehover
  1945. this.resetHoverElmt();
  1946. }
  1947. }
  1948. }
  1949. }.bind(this);
  1950. },
  1951. resetHoverElmt(){
  1952. // console.log('resetHoverElmt');
  1953. setTimeout(()=>{
  1954. this.setHoverElmt(null);
  1955. }, 100);
  1956. },
  1957. // OPEN / CLOSE (with tween)
  1958. getOpeningAvailableScale(){
  1959. // calcul opened size regarding window size and surounding contents
  1960. let header = document.querySelector('header#header');
  1961. let header_height = header.clientHeight;
  1962. let map_nav = document.querySelector('nav#map-nav');
  1963. let map_nav_height = map_nav.clientHeight;
  1964. let s_h = (this.canvas.height - header_height - map_nav_height) / (this.ray*2*1.15);
  1965. let s_w = (this.canvas.width - this.cartouch_width) / (this.ray*2*1.7);
  1966. return Math.min(s_h, s_w)
  1967. },
  1968. openClose(open) { // async
  1969. // await nextTick(); // not working
  1970. console.log(`ConcernementsMapItem ${this.id} openClose: ${open}`);
  1971. if (this.tween) {
  1972. this.tween.stop();
  1973. }
  1974. if (open) {
  1975. this.is_opening = true;
  1976. // create the paper objects to display (like entite, besoin, etc)
  1977. this.setPaperContents();
  1978. this.handlePaperVisibilityOnBeforeOpen();
  1979. let s = this.getOpeningAvailableScale();
  1980. // create once the opening tweening
  1981. this.tween = new Tween.Tween({s: this.scale, x: this.pos.x, y: this.pos.y, o: 0})
  1982. .to({
  1983. s: s,
  1984. x: (this.canvas.width - this.cartouch_width) / 2,
  1985. y: this.canvas.height / 2,
  1986. o: 0.8
  1987. }, 800)
  1988. .onUpdate((obj) => {
  1989. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  1990. // revert to the original size (by reverting the previous scale)
  1991. // Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  1992. this.paper_main_object.scale(1 / this.scale);
  1993. // then scale again to new scale
  1994. // Matter.Body.scale(this.body, obj.s, obj.s)
  1995. this.paper_main_object.scale(obj.s);
  1996. // update superposition constraints points poisitions
  1997. if (this.concernement.superposition_constraints_id
  1998. && this.concernement.superposition_constraints_id[this.id]
  1999. && this.concernement.superposition_constraints_id[this.id].length)
  2000. {
  2001. // get all the constraints of the world
  2002. let all_constrains = Matter.Composite.allConstraints(this.matterEngine.world);
  2003. // loop through all constraint ids recorded in the concernement
  2004. // get only the constraint of this concernement mapitem
  2005. for(let constraint_id of this.concernement.superposition_constraints_id[this.id]) {
  2006. // get the right constraint object from matter
  2007. let constraint;
  2008. for (const c of all_constrains) {
  2009. if (c.id === constraint_id) {
  2010. constraint = c;
  2011. break;
  2012. }
  2013. }
  2014. if (constraint) {
  2015. // tween the constraint point
  2016. let ab = this.cid === constraint.concernementA.id ? 'A' : 'B';
  2017. let point = constraint[`point${ab}`];
  2018. // revert to the original point
  2019. let oripoint = Matter.Vector.create(point.x / this.scale, point.y / this.scale)
  2020. // newpoint from original point
  2021. let newpoint = Matter.Vector.create(oripoint.x * obj.s, oripoint.y * obj.s)
  2022. constraint[`point${ab}`] = newpoint;
  2023. }
  2024. }
  2025. }
  2026. // record new scale
  2027. this.prev_scale = this.scale;
  2028. this.scale = this.mapitem.scale = obj.s;
  2029. this.opacity = obj.o;
  2030. // console.log('tween update obj.s', obj.s);
  2031. this.pos = {x:obj.x, y:obj.y};
  2032. Matter.Body.setPosition(this.body, this.pos);
  2033. })
  2034. .onComplete((obj) => {
  2035. // console.log('tween complete obj.s', obj.s);
  2036. // record tween one last time
  2037. this.prev_scale = this.scale = obj.s;
  2038. this.mapitem.scale = this.scale;
  2039. this.opacity = obj.o;
  2040. this.pos = {x:obj.x, y:obj.y};
  2041. Matter.Body.setPosition(this.body, this.pos);
  2042. this.handlePaperVisibilityOnOpened();
  2043. // fix the concernement position with a constraint
  2044. this.constraint = Matter.Constraint.create({
  2045. pointA: this.pos,
  2046. bodyB: this.body,
  2047. stiffness: 1,
  2048. damping: 0,
  2049. length: 0
  2050. });
  2051. Matter.Composite.add(this.matterEngine.world, [this.body, this.constraint]);
  2052. // paper bring to front
  2053. // this.paper_main_object.bringToFront();
  2054. this.paper_main_object.sendToBack();
  2055. this.is_opening = false;
  2056. });
  2057. // recreate the matter engine event to get it a the end of the events stack
  2058. Matter.Events.off(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  2059. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  2060. } else {
  2061. // CLOSING
  2062. this.is_closing = true;
  2063. if(this.constraint){
  2064. Matter.Composite.remove(this.matterEngine.world, this.constraint);
  2065. }
  2066. // this.concernement.active_revision = this.concernement.revision_id;
  2067. this.setActiveRevision(this.cid, this.concernement.revision_id);//this.concernement.revisions[this.concernement.revisions.length -1].revision_id);
  2068. this.resetPaperActiveRevision();
  2069. this.handlePaperVisibilityOnBeforeClose();
  2070. this.setOpenedEntiteId(null);
  2071. // this.setOpenedRecit(null);
  2072. // create once the closing tween
  2073. this.tween = new Tween.Tween({s: this.scale, o: 1})
  2074. .to({s: 1, o: 0}, 500)
  2075. .onUpdate((obj) => {
  2076. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  2077. // revert to the original size (by reverting the previous scale)
  2078. // INFO we dont need to sacle the matter body AND it may fix the montgolfière bug
  2079. // TODO find another way to increase the mass
  2080. // Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  2081. this.paper_main_object.scale(1 / this.scale);
  2082. // then scale again to new scale
  2083. // Matter.Body.scale(this.body, obj.s, obj.s)
  2084. this.paper_main_object.scale(obj.s);
  2085. // update superposition constraints points positions
  2086. if (this.concernement.superposition_constraints_id
  2087. && this.concernement.superposition_constraints_id[this.id]
  2088. && this.concernement.superposition_constraints_id[this.id].length)
  2089. {
  2090. // get all the constraints of the world
  2091. let all_constrains = Matter.Composite.allConstraints(this.matterEngine.world);
  2092. // loop through all constraint ids recorded in the concernement
  2093. // get only the constraint of this concernement mapitem
  2094. for(let constraint_id of this.concernement.superposition_constraints_id[this.id]) {
  2095. // get the right constraint object from matter
  2096. let constraint;
  2097. for (const c of all_constrains) {
  2098. if (c.id === constraint_id) {
  2099. constraint = c;
  2100. break;
  2101. }
  2102. }
  2103. if (constraint) {
  2104. // tween the constraint point
  2105. let ab = this.cid === constraint.concernementA.id ? 'A' : 'B';
  2106. let point = constraint[`point${ab}`];
  2107. // revert to the original point
  2108. let oripoint = Matter.Vector.create(point.x / this.scale, point.y / this.scale)
  2109. // newpoint from original point
  2110. let newpoint = Matter.Vector.create(oripoint.x * obj.s, oripoint.y * obj.s)
  2111. constraint[`point${ab}`] = newpoint;
  2112. }
  2113. }
  2114. }
  2115. // record new scale
  2116. this.prev_scale = this.scale;
  2117. this.scale = obj.s;
  2118. this.mapitem.scale = this.scale;
  2119. this.opacity = obj.o;
  2120. })
  2121. .onComplete((obj) => {
  2122. this.prev_scale = this.scale = 1;
  2123. this.mapitem.scale = this.scale;
  2124. this.handlePaperVisibilityOnClosed();
  2125. this.clearPaperContents();
  2126. this.is_closing = false;
  2127. });
  2128. }
  2129. this.tween.easing(Tween.Easing.Quadratic.InOut).start();
  2130. },
  2131. // ENGINE UPDATE
  2132. onBeforeEngineUpdate (event) {
  2133. // update the opening/closing tweening
  2134. // if (this.tween) {
  2135. // this.tween.update();
  2136. // }
  2137. Tween.update();
  2138. if (this.opened_concernement) {
  2139. // 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]);
  2140. // console.log(this.id, this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id].indexOf(this.id));
  2141. if (this.opened_concernement.opened_mapitem_id !== this.id) {
  2142. // push all aside mapitems not superposed the the opened mapitem
  2143. let pushaside = true;
  2144. // BUT NOT push aside mapitems superposed to this item
  2145. // opened_mapitem <-- superposed_mapitems
  2146. if (this.map_mode === 'superposition') {
  2147. if (this.opened_concernement.superposed_mapitem_id_by_mapitem_id
  2148. && this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id])
  2149. { // if opened mapitem has superposed mapitems
  2150. let superposed_ids = this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id]
  2151. if (superposed_ids.indexOf(this.id) >= 0) { //this mapitem is directly superposed to the opened mapitem
  2152. pushaside = false;
  2153. }
  2154. }
  2155. }
  2156. if (pushaside) {
  2157. this.pushAside()
  2158. }
  2159. }
  2160. } else if (!this.is_hover){
  2161. if (this.map_mode === 'proximite'
  2162. || this.map_mode === 'superposition'
  2163. || this.map_mode === 'puissancedagir'
  2164. || this.map_mode === 'action'
  2165. || this.map_mode === 'doleancer'){ // apply focus forces : move unfocused on the sides and focused on the center
  2166. this.applyFocusForces(); //
  2167. } else {
  2168. this.applyNormalForces(); // if terrain de vie apply force to gently reject items from the sides and so create a continuasly mvmt
  2169. }
  2170. } else {
  2171. // stop movement if is mouse hover
  2172. Matter.Body.setVelocity(this.body, {x:0, y:0})
  2173. }
  2174. // reset all matter rotation forces otherwise items will spin when colide
  2175. Matter.Body.setAngle(this.body, 0);
  2176. Matter.Body.setAngularSpeed(this.body, 0);
  2177. },
  2178. applyFocusForces(){
  2179. if(!this.isFocused()) {
  2180. this.pushAside()
  2181. }else{
  2182. this.bringToCenter()
  2183. }
  2184. },
  2185. isFocused(){
  2186. return this.map_mode === 'terraindevie'
  2187. || (this.map_mode === 'proximite' && this.concernement.has_proximites)
  2188. || (this.map_mode === 'superposition' && this.concernement.has_superpositions)
  2189. || (this.map_mode === 'action' && this.concernement.has_agissantes)
  2190. || (this.map_mode === 'puissancedagir' && this.concernement.has_puissancedagir)
  2191. || (this.map_mode === 'doleancer' && this.concernement.has_doleance);
  2192. },
  2193. pushAside(){
  2194. // console.log('pushAside');
  2195. // apply a force in direction of one side or an other depending of the start position
  2196. // the force is exponentialy proportional to the distance from the side
  2197. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  2198. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2199. let pseudo_center_x = this.opened_concernement
  2200. ? (this.canvas.width - this.cartouch_width) / 2
  2201. : this.canvas.width / 2;
  2202. // get the direction to the closest side
  2203. let dir = this.map_mode === 'superposition'
  2204. && this.concernement.superposition_constraints_id
  2205. && this.concernement.superposition_constraints_id[this.id]
  2206. && this.concernement.superposition_constraints_id[this.id].length // go to the right if has superposition constraint applied
  2207. ? 1 // to the right
  2208. : this.pos.x > pseudo_center_x // else
  2209. ? 1 // to the right
  2210. : -1; // to the left
  2211. if (this.map_mode === 'doleancer') {
  2212. dir = 1;
  2213. }
  2214. // max and min item position
  2215. let minp = 0;
  2216. let maxp = dir < 0
  2217. ? pseudo_center_x
  2218. : this.canvas.width - pseudo_center_x;
  2219. // max and min force
  2220. let minf = 0;
  2221. let maxf = 6;
  2222. // scale factor
  2223. let scale = (maxf-minf) / (maxp-minp);
  2224. // get the inversed distance
  2225. let dist = dir < 0
  2226. ? this.pos.x
  2227. : this.canvas.width - this.pos.x; // get the distance from the side
  2228. // // calculate the force
  2229. // let x_force = Math.pow(dist/700,100) * dir;
  2230. // calculate the logarithmic force
  2231. let x_force = Math.exp(minf + scale*(dist-minp)) * dir;
  2232. let ori_pos = {x:pseudo_center_x, y:this.body.position.y};
  2233. Matter.Body.applyForce(
  2234. this.body,
  2235. ori_pos,
  2236. {
  2237. x: x_force,
  2238. y: 0
  2239. }
  2240. );
  2241. this.body.frictionAir = 0.01;
  2242. },
  2243. bringToCenter(){
  2244. // bring to the centre
  2245. // apply a force in direction of one side or an other depending of the start position
  2246. // the force is exponentialy inversed proportional to the distance from the side
  2247. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  2248. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2249. let pseudo_center_x = this.opened_concernement
  2250. ? (this.canvas.width - this.cartouch_width) / 2
  2251. : this.canvas.width / 2;
  2252. let dir = this.pos.x > pseudo_center_x
  2253. ? -1 // to left
  2254. : 1; // to right
  2255. // get the inversed distance from the side
  2256. let dist = dir < 0
  2257. ? this.pos.x - pseudo_center_x
  2258. : pseudo_center_x - this.pos.x;
  2259. 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
  2260. // max & min item position
  2261. let minp = 0;
  2262. let maxp = dir < 0
  2263. ? this.canvas.width - pseudo_center_x
  2264. : pseudo_center_x;
  2265. // X force decreasing near the center
  2266. // max and min force
  2267. let xminf = 0;
  2268. let xmaxf = 1;// + Math.random()*4;
  2269. // scale factor
  2270. let xscale = (xmaxf-xminf) / (maxp-minp);
  2271. let x_force = Math.exp(xminf + xscale*(dist-minp)) * dir;
  2272. let ori_pos = dir < 0
  2273. ? {x:this.canvas.width, y:this.body.position.y}
  2274. : {x:0, y:this.body.position.y};
  2275. Matter.Body.applyForce(
  2276. this.body,
  2277. ori_pos,
  2278. {
  2279. x: x_force,
  2280. y: 0
  2281. }
  2282. );
  2283. // Friction air fa (increasing near the center)
  2284. let fa_minf = 0;
  2285. let fa_maxf = 20;
  2286. // scale factor
  2287. let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  2288. let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  2289. this.body.frictionAir = fa;
  2290. // console.log(`bringToCenter dist:${dist}, x_force:${x_force}, friction air:${fa}`);
  2291. } else { // in the ceneter zone let items floats freely
  2292. let fa = 0.01;
  2293. let velocity = Matter.Body.getVelocity(this.body);
  2294. if (velocity.x) {
  2295. let velocityx = Math.abs(velocity.x);
  2296. // map a range of numbers to another range of numbers
  2297. // INFO https://stackoverflow.com/a/46462321
  2298. let velocity_range = [50, 1000];
  2299. let fa_range = [0,3];
  2300. fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  2301. // console.log(`bringToCenter velocityx:${velocityx}, fa:${fa}, velocity`, velocity);
  2302. } else {
  2303. console.warn(`bringToCenter no velocity`, velocity);
  2304. }
  2305. this.body.frictionAir = fa;
  2306. }
  2307. },
  2308. applyShuffleForces() {
  2309. // console.log('applyShuffleForces');
  2310. // var dist, dir, x_velocity;
  2311. // dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
  2312. // dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
  2313. // x_velocity = Math.pow(dist/650,10) * dir;
  2314. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2315. let pseudo_center_x = this.opened_concernement
  2316. ? (this.canvas.width - this.cartouch_width) / 2
  2317. : this.canvas.width / 2;
  2318. // get the direction to the centre
  2319. let dir = this.pos.x > pseudo_center_x
  2320. ? -1 // to left
  2321. : 1; // to right
  2322. // get the inversed distance from the side
  2323. let dist = dir < 0
  2324. ? this.pos.x - pseudo_center_x
  2325. : pseudo_center_x - this.pos.x;
  2326. dist = dist > 1000 ? 1000 : dist;
  2327. 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
  2328. // max & min item position
  2329. let minp = 0;
  2330. let maxp = dir < 0
  2331. ? this.canvas.width - pseudo_center_x
  2332. : pseudo_center_x;
  2333. // X max and min force
  2334. let xminf = 0;
  2335. // let xmaxf = 4 + Math.random()*3;
  2336. let xmaxf = 2 + Math.random()*2;
  2337. // scale factor
  2338. let xscale = (xmaxf-xminf) / (maxp-minp);
  2339. let x_velocity = Math.exp(xminf + xscale*(dist-minp))*dir;
  2340. // let y_velocity = 30 + Math.random() * 50;
  2341. // y_velocity = Math.random() > 0.5 ? -y_velocity : y_velocity;
  2342. // X max and min force
  2343. let yminf = 1;
  2344. // let ymaxf = 2 + Math.random()*4;
  2345. let ymaxf = 4 + Math.random()*2;
  2346. // scale factor
  2347. let yscale = (ymaxf-yminf) / (maxp-minp);
  2348. let y_velocity = Math.exp(yminf + yscale*(dist-minp)) * (Math.random() > 0.5 ? 1 : -1); // let ori_pos = dir < 0
  2349. // ? {x:this.canvas.width, y:this.body.position.y}
  2350. // : {x:0, y:this.body.position.y};
  2351. // let x_force = Math.pow(dist/800,10) * dir;
  2352. console.log(`applyShuffleForces dir:${dir}, maxp:${maxp}, dist:${dist}, x_velocity:${x_velocity}, y_velocity:${y_velocity}`);
  2353. Matter.Body.setVelocity(this.body, {x: x_velocity, y: y_velocity});
  2354. } else { // if in center zone real shuffle velocity
  2355. // x_velocity = -50 + Math.random()*100;
  2356. // y_velocity = -50 + Math.random()*100;
  2357. // this.setInitBodyVelocity();
  2358. }
  2359. this.body.frictionAir = 0.01;
  2360. },
  2361. applyNormalForces () {
  2362. // bring gently to the centre
  2363. // apply a force in direction of one side or an other depending of the start position
  2364. // the force is exponentialy inversed proportional to the distance from the side
  2365. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  2366. // cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2367. // TODO force is proportional to window size
  2368. // X
  2369. let pseudo_center_x = this.opened_concernement
  2370. ? (this.canvas.width - this.cartouch_width) / 2
  2371. : this.canvas.width / 2;
  2372. let xdir = this.pos.x > pseudo_center_x
  2373. ? -1 // to left
  2374. : 1; // to right
  2375. // get the inversed distance from the side
  2376. let xdist = xdir < 0
  2377. ? this.pos.x - pseudo_center_x
  2378. : pseudo_center_x - this.pos.x;
  2379. // Y
  2380. let pseudo_center_y = this.canvas.height / 2;
  2381. let ydir = this.pos.y > pseudo_center_y
  2382. ? -1 // to top
  2383. : 1; // to bottom
  2384. // get the inversed distance from the side
  2385. let ydist = ydir < 0
  2386. ? this.pos.y - pseudo_center_y
  2387. : pseudo_center_y - this.pos.y;
  2388. let k = 4;
  2389. 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
  2390. // max and min force
  2391. let minf = 0;
  2392. let maxf = 0.1;
  2393. // X
  2394. // max & min item position
  2395. let xminp = 0;
  2396. let xmaxp = xdir < 0
  2397. ? this.canvas.width - pseudo_center_x
  2398. : pseudo_center_x;
  2399. // X force decrsinsing near the center
  2400. // scale factor
  2401. let xscale = (maxf-minf) / (xmaxp-xminp);
  2402. let x_force = Math.exp(minf + xscale*(xdist-xminp)) * xdir;
  2403. let xori_pos = xdir < 0
  2404. ? {x:this.canvas.width, y:this.body.position.y}
  2405. : {x:0, y:this.body.position.y};
  2406. Matter.Body.applyForce(
  2407. this.body,
  2408. xori_pos,
  2409. {
  2410. x: x_force,
  2411. y: 0
  2412. }
  2413. );
  2414. // Y
  2415. // max & min item position
  2416. let yminp = 0;
  2417. let ymaxp = ydir < 0
  2418. ? this.canvas.height - pseudo_center_y
  2419. : pseudo_center_y;
  2420. // X force decrsinsing near the center
  2421. // scale factor
  2422. let yscale = (maxf-minf) / (ymaxp-yminp);
  2423. let y_force = Math.exp(minf + yscale*(ydist-xminp)) * ydir;
  2424. let yori_pos = ydir < 0
  2425. ? {x:this.body.position.x, y:this.canvas.height}
  2426. : {x:this.body.position.x, y:0};
  2427. Matter.Body.applyForce(
  2428. this.body,
  2429. yori_pos,
  2430. {
  2431. x: 0,
  2432. y: y_force
  2433. }
  2434. );
  2435. // // Friction air fa (increasing near the center)
  2436. // let fa_minf = 0;
  2437. // let fa_maxf = 6;
  2438. // // scale factor
  2439. // let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  2440. // let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  2441. // this.body.frictionAir = fa;
  2442. // console.log(`bringToCenter dist:${xdist}, x_force:${x_force}`);
  2443. } else { // in the ceneter zone let items floats freely
  2444. // let velocity = Matter.Body.getVelocity(this.body);
  2445. // let velocityx = Math.abs(velocity.x);
  2446. // // map a range of numbers to another range of numbers
  2447. // // INFO https://stackoverflow.com/a/46462321
  2448. // let velocity_range = [50, 1000];
  2449. // let fa_range = [0,3];
  2450. // let fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  2451. // console.log(`bringToCenter velocity.x:${velocity.x}, fa:${fa}`);
  2452. // this.body.frictionAir = fa;
  2453. this.body.frictionAir = 0.1;
  2454. }
  2455. },
  2456. checkOverflow() {
  2457. // respawn element if outside screen
  2458. let pad = 1;
  2459. let circleray = this.body.circleRadius+pad*1.1;
  2460. let respawn = false;
  2461. if(this.pos.x <= pad){ this.pos.x = circleray; respawn=true;}
  2462. if(this.pos.x >= this.canvas.width-pad){ this.pos.x = this.canvas.width - circleray; respawn=true;}
  2463. if(this.pos.y <= pad){ this.pos.y = circleray; respawn=true;}
  2464. if(this.pos.y >= this.canvas.height-pad){ this.pos.y = this.canvas.height - circleray; respawn=true;}
  2465. if (respawn) {
  2466. // this.pos = respawn_pos;
  2467. Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
  2468. // this.setInitBodyVelocity();
  2469. Matter.Body.setVelocity(this.body, {x:0,y:0});
  2470. }
  2471. },
  2472. onAfterEngineUpdate (event) {
  2473. this.checkOverflow();
  2474. this.paper_main_object.position = this.pos = this.body.position;
  2475. // fadein intro
  2476. // if(!this.mapitem.clone){
  2477. if (this.paper_main_object.opacity < 9) {
  2478. this.paper_main_object.opacity = this.paper_main_object.opacity * 1.1;
  2479. } else{
  2480. this.paper_main_object.opacity = 1;
  2481. }
  2482. // }
  2483. this.handlePaperVisibilityOnAfterEnginUpdate()
  2484. // DEBUG
  2485. // this.debugDrawMatterBodyCircle();
  2486. // END DEBUG
  2487. },
  2488. // onWindowResize(event){
  2489. // // console.log('mapitem onWindowResize', event, this);
  2490. // },
  2491. debugDrawMatterBodyCircle(){
  2492. if (this.paper_main_object.children['debug_circle']) {
  2493. this.paper_main_object.children['debug_circle'].remove();
  2494. }
  2495. let debugcircle = new paper.Path.Circle({
  2496. center: this.pos,
  2497. // radius: this.ray,
  2498. radius: this.body.circleRadius,
  2499. strokeColor: '#f00',
  2500. strokeWidth: 1,
  2501. name: 'debug_circle'
  2502. })
  2503. this.paper_main_object.addChild(debugcircle)
  2504. }
  2505. },
  2506. render() {
  2507. // console.log('render()', this.ctx);
  2508. },
  2509. }
  2510. </script>