Cromon

Untitled

Aug 27th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var testControl = function () {
  2.     UI.makeControl(this);
  3. }
  4.  
  5. testControl.prototype.onDraw = function () {
  6.     Debug.print("__onDraw");
  7. }
  8.  
  9. var textureChangeDialog = {
  10.     xmlTemplate: "Terrain/TextureDialog.xml",
  11.     pages: [],
  12.     curPage: 0,
  13.     activeBlocks: [],
  14.     nextButton: null,
  15.     prevButton: null,
  16.     test: null,
  17.  
  18.     onTemplateLoaded: function () {
  19.         var me = this;
  20.  
  21.         VFileSystem.OnLoaded(function () {
  22.             me.onFileSystemReady();
  23.         });
  24.  
  25.         this.textureDialog.tileBox.SelectionChanged.Add(function (index, value) {
  26.             me.selectionChanged(index, value);
  27.         });
  28.        
  29.         this.test = new testControl();
  30.         this.textureDiaog.addElement(this.test);
  31.     },
  32.  
  33.     onFileSystemReady: function () {
  34.         for(var i in VFileSystem.Root.Tileset.Directories) {
  35.             var dir = VFileSystem.Root.Tileset.Directories[i];
  36.  
  37.             this.textureDialog.tileBox.addItem(dir.Name);
  38.         }
  39.     },
  40.  
  41.     selectionChanged: function (index, value) {
  42.         var dir = VFileSystem.Root.Tileset[value];
  43.  
  44.         this.pages = [];
  45.         var curPage = [];
  46.         this.curPage = 0;
  47.  
  48.         for (var i in dir.Files) {
  49.             var file = dir.Files[i].FullName;
  50.  
  51.             if (file.toLowerCase().endsWith("_s.blp")) {
  52.                 continue;
  53.             }
  54.  
  55.             curPage.push(dir.Files[i].FullName);
  56.             if (curPage.length == 12) {
  57.                 this.pages.push(curPage);
  58.                 curPage = [];
  59.             }
  60.         }
  61.  
  62.         if (curPage.length > 0) {
  63.             this.pages.push(curPage);
  64.         }
  65.  
  66.         if (this.nextButton === null || this.prevButton === null) {
  67.             this.prevButton = new Button(
  68.                 {
  69.                     position: { x: 160, y: 205 },
  70.                     large: false,
  71.                     caption: "prev",
  72.                     name: "prevButton",
  73.                     onClick: (function (_me) { return function (button) { _me.prevClick(button); }; })(this),
  74.                 }
  75.             );
  76.  
  77.             this.nextButton = new Button(
  78.                 {
  79.                     position: { x: 364, y: 205 },
  80.                     large: false,
  81.                     caption: "next",
  82.                     name: "nextButton",
  83.                     onClick: (function (_me) { return function (button) { _me.nextClick(button); }; })(this),
  84.                 }
  85.             );
  86.  
  87.             this.textureDialog.addElement(this.prevButton);
  88.             this.textureDialog.addElement(this.nextButton);
  89.         }
  90.  
  91.         this.prevButton.disabled = true;
  92.         this.nextButton.disabled = true;
  93.  
  94.         if (this.pages.length > 1) {
  95.             this.nextButton.disabled = false;
  96.         }
  97.  
  98.         if (this.pages.length > 0) {
  99.             this.loadPage(0);
  100.         }
  101.     },
  102.  
  103.     loadPage: function (pageNum) {
  104.         if (pageNum >= this.pages.length || pageNum < 0) {
  105.             throw "Invalid page index (must be 0 <= pageNum < this.pages.length)";
  106.         }
  107.  
  108.         for (var i in this.activeBlocks) {
  109.             var curBlock = this.activeBlocks[i];
  110.             curBlock.CheckChanged.Clear();
  111.  
  112.             this.textureDialog.removeElement(curBlock);
  113.         }
  114.  
  115.         this.activeBlocks = [];
  116.  
  117.         var page = this.pages[pageNum];
  118.         for (var i in page) {
  119.             var file = page[i];
  120.  
  121.             var curBlock = new TextureBlock(
  122.                 {
  123.                     name: "textureBlock_" + i,
  124.                     texture: file
  125.                 }
  126.             );
  127.  
  128.             var me = this;
  129.  
  130.             (function (block) {
  131.                 block.CheckChanged.Add(function (check) { me.updateCheck(block, check); });
  132.             })(curBlock);
  133.  
  134.             curBlock.clickable = true;
  135.             curBlock.blockPosition = { x: 160 + (i % 4) * 68, y: Math.floor((i / 4)) * 68 };
  136.             curBlock.blockSize = { w: 64, h: 64 };
  137.  
  138.             this.activeBlocks.push(curBlock);
  139.  
  140.             this.textureDialog.addElement(curBlock);
  141.         }
  142.     },
  143.  
  144.     prevClick: function(button) {
  145.         if (this.curPage === 0) {
  146.             return;
  147.         }
  148.  
  149.         this.nextButton.disabled = false;
  150.  
  151.         --this.curPage;
  152.         if (this.curPage === 0) {
  153.             this.prevButton.disabled = true;
  154.         }
  155.  
  156.         this.loadPage(this.curPage);
  157.     },
  158.  
  159.     nextClick: function (button) {
  160.         this.prevButton.disabled = false;
  161.         ++this.curPage;
  162.         this.curPage = Math.min(this.curPage, this.pages.length - 1);
  163.  
  164.         if (this.curPage + 1 >= this.pages.length) {
  165.             this.nextButton.disabled = true;
  166.         }
  167.  
  168.         this.loadPage(this.curPage);
  169.     },
  170.  
  171.     updateCheck: function (block, checked) {
  172.         if (checked === false) {
  173.             return;
  174.         }
  175.  
  176.         PropertyStore.setOrCreate("Terrain.Texture", block.texture);
  177.  
  178.         for (var i in this.activeBlocks) {
  179.             var cur = this.activeBlocks[i];
  180.             if (cur === block) {
  181.                 continue;
  182.             }
  183.  
  184.             cur.clicked = false;
  185.         }
  186.     }
  187. };
  188.  
  189. UI.load(textureChangeDialog);
Advertisement
Add Comment
Please, Sign In to add comment