ConcernementMapItem.vue 114 KB

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