Advertisement
Guest User

Untitled

a guest
Aug 21st, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.92 KB | None | 0 0
  1. module main;
  2.  
  3. import std.stdio;
  4.  
  5. import orange.serialization._;
  6. import orange.serialization.archives._;
  7.  
  8. alias writeln println;
  9.  
  10. enum code = `image({
  11.   id = "image2";
  12.   source = "images/Alpha-blue-trans.png";
  13.  
  14.   mouseArea({
  15.      id = "mArea2";
  16.      width = 3;// _ => image2.width;
  17.      height = 4;//_ => image2.height;
  18.  
  19.      onPressedChanged = {
  20.          writeln("onPressedChanged");
  21.          writeln(3/*mArea2.mouseX*/);
  22.      };
  23.   });
  24. });`;
  25.  
  26. void executeScript ()
  27. {
  28.     mixin(code);
  29. }
  30.  
  31. void main ()
  32. {
  33.     executeScript();
  34.     writeln(DQuickDsl.serialize());
  35. }
  36.  
  37. struct DQuickDsl
  38. {
  39.     static string serialize ()
  40.     {
  41.         auto archive = new XmlArchive!(char);
  42.         auto serializer = new Serializer(archive);
  43.  
  44.         serializer.serialize(root);
  45.         return archive.data;
  46.     }
  47. }
  48.  
  49. class Item
  50. {
  51.     string id;
  52.     string source;
  53.  
  54.     int width;
  55.     int height;
  56.  
  57.     Item[] childern;
  58.     @nonSerialized void delegate () onPressedChanged;
  59. }
  60.  
  61. Item root;
  62. Item currentItem;
  63.  
  64. void image (void delegate () block)
  65. {
  66.     restore!(currentItem, {
  67.         newItem();
  68.         block();
  69.     });
  70. }
  71.  
  72. void id (string id)
  73. {
  74.     currentItem.id = id;
  75. }
  76.  
  77. void source (string source)
  78. {
  79.     currentItem.source = source;
  80. }
  81.  
  82. void mouseArea (void delegate () block)
  83. {
  84.     restore!(currentItem, {
  85.         newItem();
  86.         block();
  87.     });
  88. }
  89.  
  90. void onPressedChanged (void delegate () block)
  91. {
  92.     currentItem.onPressedChanged = block;
  93. }
  94.  
  95. void newItem ()
  96. {
  97.     auto item = new Item;
  98.  
  99.     if (!root)
  100.         root = item;
  101.  
  102.     if (currentItem)
  103.         currentItem.childern ~= item;
  104.  
  105.     currentItem = item;
  106. }
  107.  
  108. void restore (alias var, alias block) ()
  109. {
  110.     auto tmp = var;
  111.  
  112.     scope (exit)
  113.         var = tmp;
  114.  
  115.     block();    
  116. }
  117.  
  118. void width (int width)
  119. {
  120.     currentItem.width = width;
  121. }
  122.  
  123. void height (int height)
  124. {
  125.     currentItem.height = height;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement