Cond.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package hrt.shgraph.nodes;
  2. using hxsl.Ast;
  3. @name("Condition")
  4. @description("Create a custom condition between two inputs")
  5. @group("Condition")
  6. class Cond extends ShaderNode {
  7. @input("Left") var leftVar = SType.Number;
  8. @input("Right") var rightVar = SType.Number;
  9. @output("Boolean") var output = SType.Bool;
  10. @prop() var condition : Binop;
  11. override public function checkValidityInput(key : String, type : ShaderType.SType) : Bool {
  12. if (key == "leftVar" && rightVar != null && !rightVar.isEmpty())
  13. return ShaderType.checkCompatibilities(type, ShaderType.getSType(rightVar.getType()));
  14. if (key == "rightVar" && leftVar != null && !leftVar.isEmpty())
  15. return ShaderType.checkCompatibilities(type, ShaderType.getSType(leftVar.getType()));
  16. return true;
  17. }
  18. override public function computeOutputs() {
  19. if (leftVar != null && !leftVar.isEmpty() && rightVar != null && !rightVar.isEmpty()) {
  20. var type = leftVar.getVar(rightVar.getType()).t;
  21. switch(type) {
  22. case TVec(s, t):
  23. removeOutput("output");
  24. throw ShaderException.t("Vector of bools is not supported", this.id); //addOutput("output", TVec(s, VBool));
  25. case TFloat:
  26. addOutput("output", TBool);
  27. default:
  28. removeOutput("output");
  29. }
  30. } else
  31. removeOutput("output");
  32. }
  33. override public function build(key : String) : TExpr {
  34. return {
  35. p : null,
  36. t : output.type,
  37. e : TBinop(OpAssign, {
  38. e: TVar(output),
  39. p: null,
  40. t: output.type
  41. }, {e: TBinop(this.condition,
  42. leftVar.getVar(rightVar.getType()),
  43. rightVar.getVar(leftVar.getType())),
  44. p: null, t: output.type })
  45. };
  46. }
  47. var availableConditions = [OpEq, OpNotEq, OpGt, OpGte, OpLt, OpLte, OpAnd, OpOr];
  48. var conditionStrings = ["==", "!=", ">", ">=", "<", "<=", "AND", "OR"];
  49. override public function loadProperties(props : Dynamic) {
  50. this.condition = std.Type.createEnum(Binop, Reflect.field(props, "Condition"));
  51. }
  52. override public function saveProperties() : Dynamic {
  53. if (this.condition == null)
  54. this.condition = availableConditions[0];
  55. var properties = {
  56. condition: this.condition.getName()
  57. };
  58. return properties;
  59. }
  60. #if editor
  61. override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  62. var elements = super.getPropertiesHTML(width);
  63. var element = new hide.Element('<div style="width: ${width * 0.8}px; height: 40px"></div>');
  64. element.append('<span>Condition</span>');
  65. element.append(new hide.Element('<select id="condition"></select>'));
  66. if (this.condition == null) {
  67. this.condition = availableConditions[0];
  68. }
  69. var input = element.children("select");
  70. var indexOption = 0;
  71. for (c in conditionStrings) {
  72. input.append(new hide.Element('<option value="${indexOption}">${c}</option>'));
  73. if (this.condition == availableConditions[indexOption]) {
  74. input.val(indexOption);
  75. }
  76. indexOption++;
  77. }
  78. input.on("change", function(e) {
  79. var value = input.val();
  80. this.condition = availableConditions[value];
  81. });
  82. elements.push(element);
  83. return elements;
  84. }
  85. #end
  86. }