Advertisement
aliasedfrog

Reflex ZOOM.lua

Apr 7th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. require "base/internal/ui/reflexcore"
  2.  
  3. --All credit for this to Reflex dev team, from OptionsMenu.lua
  4. local function uiKeyBind(bindCommand, x, y, w, bindState, optionalId, enabled)
  5. local h = 28;
  6.  
  7. local c = 255;
  8. local k = nil;
  9. if enabled == false then
  10. c = UI_DISABLED_TEXT;
  11. else
  12. k = inputGrabRegion(x, y, w, h, optionalId);
  13. end
  14.  
  15. nvgSave();
  16.  
  17. -- Edit
  18. nvgBeginPath();
  19. nvgRoundedRect(x+1,y+1, w-2,h-2, 4-1);
  20. nvgFillBoxGradient(x+1, y+1+1.5, w-2, h-2, 3,4, Color(c,c,c,32), Color(32,32,32,32));
  21. nvgFill();
  22.  
  23. local key = bindReverseLookup(bindCommand);
  24. if key == "(unbound)" then
  25. c = c / 2;
  26. else
  27. key = string.upper(key);
  28. end
  29.  
  30. -- default border colour
  31. local bc = Color(0,0,0,48);
  32.  
  33. -- modify when hovering
  34. bc.r = lerp(bc.r, UI_HOVER_BORDER_COLOR.r, k.hoverAmount);
  35. bc.g = lerp(bc.g, UI_HOVER_BORDER_COLOR.g, k.hoverAmount);
  36. bc.b = lerp(bc.b, UI_HOVER_BORDER_COLOR.b, k.hoverAmount);
  37. bc.a = lerp(bc.a, UI_HOVER_BORDER_COLOR.a, k.hoverAmount);
  38.  
  39. -- modify when have focus
  40. if k.focus then
  41. local intensity = k.focusAmount;
  42.  
  43. -- pulse
  44. intensity = intensity * (math.sin(OptionsMenu.keyBindPulseTimer) * 0.5 + 0.5);
  45. OptionsMenu.keyBindPulseTimer = OptionsMenu.keyBindPulseTimer + deltaTime * 16;
  46.  
  47. bc.r = lerp(bc.r, 255, intensity);
  48. bc.g = lerp(bc.g, 255, intensity);
  49. bc.b = lerp(bc.b, 0, intensity);
  50. end
  51.  
  52. -- border
  53. nvgBeginPath();
  54. nvgRoundedRect(x+0.5,y+0.5, w-1,h-1, 4-0.5);
  55. nvgStrokeColor(bc);
  56. nvgStroke();
  57.  
  58. local tw = nvgTextWidth(key);
  59. if tw >= w - 5 then
  60. nvgScissor(x, y, w - 5, 100);
  61. end
  62.  
  63. -- text
  64. nvgFontSize(20);
  65. nvgFontFace(FONT_TEXT);
  66. nvgFillColor(Color(c,c,c,64));
  67. nvgTextAlign(NVG_ALIGN_LEFT, NVG_ALIGN_MIDDLE);
  68. nvgText(x+h*0.3, y+h*0.5, key);
  69.  
  70. nvgRestore();
  71.  
  72. if k.nameKeyPressed ~= nil then
  73. if key ~= "(unbound)" then
  74. --consolePrint("unbind "..key);
  75. --Removed for destroying all binds on the key it's changing. #AFRO
  76. --consolePerformCommand("unbind "..key);
  77.  
  78. --Adding this temp fix for now. #AFRO
  79. if bindState ~= nil then
  80. consolePerformCommand("bind "..bindState.." "..key.." \"\"");
  81. else
  82. consolePerformCommand("bind "..key.." \"\"");
  83. end
  84.  
  85. end
  86. if bindState ~= nil then
  87. consolePerformCommand("bind "..bindState.." "..k.nameKeyPressed.." "..bindCommand);
  88. else
  89. consolePerformCommand("bind "..k.nameKeyPressed.." "..bindCommand);
  90. end
  91. end
  92.  
  93. return k.nameKeyPressed;
  94. end
  95.  
  96.  
  97. Zoom =
  98. {
  99. canHide = false,
  100. canPosition = true;
  101. userData = {};
  102. transitionTimer = 0,
  103. transitionRun = false
  104. ,
  105. transition = function(self, direction)
  106. local transFov = direction == 0 and self.userData.zoomFov or self.userData.fov
  107. if self.transitionRun then
  108. -- using lerp to set r_fov over time for to create the transition.
  109. transFov = lerp(direction == 1 and self.userData.fov or self.userData.zoomFov
  110. , direction == 1 and self.userData.zoomFov or self.userData.fov
  111. , self.transitionTimer );
  112. self.transitionTimer = self.transitionTimer + (deltaTimeRaw / (self.userData.scaleSpeed));
  113. end
  114. --reseting everything if finished running transition.
  115. if self.transitionTimer >= 1 then
  116. self.transitionRun = false;
  117. self.transitionTimer = 0;
  118. self.zoomDirection = 0;
  119. end
  120. --return either transitioned Fov, or return your preset zoom fov / fov depending on direction.
  121. return self.transitionRun and "r_fov " .. transFov - transFov%1 or direction == 1 and "r_fov " .. self.userData.zoomFov or "r_fov " .. self.userData.fov ;
  122. end
  123. ,
  124. drawOptions = function (self, x , y)
  125. local padding = 10;
  126. local editboxH = 35;
  127.  
  128. --Fov
  129. uiLabel("fov", x, y);
  130. local newFov = uiEditBox(self.userData.fov, x + nvgTextWidth("transition speed") + padding, y, 150, optionalId, true)
  131.  
  132. -- Zoom fov
  133. y = y + editboxH + padding;
  134. uiLabel("zoom fov", x, y);
  135. local newZoomFov = uiEditBox(self.userData.zoomFov, x + nvgTextWidth("transition speed") + padding, y, 150, optionalId, true)
  136.  
  137. -- Key bind
  138. y = y + editboxH + padding;
  139. uiLabel("key bind", x, y);
  140. local newZoomKey = uiKeyBind("ui_zoom_zoom 1", x + nvgTextWidth("transition speed") + padding, y, 150, "game", nil, true);
  141.  
  142. --Transition Speed
  143. y = y + editboxH + padding;
  144. uiLabel("transition speed", x, y);
  145. self.userData.scaleSpeed = uiSlider(x + nvgTextWidth("transition speed") + padding, y, 150, 0, 0.5, self.userData.scaleSpeed);
  146. uiLabel(string.format("%2f", self.userData.scaleSpeed), x + nvgTextWidth("transition speed") + padding + 150 + padding, y);
  147.  
  148. if newZoomKey then self.userData.zoomKey = tostring(newZoomKey) end;
  149. if tonumber(newFov) > tonumber(self.userData.zoomFov) then self.userData.fov = tonumber(newFov); end;
  150. if tonumber(newZoomFov) < tonumber(self.userData.fov) then self.userData.zoomFov = tonumber(newZoomFov); end;
  151. saveUserData(self.userData);
  152. end
  153. };
  154. registerWidget("Zoom");
  155.  
  156.  
  157. function Zoom:initialize()
  158. self.userData = loadUserData();
  159. CheckSetDefaultValue(self, "userData", "table", {});
  160. CheckSetDefaultValue(self.userData, "zoom", "boolean", false);
  161. CheckSetDefaultValue(self.userData, "zoomKey", "string", "mouse2");
  162. CheckSetDefaultValue(self.userData, "zoomFov", "number", 50);
  163. CheckSetDefaultValue(self.userData, "fov", "number", 110);
  164. CheckSetDefaultValue(self.userData, "scaleSpeed", "number", 0.1);
  165.  
  166. widgetCreateConsoleVariable("zoom", "int", 0);
  167. end
  168.  
  169. function Zoom:draw()
  170. if widgetGetConsoleVariable("zoom") == 1 then
  171. --set zoom direction mod to zoom in.
  172. self.zoomDirection = 1;
  173. if consoleGetVariable("r_fov") ~= self.userData.zoomFov then self.transitionRun = true; end;
  174. consolePerformCommand("bind game " .. self.userData.zoomKey .. " ui_zoom_zoom 0");
  175. elseif widgetGetConsoleVariable("zoom") == 0 then
  176. --set zoom direction mod to zoom out.
  177. self.zoomDirection = -1;
  178. if consoleGetVariable("r_fov") == self.userData.fov then self.transitionRun = false;
  179. else self.transitionRun = true;
  180. end;
  181. consolePerformCommand("bind game " .. self.userData.zoomKey .. " ui_zoom_zoom 1");
  182. end
  183. if self.zoomDirection == 1 then consolePerformCommand(self.transition(self, self.zoomDirection)); end;
  184. if self.zoomDirection == -1 then consolePerformCommand(self.transition(self, self.zoomDirection)); end;
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement