ConcernementMapItem.vue 112 KB

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