Răsfoiți Sursa

Add better comments to repeating timer example.

Pawel Jarosz 2 ani în urmă
părinte
comite
259ab3031b

+ 1 - 1
examples/timer/repeating_timer/repeating_timer.collection

@@ -3,7 +3,7 @@ scale_along_z: 0
 embedded_instances {
   id: "go"
   data: "components {\n"
-  "  id: \"progress\"\n"
+  "  id: \"repeating_timer\"\n"
   "  component: \"/examples/timer/repeating_timer/repeating_timer.gui\"\n"
   "  position {\n"
   "    x: 0.0\n"

+ 22 - 16
examples/timer/repeating_timer/repeating_timer.gui_script

@@ -1,8 +1,7 @@
--- set value of numeric time indicator (in percent from 0 to 60s)
+-- set value of numeric time indicator (from 0 to 60s)
 local function update_numeric(p)
 	local node = gui.get_node("numeric")
-	local percent = math.floor(p)
-	gui.set_text(node, tostring(percent) .. "s")
+	gui.set_text(node, tostring(p) .. "s")
 end
 
 -- update radial/circle time indicator by changing the fill angle
@@ -13,18 +12,25 @@ local function update_radial(p)
 end
 
 function init(self)
-	self.time = 0
+	self.count = 0			-- <1>
+	local interval = 1		-- <2>
+	local repeating = true	-- <3>
 
-	-- create a timer delay with 1s interval, repeating
-	local interval = 1
-	local repeating = true
-	timer.delay(interval, repeating, function()
-		-- each second this function will be called
-		self.time = self.time + 1
-		-- take modulo out of time elapsed from the beginning for 60s periods:
-		local p = self.time % 60
-
-		update_numeric(p)
-		update_radial(p)
+	timer.delay(interval, repeating, function()		-- <4>
+		self.count = self.count + 1					-- <5>
+		local p = self.time % 60					-- <6>
+		update_numeric(p)							-- <7>
+		update_radial(p)							-- <8>
 	end)
-end
+end
+
+--[[
+1. Start the count with value 0.
+2. We will use interval of 1 [s].
+3. We will be repeating the timer endlessly.
+4. Start the timer with interval (1s) and repeating (true) and pass a callback function.
+5. The function will be called every 1s, so increase the count by 1 each time.
+6. Get the modulo of 60, because the timer will be reset every 60s.
+7. Update the numeric display of seconds passed.
+8. Update the radial indicator of seconds passed.
+--]]