sr2lua/cdl01.lua

  1. 										----------------------- 
  2. 										--[ CAR  DEALERSHIP ]-- 
  3. 										----------------------- 
  4.  
  5. --[ SYSTEM FUNCTIONS ]-- 
  6.  
  7. function cdl01_init() 
  8. 	local store_name_crc, store_name_str = cdl_store_info() 
  9. 	 
  10. 	if store_name_crc ~= 0 then 
  11. 		Cdl01_car_menu.header_label_crc = store_name_crc 
  12. 		Cdl01_color_menu.header_label_crc = store_name_crc 
  13. 	else 
  14. 		Cdl01_car_menu.header_label_str = store_name_str 
  15. 		Cdl01_color_menu.header_label_str = store_name_str 
  16. 	end 
  17.  
  18. 	menu_store_init(true) 
  19. 	menu_init() 
  20. 	menu_show(Cdl01_car_menu) 
  21. 	 
  22. 	--Event Tracking (Register Interface) 
  23. 	event_tracking_interface_enter("Car Dealership") 
  24. end 
  25.  
  26. -- Shutdown and cleanup the menu 
  27. function cdl01_cleanup() 
  28.  
  29. end 
  30.  
  31. function cdl01_store_exit() 
  32. 	dialog_box_confirmation("STORE_EXIT_WARNING_HEADER", "STORE_EXIT_WARNING_BODY", "cdl01_exit_confirm") 
  33. end 
  34.  
  35. function cdl01_exit_confirm(result, action) 
  36. 	if action == DIALOG_ACTION_CLOSE and result == DIALOG_RESULT_YES then 
  37. 		cdl01_exit_final() 
  38. 	end 
  39. end 
  40.  
  41. function cdl01_exit_final() 
  42. 	menu_close(cdl01_exit_final_final) 
  43. end 
  44.  
  45. function cdl01_exit_final_final() 
  46. 	vint_document_unload(vint_document_find("cdl01"))	 
  47. end 
  48.  
  49. --[ FOOTER FUNCTIONALITY ]-- 
  50.  
  51. function cdl01_build_footer(menu_data) 
  52. 	local grp = vint_object_clone(vint_object_find("style_footer"), Menu_option_labels.control_parent) 
  53. 	vint_set_property(grp, "visible", true) 
  54. 	 
  55. 	if menu_data.footer ~= nil and menu_data.footer.footer_grp ~= nil then 
  56. 		vint_object_destroy(menu_data.footer.footer_grp) 
  57. 	end 
  58. 	 
  59. 	menu_data.footer = {} 
  60. 	menu_data.footer.footer_grp = grp 
  61. 	menu_data.footer.label_h = vint_object_find("price_label", grp) 
  62. 	menu_data.footer.price_h = vint_object_find("price_amount", grp) 
  63. 	menu_data.footer.style_h = vint_object_find("style_amount", grp) 
  64. 	 
  65. 	--Build second horizontal rule 
  66. 	local bg_h = vint_object_find("menu_bg_header_hr", Menu_option_labels.control_parent) 
  67. 	bg_h = vint_object_clone(bg_h, grp) 
  68. 	vint_object_rename(bg_h, "footer_line") 
  69. 	vint_set_property(bg_h, "anchor", -17, 48) 
  70. 	menu_data.footer.line_h = bg_h 
  71. 	 
  72. 	--Attach button tips to this footer_grp 
  73. 	menu_data.footer.btn_tips, menu_data.footer.btn_tips_width = btn_tips_footer_build(grp, -5, 57) 
  74. 	 
  75. 	vint_set_property(menu_data.footer.price_h , "visible", true) 
  76. end 
  77.  
  78. function cdl01_update_car_footer(menu_data) 
  79. 	local price, style, label 
  80. 	local car_item = Cdl01_car_menu[Cdl01_car_menu.highlighted_item] 
  81. 	 
  82. 	if car_item.price ~= nil then 
  83. 		price = "$"..format_cash(car_item.price) 
  84. 		style = award_style("vehicle", car_item.price, true) 
  85.  
  86. 		if menu_store_get_player_cash() < car_item.price then 
  87. 			vint_set_property(menu_data.footer.price_h, "tint", 1, 0, 0) 
  88. 		else 
  89. 			vint_set_property(menu_data.footer.price_h, "tint", 0.88, 0.749, 0.05) 
  90. 		end 
  91.  
  92. 		vint_set_property(menu_data.footer.price_h, "visible", true) 
  93. 		vint_set_property(menu_data.footer.style_h, "visible", true) 
  94. 		vint_set_property(menu_data.footer.label_h, "visible", true) 
  95.  
  96. 		vint_set_property(menu_data.footer.price_h, "text_tag", price) 
  97. 		vint_set_property(menu_data.footer.style_h, "insert_values", style, 0) 
  98. 	else 
  99. 		vint_set_property(menu_data.footer.price_h, "visible", false) 
  100. 		vint_set_property(menu_data.footer.style_h, "visible", false) 
  101. 		vint_set_property(menu_data.footer.label_h, "visible", false) 
  102. 	end 
  103. end 
  104.  
  105. function cdl01_update_color_footer(menu_data) 
  106. 	local price, style, label 
  107. 	local car_item = Cdl01_car_menu[Cdl01_car_menu.highlighted_item] 
  108.  
  109. 	local menu_item = menu_data[0] 
  110. 	local idx = menu_item.cur_row * menu_item.num_cols + menu_item.cur_col 
  111. 	label = menu_item.swatches[idx].label 
  112. 	 
  113. 	if car_item.price ~= nil then 
  114. 		price = "$"..format_cash(car_item.price) 
  115. 		style = award_style("vehicle", car_item.price, true) 
  116.  
  117. 		if menu_store_get_player_cash() < car_item.price then 
  118. 			vint_set_property(menu_data.footer.price_h, "tint", 1, 0, 0) 
  119. 		else 
  120. 			vint_set_property(menu_data.footer.price_h, "tint", 0.88, 0.749, 0.05) 
  121. 		end 
  122. 	else 
  123. 		price = "" 
  124. 		style = "" 
  125. 	end 
  126. 	 
  127. 	vint_set_property(Cdl01_color_menu.footer.label_h, "text_tag", label) 
  128. 	vint_set_property(Cdl01_color_menu.footer.price_h, "text_tag", price) 
  129. 	vint_set_property(Cdl01_color_menu.footer.style_h, "insert_values", style, 0) 
  130. end 
  131.  
  132. function cdl01_finalize_footer(menu_data) 
  133. 	vint_set_property(menu_data.footer.price_h, "anchor", menu_data.menu_width - 10, 0) 
  134. 	 
  135. 	--Scale extra line for button tips 
  136. 	local source_x, source_y = vint_get_property(menu_data.footer.line_h, "source_se") 
  137. 	vint_set_property(menu_data.footer.line_h, "source_se", menu_data.menu_width + 10, source_y) 
  138. end 
  139.  
  140. function cdl01_footer_release(menu_data) 
  141. 	if menu_data.footer.footer_grp ~= nil then 
  142. 		vint_object_destroy(menu_data.footer.footer_grp) 
  143. 	end 
  144. end 
  145.  
  146. --[ CAR MENU FUNCTIONS ]-- 
  147.  
  148. function cdl01_car_menu_show(menu_data) 
  149. 	cdl01_build_footer(menu_data) 
  150. 	Cdl01_car_menu.num_items = 0 
  151. 	 
  152. 	if Cdl01_car_menu.highlighted_item  == nil then 
  153. 		Cdl01_car_menu.highlighted_item = 0 
  154. 	end 
  155. 	 
  156. 	vint_dataresponder_request("CDL_CARS", "cdl01_add_car_record", 0) 
  157. 	 
  158. 	if Cdl01_car_menu.num_items == 0 then 
  159. 		Cdl01_car_menu[0] = { label = "No Cars Available", type = MENU_ITEM_TYPE_SELECTABLE } 
  160. 		Cdl01_car_menu.num_items = 1 
  161. 	end 
  162. 	 
  163. 	cdl01_car_menu_nav(menu_data) 
  164. end 
  165.  
  166. function cdl01_add_car_record(name_str, car_index, car_price) 
  167. 	local c = Cdl01_car_menu.num_items 
  168. 	Cdl01_car_menu[c] = { label = name_str, type = MENU_ITEM_TYPE_SELECTABLE, car_index = car_index, price = car_price, on_select = cdl01_car_menu_select } 
  169. 	 
  170. 	c = c + 1 
  171. 	Cdl01_car_menu.num_items = c 
  172. end 
  173.  
  174. function cdl01_car_menu_nav(menu_data) 
  175. 	cdl_preview_car(menu_data[menu_data.highlighted_item].car_index) 
  176. 	cdl01_update_car_footer(menu_data) 
  177. end 
  178.  
  179. function cdl01_car_menu_back(menu_data) 
  180. 	cdl01_store_exit() 
  181. end 
  182.  
  183. function cdl01_car_menu_release(menu_data) 
  184. 	cdl01_footer_release(menu_data) 
  185. end 
  186.  
  187. function cdl01_car_menu_finalize(menu_data) 
  188. 	cdl01_finalize_footer(menu_data) 
  189. end 
  190.  
  191. function cdl01_car_menu_select(menu_label, menu_data) 
  192. 	if menu_data.price > menu_store_get_player_cash() then 
  193. 		dialog_box_message("MENU_TITLE_NOTICE", "HUD_SHOP_INSUFFICIENT_FUNDS") 
  194. 		return 
  195. 	end 
  196. 	 
  197. 	--make sure this car is loaded, otherwise you can't buy it yet! 
  198. 	if cdl_car_loaded() == true then 
  199. 		dialog_box_confirmation("CARDEALER_PURCHASE_CONFIRM_TITLE", "CARDEALER_PURCHASE_CONFIRM_BODY", "cdl01_purchase_confirm") 
  200. 	end 
  201. end 
  202.  
  203. function cdl01_car_menu_get_width(menu_data) 
  204. 	--Returns the max size of the menu which also includes the button tips. 
  205. 	local width = menu_data.footer.btn_tips_width  
  206. 	if Menu_option_labels.max_label_w ~= nil then 
  207. 		if width < Menu_option_labels.max_label_w then 
  208. 			width = Menu_option_labels.max_label_w 
  209. 		end 
  210. 	end 
  211. 	return width 
  212. end 
  213.  
  214. --[ COLOR MENU FUNCTIONS ]-- 
  215.  
  216. function cdl01_color_menu_show(menu_data) 
  217.  
  218. 	local menu_item = menu_data[0] 
  219. 	menu_item.swatches = { num_swatches = 0 } 
  220. 	menu_item.cur_idx = 0 
  221.  
  222. 	vint_dataresponder_request("CDL_COLORS", "cdl01_add_color_record", 0) 
  223.  
  224. 	menu_grid_show(menu_data, menu_item, vint_object_find("swatch_paint", nil, MENU_BASE_DOC_HANDLE))	 
  225.  
  226. 	cdl01_build_footer(menu_data) 
  227. 	cdl01_color_menu_nav(nil, menu_data[0]) 
  228. end 
  229.  
  230. function cdl01_add_color_record(name, index, base_r, base_g, base_b, spec_col_r, spec_col_g, spec_col_b, spec_alpha, fres_col_r, fres_col_g, fres_col_b, fres_con, refl) 
  231. 	local swatches = Cdl01_color_menu[0].swatches 
  232.  
  233. 	swatches[swatches.num_swatches] = { 
  234. 		label = name, color_index = index, 
  235. 		base_r = base_r, base_g = base_g, base_b = base_b,										-- Base paint color, used on base 
  236. 		spec_col_r = spec_col_r, spec_col_g = spec_col_g, spec_col_b = spec_col_b,		-- Specular color, used on spec and irr 
  237. 		spec_alpha = spec_alpha,                                                                                                                                                                                                                                                                            -- Specular alpha, used on spec 
  238. 		fres_col_r = fres_col_r, fres_col_g = fres_col_g, fres_col_b = fres_col_b,		-- Fresnel color, used on fres, refl, sky 
  239. 		fres_con = fres_con,                                                                                                                                                                                                                                                                                      -- Fresnel 'alpha' (contrast), used on fres 
  240. 		refl = refl                                                                                                                                                                                                                                                                                                                                             -- Reflection color 
  241. 	} 
  242.  
  243. 	swatches.num_swatches = swatches.num_swatches + 1 
  244. end 
  245.  
  246. function cdl01_color_swatch_update(swatch) 
  247. 	 local icon_h = vint_object_find("icon", swatch.swatch_h) 
  248. 	  
  249. 	 vint_set_property(icon_h, "visible", true) 
  250. 	 vint_set_property(swatch.swatch_h, "visible", true) 
  251. 	  
  252. 	 local base_h = vint_object_find("base", icon_h) 
  253. 	 local fres_h = vint_object_find("fres", icon_h) 
  254. 	 local irr_h  = vint_object_find("irr", icon_h) 
  255. 	 local refl_h = vint_object_find("refl", icon_h) 
  256. 	 local sky_h  = vint_object_find("sky", icon_h) 
  257. 	 local spec_h = vint_object_find("spec", icon_h) 
  258. 	  
  259. 	 -- Setup the colors 
  260. 	 vint_set_property(base_h, "tint", swatch.base_r, swatch.base_g, swatch.base_b) 
  261. 	 vint_set_property(spec_h, "tint", swatch.spec_col_r, swatch.spec_col_g, swatch.spec_col_b) 
  262. 	 vint_set_property(irr_h, "tint", swatch.spec_col_r, swatch.spec_col_g, swatch.spec_col_b) 
  263. 	 vint_set_property(fres_h, "tint", swatch.fres_col_r, swatch.fres_col_g, swatch.fres_col_b) 
  264. 	 vint_set_property(refl_h, "tint", swatch.fres_col_r, swatch.fres_col_g, swatch.fres_col_b) 
  265. 	  
  266. 	 --Setup the alphas 
  267. 	 vint_set_property(spec_h, "alpha", swatch.spec_alpha * 0.8) 
  268. 	 vint_set_property(refl_h, "alpha", swatch.refl) 
  269. 	 vint_set_property(sky_h, "alpha",  swatch.refl * 0.2) 
  270. 	 vint_set_property(fres_h, "alpha", swatch.fres_con * 0.5) 
  271. 	  
  272. 	 if swatch.fres_con == 0 then 
  273. 		vint_set_property(irr_h, "visible", false) 
  274. 	 end 
  275. end 
  276.  
  277. function cdl01_color_menu_nav(menu_label, menu_data) 
  278. 	local swatches = menu_data.swatches 
  279. 	local idx = menu_data.cur_row * menu_data.num_cols + menu_data.cur_col 
  280. 	 
  281. 	if idx < swatches.num_swatches then 
  282. 		cdl_preview_color(swatches[idx].color_index) 
  283. 		cdl01_update_color_footer(Cdl01_color_menu) 
  284. 	end 
  285. end 
  286.  
  287. function cdl01_color_menu_select(menu_label, menu_data) 
  288. --	dialog_box_confirmation("PURCHASE", "Purchase this car?", "cdl01_purchase_confirm") 
  289. end 
  290.  
  291. function cdl01_exit_deferred() 
  292. 	delay(.5) 
  293. 	while vint_document_find("menu_style_dialog") ~= 0 do 
  294. 		thread_yield() 
  295. 	end 
  296. 	cdl01_exit_final() 
  297. end 
  298.  
  299. function cdl01_purchase_confirm(result, action) 
  300. 	if action ~= DIALOG_ACTION_CLOSE then 
  301. 		return 
  302. 	end 
  303. 	 
  304. 	if result == 0 then 
  305. 		cdl_purchase_car() 
  306. 		award_style("vehicle", Cdl01_car_menu[Cdl01_car_menu.highlighted_item].price) 
  307. 		thread_new("cdl01_exit_deferred") 
  308. 	end 
  309. end 
  310.  
  311. function cdl01_color_menu_back(menu_data) 
  312. 	menu_show(Cdl01_car_menu) 
  313. end 
  314.  
  315. function cdl01_color_menu_release(menu_data) 
  316. 	cdl01_footer_release(menu_data) 
  317. 	menu_grid_release(menu_data[0]) 
  318. end 
  319.  
  320. function cdl01_color_menu_finalize(menu_data) 
  321. 	cdl01_finalize_footer(menu_data) 
  322. end 
  323.  
  324. --[ MENU DATA ]-- 
  325.  
  326. Cdl01_car_menu = { 
  327. 	header_label_str	= "NONE", 
  328. 	on_show				= cdl01_car_menu_show, 
  329. 	on_nav				= cdl01_car_menu_nav, 
  330. 	on_back				= cdl01_car_menu_back, 
  331. 	on_release			= cdl01_car_menu_release, 
  332. 	on_post_show		= cdl01_car_menu_finalize, 
  333. 	get_width			= cdl01_car_menu_get_width, 
  334. 	 
  335. 	footer_height 		= 83, 
  336. } 
  337.  
  338. Cdl01_color_menu = { 
  339. 	header_label_str	= "NONE", 
  340. 	on_show				= cdl01_color_menu_show, 
  341. 	on_back				= cdl01_color_menu_back, 
  342. 	on_release			= cdl01_color_menu_release, 
  343. 	on_post_show		= cdl01_color_menu_finalize, 
  344.  
  345. 	footer_height		= 40, 
  346. 	num_items			= 1, 
  347.  
  348. 	[0] = { label = "", type = MENU_ITEM_TYPE_GRID, on_select = cdl01_color_menu_select, on_nav = cdl01_color_menu_nav, update_swatch = cdl01_color_swatch_update, 
  349. 		row_height = MENU_SWATCH_DEFAULT_ROW_HEIGHT, num_cols = 5, col_width = MENU_SWATCH_DEFAULT_COL_WIDTH, highlight_scale = 1, unhighlight_scale = 0.8}, 
  350. } 
  351.