Last active
June 25, 2024 14:47
-
-
Save Riemnand/8959509ea8aa1ad3c3d25ba85f74884e to your computer and use it in GitHub Desktop.
Robo's istrolid script, which adds a convenient ruler for measuring distances on the battlefield.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A script that adds a ruler tool to the Istrolid. | |
Activation set on ~ (tilde, the top-left button under the esc key) button by default. It can be changed in settings. | |
You can make multiple points by activating the ruler and then clicking left mouse button. Pretty satisfying drawing process btw! | |
Made by RoboDrone @riemnand. | |
*/ | |
//ruler script: | |
(function() { | |
if (window.rulerScriptLoaded) { | |
return console.error("Error loading Robo's ruler script: another one is already loaded.") | |
} else { | |
window.rulerScriptLoaded = true | |
} | |
try { | |
var rulerScriptHotkey = window.rulerScriptHotkey || { | |
onkeydown: BattleMode.prototype.onkeydown | |
}; | |
var rulerScriptMouseControls = window.rulerScriptMouseControls || { | |
onmousedown: BattleMode.prototype.onmousedown | |
} | |
var rulerScriptDraw = window.rulerScriptDraw || { | |
draw: Interpolator.prototype.draw | |
}; | |
window.DEFAULT_SETTINGS.RulerKey = { | |
keys: [{ | |
which: 192 // ` and ~ | |
}, null] | |
}; | |
function simpleContrastRGBA(rgba) { | |
var rgb = rgba.slice(0, -1), | |
max = Math.max(...rgb), | |
min = Math.min(...rgb), | |
maxIndex, minIndex, correctedRgb; | |
for (let i = 0; i < 3; i++) { | |
if (rgb[i] === max) { | |
maxIndex = i | |
} | |
if (rgb[i] === min) { | |
minIndex = i | |
} | |
} | |
rgb[maxIndex] = min; | |
rgb[minIndex] = max; | |
brightnessMultiplier = 255 / (0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]); | |
correctedRgb = rgb.map(num => Math.min(num * brightnessMultiplier, 255)); | |
correctedRgb.push(rgba[3]); | |
return correctedRgb | |
} | |
function bgColor() { | |
var result = [], | |
spotAlphaValue = intp.theme.spotColor[3] / 255 | |
for (let i = 0; i < 4; i++) { | |
result.push(Math.min(Math.max(intp.theme.fillColor[i] + intp.theme.spotColor[i] * spotAlphaValue, 1), 255)) | |
}; | |
return result | |
} | |
function drawLine(startPoint, endPoint) { // used R26's drawLine code from https://gist.github.com/Rio6/8135d6b4145e6402e29bffbebc4f3b03 | |
var offset = v2.create, | |
rot, d, bias; | |
v2.sub(endPoint, startPoint, offset); | |
rot = v2.angle(offset); | |
d = v2.mag(offset) / 256; | |
v2.scale(offset, .5); | |
v2.add(offset, startPoint); | |
baseAtlas.drawSprite("img/newbg/fill.png", offset, [0.16, d], rot, simpleContrastRGBA(bgColor())); | |
} | |
function drawString(string, point) { | |
var bias = v2.create([40 * battleMode.zoom, 0]); | |
string.split('').forEach(symbol => { | |
baseAtlas.drawSprite("parts/decals/letter" + symbol.toUpperCase() + ".png", point, [-1 * battleMode.zoom, battleMode.zoom], Math.PI, simpleContrastRGBA(bgColor())); | |
v2.add(point, bias); | |
}) | |
} | |
var startPoint = v2.create(), | |
endPoint = v2.create(), | |
pointsBetween = [], | |
active = false; | |
BattleMode.prototype.onkeydown = function(e) { | |
if (settings.key(e, "RulerKey")) { | |
v2.set(battleMode.mouse, startPoint); | |
active = !active; | |
pointsBetween = [] | |
}; | |
return rulerScriptHotkey.onkeydown.call(this, e); | |
} | |
BattleMode.prototype.onmousedown = function(e) { | |
if (e.which === 1) { | |
if (active) { | |
pointsBetween.push(v2.create(battleMode.mouse)) | |
} | |
} | |
return rulerScriptMouseControls.onmousedown.call(this, e) | |
} | |
window.battleMode = new BattleMode() | |
Interpolator.prototype.draw = function() { | |
rulerScriptDraw.draw.call(this); | |
var distance = 0, | |
offset = relativeEndPoint = v2.create(), | |
bias, expectedDigits, points, previousPoint; | |
const log = (a, b) => { | |
return Math.log(b) / Math.log(a) | |
}; | |
if (active) { | |
if (ui.mode === 'battle') { | |
v2.set(battleMode.mouse, endPoint); | |
points = [startPoint, ...pointsBetween, endPoint]; | |
previousPoint = points[points.length - 2]; | |
for (let i = 1; i < points.length; i++) { | |
drawLine(points[i - 1], points[i]); | |
distance += v2.mag(v2.sub(points[i - 1], points[i], relativeEndPoint)); | |
} | |
expectedDigits = Math.floor(log(10, distance)) + 1 | |
v2.direction(endPoint, previousPoint, offset); | |
bias = (40 + Math.max((expectedDigits * 40) * -1 * (offset[0] + Math.abs(offset[1])), 0)) * battleMode.zoom | |
v2.scale(offset, bias); | |
v2.add(offset, endPoint); | |
drawString(Math.round(distance).toString() + "m", offset) | |
} else { | |
active = false | |
} | |
} | |
} | |
} catch (err) { | |
console.error("Error in Robo's ruler script:\n" + err) | |
active = false | |
} | |
}).call(this) | |
//end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Peek.2024-03-23.08-54.mp4