./screen_fade.lua

  1.  
  2. local SCREEN_FADE_STYLE_LEFT_TO_RIGHT 	= 0 
  3. local SCREEN_FADE_STYLE_ALPHA 			= 1 
  4.  
  5. local SCREEN_FADE_TYPE_IN 	= 0 
  6. local SCREEN_FADE_TYPE_OUT = 1 
  7.  
  8. local SCREEN_FADE_DEFAULT_DURATION = 1000		-- only used for classic in out fades.. 
  9.  
  10. local SCREEN_FADE_LEFT 	= 1 
  11. local SCREEN_FADE_RIGHT = -1 
  12.  
  13. local Screen_fade_state	= -1	--The state of the last called fade... 
  14.  
  15. local Screen_fades = { 
  16. 	[SCREEN_FADE_STYLE_LEFT_TO_RIGHT] = { 
  17. 		[SCREEN_FADE_TYPE_OUT] = { 
  18. 			anim_name = "left_to_right_black_anim", 
  19. 			grp_name = "screen_left_to_right_grp", 
  20. 		}, 
  21. 		[SCREEN_FADE_TYPE_IN] = { 
  22. 			anim_name = "left_to_right_none_anim", 
  23. 			grp_name = "screen_left_to_right_grp", 
  24. 		}, 
  25. 	},	 
  26. 	[SCREEN_FADE_STYLE_ALPHA] = { 
  27. 		[SCREEN_FADE_TYPE_OUT] = { 
  28. 			anim_name = "fade_none_to_black_anim",  
  29. 			grp_name = "screen_fade_grp" 
  30. 		}, 
  31. 		[SCREEN_FADE_TYPE_IN] = { 
  32. 			anim_name = "fade_black_to_none_anim", 
  33. 			grp_name = "screen_fade_grp" 
  34. 		}, 
  35. 	}, 
  36. } 
  37.  
  38. local Screen_fade_logo_is_visible 		= false		-- determines if our logo is visible or not. 
  39. local Screen_fade_images_is_visible 	= false	-- deterimes if our images are visible or not... 
  40.  
  41. local Screen_fade_images_grp_h 			= 0 
  42. local Screen_fade_images_trans_anim_h 	= 0 
  43. local Screen_fade_images_fade_anim_h 	= 0 
  44. local Screen_fade_images_fade_twn_h 	= 0 
  45.  
  46.  
  47. --Screen fade images globals... 
  48. Screen_fade_images = {	 
  49. 	"ui_mainmenu_load_01", 
  50. 	"ui_mainmenu_load_02", 
  51. 	"ui_mainmenu_load_03", 
  52. 	"ui_mainmenu_load_04", 
  53. 	"ui_mainmenu_load_05", 
  54. 	"ui_mainmenu_load_06", 
  55. 	"ui_mainmenu_load_07", 
  56. 	"ui_mainmenu_load_08", 
  57. 	"ui_mainmenu_load_09", 
  58. 	"ui_mainmenu_load_10", 
  59. 	"ui_mainmenu_load_11", 
  60. } 
  61.  
  62. Screen_fade_image_1_h = 0 
  63. Screen_fade_image_2_h = 0 
  64. Screen_fade_loaded_images = {} 
  65. Screen_fade_use_load_images 				= false 
  66.  
  67. Screen_fade_autosave_grp_h = 0 
  68. Screen_fade_autosave_grp_x, Screen_fade_autosave_grp_y = 0, 0 
  69.  
  70. function screen_fade_init() 
  71. 	--Hide all groups 
  72. 	local safe_frame_h = vint_object_find("screen_grp") 
  73. 	vint_hide_children(safe_frame_h) 
  74. 	 
  75. 	--Setup tween callbacks.... 
  76. 	local tween_names_for_cb = { 
  77. 		"left_to_right_black_twn", 
  78. 		"fade_black_to_none_twn", 
  79. 		"fade_none_to_black_twn", 
  80. 		"left_to_right_none_twn", 
  81. 	} 
  82. 	for idx, val in pairs(tween_names_for_cb) do 
  83. 		local h = vint_object_find(val) 
  84. 		vint_set_property(h, "end_event", "screen_fade_complete_cb") 
  85. 	end	 
  86. 	 
  87. 	--hide loading 
  88. 	local loading_grp_h = vint_object_find("loading_grp") 
  89. 	vint_set_property(loading_grp_h, "alpha", 0) 
  90. 	 
  91. 	local loading_fade_out_twn_h = vint_object_find("loading_fade_out_twn") 
  92. 	vint_set_property(loading_fade_out_twn_h, "end_event", "screen_fade_logo_out_cb") 
  93. 	 
  94. 	--loop animation that plays over everythinng... 
  95. 	local loading_anim_h = vint_object_find("loading_anim") 
  96. 	local loading_twn_h = vint_object_find("loading_twn", loading_anim_h) 
  97. 	vint_set_property(loading_twn_h, "end_event", "vint_anim_loop_callback") 
  98. 		 
  99. 	--Initialize Image Stuff 
  100. 	screen_fade_images_init() 
  101. 	 
  102. 	--Initialize auto save text... 
  103. 	screen_fade_auto_save_init() 
  104. 	 
  105. 	--Hide auto save by default 
  106. 	screen_fade_auto_save_hide() 
  107. end 
  108.  
  109. function screen_fade_cleanup() 
  110. 	screen_fade_images_cleanup() 
  111. end 
  112.  
  113. ------------------------------------------------------------------------------- 
  114. -- Fades the screen. This should be called directly from c++ 
  115. -- @param	style		style of fade(left to right, right to left, fade) SCREEN_FADE_STYLE_LEFT_TO_RIGHT, SCREEN_FADE_STYLE_RIGHT_TO_LEFT, SCREEN_FADE_STYLE_ALPHA) 
  116. -- @param	type		In or out? out = show black, in = show what is behind. (SCREEN_FADE_TYPE_IN, SCREEN_FADE_TYPE_OUT) 
  117. -- @param	duration	only for SCREEN_FADE_STYLE_ALPHA stuff. defaults to .25 seconds.  Units passed in are in milleseconds. 
  118. ------------------------------------------------------------------------------- 
  119. function screen_fade_do(style, type, duration) 
  120. 	-- Force Hide all groups 
  121. 	local safe_frame_h = vint_object_find("screen_grp") 
  122. 	vint_hide_children(safe_frame_h) 
  123.  
  124. 	-- Force Pause all animations... 
  125. 	for key, val in pairs(Screen_fades) do 
  126. 		for k, v in pairs(val) do 
  127. 			local anim_h = vint_object_find(v.anim_name) 
  128. 			vint_set_property(anim_h, "is_paused", true) 
  129. 		end 
  130. 	end 
  131. 	 
  132. 	local screen_fade_data = Screen_fades[style][type] 
  133. 	if screen_fade_data == nil then 
  134. 		return 
  135. 	end 
  136.  
  137. 	-- Normal screen fades can accept a duration... 
  138. 	if style == SCREEN_FADE_STYLE_ALPHA then 
  139. 		local fade_1_h = vint_object_find("fade_black_to_none_twn") 
  140. 		local fade_2_h = vint_object_find("fade_none_to_black_twn") 
  141. 		 
  142. 		if duration == nil or duration < 0 then 
  143. 			duration = SCREEN_FADE_DEFAULT_DURATION 
  144. 		end 
  145. 		 
  146. 		-- convert to seconds 
  147. 		duration = duration * 0.001 
  148. 		 
  149. 		vint_set_property(fade_1_h, "duration", duration) 
  150. 		vint_set_property(fade_2_h, "duration", duration) 
  151. 	end 
  152.  
  153. 	--check to see if we need to hide the logo 
  154. 	if Screen_fade_logo_is_visible and type == SCREEN_FADE_TYPE_IN then 
  155. 		screen_fade_logo_hide() 
  156. 	end 
  157. 	 
  158. 	local fade_out_delay = 0 
  159. 	if Screen_fade_images_is_visible and type == SCREEN_FADE_TYPE_IN then 
  160. 		fade_out_delay = 1 
  161. 		screen_fade_images_hide() 
  162. 	end 
  163. 	 
  164. 	local anim_h = vint_object_find(screen_fade_data.anim_name) 
  165. 	local grp_h = vint_object_find(screen_fade_data.grp_name) 
  166. 	vint_set_property(grp_h, "visible", true) 
  167. 	lua_play_anim(anim_h, fade_out_delay) 
  168. 	 
  169. 	Screen_fade_state = type 
  170. end 
  171.  
  172. --This is called when the transition is finished 
  173. function screen_fade_complete_cb(tween_h) 
  174. 	--Let C know we are done fading out the screen... 
  175. 	Screen_fade_transition_complete()  
  176.  
  177. 	--If we are fading back into game hide the bitmaps... 
  178. 	if Screen_fade_state == SCREEN_FADE_TYPE_IN then 
  179. 		-- Force Hide all groups 
  180. 		local safe_frame_h = vint_object_find("screen_grp") 
  181. 		vint_hide_children(safe_frame_h) 
  182. 	end 
  183. end 
  184.  
  185. ------------------------------------------------------------------------------- 
  186. -- C determines if we are going to show a logo during this transition... 
  187. -- 
  188. function screen_fade_logo_show() 
  189. 	 
  190. 	if Screen_fade_state == SCREEN_FADE_TYPE_IN then 
  191. 		--Do not show the logo if we are faded in... 
  192. 		return 
  193. 	end 
  194. 	 
  195. 	--Play fade out of logo 
  196. 	local loading_fade_out_anim_h = vint_object_find("loading_fade_out_anim") 
  197. 	vint_set_property(loading_fade_out_anim_h, "is_paused", true) 
  198.  
  199. 	--Start highlight on logo... 
  200. 	local loading_anim_h = vint_object_find("loading_anim") 
  201. 	lua_play_anim(loading_anim_h) 
  202. 	 
  203. 	--Pulse center of logo... 
  204. 	local loading_pulse_anim_h = vint_object_find("loading_pulse_anim") 
  205. 	lua_play_anim(loading_pulse_anim_h) 
  206. 	 
  207. 	--Fade in logo 
  208. 	local loading_fade_in_anim_h = vint_object_find("loading_fade_in_anim") 
  209. 	lua_play_anim(loading_fade_in_anim_h) 
  210. 	 
  211. 	Screen_fade_logo_is_visible = true 
  212. end 
  213.  
  214.  
  215. ------------------------------------------------------------------------------- 
  216. -- This is called to fade out the logo... 
  217. -- 
  218. function screen_fade_logo_hide() 
  219. 	-- Pause fade in of logo... 
  220. 	local loading_fade_in_anim_h = vint_object_find("loading_fade_in_anim") 
  221. 	vint_set_property(loading_fade_in_anim_h, "is_paused", true) 
  222. 	 
  223. 	--Play fade out of logo 
  224. 	local loading_fade_out_anim_h = vint_object_find("loading_fade_out_anim") 
  225. 	lua_play_anim(loading_fade_out_anim_h) 
  226. end 
  227.  
  228. -------------------------------------------------------------------------------- 
  229. -- Called when logo is completely faded out 
  230. -- 
  231. function screen_fade_logo_out_cb() 
  232. 	--Start highlight on logo... 
  233. 	local loading_anim_h = vint_object_find("loading_anim") 
  234. 	vint_set_property(loading_anim_h, "is_paused", true) 
  235. 	 
  236. 	--Pulse center of logo... 
  237. 	local loading_pulse_anim_h = vint_object_find("loading_pulse_anim") 
  238. 	vint_set_property(loading_pulse_anim_h, "is_paused", true) 
  239. 	Screen_fade_logo_is_visible = false 
  240. end 
  241.  
  242. -------------------------------------------------------------------------------- 
  243. -- Shows "Saving..." text and displays rotating spinner 
  244. -- 
  245. function screen_fade_auto_save_show() 
  246. 	 
  247. 	vint_set_property(Screen_fade_autosave_grp_h, "visible", true) 
  248. 	 
  249. 	local autosave_loop_anim_h = vint_object_find("autosave_loop_anim") 
  250. 	lua_play_anim(autosave_loop_anim_h) 
  251.  
  252. 	local hud_doc_h = vint_document_find("hud") 
  253. 	local move_y = 0 
  254. 	if hud_doc_h ~= 0 and Hud_gsi ~= -1 then 
  255. 		--check if hud gsi is active 
  256. 		if Hud_gsi:is_active_get() then 
  257. 			--Move it 
  258. 			local width, height = Hud_gsi:get_size() 
  259. 			move_y = Screen_fade_autosave_grp_y + height + 5 
  260. 		end 
  261. 	end 
  262. 	screen_fade_auto_save_move(move_y) 
  263. end 
  264.  
  265. -------------------------------------------------------------------------------- 
  266. -- Hides "Saving..." text  
  267. -- 
  268. function screen_fade_auto_save_hide() 
  269. 	local autosave_grp_h = vint_object_find("autosave_grp") 
  270. 	vint_set_property(autosave_grp_h, "visible", false) 
  271. 	 
  272. 	local autosave_loop_anim_h = vint_object_find("autosave_loop_anim") 
  273. 	vint_set_property(autosave_loop_anim_h, "is_paused", true) 
  274. end 
  275.  
  276. ------------------------------------------------------------------------------- 
  277. -- Initialize screen fades 
  278. -- Setup text boxes 
  279. -- 
  280. function screen_fade_auto_save_init() 
  281. 	Screen_fade_autosave_grp_h = vint_object_find("autosave_grp") 
  282. 	local saving_content_text_1_h = vint_object_find("saving_content_text_1") 
  283. 	local saving_content_bg_h 		= vint_object_find("saving_content_bg") 
  284. 	 
  285. 	vint_set_property(saving_content_text_1_h, "text_tag", "SAVE_WARNING_GAME")	--Saving Content 
  286. 	 
  287. 	local bg_width, bg_height = element_get_actual_size(saving_content_bg_h) 
  288. 	local text_1_width, text_1_height = element_get_actual_size(saving_content_text_1_h) 
  289. 	local text_1_x, text_1_y = vint_get_property(saving_content_text_1_h, "anchor") 
  290. 	local BG_PADDING = 10	 
  291. 		 
  292. 	local max_width = 340 
  293. 	if text_1_width > max_width then 
  294. 		local scale = max_width / text_1_width 
  295. 		vint_set_property(saving_content_text_1_h, "scale", scale, scale) 
  296. 	end 
  297. 	 
  298. 	local text_1_width, text_1_height = element_get_actual_size(saving_content_text_1_h) 
  299. 	bg_width = text_1_x + text_1_width + BG_PADDING 
  300. 	element_set_actual_size(saving_content_bg_h, bg_width, bg_height)	 
  301. 	 
  302. 	Screen_fade_autosave_grp_x, Screen_fade_autosave_grp_y = vint_get_property(Screen_fade_autosave_grp_h, "anchor") 
  303. end 
  304.  
  305. function screen_fade_auto_save_move(y_position) 
  306. 	if y_position ~= 0 then 
  307. 		vint_set_property(Screen_fade_autosave_grp_h, "anchor", Screen_fade_autosave_grp_x, y_position) 
  308. 	else 
  309. 		vint_set_property(Screen_fade_autosave_grp_h, "anchor", Screen_fade_autosave_grp_x, Screen_fade_autosave_grp_y) 
  310. 	end 
  311. end 
  312.  
  313.  
  314. ------------------------------------------------------------------------------- 
  315. -- Initialize screen fade images (called from init) 
  316. -- 
  317. function screen_fade_images_init() 
  318. 	Screen_fade_images_grp_h = vint_object_find("images_grp") 
  319. 	Screen_fade_images_fade_anim_h = vint_object_find("images_fade_anim") 
  320. 	Screen_fade_images_fade_twn_h  = vint_object_find("images_fade_twn", Screen_fade_images_fade_anim_h) 
  321. 	Screen_fade_images_trans_anim_h  = vint_object_find("images_transition_anim") 		 
  322. 	 
  323. 	--Setup callback for fading in/out 
  324. 	vint_set_property(Screen_fade_images_fade_twn_h, "end_event", "screen_fade_images_fade_cb") 
  325. 	 
  326. 	--Set up loop for images... 
  327. 	local bmp1_fade_in_twn_h = vint_object_find("bmp1_fade_in_twn", Screen_fade_images_trans_anim_h) 
  328. 	vint_set_property(bmp1_fade_in_twn_h, "end_event", "vint_anim_loop_callback") 
  329.  
  330. 	--Hide images...	 
  331. 	vint_set_property(Screen_fade_images_grp_h, "alpha", 0) 
  332. 	 
  333. 	--Store paths to images... 
  334. 	Screen_fade_image_1_h = vint_object_find("bmp1", Screen_fade_images_grp_h) 
  335. 	Screen_fade_image_2_h = vint_object_find("bmp2", Screen_fade_images_grp_h) 
  336. 	 
  337. 	if sfx_use_load_images() then 
  338. 		local loading_images = {} 
  339. 		local random_num_values = {} 
  340. 		local random_num_count = 1 
  341. 		local screen_fade_images_num = #Screen_fade_images 
  342. 		 
  343. 		while #random_num_values < 2 do 
  344. 			local found_match = false 
  345. 			local random_num = rand_int(1, screen_fade_images_num) 
  346. 			for i = 1, 3 do 
  347. 				if random_num == random_num_values[i] then 
  348. 					found_match = true 
  349. 				end 
  350. 			end 
  351. 			if found_match == false then 
  352. 				random_num_values[random_num_count] = random_num 
  353. 				random_num_count = random_num_count + 1 
  354. 			end 
  355. 		end 
  356. 		 
  357. 		local platform_file_ext = "" 
  358. 		if game_get_platform() == "PC" then 
  359. 			platform_file_ext = "_pc" 
  360. 		end 
  361. 		 
  362. 		for i = 1, #random_num_values do 
  363. 			loading_images[i] = Screen_fade_images[random_num_values[i]] .. platform_file_ext 
  364. 		end 
  365.  
  366. 		Screen_fade_loaded_images = loading_images 
  367. 		game_peg_load_with_cb("screen_fade_images_loaded", 2, loading_images[1],  loading_images[2])	 
  368. 		Screen_fade_use_load_images = true 
  369. 	end 
  370. end 
  371.  
  372. ------------------------------------------------------------------------------- 
  373. -- Cleanup any loaded images... (called from cleanup) 
  374. -- 
  375. function screen_fade_images_cleanup() 
  376. 	if Screen_fade_use_load_images then 
  377. 		--Free up memory... 
  378. 		for i, v in pairs(Screen_fade_loaded_images) do 
  379. 			game_peg_unload(v) 
  380. 		end 
  381. 		Screen_fade_loaded_images = {} 
  382. 	end 
  383. end 
  384.  
  385. ------------------------------------------------------------------------------- 
  386. -- Shows the screen fade images... (called from c++) 
  387. -- 
  388. function screen_fade_images_show() 
  389. 	--Fade in group based and fade it in with transition animation... 
  390. 	local images_alpha = vint_get_property(Screen_fade_images_grp_h, "alpha") 
  391. 	vint_set_property(Screen_fade_images_fade_twn_h, "start_value", images_alpha) 
  392. 	vint_set_property(Screen_fade_images_fade_twn_h, "end_value", 1) 
  393. 	 
  394. 	--Apply start values of animations 
  395. 	vint_apply_start_values(Screen_fade_images_fade_anim_h) 
  396. 	vint_apply_start_values(Screen_fade_images_trans_anim_h) 
  397. 	 
  398. 	--Start animations 
  399. 	lua_play_anim(Screen_fade_images_fade_anim_h) 
  400. 	lua_play_anim(Screen_fade_images_trans_anim_h) 
  401. 	 
  402. 	Screen_fade_images_is_visible = true 
  403. end 
  404.  
  405. ------------------------------------------------------------------------------- 
  406. -- Hides the screen fade images... (called from c++) 
  407. -- 
  408. function screen_fade_images_hide() 
  409. 	--Fade in group based and fade it in with transition animation... 
  410. 	local images_alpha = vint_get_property(Screen_fade_images_grp_h, "alpha") 
  411. 	vint_set_property(Screen_fade_images_fade_twn_h, "start_value", images_alpha) 
  412. 	vint_set_property(Screen_fade_images_fade_twn_h, "end_value", 0) 
  413. 	 
  414. 	lua_play_anim(Screen_fade_images_fade_anim_h) 
  415. 	 
  416. 	Screen_fade_images_is_visible = false 
  417. end 
  418.  
  419. ------------------------------------------------------------------------------- 
  420. -- Callback for when the images are done fading... 
  421. -- 
  422. function screen_fade_images_fade_cb() 
  423. 	if Screen_fade_images_is_visible == false then 
  424. 		--stop transition animation if we are paused... 
  425. 		vint_set_property(Screen_fade_images_trans_anim_h, "is_paused", true) 
  426. 	end 
  427. end 
  428.  
  429. ------------------------------------------------------------------------------- 
  430. -- Callabck to set the images that are loaded 
  431. -- 
  432. function screen_fade_images_loaded() 
  433. 	--Replace image names with loaded images 
  434. 	vint_set_property(Screen_fade_image_1_h, "image", Screen_fade_loaded_images[1]) 
  435. 	vint_set_property(Screen_fade_image_2_h, "image", Screen_fade_loaded_images[2]) 
  436. end 
  437.  
  438. ------------------------------------------------------------------------------- 
  439. -- These are test functions to test the fades... 
  440. ------------------------------------------------------------------------------- 
  441. function screen_fade_1()  
  442. 	screen_fade_do(SCREEN_FADE_STYLE_LEFT_TO_RIGHT, SCREEN_FADE_TYPE_IN)	 
  443. end 
  444. function screen_fade_2() 
  445. 	screen_fade_do(SCREEN_FADE_STYLE_LEFT_TO_RIGHT, SCREEN_FADE_TYPE_OUT)	 
  446. end 
  447. function screen_fade_3() 
  448. 	screen_fade_do(SCREEN_FADE_STYLE_ALPHA, SCREEN_FADE_TYPE_IN)	 
  449. end 
  450. function screen_fade_4() 
  451. 	screen_fade_do(SCREEN_FADE_STYLE_ALPHA, SCREEN_FADE_TYPE_OUT)	 
  452. end 
  453. function screen_fade_5() 
  454. 	screen_fade_do(SCREEN_FADE_STYLE_ALPHA, SCREEN_FADE_TYPE_IN, 5000)	 
  455. end 
  456. function screen_fade_6() 
  457. 	screen_fade_do(SCREEN_FADE_STYLE_ALPHA, SCREEN_FADE_TYPE_OUT, 5000)	 
  458. end 
  459.  
  460. --Debug stuff put in for jon bruer JMH(3/24/2011) 
  461. function screen_fade_hide() 
  462. 	local safe_frame_h = vint_object_find("screen_grp") 
  463. 	vint_hide_children(safe_frame_h) 
  464. end 
  465.  
  466.