ConcernementMapItem.vue 117 KB

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