1<!-- Copyright (C) 2017 The Android Open Source Project
2
3     Licensed under the Apache License, Version 2.0 (the "License");
4     you may not use this file except in compliance with the License.
5     You may obtain a copy of the License at
6
7          http://www.apache.org/licenses/LICENSE-2.0
8
9     Unless required by applicable law or agreed to in writing, software
10     distributed under the License is distributed on an "AS IS" BASIS,
11     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12     See the License for the specific language governing permissions and
13     limitations under the License.
14-->
15<template>
16  <div class="bounds" :style="boundsStyle">
17    <div
18      class="rect" v-for="r in filteredRects"
19      :style="rectToStyle(r)"
20      @click="onClick(r)"
21      v-bind:key="`${r.left}-${r.right}-${r.top}-${r.bottom}-${r.ref.name}`"
22    >
23      <span class="label">{{r.label}}</span>
24    </div>
25    <div class="highlight" v-if="highlight" :style="rectToStyle(highlight)" />
26  </div>
27</template>
28
29<script>
30
31// eslint-disable-next-line camelcase
32import {multiplyRect} from './matrix_utils.js';
33
34export default {
35  name: 'rects',
36  props: ['bounds', 'rects', 'highlight'],
37  data() {
38    return {
39      desiredHeight: 800,
40      desiredWidth: 400,
41    };
42  },
43  computed: {
44    boundsC() {
45      if (this.bounds) {
46        return this.bounds;
47      }
48      const width = Math.max(
49          ...this.rects.map((r) => multiplyRect(r.transform, r).right));
50      const height = Math.max(
51          ...this.rects.map((r) => multiplyRect(r.transform, r).bottom));
52      return {width, height};
53    },
54    boundsStyle() {
55      return this.rectToStyle({top: 0, left: 0, right: this.boundsC.width,
56        bottom: this.boundsC.height});
57    },
58    filteredRects() {
59      return this.rects.filter((rect) => {
60        const isVisible = rect.ref.isVisible;
61        console.warn(`Name: ${rect.ref.name}`, `Kind: ${rect.ref.kind}`,
62            `isVisible=${isVisible}`);
63        return isVisible;
64      });
65    },
66  },
67  methods: {
68    s(sourceCoordinate) { // translate source into target coordinates
69      let scale;
70      if (this.boundsC.width < this.boundsC.height) {
71        scale = this.desiredHeight / this.boundsC.height;
72      } else {
73        scale = this.desiredWidth / this.boundsC.width;
74      }
75      return sourceCoordinate * scale;
76    },
77    rectToStyle(r) {
78      const x = this.s(r.left);
79      const y = this.s(r.top);
80      const w = this.s(r.right) - this.s(r.left);
81      const h = this.s(r.bottom) - this.s(r.top);
82
83      let t;
84      if (r.transform && r.transform.matrix) {
85        t = r.transform.matrix;
86      } else {
87        t = r.transform;
88      }
89
90      const tr = t ? `matrix(${t.dsdx}, ${t.dtdx}, ${t.dsdy}, ${t.dtdy}, ` +
91          `${this.s(t.tx)}, ${this.s(t.ty)})` : '';
92      const rectStyle = `top: ${y}px; left: ` +
93            `${x}px; height: ${h}px; width: ${w}px; ` +
94            `transform: ${tr}; transform-origin: 0 0;`;
95      if (r && r.ref) {
96        console.log(`${r.ref.name} - ${rectStyle}`);
97      }
98      return rectStyle;
99    },
100    onClick(r) {
101      this.$emit('rect-click', r.ref);
102    },
103  },
104};
105</script>
106
107<style scoped>
108.bounds {
109  position: relative;
110  overflow: hidden;
111}
112.highlight, .rect {
113  position: absolute;
114  box-sizing: border-box;
115  display: flex;
116  justify-content: flex-end;
117}
118.rect {
119  border: 1px solid black;
120  background-color: rgba(110, 114, 116, 0.8);
121}
122.highlight {
123  border: 2px solid rgb(235, 52, 52);
124  pointer-events: none;
125}
126.label {
127  align-self: center;
128}
129</style>
130