From 0b6ac62b0cc86d0e7ecb762c92bbbb2245e36da2 Mon Sep 17 00:00:00 2001 From: Maki Date: Thu, 20 Dec 2018 21:38:58 -0800 Subject: [PATCH] Ts things --- .gitmodules | 6 +- _snail | 1 + _typings | 1 - ts-client-scripts/_minecraft/_inventory.js | 66 +++++++++-------- .../_minecraft/src/ConnectionHandler.ts | 19 ----- .../_minecraft/src/EventHandler.ts | 24 +++++++ ts-client-scripts/_minecraft/src/Inventory.ts | 71 +++++++++++-------- ts-client-scripts/_minecraft/tsconfig.json | 2 +- 8 files changed, 107 insertions(+), 83 deletions(-) create mode 160000 _snail delete mode 160000 _typings delete mode 100644 ts-client-scripts/_minecraft/src/ConnectionHandler.ts create mode 100644 ts-client-scripts/_minecraft/src/EventHandler.ts diff --git a/.gitmodules b/.gitmodules index 5f89901..8a1c265 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "_typings"] - path = _typings - url = https://github.com/MarcusLlewellyn/hifi-typescript +[submodule "_snail"] + path = _snail + url = https://github.com/theepicsnail/hifi diff --git a/_snail b/_snail new file mode 160000 index 0000000..ac6267a --- /dev/null +++ b/_snail @@ -0,0 +1 @@ +Subproject commit ac6267a0f57bcba599a84fe36b751b43a44a4605 diff --git a/_typings b/_typings deleted file mode 160000 index bc54368..0000000 --- a/_typings +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bc5436898084a55523dc2312b79c0bde7620cb1a diff --git a/ts-client-scripts/_minecraft/_inventory.js b/ts-client-scripts/_minecraft/_inventory.js index 0407d44..17205d0 100644 --- a/ts-client-scripts/_minecraft/_inventory.js +++ b/ts-client-scripts/_minecraft/_inventory.js @@ -1,72 +1,80 @@ -var ConnectionHandler = (function () { - function ConnectionHandler() { +var EventHandler = (function () { + function EventHandler() { + this.connections = []; } - ConnectionHandler.prototype.connect = function (con, fun) { - con.connect(fun); + EventHandler.prototype.connect = function (event, callback) { + event.connect(callback); this.connections.push({ - con: con, fun: fun + event: event, + callback: callback }); }; - ConnectionHandler.prototype.disconnectAll = function () { - this.connections.forEach(function (connection) { - connection.con.disconnect(connection.fun); + EventHandler.prototype.disconnectAll = function () { + this.connections.forEach(function (con) { + con.event.disconnect(con.callback); }); }; - return ConnectionHandler; + return EventHandler; }()); var Inventory = (function () { function Inventory() { + this.selectedIndex = 0; this.imageSizes = { inventory: { width: 546, height: 66 }, selector: { width: 72, height: 72 } }; - this.selectedIndex = 0; - this.overlays = { + this.overlayIDs = { inventory: Overlays.addOverlay("image", { x: Window.innerWidth / 2 - this.imageSizes.inventory.width / 2, y: this.imageSizes.inventory.height / 2, width: this.imageSizes.inventory.width, height: this.imageSizes.inventory.height, - imageURL: "file:///D:/Git/hifi-stuff/client-scripts/minecraftHifi/assets/inventory.png" + imageURL: Script.resolvePath("assets/inventory.png") }), selector: Overlays.addOverlay("image", { x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60, y: this.imageSizes.selector.height / 2 - 6, width: this.imageSizes.selector.width, height: this.imageSizes.selector.height, - imageURL: "file:///D:/Git/hifi-stuff/client-scripts/minecraftHifi/assets/selector.png" + imageURL: Script.resolvePath("assets/selector.png") }) }; + this.events = new EventHandler(); + this.events.connect(Window.geometryChanged, this.geometryChanged.bind(this)); + this.events.connect(Overlays.mousePressOnOverlay, this.mousePressOnOverlay.bind(this)); } - Inventory.prototype.changeSelectedIndex = function (index) { - this.selectedIndex = index; - Overlays.editOverlay(this.overlays.selector, { - x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60 - }); - }; Inventory.prototype.geometryChanged = function () { - Overlays.editOverlay(this.overlays.inventory, { + console.log(JSON.stringify(this)); + Overlays.editOverlay(this.overlayIDs.inventory, { x: Window.innerWidth / 2 - this.imageSizes.inventory.width / 2 }); - Overlays.editOverlay(this.overlays.selector, { + Overlays.editOverlay(this.overlayIDs.selector, { + x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60 + }); + }; + Inventory.prototype.mousePressOnOverlay = function (overlayID, event) { + switch (overlayID) { + case this.overlayIDs.inventory: + console.log(JSON.stringify(event.pos2D)); + break; + } + }; + Inventory.prototype.changeSelectedIndex = function (index) { + this.selectedIndex = index; + Overlays.editOverlay(this.overlayIDs.selector, { x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60 }); }; Inventory.prototype.unload = function () { var _this = this; - Object.keys(this.overlays).forEach(function (key) { - Overlays.deleteOverlay(_this.overlays[key]); + this.events.disconnectAll(); + Object.keys(this.overlayIDs).forEach(function (key) { + Overlays.deleteOverlay(_this.overlayIDs[key]); }); }; return Inventory; }()); var inventory = new Inventory(); -var cons = new ConnectionHandler(); -cons.connect(Window.geometryChanged, function () { - inventory.geometryChanged(); -}); Script.scriptEnding.connect(function () { - cons.disconnectAll(); inventory.unload(); }); -console.log("test"); diff --git a/ts-client-scripts/_minecraft/src/ConnectionHandler.ts b/ts-client-scripts/_minecraft/src/ConnectionHandler.ts deleted file mode 100644 index fa66d9b..0000000 --- a/ts-client-scripts/_minecraft/src/ConnectionHandler.ts +++ /dev/null @@ -1,19 +0,0 @@ -class ConnectionHandler { - private connections: [{ - con: any, - fun: Function, - }]; - - connect(con: any, fun: Function) { - con.connect(fun); - this.connections.push({ - con: con, fun: fun - }); - } - - disconnectAll() { - this.connections.forEach(connection=>{ - connection.con.disconnect(connection.fun); - }); - } -} \ No newline at end of file diff --git a/ts-client-scripts/_minecraft/src/EventHandler.ts b/ts-client-scripts/_minecraft/src/EventHandler.ts new file mode 100644 index 0000000..31f0a67 --- /dev/null +++ b/ts-client-scripts/_minecraft/src/EventHandler.ts @@ -0,0 +1,24 @@ +class EventHandler { + private connections: { + event: any, + callback: Function, + }[]; + + constructor() { + this.connections = []; + } + + connect(event: any, callback: Function) { + event.connect(callback); + this.connections.push({ + event: event, + callback: callback + }); + } + + disconnectAll() { + this.connections.forEach(con=>{ + con.event.disconnect(con.callback); + }); + } +} \ No newline at end of file diff --git a/ts-client-scripts/_minecraft/src/Inventory.ts b/ts-client-scripts/_minecraft/src/Inventory.ts index 40abbcc..eabec17 100644 --- a/ts-client-scripts/_minecraft/src/Inventory.ts +++ b/ts-client-scripts/_minecraft/src/Inventory.ts @@ -1,66 +1,77 @@ -/// +/// class Inventory { - private overlays: {[key: string]: Uuid}; - private imageSizes: {[key: string]: {width: number, height: number}} = { - inventory: { width: 546, height: 66}, - selector: { width: 72, height: 72}, - }; + private events: EventHandler; + private overlayIDs: {[key: string]: Uuid}; + private imageSizes: {[key: string]: {width: number, height: number}}; selectedIndex: number = 0; constructor() { - this.overlays = { + this.imageSizes = { + inventory: { width: 546, height: 66}, + selector: { width: 72, height: 72}, + }; + + this.overlayIDs = { inventory: Overlays.addOverlay("image", { x: Window.innerWidth/2 - this.imageSizes.inventory.width/2, y: this.imageSizes.inventory.height/2, width: this.imageSizes.inventory.width, height: this.imageSizes.inventory.height, - imageURL: "file:///D:/Git/hifi-stuff/client-scripts/minecraftHifi/assets/inventory.png", + imageURL: Script.resolvePath("assets/inventory.png"), }), selector: Overlays.addOverlay("image", { x: Window.innerWidth/2 - this.imageSizes.selector.width/2 + (this.selectedIndex-4)*60, y: this.imageSizes.selector.height/2 - 6, width: this.imageSizes.selector.width, height: this.imageSizes.selector.height, - imageURL: "file:///D:/Git/hifi-stuff/client-scripts/minecraftHifi/assets/selector.png", + imageURL: Script.resolvePath("assets/selector.png"), }) - } + }; + + this.events = new EventHandler(); + this.events.connect(Window.geometryChanged, this.geometryChanged.bind(this)); + this.events.connect(Overlays.mousePressOnOverlay, this.mousePressOnOverlay.bind(this)); } - changeSelectedIndex(index: number) { - this.selectedIndex = index; + private geometryChanged() { + console.log(JSON.stringify(this)); - Overlays.editOverlay(this.overlays.selector, { + Overlays.editOverlay(this.overlayIDs.inventory, { + x: Window.innerWidth/2 - this.imageSizes.inventory.width/2, + }); + Overlays.editOverlay(this.overlayIDs.selector, { x: Window.innerWidth/2 - this.imageSizes.selector.width/2 + (this.selectedIndex-4)*60, }); } - geometryChanged() { - Overlays.editOverlay(this.overlays.inventory, { - x: Window.innerWidth/2 - this.imageSizes.inventory.width/2, - }); - Overlays.editOverlay(this.overlays.selector, { + private mousePressOnOverlay(overlayID, event) { + switch (overlayID) { + case this.overlayIDs.inventory: + console.log(JSON.stringify(event.pos2D)); + + break; + } + } + + changeSelectedIndex(index: number) { + this.selectedIndex = index; + + Overlays.editOverlay(this.overlayIDs.selector, { x: Window.innerWidth/2 - this.imageSizes.selector.width/2 + (this.selectedIndex-4)*60, }); } unload() { - Object.keys(this.overlays).forEach(key=>{ - Overlays.deleteOverlay(this.overlays[key]); + this.events.disconnectAll(); + + Object.keys(this.overlayIDs).forEach(key=>{ + Overlays.deleteOverlay(this.overlayIDs[key]); }); } } let inventory = new Inventory(); -let cons = new ConnectionHandler(); - -cons.connect(Window.geometryChanged, ()=>{ - inventory.geometryChanged(); -}) - Script.scriptEnding.connect(()=>{ - cons.disconnectAll(); inventory.unload(); -}); - -console.log("test") \ No newline at end of file +}); \ No newline at end of file diff --git a/ts-client-scripts/_minecraft/tsconfig.json b/ts-client-scripts/_minecraft/tsconfig.json index 4f8e801..7bde1e3 100644 --- a/ts-client-scripts/_minecraft/tsconfig.json +++ b/ts-client-scripts/_minecraft/tsconfig.json @@ -7,7 +7,7 @@ "removeComments": true }, "include": [ - "../../_typings", + "../../_snail/typings", "src" ], "exclude": [ "typings" ]