Advertisement
Nicklaj

Untitled

Dec 1st, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. import JSON;
  2. import skse;
  3. import AchievementUtils;
  4. import Components.CrossPlatformButtons;
  5.  
  6. class AchievementMenu extends MovieClip {
  7.  
  8. /* stage elements */
  9. public var heading_tf:TextField;
  10. public var progress_tf:TextField;
  11. public var AchievementGroups_mc:MovieClip;
  12. public var AchievementList_mc:MovieClip;
  13. public var BottomBar_mc:MovieClip;
  14.  
  15. private var data:Object;
  16. private var isFocusOnGroups:Boolean = true;
  17. private var focusIndex:Number = -1;
  18. private var mouseListener:Object;
  19. private var currentGroupIndex:Number = 0;
  20.  
  21. function onLoad(): Void {
  22. BottomBar_mc = _root.BottomBar_mc;
  23. Key.addListener(this);
  24. setupMouseListener();
  25. Selection["alwaysEnableArrowKeys"] = false;
  26. heading_tf.noTranslate = progress_tf.noTranslate = true;
  27.  
  28. // var fetcher:LoadVars = new LoadVars();
  29. // var _this = this;
  30. // fetcher.onData = function( theData ) {
  31. // if ( theData !== undefined && theData !== '' ) {
  32. // _this.setData(theData);
  33. // }
  34. // }
  35. // fetcher.load( '../../../flash/Test.txt' );
  36. // trace("test");
  37. }
  38.  
  39. function setupMouseListener() {
  40. mouseListener = new Object();
  41. var _this = this;
  42. mouseListener.onMouseWheel = function( delta ) {
  43. _this.onMouseWheelCallback( delta );
  44. }
  45. Mouse.addListener(mouseListener);
  46. }
  47.  
  48. function onMouseWheelCallback( delta ) {
  49. if ( isFocusOnGroups ) {
  50. AchievementGroups_mc.onMouseWheelCallback(delta);
  51. } else {
  52. AchievementList_mc.onMouseWheelCallback(delta);
  53. }
  54. }
  55.  
  56. function InitExtensions(): Void {
  57. skse.SendModEvent( 'AchievementMenu_Init', '', 0, 0 ); /* signal we're ready to load */
  58. }
  59.  
  60. function SetPlatform(aiPlatformIndex: Number, abPS3Switch: Boolean): Void {
  61. isFocusOnGroups = aiPlatformIndex != 0 ? true : false;
  62. var BackButtonInstance:CrossPlatformButtons = BottomBar_mc.ButtonRect.BackButtonInstance;
  63. BackButtonInstance.SetPlatform(aiPlatformIndex, abPS3Switch);
  64. var UpDownInstance:CrossPlatformButtons = BottomBar_mc.ButtonRect.UpDownInstance;
  65. UpDownInstance.SetPlatform(aiPlatformIndex, abPS3Switch);
  66. var RightInstance:CrossPlatformButtons = BottomBar_mc.ButtonRect.RightInstance;
  67. RightInstance.SetPlatform(aiPlatformIndex, abPS3Switch);
  68. var LeftInstance:CrossPlatformButtons = BottomBar_mc.ButtonRect.LeftInstance;
  69. LeftInstance.SetPlatform(aiPlatformIndex, abPS3Switch);
  70. }
  71.  
  72. function render() {
  73. AchievementGroups_mc.showGroups(data);
  74. }
  75.  
  76. function loadGroup(index) {
  77. AchievementUtils.setText( heading_tf, data[ index ].groupName );
  78. AchievementList_mc.render( index );
  79. calculateProgress( index );
  80. }
  81.  
  82. function calculateProgress(groupIndex:Number) {
  83. var completed:Number = 0;
  84. var achievements = data[ groupIndex ].AchievementData;
  85. for ( var i = 0; i < achievements.length; i++ ) {
  86. if ( achievements[ i ].Unlocked === true ) {
  87. completed++;
  88. }
  89. }
  90. var percent = Math.ceil( ( completed / achievements.length ) * 100 ),
  91. progressText = AchievementUtils.get_i18n( '$ACH_PROGRESS' );
  92. progressText = AchievementUtils.str_replace( '<completed>', completed, progressText );
  93. progressText = AchievementUtils.str_replace( '<all>', achievements.length, progressText );
  94. progressText = AchievementUtils.str_replace( '<percent>', percent, progressText );
  95. AchievementUtils.setText( progress_tf, progressText );
  96. }
  97.  
  98. function getAchievements( index:Number ) {
  99. return data[ index ].AchievementData;
  100. }
  101.  
  102. function closeMenu() {
  103. skse.SendModEvent( 'AchievementMenu_Close', '', 0, 0 );
  104. }
  105.  
  106. function setFocusTo(nextIndex:Number) {
  107. if ( isFocusOnGroups ) {
  108. if ( AchievementGroups_mc.getClipIndex( nextIndex ) ) {
  109. AchievementGroups_mc.setFocusTo( nextIndex );
  110. focusIndex = nextIndex;
  111. }
  112. } else {
  113. if ( AchievementList_mc.getClipIndex( nextIndex ) ) {
  114. AchievementList_mc.setFocusTo( nextIndex );
  115. focusIndex = nextIndex;
  116. }
  117. }
  118. }
  119.  
  120. function onKeyDown() {
  121. if ( Key.getCode() === 9 ) {
  122. _parent.play();
  123. } else if ( Key.getCode() === 38 ) { // up
  124. setFocusTo( focusIndex - 1 )
  125. } else if ( Key.getCode() === 40 ) { // down
  126. setFocusTo( focusIndex + 1 )
  127. } else if ( Key.getCode() === 37 ) { // left
  128. if ( ! isFocusOnGroups ) {
  129. isFocusOnGroups = true;
  130. focusIndex = 0;
  131. AchievementGroups_mc.setFocusTo(0);
  132. }
  133. } else if ( Key.getCode() === 39 ) { // right
  134. if ( isFocusOnGroups ) {
  135. isFocusOnGroups = false;
  136. focusIndex = 0;
  137. AchievementList_mc.setFocusTo(0);
  138. }
  139. } else if ( Key.getCode() === 13 ) { // Activate
  140. var mc;
  141. if ( isFocusOnGroups && ( mc = AchievementGroups_mc.getClipIndex( focusIndex ) ) ) {
  142. mc.onRelease();
  143. }
  144. }
  145. }
  146.  
  147. // @api
  148. function setData( dataString:String ) {
  149. // trace("test2");
  150. var decodedData:Object = [];
  151.  
  152. // Split the input into lines
  153. var lines:Array = dataString.split("\n");
  154.  
  155. // Remove all \n
  156. for (var i:Number = 0; i < lines.length; i++) {
  157. lines[i] = lines[i].substr(0, lines[i].length - 1);
  158. }
  159.  
  160. var groupCount:Number = parseInt(lines[0]); // First line gives us the number of groups
  161. var groups:Array = []; // Temporary array to store group data
  162.  
  163. // Process group data
  164. for (var i = 1; i <= groupCount; i++) {
  165. var groupLine:Array = lines[i].split("||");
  166. if (groupLine.length < 3) continue; // Skip invalid lines
  167.  
  168. var groupId:String = groupLine[0];
  169. groups.push({
  170. groupId: groupId,
  171. groupName: groupLine[1],
  172. image: groupLine[2],
  173. AchievementData: [] // Prepare an empty array for achievements
  174. });
  175. }
  176.  
  177. // Process achievements
  178. for (var j:Number = groupCount + 1; j < lines.length; j++) {
  179. var achievementLine:Array = lines[j].split("||");
  180. if (achievementLine.length < 5) continue; // Skip invalid lines
  181.  
  182. var groupId:String = achievementLine[0]; // Group ID this achievement belongs to
  183. var achievement:Object = {
  184. AchievementName: achievementLine[1],
  185. Description: achievementLine[2],
  186. Icon: achievementLine[4],
  187. UnlockDatetime: parseInt(achievementLine[3]),
  188. Unlocked: parseInt(achievementLine[3]) > 0 // Unlocked if timestamp > 0
  189. };
  190.  
  191. // Find the correct group and add this achievement
  192. for (var k:Number = 0; k < groups.length; k++) {
  193. if (groups[k].groupId == groupId) {
  194. groups[k].AchievementData.push(achievement);
  195. break;
  196. }
  197. }
  198. }
  199.  
  200. // Finalize the output structure
  201. for (var l:Number = 0; l < groups.length; l++) {
  202. decodedData.push({
  203. groupName: groups[l].groupName,
  204. image: groups[l].image,
  205. AchievementData: groups[l].AchievementData
  206. });
  207. }
  208.  
  209. data = decodedData;
  210. render();
  211. _parent.play();
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement