Advertisement
BombBloke

Banner Editor (ComputerCraft)

Apr 4th, 2016
2,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.46 KB | None | 0 0
  1. -- +--------------------------------------------------------+
  2. -- |                                                        |
  3. -- |                        BanEdit                         |
  4. -- |                                                        |
  5. -- +--------------------------------------------------------+
  6.  
  7. local version = "Version 1.0.1"
  8.  
  9. -- By Jeffrey Alexander, aka Bomb Bloke.
  10. -- Command Computer script for the purpose of editing banners.
  11. -- http://www.computercraft.info/forums2/index.php?/topic/26391-mc18-banner-editor/
  12.  
  13. ---------------------------------------------
  14. ------------Variable Declarations------------
  15. ---------------------------------------------
  16.  
  17. if not blittle then
  18.     if not (fs.exists("blittle") or fs.exists(shell.resolve("blittle"))) then
  19.         shell.run("pastebin get ujchRSnU blittle")
  20.         os.loadAPI(shell.resolve("blittle"))
  21.     else os.loadAPI(fs.exists("blittle") and "blittle" or shell.resolve("blittle")) end
  22. end
  23.  
  24. local xSize, ySize, myEvent
  25. local x, y, z = commands.getBlockPosition()
  26. local patternLookup, colourNum, colourChar, layers, bg = {}, {}, {}, {}, colours.white
  27. local win = blittle.createWindow(term.current(), 40, 3, 10, 13, false)
  28. local cursor = {{">>  ","  <<"},{"> > "," < <"},{" >> "," << "},{"> > "," < <"}}
  29.  
  30. local patterns = {
  31.     {"--------------------------", "-",   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  32.     {"Base Fess",                  "bs",  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575}},
  33.     {"Chief Fess",                 "ts",  {1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  34.     {"Pale Dexter",                "ls",  {127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127}},
  35.     {"Pale Sinister",              "rs",  {1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384, 1040384}},
  36.     {"Pale",                       "cs",  {8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064, 8064}},
  37.     {"Fess",                       "ms",  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  38.     {"Bend",                       "drs", {15, 15, 31, 31, 63, 63, 127, 127, 254, 254, 508, 508, 1016, 1016, 2032, 2032, 4064, 4064, 8128, 8128, 16256, 16256, 32512, 32512, 65024, 65024, 130048, 130048, 260096, 260096, 520192, 520192, 1040384, 1040384, 1032192, 1032192, 1015808, 1015808, 983040}},
  39.     {"Bend Sinister",              "dls", {983040, 983040, 1015808, 1015808, 1032192, 1032192, 1040384, 1040384, 520192, 520192, 260096, 260096, 130048, 130048, 65024, 65024, 32512, 32512, 16256, 16256, 8128, 8128, 4064, 4064, 2032, 2032, 1016, 1016, 508, 508, 254, 254, 127, 127, 63, 63, 31, 31, 15}},
  40.     {"Paly",                       "ss",  {473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550, 473550}},
  41.     {"Saltire",                    "cr",  {917511, 917511, 983055, 983055, 1015839, 1015839, 507966, 507966, 254076, 254076, 127224, 127224, 63984, 63984, 32736, 32736, 16320, 16320, 8064, 8064, 8064, 8064, 16320, 16320, 32736, 32736, 63984, 63984, 127224, 127224, 254076, 254076, 507966, 507966, 1015839, 1015839, 983055, 983055, 917511}},
  42.     {"Cross",                      "sc",  {3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 1048575, 1048575, 1048575, 1048575, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840}},
  43.     {"Per Bend Sinister",          "ld",  {1048575, 1048575, 524287, 524287, 262143, 262143, 131071, 131071, 65535, 65535, 32767, 32767, 16383, 16383, 8191, 8191, 4095, 4095, 2047, 2047, 1023, 1023, 511, 511, 255, 255, 127, 127, 63, 63, 31, 31, 15, 15, 7, 7, 3, 3, 1}},
  44.     {"Per Bend",                   "rud", {1048575, 1048575, 1048574, 1048574, 1048572, 1048572, 1048568, 1048568, 1048560, 1048560, 1048544, 1048544, 1048512, 1048512, 1048448, 1048448, 1048320, 1048320, 1048064, 1048064, 1047552, 1047552, 1046528, 1046528, 1044480, 1044480, 1040384, 1040384, 1032192, 1032192, 1015808, 1015808, 983040, 983040, 917504, 917504, 786432, 786432, 524288}},
  45.     {"Per Bend Inverted",          "lud", {1, 1, 3, 3, 7, 7, 15, 15, 31, 31, 63, 63, 127, 127, 255, 255, 511, 511, 1023, 1023, 2047, 2047, 4095, 4095, 8191, 8191, 16383, 16383, 32767, 32767, 65535, 65535, 131071, 131071, 262143, 262143, 524287, 524287, 1048575}},
  46.     {"Per Bend Sinister Inverted", "rd",  {524288, 524288, 786432, 786432, 917504, 917504, 983040, 983040, 1015808, 1015808, 1032192, 1032192, 1040384, 1040384, 1044480, 1044480, 1046528, 1046528, 1047552, 1047552, 1048064, 1048064, 1048320, 1048320, 1048448, 1048448, 1048512, 1048512, 1048544, 1048544, 1048560, 1048560, 1048568, 1048568, 1048572, 1048572, 1048574, 1048574, 1048575}},
  47.     {"Per Pale",                   "vh",  {2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047}},
  48.     {"Per Pale Inverted",          "vhr", {1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064, 1048064}},
  49.     {"Per Fess",                   "hh",  {1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  50.     {"Per Fess Inverted",          "hhb", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575, 1048575}},
  51.     {"Base Dexter Canton",         "bl",  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023}},
  52.     {"Base Sinister Canton",       "br",  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552}},
  53.     {"Chief Dexter Canton",        "tl",  {1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  54.     {"Chief Sinister Canton",      "tr",  {1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 1047552, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  55.     {"Chevron",                    "bt",  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1536, 3840, 3840, 8064, 8064, 16320, 16320, 32736, 32736, 65520, 65520, 131064, 131064, 262140, 262140, 524286, 524286, 1048575, 1048575}},
  56.     {"Inverted Chevron",           "tt",  {1048575, 1048575, 1048575, 524286, 524286, 262140, 262140, 131064, 131064, 65520, 65520, 32736, 32736, 16320, 16320, 8064, 8064, 3840, 3840, 1536, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  57.     {"Base Indented",              "bts", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 198156, 495390, 495390, 1040319, 1040319, 1048575}},
  58.     {"Chief Indented",             "tts", {1048575, 1048575, 1040319, 1040319, 495390, 495390, 198156, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  59.     {"Roundel",                    "mc",  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1536, 8064, 16320, 32736, 32736, 65520, 65520, 32736, 32736, 16320, 8064, 1536, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  60.     {"Lozenge",                    "mr",  {0, 0, 0, 0, 0, 0, 0, 0, 1536, 3840, 3840, 8064, 8064, 16320, 16320, 32736, 32736, 65520, 65520, 131064, 131064, 65520, 65520, 32736, 32736, 16320, 16320, 8064, 8064, 3840, 3840, 1536, 0, 0, 0, 0, 0, 0, 0}},
  61.     {"Bordure",                    "bo",  {1048575, 1048575, 1048575, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 917511, 1048575, 1048575}},
  62.     {"Bordure Indented",           "cbo", {1048575, 1047039, 1040511, 1040511, 1032255, 1015839, 983055, 786435, 786435, 524289, 524289, 786435, 786435, 983055, 1015839, 1015839, 983055, 786435, 786435, 524289, 524289, 786435, 786435, 983055, 1015839, 1015839, 983055, 786435, 786435, 524289, 524289, 786435, 786435, 983055, 1015839, 1032255, 1040511, 1040511, 1047039}},
  63.     {"Field Masoned",              "bri", {915423, 915423, 915423, 0, 1031931, 1031931, 1031931, 0, 915423, 915423, 915423, 0, 1031931, 1031931, 1031931, 0, 915423, 915423, 915423, 0, 1031931, 1031931, 1031931, 0, 915423, 915423, 915423, 0, 1031931, 1031931, 1031931, 0, 915423, 915423, 915423, 0, 1031931, 1031931, 1031931}},
  64.     {"Gradient",                   "gra", {1048575, 1048575, 489335, 1048575, 1040351, 1048575, 489335, 1048575, 349525, 1048575, 349525, 1048575, 349525, 768955, 349525, 978670, 349525, 768955, 349525, 699050, 349525, 666154, 349525, 699050, 349525, 139810, 349525, 526344, 349525, 131586, 349525, 0, 349525, 0, 69905, 0, 263172, 0, 69905}},
  65.     {"Base Gradient",              "gru", {0, 0, 69905, 0, 345157, 0, 69905, 0, 349525, 0, 349525, 0, 349525, 139810, 349525, 567466, 349525, 139810, 349525, 699050, 349525, 764859, 349525, 978670, 349525, 768955, 349525, 1048575, 349525, 1048575, 481111, 1048575, 349525, 1048575, 489335, 1048575, 1040351, 1048575, 1015679}},
  66.     {"Creeper Charge",             "cre", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 516222, 516222, 516222, 516222, 516222, 516222, 8064, 8064, 8064, 65520, 65520, 65520, 65520, 65520, 65520, 57456, 57456, 57456, 0, 0, 0, 0, 0, 0, 0}},
  67.     {"Skull Charge",               "sku", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16320, 16320, 16320, 16320, 9792, 14784, 8256, 16320, 196620, 229404, 254076, 31200, 8064, 16320, 258300, 245820, 196620, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  68.     {"Flower Charge",              "flo", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1536, 53040, 126840, 65520, 196596, 516222, 511806, 253884, 57264, 57264, 253884, 511806, 516222, 196596, 65520, 126840, 53040, 1536, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  69.     {"Mojang Charge",              "moj", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16384, 49152, 49632, 54264, 16380, 131068, 123004, 114748, 229436, 196668, 65596, 60, 124, 252, 197628, 262136, 65504, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}}
  70.  
  71. ---------------------------------------------
  72. ------------Function Declarations------------
  73. ---------------------------------------------
  74.  
  75. -- Returns whether a click was performed at a given location.
  76. -- If one parameter is passed, it checks to see if y is [1].
  77. -- If two parameters are passed, it checks to see if x is [1] and y is [2].
  78. -- If three parameters are passed, it checks to see if x is between [1]/[2] (non-inclusive) and y is [3].
  79. -- If four paramaters are passed, it checks to see if x is between [1]/[2] and y is between [3]/[4] (non-inclusive).
  80. local function clickedAt(...)
  81.     if myEvent[1] ~= "mouse_click" then return false end
  82.     if #arg == 1 then return (arg[1] == myEvent[4])
  83.     elseif #arg == 2 then return (myEvent[3] == arg[1] and myEvent[4] == arg[2])
  84.     elseif #arg == 3 then return (myEvent[3] > arg[1] and myEvent[3] < arg[2] and myEvent[4] == arg[3])
  85.     else return (myEvent[3] > arg[1] and myEvent[3] < arg[2] and myEvent[4] > arg[3] and myEvent[4] < arg[4]) end
  86. end
  87.  
  88. -- Returns whether one of a given set of keys was pressed.
  89. local function pressedKey(...)
  90.     if myEvent[1] ~= "key" then return false end
  91.     for i=1,#arg do if arg[i] == myEvent[2] then return true end end
  92.     return false
  93. end
  94.  
  95. -- Not feature-complete, but should work within the confines of this particular script.
  96. local function unserialiseJSON(json)
  97.     local i = 1
  98.    
  99.     while true do
  100.         local curChar = json:sub(i, i)
  101.         i = i + 1
  102.        
  103.         if curChar == "{" then
  104.             -- Map (Compound).
  105.             local key, result, counter = {}, {}, 1
  106.            
  107.             repeat
  108.                 curChar = json:sub(i, i)
  109.                 i = i + 1
  110.                
  111.                 if curChar == ":" then
  112.                     result[table.concat(key)], json = unserialiseJSON(json:sub(i))
  113.                     key, counter, i = {}, 1, 1
  114.                 elseif curChar ~= "," then
  115.                     key[counter] = curChar
  116.                     counter = counter + 1
  117.                 end
  118.             until curChar == "}"
  119.            
  120.             return result, json:sub(i)
  121.         elseif curChar == "[" then
  122.             -- Array (List).
  123.             local result, counter = {}, 1
  124.            
  125.             repeat
  126.                 curChar = json:sub(i, i)
  127.                 i = i + 1
  128.                
  129.                 if curChar == ":" then
  130.                     result[counter], json = unserialiseJSON(json:sub(i))
  131.                     counter, i = counter + 1, 1
  132.                 elseif curChar == "{" then
  133.                     result[counter], json = unserialiseJSON(json:sub(i - 1))
  134.                     counter, i = counter + 1, 1
  135.                 end
  136.             until curChar == "]"
  137.            
  138.             return result, json:sub(i)
  139.         elseif curChar == "\"" then
  140.             -- String.
  141.             local result, counter = {curChar}, 2
  142.            
  143.             repeat
  144.                 curChar = json:sub(i, i)
  145.                 i = i + 1
  146.                
  147.                 if curChar == "\\" then
  148.                     curChar = curChar .. json:sub(i, i)
  149.                     i = i + 1
  150.                 end
  151.                
  152.                 result[counter] = curChar
  153.                 counter = counter + 1
  154.             until curChar == "\""
  155.            
  156.             return textutils.unserialise(table.concat(result)), json:sub(i)
  157.         else
  158.             -- Number.
  159.             local result, counter = {curChar}, 2
  160.            
  161.             repeat
  162.                 curChar = json:sub(i, i)
  163.                 result[counter] = curChar
  164.                 i, counter = i + 1, counter + 1
  165.             until curChar == "," or curChar == "}" or curChar == "]"
  166.            
  167.             repeat result[#result] = nil until tonumber(result[#result])
  168.            
  169.             return tonumber(table.concat(result)), json:sub(i - 1)
  170.         end
  171.     end
  172. end
  173.  
  174. local function getNBT(...)
  175.     for i = 1, #arg do arg[i] = tostring(arg[i]) end
  176.     arg = table.concat(arg, " ")
  177.    
  178.     local _, input = commands.blockdata(arg .. " {}")
  179.     return unserialiseJSON(input[1]:sub(30))
  180. end
  181.  
  182. local function drawPattern(thisPattern)
  183.     local thisLayer, thisChar = patterns[thisPattern[1]][3], colourChar[thisPattern[2]]
  184.    
  185.     for y = 1, 39 do
  186.         local x, ystart, imageY = 1, 1, thisLayer[y]
  187.         for i = 1, #imageY do
  188.             local imageYI = imageY[i]
  189.             if type(imageYI) == "number" then
  190.                 x = x + imageYI
  191.             else
  192.                 term.setCursorPos(x, y)
  193.                 term.blit(imageYI, imageYI, string.rep(thisChar, #imageYI))
  194.                 x = x + #imageYI
  195.             end
  196.         end
  197.     end
  198. end
  199.  
  200. local function drawBanner(startLayer)
  201.     local oldTerm = term.redirect(win)
  202.    
  203.     if startLayer == 1 then
  204.         term.setBackgroundColour(bg)
  205.         term.clear()
  206.     end
  207.    
  208.     for pattern = startLayer, 16 do
  209.         local thisLayer = layers[pattern]
  210.         if thisLayer[1] ~= 1 then drawPattern(thisLayer) end
  211.     end
  212.    
  213.     win.setVisible(true)
  214.     win.setVisible(false)
  215.     term.redirect(oldTerm)
  216. end
  217.  
  218. local function drawPickerPattern(position, listWin)
  219.     term.redirect(win)
  220.     term.setBackgroundColour(colours.white)
  221.     term.clear()
  222.     drawPattern({position, colours.black})
  223.     win.setVisible(true)
  224.     win.setVisible(false)
  225.     term.redirect(listWin)
  226. end
  227.  
  228. local function patternPicker(position)
  229.     term.setBackgroundColour(colours.black)
  230.     term.clear()
  231.    
  232.     term.setTextColour(colours.cyan)
  233.     term.setBackgroundColour(colours.brown)
  234.    
  235.     term.setCursorPos(39, 2)
  236.     term.write("\159" .. string.rep("\143", 10))
  237.     term.blit("\144", "c", "9")
  238.    
  239.     term.setTextColour(colours.brown)
  240.     term.setBackgroundColour(colours.cyan)
  241.    
  242.     term.setCursorPos(39, 16)
  243.     term.write("\130" .. string.rep("\131", 10))
  244.     term.blit("\129", "c", "9")
  245.    
  246.     for y = 3, 15 do
  247.         term.setCursorPos(39, y)
  248.         term.blit("\149", "9", "c")
  249.        
  250.         term.setCursorPos(50, y)
  251.         term.blit("\149", "c", "9")
  252.     end
  253.    
  254.     local oldTerm = term.current()
  255.     local listWin = window.create(oldTerm, 1, 1, 37, 19)
  256.    
  257.     drawPickerPattern(position, listWin)
  258.    
  259.     local xSize, ySize = term.getSize()
  260.     local lastPosition, animationTimer, curCount = 0, os.startTimer(0), 1
  261.  
  262.     while true do
  263.         myEvent = {os.pullEvent()}
  264.  
  265.         -- Track bouncing cursor.
  266.         if myEvent[1] == "timer" and myEvent[2] == animationTimer then
  267.             curCount = curCount == 4 and 1 or (curCount + 1)
  268.             animationTimer = os.startTimer(0.5)
  269.             myEvent[1] = "cabbage"
  270.  
  271.         -- Bail.
  272.         elseif pressedKey(keys.backspace) or (myEvent[1] == "mouse_click" and myEvent[2] == 2) then
  273.             term.redirect(oldTerm)
  274.             return 1
  275.  
  276.         -- Move down the list.
  277.         elseif pressedKey(keys.down, keys.s) or (myEvent[1] == "mouse_scroll" and myEvent[2] == 1) then
  278.             position = position == #patterns and 1 or (position + 1)
  279.             drawPickerPattern(position, listWin)
  280.  
  281.         -- Move up the list.
  282.         elseif pressedKey(keys.up, keys.w) or (myEvent[1] == "mouse_scroll" and myEvent[2] == -1) then
  283.             position = position == 1 and #patterns or (position - 1)
  284.             drawPickerPattern(position, listWin)
  285.  
  286.         -- Select something.
  287.         elseif pressedKey(keys.enter, keys.space) or clickedAt(0, xSize + 1, math.floor(ySize / 2) + 1) then
  288.             term.redirect(oldTerm)
  289.             return position
  290.  
  291.         -- User clicked somewhere on the list; move that entry to the currently-selected position.
  292.         elseif clickedAt(0, xSize + 1, 0, ySize + 1) then
  293.             position = position + myEvent[4] - math.floor(ySize / 2) - 1
  294.             position = position > #patterns and #patterns or position
  295.             position = position < 1 and 1 or position
  296.             drawPickerPattern(position, listWin)
  297.         end
  298.  
  299.         -- Update other screen stuff.
  300.         if myEvent[1] ~= "timer" then
  301.             -- File list.
  302.             term.setBackgroundColour(colours.black)
  303.             for y = position == lastPosition and (math.floor(ySize / 2) + 1) or 1, position == lastPosition and (math.floor(ySize / 2) + 1) or ySize do
  304.                 local thisLine = y + position - math.floor(ySize / 2) - 1
  305.  
  306.                 if patterns[thisLine] then
  307.                     local thisString = patterns[thisLine][1]
  308.                    
  309.                     if thisLine == position then
  310.                         term.setCursorPos(math.floor((xSize - #thisString - 8) / 2) + 1, y)
  311.                         term.clearLine()
  312.                         term.setTextColour(colours.blue)
  313.                         term.write(cursor[curCount][1])
  314.                         term.setTextColour(colours.cyan)
  315.                         term.write(thisString)
  316.                         term.setTextColour(colours.blue)
  317.                         term.write(cursor[curCount][2])
  318.                     else
  319.                         term.setCursorPos(math.floor((xSize - #thisString) / 2) + 1, y)
  320.                         term.clearLine()
  321.  
  322.                         if y == 1 or y == ySize then term.setTextColour(colours.grey)
  323.                         elseif y == 2 or y == ySize - 1 then term.setTextColour(colours.lightGrey)
  324.                         else term.setTextColour(colours.white) end
  325.  
  326.                         term.write(thisString)
  327.                     end
  328.                 else
  329.                     term.setCursorPos(1, y)
  330.                     term.clearLine()
  331.                 end
  332.             end
  333.  
  334.             lastPosition = position
  335.         end
  336.     end
  337. end
  338.  
  339. local function colourPicker(oldCol)
  340.     term.setTextColour(colours.purple)
  341.     term.setBackgroundColour(colours.cyan)
  342.    
  343.     term.setCursorPos(16, 3)
  344.     term.write("\159" .. string.rep("\143", 16))
  345.     term.blit("\144", "9", "a")
  346.    
  347.     for y = 4, 15 do
  348.         term.setCursorPos(16, y)
  349.         term.blit("\149", "a", "9")
  350.        
  351.         term.setCursorPos(33, y)
  352.         term.blit("\149", "9", "a")
  353.     end
  354.  
  355.     term.setTextColour(colours.cyan)
  356.     term.setBackgroundColour(colours.purple)
  357.  
  358.     term.setCursorPos(16, 16)
  359.     term.write("\130" .. string.rep("\131", 16))
  360.     term.blit("\129", "9", "a")
  361.    
  362.     local thisCol = 1
  363.     for y = 0, 3 do for x = 0, 3 do
  364.         paintutils.drawFilledBox(17 + x * 4, 4 + y * 3, 20 + x * 4, 6 + y * 3, thisCol)
  365.         thisCol = thisCol * 2
  366.     end end
  367.    
  368.     while true do
  369.         myEvent = {os.pullEvent("mouse_click")}
  370.         if myEvent[2] == 2 then return oldCol end
  371.         if clickedAt(16, 33, 3, 16) then return 2 ^ (math.floor((myEvent[4] - 4) / 3) * 4 + math.floor((myEvent[3] - 17) / 4)) end
  372.     end
  373. end
  374.  
  375. local function labelLayer(layer)
  376.     local thisLayer = layers[layer]
  377.  
  378.     term.setCursorPos(3, 2 + layer)
  379.     term.setTextColour(thisLayer[2])
  380.     term.setBackgroundColour(layer % 2 == 1 and colours.lightGrey or colours.grey)
  381.     term.write("\136\140\140\132")
  382.    
  383.     term.setTextColour(colours.black)
  384.     term.write("                           " .. (layer > 1 and "\30" or " ") .. (layer < 16 and "\31" or " ") .. " ")
  385.  
  386.     thisLayer = patterns[thisLayer[1]][1]
  387.     term.setCursorPos(math.floor((26 - #thisLayer) / 2) + 7, 2 + layer)
  388.     term.write(thisLayer)
  389. end
  390.  
  391. local function drawScreen()
  392.     local line = string.rep("\127", xSize)
  393.     term.setTextColour(colours.lightBlue)
  394.     term.setBackgroundColour(colours.blue)
  395.  
  396.     for y = 1, ySize do
  397.         term.setCursorPos(1, y)
  398.         term.write(line)
  399.     end
  400.    
  401.     term.setTextColour(colours.cyan)
  402.     term.setBackgroundColour(colours.brown)
  403.    
  404.     term.setCursorPos(2, 2)
  405.     term.write("\159" .. string.rep("\143", 34))
  406.     term.blit("\144", "c", "9")
  407.    
  408.     term.setCursorPos(39, 2)
  409.     term.write("\159" .. string.rep("\143", 10))
  410.     term.blit("\144", "c", "9")
  411.    
  412.     term.setTextColour(colours.brown)
  413.     term.setBackgroundColour(colours.cyan)
  414.    
  415.     term.setCursorPos(2, 19)
  416.     term.write("\130" .. string.rep("\131", 34))
  417.     term.blit("\129", "c", "9")
  418.    
  419.     term.setCursorPos(39, 16)
  420.     term.write("\130" .. string.rep("\131", 10))
  421.     term.blit("\129", "c", "9")
  422.    
  423.     for y = 3, 18 do
  424.         term.setCursorPos(2, y)
  425.         term.blit("\149", "9", "c")
  426.        
  427.         term.setCursorPos(37, y)
  428.         term.blit("\149", "c", "9")
  429.     end
  430.    
  431.     for y = 3, 15 do
  432.         term.setCursorPos(39, y)
  433.         term.blit("\149", "9", "c")
  434.        
  435.         term.setCursorPos(50, y)
  436.         term.blit("\149", "c", "9")
  437.     end
  438.    
  439.     for y = 1, 16 do labelLayer(y) end
  440.    
  441.     term.setBackgroundColour(colours.lightGrey)
  442.     term.setCursorPos(12, 1)
  443.     term.write(" Banner Editor ")
  444.    
  445.     term.setCursorPos(43, 1)
  446.     term.setTextColour(bg)
  447.     term.setBackgroundColour(colours.grey)
  448.     term.write("\136\140\140\132")
  449.    
  450.     term.setTextColour(colours.white)
  451.     term.setCursorPos(41, 17)
  452.     term.write(" Reset  ")
  453.     term.setCursorPos(41, 18)
  454.     term.write(" Import ")
  455.     term.setCursorPos(41, 19)
  456.     term.write(" Export ")
  457.  
  458.     drawBanner(1)
  459. end
  460.  
  461. ---------------------------------------------
  462. ------------         Init        ------------
  463. ---------------------------------------------
  464.  
  465. if not commands then error("A Command Computer is required.") end
  466.  
  467. if not _HOST then error("Minecraft 1.8 or later is required.") end
  468.  
  469. if term.current().setTextScale then
  470.     local curScale = 5
  471.    
  472.     repeat
  473.         term.current().setTextScale(curScale)
  474.         xSize, ySize = term.getSize()
  475.         if xSize > 49 and ySize > 18 then break end
  476.         curScale = curScale - 0.5
  477.     until curScale == 0
  478.    
  479.     if curScale == 0 then error("A larger display is required.") end
  480. else xSize, ySize = term.getSize() end
  481.  
  482. do
  483.     local hex = "0123456789abcdef"
  484.  
  485.     for i = 1, 16 do
  486.         layers[i] = {1, colours.black}
  487.         colourChar[2^(i-1)] = hex:sub(i, i)
  488.         colourNum[2^(i-1)] = 16 - i
  489.     end
  490. end
  491.  
  492. for i = 2, 39 do patternLookup[patterns[i][2]] = i end
  493.  
  494. for i = 1, #patterns do
  495.     local thisPattern = patterns[i][3]
  496.    
  497.     for y = 1, 39 do
  498.         local temp, exp, row, chars, skip = {}, 0, thisPattern[y], 0, 0
  499.        
  500.         for x = 1, 20 do
  501.             local pow = 2 ^ exp
  502.             exp = exp + 1
  503.            
  504.             if bit.band(pow, row) == pow then
  505.                 chars = chars + 1
  506.                 if skip > 0 then
  507.                     temp[#temp + 1] = skip
  508.                     skip = 0
  509.                 end
  510.             else
  511.                 skip = skip + 1
  512.                 if chars > 0 then
  513.                     temp[#temp + 1] = string.rep(" ", chars)
  514.                     chars = 0
  515.                 end
  516.             end
  517.         end
  518.        
  519.         if chars > 0 then temp[#temp + 1] = string.rep(" ", chars) end
  520.        
  521.         thisPattern[y] = temp
  522.     end
  523. end
  524.  
  525. drawScreen()
  526.  
  527. ---------------------------------------------
  528. ------------  Main Program Loop  ------------
  529. ---------------------------------------------
  530.  
  531. while true do
  532.     myEvent = {os.pullEvent()}
  533.    
  534.     if clickedAt(33 , 36, 2, 19) then
  535.         -- Reorder layers.
  536.        
  537.         local layerNum = myEvent[4] - 2
  538.        
  539.         if myEvent[3] == 34 and layerNum > 1 then
  540.             -- Move layer up.
  541.             local temp = layers[layerNum]
  542.             layers[layerNum] = layers[layerNum - 1]
  543.             layers[layerNum - 1] = temp
  544.  
  545.             labelLayer(layerNum - 1)
  546.             labelLayer(layerNum)
  547.             drawBanner(layerNum - 1)
  548.  
  549.         elseif myEvent[3] == 35 and layerNum < 16 then
  550.             -- Move layer down.
  551.             local temp = layers[layerNum]
  552.             layers[layerNum] = layers[layerNum + 1]
  553.             layers[layerNum + 1] = temp
  554.  
  555.             labelLayer(layerNum)
  556.             labelLayer(layerNum + 1)
  557.             drawBanner(layerNum)
  558.         end
  559.        
  560.     elseif clickedAt(42, 47, 1) then
  561.         -- Set background colour.
  562.        
  563.         bg = colourPicker(bg)
  564.         drawScreen()
  565.        
  566.     elseif clickedAt(2, 7, 2, 19) then
  567.         -- Set layer colour.
  568.        
  569.         local thisLayer = myEvent[4] - 2
  570.         layers[thisLayer][2] = colourPicker(layers[thisLayer][2])
  571.         drawScreen()
  572.        
  573.     elseif clickedAt(6, 33, 2, 19) then
  574.         -- Set layer pattern.
  575.        
  576.         local thisLayer = myEvent[4] - 2
  577.         layers[thisLayer][1] = patternPicker(layers[thisLayer][1])
  578.         drawScreen()
  579.        
  580.     elseif clickedAt(40, 49, 17) then
  581.         -- Reset.
  582.        
  583.         bg = colours.white
  584.         for i = 1, 16 do layers[i] = {1, colours.black} end
  585.         drawScreen()
  586.        
  587.     elseif clickedAt(40, 49, 18) then
  588.         -- Import.
  589.        
  590.         if commands.getBlockInfo(x, y + 1, z).name == "minecraft:chest" then
  591.             local nbt = getNBT("~ ~1 ~").Items
  592.  
  593.             for i = 1, #nbt do
  594.                 local thisItem = nbt[i]
  595.  
  596.                 if thisItem.id == "minecraft:banner" then
  597.                     bg = 2 ^ (15 - thisItem.Damage)
  598.                    
  599.                     thisItem = thisItem.tag
  600.                    
  601.                     if thisItem then
  602.                         thisItem = thisItem.BlockEntityTag.Patterns
  603.                        
  604.                         for i = 1, #thisItem do
  605.                             local thisLayer = thisItem[i]
  606.                             layers[i] = {patternLookup[thisLayer.Pattern], 2 ^ (15 - thisLayer.Color)}
  607.                         end
  608.                        
  609.                         for i = #thisItem + 1, 16 do layers[i] = {1, colours.black} end
  610.                     end
  611.                    
  612.                     drawScreen()
  613.                     break
  614.                 end
  615.             end
  616.         end
  617.        
  618.     elseif clickedAt(40, 49, 19) then
  619.         -- Export.
  620.        
  621.         if commands.getBlockInfo(x, y + 1, z).name == "minecraft:chest" then
  622.             local tag = {}
  623.            
  624.             for i = 1, 16 do
  625.                 local thisLayer = layers[i]
  626.                 local thisPattern = thisLayer[1]
  627.                 if thisPattern ~= 1 then tag[#tag + 1] = {["Pattern"] = patterns[thisPattern][2], ["Color"] = colourNum[thisLayer[2]]} end
  628.             end
  629.            
  630.             local nbt = getNBT("~ ~1 ~")
  631.            
  632.             local items = nbt.Items
  633.            
  634.             for i = 1, #items do
  635.                 local thisItem = items[i]
  636.                 if type(thisItem) == "table" and thisItem.id == "minecraft:banner" then thisItem.Damage, thisItem.tag = colourNum[bg], "BBUniqueString" end
  637.             end
  638.            
  639.             -- Workaround for CC's false recursion complaints:
  640.             commands.blockdata("~ ~1 ~ " .. textutils.serialiseJSON(nbt, true):gsub("\"BBUniqueString\"", textutils.serialiseJSON({["BlockEntityTag"] = {["Patterns"] = tag, ["Base"] = colourNum[bg]}}, true)))
  641.         end
  642.        
  643.     elseif pressedKey(keys.q, keys.x) then
  644.         -- Quit.
  645.        
  646.         os.pullEvent("char")
  647.         break
  648.     end
  649. end
  650.  
  651. term.setTextColour(colours.white)
  652. term.setBackgroundColour(colours.black)
  653. term.clear()
  654. term.setCursorPos(1, 1)
  655. print("Thanks for using Banner Editor!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement