|
| 1 | +<script> |
| 2 | +// Line drawing tool |
| 3 | +
|
| 4 | +import Overlay from '../../mixins/overlay.js' |
| 5 | +import Tool from '../../mixins/tool.js' |
| 6 | +import Icons from '../../stuff/icons.json' |
| 7 | +import Pin from '../primitives/pin.js' |
| 8 | +import Seg from '../primitives/seg.js' |
| 9 | +
|
| 10 | +export default { |
| 11 | + name: 'RangeTool', |
| 12 | + mixins: [Overlay, Tool], |
| 13 | + methods: { |
| 14 | + meta_info() { |
| 15 | + return { author: 'C451', version: '1.0.0' } |
| 16 | + }, |
| 17 | + tool() { |
| 18 | + return { |
| 19 | + // Descriptor for the tool |
| 20 | + group: 'Measurements', icon: Icons['price_range.png'], |
| 21 | + type: 'Price', |
| 22 | + hint: 'Price Range', |
| 23 | + data: [], // Default data |
| 24 | + settings: {} // Default settings |
| 25 | + } |
| 26 | + }, |
| 27 | + // Called after overlay mounted |
| 28 | + init() { |
| 29 | + // First pin is settled at the mouse position |
| 30 | + this.pins.push(new Pin(this, 'p1')) |
| 31 | + // Second one is following mouse until it clicks |
| 32 | + this.pins.push(new Pin(this, 'p2', { |
| 33 | + state: 'tracking' |
| 34 | + })) |
| 35 | + this.pins[1].on('settled', () => { |
| 36 | + // Call when current tool drawing is finished |
| 37 | + // (Optionally) reset the mode back to 'Cursor' |
| 38 | + this.set_state('finished') |
| 39 | + this.$emit('drawing-mode-off') |
| 40 | + }) |
| 41 | + }, |
| 42 | + draw(ctx) { |
| 43 | + if (!this.p1 || !this.p2) return |
| 44 | +
|
| 45 | + ctx.lineWidth = this.line_width |
| 46 | + ctx.strokeStyle = this.color |
| 47 | + ctx.beginPath() |
| 48 | +
|
| 49 | + // Top |
| 50 | + new Seg(this, ctx).draw( |
| 51 | + [this.p1[0], this.p2[1]], [this.p2[0], this.p2[1]] |
| 52 | + ) |
| 53 | + // Bottom |
| 54 | + new Seg(this, ctx).draw( |
| 55 | + [this.p1[0], this.p1[1]], [this.p2[0], this.p1[1]] |
| 56 | + ) |
| 57 | + ctx.stroke() |
| 58 | +
|
| 59 | + // Vertical |
| 60 | + ctx.beginPath() |
| 61 | + ctx.setLineDash([5, 5]) |
| 62 | + new Seg(this, ctx).draw( |
| 63 | + [(this.p1[0] + this.p2[0]) * 0.5, this.p2[1]], |
| 64 | + [(this.p1[0] + this.p2[0]) * 0.5, this.p1[1]] |
| 65 | + ) |
| 66 | + ctx.stroke() |
| 67 | + ctx.setLineDash([]) |
| 68 | + this.render_pins(ctx) |
| 69 | +
|
| 70 | + }, |
| 71 | + use_for() { return ['RangeTool'] }, |
| 72 | + data_colors() { return [this.color] } |
| 73 | + }, |
| 74 | + // Define internal setting & constants here |
| 75 | + computed: { |
| 76 | + sett() { |
| 77 | + return this.$props.settings |
| 78 | + }, |
| 79 | + p1() { |
| 80 | + return this.$props.settings.p1 |
| 81 | + }, |
| 82 | + p2() { |
| 83 | + return this.$props.settings.p2 |
| 84 | + }, |
| 85 | + line_width() { |
| 86 | + return this.sett.lineWidth || 0.9 |
| 87 | + }, |
| 88 | + color() { |
| 89 | + return this.sett.color || |
| 90 | + this.$props.colors.colorCross |
| 91 | + } |
| 92 | + }, |
| 93 | + data() { |
| 94 | + return {} |
| 95 | + } |
| 96 | +
|
| 97 | +} |
| 98 | +</script> |
0 commit comments