From 13a4f93c1a149bf0439c1a227808732ea82f3737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Thu, 27 Jul 2017 10:05:31 +0200 Subject: [PATCH] Added TCP client example --- examples/tcp_client/README.md | 2 + examples/tcp_client/tcp_client.collection | 37 ++ examples/tcp_client/tcp_client.gui | 459 ++++++++++++++++++++++ examples/tcp_client/tcp_client.gui_script | 48 +++ 4 files changed, 546 insertions(+) create mode 100644 examples/tcp_client/README.md create mode 100644 examples/tcp_client/tcp_client.collection create mode 100644 examples/tcp_client/tcp_client.gui create mode 100644 examples/tcp_client/tcp_client.gui_script diff --git a/examples/tcp_client/README.md b/examples/tcp_client/README.md new file mode 100644 index 0000000..096e0e4 --- /dev/null +++ b/examples/tcp_client/README.md @@ -0,0 +1,2 @@ +# TCP client example +This example shows how to create a TCP client and send and receive TCP data. diff --git a/examples/tcp_client/tcp_client.collection b/examples/tcp_client/tcp_client.collection new file mode 100644 index 0000000..c181f9c --- /dev/null +++ b/examples/tcp_client/tcp_client.collection @@ -0,0 +1,37 @@ +name: "tcp_client" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"gui\"\n" + " component: \"/examples/tcp_client/tcp_client.gui\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + position { + x: 640.0 + y: 360.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale3 { + x: 1.0 + y: 1.0 + z: 1.0 + } +} diff --git a/examples/tcp_client/tcp_client.gui b/examples/tcp_client/tcp_client.gui new file mode 100644 index 0000000..c955a81 --- /dev/null +++ b/examples/tcp_client/tcp_client.gui @@ -0,0 +1,459 @@ +script: "/examples/tcp_client/tcp_client.gui_script" +fonts { + name: "kenvector_15" + font: "/examples/assets/fonts/kenvector_15.font" +} +textures { + name: "ui" + texture: "/examples/assets/ui.atlas" +} +background_color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 +} +nodes { + position { + x: 320.0 + y: 591.434 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 128.0 + y: 128.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "ui/logo" + id: "logo" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_AUTO +} +nodes { + position { + x: 320.0 + y: 360.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "send" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/examples/assets/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 45.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "ui/green_button08" + id: "send/button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "send" + layer: "below" + inherit_alpha: true + slice9 { + x: 8.0 + y: 8.0 + z: 8.0 + w: 8.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 40.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "SEND" + font: "kenvector_15" + id: "send/label" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "send/button" + layer: "text" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: 320.0 + y: 425.688 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "connect" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/examples/assets/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 45.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "ui/green_button08" + id: "connect/button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "connect" + layer: "below" + inherit_alpha: true + slice9 { + x: 8.0 + y: 8.0 + z: 8.0 + w: 8.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 40.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "CONNECT" + font: "kenvector_15" + id: "connect/label" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "connect/button" + layer: "text" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: 320.0 + y: 487.313 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 600.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "" + font: "kenvector_15" + id: "data" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} +layers { + name: "below" +} +layers { + name: "text" +} +layers { + name: "above" +} +material: "/builtins/materials/gui.material" +adjust_reference: ADJUST_REFERENCE_PARENT +max_nodes: 512 diff --git a/examples/tcp_client/tcp_client.gui_script b/examples/tcp_client/tcp_client.gui_script new file mode 100644 index 0000000..8146901 --- /dev/null +++ b/examples/tcp_client/tcp_client.gui_script @@ -0,0 +1,48 @@ +local tcp_client = require "defnet.tcp_client" + +local SERVER_PORT = 5000 + +function init(self) + msg.post(".", "acquire_input_focus") + msg.post("@render:", "clear_color", { color = vmath.vector4(0.4, 0.5, 0.8, 1.0) }) + + self.connect = gui.get_node("connect/button") + self.send = gui.get_node("send/button") + gui.set_enabled(self.send, false) +end + +function final(self) + if self.client then + self.client.destroy() + end +end + +function update(self, dt) + if self.client then + self.client.update() + end +end + +function on_input(self, action_id, action) + if not action.released then + return + end + + if gui.pick_node(self.connect, action.x, action.y) and gui.is_enabled(self.connect) then + gui.set_enabled(self.connect, false) + gui.set_enabled(self.send, true) + + self.client = tcp_client.create("127.0.0.1", SERVER_PORT, + function(data) + print(data) + gui.set_text(gui.get_node("data"), tostring(data)) + end, + function() + self.client = nil + gui.set_enabled(self.connect, true) + gui.set_enabled(self.send, false) + end) + elseif gui.pick_node(self.send, action.x, action.y) and gui.is_enabled(self.send) then + self.client.send("foobar " .. tostring(os.time())) + end +end