2
0
Эх сурвалжийг харах

Updated load_texture example

Fixes #12
Björn Ritzl 5 жил өмнө
parent
commit
ac048ccc9a

+ 17 - 0
assets/text12.font

@@ -0,0 +1,17 @@
+font: "/assets/SourceSansPro.ttf"
+material: "/builtins/fonts/font.material"
+size: 12
+antialias: 1
+alpha: 1.0
+outline_alpha: 0.0
+outline_width: 0.0
+shadow_alpha: 0.0
+shadow_blur: 0
+shadow_x: 0.0
+shadow_y: 0.0
+extra_characters: ""
+output_format: TYPE_BITMAP
+all_chars: false
+cache_width: 0
+cache_height: 0
+render_mode: MODE_SINGLE_LAYER

+ 66 - 0
examples/gui/load_texture/load_texture.gui

@@ -3,6 +3,10 @@ fonts {
   name: "text48"
   font: "/assets/text48.font"
 }
+fonts {
+  name: "text12"
+  font: "/assets/text12.font"
+}
 textures {
   name: "sprites"
   texture: "/assets/sprites.atlas"
@@ -184,6 +188,68 @@ nodes {
   text_leading: 1.0
   text_tracking: 0.0
 }
+nodes {
+  position {
+    x: 360.0
+    y: 107.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: 50.0
+    z: 0.0
+    w: 1.0
+  }
+  color {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+  type: TYPE_TEXT
+  blend_mode: BLEND_MODE_ALPHA
+  text: ""
+  font: "text12"
+  id: "message"
+  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
+  layer: ""
+  inherit_alpha: true
+  alpha: 1.0
+  outline_alpha: 0.0
+  shadow_alpha: 1.0
+  template_node_child: false
+  text_leading: 1.0
+  text_tracking: 0.0
+}
 material: "/builtins/materials/gui.material"
 adjust_reference: ADJUST_REFERENCE_LEGACY
 max_nodes: 512

+ 51 - 38
examples/gui/load_texture/load_texture.gui_script

@@ -1,65 +1,78 @@
-local function on_finish_loading(self, msg)
-	print(msg)
-	gui.set_text(self.text, "LOAD RANDOM") -- <11>
+local function set_message(text)
+	gui.set_text(gui.get_node("message"), text) -- <11>
 end
 
-local function on_load_finish(self, id, res)
-	if res.status ~= 200 and res.status ~= 304 then -- <7>
-		on_finish_loading(self, "Unable to get image\n".. res.response)
-		return
+local function set_image(self, texture_id, image_data)
+	if self.texture_id then -- <8>
+		gui.delete_texture(self.texture_id) 
+		self.texture_id = nil
 	end
-		
-	local img = image.load(res.response) -- <8>
+
+	local img = image.load(image_data) -- <9>
 	if not img then 
-		on_finish_loading(self, "Unable to load image")
+		set_message("Unable to load image")
 		return
 	end
 	
-	if gui.new_texture(self.texture_url, img.width, img.height, img.type, img.buffer) then -- <9>
-		gui.set_texture(self.img, self.texture_url) -- <10>
-		on_finish_loading(self, "LOADED: "..self.texture_url)
+	if gui.new_texture(texture_id, img.width, img.height, img.type, img.buffer) then -- <10>
+		self.texture_id = texture_id -- <11>
+		gui.set_texture(gui.get_node("img"), texture_id) -- <12>
+		set_message("Set new texture")
 	else
-		on_finish_loading(self, "Unable to create texture")
+		set_message("Unable to create texture")
 	end
 end
 
-local function start_load_random(self)
-	if self.texture_url then -- <4>
-		gui.delete_texture(self.texture_url) 
-	end
-	self.texture_url = "https://i.picsum.photos/id/"..math.random(1, 10).."/200/300.jpg"
-	http.request(self.texture_url, "GET", on_load_finish) -- <5>
-	gui.set_text(self.text, "Loading...") -- <6>
+
+local load_image
+load_image = function(url)
+	http.request(url, "GET", function(self, id, res)  -- <6>
+		-- redirect?
+		if res.status == 302 or res.status == 301 then -- <7>
+			set_message("Redirect: " .. res.headers.location)
+			load_image(res.headers.location)
+		-- ok or cached?
+		elseif res.status == 200 or res.status == 304 then -- <7>
+			set_image(self, url, res.response)
+		-- error
+		else
+			set_message("Unable to get image: " .. res.response)
+		end
+	end)
+end
+
+local function load_random(self)
+	local url = "https://picsum.photos/id/"..math.random(1, 10).."/200/300.jpg" -- <3>
+	set_message("Loading...") -- <4>
+	load_image(url) -- <5>
 end
 
 function init(self)
 	msg.post(".", "acquire_input_focus") -- <1>
-	self.button = gui.get_node("button") -- <2>
-	self.img = gui.get_node("img")	
-	self.text = gui.get_node("text")
-	start_load_random(self)	-- <3>
+	load_random(self)	-- <2>
 end
 
 function on_input(self, action_id, action)
 	if action_id == hash("touch") and action.pressed then
-		if gui.pick_node(self.button, action.x, action.y) then -- <12>
-			start_load_random(self) -- <3>
+		if gui.pick_node(gui.get_node("button"), action.x, action.y) then -- <13>
+			load_random(self) -- <2>
 		end
 	end
 end
 
 --[[
 1. Tell the engine that this game object wants to receive input.
-2. Get nodes and save them in self table
-3. Start loading random image
-4. Remove prev. created texture
-5. Make an HTTP request, and set on_load_finish method for request
-6. Change the label text.
-7. Check server responce
-8. Check is request an image, then create an image resource
-9. Create a new texture using img resource
-10. Set new texture as node texture
-11. Change the label text.
-12. Check if the click position (`action.x` and `action.y`) is within the boundaries of 
+2. Start loading random image
+3. Generate a URL to a random image
+4. Change the label text.
+5. Call function load image from URL
+6. Make an HTTP request and handle redirects
+7. Check server response
+8. Remove previous texture if any
+9. Create an image resource from loaded image data
+10. Create a new texture using image resource
+11. Save the texture id 
+12. Set new texture as node texture
+13. Check if the click position (`action.x` and `action.y`) is within the boundaries of 
    the button node.
 --]]

BIN
examples/gui/load_texture/load_texture.png


BIN
examples/gui/load_texture/load_texture_gui.png