Guest User

Untitled

a guest
Jun 12th, 2020
2,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.31 KB | None | 0 0
  1. // UIColorPicker.js
  2. // Version: 0.1.0
  3. // Event: Initialized Event
  4. // Description: Choose a color from a color palette and apply it to a Mesh Visual
  5. //
  6. // ----- USAGE -----
  7. // Attach this script to a Scene Object with a Screen Transform Component.
  8. // Assign a Screen Image Object to the "Background Object" parameter.
  9. // Assign a Screen Image Object parented to the "Background Object" to the "Palette Object" parameter.
  10. // Assign a Scene Object with a UIButton Script Component and parented to the "Palette Object" to the "Button Object" parameter.
  11. //
  12. // ----- LOCAL API USAGE -----
  13. // Valid Event Types: "onEnableInteractable", "onDisableInteractable", "onColorChanged", "onSliderValueChanged"
  14. //
  15. // Manually enable interactable
  16. // script.api.enableInteractable()
  17. //
  18. // Manually disable interactable
  19. // script.api.disableInteractable()
  20. //
  21. // Add callback function to event
  22. // script.api.addCallback(eventType, callback)
  23. //
  24. // Remove callback function from event
  25. // script.api.removeCallback(eventType, callback)
  26. //
  27. // True if interactable
  28. // script.api.isInteractable()
  29. //
  30. // Enable touch events
  31. // script.api.enableTouchEvents()
  32. //
  33. // Disable touch events
  34. // script.api.disableTouchEvents()
  35. //
  36. // Get the current color selected on this Color Picker
  37. // script.api.getColor()
  38. //
  39. // Get the current value (0-1) of this Color Picker's slider
  40. // script.api.getSliderValue()
  41. //
  42. // Manually set the value (0-1) of this Color Picker's slider
  43. // script.api.setSliderValue(value)
  44. // -----------------
  45.  
  46. //@input bool interactable = true
  47. //@ui {"widget":"separator"}
  48. //@input Component.MeshVisual colorRecipient
  49.  
  50. //@ui {"widget":"separator"}
  51. //@input Component.MeshVisual alphaRecipient
  52.  
  53. //@ui {"widget":"separator"}
  54. //@input bool editProperties = false
  55. //@ui {"widget":"group_start", "label":"Properties", "showIf":"editProperties"}
  56. //@input int paletteType = 0 {"widget":"combobox", "values":[{"label":"Full Palette", "value": 0}, {"label":"2-Color Gradient", "value": 1}]}
  57.  
  58. //@ui {"widget":"group_start", "label":"2-Color Properties", "showIf":"paletteType", "showIfValue": 1}
  59. //@input vec4 colorTop = {1.0, 1.0, 1.0, 1.0} {"widget":"color"}
  60. //@input vec4 colorBottom = {0.0, 0.0, 0.0, 1.0} {"widget":"color"}
  61. //@ui {"widget":"group_end"}
  62.  
  63. //@input float initialYPosition = 0.5 {"widget":"slider", "min": 0.0, "max": 1.0, "step": 0.01, "label":"Initial Position"}
  64.  
  65. //@ui {"widget":"group_end"}
  66.  
  67. //@ui {"widget":"separator"}
  68. //@input bool editEventCallbacks = false
  69. //@ui {"widget":"group_start", "label":"Event Callbacks", "showIf":"editEventCallbacks"}
  70. //@input int callbackType = 0 {"widget":"combobox", "values":[{"label":"None", "value":0}, {"label":"Behavior Script", "value": 1}, {"label":"Behavior Custom Trigger", "value":2}, {"label":"Custom Function", "value":3}]}
  71.  
  72. //@input Component.ScriptComponent[] onColorChangedBehaviors {"label":"On Color Changed", "showIf":"callbackType", "showIfValue":1}
  73. //@input Component.ScriptComponent[] onSliderValueChangedBehaviors {"label":"On Value Changed", "showIf":"callbackType", "showIfValue":1}
  74.  
  75. //@input string[] onColorChangedGlobalBehaviors {"label":"On Color Changed", "showIf":"callbackType", "showIfValue":2}
  76. //@input string[] onSliderValueChangedGlobalBehaviors {"label":"On Value Changed", "showIf":"callbackType", "showIfValue":2}
  77.  
  78. //@input Component.ScriptComponent customFunctionScript {"showIf":"callbackType", "showIfValue":3}
  79. //@input string[] onColorChangedFunctionNames {"label":"On Color Changed", "showIf":"callbackType", "showIfValue":3}
  80. //@input string[] onSliderValueChangedFunctionNames {"label":"On Value Changed", "showIf":"callbackType", "showIfValue":3}
  81. //@ui {"widget":"group_end"}
  82.  
  83. //@ui {"widget":"separator"}
  84. //@input bool editAdvancedOptions = false
  85. //@ui {"widget":"group_start", "label":"Advanced Options", "showIf":"editAdvancedOptions"}
  86. //@input bool printDebugStatements = false
  87. //@input bool printWarningStatements = true
  88. //@input bool disableTouchEvents = false
  89. //@input bool editConnections = false
  90. //@ui {"widget":"group_start", "label":"Connections", "showIf":"editConnections"}
  91. //@input SceneObject backgroundObject
  92. //@input SceneObject paletteObject
  93. //@input SceneObject buttonObject
  94. //@input SceneObject colorVisualObject
  95. //@input SceneObject popupObject
  96. //@ui {"widget":"group_end"}
  97. //@ui {"widget":"group_end"}
  98.  
  99. var callbackTracker = new global.CallbackTracker(script);
  100.  
  101. // Local API
  102. script.api.enableInteractable = enableInteractable;
  103. script.api.disableInteractable = disableInteractable;
  104. script.api.isInteractable = isInteractable;
  105. script.api.enableTouchEvents = enableTouchEvents;
  106. script.api.disableTouchEvents = disableTouchEvents;
  107. script.api.getColor = getColor;
  108. script.api.getSliderValue = getSliderValue;
  109. script.api.setSliderValue = setSliderValue;
  110. script.api.initialized = false;
  111. script.api.widgetType = global.WidgetTypes.UIColorPicker;
  112. script.api.acceptChildWidget = acceptChildWidget;
  113.  
  114. script.api.addCallback = callbackTracker.addCallback.bind(callbackTracker);
  115. script.api.removeCallback = callbackTracker.removeCallback.bind(callbackTracker);
  116.  
  117. // Touch Event callbacks
  118. script.api.onTouchStart = onTouchStart;
  119. script.api.onTouchEnd = onTouchEnd;
  120. script.api.onTouchMove = onTouchMove;
  121.  
  122. script.api.allowTouchEvents = !script.disableTouchEvents;
  123.  
  124. script.api.setOwner = setOwner;
  125. script.api.notifyOnInitialize = notifyOnInitialize;
  126.  
  127. script.api.claimTouchStart = claimTouchStart;
  128. script.api.getMainRenderOrder = getMainRenderOrder;
  129.  
  130. // Is this widget interactable?
  131. var interactable = script.interactable;
  132.  
  133. // 2D Slider properties
  134. var minValueX = 0.0;
  135. var maxValueX = 1.0;
  136. var stepValueX = 0.001;
  137. var initialValueX = 0.5;
  138.  
  139. var minValueY = 0.0;
  140. var maxValueY = 1.0;
  141. var stepValueY = 0.001;
  142. var initialValueY = script.initialYPosition;
  143.  
  144. var minEndpointX = 0.0;
  145. var maxEndpointX = 1.0;
  146. var minEndpointY = 0.0;
  147. var maxEndpointY = 1.0;
  148.  
  149. // Relevant Components
  150. var paletteScreenTransform = null;
  151. var paletteImage = null;
  152. var thisScreenTransform = null;
  153. var backgroundScreenTransform = null;
  154. var backgroundImage = null;
  155. var buttonScript = null;
  156. var buttonScreenTransform = null;
  157. var colorVisualImage = null;
  158. var popupWidget = null;
  159.  
  160. // Color Picker properties
  161. var currentColor = new vec4(0.0, 0.0, 0.0, 0.0);
  162. var currentValue = 0;
  163. var localScreenPos = new vec2(0.0, 0.0);
  164. var cursorIsSlideable = false;
  165.  
  166. var sceneObject = script.getSceneObject();
  167.  
  168. var refreshHelper = new global.RefreshHelper(initParams);
  169.  
  170. function refresh() {
  171. refreshHelper.requestRefresh();
  172. }
  173.  
  174. refresh();
  175.  
  176. function claimTouchStart(touchPosition) {
  177. return (backgroundScreenTransform && backgroundScreenTransform.containsScreenPoint(touchPosition))
  178. ? global.TouchClaimTypes.Claim
  179. : global.TouchClaimTypes.Reject;
  180. }
  181.  
  182. function getMainRenderOrder() {
  183. return backgroundImage ? backgroundImage.getRenderOrder() : null;
  184. }
  185.  
  186. function acceptChildWidget(widget) {
  187. var api = widget.api;
  188. if (acceptPopupWidget(widget)) {
  189. return true;
  190. } else if (!widget.api.ownerScript && api.widgetType >= 0) {
  191. global.politeCall(widget, "setOwner", [script]);
  192. refresh();
  193. return true;
  194. }
  195. return false;
  196. }
  197.  
  198. function notifyOnInitialize(callback) {
  199. callback(script);
  200. }
  201.  
  202. // Initialize all parameters
  203. function initParams() {
  204. if (script.api.initialized) {
  205. return;
  206. }
  207.  
  208. if (!initColorPicker() ||
  209. !initBackground() ||
  210. !initPalette() ||
  211. !initButton() ||
  212. !initColorVisual() ||
  213. !initLocalScreenPos() ||
  214. !initInteractable() ||
  215. !initPopup()
  216. ) {
  217. return;
  218. }
  219.  
  220. global.answerPoliteCalls(script, "notifyOnInitialize");
  221. checkOwner();
  222.  
  223. script.api.initialized = true;
  224. }
  225.  
  226. function seekOwner() {
  227. global.findScriptUpwards(sceneObject, "acceptChildWidget", function(scr) {
  228. return scr.api.acceptChildWidget(script);
  229. });
  230. }
  231.  
  232. function setOwner(ownerScript) {
  233. script.api.ownerScript = ownerScript;
  234. refresh();
  235. }
  236.  
  237. function checkOwner() {
  238. if (!script.api.ownerScript) {
  239. seekOwner();
  240. }
  241. return !!script.api.ownerScript;
  242. }
  243.  
  244. // Initialize Color Picker parameters
  245. function initColorPicker() {
  246. thisScreenTransform = safeGetComponent(sceneObject, "Component.ScreenTransform");
  247. if (!thisScreenTransform) {
  248. printWarning("please assign a Screen Transform component to this Scene Object!");
  249. return false;
  250. }
  251.  
  252. return true;
  253. }
  254.  
  255. // Initialize Palette parameters
  256. function initPalette() {
  257. if (!script.paletteObject) {
  258. printWarning("no Reference to Palette Scene Object! Attempting to search children...");
  259. script.paletteObject = global.getChildByName(sceneObject, "Palette");
  260. if (!script.paletteObject) {
  261. printWarning("the Palette Scene Object has not been assigned! Please go to \"Advanced Options\" and reassign it under \"Edit Connections\"!");
  262. return false;
  263. }
  264. }
  265.  
  266. // Obtain Screen Transform Component from the palette
  267. paletteScreenTransform = safeGetComponent(script.paletteObject, "Component.ScreenTransform");
  268. if (!paletteScreenTransform) {
  269. printWarning("missing a Screen Transform Component!");
  270. return false;
  271. }
  272.  
  273. // Obtain Image Component from the palette
  274. paletteImage = safeGetComponent(script.paletteObject, "Component.Image");
  275. if (!paletteImage) {
  276. printWarning("missing an Image Component!");
  277. return false;
  278. }
  279.  
  280. minEndpointX = 0.5;
  281. maxEndpointX = 0.5;
  282.  
  283. return true;
  284. }
  285.  
  286. // Initialize Background parameters
  287. function initBackground() {
  288. if (!script.backgroundObject) {
  289. printWarning("no Reference to Background Scene Object! Attempting to search children...");
  290. script.backgroundObject = global.getChildByName(sceneObject, "Background");
  291. if (!script.backgroundObject) {
  292. printWarning("the Background Scene Object has not been assigned! Please go to \"Advanced Options\" and reassign it under \"Edit Connections\"!");
  293. return false;
  294. }
  295. }
  296.  
  297. // Obtain Screen Transform Component from the background
  298. backgroundScreenTransform = safeGetComponent(script.backgroundObject, "Component.ScreenTransform");
  299. if (!backgroundScreenTransform) {
  300. printWarning("missing a Screen Transform Component!");
  301. return false;
  302. }
  303.  
  304. // Obtain Image Component from the background
  305. backgroundImage = safeGetComponent(script.backgroundObject, "Component.Image");
  306. if (!backgroundImage) {
  307. printWarning("missing an Image Component!");
  308. return false;
  309. }
  310.  
  311. return true;
  312. }
  313.  
  314. function acceptButtonWidget(widget) {
  315. var api = widget.api;
  316. if (!buttonScript && !api.ownerScript && api.widgetType == global.WidgetTypes.UIButton) {
  317. global.politeCall(widget, "setOwner", [script]);
  318. buttonScript = widget;
  319. updateWidgetInteractable(buttonScript);
  320. refresh();
  321. return true;
  322. }
  323. return false;
  324. }
  325.  
  326. // Initialize Button parameters
  327. function initButton() {
  328. // Obtain the Script Component of the Button that this Color Picker controls
  329. if (!script.buttonObject) {
  330. printWarning("no Reference to Button Scene Object! Attempting to search children...");
  331. script.buttonObject = global.getChildByName(sceneObject, "Cursor");
  332. if (!script.buttonObject) {
  333. printWarning("the Button Scene Object has not been assigned! Please go to \"Advanced Options\" and reassign it under \"Edit Connections\"!");
  334. return false;
  335. }
  336. }
  337.  
  338. if (!buttonScript) {
  339. global.findScript(script.buttonObject, null, function(scr) {
  340. global.politeCall(scr, "notifyOnInitialize", [acceptButtonWidget]);
  341. });
  342. }
  343.  
  344. if (!buttonScript) {
  345. return false;
  346. }
  347.  
  348. // Obtain Screen Transform Component from button
  349. if (!buttonScreenTransform) {
  350. buttonScreenTransform = safeGetComponent(script.buttonObject, "Component.ScreenTransform");
  351. if (!buttonScreenTransform) {
  352. printWarning("assigned a Button Scene Object that is missing a Screen Transform Component!");
  353. return false;
  354. }
  355. }
  356. return true;
  357. }
  358.  
  359. function acceptPopupWidget(widget) {
  360. var api = widget.api;
  361. if (!popupWidget && !api.ownerScript && api.widgetType == global.WidgetTypes.UIPopup) {
  362. global.politeCall(widget, "setOwner", [script]);
  363. popupWidget = widget;
  364.  
  365. script.api.addCallback("onPaletteTouchStart", function() {
  366. popupWidget.api.scaleUp();
  367. });
  368.  
  369. script.api.addCallback("onTouchEnd", function() {
  370. popupWidget.api.scaleDown();
  371. });
  372.  
  373. script.api.addCallback("onColorChanged", function(newColor) {
  374. popupWidget.api.setColor(newColor);
  375. popupWidget.api.movePopup(buttonScreenTransform);
  376. });
  377.  
  378. popupWidget.api.initPopupState(buttonScreenTransform);
  379. popupWidget.api.scaleDown(buttonScreenTransform);
  380.  
  381. refresh();
  382. return true;
  383. }
  384.  
  385. return false;
  386. }
  387.  
  388.  
  389. function initPopup() {
  390. // Obtain the Script Component of the Popup that this Color Picker controls
  391. if (!script.popupObject) {
  392. script.popupObject = global.getChildByName(sceneObject, "UI Popup");
  393. }
  394.  
  395. if (!popupWidget && script.popupObject) {
  396. global.findScript(script.popupObject, null, function(scr) {
  397. global.politeCall(scr, "notifyOnInitialize", [acceptPopupWidget]);
  398. });
  399. }
  400. return true;
  401. }
  402.  
  403. // Initialize Color Visual parameters
  404. function initColorVisual() {
  405. if (!script.colorVisualObject) {
  406. printWarning("no Reference to Color Visual Scene Object! Attempting to search children...");
  407. script.colorVisualObject = global.getChildByName(sceneObject, "Color Visual");
  408. if (!script.colorVisualObject) {
  409. printWarning("the Color Visual Scene Object has not been assigned! If you would like the currently selected color to be displayed on an Image, please go to \"Advanced Options\" and reassign it under \"Edit Connections\"!");
  410. return true;
  411. }
  412. }
  413.  
  414. // Obtain Color Visual Image Component
  415. if (!colorVisualImage) {
  416. colorVisualImage = safeGetComponent(script.colorVisualObject, "Component.Image");
  417. if (!colorVisualImage) {
  418. printWarning("assigned a Color Visual Scene Object that is missing an Image Component!");
  419. return false;
  420. }
  421. }
  422.  
  423. return true;
  424. }
  425.  
  426.  
  427. // Initialize this interactable
  428. function initInteractable() {
  429. updateWidgetInteractable(buttonScript);
  430. // Initialize the Color
  431. updateColor();
  432. return true;
  433. }
  434.  
  435. // Initialize Color Picker's initial location
  436. function initLocalScreenPos() {
  437. setLocationOnPalette(new vec2(initialValueX, initialValueY));
  438. return true;
  439. }
  440.  
  441. // Disable touch event
  442. function disableTouchEvents() {
  443. script.api.allowTouchEvents = false;
  444. script.disableTouchEvents = true;
  445. }
  446.  
  447. // Enable touch event
  448. function enableTouchEvents() {
  449. script.api.allowTouchEvents = true;
  450. script.disableTouchEvents = false;
  451. }
  452.  
  453. // Called On Touch Start
  454. function onTouchStart(eventData) {
  455. if (!interactable) {
  456. return;
  457. }
  458.  
  459. var touchPos = eventData.getTouchPosition();
  460. if (backgroundScreenTransform.containsScreenPoint(touchPos)) {
  461. updatePickedLocationFromTouch(eventData);
  462. cursorIsSlideable = true;
  463. callbackTracker.invokeScriptedCallbacks("onPaletteTouchStart", eventData);
  464. }
  465.  
  466. callbackTracker.invokeScriptedCallbacks("onTouchStart", eventData);
  467. }
  468.  
  469. // Called On Touch End
  470. function onTouchEnd(eventData) {
  471. if (!interactable) {
  472. return;
  473. }
  474. touchEndPicker(eventData);
  475. callbackTracker.invokeScriptedCallbacks("onTouchEnd", eventData);
  476. }
  477.  
  478. // Apply appropriate action based on whatever was touched on the Color Picker
  479. function touchEndPicker(eventData) {
  480. cursorIsSlideable = false;
  481. }
  482.  
  483. // Called On Touch Move
  484. function onTouchMove(eventData) {
  485. if (!interactable) {
  486. return;
  487. }
  488. touchMovePicker(eventData);
  489. callbackTracker.invokeScriptedCallbacks("onTouchMove", eventData);
  490. }
  491.  
  492. // Apply appropriate action based on whatever was touched on the Color Picker
  493. function touchMovePicker(eventData) {
  494. if (cursorIsSlideable) {
  495. updatePickedLocationFromTouch(eventData);
  496. }
  497. }
  498.  
  499. // Get current color
  500. function getColor() {
  501. return new vec4(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
  502. }
  503.  
  504. function getSliderValue() {
  505. return currentValue;
  506. }
  507.  
  508. // Update the color based on where the button is on the texture, and invoke callbacks
  509. function updateColor() {
  510. if (localScreenPos != null) {
  511. currentValue = getLocationOnPalette();
  512.  
  513. // Full Palette
  514. if (script.paletteType == 0) {
  515. var hsx = currentValue;
  516. var hue = hsx * 360.0;
  517. var saturation = 1.0;
  518. var value = 1.0;
  519.  
  520. if (hsx < 10.0 / 260.0) {
  521. value = hsx / (10.0 / 260.0);
  522. } else {
  523. value = 1.0;
  524. }
  525.  
  526. if (hsx > 250.0 / 260.0) {
  527. saturation = (hsx - 1.0) / (-10.0 / 260.0);
  528. }
  529.  
  530. var RGBAColor = HSVAtoRGBA(new vec4(hue, saturation, value, 1.0));
  531.  
  532. currentColor = RGBAColor;
  533. } else if (script.paletteType == 1) { // 2-Color Palette
  534. currentColor = vec4.lerp(script.colorBottom, script.colorTop, currentValue);
  535. }
  536.  
  537. // Update any Color Visual targets with new color
  538. updateColorVisual();
  539. updateAlphaVisual();
  540.  
  541. // Invoke callbacks
  542. callbackTracker.invokeAllCallbacks("onColorChanged", currentColor);
  543. callbackTracker.invokeAllCallbacks("onSliderValueChanged", currentValue);
  544. }
  545. }
  546.  
  547. // Update palette touch position
  548. function updatePickedLocationFromTouch(eventData) {
  549. // Convert Palette data in screen space
  550. var newButtonLocation = paletteScreenTransform.screenPointToLocalPoint(eventData.getTouchPosition());
  551. newButtonLocation = newButtonLocation.uniformScale(0.5).add(new vec2(.5, .5));
  552.  
  553. var newStepValueX = stepValueX / (maxValueX - minValueX);
  554. var newStepValueY = stepValueY / (maxValueY - minValueY);
  555.  
  556. // Apply step value
  557. newButtonLocation.x = Math.round(newButtonLocation.x / newStepValueX) * newStepValueX;
  558. newButtonLocation.y = Math.round(newButtonLocation.y / newStepValueY) * newStepValueY;
  559.  
  560. setLocationOnPalette(newButtonLocation);
  561. }
  562.  
  563. // Return currently picked location local to Palette
  564. function getLocationOnPalette() {
  565. return localScreenPos.y;
  566. }
  567.  
  568. // Set current location and automatically clamp to min and max
  569. function setLocationOnPalette(proposedLocalScreenPos) {
  570. if (!interactable) {
  571. return;
  572. }
  573.  
  574. // Clamp to the min and max local space values
  575. var proposedLocalScreenPosX = Math.min(Math.max(proposedLocalScreenPos.x, minEndpointX), maxEndpointX);
  576. var proposedLocalScreenPosY = Math.min(Math.max(proposedLocalScreenPos.y, minEndpointY), maxEndpointY);
  577. var clampedLocalScreenPos = new vec2(proposedLocalScreenPosX, proposedLocalScreenPosY);
  578.  
  579. if (localScreenPos.distance(clampedLocalScreenPos) > 0.00000000000001) {
  580. // Update local screen position
  581. localScreenPos = clampedLocalScreenPos;
  582. updateButtonScreenAnchors();
  583. updateColor();
  584. }
  585. }
  586.  
  587. function setSliderValue(value) {
  588. setLocationOnPalette(new vec2(localScreenPos.x, value));
  589. }
  590.  
  591. // Update button anchors based on current location on palette
  592. function updateButtonScreenAnchors() {
  593. if (buttonScreenTransform) {
  594. var currentLocation = new vec2(localScreenPos.x, localScreenPos.y);
  595.  
  596. // Map the location to min and max endpoints
  597. currentLocation.x = 2.0 * currentLocation.x - 1.0;
  598. currentLocation.y = 2.0 * currentLocation.y - 1.0;
  599.  
  600. var currentLocationScreen = paletteScreenTransform.localPointToScreenPoint(currentLocation);
  601. var buttonParentPoint = buttonScreenTransform.screenPointToParentPoint(currentLocationScreen);
  602.  
  603. buttonScreenTransform.anchors.setCenter(buttonParentPoint);
  604. }
  605. }
  606.  
  607. // Return true if Color Picker is currently interactable, false otherwise
  608. function isInteractable() {
  609. return interactable;
  610. }
  611.  
  612. function updateWidgetInteractable(widget) {
  613. if (widget) {
  614. if (interactable) {
  615. widget.api.enableInteractable();
  616. } else {
  617. widget.api.disableInteractable();
  618. }
  619. }
  620. }
  621.  
  622. // Disable this Color Picker
  623. function disableInteractable() {
  624. if (!interactable) {
  625. return;
  626. }
  627. interactable = false;
  628.  
  629. updateWidgetInteractable(buttonScript);
  630. callbackTracker.invokeAllCallbacks("onDisableInteractable");
  631.  
  632. printDebug("Disabled!");
  633. }
  634.  
  635. // Enable this Color Picker
  636. function enableInteractable() {
  637. if (interactable) {
  638. return;
  639. }
  640. interactable = true;
  641.  
  642. updateWidgetInteractable(buttonScript);
  643. updateColor();
  644. callbackTracker.invokeAllCallbacks("onEnableInteractable");
  645.  
  646. printDebug("Enabled!");
  647. }
  648.  
  649. // Update color visuals based on the selected color
  650. function updateColorVisual() {
  651. if (colorVisualImage) {
  652. colorVisualImage.mainPass.baseColor = currentColor;
  653. }
  654.  
  655. if (script.colorRecipient) {
  656. script.colorRecipient.mainPass.baseColor = currentColor;
  657. }
  658. }
  659.  
  660. // Update color visuals based on the selected alpha
  661. function updateAlphaVisual() {
  662. if (script.alphaRecipient) {
  663. const baseColor = script.alphaRecipient.mainPass.baseColor;
  664. script.alphaRecipient.mainPass.baseColor = new vec4(baseColor.r, baseColor.g, baseColor.b, currentColor.r);
  665. }
  666. }
  667.  
  668. // Convert HSV to RGB color space
  669. function HSVAtoRGBA(hsva) {
  670. var h = hsva.r;
  671. var s = hsva.g;
  672. var v = hsva.b;
  673. var a = hsva.a;
  674. var r, g, b;
  675.  
  676. var hprime = h / 60;
  677.  
  678. const c = v * s;
  679. const x = c * (1 - Math.abs((hprime % 2) - 1));
  680. const m = v - c;
  681.  
  682. if (!hprime) {
  683. r = 0;
  684. g = 0;
  685. b = 0;
  686. }
  687.  
  688. if (hprime >= 0 && hprime < 1) {
  689. r = c;
  690. g = x;
  691. b = 0;
  692. }
  693.  
  694. if (hprime >= 1 && hprime < 2) {
  695. r = x;
  696. g = c;
  697. b = 0;
  698. }
  699.  
  700. if (hprime >= 2 && hprime < 3) {
  701. r = 0;
  702. g = c;
  703. b = x;
  704. }
  705.  
  706. if (hprime >= 3 && hprime < 4) {
  707. r = 0;
  708. g = x;
  709. b = c;
  710. }
  711.  
  712. if (hprime >= 4 && hprime < 5) {
  713. r = x;
  714. g = 0;
  715. b = c;
  716. }
  717.  
  718. if (hprime >= 5 && hprime <= 6) {
  719. r = c;
  720. g = 0;
  721. b = x;
  722. }
  723.  
  724. r = Math.round((r + m) * 255);
  725. g = Math.round((g + m) * 255);
  726. b = Math.round((b + m) * 255);
  727.  
  728. return new vec4(r / 255.0, g / 255.0, b / 255.0, a);
  729. }
  730.  
  731. // Print debug messages
  732. function printDebug(message) {
  733. if (script.printDebugStatements) {
  734. print("UIColorPicker " + sceneObject.name + " - " + message);
  735. }
  736. }
  737.  
  738. // Print warning message
  739. function printWarning(message) {
  740. if (script.printWarningStatements) {
  741. print("UIColorPicker " + sceneObject.name+ " - WARNING, " + message);
  742. }
  743. }
  744.  
  745. function safeGetComponent(obj, componentType) {
  746. return (obj.getComponentCount(componentType) > 0) ? obj.getFirstComponent(componentType) : null;
  747. }
Add Comment
Please, Sign In to add comment