Decaffeinate LabelBuffer

This commit is contained in:
Christian Paul 2017-11-02 01:16:59 -07:00
parent 1251e3f257
commit ac784977d9
2 changed files with 57 additions and 45 deletions

View File

@ -1,45 +0,0 @@
###
termap - Terminal Map Viewer
by Michael Strassburger <codepoet@cpan.org>
Using 2D spatial indexing to avoid overlapping labels and markers
and to find labels underneath a mouse cursor's position
###
rbush = require 'rbush'
stringWidth = require 'string-width'
module.exports = class LabelBuffer
tree: null
margin: 5
constructor: (@width, @height) ->
@tree = rbush()
clear: ->
@tree.clear()
project: (x, y) ->
[Math.floor(x/2), Math.floor(y/4)]
writeIfPossible: (text, x, y, feature, margin = @margin) ->
point = @project x, y
if @_hasSpace text, point[0], point[1]
data = @_calculateArea text, point[0], point[1], margin
data.feature = feature
@tree.insert data
else
false
featuresAt: (x, y) ->
@tree.search minX: x, maxX: x, minY: y, maxY: y
_hasSpace: (text, x, y) ->
not @tree.collides @_calculateArea text, x, y
_calculateArea: (text, x, y, margin = 0) ->
minX: x-margin
minY: y-margin/2
maxX: x+margin+stringWidth(text)
maxY: y+margin/2

57
src/LabelBuffer.js Normal file
View File

@ -0,0 +1,57 @@
/*
termap - Terminal Map Viewer
by Michael Strassburger <codepoet@cpan.org>
Using 2D spatial indexing to avoid overlapping labels and markers
and to find labels underneath a mouse cursor's position
*/
'use strict';
const Rbush = require('rbush');
const stringWidth = require('string-width');
module.exports = class LabelBuffer {
constructor() {
this.tree = Rbush();
this.margin = 5;
}
clear() {
this.tree.clear();
}
project(x, y) {
return [Math.floor(x/2), Math.floor(y/4)];
}
writeIfPossible(text, x, y, feature, margin) {
margin = margin || this.margin;
const point = this.project(x, y);
if (this._hasSpace(text, point[0], point[1])) {
const data = this._calculateArea(text, point[0], point[1], margin);
data.feature = feature;
return this.tree.insert(data);
} else {
return false;
}
}
featuresAt(x, y) {
this.tree.search({minX: x, maxX: x, minY: y, maxY: y});
}
_hasSpace(text, x, y) {
return !this.tree.collides(this._calculateArea(text, x, y));
}
_calculateArea(text, x, y, margin = 0) {
return {
minX: x-margin,
minY: y-margin/2,
maxX: x+margin+stringWidth(text),
maxY: y+margin/2,
};
}
}