ConcernementMapItem.vue 125 KB

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