ConcernementMapItem.vue 133 KB

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