ConcernementMapItem.vue 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895
  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. });
  909. let title = entite.entite.title;
  910. title = (title.length > 25) ? title.slice(0, 25 - 1) + '…' : title;
  911. let label_txt = new paper.PointText({
  912. pivot: pos_point,
  913. point: label_pos,
  914. content: title,
  915. fillColor: 'black',
  916. fontFamily: 'public_sans',
  917. fontSize: 4,
  918. justification: 'left',
  919. name: 'label_txt'
  920. })
  921. let b = label_txt.bounds;
  922. let label_bg = new paper.Path.Rectangle(b.x - pad, b.y - pad, b.width + pad*2, b.height + pad*2);
  923. label_bg.fillColor = '#fff';
  924. label_bg.opacity = 0.9;
  925. label_bg.name = 'label_bg';
  926. label.addChild(label_bg);
  927. label_txt.insertAbove(label_bg)
  928. // label.sendToBack();
  929. g_label.addChild(label);
  930. }
  931. }
  932. return g;
  933. },
  934. setPaperEntitesSuperposees(){
  935. // console.log('setPaperSuperpositions, superpositions', this.concernement.superpositions);
  936. let g = new paper.Group({
  937. pivot: new paper.Point(this.pos),
  938. name: 'entites_superposes'
  939. });
  940. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  941. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  942. if (entite.entite // check if we have an entite object with all the contents
  943. && this.superposedEntiteID === entite.entite.id) // check if entite id
  944. {
  945. // console.log(`entite ${entite.entite.id}`, entite, entite.entite.superposition);
  946. // use paper symbol
  947. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  948. instance.name = 'entite';
  949. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  950. // instance.scale(this.scale);
  951. instance.scale(3);
  952. instance.fillColor = '#000';
  953. instance.item_id = entite.entite.id;
  954. instance.item_type = 'entite_superpose';
  955. instance.is_symbol_instance = true;
  956. g.addChild(instance)
  957. break; // there is only one superposed entite by mapitem
  958. }
  959. }
  960. return g;
  961. },
  962. setPaperEntitesProximite(){
  963. let g = new paper.Group({
  964. pivot: new paper.Point(this.pos),
  965. name: 'entites_proximites'
  966. });
  967. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  968. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  969. if (entite.entite // check if we have an entite object with all the contents
  970. && entite.entite.proximite.length ) // check if entite id is in the list builded above
  971. {
  972. // console.log(`entite ${entite.entite.id}`, entite, entite.entite.superposition);
  973. // use paper symbol
  974. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  975. instance.name = 'entite';
  976. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  977. // instance.scale(this.scale);
  978. instance.scale(3);
  979. instance.fillColor = '#000';
  980. instance.item_id = entite.entite.id;
  981. instance.item_type = 'entite_proximite';
  982. instance.is_symbol_instance = true;
  983. g.addChild(instance)
  984. }
  985. }
  986. return g;
  987. },
  988. setPaperEntitesProximiteReferences(){
  989. console.log('setPaperEntitesProximiteReferences')
  990. let g = new paper.Group({
  991. pivot: new paper.Point(this.pos),
  992. name: 'entites_proximites_references'
  993. });
  994. // loop through all concernement's entites
  995. // keeping only those who have proximite
  996. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  997. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  998. if (entite.entite // check if we have an entite object with all the contents
  999. && entite.entite.proximite.length ) // check if entite id is in the list builded above
  1000. {
  1001. // console.log(`PROXIMITE entite ${entite.entite.id}`, entite, entite.entite.proximite);
  1002. // create the main entite paper point object
  1003. let entite_pos = {
  1004. x: this.pos.x + entite.display.pos.x * this.scale,
  1005. y: this.pos.y + entite.display.pos.y * this.scale
  1006. }
  1007. // console.log(`entite_pos x:${entite_pos.x}, y:${entite_pos.y}`);
  1008. // use paper symbol
  1009. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  1010. instance.name = 'entite';
  1011. instance.position = new paper.Point([entite_pos.x, entite_pos.y]);
  1012. // instance.scale(this.scale);
  1013. instance.scale(this.scale);
  1014. instance.fillColor = '#000';
  1015. instance.item_id = entite.entite.id;
  1016. instance.item_type = 'entite_proximite';
  1017. instance.is_symbol_instance = true;
  1018. g.addChild(instance)
  1019. // create the proximite reference of the main entite
  1020. // a paper group wich contains the ref entite point and the ref concernement's contour
  1021. let ref_g = new paper.Group({
  1022. pivot: new paper.Point(this.pos),
  1023. name: 'ref_entite_proximite'
  1024. });
  1025. let allEntiteRefConcernementContourEntites = [];
  1026. let beta = 360 / entite.entite.proximite.length;
  1027. let ray = 5;
  1028. let e=0;
  1029. // define the right scaling down of the big original concernement contours
  1030. let scaledown = 0.05;
  1031. entite.entite.proximite.forEach(entite_ref => {
  1032. // console.log(`${entite_ref.id}, ${entite_ref.title}`, this.allEntitesById[entite_ref.id]);
  1033. // get the concernement parents of the proximite reference
  1034. let ref_cid = this.allEntitesById[entite_ref.id].cid;
  1035. // console.log('PROXIMITE ref concernement jarvis_envelope_convexe', this.concernementsByID[ref_cid].sailentEntites);
  1036. let ref_concernement = this.concernementsByID[ref_cid];
  1037. // create the entite ref paper point
  1038. let ref_instance = new paper.SymbolItem(this.paper_symbol_definitions['entite']);
  1039. ref_instance.name = 'entite_ref';
  1040. let entite_ref_pos = {
  1041. 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
  1042. 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
  1043. }
  1044. // console.log(`entite_ref_pos x:${entite_ref_pos.x}, y:${entite_ref_pos.y}`);
  1045. ref_instance.position = new paper.Point(entite_ref_pos);
  1046. // ref_instance.scale(this.scale);
  1047. ref_instance.scale(this.scale*0.75); // met a l'échelle la taille du point et la diminue
  1048. ref_instance.fillColor = '#000';
  1049. ref_instance.item_id = entite_ref.id;
  1050. ref_instance.item_type = 'entite_proximite_reference';
  1051. ref_instance.ref_cid = ref_cid;
  1052. ref_instance.parent_eid = entite.entite.id;
  1053. ref_instance.is_symbol_instance = true;
  1054. ref_g.addChild(ref_instance)
  1055. // create the original concernement contours arround the entite_ref
  1056. let points = [];
  1057. ref_concernement.sailentEntites.forEach(ent => {
  1058. points.push({
  1059. 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
  1060. 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
  1061. })
  1062. // - - //
  1063. // concat all the contours points for general contours arround all contours :/ :( (that's a very clear description)
  1064. let p = {
  1065. x: entite_ref_pos.x - entite_pos.x + (ent.display.ray * Math.cos(ent.display.alpha * (Math.PI/180))) * this.scale,
  1066. y: entite_ref_pos.y - entite_pos.y + (ent.display.ray * Math.sin(ent.display.alpha * (Math.PI/180))) * this.scale
  1067. }
  1068. // console.log(`p x:${p.x}, y:${p.y}`);
  1069. allEntiteRefConcernementContourEntites.push({
  1070. display:{
  1071. ray: Math.sqrt(Math.pow(p.x,2)+Math.pow(p.y,2)),
  1072. alpha: parseFloat(Math.atan2(p.y,p.x) * (180/Math.PI)).toFixed(2)
  1073. }
  1074. })
  1075. // - - //
  1076. })
  1077. // convert points to rouded and padded segments props
  1078. let segments = this.getPaddedRoundedSegments(points, scaledown)
  1079. // create "real" Paper Segments from previous segments props
  1080. let paper_segments = [];
  1081. segments.forEach(seg => {
  1082. let paddedSegPoint = {
  1083. x: entite_ref_pos.x+seg.point[0]*scaledown,
  1084. y: entite_ref_pos.y+seg.point[1]*scaledown
  1085. }
  1086. // console.log('paddedSegPoint', paddedSegPoint);
  1087. paper_segments.push(new paper.Segment({
  1088. point: paddedSegPoint,
  1089. handleIn: seg.handleIn,
  1090. handleout: seg.handleOut
  1091. }))
  1092. // - - //
  1093. // concat all the contours points for general contours arround all contours :/ :( (that's a very clear description)
  1094. // let p = {
  1095. // x: (entite_ref_pos.x - entite_pos.x + (ent.display.ray * Math.cos(ent.display.alpha * (Math.PI/180)))),
  1096. // y: (entite_ref_pos.y - entite_pos.y + (ent.display.ray * Math.sin(ent.display.alpha * (Math.PI/180))))
  1097. // }
  1098. // console.log(`p x:${p.x}, y:${p.y}`);
  1099. // allEntiteRefConcernementContourEntites.push({
  1100. // display:{
  1101. // ray: Math.sqrt(Math.pow(paddedSegPoint.x,2)+Math.pow(paddedSegPoint.y,2)),
  1102. // alpha: parseFloat(Math.atan2(paddedSegPoint.y,paddedSegPoint.x) * (180/Math.PI)).toFixed(2)
  1103. // }
  1104. // })
  1105. // - - //
  1106. });
  1107. // create the paper path with previous segments
  1108. const contrs = new paper.Path({
  1109. name: 'contours',
  1110. segments: paper_segments,
  1111. fillColor: 'rgba(255,255,255,0.4)',
  1112. // selected: true,
  1113. strokeColor: '#fff',
  1114. strokeWidth: 1,
  1115. pivot: new paper.Point(this.pos),
  1116. cid: ref_concernement.id
  1117. });
  1118. ref_g.addChild(contrs);
  1119. contrs.sendToBack();
  1120. ref_instance.contour_paper_item_id = contrs.id;
  1121. e++;
  1122. }); // end of loop on proximite for each main entite
  1123. // general contour arround proximité
  1124. // console.log('allEntiteRefConcernementContourEntites', allEntiteRefConcernementContourEntites);
  1125. let genContoursEntite = this.getJarvisEnvelopeConvexeEntites(allEntiteRefConcernementContourEntites);
  1126. // console.log('genContoursEntite', genContoursEntite);
  1127. let points = [];
  1128. genContoursEntite.forEach(ent => {
  1129. points.push({
  1130. x: ent.display.ray * Math.cos(ent.display.alpha * (Math.PI/180)),
  1131. y: ent.display.ray * Math.sin(ent.display.alpha * (Math.PI/180))
  1132. })
  1133. })
  1134. // console.log('points', points);
  1135. let gen_scaledown = 0.16;
  1136. // scaledown = 1;
  1137. // convert points to rouded and padded segments props
  1138. let segments = this.getPaddedRoundedSegments(points, gen_scaledown)
  1139. // create "real" Paper Segments from previous segments props
  1140. let paper_segments = [];
  1141. segments.forEach(seg => {
  1142. paper_segments.push(new paper.Segment({
  1143. point: [entite_pos.x+seg.point[0]*gen_scaledown, entite_pos.y+seg.point[1]*gen_scaledown],
  1144. // point: [seg.point[0], seg.point[1]],
  1145. handleIn: seg.handleIn,
  1146. handleout: seg.handleOut
  1147. }))
  1148. });
  1149. // create the paper path with previous segments
  1150. const genContrs = new paper.Path({
  1151. name: 'gen_contours',
  1152. segments: paper_segments,
  1153. fillColor: 'rgba(255,255,255,0.4)',
  1154. // selected: true,
  1155. strokeColor: '#fff',
  1156. strokeWidth: 1,
  1157. pivot: new paper.Point(this.pos),
  1158. // cid: this.cid
  1159. });
  1160. ref_g.addChild(genContrs);
  1161. genContrs.sendToBack();
  1162. g.addChild(ref_g);
  1163. ref_g.sendToBack();
  1164. } // end of loop on entite wich have proximite
  1165. }
  1166. return g;
  1167. },
  1168. setPaperAgissantes(){
  1169. console.log('setPaperAgissantes');
  1170. let g = new paper.Group({
  1171. pivot: new paper.Point(this.pos),
  1172. name: 'agissantes'
  1173. });
  1174. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  1175. let entite = this.concernement.revisions_byid[this.concernement.revision_id].entites[i];
  1176. // console.log('agissantes', entite);
  1177. if (entite.entite && entite.entite.agissante) {
  1178. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action']);
  1179. instance.name = 'entite_action';
  1180. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  1181. instance.fillColor = '#000';
  1182. // instance.scale(0.2);
  1183. instance.scale(this.scale);
  1184. instance.item_id = entite.entite.id;
  1185. instance.item_type = 'entite_action';
  1186. instance.is_symbol_instance = true;
  1187. g.addChild(instance)
  1188. }
  1189. }
  1190. return g;
  1191. },
  1192. // setPaperAgissantesIcons(){
  1193. // let g = new paper.Group({
  1194. // pivot: new paper.Point(this.pos),
  1195. // name: 'agissantes_icons'
  1196. // });
  1197. // for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  1198. // let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  1199. // if (entite.entite && entite.entite.agissante) {
  1200. // let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action_icon']);
  1201. // instance.name = 'entite_action';
  1202. // instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  1203. // instance.fillColor = '#000';
  1204. // // instance.scale(0.2);
  1205. // instance.scale(this.scale);
  1206. // instance.item_id = entite.entite.id;
  1207. // instance.item_type = 'entite_action';
  1208. // instance.is_symbol_instance = true;
  1209. // g.addChild(instance)
  1210. // }
  1211. // }
  1212. // return g;
  1213. // },
  1214. setPaperPuissanceagirBesoins(){
  1215. let g = new paper.Group({
  1216. pivot: new paper.Point(this.pos),
  1217. name: 'puissanceagir_besoins'
  1218. });
  1219. let res_fields = ['qui','quoi','ou','avec'];
  1220. let arc = (360 / 16); // unit arc
  1221. let r = (this.ray * this.scale)/5; // unit ray
  1222. let br = r - r/3; // besoin ray
  1223. for (let i = 0; i < this.concernement.besoins.length; i++) {
  1224. let start_a = arc * i; // angle depart (for reponses)
  1225. let center_a = start_a + arc/2; // angle central
  1226. let x = Math.cos(center_a*(Math.PI/180)) * br;
  1227. let y = Math.sin(center_a*(Math.PI/180)) * br;
  1228. // use paper symbol
  1229. let besoin = new paper.SymbolItem(this.paper_symbol_definitions['besoin']);
  1230. besoin.position = new paper.Point([this.pos.x + x, this.pos.y + y]);
  1231. besoin.scale(this.scale);
  1232. besoin.item_id = this.concernement.besoins[i].id;
  1233. besoin.item_cid = this.concernement.id;
  1234. besoin.item_type = 'besoin';
  1235. besoin.is_symbol_instance = true;
  1236. console.log(`paper besoin id:${besoin.id}`);
  1237. this.setBesoinPaperId(besoin.id, this.cid, this.concernement.besoins[i].id);
  1238. g.addChild(besoin)
  1239. let res_arc = arc / (1 + this.concernement.besoins[i].reponses.length); // unit arc for responses depending responses number
  1240. for (let j = 0; j < this.concernement.besoins[i].reponses.length; j++) {
  1241. let res_a = start_a + res_arc * (j+1); // angle for response line
  1242. for (let f = 0; f < res_fields.length; f++) { // loop through all 4 fields, keep only the last one filled
  1243. if( this.concernement.besoins[i].reponses[j][res_fields[f]] // if field filled
  1244. && (f === res_fields.length -1 || !this.concernement.besoins[i].reponses[j][res_fields[f+1]]) // and is last field or last field filled
  1245. ){
  1246. let rr = this.ray * this.scale - r*f - r/2; // reponse field ray
  1247. let rx = Math.cos(res_a*(Math.PI/180)) * rr;
  1248. let ry = Math.sin(res_a*(Math.PI/180)) * rr;
  1249. // use paper symbol
  1250. let reponse = new paper.SymbolItem(this.paper_symbol_definitions['reponse']);
  1251. reponse.position = new paper.Point([this.pos.x + rx, this.pos.y + ry]);
  1252. reponse.scale(this.scale);
  1253. reponse.item_id = this.concernement.besoins[i].reponses[j].id;
  1254. reponse.item_bid = this.concernement.besoins[i].id;
  1255. reponse.item_cid = this.concernement.id;
  1256. reponse.item_type = 'reponse';
  1257. reponse.is_symbol_instance = true;
  1258. this.setBesoinPaperId(reponse.id, this.cid, this.concernement.besoins[i].id, this.concernement.besoins[i].reponses[j].id);
  1259. g.addChild(reponse)
  1260. }
  1261. }
  1262. }
  1263. }
  1264. return g;
  1265. },
  1266. setPaperDoleances(){
  1267. let g = new paper.Group({
  1268. pivot: new paper.Point({x:0,y:0}),
  1269. name: `doleances`
  1270. });
  1271. this.concernement.doleances.forEach((d) => {
  1272. g.addChild(this.setPaperDoleanceSteps(d))
  1273. });
  1274. return g;
  1275. },
  1276. setPaperDoleanceSteps(doleance){
  1277. let g = new paper.Group({
  1278. pivot: new paper.Point({x:0,y:0}),
  1279. name: `doleance_${doleance.id}`,
  1280. doleance_id: doleance.id,
  1281. visible: false
  1282. });
  1283. // let doleance = this.concernement.doleances[0];
  1284. let all_fields = [
  1285. [
  1286. 'leprobleme',
  1287. 'lenquete',
  1288. {
  1289. fieldname: 'groupesinterets',
  1290. fields: [
  1291. 'groupe_interets',
  1292. 'accorder_interets',
  1293. 'formuler',
  1294. ]
  1295. }
  1296. ],
  1297. [
  1298. 'entite_addresse_doleance',
  1299. 'comment_ennonce_doleance',
  1300. 'aqui_addresse_doleance',
  1301. {
  1302. fieldname: 'reception_traitement',
  1303. fields: [
  1304. 'entite_adressee',
  1305. 'doleance_formulee',
  1306. 'traite_doleance',
  1307. 'entite_recoit_doleance',
  1308. ]
  1309. }
  1310. ],
  1311. [
  1312. 'entites_decisionnaires',
  1313. 'decision_formule',
  1314. {
  1315. fieldname: 'mise_en_oeuvre_decision',
  1316. fields: [
  1317. 'entite_adresse_decision',
  1318. 'formule_decision',
  1319. 'entite_metenoeuvre_decisio',
  1320. ]
  1321. }
  1322. ],
  1323. [
  1324. 'entite_adresse_application',
  1325. 'aqui_adresse_decision',
  1326. 'comment_formule_decision',
  1327. {
  1328. fieldname: 'receptions_et_applications',
  1329. fields: [
  1330. 'applique_decision',
  1331. 'formule_decision_applic',
  1332. 'entite_recoit_decision',
  1333. ]
  1334. },
  1335. 'probleme_initial_resolu',
  1336. 'oui_nouvelle_situation',
  1337. 'non_adresse_doleance',
  1338. ]
  1339. ];
  1340. var r = this.ray * this.scale * 0.8; // ray
  1341. var dr = r/2; // demi ray
  1342. // var pcr = 2*this.scale; // petits cercle rayon
  1343. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  1344. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  1345. // radians = degrees * (pi/180)
  1346. // degrees = radians * (180/pi)
  1347. // Points for 45° axes
  1348. let m = Math.sin(45*(Math.PI/180)) * r; // x = y for rayon
  1349. let n = Math.sin(45*(Math.PI/180)) * r/2; // x = y for demi rayon
  1350. let o = Math.cos(22.5*(Math.PI/180)) * r; // x @ 22.5° for rayon
  1351. let p = Math.sin(22.5*(Math.PI/180)) * r; // y @ 22.5° for rayon
  1352. let o_d = Math.cos(22.5*(Math.PI/180)) * r/2; // x @ 22.5° for demi rayon
  1353. let p_d = Math.sin(22.5*(Math.PI/180)) * r/2; // y @ 22.5° for demi rayon
  1354. let step_Style = {
  1355. strokeColor: '#fff',
  1356. strokeWidth: 2,
  1357. fillColor: "rgba(255, 255, 255, 0.3)"
  1358. }
  1359. // let fontsize = 4;
  1360. //
  1361. // CAMENBERT STATIQUES
  1362. //
  1363. // l'enquete
  1364. //
  1365. // camenbert
  1366. if (doleance.lenquete) {
  1367. let cam = new paper.Path({...step_Style, ...{
  1368. item_type: 'doleance_step',
  1369. item_id: `lenquete`,
  1370. did: doleance.id,
  1371. field: 'lenquete',
  1372. field_index: null
  1373. }
  1374. });
  1375. cam.add({x: this.pos.x , y: this.pos.y + dr});
  1376. cam.lineTo({x: this.pos.x, y: this.pos.y + r});
  1377. cam.arcTo({x: this.pos.x - p, y: this.pos.y + o}, {x: this.pos.x - m, y: this.pos.y + m});
  1378. cam.lineTo({x: this.pos.x - n, y: this.pos.y + n});
  1379. cam.arcTo({x: this.pos.x - p_d, y: this.pos.y + o_d}, {x: this.pos.x , y: this.pos.y + dr});
  1380. // texte
  1381. // enquete.addChild(new paper.PointText({
  1382. // point: {
  1383. // x:this.pos.x + Math.sin(22.5*(Math.PI/180)) * r*0.75,
  1384. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  1385. // },
  1386. // content: doleance['lenquete'],
  1387. // fontSize: fontsize,
  1388. // fillColor: '#fff',
  1389. // justification: 'center',
  1390. // locked: true
  1391. // }));
  1392. g.addChild(cam);
  1393. }
  1394. //
  1395. // probleme_initial_resolu
  1396. //
  1397. // camenbert
  1398. if(doleance.probleme_initial_resolu === 0 || doleance.probleme_initial_resolu === 1){
  1399. let rescam = new paper.Path({...step_Style, ...{
  1400. closed: true,
  1401. item_type: 'doleance_step',
  1402. item_id: `probleme_initial_resolu`,
  1403. did: doleance.id,
  1404. field: 'probleme_initial_resolu',
  1405. field_index: null
  1406. }
  1407. });
  1408. rescam.add({x: this.pos.x + n, y: this.pos.y + n});
  1409. rescam.lineTo({x: this.pos.x + m , y: this.pos.y + m});
  1410. rescam.arcTo({x: this.pos.x + p, y: this.pos.y + o}, {x: this.pos.x, y: this.pos.y + r});
  1411. rescam.lineTo({x: this.pos.x, y: this.pos.y + dr});
  1412. rescam.arcTo({x: this.pos.x + p_d, y: this.pos.y + o_d}, {x: this.pos.x + n, y: this.pos.y + n});
  1413. // texte
  1414. // resolution.addChild(new paper.PointText({
  1415. // point: {
  1416. // x:this.pos.x - Math.sin(22.5*(Math.PI/180)) * r*0.75,
  1417. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  1418. // },
  1419. // content: doleance['probleme_initial_resolu'] ? doleance['oui_nouvelle_situation'] : doleance['non_adresse_doleance'],
  1420. // fontSize: fontsize,
  1421. // fillColor: '#fff',
  1422. // justification: 'center'
  1423. // }));
  1424. g.addChild(rescam)
  1425. }
  1426. //
  1427. // MULTIPLE FIELDS
  1428. //
  1429. let multiple_fields = [
  1430. { field_name: 'groupesinterets', arc: 45, decalage: -45 },
  1431. { field_name: 'reception_traitement', arc: 90, decalage: -90 },
  1432. { field_name: 'mise_en_oeuvre_decision', arc: 90, decalage: -180 },
  1433. { field_name: 'receptions_et_applications', arc: 45, decalage: 90 }
  1434. ]
  1435. multiple_fields.forEach((mf, j) => {
  1436. for (let i = 0, l = doleance[mf.field_name].length, a = mf.arc/l; i < l; i++) {
  1437. // let gi = new paper.Group({
  1438. // item_type: 'doleance_step',
  1439. // item_id: 'lenquete',
  1440. // item_field: mf.field_name,
  1441. // item_index: i
  1442. // });
  1443. //camenbert
  1444. let x1= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * dr,
  1445. y1= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * dr;
  1446. let x2= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * r,
  1447. y2= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * r;
  1448. let x3= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * r,
  1449. y3= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * r;
  1450. let x3t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r,
  1451. y3t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r;
  1452. let x4= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * dr,
  1453. y4= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * dr;
  1454. let x4t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr,
  1455. y4t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr;
  1456. let x5= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75,
  1457. y5= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75;
  1458. let p = new paper.Path({...step_Style, ...{
  1459. item_type: 'doleance_step',
  1460. item_id: `${mf.field_name}-${i}`,
  1461. did: doleance.id,
  1462. field: mf.field_name,
  1463. field_index: i
  1464. }
  1465. });
  1466. p.add([x1,y1]);
  1467. p.lineTo([x2,y2]);
  1468. p.arcTo([x3t,y3t], [x3,y3]);
  1469. p.lineTo([x4,y4]);
  1470. p.arcTo([x4t,y4t], [x1,y1]);
  1471. g.addChild(p);
  1472. // gi.addChild(p);
  1473. // // text
  1474. // gi.addChild(new paper.PointText({
  1475. // point: {x:x5,y:y5},
  1476. // content: `${j}-${i}`,
  1477. // fontSize: fontsize,
  1478. // fillColor: '#fff',
  1479. // justification: 'center'
  1480. // }))
  1481. // g.addChild(gi)
  1482. }
  1483. });
  1484. // POINTS CARDINAUX
  1485. let cardinal_Style = {
  1486. strokeColor: '#fff',
  1487. strokeWidth: 2,
  1488. fillColor: "rgba(255, 255, 255, 0.9)"
  1489. }
  1490. // leprobleme
  1491. if (doleance.leprobleme) {
  1492. g.addChild(new paper.Path.Circle({
  1493. center: [this.pos.x, this.pos.y + r],
  1494. radius: 3 * this.scale,
  1495. style: cardinal_Style,
  1496. item_type: 'doleance_step',
  1497. item_id: `leprobleme`,
  1498. did: doleance.id,
  1499. field: 'leprobleme'
  1500. }));
  1501. }
  1502. // adresse de la doleance
  1503. if (doleance.entite_addresse_doleance || doleance.aqui_addresse_doleance || doleance.comment_ennonce_doleance) {
  1504. g.addChild(new paper.Path.Circle({
  1505. center: [this.pos.x - r, this.pos.y],
  1506. radius: 3 * this.scale,
  1507. style: cardinal_Style,
  1508. item_type: 'doleance_step',
  1509. item_id: `adresse_de_la_doleance`,
  1510. did: doleance.id,
  1511. field: 'adresse_de_la_doleance'
  1512. }));
  1513. }
  1514. // decision
  1515. if (doleance.entites_decisionnaires || doleance.decision_formule) {
  1516. g.addChild(new paper.Path.Circle({
  1517. center: [this.pos.x, this.pos.y - r],
  1518. radius: 3 * this.scale,
  1519. style: cardinal_Style,
  1520. item_type: 'doleance_step',
  1521. item_id: `decision`,
  1522. did: doleance.id,
  1523. field: 'decision'
  1524. }));
  1525. }
  1526. // adresse_de_la_decision
  1527. if (doleance.entite_adresse_application || doleance.aqui_adresse_decision || doleance.comment_formule_decision) {
  1528. g.addChild(new paper.Path.Circle({
  1529. center: [this.pos.x + r, this.pos.y],
  1530. radius: 3 * this.scale,
  1531. style: cardinal_Style,
  1532. item_type: 'doleance_step',
  1533. item_id: `adresse_de_la_decision`,
  1534. did: doleance.id,
  1535. field: 'adresse_de_la_decision'
  1536. }));
  1537. }
  1538. return g;
  1539. },
  1540. // mapitem visibility
  1541. handleMapitemVisibility(state){
  1542. this.is_visible = state;
  1543. this.paper_main_object.visible = this.is_visible;
  1544. },
  1545. // PAPER VISIBILITY
  1546. handlePaperVisibilityOnBeforeOpen(){
  1547. // // agissantes
  1548. // if (this.concernement.has_agissantes && this.map_mode === "action") {
  1549. // if (!this.is_open) {
  1550. // this.paper_main_object.children.agissantes_icons.visible = true;
  1551. // } else {
  1552. // this.paper_main_object.children.agissantes_icons.visible = false;
  1553. // }
  1554. // }
  1555. // superposition
  1556. // scale down superposed entites on open
  1557. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1558. if(entites_superposes){
  1559. for(let paper_item of entites_superposes.children) {
  1560. paper_item.scale(0.25)
  1561. }
  1562. }
  1563. // proximite
  1564. // scale down proximite entites on open
  1565. let entites_proximites = this.paper_main_object.children['entites_proximites'];
  1566. if(entites_proximites){
  1567. for(let paper_item of entites_proximites.children) {
  1568. paper_item.scale(0.25)
  1569. }
  1570. }
  1571. },
  1572. handlePaperVisibilityOnOpened(){},
  1573. handlePaperVisibilityOnBeforeClose(){
  1574. // superposition
  1575. // scale up superposed entites on open
  1576. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1577. if(entites_superposes){
  1578. for(let paper_item of entites_superposes.children) {
  1579. paper_item.scale(4)
  1580. }
  1581. }
  1582. // proximites
  1583. // scale up proximites entites on open
  1584. let entites_proximites = this.paper_main_object.children['entites_proximites'];
  1585. if(entites_proximites){
  1586. for(let paper_item of entites_proximites.children) {
  1587. paper_item.scale(4)
  1588. }
  1589. }
  1590. },
  1591. handlePaperVisibilityOnClosed(){
  1592. // // agissantes
  1593. // if (this.concernement.has_agissantes && this.map_mode === "action") {
  1594. // if (!this.is_open) {
  1595. // this.paper_main_object.children.agissantes_icons.visible = true;
  1596. // } else {
  1597. // this.paper_main_object.children.agissantes_icons.visible = false;
  1598. // }
  1599. // }
  1600. },
  1601. handlePaperVisibilityOnAfterEnginUpdate(){
  1602. // contours focused
  1603. if (!this.isFocused()){
  1604. 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)"; //
  1605. }else{
  1606. 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)"; //
  1607. if (this.is_hover) {
  1608. this.paper_main_object.children['contours'].strokeColor = "#01ffe2";
  1609. this.paper_main_object.children['contours'].strokeWidth = 2;
  1610. }else{
  1611. this.paper_main_object.children['contours'].strokeColor = "#fff";
  1612. this.paper_main_object.children['contours'].strokeWidth = 1;
  1613. }
  1614. }
  1615. // contours visibility
  1616. if (!this.is_open
  1617. || (this.is_open && this.map_mode !== "puissancedagir" && this.map_mode !== "doleancer")) {
  1618. this.paper_main_object.children['contours'].visible = true;
  1619. } else {
  1620. this.paper_main_object.children['contours'].visible = false;
  1621. }
  1622. // proximite
  1623. if (this.concernement.has_proximites) {
  1624. if (this.map_mode === "proximite") {
  1625. this.paper_main_object.children.entites_proximites.visible = true;
  1626. }else{
  1627. this.paper_main_object.children.entites_proximites.visible = false;
  1628. }
  1629. }
  1630. // superposition
  1631. if (this.concernement.has_superpositions) {
  1632. if (this.map_mode === "superposition") {
  1633. this.paper_main_object.children.entites_superposes.visible = true;
  1634. // highlight of entite part of opened superposition
  1635. let group = this.paper_main_object.children['entites_superposes'];
  1636. if(group){
  1637. if (this.$route.query.superposition_id) {
  1638. let superposition_id = this.$route.query.superposition_id;
  1639. if(superposition_id){
  1640. // let ids = superposition_id.match(/(\d+)_(\d+)__(\d+)_(\d+)/i)
  1641. // group.children.forEach((item) => {
  1642. // if ( (this.cid === parseInt(ids[1]) && item.item_id === parseInt(ids[2]))
  1643. // || (this.cid === parseInt(ids[3]) && item.item_id === parseInt(ids[4])) ) {
  1644. // item.definition = this.paper_symbol_definitions.entite_hover;
  1645. // if(!item.focused){
  1646. // item.scale(2)
  1647. // item.focused = true;
  1648. // }
  1649. // } else if(!this.is_hover) {
  1650. // item.definition = this.paper_symbol_definitions.entite;
  1651. // if (item.focused) {
  1652. // item.scale(0.5)
  1653. // item.focused = false;
  1654. // }
  1655. // }
  1656. // })
  1657. }
  1658. }else{
  1659. group.children.forEach((item) => {
  1660. if(!this.is_hover) {
  1661. item.definition = this.paper_symbol_definitions.entite;
  1662. if (item.focused) {
  1663. item.scale(0.5)
  1664. item.focused = false;
  1665. }
  1666. }
  1667. })
  1668. }
  1669. }
  1670. } else {
  1671. this.paper_main_object.children.entites_superposes.visible = false;
  1672. }
  1673. }
  1674. // // puissance d'agir
  1675. // if (this.concernement.has_puissancedagir) {
  1676. // if (this.map_mode === "puissancedagir") {
  1677. // if (!this.is_open) {
  1678. // this.paper_main_object.children.puissanceagir_icon.visible = true; // if not opened and has_puissancedagir draw the puissance d'agir icone
  1679. // } else {
  1680. // this.paper_main_object.children.puissanceagir_icon.visible = false;
  1681. // }
  1682. // } else {
  1683. // this.paper_main_object.children.puissanceagir_icon.visible = false;
  1684. // }
  1685. // }
  1686. // agissantes
  1687. // if (this.concernement.has_agissantes) {
  1688. // if (this.map_mode !== "action") {
  1689. // this.paper_main_object.children.agissantes_icons.visible = false;
  1690. // } else if(!this.is_open && !this.is_closing){
  1691. // this.paper_main_object.children.agissantes_icons.visible = true;
  1692. // }
  1693. // }
  1694. // doleance
  1695. if (this.concernement.has_doleance) {
  1696. if (this.map_mode === "doleancer") {
  1697. if (!this.is_open) {
  1698. // this.paper_main_object.children.doleance_icon.visible = true;
  1699. } else {
  1700. // this.paper_main_object.children.doleance_icon.visible = false;
  1701. // display the right (opened) doleance
  1702. let from = null
  1703. let to = this.concernement.opened_doleance.id;
  1704. this.concernement.doleances.forEach((d) => {
  1705. // this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible = d.id === this.concernement.opened_doleance.id;
  1706. if (this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible) {
  1707. from = d.id
  1708. }
  1709. })
  1710. // console.log(`opened_doleance from:${from}, to:${to}`);
  1711. if (from && from !== to) {
  1712. this.initDoleanceTransition(from, to);
  1713. } else if(!this.doleance_transition) {
  1714. this.concernement.doleances.forEach((d) => {
  1715. this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible = d.id === this.concernement.opened_doleance.id;
  1716. })
  1717. }
  1718. }
  1719. } else {
  1720. // this.paper_main_object.children.doleance_icon.visible = false;
  1721. }
  1722. }
  1723. // focus on opened entite
  1724. if (this.is_open && (this.map_mode === 'terraindevie' || this.map_mode === 'action')) {
  1725. let group = this.map_mode === 'terraindevie' ? this.paper_main_object.children['entites'] : this.paper_main_object.children['agissantes'];
  1726. if(group){
  1727. group.children.forEach((item) => {
  1728. if (this.opened_entite_id && item.item_id === parseInt(this.opened_entite_id) && item.item_type !== 'entite_hidden') {
  1729. item.definition = item.item_type === 'entite' ? this.paper_symbol_definitions.entite_hover : this.paper_symbol_definitions.entite_action_hover;
  1730. } else {
  1731. if ((!this.hover_elmt || item.item_id !== this.hover_elmt.id) && item.item_type !== 'entite_hidden') {
  1732. item.definition = item.item_type === 'entite' ? this.paper_symbol_definitions.entite : this.paper_symbol_definitions.entite_action;
  1733. }
  1734. }
  1735. })
  1736. }
  1737. }
  1738. },
  1739. initDoleanceTransition(from, to){
  1740. if (!this.doleance_transition) {
  1741. this.doleance_transition = true
  1742. // console.log(`initDoleanceTransition from:${from}, to:${to}`);
  1743. let from_pg = this.paper_main_object.children.doleances.children[`doleance_${from}`]
  1744. from_pg.visible = true;
  1745. from_pg.opacity = 1;
  1746. let to_pg = this.paper_main_object.children.doleances.children[`doleance_${to}`]
  1747. to_pg.visible = true;
  1748. to_pg.opacity = 0;
  1749. let bg = this.paper_main_object.children.doleance_bg;
  1750. // // fade out the current visible cercle politique then fade in the new one
  1751. new Tween.Tween({o: 1})
  1752. .to({
  1753. o: 0
  1754. }, 600)
  1755. .onUpdate((obj) => {
  1756. console.log(`tween update. o=${obj.o}`);
  1757. from_pg.opacity = bg.opacity = obj.o;
  1758. })
  1759. .onComplete((obj) => {
  1760. new Tween.Tween({o: 0})
  1761. .to({
  1762. o: 1
  1763. }, 900)
  1764. .onUpdate((objb) => {
  1765. to_pg.opacity = bg.opacity = objb.o;
  1766. })
  1767. .onComplete((obj) => {
  1768. from_pg.visible = false;
  1769. from_pg.opacity = 1;
  1770. to_pg.opacity = 1;
  1771. this.doleance_transition = false
  1772. })
  1773. .delay(500)
  1774. .easing(Tween.Easing.Quadratic.InOut)
  1775. .start();
  1776. })
  1777. .easing(Tween.Easing.Quadratic.Out)
  1778. .start();
  1779. }
  1780. },
  1781. onIsHover(){
  1782. // handle layer z-index
  1783. if (this.is_hover) {
  1784. this.paper_main_object.bringToFront();
  1785. } else {
  1786. // mapitem can be hover while an other mapitem is open if it is superposed (superposition) to the currently opened mapitem
  1787. // in this case don't send it back
  1788. if (!this.opened_concernement) {
  1789. this.paper_main_object.sendToBack();
  1790. }
  1791. }
  1792. // hightlight superposed entites on focus
  1793. let entites_superposes = this.paper_main_object.children['entites_superposes'];
  1794. // console.log('entites_superposes.children', entites_superposes.children);
  1795. if(entites_superposes){
  1796. for(let paper_item of entites_superposes.children) {
  1797. if (this.is_hover) {
  1798. paper_item.definition = this.paper_symbol_definitions.entite_hover;
  1799. // paper_item.scale(2)
  1800. } else {
  1801. paper_item.definition = this.paper_symbol_definitions.entite;
  1802. // paper_item.scale(0.5)
  1803. }
  1804. }
  1805. }
  1806. },
  1807. /*
  1808. * PAPER EVENTS
  1809. */
  1810. initPaperEvents(){
  1811. this.paper_main_object.onMouseLeave = function(event){
  1812. if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened
  1813. this.resetHoverElmt();
  1814. document.body.style.cursor = "auto";
  1815. }
  1816. if (this.is_open) {
  1817. document.body.style.cursor = "auto";
  1818. }
  1819. }.bind(this);
  1820. this.paper_main_object.onMouseMove = function(event){
  1821. console.log(`onmousemove ${this.id}`, this.cartouch_is_opened);
  1822. // prevent hover map item mouse event if cartouch is opened
  1823. if (this.cartouch_is_opened && this.map_mode !== "superposition") return;
  1824. if (!this.is_open) {
  1825. if (!this.opened_concernement) { // only if no concernement is opened and is this focused
  1826. if(this.isFocused()){
  1827. if (this.map_mode === 'superposition') {
  1828. // get the superposed entite
  1829. this.setHoverElmt({
  1830. type: 'superposition',
  1831. id: this.id,
  1832. cid: this.cid,
  1833. eid: this.superposedEntiteID
  1834. });
  1835. } else{
  1836. this.setHoverElmt({
  1837. type: 'concernement',
  1838. id: this.id,
  1839. cid: this.cid
  1840. });
  1841. }
  1842. document.body.style.cursor = "pointer";
  1843. }
  1844. } else if (this.map_mode === 'superposition'){
  1845. // if this mapitem is superposed to the opened mapitem
  1846. if(this.opened_concernement.all_superposed_concernements_id.indexOf(this.cid) >= 0){
  1847. this.setHoverElmt({
  1848. type: 'superposition',
  1849. id: this.id,
  1850. cid: this.cid,
  1851. eid: this.superposedEntiteID
  1852. });
  1853. document.body.style.cursor = "pointer";
  1854. }
  1855. }
  1856. } else {
  1857. // reset all proximity contrs stroke to white
  1858. if (this.paper_main_object.children['entites_proximites_references']) {
  1859. this.paper_main_object.children['entites_proximites_references'].getItems({name:'contours'}).forEach((contr) => {
  1860. contr.strokeColor = '#fff';
  1861. })
  1862. }
  1863. // lets define some options regarding the map_mode
  1864. let paper_group_tohit;
  1865. switch (this.map_mode) {
  1866. case "terraindevie":
  1867. paper_group_tohit = this.paper_main_object.children['entites'];
  1868. break;
  1869. case "proximite":
  1870. paper_group_tohit = this.paper_main_object.children['entites_proximites_references'];
  1871. break;
  1872. case "superposition":
  1873. paper_group_tohit = this.paper_main_object.children['entites_superposes'];
  1874. break;
  1875. case "action":
  1876. paper_group_tohit = this.paper_main_object.children['agissantes'];
  1877. break;
  1878. case "puissancedagir":
  1879. paper_group_tohit = this.paper_main_object.children['puissanceagir_besoins'];
  1880. break;
  1881. case "doleancer":
  1882. if(this.paper_main_object.children['doleances']){
  1883. paper_group_tohit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1884. }
  1885. break;
  1886. }
  1887. let result = paper_group_tohit ? paper_group_tohit.hitTest(event.point) : null;
  1888. // console.log('move result', result);
  1889. if (result && result.item.item_id) {
  1890. // console.log('move has result', result);
  1891. let new_hover_elmt = {
  1892. paper_id: result.item.id,
  1893. type: result.item.item_type,
  1894. id: result.item.item_id
  1895. };
  1896. switch (result.item.item_type) {
  1897. case "entite_proximite_reference":
  1898. new_hover_elmt.ref_cid = result.item.ref_cid;
  1899. new_hover_elmt.parent_eid = result.item.parent_eid;
  1900. // set the mouse overed contrs to blue
  1901. // contour_paper_item_id is recorded when creating the full paper objects in setPaperEntitesProximiteReferences()
  1902. let contrs_paper_id = result.item.contour_paper_item_id;
  1903. let contrs_paper_obj = result.item.parent.getItem({id: contrs_paper_id});
  1904. contrs_paper_obj.strokeColor = "#01ffe2";
  1905. break;
  1906. case "besoin":
  1907. new_hover_elmt.no_popup = true;
  1908. break;
  1909. case "reponse":
  1910. new_hover_elmt.bid = result.item.item_bid;
  1911. new_hover_elmt.cid = result.item.item_cid;
  1912. new_hover_elmt.no_popup = true;
  1913. break;
  1914. }
  1915. if (!this.hover_elmt || new_hover_elmt.paper_id !== this.hover_elmt.paper_id) {
  1916. // console.log(`before setHoverElmt ${this.id}`);
  1917. this.setHoverElmt(new_hover_elmt);
  1918. }
  1919. document.body.style.cursor = "pointer";
  1920. } else {
  1921. // console.log('move no result');
  1922. this.resetHoverElmt();
  1923. if (this.detailsZoomValue > 1) {
  1924. document.body.style.cursor = "grab";
  1925. } else {
  1926. document.body.style.cursor = "auto";
  1927. }
  1928. if (result && (result.item.name === 'label_txt' || result.item.name === 'label_bg')) {
  1929. result.item.parent.bringToFront();
  1930. }
  1931. }
  1932. }
  1933. }.bind(this);
  1934. this.paper_main_object.onClick = async function(event){
  1935. console.log('paper concernement onClick');
  1936. // prevent hover map item mouse event if cartouch is opened
  1937. if (this.cartouch_is_opened) return;
  1938. if (!this.is_open) { // si ce concernement n'est pas ouvet
  1939. console.log('mapitem is NOT opened');
  1940. if (!this.opened_concernement) { // si aucun concernement n'est ouvert
  1941. console.log(`Open me ${this.id}`);
  1942. // push route (keep the hash for map_mode)
  1943. // wait for routing to be finished before opening the mapItem
  1944. let params = {cid: parseInt(this.cid)}
  1945. let query = {mapitemid: this.id};
  1946. if (this.map_mode === "superposition") {
  1947. params.eid = this.superposedEntiteID
  1948. query.superposition_cluster_index = this.mapitem.superposition_cluster_index;
  1949. }
  1950. await this.$router.push({
  1951. name: 'concernement',
  1952. params: params,
  1953. query: query,
  1954. hash: `#${this.map_mode}`
  1955. });
  1956. // open/close all concernements
  1957. this.openCloseConcernements(this.cid, this.id)
  1958. // reset the mousehover
  1959. this.resetHoverElmt();
  1960. } else if (this.map_mode === 'superposition'){ // si un concernement est ouvert et qu'on est en mode superposition
  1961. // if this mapitem is superposed to the opened mapitem
  1962. if(this.opened_concernement.all_superposed_concernements_id.indexOf(this.cid) >= 0){
  1963. // push route (keep the hash for map_mode)
  1964. // wait for routing to be finished before opening the mapItem
  1965. console.log('click on superposed this.mapitem.superposition_cluster_index', this.mapitem.superposition_cluster_index);
  1966. await this.$router.push({
  1967. name: 'concernement',
  1968. params: {cid: this.opened_concernement.cid, eid: this.$route.params.eid},
  1969. query: {
  1970. mapitemid: this.opened_concernement.opened_mapitem_id,
  1971. superposition_cluster_index: this.mapitem.superposition_cluster_index,
  1972. superposed_cid: this.cid
  1973. },
  1974. hash: `#${this.map_mode}`
  1975. });
  1976. }
  1977. }
  1978. } else { // si ce concernement est ouvert
  1979. console.log('mapitem is opened');
  1980. // do not click if we were dragging
  1981. if (this.is_dragging) {
  1982. this.is_dragging = false;
  1983. return;
  1984. }
  1985. // lets define some options regarding the map_mode
  1986. let group_to_hit = null;
  1987. switch (this.map_mode) {
  1988. case "terraindevie":
  1989. group_to_hit = this.paper_main_object.children['entites'];
  1990. break;
  1991. case "proximite":
  1992. group_to_hit = this.paper_main_object.children['entites_proximites_references'];
  1993. break;
  1994. case "superposition":
  1995. group_to_hit = this.paper_main_object.children['entites_superposes'];
  1996. break;
  1997. case "action":
  1998. group_to_hit = this.paper_main_object.children['agissantes'];
  1999. break;
  2000. case "doleancer":
  2001. group_to_hit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  2002. break;
  2003. }
  2004. if (group_to_hit) {
  2005. let result = group_to_hit.hitTest(event.point);
  2006. console.log('click result', result);
  2007. if (result) {
  2008. switch (this.map_mode) {
  2009. case "terraindevie":
  2010. case "superposition":
  2011. case "action":
  2012. // we have clicked on an entite
  2013. this.$router.push({
  2014. name: 'concernement',
  2015. params: {cid: this.cid, eid: result.item.item_id},
  2016. query: {
  2017. mapitemid: this.id,
  2018. // superposition_id: this.mapitem.superposition_ids[0]
  2019. },
  2020. hash: `#${this.map_mode}`,
  2021. });
  2022. break;
  2023. case "proximite":
  2024. switch (result.item.item_type) {
  2025. case "entite_proximite":
  2026. this.$router.push({
  2027. name: 'concernement',
  2028. params: {cid: this.cid, eid: result.item.item_id},
  2029. hash: `#${this.map_mode}`,
  2030. });
  2031. break;
  2032. case "entite_proximite_reference":
  2033. this.$router.push({
  2034. name: 'concernement',
  2035. params: {cid: this.cid, eid: result.item.parent_eid},
  2036. query: {
  2037. proximite_cid: result.item.ref_cid,
  2038. proximite_eid: result.item.item_id
  2039. },
  2040. hash: `#${this.map_mode}`,
  2041. });
  2042. break;
  2043. }
  2044. case "doleancer":
  2045. this.setOpenedDoleanceField(this.cid, result.item.did, result.item.field, result.item.field_index);
  2046. break;
  2047. }
  2048. } else {
  2049. // otherwise we close the entite and come back to the concernement
  2050. this.$router.push({
  2051. name: 'concernement',
  2052. hash: `#${this.map_mode}`,
  2053. params: {id: this.cid, mapitemid: this.id}
  2054. });
  2055. // reset the mousehover
  2056. this.resetHoverElmt();
  2057. }
  2058. }
  2059. }
  2060. }.bind(this);
  2061. /*
  2062. * works i correlation with updateDetailsZoomScale()
  2063. */
  2064. this.paper_main_object.onMouseDrag = async function(event){
  2065. if (this.is_open && this.map_mode === 'terraindevie') {
  2066. event.stopPropagation();
  2067. event.preventDefault();
  2068. console.log('paper concernement onMouseDrag', event.delta);
  2069. if (this.detailsZoomValue > 1) {
  2070. let x = this.pos.x + event.delta.x;
  2071. let y = this.pos.y + event.delta.y;
  2072. this.pos = this.paper_main_object.position = this.constraint.pointA = {x: x, y: y};
  2073. Matter.Body.setPosition(this.body, this.pos);
  2074. this.is_dragging = true;
  2075. }
  2076. event.stop();
  2077. return false;
  2078. }
  2079. }.bind(this);
  2080. },
  2081. resetHoverElmt(){
  2082. // console.log('resetHoverElmt');
  2083. setTimeout(()=>{
  2084. this.setHoverElmt(null);
  2085. }, 100);
  2086. },
  2087. // OPEN / CLOSE (with tween)
  2088. getOpeningAvailableScale(){
  2089. // calcul opened size regarding window size and surounding contents
  2090. let header = document.querySelector('header#header');
  2091. let header_height = header.clientHeight;
  2092. let map_nav = document.querySelector('nav#map-nav');
  2093. let map_nav_height = map_nav.clientHeight;
  2094. let s_h = (this.canvas.height - header_height - map_nav_height) / (this.ray*2*1.15);
  2095. let s_w = (this.canvas.width - this.cartouch_width) / (this.ray*2*1.7);
  2096. return Math.min(s_h, s_w)
  2097. },
  2098. openClose(open) { // async
  2099. // await nextTick(); // not working
  2100. console.log(`ConcernementsMapItem ${this.id} openClose: ${open}`);
  2101. if (this.tween) {
  2102. this.tween.stop();
  2103. }
  2104. if (open) {
  2105. this.is_opening = true;
  2106. // create the paper objects to display (like entite, besoin, etc)
  2107. this.setPaperContents();
  2108. this.handlePaperVisibilityOnBeforeOpen();
  2109. let s = this.getOpeningAvailableScale();
  2110. // create once the opening tweening
  2111. this.tween = new Tween.Tween({s: this.scale, x: this.pos.x, y: this.pos.y, o: 0})
  2112. .to({
  2113. s: s,
  2114. x: (this.canvas.width - this.cartouch_width) / 2,
  2115. y: this.canvas.height / 2,
  2116. o: 0.8
  2117. }, 800)
  2118. .onUpdate((obj) => {
  2119. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  2120. // revert to the original size (by reverting the previous scale)
  2121. // Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  2122. this.paper_main_object.scale(1 / this.scale);
  2123. // then scale again to new scale
  2124. // Matter.Body.scale(this.body, obj.s, obj.s)
  2125. this.paper_main_object.scale(obj.s);
  2126. // update superposition constraints points positions
  2127. this.resetSuperpositionsConstraintsScaling(obj.s);
  2128. // record new scale
  2129. this.prev_scale = this.scale;
  2130. this.scale = this.mapitem.scale = obj.s;
  2131. this.details_zoom_scale = this.scale * this.detailsZoomValue;
  2132. this.opacity = obj.o;
  2133. // console.log('tween update obj.s', obj.s);
  2134. this.pos = {x:obj.x, y:obj.y};
  2135. Matter.Body.setPosition(this.body, this.pos);
  2136. })
  2137. .onComplete((obj) => {
  2138. // console.log('tween complete obj.s', obj.s);
  2139. // record tween one last time
  2140. this.prev_scale = this.scale = obj.s;
  2141. this.details_zoom_scale = this.scale * this.detailsZoomValue;
  2142. this.mapitem.scale = this.scale;
  2143. this.opacity = obj.o;
  2144. this.pos = {x:obj.x, y:obj.y};
  2145. Matter.Body.setPosition(this.body, this.pos);
  2146. this.handlePaperVisibilityOnOpened();
  2147. // fix the concernement position with a constraint
  2148. this.constraint = Matter.Constraint.create({
  2149. pointA: this.pos,
  2150. bodyB: this.body,
  2151. stiffness: 1,
  2152. damping: 0,
  2153. length: 0
  2154. });
  2155. Matter.Composite.add(this.matterEngine.world, [this.body, this.constraint]);
  2156. // paper bring to front
  2157. // this.paper_main_object.bringToFront();
  2158. this.paper_main_object.sendToBack();
  2159. this.is_opening = false;
  2160. });
  2161. // recreate the matter engine event to get it a the end of the events stack
  2162. Matter.Events.off(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  2163. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  2164. } else {
  2165. // CLOSING
  2166. this.is_closing = true;
  2167. if(this.constraint){
  2168. Matter.Composite.remove(this.matterEngine.world, this.constraint);
  2169. }
  2170. // this.concernement.active_revision = this.concernement.revision_id;
  2171. this.setActiveRevision(this.cid, this.concernement.revision_id);//this.concernement.revisions[this.concernement.revisions.length -1].revision_id);
  2172. this.resetPaperActiveRevision();
  2173. this.handlePaperVisibilityOnBeforeClose();
  2174. this.setOpenedEntiteId(null);
  2175. // this.setOpenedRecit(null);
  2176. // create once the closing tween
  2177. this.tween = new Tween.Tween({s: this.scale, o: 1})
  2178. .to({s: 1, o: 0}, 500)
  2179. .onUpdate((obj) => {
  2180. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  2181. // revert to the original size (by reverting the previous scale)
  2182. // INFO we dont need to sacle the matter body AND it may fix the montgolfière bug
  2183. // TODO find another way to increase the mass
  2184. // Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  2185. this.paper_main_object.scale(1 / this.scale);
  2186. // then scale again to new scale
  2187. // Matter.Body.scale(this.body, obj.s, obj.s)
  2188. this.paper_main_object.scale(obj.s);
  2189. // update superposition constraints points positions
  2190. if (this.concernement.superposition_constraints_id
  2191. && this.concernement.superposition_constraints_id[this.id]
  2192. && this.concernement.superposition_constraints_id[this.id].length)
  2193. {
  2194. // get all the constraints of the world
  2195. let all_constrains = Matter.Composite.allConstraints(this.matterEngine.world);
  2196. // loop through all constraint ids recorded in the concernement
  2197. // get only the constraint of this concernement mapitem
  2198. for(let constraint_id of this.concernement.superposition_constraints_id[this.id]) {
  2199. // get the right constraint object from matter
  2200. let constraint;
  2201. for (const c of all_constrains) {
  2202. if (c.id === constraint_id) {
  2203. constraint = c;
  2204. break;
  2205. }
  2206. }
  2207. if (constraint) {
  2208. // tween the constraint point
  2209. let ab = this.cid === constraint.concernementA.id ? 'A' : 'B';
  2210. let point = constraint[`point${ab}`];
  2211. // revert to the original point
  2212. let oripoint = Matter.Vector.create(point.x / this.scale, point.y / this.scale)
  2213. // newpoint from original point
  2214. let newpoint = Matter.Vector.create(oripoint.x * obj.s, oripoint.y * obj.s)
  2215. constraint[`point${ab}`] = newpoint;
  2216. }
  2217. }
  2218. }
  2219. // record new scale
  2220. this.prev_scale = this.scale;
  2221. this.scale = obj.s;
  2222. this.mapitem.scale = this.scale;
  2223. this.details_zoom_scale = this.scale * this.detailsZoomValue;
  2224. this.opacity = obj.o;
  2225. })
  2226. .onComplete((obj) => {
  2227. this.prev_scale = this.scale = 1;
  2228. this.mapitem.scale = this.scale;
  2229. this.details_zoom_scale = this.scale * this.detailsZoomValue;
  2230. this.handlePaperVisibilityOnClosed();
  2231. this.clearPaperContents();
  2232. // reset the zoom value
  2233. this.setDetailsZoomValue(1);
  2234. this.is_closing = false;
  2235. });
  2236. }
  2237. this.tween.easing(Tween.Easing.Quadratic.InOut).start();
  2238. },
  2239. resetSuperpositionsConstraintsScaling(s){
  2240. if (this.concernement.superposition_constraints_id
  2241. && this.concernement.superposition_constraints_id[this.id]
  2242. && this.concernement.superposition_constraints_id[this.id].length)
  2243. {
  2244. // get all the constraints of the world
  2245. let all_constrains = Matter.Composite.allConstraints(this.matterEngine.world);
  2246. // loop through all constraint ids recorded in the concernement
  2247. // get only the constraint of this concernement mapitem
  2248. for(let constraint_id of this.concernement.superposition_constraints_id[this.id]) {
  2249. // get the right constraint object from matter
  2250. let constraint;
  2251. for (const c of all_constrains) {
  2252. if (c.id === constraint_id) {
  2253. constraint = c;
  2254. break;
  2255. }
  2256. }
  2257. if (constraint) {
  2258. // tween the constraint point
  2259. let ab = this.cid === constraint.concernementA.id ? 'A' : 'B';
  2260. let point = constraint[`point${ab}`];
  2261. // revert to the original point
  2262. let oripoint = Matter.Vector.create(point.x / this.scale, point.y / this.scale)
  2263. // newpoint from original point
  2264. let newpoint = Matter.Vector.create(oripoint.x * s, oripoint.y * s)
  2265. constraint[`point${ab}`] = newpoint;
  2266. }
  2267. }
  2268. }
  2269. },
  2270. async resetMapItemPosition(){
  2271. await nextTick();
  2272. console.log('resetMapItemPosition');
  2273. this.pos = this.paper_main_object.position = {
  2274. x: (this.canvas.width - this.cartouch_width) / 2,
  2275. y: this.canvas.height / 2
  2276. };
  2277. this.constraint.pointA = this.pos;
  2278. console.log('resetMapItemPosition', this.cartouch_width, this.pos);
  2279. Matter.Body.setPosition(this.body, this.pos);
  2280. },
  2281. async resetMapItemScale(){
  2282. await nextTick();
  2283. console.log('resetMapItemScale');
  2284. let s = this.getOpeningAvailableScale();
  2285. // revert to the original scale (by reverting the previous scale)
  2286. this.paper_main_object.scale(1 / this.scale);
  2287. // then scale again to new scale
  2288. this.paper_main_object.scale(s);
  2289. // TODO handle superposition scaling
  2290. this.resetSuperpositionsConstraintsScaling(s);
  2291. // record the new scale
  2292. this.prev_scale = this.scale = s;
  2293. this.details_zoom_scale = this.scale * this.detailsZoomValue;
  2294. },
  2295. // ENGINE UPDATE
  2296. onBeforeEngineUpdate (event) {
  2297. // update the opening/closing tweening
  2298. // if (this.tween) {
  2299. // this.tween.update();
  2300. // }
  2301. Tween.update();
  2302. if (this.opened_concernement) {
  2303. // 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]);
  2304. // console.log(this.id, this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id].indexOf(this.id));
  2305. if (this.opened_concernement.opened_mapitem_id !== this.id) {
  2306. // push all aside mapitems not superposed the the opened mapitem
  2307. let pushaside = true;
  2308. // BUT NOT push aside mapitems superposed to this item
  2309. // opened_mapitem <-- superposed_mapitems
  2310. if (this.map_mode === 'superposition') {
  2311. if (this.opened_concernement.superposed_mapitem_id_by_mapitem_id
  2312. && this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id])
  2313. { // if opened mapitem has superposed mapitems
  2314. let superposed_ids = this.opened_concernement.superposed_mapitem_id_by_mapitem_id[this.opened_concernement.opened_mapitem_id]
  2315. if (superposed_ids.indexOf(this.id) >= 0) { //this mapitem is directly superposed to the opened mapitem
  2316. pushaside = false;
  2317. }
  2318. }
  2319. }
  2320. if (pushaside) {
  2321. this.pushAside()
  2322. }
  2323. }
  2324. } else if (!this.is_hover){
  2325. if (this.map_mode === 'proximite'
  2326. || this.map_mode === 'superposition'
  2327. || this.map_mode === 'puissancedagir'
  2328. || this.map_mode === 'action'
  2329. || this.map_mode === 'doleancer'){ // apply focus forces : move unfocused on the sides and focused on the center
  2330. this.applyFocusForces(); //
  2331. } else {
  2332. this.applyNormalForces(); // if terrain de vie apply force to gently reject items from the sides and so create a continuasly mvmt
  2333. }
  2334. } else {
  2335. // stop movement if is mouse hover
  2336. Matter.Body.setVelocity(this.body, {x:0, y:0})
  2337. }
  2338. // reset all matter rotation forces otherwise items will spin when colide
  2339. Matter.Body.setAngle(this.body, 0);
  2340. Matter.Body.setAngularSpeed(this.body, 0);
  2341. },
  2342. applyFocusForces(){
  2343. if(!this.isFocused()) {
  2344. this.pushAside()
  2345. }else{
  2346. this.bringToCenter()
  2347. }
  2348. },
  2349. isFocused(){
  2350. return this.map_mode === 'terraindevie'
  2351. || (this.map_mode === 'proximite' && this.concernement.has_proximites)
  2352. || (this.map_mode === 'superposition' && this.concernement.has_superpositions)
  2353. || (this.map_mode === 'action' && this.concernement.has_agissantes)
  2354. || (this.map_mode === 'puissancedagir' && this.concernement.has_puissancedagir)
  2355. || (this.map_mode === 'doleancer' && this.concernement.has_doleance);
  2356. },
  2357. pushAside(){
  2358. // console.log('pushAside');
  2359. // apply a force in direction of one side or an other depending of the start position
  2360. // the force is exponentialy proportional to the distance from the side
  2361. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  2362. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2363. let pseudo_center_x = this.opened_concernement
  2364. ? (this.canvas.width - this.cartouch_width) / 2
  2365. : this.canvas.width / 2;
  2366. // get the direction to the closest side
  2367. let dir = this.map_mode === 'superposition'
  2368. && this.concernement.superposition_constraints_id
  2369. && this.concernement.superposition_constraints_id[this.id]
  2370. && this.concernement.superposition_constraints_id[this.id].length // go to the right if has superposition constraint applied
  2371. ? 1 // to the right
  2372. : this.pos.x > pseudo_center_x // else
  2373. ? 1 // to the right
  2374. : -1; // to the left
  2375. if (this.map_mode === 'doleancer') {
  2376. dir = 1;
  2377. }
  2378. // max and min item position
  2379. let minp = 0;
  2380. let maxp = dir < 0
  2381. ? pseudo_center_x
  2382. : this.canvas.width - pseudo_center_x;
  2383. // max and min force
  2384. let minf = 0;
  2385. let maxf = 6;
  2386. // scale factor
  2387. let scale = (maxf-minf) / (maxp-minp);
  2388. // get the inversed distance
  2389. let dist = dir < 0
  2390. ? this.pos.x
  2391. : this.canvas.width - this.pos.x; // get the distance from the side
  2392. // // calculate the force
  2393. // let x_force = Math.pow(dist/700,100) * dir;
  2394. // calculate the logarithmic force
  2395. let x_force = Math.exp(minf + scale*(dist-minp)) * dir;
  2396. let ori_pos = {x:pseudo_center_x, y:this.body.position.y};
  2397. Matter.Body.applyForce(
  2398. this.body,
  2399. ori_pos,
  2400. {
  2401. x: x_force,
  2402. y: 0
  2403. }
  2404. );
  2405. this.body.frictionAir = 0.01;
  2406. },
  2407. bringToCenter(){
  2408. // bring to the centre
  2409. // apply a force in direction of one side or an other depending of the start position
  2410. // the force is exponentialy inversed proportional to the distance from the side
  2411. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  2412. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2413. let pseudo_center_x = this.opened_concernement
  2414. ? (this.canvas.width - this.cartouch_width) / 2
  2415. : this.canvas.width / 2;
  2416. let dir = this.pos.x > pseudo_center_x
  2417. ? -1 // to left
  2418. : 1; // to right
  2419. // get the inversed distance from the side
  2420. let dist = dir < 0
  2421. ? this.pos.x - pseudo_center_x
  2422. : pseudo_center_x - this.pos.x;
  2423. 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
  2424. // max & min item position
  2425. let minp = 0;
  2426. let maxp = dir < 0
  2427. ? this.canvas.width - pseudo_center_x
  2428. : pseudo_center_x;
  2429. // X force decreasing near the center
  2430. // max and min force
  2431. let xminf = 0;
  2432. let xmaxf = 1;// + Math.random()*4;
  2433. // scale factor
  2434. let xscale = (xmaxf-xminf) / (maxp-minp);
  2435. let x_force = Math.exp(xminf + xscale*(dist-minp)) * dir;
  2436. let ori_pos = dir < 0
  2437. ? {x:this.canvas.width, y:this.body.position.y}
  2438. : {x:0, y:this.body.position.y};
  2439. Matter.Body.applyForce(
  2440. this.body,
  2441. ori_pos,
  2442. {
  2443. x: x_force,
  2444. y: 0
  2445. }
  2446. );
  2447. // Friction air fa (increasing near the center)
  2448. let fa_minf = 0;
  2449. let fa_maxf = 20;
  2450. // scale factor
  2451. let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  2452. let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  2453. this.body.frictionAir = fa;
  2454. // console.log(`bringToCenter dist:${dist}, x_force:${x_force}, friction air:${fa}`);
  2455. } else { // in the ceneter zone let items floats freely
  2456. let fa = 0.01;
  2457. let velocity = Matter.Body.getVelocity(this.body);
  2458. if (velocity.x) {
  2459. let velocityx = Math.abs(velocity.x);
  2460. // map a range of numbers to another range of numbers
  2461. // INFO https://stackoverflow.com/a/46462321
  2462. let velocity_range = [50, 1000];
  2463. let fa_range = [0,3];
  2464. fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  2465. // console.log(`bringToCenter velocityx:${velocityx}, fa:${fa}, velocity`, velocity);
  2466. } else {
  2467. console.warn(`bringToCenter no velocity`, velocity);
  2468. }
  2469. this.body.frictionAir = fa;
  2470. }
  2471. },
  2472. applyShuffleForces() {
  2473. // console.log('applyShuffleForces');
  2474. // var dist, dir, x_velocity;
  2475. // dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
  2476. // dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
  2477. // x_velocity = Math.pow(dist/650,10) * dir;
  2478. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2479. let pseudo_center_x = this.opened_concernement
  2480. ? (this.canvas.width - this.cartouch_width) / 2
  2481. : this.canvas.width / 2;
  2482. // get the direction to the centre
  2483. let dir = this.pos.x > pseudo_center_x
  2484. ? -1 // to left
  2485. : 1; // to right
  2486. // get the inversed distance from the side
  2487. let dist = dir < 0
  2488. ? this.pos.x - pseudo_center_x
  2489. : pseudo_center_x - this.pos.x;
  2490. dist = dist > 1000 ? 1000 : dist;
  2491. 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
  2492. // max & min item position
  2493. let minp = 0;
  2494. let maxp = dir < 0
  2495. ? this.canvas.width - pseudo_center_x
  2496. : pseudo_center_x;
  2497. // X max and min force
  2498. let xminf = 0;
  2499. // let xmaxf = 4 + Math.random()*3;
  2500. let xmaxf = 2 + Math.random()*2;
  2501. // scale factor
  2502. let xscale = (xmaxf-xminf) / (maxp-minp);
  2503. let x_velocity = Math.exp(xminf + xscale*(dist-minp))*dir;
  2504. // let y_velocity = 30 + Math.random() * 50;
  2505. // y_velocity = Math.random() > 0.5 ? -y_velocity : y_velocity;
  2506. // X max and min force
  2507. let yminf = 1;
  2508. // let ymaxf = 2 + Math.random()*4;
  2509. let ymaxf = 4 + Math.random()*2;
  2510. // scale factor
  2511. let yscale = (ymaxf-yminf) / (maxp-minp);
  2512. let y_velocity = Math.exp(yminf + yscale*(dist-minp)) * (Math.random() > 0.5 ? 1 : -1); // let ori_pos = dir < 0
  2513. // ? {x:this.canvas.width, y:this.body.position.y}
  2514. // : {x:0, y:this.body.position.y};
  2515. // let x_force = Math.pow(dist/800,10) * dir;
  2516. // console.log(`applyShuffleForces dir:${dir}, maxp:${maxp}, dist:${dist}, x_velocity:${x_velocity}, y_velocity:${y_velocity}`);
  2517. Matter.Body.setVelocity(this.body, {x: x_velocity, y: y_velocity});
  2518. } else { // if in center zone real shuffle velocity
  2519. // x_velocity = -50 + Math.random()*100;
  2520. // y_velocity = -50 + Math.random()*100;
  2521. // this.setInitBodyVelocity();
  2522. }
  2523. this.body.frictionAir = 0.01;
  2524. },
  2525. applyNormalForces () {
  2526. // bring gently to the centre
  2527. // apply a force in direction of one side or an other depending of the start position
  2528. // the force is exponentialy inversed proportional to the distance from the side
  2529. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  2530. // cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  2531. // TODO force is proportional to window size
  2532. // X
  2533. let pseudo_center_x = this.opened_concernement
  2534. ? (this.canvas.width - this.cartouch_width) / 2
  2535. : this.canvas.width / 2;
  2536. let xdir = this.pos.x > pseudo_center_x
  2537. ? -1 // to left
  2538. : 1; // to right
  2539. // get the inversed distance from the side
  2540. let xdist = xdir < 0
  2541. ? this.pos.x - pseudo_center_x
  2542. : pseudo_center_x - this.pos.x;
  2543. // Y
  2544. let pseudo_center_y = this.canvas.height / 2;
  2545. let ydir = this.pos.y > pseudo_center_y
  2546. ? -1 // to top
  2547. : 1; // to bottom
  2548. // get the inversed distance from the side
  2549. let ydist = ydir < 0
  2550. ? this.pos.y - pseudo_center_y
  2551. : pseudo_center_y - this.pos.y;
  2552. let k = 4;
  2553. 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
  2554. // max and min force
  2555. let minf = 0;
  2556. let maxf = 0.1;
  2557. // X
  2558. // max & min item position
  2559. let xminp = 0;
  2560. let xmaxp = xdir < 0
  2561. ? this.canvas.width - pseudo_center_x
  2562. : pseudo_center_x;
  2563. // X force decrsinsing near the center
  2564. // scale factor
  2565. let xscale = (maxf-minf) / (xmaxp-xminp);
  2566. let x_force = Math.exp(minf + xscale*(xdist-xminp)) * xdir;
  2567. let xori_pos = xdir < 0
  2568. ? {x:this.canvas.width, y:this.body.position.y}
  2569. : {x:0, y:this.body.position.y};
  2570. Matter.Body.applyForce(
  2571. this.body,
  2572. xori_pos,
  2573. {
  2574. x: x_force,
  2575. y: 0
  2576. }
  2577. );
  2578. // Y
  2579. // max & min item position
  2580. let yminp = 0;
  2581. let ymaxp = ydir < 0
  2582. ? this.canvas.height - pseudo_center_y
  2583. : pseudo_center_y;
  2584. // X force decrsinsing near the center
  2585. // scale factor
  2586. let yscale = (maxf-minf) / (ymaxp-yminp);
  2587. let y_force = Math.exp(minf + yscale*(ydist-xminp)) * ydir;
  2588. let yori_pos = ydir < 0
  2589. ? {x:this.body.position.x, y:this.canvas.height}
  2590. : {x:this.body.position.x, y:0};
  2591. Matter.Body.applyForce(
  2592. this.body,
  2593. yori_pos,
  2594. {
  2595. x: 0,
  2596. y: y_force
  2597. }
  2598. );
  2599. // // Friction air fa (increasing near the center)
  2600. // let fa_minf = 0;
  2601. // let fa_maxf = 6;
  2602. // // scale factor
  2603. // let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  2604. // let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  2605. // this.body.frictionAir = fa;
  2606. // console.log(`bringToCenter dist:${xdist}, x_force:${x_force}`);
  2607. } else { // in the ceneter zone let items floats freely
  2608. // let velocity = Matter.Body.getVelocity(this.body);
  2609. // let velocityx = Math.abs(velocity.x);
  2610. // // map a range of numbers to another range of numbers
  2611. // // INFO https://stackoverflow.com/a/46462321
  2612. // let velocity_range = [50, 1000];
  2613. // let fa_range = [0,3];
  2614. // let fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  2615. // console.log(`bringToCenter velocity.x:${velocity.x}, fa:${fa}`);
  2616. // this.body.frictionAir = fa;
  2617. this.body.frictionAir = 0.1;
  2618. }
  2619. },
  2620. checkOverflow() {
  2621. // respawn element if not opened and outside screen
  2622. if (!this.is_open) {
  2623. let pad = 1;
  2624. let circleray = this.body.circleRadius+pad*1.1;
  2625. let respawn = false;
  2626. if(this.pos.x <= pad){ this.pos.x = circleray; respawn=true;}
  2627. if(this.pos.x >= this.canvas.width-pad){ this.pos.x = this.canvas.width - circleray; respawn=true;}
  2628. if(this.pos.y <= pad){ this.pos.y = circleray; respawn=true;}
  2629. if(this.pos.y >= this.canvas.height-pad){ this.pos.y = this.canvas.height - circleray; respawn=true;}
  2630. if (respawn) {
  2631. console.log('respawning');
  2632. // this.pos = respawn_pos;
  2633. Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
  2634. // this.setInitBodyVelocity();
  2635. Matter.Body.setVelocity(this.body, {x:0,y:0});
  2636. }
  2637. }
  2638. },
  2639. onAfterEngineUpdate (event) {
  2640. this.checkOverflow();
  2641. this.paper_main_object.position = this.pos = this.body.position;
  2642. // fadein intro
  2643. // if(!this.mapitem.clone){
  2644. if (this.paper_main_object.opacity < 9) {
  2645. this.paper_main_object.opacity = this.paper_main_object.opacity * 1.1;
  2646. } else{
  2647. this.paper_main_object.opacity = 1;
  2648. }
  2649. // }
  2650. this.handlePaperVisibilityOnAfterEnginUpdate()
  2651. // DEBUG
  2652. // this.debugDrawMatterBodyCircle();
  2653. // END DEBUG
  2654. },
  2655. debugDrawMatterBodyCircle(){
  2656. if (this.paper_main_object.children['debug_circle']) {
  2657. this.paper_main_object.children['debug_circle'].remove();
  2658. }
  2659. let debugcircle = new paper.Path.Circle({
  2660. center: this.pos,
  2661. // radius: this.ray,
  2662. radius: this.body.circleRadius,
  2663. strokeColor: '#f00',
  2664. strokeWidth: 1,
  2665. name: 'debug_circle'
  2666. })
  2667. this.paper_main_object.addChild(debugcircle)
  2668. }
  2669. },
  2670. render() {
  2671. // console.log('render()', this.ctx);
  2672. },
  2673. }
  2674. </script>