Slideshow Applet

From Pointui

This example applet allows the user to configure a folder to fetch images from and will show a slideshow that can be automatically or manually navigated through.

Code (Slideshow.cs)

  1. //Pointui C Script
  2. /*
  3. 	COPYRIGHT
  4.  
  5. 	Copyright (c) 2008-2009 Pointui Pty Ltd, All Rights Reserved.
  6. 	ABN: 80 129 073 678
  7.  
  8. 	This software is provided under license by Pointui Pty Ltd, and use thereof
  9. 	is subject to the licensing terms. Distribution of this software is on an 
  10. 	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND.
  11. 	This software is protected by Australian and international law. 
  12. 	This software program may not be reproduced, transmitted, or disclosed to
  13. 	third parties, in whole or in part, in any form or by any manner, electronic 
  14. 	or mechanical, without the express written consent of Pointui Pty Ltd, except
  15. 	to the extent provided for by applicable license
  16. */
  17.  
  18. #region Applet
  19.  
  20. class SlideshowApplet : Applet
  21. {
  22. 	//data table to store the list of files in the folder being displayed
  23. 	DataTable tblFiles;
  24.  
  25. 	//timer that is used to swap to the next image
  26. 	Timer ssUpdateTimer;
  27.  
  28. 	//images for the slideshow borders
  29. 	Image imgTop, imgLeft, imgRight, imgBottom, imgBottomFade;
  30.  
  31. 	//current image being displayed
  32. 	Image imgScreenshot;
  33.  
  34. 	//buttons for navigating
  35. 	Button btnPlayPause, btnSettings, btnNext, btnPrev;
  36.  
  37. 	int x, y, r, w, h;
  38. 	int displayX, displayY, displayHeight, displayWidth;
  39.  
  40. 	String PictureFolderPath;
  41.  
  42. 	int PictureCount;
  43. 	int CurrentPicture;
  44. 	int ssInterval;
  45.  
  46. 	bool ControlsAreBright;
  47.  
  48. 	void ShowNextPicture(int offsetFromCurrent)
  49. 	{
  50. 		//increment the picture index
  51. 		CurrentPicture += offsetFromCurrent;
  52.  
  53. 		//is this after the last one?
  54. 		if (CurrentPicture >= PictureCount)
  55. 		{
  56. 			//go back to start
  57. 			CurrentPicture = 0;
  58. 		}
  59. 		//is this before the first one
  60. 		if (CurrentPicture < 0)
  61. 		{
  62. 			//wrap around to end
  63. 			CurrentPicture = PictureCount - 1;
  64. 		}
  65.  
  66. 		//try move to next image in list of files
  67. 		bool anyMore;
  68. 		anyMore = tblFiles.MoveTo(CurrentPicture);
  69. 		if (anyMore)
  70. 		{
  71. 			//was another image, so get the filename
  72. 			String s;
  73. 			tblFiles.GetValue("Filename", s);
  74. 			//show this picture
  75. 			ShowPicture(s);
  76. 		} else {
  77. 			//no pictures in this folder, so show default image
  78. 			ShowPicture("Default.Image.jpg");
  79. 		}
  80. 	}
  81.  
  82. 	//event handler to swap to the next picture
  83. 	void ssUpdateTimer_OnTimer()
  84. 	{
  85. 		ShowNextPicture(1);
  86. 	}
  87.  
  88. 	//click handler for when picture being shown is clicked
  89. 	void imgScreenshot_OnClick()
  90. 	{
  91. 		//stop cycling through pictures
  92. 		ssUpdateTimer.Stop();
  93.  
  94. 		//fade back in the navigation controls
  95. 		ControlsFade(false);
  96. 	}
  97.  
  98. 	//click handler for the settings button
  99. 	void btnSettings_OnClick()
  100. 	{
  101. 		//show the screen to select a different folder
  102. 		ScreenSelectFolder s;
  103. 		s.OnSelectFolder = ScreenSelectFolder_OnSelectFolder;
  104. 		FlowStack.Branch(s);
  105. 	}
  106.  
  107. 	//handler for when folder selected on the ScreenSelectFolder
  108. 	void ScreenSelectFolder_OnSelectFolder(String FolderPath)
  109. 	{
  110. 		String path;
  111.  
  112. 		//save the path in the Attributes collection of this applet
  113. 		Attributes.Add("FolderPath", FolderPath);
  114. 		//save the folder path to file so if the user restarts their device it will still be the same
  115. 		File.Write("Settings.txt", Attributes);
  116.  
  117. 		//get files in the folder
  118. 		Path.GetFiles(FolderPath, tblFiles);
  119. 		//make sure list doesn't include any directories
  120. 		Path.RemoveDirectories(tblFiles);
  121.  
  122. 		//how many pictures?
  123. 		PictureCount = tblFiles.GetCount();
  124. 		CurrentPicture = -1;
  125.  
  126. 		ShowNextPicture(1);
  127. 	}
  128.  
  129. 	//click handler for the play button
  130. 	void btnPlayPause_OnClick()
  131. 	{
  132. 		//start the picture cycle timer
  133. 		ssUpdateTimer.StartSecondsTimer(ssInterval);
  134. 		//hide the controls
  135. 		ControlsFade(true);
  136. 	}
  137.  
  138. 	//handler for the next picture button
  139. 	void btnNext_OnClick()
  140. 	{
  141. 		//ensure auto cycling is stopped
  142. 		ssUpdateTimer.Stop();
  143.  
  144. 		//show next picture
  145. 		ShowNextPicture(1);
  146. 	}
  147.  
  148. 	//click handler for previous button
  149. 	void btnPrev_OnClick()
  150. 	{
  151. 		//ensure auto cycling is stopped
  152. 		ssUpdateTimer.Stop();
  153.  
  154. 		//show previous picture
  155. 		ShowNextPicture(-1);
  156. 	}
  157.  
  158. 	//shows the picture specified in path
  159. 	void ShowPicture(String path)
  160. 	{
  161. 		//load the image and force it to the required dimensions
  162. 		imgScreenshot.Surface.LoadFromFile(path, displayWidth, displayHeight);
  163.  
  164. 		//did it load?
  165. 		if (imgScreenshot.Surface.GetWidth() <= 0)
  166. 		{
  167. 			//no, so use the default image
  168. 			imgScreenshot.Surface.LoadFromFile("Default.Image.jpg");
  169. 		}
  170.  
  171. 		//make sure the image control is positioned correctly
  172. 		imgScreenshot.SetBounds(displayX, displayY);
  173.  
  174. 		//and make sure screen is forced to update
  175. 		FlagScreenChanged();
  176. 	}
  177.  
  178. 	//show or hide the navigation buttons
  179. 	void ControlsFade(bool DirectionUp)
  180. 	{
  181. 		int Start, Finish;
  182.  
  183. 		if (DirectionUp)
  184. 		{
  185. 			if (ControlsAreBright == false)
  186. 			{
  187. 				return;
  188. 			}
  189. 			ControlsAreBright = false;
  190. 			Start = 100;
  191. 			Finish = 10;
  192. 		}
  193. 		else
  194. 		{
  195. 			if (ControlsAreBright == true)
  196. 			{
  197. 				return;
  198. 			}
  199. 			ControlsAreBright = true;
  200. 			Start = 10;
  201. 			Finish = 100;
  202. 		}
  203.  
  204. 		//clear any previous animations that had been applied to these controls
  205. 		//because don't want parallel animations running and fighting each other
  206. 		btnPrev.AnimateClear();
  207. 		btnPlayPause.AnimateClear();
  208. 		btnSettings.AnimateClear();
  209. 		btnNext.AnimateClear();
  210.  
  211. 		//fade the controls either in or out based on the Start/Finish values
  212. 		btnPrev.AnimateFade(Start, Finish, 5);
  213. 		btnPlayPause.AnimateFade(Start, Finish, 5, 1);
  214. 		btnSettings.AnimateFade(Start, Finish, 5, 2);
  215. 		btnNext.AnimateFade(Start, Finish, 5, 3);
  216. 	}
  217.  
  218. 	//this method is called once for the Applet when it is being initially loaded
  219. 	//and is where the controls for the applet are initialised, settings fetched from file etc
  220. 	void Load()
  221. 	{
  222. 		x = 0;
  223. 		y = 0;
  224. 		w = GetWidth();
  225. 		r = x + w;
  226.  
  227. 		//frame controls that will be displayed around the picture
  228. 		y = 0;
  229. 		imgTop.Surface.LoadFromFile("Frame.Top.jif");
  230. 		Controls.Add(imgTop);
  231. 		imgTop.SetBounds(x, y);
  232. 		y += imgTop.GetHeight();
  233. 		imgLeft.Surface.LoadFromFile("Frame.Left.jif");
  234. 		Controls.Add(imgLeft);
  235. 		imgLeft.SetBounds(x, y);
  236. 		imgRight.Surface.LoadFromFile("Frame.Right.jif");
  237. 		Controls.Add(imgRight);
  238. 		r -= imgRight.GetWidth();
  239. 		imgRight.SetBounds(r, y);
  240. 		y += imgLeft.GetHeight();
  241. 		imgBottom.Surface.LoadFromFile("Frame.Bottom.jif");
  242. 		Controls.Add(imgBottom);
  243. 		imgBottom.SetBounds(x, y);
  244.  
  245. 		//determine the display area limits - how much space is there to show the slideshow pictures?
  246. 		displayHeight = GetHeight();
  247. 		displayHeight -= ((imgTop.GetHeight() + imgBottom.GetHeight()) / 2);
  248. 		displayWidth = GetWidth();
  249. 		displayWidth -= ((imgLeft.GetWidth() + imgRight.GetWidth()) / 2);
  250. 		displayX = imgLeft.GetWidth() / 2;
  251. 		displayY = imgTop.GetHeight() / 2;
  252.  
  253. 		//load the default picture
  254. 		imgScreenshot.Surface.LoadFromFile("Default.Image.jpg", displayWidth, displayHeight);
  255. 		Controls.Add(imgScreenshot);
  256. 		imgScreenshot.SendToBack();
  257. 		imgScreenshot.SetBounds(displayX, displayY);
  258. 		imgScreenshot.SetTabStop(true);
  259. 		imgScreenshot.OnClick = imgScreenshot_OnClick;
  260.  
  261. 		x = 0;
  262.  
  263. 		//init the buttons
  264.  
  265. 		//load the images for normal and selected states
  266. 		btnPrev.ImageSelected.LoadFromFile("Button.Previous.Selected.jif");
  267. 		btnPrev.Image.LoadFromFile("Button.Previous.jif");
  268. 		//add this button to be a direct child of the applet
  269. 		Controls.Add(btnPrev);
  270. 		//position it
  271. 		y = GetHeight() - btnPrev.GetHeight();
  272. 		btnPrev.SetBounds(x, y);
  273. 		//provide an OnClick event handler so can respond to when the user clicks the button
  274. 		btnPrev.OnClick = btnPrev_OnClick;
  275.  
  276. 		x += btnPrev.GetWidth();
  277. 		btnPlayPause.ImageSelected.LoadFromFile("Button.Right.Arrow.Selected.jif");
  278. 		btnPlayPause.Image.LoadFromFile("Button.Right.Arrow.jif");
  279. 		Controls.Add(btnPlayPause);
  280. 		btnPlayPause.SetBounds(x, y);
  281. 		x += btnPlayPause.GetWidth();
  282. 		btnPlayPause.OnClick = btnPlayPause_OnClick;
  283.  
  284. 		btnSettings.ImageSelected.LoadFromFile("Button.Settings.Selected.jif");
  285. 		btnSettings.Image.LoadFromFile("Button.Settings.jif");
  286. 		Controls.Add(btnSettings);
  287. 		btnSettings.SetBounds(x, y);
  288. 		x += btnSettings.GetWidth();
  289. 		btnSettings.OnClick = btnSettings_OnClick;
  290.  
  291. 		btnNext.ImageSelected.LoadFromFile("Button.Next.Selected.jif");
  292. 		btnNext.Image.LoadFromFile("Button.Next.jif");
  293. 		Controls.Add(btnNext);
  294. 		btnNext.SetBounds(x, y);
  295. 		x += btnNext.GetWidth();
  296. 		btnNext.OnClick = btnNext_OnClick;
  297.  
  298. 		//when playing go to next picture every 3 seconds
  299. 		ssInterval = 3;
  300. 		//set the event handler for OnTimer
  301. 		ssUpdateTimer.OnTimer = ssUpdateTimer_OnTimer;
  302.  
  303. 		ControlsAreBright = true;
  304.  
  305. 		PictureCount = 0;
  306.  
  307. 		//load the settings into the Attributes collection
  308. 		File.Read("Settings.txt", Attributes);
  309. 		//read the last folder that was being viewed
  310. 		PictureFolderPath = Attributes.Item("FolderPath");
  311.  
  312. 		//is there are path previously saved?
  313. 		if (PictureFolderPath.GetLength() > 0)
  314. 		{
  315. 			//yes, so make it load right now
  316. 			ScreenSelectFolder_OnSelectFolder(PictureFolderPath);
  317. 		}
  318. 	}
  319.  
  320. 	//this method is called when the applet loses focus for some reason such as the user
  321. 	//sliding to another applet, or going off to another screen after having this applet visible
  322. 	void Deactivated()
  323. 	{
  324. 		//stop the update timer - no point loading more images if the user can't see them
  325. 		ssUpdateTimer.Stop();
  326. 		//show controls again
  327. 		ControlsFade(false);
  328. 	}
  329.  
  330. }
  331. #endregion 
  332.  
  333. #region Screens
  334.  
  335. //screen that allows browsing through file system to select a folder
  336. class ScreenSelectFolder : FileExplorerScreen
  337. {
  338. 	//title of the screen
  339. 	Label lblTitle;
  340.  
  341. 	//initialize the screen
  342. 	void Load()
  343. 	{
  344. 		//init the title label
  345. 		Controls.Add(lblTitle);
  346. 		SetTitlePosition(lblTitle);
  347.  
  348. 		//set the properties for this FileExplorerScreen
  349. 		SetBasePath("\\");
  350. 		SetDisplayFileExt(true);
  351. 		SetMode("SelectFolder");
  352. 	}
  353.  
  354. 	//event to raise when the user selects a folder
  355. 	Event OnSelectFolder;
  356.  
  357. 	//this method is called by the FileExplorerScreen when the user selects a folder
  358. 	void FolderSelected(String filename)
  359. 	{
  360. 		//raise the event
  361. 		OnSelectFolder(filename);
  362.  
  363. 		//return from the current screen
  364. 		FlowStack.Return();
  365. 	}
  366.  
  367. 	//this method is called by the FileExplorerScreen when the user navigates to each folder
  368. 	//while trying to select one
  369. 	void FolderChanged(String folder)
  370. 	{
  371. 		//get just the current folder name from the full path
  372. 		String s;
  373. 		s = Path.GetFilename(folder);
  374.  
  375. 		//are we at root?
  376. 		if (s.GetLength() <= 0)
  377. 		{
  378. 			s = "Choose Slideshow Folder";
  379. 		}
  380.  
  381. 		//set the screen title
  382. 		lblTitle.SetText(s);
  383. 	}
  384.  
  385. }
  386.  
  387. #endregion

Remarks

Line 20: This slideshow applet inherits from the Applet class. Notice the name of the class "SlideshowApplet". All applet classes must be named with the word "Applet" at the end. Because this applet is called "Slideshow" there needs to be a corresponding folder in the file system (ApplcationDirectory)\AppletRibbon\Slideshow. All files including the Slideshow.cs script file, images, and any other configuration files like xml files must sit in this folder.