Advertisement
DayDun

OWOP Text Tool

Jun 28th, 2018
4,930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. OWOP.tool.addToolObject(new OWOP.tool.class("Text", OWOP.cursors.write, OWOP.fx.player.NONE, OWOP.RANK.NONE, function(tool) {
  2.     var xPos = null;
  3.     var yPos = null;
  4.     var fonts = {};
  5.     var font = null;
  6.    
  7.     var fontInput = new OWOP.windowSys.class.input("Choose Font", 955, "number", function(value) {
  8.         var id = parseInt(value);
  9.         if (id in fonts) {
  10.             font = id;
  11.             return;
  12.         }
  13.        
  14.         var xhttp = new XMLHttpRequest();
  15.         xhttp.addEventListener("load", function() {
  16.             var source = xhttp.responseXML.body.children[2].innerHTML;
  17.             var data = JSON.parse(source.match(/loadData\('(.+)'\)/)[1]);
  18.             var meta = source.match(/drawSample\('',([0-9]+),(-?[0-9]+)\)/);
  19.             data.letterspace = parseInt(meta[1]);
  20.             data.monospacewidth = parseInt(meta[2]);
  21.            
  22.             fonts[id] = data;
  23.             font = id;
  24.         });
  25.         xhttp.open("GET", "https://cors-anywhere.herokuapp.com/http://www.pentacom.jp/pentacom/bitfontmaker2/gallery/?id=" + id);
  26.         xhttp.responseType = "document";
  27.         xhttp.send();
  28.     });
  29.    
  30.     var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
  31.     chars += "¡¢£€¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
  32.     chars += "ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽž";
  33.    
  34.     tool.setFxRenderer(function (fx, ctx, time) {
  35.         var x = fx.extra.player.x;
  36.         var y = fx.extra.player.y;
  37.         if (xPos !== null && yPos !== null) {
  38.             x = xPos * 16;
  39.             y = yPos * 16;
  40.         }
  41.         var fxx = (Math.floor(x / 16) - OWOP.camera.x) * OWOP.camera.zoom;
  42.         var fxy = (Math.floor(y / 16) - OWOP.camera.y) * OWOP.camera.zoom;
  43.         ctx.globalAlpha = 0.8;
  44.         ctx.strokeStyle = fx.extra.player.htmlRgb;
  45.         ctx.strokeRect(fxx, fxy, OWOP.camera.zoom, OWOP.camera.zoom * 12);
  46.         return 0;
  47.     });
  48.    
  49.     tool.setEvent("select", function() {
  50.         OWOP.windowSys.addWindow(fontInput);
  51.     });
  52.     tool.setEvent("deselect", function() {
  53.         font = null;
  54.     });
  55.    
  56.     tool.setEvent("mousedown mousemove", function (mouse, event) {
  57.         if (mouse.buttons === 1) {
  58.             xPos = mouse.tileX;
  59.             yPos = mouse.tileY;
  60.         }
  61.     });
  62.     tool.setEvent("keydown", function() {return true;});
  63.     tool.setEvent("keyup", function() {return true;});
  64.    
  65.     window.addEventListener("keypress", function(event) {
  66.         if (font === null || xPos === null || yPos === null || ["INPUT", "TEXTAREA"].includes(document.activeElement.tagName)) {
  67.             return;
  68.         }
  69.        
  70.         var f = fonts[font];
  71.         var letterSpacing = (f.letterspace / 64 | 0) - 1;
  72.         var isMono = f.monospacewidth !== -1;
  73.        
  74.         if (event.which == 32) {
  75.             xPos += isMono ? f.monospacewidth : 4 + letterSpacing;
  76.             return;
  77.         }
  78.        
  79.         var char = f[event.which];
  80.         if (!char) {
  81.             return;
  82.         }
  83.        
  84.         var width = 0;
  85.         for (var y=0; y<16; y++) {
  86.             for (var x=0; x<16; x++) {
  87.                 if (char[y] & (1 << x) && x > width) width = x;
  88.             }
  89.         }
  90.        
  91.         var color = OWOP.player.palette[OWOP.player.paletteIndex];
  92.         for (var y=0; y<16; y++) {
  93.             for (var x=0; x<16; x++) {
  94.                 if (!(char[y] & (1 << x))) {
  95.                     continue;
  96.                 }
  97.                 OWOP.world.setPixel(xPos + x - 2, yPos + y, color);
  98.             }
  99.         }
  100.        
  101.         xPos += isMono ? f.monospacewidth : width + letterSpacing;
  102.     });
  103. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement