ConcernementMapItem.vue 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. <script>
  2. // https://brm.io/matter-js/docs/classes/Engine.html
  3. // import {
  4. // // Engine,
  5. // // Render,
  6. // // World,
  7. // Bodies,
  8. // Body,
  9. // Events,
  10. // Composite,
  11. // // Composites,
  12. // // Constraint,
  13. // // Vertices,
  14. // // Mouse,
  15. // // MouseConstraint,
  16. // // Query,
  17. // // Common
  18. // } from "matter-js";
  19. import Matter from "matter-js";
  20. import MatterAttractors from "matter-attractors";
  21. // Matter.use(MatterAttractors);
  22. // import polydecomp from "poly-decomp";
  23. // import { easeInOutQuad, easeInOutQuart } from 'easing-utils';
  24. import Tween from "@tweenjs/tween.js";
  25. import { mapState, mapActions } from 'pinia'
  26. import { ConcernementsStore } from '@/stores/concernements'
  27. import { CommonStore } from '@/stores/common'
  28. export default {
  29. inject: ['canvasMap', 'matterEngine'],
  30. data() {
  31. return {
  32. id: null,
  33. entities: null,
  34. // opened_entite_id: null,
  35. canvas: null,
  36. ctx: null,
  37. pos : {
  38. x: 0,
  39. y: 0
  40. },
  41. ray: 60,
  42. time: 0,
  43. salientPoints: [],
  44. scale: 1,
  45. opacity: 0,
  46. tween: null,
  47. body: null,
  48. body_parts: [],
  49. constraint: null,
  50. isHover: false
  51. }
  52. },
  53. props: ['concernement', 'opened'],
  54. computed: {
  55. ...mapState(ConcernementsStore,['map_mode']),
  56. ...mapState(ConcernementsStore,['concernementsByID']),
  57. ...mapState(ConcernementsStore,['opened_entite_id']),
  58. ...mapState(CommonStore,['hover_elmt'])
  59. },
  60. created () {
  61. // console.log(`ConcernementsMapItem ${this.concernement.id} created`, this.canvasMap, this.matterEngine);
  62. this.id = this.concernement.id
  63. this.entites = this.concernement.entites
  64. this.entites_byid = this.concernement.entites_byid
  65. // console.log(`ConcernementsMapItem ${this.concernement.id} $route`, this.id, this.$route);
  66. // if (this.$route.name === 'concernement'
  67. // && parseInt(this.$route.params.id) === this.id
  68. // && typeof this.$route.params.eid !== "undefined") {
  69. // // console.log("we have an entity");
  70. // this.opened_entite_id = parseInt(this.$route.params.eid);
  71. // }
  72. this.parsePoints()
  73. this.getSalientPoints()
  74. if (this.salientPoints.length > 3) { // do not build item if it doesn't get enougth salient points
  75. if (this.canvasMap) {
  76. this.initCanvasMap()
  77. }
  78. }
  79. },
  80. // mounted() {
  81. // console.log(`ConcernementsMapItem ${this.concernement.id} mounted`, this.canvasMap.canvas);
  82. // },
  83. watch: {
  84. // canvasMap (n, o) {
  85. // console.log("concernementItem watch canvasMap", o, n);
  86. // }
  87. canvasMap: {
  88. handler (n, o){
  89. // console.log("concernementItem watch canvasMap.ctx", typeof this.canvas, o, n);
  90. if (!this.canvas) {
  91. this.initCanvasMap()
  92. }
  93. },
  94. deep: true
  95. },
  96. opened: {
  97. handler (n, o) {
  98. if(n){ // opened
  99. this.openClose(true);
  100. }else{ // closed
  101. this.openClose(false)
  102. }
  103. },
  104. deep: true
  105. },
  106. map_mode: {
  107. handler (n, o) {
  108. console.log('watch map_mode', o, n);
  109. if (n === 'terraindevie') {
  110. this.applyShuffleForces();
  111. }
  112. },
  113. deep: true
  114. },
  115. hover_elmt: {
  116. handler (n, o) {
  117. // console.log('watch hover_elmt', o, n);
  118. if (n && n.type === 'concernement' && n.id === this.id) {
  119. this.isHover = true;
  120. } else {
  121. this.isHover = false;
  122. }
  123. },
  124. deep: true
  125. }
  126. },
  127. methods: {
  128. parsePoints (){
  129. // converts data (menace/maintien, actuel/future, prise) into atcual position x,y
  130. for (let i = 0; i < this.entites.length; i++) {
  131. let entite = this.entites[i]
  132. // console.log('entite', entite);
  133. this.entites[i].display = {
  134. alpha: null,
  135. ray: null
  136. }
  137. // RAYON
  138. // https://stackoverflow.com/questions/5731863/mapping-a-numeric-range-onto-another
  139. // slope = (output_end - output_start) / (input_end - input_start)
  140. // output = output_start + slope * (input - input_start)
  141. // from range 0 -> 100 to range 0 -> this.ray
  142. let init_max = 100
  143. let slope = this.ray / init_max
  144. this.entites[i].display.ray = slope * (init_max - entite.prise);
  145. // if (this.concernement.id === 28) {
  146. // console.log(`entity prise: ${entite.prise} | ray: ${this.entites[i].display.ray}`);
  147. // }
  148. // ANGLE
  149. // -90 <= mm <= 90
  150. if (entite.actuelfuture) {
  151. // future en haut : 180 <= a <= 360
  152. // from -90 -> 90 to range 180 -> 360
  153. this.entites[i].display.alpha = entite.menacemaintien + 270
  154. } else {
  155. // actuel: en bas : O <= a <= 180
  156. // from -90 -> 90 to range 180 -> 0
  157. this.entites[i].display.alpha = -1 * entite.menacemaintien + 90
  158. }
  159. // POSITION X Y (par rapport au centre du concernement)
  160. this.entites[i].display.pos = {
  161. x: this.entites[i].display.ray * Math.cos(this.entites[i].display.alpha * (Math.PI/180)),
  162. y: this.entites[i].display.ray * Math.sin(this.entites[i].display.alpha * (Math.PI/180))
  163. }
  164. this.entites_byid[entite.entite.id].display = this.entites[i].display;
  165. }
  166. },
  167. getSalientPoints () {
  168. // debugger
  169. // console.log(this.entites);
  170. let arc = 360/10;
  171. // loop through arcs
  172. // for (let i = 360/arc; i >= 0 ; i--) {
  173. for (let i = 0; i <= 360/arc ; i++) {
  174. // loop through entities to find the farest on the arc
  175. let max_r = 0;
  176. let farest = null;
  177. for (let j = 0; j < this.entites.length; j++) {
  178. let entite = this.entites[j];
  179. if(arc*i <= entite.display.alpha && entite.display.alpha <= arc*i+arc) { // if entity is in arc
  180. if (entite.display.ray > max_r) { // && entite.display.ray > this.ray/2 // and farest from minimu
  181. // if entity is farest from precedent one
  182. max_r = entite.display.ray;
  183. // recalcul x & y to get a little padding between entite and contour by increasing ray
  184. farest = {
  185. alpha: entite.display.alpha,
  186. ray: entite.display.ray,
  187. pos: {
  188. x: (entite.display.ray + 3) * Math.cos(entite.display.alpha * (Math.PI/180)),
  189. y: (entite.display.ray + 3) * Math.sin(entite.display.alpha * (Math.PI/180))
  190. }
  191. };
  192. }
  193. }
  194. }
  195. if (farest) {
  196. this.salientPoints.push(farest)
  197. }
  198. }
  199. // console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints);
  200. },
  201. initCanvasMap (){
  202. // console.log(`ConcernementsMapItem ${this.concernement.id} initCanvasMap`);
  203. // record canvas and ctx for rendering (drawing)
  204. this.canvas = this.canvasMap.canvas
  205. this.ctx = this.canvasMap.ctx
  206. // define init position of the item
  207. this.pos = this.getRandomPos();
  208. //
  209. this.initMatterBody()
  210. },
  211. getRandomPos(){
  212. let pad = 200;
  213. return {
  214. x: pad + this.ray/2 + Math.random()*(this.canvas.width - this.ray - pad),
  215. y: pad + this.ray/2 + Math.random()*(this.canvas.height - this.ray - pad)
  216. };
  217. },
  218. initMatterBody (){
  219. // MATTER
  220. // create the matter body and add it to the engine
  221. if (!this.body) {
  222. // console.log('concernementItem creating body');
  223. // https://github.com/liabru/matter-attractors/issues/8
  224. // https://github.com/liabru/matter-attractors/blob/master/index.js
  225. // MatterAttractors.Attractors.gravityConstant = -5;
  226. // Create parts of the body : main big circle & entities
  227. this.body_parts = [
  228. Matter.Bodies.circle(0, 0, this.ray, {
  229. item_type: 'concernement',
  230. id: this.concernement.id,
  231. })
  232. ];
  233. // // Create parts of the body : contours
  234. // if (this.salientPoints.length > 2) {
  235. // var decomp = require('poly-decomp');
  236. // // window.decomp = decomp;
  237. // // debugger;
  238. // Matter.Common.setDecomp(decomp);
  239. // // Matter.Common.setDecomp(require('poly-decomp'));
  240. // // let contourpoints = [];
  241. // // for (let j = 0; j < this.salientPoints.length; j++) {
  242. // // contourpoints.push(this.salientPoints[j].pos);
  243. // // }
  244. // let contourpoints = this.salientPoints.map(function(point) {
  245. // return point.pos; //[point.pos.x, point.pos.y]
  246. // });
  247. // // console.log('contourpoints', contourpoints);
  248. // // let contourpoints = Matter.Vertices.fromPath('35 7 19 17 14 38 14 58 25 79 45 85 65 84 65 66 46 67 34 59 30 44 33 29 45 23 66 23 66 7 53 7');
  249. // // let ccw_contourpoints = decomp.makeCCW(contourpoints);
  250. // // console.log('ccw_contourpoints', ccw_contourpoints);
  251. // let vertices_contour = Matter.Vertices.create(contourpoints);
  252. // // console.log('vertices_contour', vertices_contour);
  253. // // let vertices_contour_bounds = Matter.Bounds.create(vertices_contour);
  254. // // console.log('vertices_contour_bounds', vertices_contour_bounds);
  255. // // debugger;
  256. // let body_contour = Matter.Bodies.fromVertices(0, 0, vertices_contour, {
  257. // mass: 0,
  258. // item_type: 'concernement-contours',
  259. // id: this.concernement.id,
  260. // }, false, 0, 0, 0);
  261. // // debugger;
  262. // // console.log('body_contour.bounds', body_contour.bounds);
  263. // // // https://github.com/liabru/matter-js/issues/186
  264. // // Matter.Body.translate(body_contour, Matter.Vector.sub(vertices_contour_bounds.min, body_contour.bounds.min))
  265. // this.body_parts.push(body_contour);
  266. // }
  267. // Create parts of the body : entities
  268. for (let i = 0; i < this.entites.length; i++) {
  269. // parts.push(Matter.Bodies.circle(this.pos.x+this.entites[i].display.pos.x, this.pos.y+this.entites[i].display.pos.y, 15, {
  270. // item_type: 'entite',
  271. // id: this.entites[i].id
  272. // }))
  273. this.body_parts.push(Matter.Bodies.circle(this.entites[i].display.pos.x, this.entites[i].display.pos.y, 0.8, {
  274. item_type: 'entite',
  275. id: this.entites[i].entite.id,
  276. cid: this.concernement.id,
  277. agissante: this.entites[i].entite.agissante,
  278. isSensor: true
  279. }))
  280. }
  281. // Create parts of the body : besoins and responses
  282. this.createBesoinsBodyParts();
  283. // create the body
  284. this.body = Matter.Body.create({
  285. parts: this.body_parts,
  286. item_type: 'concernement',
  287. id: this.concernement.id,
  288. frictionAir: 0,
  289. // mass: Math.pow(3, this.entites.length),
  290. mass: 10,
  291. restitution: 0.15,
  292. collisionFilter: {
  293. group: -1
  294. },
  295. plugin: {
  296. attractors: [
  297. // there is a built in helper function for Newtonian gravity!
  298. // you can find out how it works in index.js
  299. MatterAttractors.Attractors.gravity
  300. ]
  301. }
  302. });
  303. Matter.Body.setPosition(this.body, this.pos);
  304. // add init velocity
  305. let delta = 10;
  306. Matter.Body.setVelocity(this.body, {
  307. x: -delta + Math.random()*delta*2,
  308. y: -delta + Math.random()*delta*2
  309. });
  310. // console.log('concernementItem mass', this.body.mass);
  311. Matter.Composite.add(this.matterEngine.world, this.body);
  312. // console.log('concernement body', this.body);
  313. // // listen for animate event dispatched from parent
  314. // this.canvas.addEventListener('animate', this.animate)
  315. // listen for afterUpdate event from Matter.Engine object
  316. Matter.Events.on(this.matterEngine, "beforeUpdate", this.onBeforeEngineUpdate);
  317. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  318. }
  319. },
  320. createBesoinsBodyParts(){
  321. let res_fields = ['qui','quoi','ou','avec'];
  322. let arc = (360 / 16); // unit arc
  323. let r = (this.ray * this.scale)/5; // unit ray
  324. let br = r - r/3; // besoin ray
  325. for (let i = 0; i < this.concernement.besoins.length; i++) {
  326. let start_a = arc * i; // angle depart (for reponses)
  327. let center_a = start_a + arc/2; // angle central
  328. let x = Math.cos(center_a*(Math.PI/180)) * br;
  329. let y = Math.sin(center_a*(Math.PI/180)) * br;
  330. this.body_parts.push(Matter.Bodies.circle(x, y, 0.8, {
  331. item_type: 'besoin',
  332. id: this.concernement.besoins[i].id,
  333. cid: this.concernement.id,
  334. isSensor: true
  335. }));
  336. let res_arc = arc / (1 + this.concernement.besoins[i].reponses.length); // unit arc for responses depending responses number
  337. for (let j = 0; j < this.concernement.besoins[i].reponses.length; j++) {
  338. let res_a = start_a + res_arc * (j+1); // angle for response line
  339. for (let f = 0; f < res_fields.length; f++) {
  340. if(this.concernement.besoins[i].reponses[j][res_fields[f]]){
  341. let rr = this.ray * this.scale - r*f - r/2; // reponse field ray
  342. let rx = Math.cos(res_a*(Math.PI/180)) * rr;
  343. let ry = Math.sin(res_a*(Math.PI/180)) * rr;
  344. this.body_parts.push(Matter.Bodies.circle(rx, ry, 0.8, {
  345. item_type: 'reponse',
  346. field: res_fields[f],
  347. id: this.concernement.besoins[i].reponses[j].id,
  348. bid: this.concernement.besoins[i].id,
  349. cid: this.concernement.id,
  350. isSensor: true
  351. }));
  352. }
  353. }
  354. }
  355. }
  356. },
  357. openClose(open) {
  358. // console.log(`ConcernementsMapItem ${this.concernement.id} openClose: ${open}`);
  359. if (this.tween) {
  360. this.tween.stop();
  361. }
  362. if (open) {
  363. // opening tweening
  364. this.tween = new Tween.Tween({s: this.scale, x: this.pos.x, y: this.pos.y, o: 0})
  365. .to({
  366. s: 7,
  367. x: (this.canvas.width - 450) / 2,
  368. y: this.canvas.height / 2,
  369. o: 0.8
  370. }, 800)
  371. .onUpdate((obj) => {
  372. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  373. // revert to the original size (by reverting the previous scale)
  374. Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  375. // then scale again to new scale
  376. Matter.Body.scale(this.body, obj.s, obj.s)
  377. // record new scale
  378. this.scale = obj.s;
  379. this.opacity = obj.o;
  380. Matter.Body.setPosition(this.body, {x:obj.x, y:obj.y});
  381. this.pos = {x:obj.x, y:obj.y};
  382. })
  383. .onComplete((obj) => {
  384. this.constraint = Matter.Constraint.create({
  385. pointA: this.pos,
  386. bodyB: this.body,
  387. stiffness: 1,
  388. damping: 0,
  389. length: 0
  390. });
  391. Matter.Composite.add(this.matterEngine.world, [this.body, this.constraint]);
  392. });
  393. // recreate the matter engine event to get it a the end of the events stack
  394. Matter.Events.off(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  395. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  396. } else {
  397. // closing
  398. if(this.constraint){
  399. Matter.Composite.remove(this.matterEngine.world, this.constraint);
  400. }
  401. this.tween = new Tween.Tween({s: this.scale, o: 1})
  402. .to({s: 1, o: 0}, 500)
  403. .onUpdate((obj) => {
  404. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  405. // revert to the original size (by reverting the previous scale)
  406. Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  407. // then scale again to new scale
  408. Matter.Body.scale(this.body, obj.s, obj.s)
  409. // record new scale
  410. this.scale = obj.s;
  411. this.opacity = obj.o;
  412. });
  413. }
  414. this.tween.easing(Tween.Easing.Quadratic.InOut).start();
  415. },
  416. onBeforeEngineUpdate (event) {
  417. if (this.tween) {
  418. this.tween.update();
  419. }
  420. if (this.map_mode === 'action' || this.map_mode === 'puissancedagir'){
  421. this.applyFocusForces();
  422. // TODO apply a little force to check the map when returning to terrain de vie
  423. }
  424. Matter.Body.setAngle(this.body, 0);
  425. Matter.Body.setAngularSpeed(this.body, 0);
  426. },
  427. applyFocusForces(){
  428. // map_mode action
  429. var dist, dir, ori_pos;
  430. var x_force = 0;
  431. if(!this.isFocused()) {
  432. // does not has actions -> take a side
  433. // apply a force in direction of one side or an other depending of the start position
  434. // the force is exponentialy proportional to the distance from the side
  435. dir = this.pos.x > this.canvas.width/2 ? 1 : -1; // get the direction to the closest side
  436. dist = (dir < 0 ? this.pos.x : this.canvas.width - this.pos.x); // get the distance from the side
  437. ori_pos = {x:this.canvas.width/2, y:this.body.position.y};
  438. x_force = Math.pow(dist/700,10) * dir;
  439. }else{
  440. // has action, get to the centre
  441. dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
  442. dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
  443. ori_pos = dir < 0 ? {x:this.canvas.width, y:this.body.position.y} : {x:0, y:this.body.position.y}
  444. x_force = Math.pow(dist/800,10) * dir;
  445. this.body.frictionAir = 0.05;
  446. }
  447. // x_force = (dist > 200 ? Math.pow(dist/700,10) : 0) * dir
  448. Matter.Body.applyForce(
  449. this.body,
  450. ori_pos,
  451. {
  452. x: x_force,
  453. y: 0
  454. }
  455. );
  456. },
  457. applyShuffleForces() {
  458. var dist, dir, x_velocity;
  459. dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
  460. dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
  461. x_velocity = Math.pow(dist/650,10) * dir;
  462. Matter.Body.setVelocity(this.body, {x: x_velocity, y: 0});
  463. },
  464. onAfterEngineUpdate (event) {
  465. // respawn element if outside screen
  466. if(this.pos.x < 0
  467. || this.pos.x > this.canvas.width
  468. || this.pos.y < 0
  469. || this.pos.y > this.canvas.height){
  470. this.pos = this.getRandomPos()
  471. Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
  472. }
  473. this.draw()
  474. },
  475. draw() {
  476. if (!this.ctx) return;
  477. // record the position from the main matter body
  478. this.pos = this.body.position;
  479. // drawing backgrounds
  480. if (this.opened) {
  481. switch (this.map_mode) {
  482. case 'terraindevie':
  483. this.drawBoussoleBG();
  484. break;
  485. case 'puissancedagir':
  486. this.drawPuissanceagirBG();
  487. break;
  488. case 'doleancer':
  489. this.drawDoleancerBG();
  490. break;
  491. }
  492. }
  493. // contours
  494. if (!this.opened
  495. || (this.opened && this.map_mode !== "puissancedagir" && this.map_mode !== "doleancer")) {
  496. this.drawContour();
  497. }
  498. // map mode puissance d'agir
  499. if (this.concernement.has_puissancedagir && this.map_mode === "puissancedagir") {
  500. if (!this.opened) {
  501. this.drawPuissanceagirIcon(); // if not opened and has_puissancedagir draw the puissance d'agir icone
  502. } else {
  503. this.drawBesoins();
  504. }
  505. }
  506. // map mode doleancer
  507. // if not opened and has_doleance draw the doleance icone
  508. if (this.concernement.has_doleance && this.map_mode === "doleancer") {
  509. if (!this.opened) {
  510. this.drawDoleanceIcon(); // if not opened and has_puissancedagir draw the puissance d'agir icone
  511. } else {
  512. this.drawDoleanceSteps();
  513. }
  514. }
  515. if (this.map_mode !== 'puissancedagir' && this.map_mode !== 'doleancer') {
  516. this.drawEntites()
  517. }
  518. },
  519. drawPuissanceagirBG(){
  520. for (let i = 1; i < 6; i++) {
  521. this.ctx.beginPath();
  522. this.ctx.lineWidth = 0.5;
  523. this.ctx.strokeStyle = `rgba(255,255,255,1)`;
  524. this.ctx.arc(this.pos.x, this.pos.y, ((this.ray*this.scale)/5)*i, 0, 2 * Math.PI, false);
  525. this.ctx.stroke();
  526. }
  527. this.ctx.beginPath();
  528. this.ctx.lineWidth = 1;
  529. this.ctx.strokeStyle = `rgba(255,255,255,1)`;
  530. this.ctx.setLineDash([5,5]);
  531. for (let j = 0; j < 4; j++) {
  532. let a = (90 / 4) * j;
  533. // diagonale
  534. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  535. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  536. // radians = degrees * (pi/180)
  537. // degrees = radians * (180/pi)
  538. let x = Math.cos(a*(Math.PI/180)) * this.ray * this.scale;
  539. let y = Math.sin(a*(Math.PI/180)) * this.ray * this.scale;
  540. // console.log('m', m);
  541. this.ctx.moveTo(this.pos.x + x, this.pos.y + y);
  542. this.ctx.lineTo(this.pos.x - x, this.pos.y - y);
  543. //
  544. this.ctx.moveTo(this.pos.x - y, this.pos.y + x);
  545. this.ctx.lineTo(this.pos.x + y, this.pos.y - x);
  546. }
  547. this.ctx.stroke();
  548. this.ctx.setLineDash([]);
  549. this.ctx.beginPath();
  550. this.ctx.fillStyle = `rgba(255,255,255,0.6)`;
  551. this.ctx.lineWidth = 2;
  552. this.ctx.strokeStyle = `#fff`;
  553. this.ctx.arc(this.pos.x, this.pos.y, this.ray*this.scale, 0, 2 * Math.PI, false);
  554. this.ctx.fill();
  555. this.ctx.stroke()
  556. this.ctx.closePath();
  557. },
  558. drawPuissanceagirIcon(){
  559. var r = 20 * this.scale; // ray
  560. var dr = r/2; // demi ray
  561. var d = r*2; // diameter
  562. this.ctx.beginPath();
  563. this.ctx.lineWidth = 1;
  564. this.ctx.strokeStyle = `#000`;
  565. this.ctx.arc(this.pos.x, this.pos.y, r, 0, 2 * Math.PI, false);
  566. this.ctx.stroke();
  567. this.ctx.beginPath();
  568. this.ctx.lineWidth = 1;
  569. this.ctx.strokeStyle = `#000`;
  570. this.ctx.arc(this.pos.x, this.pos.y, dr, 0, 2 * Math.PI, false);
  571. this.ctx.stroke();
  572. this.ctx.beginPath();
  573. this.ctx.lineWidth = 1;
  574. this.ctx.strokeStyle = `#000`;
  575. this.ctx.fillStyle = '#000';
  576. this.ctx.arc(this.pos.x, this.pos.y, 2*this.scale, 0, 2 * Math.PI, false);
  577. this.ctx.fill();
  578. this.ctx.stroke();
  579. // axes
  580. this.ctx.beginPath();
  581. this.ctx.lineWidth = 1;
  582. this.ctx.strokeStyle = `#000`;
  583. // vertical
  584. // this.ctx.moveTo(this.pos.x, this.pos.y - r);
  585. // this.ctx.lineTo(this.pos.x , this.pos.y - dr);
  586. // this.ctx.moveTo(this.pos.x, this.pos.y + r);
  587. // this.ctx.lineTo(this.pos.x , this.pos.y + dr);
  588. this.ctx.moveTo(this.pos.x, this.pos.y - r);
  589. this.ctx.lineTo(this.pos.x , this.pos.y + r);
  590. // horizontal
  591. // this.ctx.moveTo(this.pos.x - r, this.pos.y);
  592. // this.ctx.lineTo(this.pos.x - dr, this.pos.y);
  593. // this.ctx.moveTo(this.pos.x + r, this.pos.y);
  594. // this.ctx.lineTo(this.pos.x + dr, this.pos.y);
  595. this.ctx.moveTo(this.pos.x - r, this.pos.y);
  596. this.ctx.lineTo(this.pos.x + r, this.pos.y);
  597. // diagonale
  598. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  599. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  600. // radians = degrees * (pi/180)
  601. // degrees = radians * (180/pi)
  602. var m = Math.sin(45*(Math.PI/180)) * r;
  603. // console.log('m', m);
  604. this.ctx.moveTo(this.pos.x + m, this.pos.y + m);
  605. this.ctx.lineTo(this.pos.x - m, this.pos.y - m);
  606. //
  607. this.ctx.moveTo(this.pos.x - m, this.pos.y + m);
  608. this.ctx.lineTo(this.pos.x + m, this.pos.y - m);
  609. this.ctx.stroke();
  610. },
  611. drawBesoins(){
  612. for (let i = 0; i < this.body.parts.length; i++) {
  613. if (this.body.parts[i].item_type === 'besoin' || this.body.parts[i].item_type === 'reponse') {
  614. let part = this.body.parts[i];
  615. switch (part.item_type) {
  616. case 'besoin':
  617. this.ctx.beginPath();
  618. this.ctx.fillStyle = "#000";
  619. this.drawDiamond(part.position.x, part.position.y, 4);
  620. this.ctx.fill();
  621. break;
  622. case 'reponse':
  623. this.ctx.beginPath();
  624. this.ctx.fillStyle = "#eee";
  625. this.ctx.strokeStyle = "#000";
  626. this.ctx.lineWidth = 1;
  627. // this.ctx.arc(this.pos.x + rx, this.pos.y + ry, 2*(f+1), 0, 2 * Math.PI, false);
  628. this.drawDiamond(part.position.x, part.position.y, 5);
  629. this.ctx.fill();
  630. this.ctx.stroke();
  631. break;
  632. }
  633. }
  634. }
  635. },
  636. drawDiamond(x,y,r){
  637. this.ctx.moveTo(x, y - r);
  638. this.ctx.lineTo(x + r, y);
  639. this.ctx.lineTo(x, y + r);
  640. this.ctx.lineTo(x - r, y);
  641. this.ctx.lineTo(x, y - r);
  642. },
  643. drawDoleanceIcon(){
  644. var r = 20 * this.scale; // ray
  645. var dr = r/2; // demi ray
  646. var d = r*2; // diameter
  647. this.ctx.beginPath();
  648. this.ctx.lineWidth = 1;
  649. this.ctx.strokeStyle = `#000`;
  650. this.ctx.arc(this.pos.x, this.pos.y, r, 0, 2 * Math.PI, false);
  651. this.ctx.stroke();
  652. this.ctx.beginPath();
  653. this.ctx.lineWidth = 1;
  654. this.ctx.strokeStyle = `#000`;
  655. this.ctx.arc(this.pos.x, this.pos.y, dr, 0, 2 * Math.PI, false);
  656. this.ctx.stroke();
  657. // axes
  658. this.ctx.beginPath();
  659. this.ctx.lineWidth = 1;
  660. this.ctx.strokeStyle = `#000`;
  661. // vertical
  662. this.ctx.moveTo(this.pos.x, this.pos.y - r);
  663. this.ctx.lineTo(this.pos.x , this.pos.y - dr);
  664. this.ctx.moveTo(this.pos.x, this.pos.y + r);
  665. this.ctx.lineTo(this.pos.x , this.pos.y + dr);
  666. // this.ctx.moveTo(this.pos.x, this.pos.y - r);
  667. // this.ctx.lineTo(this.pos.x , this.pos.y + r);
  668. // horizontal
  669. this.ctx.moveTo(this.pos.x - r, this.pos.y);
  670. this.ctx.lineTo(this.pos.x - dr, this.pos.y);
  671. this.ctx.moveTo(this.pos.x + r, this.pos.y);
  672. this.ctx.lineTo(this.pos.x + dr, this.pos.y);
  673. // this.ctx.moveTo(this.pos.x - r, this.pos.y);
  674. // this.ctx.lineTo(this.pos.x + r, this.pos.y);
  675. // diagonale
  676. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  677. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  678. // radians = degrees * (pi/180)
  679. // degrees = radians * (180/pi)
  680. let m,n;
  681. m = Math.sin(45*(Math.PI/180)) * r;
  682. n = Math.sin(45*(Math.PI/180)) * r/2;
  683. // console.log('m', m);
  684. this.ctx.moveTo(this.pos.x + m, this.pos.y + m);
  685. this.ctx.lineTo(this.pos.x + n, this.pos.y + n);
  686. //
  687. this.ctx.moveTo(this.pos.x - m, this.pos.y + m);
  688. this.ctx.lineTo(this.pos.x - n, this.pos.y + n);
  689. //
  690. this.ctx.moveTo(this.pos.x + m, this.pos.y - m);
  691. this.ctx.lineTo(this.pos.x + n, this.pos.y - n);
  692. //
  693. this.ctx.moveTo(this.pos.x - m, this.pos.y - m);
  694. this.ctx.lineTo(this.pos.x - n, this.pos.y - n);
  695. this.ctx.stroke();
  696. },
  697. drawDoleancerBG(){
  698. var r = this.ray * this.scale; // ray
  699. var dr = r/2; // demi ray
  700. var d = r*2; // diameter
  701. var pcr = 2*this.scale; // petits cercle rayon
  702. // cercle exterieur
  703. this.ctx.beginPath();
  704. this.ctx.lineWidth = 2;
  705. this.ctx.strokeStyle = `#FFF`;
  706. this.ctx.arc(this.pos.x, this.pos.y, r, 0, 2 * Math.PI, false);
  707. this.ctx.stroke();
  708. // cercle interieur
  709. this.ctx.beginPath();
  710. this.ctx.lineWidth = 2;
  711. this.ctx.strokeStyle = `#FFF`;
  712. this.ctx.arc(this.pos.x, this.pos.y, dr, 0, 2 * Math.PI, false);
  713. this.ctx.stroke();
  714. // petit cercles
  715. this.ctx.beginPath();
  716. this.ctx.lineWidth = 2;
  717. this.ctx.strokeStyle = `#FFF`;
  718. this.ctx.arc(this.pos.x, this.pos.y - r, pcr, 0, 2 * Math.PI, false);
  719. this.ctx.stroke();
  720. this.ctx.beginPath();
  721. this.ctx.lineWidth = 2;
  722. this.ctx.strokeStyle = `#FFF`;
  723. this.ctx.arc(this.pos.x, this.pos.y + r, pcr, 0, 2 * Math.PI, false);
  724. this.ctx.stroke();
  725. this.ctx.beginPath();
  726. this.ctx.lineWidth = 2;
  727. this.ctx.strokeStyle = `#FFF`;
  728. this.ctx.arc(this.pos.x + r, this.pos.y, pcr, 0, 2 * Math.PI, false);
  729. this.ctx.stroke();
  730. this.ctx.beginPath();
  731. this.ctx.lineWidth = 2;
  732. this.ctx.strokeStyle = `#FFF`;
  733. this.ctx.arc(this.pos.x - r, this.pos.y, pcr, 0, 2 * Math.PI, false);
  734. this.ctx.stroke();
  735. // axes
  736. this.ctx.beginPath();
  737. this.ctx.lineWidth = 2;
  738. this.ctx.strokeStyle = `#FFF`;
  739. // vertical
  740. this.ctx.moveTo(this.pos.x, this.pos.y - r);
  741. this.ctx.lineTo(this.pos.x , this.pos.y - dr);
  742. this.ctx.moveTo(this.pos.x, this.pos.y + r);
  743. this.ctx.lineTo(this.pos.x , this.pos.y + dr);
  744. // this.ctx.moveTo(this.pos.x, this.pos.y - r);
  745. // this.ctx.lineTo(this.pos.x , this.pos.y + r);
  746. // horizontal
  747. this.ctx.moveTo(this.pos.x - r, this.pos.y);
  748. this.ctx.lineTo(this.pos.x - dr, this.pos.y);
  749. this.ctx.moveTo(this.pos.x + r, this.pos.y);
  750. this.ctx.lineTo(this.pos.x + dr, this.pos.y);
  751. // this.ctx.moveTo(this.pos.x - r, this.pos.y);
  752. // this.ctx.lineTo(this.pos.x + r, this.pos.y);
  753. // diagonale
  754. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  755. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  756. // radians = degrees * (pi/180)
  757. // degrees = radians * (180/pi)
  758. let m,n;
  759. m = Math.sin(45*(Math.PI/180)) * r;
  760. n = Math.sin(45*(Math.PI/180)) * r/2;
  761. // console.log('m', m);
  762. this.ctx.moveTo(this.pos.x + m, this.pos.y + m);
  763. this.ctx.lineTo(this.pos.x + n, this.pos.y + n);
  764. //
  765. this.ctx.moveTo(this.pos.x - m, this.pos.y + m);
  766. this.ctx.lineTo(this.pos.x - n, this.pos.y + n);
  767. //
  768. this.ctx.moveTo(this.pos.x + m, this.pos.y - m);
  769. this.ctx.lineTo(this.pos.x + n, this.pos.y - n);
  770. //
  771. this.ctx.moveTo(this.pos.x - m, this.pos.y - m);
  772. this.ctx.lineTo(this.pos.x - n, this.pos.y - n);
  773. this.ctx.stroke();
  774. },
  775. drawDoleanceSteps(){
  776. },
  777. drawBoussoleBG(){
  778. // BOUSSOLE
  779. // exterieur circle
  780. this.ctx.beginPath();
  781. this.ctx.lineWidth = 2;
  782. this.ctx.strokeStyle = `rgba(255,255,255,${this.opacity})`;
  783. // external circle is %8 less than max ray = (*0.92)
  784. this.ctx.arc(this.pos.x, this.pos.y, this.ray*this.scale*0.92, 0, 2 * Math.PI, false);
  785. // this.ctx.stroke();
  786. // interieur circle
  787. this.ctx.arc(this.pos.x, this.pos.y, this.ray/2*this.scale, 0, 2 * Math.PI, false);
  788. // this.ctx.stroke();
  789. // axes
  790. // vertical
  791. this.ctx.moveTo(this.pos.x, this.pos.y - this.ray*this.scale);
  792. this.ctx.lineTo(this.pos.x, this.pos.y + this.ray*this.scale);
  793. // horizontal
  794. this.ctx.moveTo(this.pos.x - this.ray*this.scale, this.pos.y);
  795. this.ctx.lineTo(this.pos.x + this.ray*this.scale, this.pos.y);
  796. // this.ctx.stroke();
  797. // fleches
  798. // haute
  799. this.ctx.moveTo(this.pos.x - (8*this.scale), this.pos.y - this.ray*this.scale*0.92 + (8*this.scale));
  800. this.ctx.lineTo(this.pos.x, this.pos.y - this.ray*this.scale*0.92);
  801. this.ctx.lineTo(this.pos.x + (8*this.scale), this.pos.y - this.ray*this.scale*0.92 + (8*this.scale));
  802. // milieu
  803. this.ctx.moveTo(this.pos.x - (8*this.scale), this.pos.y + (8*this.scale));
  804. this.ctx.lineTo(this.pos.x, this.pos.y);
  805. this.ctx.lineTo(this.pos.x + (8*this.scale), this.pos.y + (8*this.scale));
  806. this.ctx.stroke();
  807. // MOINS - PLUS
  808. this.ctx.beginPath();
  809. this.ctx.lineWidth = 8;
  810. this.ctx.strokeStyle = `rgba(255,255,255,${this.opacity})`;;
  811. // PLUS
  812. // horizontal
  813. this.ctx.moveTo(this.pos.x + this.ray*this.scale - (5 * this.scale), this.pos.y - this.ray*this.scale);
  814. this.ctx.lineTo(this.pos.x + this.ray*this.scale + (5 * this.scale), this.pos.y - this.ray*this.scale);
  815. // vertical
  816. this.ctx.moveTo(this.pos.x + this.ray*this.scale, this.pos.y - this.ray*this.scale - (5 * this.scale));
  817. this.ctx.lineTo(this.pos.x + this.ray*this.scale, this.pos.y - this.ray*this.scale + (5 * this.scale));
  818. // MOINS
  819. // horizontal
  820. this.ctx.moveTo(this.pos.x - this.ray*this.scale - (5 * this.scale), this.pos.y - this.ray*this.scale);
  821. this.ctx.lineTo(this.pos.x - this.ray*this.scale + (5 * this.scale), this.pos.y - this.ray*this.scale);
  822. // vertical
  823. this.ctx.stroke();
  824. },
  825. drawEntites(){
  826. // IF OPENED
  827. if (this.opened) {
  828. // place all entities points
  829. // OR using entitées matter bodies
  830. for (let i = 0; i < this.body.parts.length; i++) {
  831. if (this.body.parts[i].item_type === 'entite'
  832. // draw only entite agissante if map mode is action
  833. && ((this.map_mode === 'action' && this.body.parts[i].agissante) || this.map_mode !== "action")) {
  834. let part = this.body.parts[i];
  835. this.ctx.beginPath();
  836. this.ctx.arc(part.position.x, part.position.y, 0.3*this.scale, 0, 2 * Math.PI, false);
  837. // console.log(part.id, this.opened_entite_id);
  838. if (part.id === this.opened_entite_id) {
  839. this.ctx.fillStyle = "#01ffe2";
  840. } else {
  841. this.ctx.fillStyle = "#000";
  842. }
  843. this.ctx.fill();
  844. }
  845. }
  846. }
  847. // IF CLOSED
  848. else {
  849. // map mode action
  850. // if not opened and has_agissantes draw only entites agissantes
  851. if (this.concernement.has_agissantes && this.map_mode === "action") {
  852. for (let i = 0; i < this.body.parts.length; i++) {
  853. if (this.body.parts[i].item_type === 'entite' && this.body.parts[i].agissante) {
  854. let part = this.body.parts[i];
  855. this.ctx.beginPath();
  856. this.ctx.arc(part.position.x, part.position.y, 1*this.scale, 0, 2 * Math.PI, false);
  857. // console.log(part.id, this.opened_entite_id);
  858. if (part.id === this.opened_entite_id) {
  859. this.ctx.fillStyle = "#01ffe2";
  860. } else {
  861. this.ctx.fillStyle = "#000";
  862. }
  863. this.ctx.fill();
  864. }
  865. }
  866. }
  867. }
  868. },
  869. drawContour(){
  870. if (this.salientPoints.length > 3) {
  871. var fillStyle;
  872. let strokeStyle = "#000";
  873. if (!this.isFocused()){
  874. fillStyle = "rgba(255,255,255,0.3)";
  875. }else{
  876. fillStyle = "rgba(255,255,255,0.8)"
  877. if (this.isHover) {
  878. strokeStyle = "#01ffe2";
  879. }
  880. }
  881. this.ctx.beginPath();
  882. this.ctx.lineWidth = 1;
  883. this.ctx.strokeStyle = strokeStyle;
  884. this.ctx.fillStyle = fillStyle;
  885. let gap = 1;//1.15;
  886. this.ctx.moveTo(this.pos.x+this.salientPoints[0].pos.x*this.scale*gap, this.pos.y+this.salientPoints[0].pos.y*this.scale*gap)
  887. for (let j = 1; j < this.salientPoints.length; j++) {
  888. this.ctx.lineTo(this.pos.x+this.salientPoints[j].pos.x*this.scale*gap, this.pos.y+this.salientPoints[j].pos.y*this.scale*gap)
  889. }
  890. this.ctx.lineTo(this.pos.x+this.salientPoints[0].pos.x*this.scale*gap, this.pos.y+this.salientPoints[0].pos.y*this.scale*gap)
  891. this.ctx.fill();
  892. this.ctx.stroke();
  893. // // test draw contour from body part
  894. // for (let i = 0; i < this.body.parts.length; i++) {
  895. // if (this.body.parts[i].item_type === 'concernement-contours'){
  896. // // console.log('concernement contours', this.body.parts[i]);
  897. // this.ctx.beginPath();
  898. // this.ctx.lineWidth = 1;
  899. // this.ctx.strokeStyle = "#F00";
  900. // this.ctx.moveTo(this.body.parts[i].vertices[0].x, this.body.parts[i].vertices[0].y);
  901. // for (let j = 1; j < this.body.parts[i].vertices.length; j++) {
  902. // this.ctx.lineTo(this.body.parts[i].vertices[j].x, this.body.parts[i].vertices[j].y);
  903. // }
  904. // this.ctx.lineTo(this.body.parts[i].vertices[0].x, this.body.parts[i].vertices[0].y);
  905. // this.ctx.stroke();
  906. // // for (let k = 0; k < this.body.parts[i].parts.length; k++) {
  907. // // let part = this.body.parts[i];
  908. // // let partpart = part.parts[k];
  909. // // debugger;
  910. // // this.ctx.beginPath();
  911. // // this.ctx.lineWidth = 1;
  912. // // this.ctx.strokeStyle = "#F00";
  913. // // this.ctx.moveTo(this.body.parts[i].parts[k].vertices[0].x, this.body.parts[i].parts[k].vertices[0].y);
  914. // // for (let j = 1; j < this.body.parts[i].parts[k].vertices.length; j++) {
  915. // // this.ctx.lineTo(this.body.parts[i].parts[k].vertices[j].x, this.body.parts[i].parts[k].vertices[j].y);
  916. // // }
  917. // // this.ctx.lineTo(this.body.parts[i].parts[k].vertices[0].x, this.body.parts[i].parts[k].vertices[0].y);
  918. // // this.ctx.stroke();
  919. // // }
  920. // }
  921. // }
  922. }
  923. },
  924. isFocused(){
  925. return this.map_mode === 'terraindevie'
  926. || (this.map_mode === 'action' && this.concernement.has_agissantes)
  927. || (this.map_mode === 'puissancedagir' && this.concernement.has_puissancedagir)
  928. || (this.map_mode === 'doleancer' && this.concernement.has_doleance);
  929. }
  930. },
  931. render() {
  932. // console.log('render()', this.ctx);
  933. },
  934. }
  935. </script>