Advertisement
Guest User

Untitled

a guest
Oct 5th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 8.97 KB | None | 0 0
  1. package issues.example_complex_item_renderer;
  2.  
  3. import haxe.ui.containers.HBox;
  4. import haxe.ui.containers.VBox;
  5. import haxe.ui.containers.dialogs.Dialogs;
  6. import haxe.ui.core.ItemRenderer;
  7. import haxe.ui.events.ItemEvent;
  8. import haxe.ui.events.MouseEvent;
  9.  
  10. @:xml('
  11. <vbox style="padding: 5px;">
  12.    <hbox>
  13.        <button id="editSelectedItemButton" text="Edit Selected Item" />
  14.        <button id="addUserButton" text="Add User" />
  15.        <button id="removeUserButton" text="Remove User" />
  16.    </hbox>
  17.  
  18.    <listview id="theList" width="500" height="200">
  19.        <my-item-renderer />
  20.    </listview>
  21. </vbox>
  22. ')
  23. class MainView extends VBox {
  24.     public function new() {
  25.         super();
  26.  
  27.         theList.itemRenderer = new MyItemRenderer();
  28.  
  29.         theList.dataSource.add({
  30.             balances: {
  31.                 currency1: { amount: 101, primaryIcon: "error-small.png" },
  32.                 currency2: { amount: 102, primaryIcon: "help-small.png", secondaryIcon: "warning-small.png" }
  33.             },
  34.             users: [
  35.                 { name: "User 1", image: "error-large.png"},
  36.                 { name: "User 2", image: "info-large.png"},
  37.                 { name: "User 3", image: "warning-large.png"},
  38.             ],
  39.             watching: {
  40.                 watchers1: { current: 5, max: 10 },
  41.                 watchers2: { current: 8, max: 30 }
  42.             }
  43.         });
  44.  
  45.         theList.dataSource.add({
  46.             balances: {
  47.                 currency1: { amount: 107, primaryIcon: "error-small.png" },
  48.                 currency2: { amount: 302, primaryIcon: "help-small.png", secondaryIcon: "warning-small.png" }
  49.             },
  50.             users: [
  51.                 { name: "User 4", image: "info-large.png"},
  52.                 { name: "User 5", image: "error-large.png"},
  53.                 { name: "User 6", image: "warning-large.png"},
  54.                 { name: "User 7", image: "help-large.png"},
  55.             ],
  56.             watching: {
  57.                 watchers1: { current: 1, max: 10 },
  58.                 watchers2: { current: 14, max: 30 }
  59.             }
  60.         });
  61.  
  62.         theList.dataSource.add({
  63.             balances: {
  64.                 currency1: { amount: 42, primaryIcon: "error-small.png" },
  65.                 currency2: { amount: 888, primaryIcon: "help-small.png", secondaryIcon: "warning-small.png" }
  66.             },
  67.             users: [
  68.                 { name: "User 8", image: "info-large.png"},
  69.                 { name: "User 9", image: "error-large.png"},
  70.             ],
  71.             watching: {
  72.                 watchers1: { current: 1, max: 10 },
  73.                 watchers2: { current: 14, max: 30 }
  74.             }
  75.         });
  76.     }
  77.  
  78.     @:bind(editSelectedItemButton, MouseEvent.CLICK)
  79.     private function onEditSelectedItemButton(_) {
  80.         var selectedIndex = theList.selectedIndex;
  81.         if (selectedIndex == -1) {
  82.             return;
  83.         }
  84.  
  85.         // note: if datasource was typed (and implemented IDataItem) then you would need to update it
  86.         var item = theList.dataSource.get(selectedIndex);
  87.         item.balances.currency1.amount = Std.random(999);
  88.         item.balances.currency2.amount = Std.random(999);
  89.         item.watching.watchers1.current = Std.random(50);
  90.         item.watching.watchers1.max = Std.random(50) + 50;
  91.         item.watching.watchers2.current = Std.random(50);
  92.         item.watching.watchers2.max = Std.random(50) + 50;
  93.         theList.dataSource.update(selectedIndex, item);
  94.     }
  95.  
  96.     @:bind(addUserButton, MouseEvent.CLICK)
  97.     private function onAddUser(_) {
  98.         var selectedIndex = theList.selectedIndex;
  99.         if (selectedIndex == -1) {
  100.             return;
  101.         }
  102.  
  103.         // note: if datasource was typed (and implemented IDataItem) then you would need to update it
  104.         var item = theList.dataSource.get(selectedIndex);
  105.         if (item.users == null) {
  106.             item.users = [];
  107.         }
  108.         item.users.push({
  109.             name: "New " + Std.random(9),
  110.             image: "info-large.png"
  111.         });
  112.         theList.dataSource.update(selectedIndex, item);
  113.     }
  114.  
  115.     @:bind(removeUserButton, MouseEvent.CLICK)
  116.     private function onRemoveUser(_) {
  117.         var selectedIndex = theList.selectedIndex;
  118.         if (selectedIndex == -1) {
  119.             return;
  120.         }
  121.  
  122.         // note: if datasource was typed (and implemented IDataItem) then you would need to update it
  123.         var item = theList.dataSource.get(selectedIndex);
  124.         if (item.users == null) {
  125.             return;
  126.         }
  127.         item.users.pop();
  128.         theList.dataSource.update(selectedIndex, item);
  129.     }
  130.  
  131.     @:bind(theList, ItemEvent.COMPONENT_EVENT)
  132.     private function onListComponentEvent(e:ItemEvent) {
  133.         Dialogs.messageBox("You clicked on " + e.source.id + " from row #" + e.itemIndex);
  134.     }
  135. }
  136.  
  137. @:xml('
  138. <item-renderer width="100%" layoutName="horizontal">
  139.    <style>
  140.        .transparent-scrollview {
  141.            border: none;
  142.            background-color: none;
  143.        }
  144.    </style>
  145.    <vbox verticalAlign="center">
  146.        <balance id="theBalance1" />
  147.        <balance id="theBalance2" />
  148.    </vbox>
  149.    <rule direction="vertical" />
  150.    <scrollview width="100%" styleName="transparent-scrollview">
  151.        <hbox id="avatarsContainer">
  152.        </hbox>
  153.    </scrollview>
  154.    <rule direction="vertical" />
  155.    <vbox verticalAlign="center">
  156.        <count id="theWatchers1" width="50" />
  157.        <count id="theWatchers2" />
  158.    </vbox>
  159.    <rule direction="vertical" />
  160.    <vbox verticalAlign="center">
  161.        <button id="enterButton" text="Enter" />
  162.        <link text="Watch" horizontalAlign="center" />
  163.    </vbox>
  164. </item-renderer>
  165. ')
  166. class MyItemRenderer extends ItemRenderer {
  167.     private override function onDataChanged(data:Dynamic) {
  168.         super.onDataChanged(data);
  169.         if (data != null) {
  170.             // set the data from the data item -> item renderer sub components
  171.             // balances
  172.             theBalance1.balance = data.balances.currency1.amount;
  173.             theBalance1.primaryIcon = data.balances.currency1.primaryIcon;
  174.             theBalance1.secondaryIcon = data.balances.currency1.secondaryIcon;
  175.             theBalance2.balance = data.balances.currency2.amount;
  176.             theBalance2.primaryIcon = data.balances.currency2.primaryIcon;
  177.             theBalance2.secondaryIcon = data.balances.currency2.secondaryIcon;
  178.  
  179.             // users
  180.             avatarsContainer.removeAllComponents();
  181.             if (data.users != null) {
  182.                 var users:Array<Dynamic> = data.users;
  183.                 for (user in users) {
  184.                     var avatar = new Avatar();
  185.                     avatar.name = user.name;
  186.                     avatar.image = user.image;
  187.                     avatarsContainer.addComponent(avatar);
  188.                 }
  189.             }
  190.  
  191.             // watchers
  192.             theWatchers1.current = data.watching.watchers1.current;
  193.             theWatchers1.max = data.watching.watchers1.max;
  194.             theWatchers2.current = data.watching.watchers2.current;
  195.             theWatchers2.max = data.watching.watchers2.max;
  196.         }
  197.     }
  198. }
  199.  
  200. @:xml('
  201. <hbox>
  202.    <image id="thePrimaryIcon" hidden="true" />
  203.    <image id="theSecondaryIcon" hidden="true" />
  204.    <label id="theBalance" text="n/a" />
  205. </hbox>
  206. ')
  207. class Balance extends HBox {
  208.     public var balance(null, set):Float;
  209.     private function set_balance(value:Float):Float {
  210.         theBalance.text = Std.string(value);
  211.         return value;
  212.     }
  213.  
  214.     public var primaryIcon(null, set):String;
  215.     private function set_primaryIcon(value:String):String {
  216.         if (value != null) {
  217.             thePrimaryIcon.resource = "haxeui-core/styles/shared/" + value;
  218.             thePrimaryIcon.show();
  219.         }
  220.         return value;
  221.     }
  222.  
  223.     public var secondaryIcon(null, set):String;
  224.     private function set_secondaryIcon(value:String):String {
  225.         if (value != null) {
  226.             theSecondaryIcon.resource = "haxeui-core/styles/shared/" + value;
  227.             theSecondaryIcon.show();
  228.         }
  229.         return value;
  230.     }
  231. }
  232.  
  233. @:xml('
  234. <vbox>
  235.    <image id="theImage" />
  236.    <label id="theName" horizontalAlign="center" />
  237. </vbox>
  238. ')
  239. class Avatar extends VBox {
  240.     public var image(null, set):String;
  241.     private function set_image(value:String):String {
  242.         if (value != null) {
  243.             theImage.resource = "haxeui-core/styles/shared/" + value;
  244.         }
  245.         return value;
  246.     }
  247.  
  248.     public var name(null, set):String;
  249.     private function set_name(value:String):String {
  250.         theName.text = value;
  251.         return value;
  252.     }
  253. }
  254.  
  255. @:xml('
  256. <hbox>
  257.    <label id="theCurrent" text="0" />
  258.    <label text="/" />
  259.    <label id="theMax" text="0" />
  260. </hbox>
  261. ')
  262. class Count extends HBox {
  263.     public var current(null, set):Float;
  264.     private function set_current(value:Float):Float {
  265.         theCurrent.text = Std.string(value);
  266.         return value;
  267.     }
  268.  
  269.     public var max(null, set):Float;
  270.     private function set_max(value:Float):Float {
  271.         theMax.text = Std.string(value);
  272.         return value;
  273.     }
  274. }
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement