Cond.hx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package hrt.shgraph.nodes;
  2. import hide.Element;
  3. using hxsl.Ast;
  4. @name("Condition")
  5. @description("Create a custom condition between two inputs")
  6. @group("Condition")
  7. class Cond extends ShaderNode {
  8. @input("left") var leftVar = SType.Variant;
  9. @input("right") var rightVar = SType.Variant;
  10. @output("boolean") var output = SType.Bool;
  11. @param() var condition : Binop;
  12. override public function checkValidityInput(key : String, type : ShaderType.SType) : Bool {
  13. if (key == "leftVar" && rightVar != null)
  14. return ShaderType.checkCompatibilities(type, ShaderType.getType(rightVar.getType()));
  15. if (key == "rightVar" && leftVar != null)
  16. return ShaderType.checkCompatibilities(type, ShaderType.getType(leftVar.getType()));
  17. return true;
  18. }
  19. override public function createOutputs() {
  20. addOutput("output", TBool);
  21. }
  22. override public function build(key : String) : TExpr {
  23. return {
  24. p : null,
  25. t : output.type,
  26. e : TBinop(OpAssign, {
  27. e: TVar(output),
  28. p: null,
  29. t: output.type
  30. }, {e: TBinop(this.condition,
  31. leftVar.getVar(),
  32. rightVar.getVar()),
  33. p: null, t: output.type })
  34. };
  35. }
  36. var availableConditions = [OpEq, OpNotEq, OpGt, OpGte, OpLt, OpLte, OpAnd, OpOr];
  37. var conditionStrings = ["==", "!=", ">", ">=", "<", "<=", "AND", "OR"];
  38. override public function saveParameters() : Dynamic {
  39. var parameters = {
  40. condition: this.condition.getName()
  41. };
  42. return parameters;
  43. }
  44. #if editor
  45. override public function getParametersHTML(width : Float) : Array<Element> {
  46. var elements = super.getParametersHTML(width);
  47. var element = new Element('<div style="width: ${width * 0.8}px; height: 40px"></div>');
  48. element.append('<span>Condition</span>');
  49. element.append(new Element('<select id="condition"></select>'));
  50. var input = element.children("select");
  51. var indexOption = 0;
  52. for (c in conditionStrings) {
  53. input.append(new Element('<option value="${indexOption}">${c}</option>'));
  54. indexOption++;
  55. }
  56. input.on("change", function(e) {
  57. var value = input.val();
  58. this.condition = availableConditions[value];
  59. });
  60. this.condition = availableConditions[0];
  61. elements.push(element);
  62. return elements;
  63. }
  64. #end
  65. }