Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 8.16 KB | None | 0 0
  1. package custom;
  2.  
  3. import haxe.ui.components.CheckBox;
  4. import haxe.ui.components.DropDown;
  5. import haxe.ui.components.Image;
  6. import haxe.ui.components.Label;
  7. import haxe.ui.components.TextField;
  8. import haxe.ui.containers.Box;
  9. import haxe.ui.containers.Grid;
  10. import haxe.ui.containers.HBox;
  11. import haxe.ui.containers.ScrollView;
  12. import haxe.ui.containers.VBox;
  13. import haxe.ui.core.Component;
  14. import haxe.ui.core.KeyboardEvent;
  15. import haxe.ui.core.UIEvent;
  16. import haxe.ui.data.ArrayDataSource;
  17.  
  18. class PropertyEditor extends ScrollView {
  19.     private var _groups:Array<PropertyEditorGroup> = [];
  20.    
  21.     public function new() {
  22.         super();
  23.        
  24.         percentContentWidth = 100;
  25.     }
  26.    
  27.     public function addGroup(name:String):PropertyEditorGroup {
  28.         if (getGroup(name) != null) {
  29.             return getGroup(name);
  30.         }
  31.         var group:PropertyEditorGroup = new PropertyEditorGroup();
  32.         group.percentWidth = 100;
  33.         group.title = name;
  34.         addComponent(group);
  35.         group.registerEvent(UIEvent.CHANGE, function(e:UIEvent) {
  36.             var newEvent:UIEvent = new UIEvent(e.type);
  37.             newEvent.data = e.data;
  38.             dispatch(newEvent);
  39.         });
  40.        
  41.         _groups.push(group);
  42.         return group;
  43.     }
  44.    
  45.     public function clearGroups() {
  46.         for (g in _groups) {
  47.             removeComponent(g);
  48.         }
  49.         _groups = [];
  50.     }
  51.    
  52.     public function getGroup(name:String):PropertyEditorGroup {
  53.         var group:PropertyEditorGroup = null;
  54.         for (g in _groups) {
  55.             if (g.title == name) {
  56.                 group = g;
  57.                 break;
  58.             }
  59.         }
  60.         return group;
  61.     }
  62.    
  63.     public function addProperty(groupName:String, name:String, type:String, options:Dynamic = null) {
  64.         var group:PropertyEditorGroup = getGroup(groupName);
  65.         if (group == null) {
  66.             group = addGroup(groupName);
  67.         }
  68.        
  69.         group.addProperty(name, type, options);
  70.     }
  71.    
  72.     public function updateProperty(groupName:String, name:String, value:Dynamic) {
  73.         var group:PropertyEditorGroup = getGroup(groupName);
  74.         if (group == null) {
  75.             return;
  76.         }
  77.        
  78.         group.updateProperty(name, value);
  79.     }
  80.    
  81.     public function addCustom(groupName:String, c:Component) {
  82.         var group:PropertyEditorGroup = getGroup(groupName);
  83.         if (group == null) {
  84.             group = addGroup(groupName);
  85.         }
  86.        
  87.         group.addCustom(c);
  88.     }
  89. }
  90.  
  91. class PropertyEditorGroup extends VBox {
  92.     private var _title:Label;
  93.     private var _titleIcon:Image;
  94.    
  95.     private var _grid:Grid;
  96.     private var _properties:Array<Dynamic> = [];
  97.    
  98.     public function new() {
  99.         super();
  100.        
  101.         var hbox:HBox = new HBox();
  102.         hbox.id = "title";
  103.         hbox.percentWidth = 100;
  104.        
  105.         _titleIcon = new Image();
  106.         _titleIcon.resource = "icons/control-270-small.png";
  107.         hbox.addComponent(_titleIcon);
  108.        
  109.         _title = new Label();
  110.         _title.text = "Group";
  111.         hbox.addComponent(_title);
  112.         addComponent(hbox);
  113.        
  114.         hbox.onClick = function(e) {
  115.             if (childComponents.length == 2) {
  116.                 var c = childComponents[1];
  117.                 if (c.hidden == false) {
  118.                     c.hide();
  119.                     _titleIcon.resource = "icons/control-000-small.png";
  120.                 } else {
  121.                     c.show();
  122.                     _titleIcon.resource = "icons/control-270-small.png";
  123.                 }
  124.             }
  125.         }
  126.     }
  127.    
  128.     public var title(get, set):String;
  129.     private function get_title():String {
  130.         return _title.text;
  131.     }
  132.     private function set_title(value:String):String {
  133.         _title.text = value;
  134.         return value;
  135.     }
  136.    
  137.     public function addProperty(name:String, type:String, options:Dynamic = null) {
  138.         if (_grid == null) {
  139.             _grid = new Grid();
  140.             _grid.columns = 2;
  141.             _grid.id = "grid";
  142.             addComponent(_grid);
  143.         }
  144.        
  145.         var component:Component = null;
  146.         switch (type) {
  147.             case "text" | "float":
  148.                 component = new TextField();
  149.                 cast(component, TextField).registerEvent(UIEvent.CHANGE, function(e) {
  150.                     var event:UIEvent = new UIEvent(UIEvent.CHANGE);
  151.                     event.data = {
  152.                         group: title,
  153.                         name: name,
  154.                         value: component.text
  155.                     };
  156.                     dispatch(event);
  157.                 });
  158.             case "boolean":
  159.                 component = new CheckBox();
  160.                 cast(component, CheckBox).registerEvent(UIEvent.CHANGE, function(e) {
  161.                     var event:UIEvent = new UIEvent(UIEvent.CHANGE);
  162.                     event.data = {
  163.                         group: title,
  164.                         name: name,
  165.                         value: cast(component, CheckBox).selected
  166.                     };
  167.                     dispatch(event);
  168.                 });
  169.             case "option":
  170.                 component = new DropDown();
  171.                 var ds:ArrayDataSource<Dynamic> = new ArrayDataSource<Dynamic>();
  172.                 var arr:Array<Dynamic> = cast options;
  173.                 var v = null;
  174.                 for (a in arr) {
  175.                     if (v == null) {
  176.                         v = a.name;
  177.                     }
  178.                     ds.add({
  179.                         value: a.name,
  180.                         value2: a.value
  181.                     });
  182.                 }
  183.                 cast(component, DropDown).dataSource = ds;
  184.                 cast(component, DropDown).text = "";
  185.                 cast(component, DropDown).registerEvent(UIEvent.CHANGE, function(e) {
  186.                     var event:UIEvent = new UIEvent(UIEvent.CHANGE);
  187.                     event.data = {
  188.                         group: title,
  189.                         name: name,
  190.                         value: cast(component, DropDown).selectedItem.value2
  191.                     };
  192.                     dispatch(event);
  193.                 });
  194.         }
  195.        
  196.         if (component != null) {
  197.             component.id = name.toLowerCase();
  198.             var box:Box = new Box();
  199.             box.addClass("name-container");
  200.            
  201.             var label:Label = new Label();
  202.             label.text = name;
  203.             box.addComponent(label);
  204.             _grid.addComponent(box);
  205.            
  206.             var box:Box = new Box();
  207.             box.addClass("value-container");
  208.             box.addComponent(component);
  209.             _grid.addComponent(box);
  210.            
  211.             _properties.push({
  212.                 name: name,
  213.                 type: type,
  214.                 component: component
  215.             });
  216.         }
  217.     }
  218.    
  219.     public function addCustom(c:Component) {
  220.         addComponent(c);
  221.     }
  222.    
  223.     public function updateProperty(name:String, value:Dynamic) {
  224.         if (value == null) {
  225.             return;
  226.         }
  227.        
  228.         var prop:Dynamic = null;
  229.         for (p in _properties) {
  230.             if (p.name == name) {
  231.                 prop = p;
  232.                 break;
  233.             }
  234.         }
  235.        
  236.         if (prop != null) {
  237.             switch (prop.type) {
  238.                 case "text" | "float":
  239.                     cast(prop.component, TextField).text = Std.string(value);
  240.                 case "boolean":
  241.                     cast(prop.component, CheckBox).selected = Std.string(value) == "true";
  242.                 case "option":
  243.                     var ds:ArrayDataSource<Dynamic> = cast cast(prop.component, DropDown).dataSource;
  244.                     var selectedIndex:Int = -1;
  245.                     for (i in 0...ds.size) {
  246.                         if (ds.get(i).value2 == Std.string(value)) {
  247.                             selectedIndex = i;
  248.                             break;
  249.                         }
  250.                        
  251.                     }
  252.                     cast(prop.component, DropDown).selectedIndex = selectedIndex;
  253.             }
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement