aboutsummaryrefslogtreecommitdiff
path: root/src_js/vt/svg.ts
blob: 73406fdf5acbaf3535cb27efc91932deb97cc1eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import r from '@hat-open/renderer';
import * as u from '@hat-open/util';

import * as common from '../common';


export function main(): u.VNode | null {
    const result = r.get('result') as common.Result | null;
    const selected = r.get('selected') as {
        panel: string | null,
        item: string | null
    };
    if (!result || !selected.panel)
        return null;

    const panel = result.params.panels[selected.panel];
    const used = u.filter(i => i.panel == selected.panel, result.used);
    const unused = u.filter(i => i.panel == selected.panel, result.unused);

    const fontSize = String(
        Math.max(panel.height, panel.width) * 0.02 *
        u.strictParseFloat(r.get('svg', 'font_size') as string)
    );
    const showNames = r.get('svg', 'show_names');
    const showDimensions = r.get('svg', 'show_dimensions');
    const cutColor = r.get('svg', 'cut_color');
    const itemColor = r.get('svg', 'item_color');
    const selectedItemColor = r.get('svg', 'selected_color');
    const unusedColor = r.get('svg', 'unused_color');

    return ['svg', {
        attrs: {
            width: '100%',
            height: '100%',
            viewBox: [0, 0, panel.width, panel.height].join(' '),
            preserveAspectRatio: 'xMidYMid'
        }},
        ['rect', {
            attrs: {
                x: '0',
                y: '0',
                width: String(panel.width),
                height: String(panel.height),
                'stroke-width': '0',
                fill: cutColor
            }}
        ],
        used.map(used => {
            const item = result.params.items[used.item];
            const width = (used.rotate ? item.height : item.width);
            const height = (used.rotate ? item.width : item.height);
            return ['rect', {
                props: {
                    style: 'cursor: pointer'
                },
                attrs: {
                    x: String(used.x),
                    y: String(used.y),
                    width: String(width),
                    height: String(height),
                    'stroke-width': '0',
                    fill: (used.item == selected.item ? selectedItemColor : itemColor)
                },
                on: {
                    click: () => r.set(['selected', 'item'], used.item)
                }}
            ];
        }),
        unused.map(unused => {
            return ['rect', {
                attrs: {
                    x: String(unused.x),
                    y: String(unused.y),
                    width: String(unused.width),
                    height: String(unused.height),
                    'stroke-width': '0',
                    fill: unusedColor
                }}
            ];
        }),
        used.map(used => {
            const item = result.params.items[used.item];
            const width = (used.rotate ? item.height : item.width);
            const height = (used.rotate ? item.width : item.height);
            const click = () => r.set(['selected', 'item'], used.item);
            return [
                (!showNames ? [] : ['text', {
                    props: {
                        style: 'cursor: pointer'
                    },
                    attrs: {
                        x: String(used.x + width / 2),
                        y: String(used.y + height / 2),
                        'alignment-baseline': 'middle',
                        'text-anchor': 'middle',
                        'font-size': fontSize
                    },
                    on: {
                        click: click
                    }},
                    used.item + (used.rotate ? ' \u293E' : '')
                ]),
                (!showDimensions ? [] : ['text', {
                    props: {
                        style: 'cursor: pointer'
                    },
                    attrs: {
                        x: String(used.x + width / 2),
                        y: String(used.y + height),
                        'alignment-baseline': 'baseline',
                        'text-anchor': 'middle',
                        'font-size': fontSize
                    },
                    on: {
                        click: click
                    }},
                    String(width)
                ]),
                (!showDimensions ? [] : ['text', {
                    props: {
                        style: 'cursor: pointer'
                    },
                    attrs: {
                        x: String(used.x + width),
                        y: String(used.y + height / 2),
                        'transform': `rotate(-90, ${used.x + width}, ${used.y + height / 2})`,
                        'alignment-baseline': 'baseline',
                        'text-anchor': 'middle',
                        'font-size': fontSize
                    },
                    on: {
                        click: click
                    }},
                    String(height)
                ])
            ];
        })
    ];
}