Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.27 KB | None | 0 0
  1.  
  2.  
  3. /**
  4.  * @author Julian Scheffers
  5. **/
  6.  
  7. /*
  8.  * GUI, Copyright (©) Julian Scheffers, all rights reserved.
  9.  */
  10.  
  11.  
  12.  
  13. int sketchWidth;
  14. int sketchHeight;
  15.  
  16. interface SidedRunnable {
  17.   public void pre();
  18.   public void post();
  19. }
  20.  
  21. interface AdvRunnable {
  22.   public Object run(Object in);
  23. }
  24.  
  25. AdvRunnable advRunnable(final Runnable S_simple) {
  26.   return new AdvRunnable(){
  27.     Runnable simple = S_simple;
  28.     public Object run(Object o) {
  29.       simple.run();
  30.       return null;
  31.     }
  32.   };
  33. }
  34.  
  35. class Style {
  36.   color fill;
  37.   color stroke;
  38.   float strokeWeight;
  39.   Style(color S_fill, color S_stroke, float S_strokeWeight) {
  40.     fill = S_fill;
  41.     stroke = S_stroke;
  42.     strokeWeight = S_strokeWeight;
  43.   }
  44.   PGraphics _set(PGraphics graphics) {
  45.     graphics.fill(fill);
  46.     graphics.stroke(stroke);
  47.     graphics.strokeWeight(strokeWeight);
  48.     return graphics;
  49.   }
  50.   void _set() {
  51.     fill(fill);
  52.     stroke(stroke);
  53.     strokeWeight(strokeWeight);
  54.   }
  55.   void _rect(float x, float y, float width, float height) {
  56.     fill(fill);
  57.     stroke(stroke);
  58.     strokeWeight(strokeWeight);
  59.     rect(x, y, width, height);
  60.   }
  61.   void _ellipse(float x, float y, float width, float height) {
  62.     fill(fill);
  63.     stroke(stroke);
  64.     strokeWeight(strokeWeight);
  65.     ellipse(x, y, width, height);
  66.   }
  67.   void _line(float bx, float by, float ex, float ey) {
  68.     fill(fill);
  69.     stroke(stroke);
  70.     strokeWeight(strokeWeight);
  71.     line(bx, by, ex, ey);
  72.   }
  73.   void _rect(float x, float y, float width, float height, PGraphics graphics) {
  74.     graphics.fill(fill);
  75.     graphics.stroke(stroke);
  76.     graphics.strokeWeight(strokeWeight);
  77.     graphics.rect(x, y, width, height);
  78.   }
  79.   void _ellipse(float x, float y, float width, float height, PGraphics graphics) {
  80.     graphics.fill(fill);
  81.     graphics.stroke(stroke);
  82.     graphics.strokeWeight(strokeWeight);
  83.     graphics.ellipse(x, y, width, height);
  84.   }
  85.   void _line(float bx, float by, float ex, float ey, PGraphics graphics) {
  86.     graphics.fill(fill);
  87.     graphics.stroke(stroke);
  88.     graphics.strokeWeight(strokeWeight);
  89.     graphics.line(bx, by, ex, ey);
  90.   }
  91. }
  92.  
  93. class TextStyle {
  94.   color col;
  95.   float size;
  96.   int align;
  97.   TextStyle(color S_col, float S_size, int S_align) {
  98.     col = S_col;
  99.     size = S_size;
  100.     align = S_align;
  101.   }
  102.   TextStyle(color S_col) {
  103.     col = S_col;
  104.     size = 12;
  105.     align = CORNER;
  106.   }
  107.   void _set() {
  108.     fill(col);
  109.     textSize(size);
  110.     textAlign(align);
  111.   }
  112.   void _set(PGraphics graphics) {
  113.     graphics.fill(col);
  114.     graphics.textSize(size);
  115.     graphics.textAlign(align);
  116.   }
  117.   void _text(String text, float x, float y) {
  118.     fill(col);
  119.     textSize(size);
  120.     textAlign(align);
  121.     text(text, x, y);
  122.   }
  123.   void _text(String text, float x, float y, PGraphics graphics) {
  124.     graphics.fill(col);
  125.     graphics.textSize(size);
  126.     graphics.textAlign(align);
  127.     graphics.text(text, x, y);
  128.   }
  129. }
  130.  
  131. class Text {
  132.   float x;
  133.   float y;
  134.   String text;
  135.   TextStyle style;
  136.   Text(float S_x, float S_y, String S_text, TextStyle S_style) {
  137.     x = S_x;
  138.     y = S_y;
  139.     text = S_text;
  140.     style = S_style;
  141.   }
  142.   Text(float S_x, float S_y, String S_text) {
  143.     x = S_x;
  144.     y = S_y;
  145.     text = S_text;
  146.     style = new TextStyle(0);
  147.   }
  148.   void display() {
  149.     style._text(text, x, y);
  150.   }
  151.   void display(PGraphics p) {
  152.     style._text(text, x, y, p);
  153.   }
  154. }
  155.  
  156. class CheckboxStyle {
  157.   Style check;
  158.   Style cross;
  159.   Style box;
  160.   Style disabled;
  161.   CheckboxStyle() {
  162.     check = new Style(255, #00ff00, 1);
  163.     cross = new Style(255, #ff0000, 1);
  164.     box = new Style(255, 127, 1);
  165.     disabled = new Style(255, 192, 1);
  166.   }
  167.   CheckboxStyle(Style S_check, Style S_cross, Style S_box, Style S_disabled) {
  168.     check = S_check;
  169.     cross = S_cross;
  170.     box = S_box;
  171.     disabled = S_disabled;
  172.   }
  173. }
  174.  
  175. class Checkbox {
  176.   int x;
  177.   int y;
  178.   int width;
  179.   int height;
  180.   boolean value;
  181.   boolean enabled;
  182.   CheckboxStyle style;
  183.   Runnable onChange;
  184.   Checkbox(int S_x, int S_y, int S_width, int S_height) {
  185.     x = S_x;
  186.     y = S_y;
  187.     width = S_width;
  188.     height = S_height;
  189.     value = false;
  190.     enabled = true;
  191.     style = new CheckboxStyle();
  192.     onChange = null;
  193.   }
  194.   Checkbox(int S_x, int S_y, int S_width, int S_height, CheckboxStyle S_style) {
  195.     x = S_x;
  196.     y = S_y;
  197.     width = S_width;
  198.     height = S_height;
  199.     value = false;
  200.     enabled = true;
  201.     style = S_style;
  202.     onChange = null;
  203.   }
  204.   Checkbox(int S_x, int S_y, int S_width, int S_height, Runnable S_onChange) {
  205.     x = S_x;
  206.     y = S_y;
  207.     width = S_width;
  208.     height = S_height;
  209.     value = false;
  210.     enabled = true;
  211.     style = new CheckboxStyle();
  212.     onChange = S_onChange;
  213.   }
  214.   Checkbox(int S_x, int S_y, int S_width, int S_height, CheckboxStyle S_style, Runnable S_onChange) {
  215.     x = S_x;
  216.     y = S_y;
  217.     width = S_width;
  218.     height = S_height;
  219.     value = false;
  220.     enabled = true;
  221.     style = S_style;
  222.     onChange = S_onChange;
  223.   }
  224.   void display() {
  225.     if (enabled) {
  226.       if (value) {
  227.         style.box._set();
  228.         rect(x, y, width, height);
  229.         style.check._set();
  230.         line(x, y + height / 2, x + width / 2, y + height);
  231.         line(x + width / 2, y + height, x + width, y);
  232.       }
  233.       else
  234.       {
  235.         style.box._set();
  236.         rect(x, y, width, height);
  237.         style.cross._set();
  238.         line(x, y, x + width, y + height);
  239.         line(x + width, y, x, y + height);
  240.       }
  241.     }
  242.     else
  243.     {
  244.       style.disabled._set();
  245.       rect(x, y, width, height);
  246.       if (value) {
  247.         line(x, y + height / 2, x + width / 2, y + height);
  248.         line(x + width / 2, y + height, x + width, y);
  249.       }
  250.       else
  251.       {
  252.         line(x, y, x + width, y + height);
  253.         line(x + width, y, x, y + height);
  254.       }
  255.     }
  256.   }
  257.   //do render in mousePressed
  258.   void render() {
  259.     if (enabled && mouseX >= x && mouseX < x + width && mouseY >= y && mouseY < y + height) value = !value;
  260.   }
  261. }
  262.  
  263. class DropdownStyle {
  264.   TextStyle text;
  265.   TextStyle textDisabled;
  266.   Style normal;
  267.   Style disabled;
  268.   color selected;
  269.   color hover;
  270.   DropdownStyle() {
  271.     text = new TextStyle(0);
  272.     textDisabled = new TextStyle(192);
  273.     normal = new Style(255, 127, 1);
  274.     disabled = new Style(255, 192, 1);
  275.     selected = 192;
  276.     hover = #60afff;
  277.   }
  278.   DropdownStyle(TextStyle S_text, TextStyle S_textDisabled, Style S_normal, Style S_disabled) {
  279.     text = S_text;
  280.     textDisabled = S_textDisabled;
  281.     normal = S_normal;
  282.     disabled = S_disabled;
  283.   }
  284. }
  285.  
  286. class DropdownElement {
  287.   String id;
  288.   String value;
  289.   DropdownElement(String S_id, String S_value) {
  290.     id = S_id;
  291.     value = S_value;
  292.   }
  293. }
  294.  
  295. class Dropdown {
  296.   int x;
  297.   int y;
  298.   int width;
  299.   int height;
  300.   int selectedIndex;
  301.   boolean open;
  302.   boolean enabled;
  303.   DropdownElement[] elements;
  304.   DropdownStyle style;
  305.   SidedRunnable onChange;
  306.   Dropdown(int S_x, int S_y, int S_width, int S_height, DropdownElement[] S_elements) {
  307.     x = S_x;
  308.     y = S_y;
  309.     width = S_width;
  310.     height = S_height;
  311.     selectedIndex = 0;
  312.     open = false;
  313.     enabled = true;
  314.     elements = S_elements;
  315.     style = new DropdownStyle();
  316.     onChange = null;
  317.   }
  318.   Dropdown(int S_x, int S_y, int S_width, int S_height, DropdownElement[] S_elements, DropdownStyle S_style) {
  319.     x = S_x;
  320.     y = S_y;
  321.     width = S_width;
  322.     height = S_height;
  323.     selectedIndex = 0;
  324.     open = false;
  325.     enabled = true;
  326.     elements = S_elements;
  327.     style = S_style;
  328.     onChange = null;
  329.   }
  330.   Dropdown(int S_x, int S_y, int S_width, int S_height, DropdownElement[] S_elements, SidedRunnable S_onChange) {
  331.     x = S_x;
  332.     y = S_y;
  333.     width = S_width;
  334.     height = S_height;
  335.     selectedIndex = 0;
  336.     open = false;
  337.     enabled = true;
  338.     elements = S_elements;
  339.     style = new DropdownStyle();
  340.     onChange = S_onChange;
  341.   }
  342.   Dropdown(int S_x, int S_y, int S_width, int S_height, DropdownElement[] S_elements, DropdownStyle S_style, SidedRunnable S_onChange) {
  343.     x = S_x;
  344.     y = S_y;
  345.     width = S_width;
  346.     height = S_height;
  347.     selectedIndex = 0;
  348.     open = false;
  349.     enabled = true;
  350.     elements = S_elements;
  351.     style = S_style;
  352.     onChange = S_onChange;
  353.   }
  354.   void render() {
  355.     if (open && enabled) {
  356.       style.normal._set();
  357.       rect(x, y, width, height);
  358.       line(x + width - height + height / 4, y + (height / 4) * 3, x + width - height / 2, y + height / 4);
  359.       line(x + width - height / 4, y + (height / 4) * 3, x + width - height / 2, y + height / 4);
  360.       int optionHeight = elements.length * (height / 3 * 2) + 1;
  361.       int optiony = y;
  362.       if (optiony + optionHeight > sketchHeight) optiony -= optiony + optionHeight - sketchHeight + 1;
  363.       rect(x, optiony, width - height, optionHeight);
  364.       for (int i = 0; i < elements.length; i++) {
  365.         if (mouseX >= x && mouseX < x + width - height && mouseY >= optiony + (height / 3 * 2) * i && mouseY < optiony + (height / 3 * 2) * (i + 1)) {
  366.           noStroke();
  367.           fill(style.hover);
  368.           rect(x + 1, optiony + (height / 3 * 2) * i + 1, width - height - 1, height / 3 * 2);
  369.           if (mousePressed) {
  370.             if (onChange != null) onChange.pre();
  371.             selectedIndex = i;
  372.             open = false;
  373.             if (onChange != null) onChange.post();
  374.           }
  375.         }
  376.         else if (selectedIndex == i) {
  377.           noStroke();
  378.           fill(style.selected);
  379.           rect(x + 1, optiony + (height / 3 * 2) * i + 1, width - height - 1, height / 3 * 2);
  380.         }
  381.         style.text._text(elements[i].value, x + 5, optiony + (height / 3 * 2) * (i + 1));
  382.       }
  383.     }
  384.     else
  385.     {
  386.       if (enabled) style.normal._set();
  387.       else style.disabled._set();
  388.       rect(x, y, width, height);
  389.       line(x + width - height, y, x + width - height, y + height);
  390.       line(x + width - height + height / 4, y + height / 4, x + width - height / 2, y + (height / 4) * 3);
  391.       line(x + width - height / 4, y + height / 4, x + width - height / 2, y + (height / 4) * 3);
  392.       if (enabled) style.text._set();
  393.       else style.textDisabled._set();
  394.       text(elements[selectedIndex].value, x + 5, y + height / 4 * 3);
  395.     }
  396.   }
  397.   //do clicked in mousePressed
  398.   void clicked() {
  399.     if (enabled) {
  400.       if (open) {
  401.       int optionHeight = elements.length * (height / 3 * 2) + 1;
  402.       int optiony = y;
  403.       if (optiony + optionHeight > sketchHeight) optiony -= optiony + optionHeight - sketchHeight + 1;
  404.         if (mouseX < x || mouseX > x + width - height || mouseY < optiony || mouseY > optiony + optionHeight) open = false;
  405.       }
  406.       else if (!open && mouseX >= x && mouseX < x + width && mouseY >= y && mouseY < y + height && elements.length >= 1) open = true;
  407.     }
  408.   }
  409. }
  410.  
  411. class TextInputStyle {
  412.   TextStyle text;
  413.   TextStyle textExample;
  414.   TextStyle textDisabled;
  415.   Style normal;
  416.   Style selected;
  417.   Style disabled;
  418.   TextInputStyle() {
  419.     text = new TextStyle(0);
  420.     textExample = new TextStyle(127);
  421.     textDisabled = new TextStyle(192);
  422.     normal = new Style(255, 127, 1);
  423.     selected = new Style(255, #60afff, 3);
  424.     disabled = new Style(255, 192, 1);
  425.   }
  426.   TextInputStyle(TextStyle S_text, TextStyle S_textExample, TextStyle S_textDisabled, Style S_normal, Style S_selected, Style S_disabled) {
  427.     text = S_text;
  428.     textExample = S_textExample;
  429.     textDisabled = S_textDisabled;
  430.     normal = S_normal;
  431.     selected = S_selected;
  432.     disabled = S_disabled;
  433.   }
  434. }
  435.  
  436. class TextInput {
  437.   TextInputStyle style;
  438.   int x;
  439.   int y;
  440.   int width;
  441.   int height;
  442.   int cursorPos;
  443.   boolean password;
  444.   boolean numeric;
  445.   boolean selected;
  446.   boolean enabled;
  447.   String text;
  448.   String example;
  449.   int lastMillis;
  450.   int barTimer;
  451.   Runnable onEnter;
  452.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric) {
  453.     x = S_x;
  454.     y = S_y;
  455.     width = S_width;
  456.     height = S_height;
  457.     password = S_password;
  458.     numeric = S_numeric;
  459.     selected = false;
  460.     enabled = true;
  461.     text = "";
  462.     example = "";
  463.     onEnter = null;
  464.     style = new TextInputStyle();
  465.     barTimer = 0;
  466.     lastMillis = millis();
  467.   }
  468.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, TextInputStyle S_style) {
  469.     x = S_x;
  470.     y = S_y;
  471.     width = S_width;
  472.     height = S_height;
  473.     password = S_password;
  474.     numeric = S_numeric;
  475.     selected = false;
  476.     enabled = true;
  477.     text = "";
  478.     example = "";
  479.     onEnter = null;
  480.     style = S_style;
  481.     barTimer = 0;
  482.     lastMillis = millis();
  483.   }
  484.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, String S_example) {
  485.     x = S_x;
  486.     y = S_y;
  487.     width = S_width;
  488.     height = S_height;
  489.     password = S_password;
  490.     numeric = S_numeric;
  491.     selected = false;
  492.     enabled = true;
  493.     text = "";
  494.     example = S_example;
  495.     onEnter = null;
  496.     style = new TextInputStyle();
  497.     barTimer = 0;
  498.     lastMillis = millis();
  499.   }
  500.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, String S_example, TextInputStyle S_style) {
  501.     x = S_x;
  502.     y = S_y;
  503.     width = S_width;
  504.     height = S_height;
  505.     password = S_password;
  506.     numeric = S_numeric;
  507.     selected = false;
  508.     enabled = true;
  509.     text = "";
  510.     example = S_example;
  511.     onEnter = null;
  512.     style = S_style;
  513.     barTimer = 0;
  514.     lastMillis = millis();
  515.   }
  516.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, Runnable S_onEnter) {
  517.     x = S_x;
  518.     y = S_y;
  519.     width = S_width;
  520.     height = S_height;
  521.     password = S_password;
  522.     numeric = S_numeric;
  523.     selected = false;
  524.     enabled = true;
  525.     text = "";
  526.     example = "";
  527.     onEnter = S_onEnter;
  528.     style = new TextInputStyle();
  529.     barTimer = 0;
  530.     lastMillis = millis();
  531.   }
  532.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, TextInputStyle S_style, Runnable S_onEnter) {
  533.     x = S_x;
  534.     y = S_y;
  535.     width = S_width;
  536.     height = S_height;
  537.     password = S_password;
  538.     numeric = S_numeric;
  539.     selected = false;
  540.     enabled = true;
  541.     text = "";
  542.     example = "";
  543.     onEnter = S_onEnter;
  544.     style = S_style;
  545.     barTimer = 0;
  546.     lastMillis = millis();
  547.   }
  548.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, String S_example, Runnable S_onEnter) {
  549.     x = S_x;
  550.     y = S_y;
  551.     width = S_width;
  552.     height = S_height;
  553.     password = S_password;
  554.     numeric = S_numeric;
  555.     selected = false;
  556.     enabled = true;
  557.     text = "";
  558.     example = S_example;
  559.     onEnter = S_onEnter;
  560.     style = new TextInputStyle();
  561.     barTimer = 0;
  562.     lastMillis = millis();
  563.   }
  564.   TextInput(int S_x, int S_y, int S_width, int S_height, boolean S_password, boolean S_numeric, String S_example, TextInputStyle S_style, Runnable S_onEnter) {
  565.     x = S_x;
  566.     y = S_y;
  567.     width = S_width;
  568.     height = S_height;
  569.     password = S_password;
  570.     numeric = S_numeric;
  571.     selected = false;
  572.     enabled = true;
  573.     text = "";
  574.     example = S_example;
  575.     onEnter = S_onEnter;
  576.     style = S_style;
  577.     barTimer = 0;
  578.     lastMillis = millis();
  579.   }
  580.   void display() {
  581.     if (enabled && selected) style.selected._set();
  582.     else if (enabled) style.normal._set();
  583.     else style.disabled._set();
  584.     rect(x, y, width, height);
  585.     if (enabled && text.equals("") && !example.equals("")) style.textExample._set();
  586.     else if (enabled) style.text._set();
  587.     else style.textDisabled._set();
  588.     if (text.equals("") && !example.equals("")) text(example, x + 3, y + height - 3);
  589.     else text(text, x + 3, y + height - 3);
  590.     String left = "";
  591.     for (int i = 0; i < cursorPos; i++) {
  592.       left += text.charAt(i);
  593.     }
  594.     strokeWeight(1);
  595.     barTimer += millis() - lastMillis;
  596.     lastMillis = millis();
  597.     if (barTimer >= 1000) {
  598.       barTimer -= 1000;
  599.     }
  600.     if (barTimer < 500 && selected) stroke(0, 255);
  601.     else noStroke();
  602.     line(x + 3 + textWidth(left), y + 2, x + 3 + textWidth(left), y + height - 2);
  603.   }
  604.   boolean mouseOver() {
  605.     return mouseX >= x && mouseX <= x + width - 1 && mouseY >= y && mouseY <= y + height - 1;
  606.   }
  607.   //do select in mousePressed
  608.   void select() {
  609.     selected = mouseOver() && enabled;
  610.   }
  611.   //do render in keyPressed
  612.   void render() {
  613.     if (enabled && selected) {
  614.       style.text._set();
  615.       char pressed = key;
  616.       int code = keyCode;
  617.       if (pressed == ENTER || pressed == RETURN) {
  618.         if (onEnter != null) onEnter.run();
  619.       }
  620.       else if (pressed == BACKSPACE) {
  621.         String pre = "";
  622.         String post = "";
  623.         boolean isPost = false;
  624.         for (int i = 0; i < text.length(); i++) {
  625.           if (i == cursorPos) isPost = true;
  626.           if (isPost) post += text.charAt(i);
  627.           else if (i < cursorPos - 1) pre += text.charAt(i);
  628.         }
  629.         text = pre + post;
  630.         if (cursorPos > 0) cursorPos --;
  631.       }
  632.       else if (pressed == '\f' || pressed == TAB);
  633.       else if (pressed == CODED) {
  634.         if (code == LEFT) {
  635.           if (cursorPos > 0) cursorPos --;
  636.         }
  637.         else if (code == RIGHT) {
  638.           if (cursorPos < text.length()) cursorPos ++;
  639.         }
  640.       }
  641.       else if (textWidth(text + pressed) <= width - 6 && !(numeric && ",.0123456789".indexOf(pressed) < 0)) {
  642.         String pre = "";
  643.         String post = "";
  644.         boolean isPost = false;
  645.         for (int i = 0; i < text.length(); i++) {
  646.           if (i == cursorPos) isPost = true;
  647.           if (isPost) post += text.charAt(i);
  648.           else pre += text.charAt(i);
  649.         }
  650.         text = pre + pressed + post;
  651.         cursorPos ++;
  652.       }
  653.       barTimer = 0;
  654.       lastMillis = millis();
  655.     }
  656.   }
  657. }
  658.  
  659. class ButtonStyle {
  660.   TextStyle text;
  661.   TextStyle textDisabled;
  662.   Style normal;
  663.   Style hover;
  664.   Style pressed;
  665.   Style disabled;
  666.   ButtonStyle() {
  667.     text = new TextStyle(0, 12, CENTER);
  668.     textDisabled = new TextStyle(200, 12, CENTER);
  669.     normal = new Style(255, 127, 1);
  670.     hover = new Style(#91c8ff, #60afff, 3);
  671.     pressed = new Style(#3c3fe8, #656691, 1);
  672.     disabled = new Style(255, 200, 1);
  673.   }
  674.   ButtonStyle(TextStyle S_text, TextStyle S_textDisabled, Style S_normal, Style S_hover, Style S_pressed, Style S_disabled) {
  675.     text = S_text;
  676.     textDisabled = S_textDisabled;
  677.     normal = S_normal;
  678.     hover = S_hover;
  679.     pressed = S_pressed;
  680.     disabled = S_disabled;
  681.     //make proper alignment stuffs (_textInRect()?)
  682.     text.align = CENTER;
  683.     textDisabled.align = CENTER;
  684.   }
  685. }
  686.  
  687. class Button {
  688.   int x;
  689.   int y;
  690.   int width;
  691.   int height;
  692.   String text;
  693.   boolean enabled;
  694.   boolean wasPressed;
  695.   boolean dEdge;
  696.   ButtonStyle style;
  697.   AdvRunnable onPress;
  698.   Button(int S_x, int S_y, int S_width, int S_height, String S_text, boolean S_dEdge, ButtonStyle S_style) {
  699.     x = S_x;
  700.     y = S_y;
  701.     width = S_width;
  702.     height = S_height;
  703.     text = S_text;
  704.     enabled = true;
  705.     style = S_style;
  706.     onPress = null;
  707.     wasPressed = false;
  708.     dEdge = S_dEdge;
  709.   }
  710.   Button(int S_x, int S_y, int S_width, int S_height, String S_text, boolean S_dEdge) {
  711.     x = S_x;
  712.     y = S_y;
  713.     if (S_width > ceil(textWidth(S_text))) width = S_width;
  714.     else width = ceil(textWidth(S_text));
  715.     height = S_height;
  716.     text = S_text;
  717.     enabled = true;
  718.     style = new ButtonStyle();
  719.     onPress = null;
  720.     wasPressed = false;
  721.     dEdge = S_dEdge;
  722.   }
  723.   Button(int S_x, int S_y, int S_width, int S_height, String S_text, boolean S_dEdge, ButtonStyle S_style, final Runnable S_onPress) {
  724.     x = S_x;
  725.     y = S_y;
  726.     width = S_width;
  727.     height = S_height;
  728.     text = S_text;
  729.     enabled = true;
  730.     style = S_style;
  731.     onPress = advRunnable(S_onPress);
  732.     wasPressed = false;
  733.     dEdge = S_dEdge;
  734.   }
  735.   Button(int S_x, int S_y, int S_width, int S_height, String S_text, boolean S_dEdge, Runnable S_onPress) {
  736.     x = S_x;
  737.     y = S_y;
  738.     if (S_width > ceil(textWidth(S_text))) width = S_width;
  739.     else width = ceil(textWidth(S_text));
  740.     height = S_height;
  741.     text = S_text;
  742.     enabled = true;
  743.     style = new ButtonStyle();
  744.     onPress = advRunnable(S_onPress);
  745.     wasPressed = false;
  746.     dEdge = S_dEdge;
  747.   }
  748.   Button(int S_x, int S_y, int S_width, int S_height, String S_text, boolean S_dEdge, ButtonStyle S_style, AdvRunnable S_onPress) {
  749.     x = S_x;
  750.     y = S_y;
  751.     width = S_width;
  752.     height = S_height;
  753.     text = S_text;
  754.     enabled = true;
  755.     style = S_style;
  756.     onPress = S_onPress;
  757.     wasPressed = false;
  758.     dEdge = S_dEdge;
  759.   }
  760.   Button(int S_x, int S_y, int S_width, int S_height, String S_text, boolean S_dEdge, AdvRunnable S_onPress) {
  761.     x = S_x;
  762.     y = S_y;
  763.     if (S_width > ceil(textWidth(S_text))) width = S_width;
  764.     else width = ceil(textWidth(S_text));
  765.     height = S_height;
  766.     text = S_text;
  767.     enabled = true;
  768.     style = new ButtonStyle();
  769.     onPress = S_onPress;
  770.     wasPressed = false;
  771.     dEdge = S_dEdge;
  772.   }
  773.   void render() {
  774.     if (enabled) {
  775.       if (pressed()) {
  776.         style.pressed._rect(x, y, width, height);
  777.         if (onPress != null && !wasPressed) onPress.run(this);
  778.       }
  779.       else if (mouseOver()) {
  780.         style.hover._rect(x, y, width, height);
  781.       }
  782.       else
  783.       {
  784.         style.normal._rect(x, y, width, height);
  785.       }
  786.       if (!pressed() && wasPressed && dEdge && onPress != null) onPress.run(this);
  787.       style.text._text(text, x + width / 2, y + height - 3);
  788.       wasPressed = pressed();
  789.     }
  790.     else
  791.     {
  792.       style.disabled._rect(x, y, width, height);
  793.       style.textDisabled._text(text, x + width / 2,  y + height - 3);
  794.     }
  795.   }
  796.   void render(PApplet app, int mouseX, int mouseY, boolean mousePressed) {
  797.     if (enabled) {
  798.       if (pressed(mouseX, mouseY, mousePressed)) {
  799.         style.pressed._rect(x, y, width, height, app.g);
  800.         if (onPress != null && !wasPressed) onPress.run(this);
  801.       }
  802.       else if (mouseOver(mouseX, mouseY)) {
  803.         style.hover._rect(x, y, width, height, app.g);
  804.       }
  805.       else
  806.       {
  807.         style.normal._rect(x, y, width, height, app.g);
  808.       }
  809.       if (!pressed(mouseX, mouseY, mousePressed) && wasPressed && dEdge && onPress != null) onPress.run(this);
  810.       style.text._text(text, x + width / 2, y + height - 3, app.g);
  811.       wasPressed = pressed(mouseX, mouseY, mousePressed);
  812.     }
  813.     else
  814.     {
  815.       style.disabled._rect(x, y, width, height, app.g);
  816.       style.textDisabled._text(text, x + width / 2,  y + height - 3, app.g);
  817.     }
  818.   }
  819.   void setEnabled(boolean S_enabled) {
  820.     enabled = S_enabled;
  821.   }
  822.   boolean isEnabled() {
  823.     return enabled;
  824.   }
  825.   boolean pressed() {
  826.     return mousePressed && enabled && mouseOver();
  827.   }
  828.   boolean mouseOver() {
  829.     return mouseX >= x && mouseX <= x + width - 1 && mouseY >= y && mouseY <= y + height - 1;
  830.   }
  831.   boolean pressed(int mouseX, int mouseY, boolean mousePressed) {
  832.     return mousePressed && enabled && mouseOver(mouseX, mouseY);
  833.   }
  834.   boolean mouseOver(int mouseX, int mouseY) {
  835.     return mouseX >= x && mouseX <= x + width - 1 && mouseY >= y && mouseY <= y + height - 1;
  836.   }
  837. }
  838.  
  839. class TextureButtonStyle {
  840.   PImage normal;
  841.   PImage hover;
  842.   PImage pressed;
  843.   PImage disabled;
  844.   TextureButtonStyle(PImage S_normal, PImage S_hover, PImage S_pressed, PImage S_disabled) {
  845.     normal = S_normal;
  846.     hover = S_hover;
  847.     pressed = S_pressed;
  848.     disabled = S_disabled;
  849.   }
  850. }
  851.  
  852. class TextureButton {
  853.   int x;
  854.   int y;
  855.   int width;
  856.   int height;
  857.   boolean enabled;
  858.   boolean wasPressed;
  859.   boolean dEdge;
  860.   TextureButtonStyle style;
  861.   AdvRunnable onPress;
  862.   TextureButton(int S_x, int S_y, int S_width, int S_height, boolean S_dEdge, TextureButtonStyle S_style) {
  863.     x = S_x;
  864.     y = S_y;
  865.     width = S_width;
  866.     height = S_height;
  867.     enabled = true;
  868.     style = S_style;
  869.     onPress = null;
  870.     wasPressed = false;
  871.     dEdge = S_dEdge;
  872.   }
  873.   TextureButton(int S_x, int S_y, int S_width, int S_height, boolean S_dEdge, TextureButtonStyle S_style, Runnable S_onPress) {
  874.     x = S_x;
  875.     y = S_y;
  876.     width = S_width;
  877.     height = S_height;
  878.     enabled = true;
  879.     style = S_style;
  880.     onPress = advRunnable(S_onPress);
  881.     wasPressed = false;
  882.     dEdge = S_dEdge;
  883.   }
  884.   TextureButton(int S_x, int S_y, int S_width, int S_height, boolean S_dEdge, TextureButtonStyle S_style, AdvRunnable S_onPress) {
  885.     x = S_x;
  886.     y = S_y;
  887.     width = S_width;
  888.     height = S_height;
  889.     enabled = true;
  890.     style = S_style;
  891.     onPress = S_onPress;
  892.     wasPressed = false;
  893.     dEdge = S_dEdge;
  894.   }
  895.   void render() {
  896.     if (enabled) {
  897.       if (pressed()) {
  898.         image(style.pressed, x, y, width, height);
  899.         if (onPress != null && !wasPressed) onPress.run(this);
  900.       }
  901.       else if (mouseOver()) {
  902.         image(style.hover, x, y, width, height);
  903.       }
  904.       else
  905.       {
  906.         image(style.normal, x, y, width, height);
  907.       }
  908.       if (!pressed() && wasPressed && dEdge && onPress != null) onPress.run(this);
  909.       wasPressed = pressed();
  910.     }
  911.     else
  912.     {
  913.       image(style.disabled, x, y, width, height);
  914.     }
  915.   }
  916.   void render(PGraphics p, int mouseX, int mouseY, boolean mousePressed) {
  917.     if (enabled) {
  918.       if (pressed(mouseX, mouseY, mousePressed)) {
  919.         p.image(style.pressed, x, y, width, height);
  920.         if (onPress != null && !wasPressed) onPress.run(this);
  921.       }
  922.       else if (mouseOver(mouseX, mouseY)) {
  923.         p.image(style.hover, x, y, width, height);
  924.       }
  925.       else
  926.       {
  927.         p.image(style.normal, x, y, width, height);
  928.       }
  929.       if (!pressed(mouseX, mouseY, mousePressed) && wasPressed && dEdge && onPress != null) onPress.run(this);
  930.       wasPressed = pressed(mouseX, mouseY, mousePressed);
  931.     }
  932.     else
  933.     {
  934.       p.image(style.disabled, x, y, width, height);
  935.     }
  936.   }
  937.   void setEnabled(boolean S_enabled) {
  938.     enabled = S_enabled;
  939.   }
  940.   boolean isEnabled() {
  941.     return enabled;
  942.   }
  943.   boolean pressed() {
  944.     return mousePressed && enabled && mouseOver();
  945.   }
  946.   boolean mouseOver() {
  947.     return mouseX >= x && mouseX <= x + width - 1 && mouseY >= y && mouseY <= y + height - 1;
  948.   }
  949.   boolean pressed(int mouseX, int mouseY, boolean mousePressed) {
  950.     return mousePressed && enabled && mouseOver(mouseX, mouseY);
  951.   }
  952.   boolean mouseOver(int mouseX, int mouseY) {
  953.     return mouseX >= x && mouseX <= x + width - 1 && mouseY >= y && mouseY <= y + height - 1;
  954.   }
  955. }
  956.  
  957. class ScrollBar {
  958.   int x;
  959.   int y;
  960.   int length;
  961.   int max;
  962.   int value;
  963.   boolean vertical;
  964.   ScrollBar(boolean S_vertical, int S_x, int S_y, int S_length, int S_max) {
  965.     x = S_x;
  966.     y = S_y;
  967.     length = S_length;
  968.     vertical = S_vertical;
  969.     max = S_max;
  970.   }
  971.   void display() {
  972.     if (max > 0) {
  973.       int barLength = length / max;
  974.       if (barLength < 10) barLength = 10;
  975.       float pixelPerValue = (length - barLength) / max;
  976.       float pixelOffset = pixelPerValue * value;
  977.       if (vertical) {
  978.         noStroke();
  979.         fill(127);
  980.         ellipse(x + 5, y + 5 + pixelOffset, 10, 10);
  981.         ellipse(x + 5, y + 5 + pixelOffset + barLength - 10, 10, 10);
  982.         rect(x, y + 5 + pixelOffset, 10, barLength - 10);
  983.       }
  984.       else
  985.       {
  986.        
  987.       }
  988.     }
  989.   }
  990.   void scroll(int scroll) {
  991.     value += scroll;
  992.     if (value < 0) value = 0;
  993.     if (value > max) value = max;
  994.   }
  995. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement