ConcernementMapItem.vue 127 KB

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