Style.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package h2d.domkit;
  2. class Style extends domkit.CssStyle {
  3. var currentObjects : Array<h2d.Object> = [];
  4. var resources : Array<hxd.res.Resource> = [];
  5. var errors : Array<String>;
  6. var errorsText : h2d.Text;
  7. public var allowInspect(default, set) = false;
  8. public var inspectKeyCode : Int = 0;
  9. public var inspectDetailsKeyCode : Int = hxd.Key.CTRL;
  10. public var s3d : h3d.scene.Scene;
  11. public function new() {
  12. super();
  13. }
  14. public function load( r : hxd.res.Resource, watchChanges = true ) {
  15. if( watchChanges ) r.watch(function() {
  16. #if (sys || nodejs)
  17. var fs = hxd.impl.Api.downcast(hxd.res.Loader.currentInstance.fs, hxd.fs.LocalFileSystem);
  18. if( fs != null ) fs.clearCache();
  19. #end
  20. onChange();
  21. });
  22. resources.push(r);
  23. add(new domkit.CssParser().parseSheet(r.entry.getText()));
  24. for( o in currentObjects )
  25. o.dom.applyStyle(this);
  26. }
  27. override function clear() {
  28. super.clear();
  29. resources.resize(0);
  30. }
  31. public function addObject( obj ) {
  32. currentObjects.remove(obj);
  33. currentObjects.push(obj);
  34. obj.dom.applyStyle(this);
  35. }
  36. public function removeObject(obj) {
  37. currentObjects.remove(obj);
  38. }
  39. /**
  40. Returns number of dom elements that were updated
  41. **/
  42. public function sync( ?dt : Float ) {
  43. if( dt == null )
  44. dt = hxd.Timer.elapsedTime;
  45. var T0 = domkit.CssStyle.TAG;
  46. for( o in currentObjects )
  47. o.dom.applyStyle(this, true);
  48. updateTime(dt);
  49. return domkit.CssStyle.TAG - T0;
  50. }
  51. override function onInvalidProperty(e:domkit.Properties<Dynamic>, s:domkit.CssStyle.RuleStyle, msg:String) {
  52. if( errors != null ) {
  53. var path = s.p.name;
  54. var ee = e;
  55. while(ee != null) {
  56. path = (ee.id.isDefined() ? "#" + ee.id.toString() : ee.component.name) + "." + path;
  57. ee = ee.parent;
  58. }
  59. if( msg == null ) msg = "Invalid property value '"+(domkit.CssParser.valueStr(s.value))+"'";
  60. errors.push(msg+" for " + path);
  61. }
  62. }
  63. function onChange( ntry : Int = 0 ) {
  64. if( ntry >= 10 ) return;
  65. ntry++;
  66. var oldRules = data.rules;
  67. errors = [];
  68. data.rules = [];
  69. for( r in resources ) {
  70. var txt = try r.entry.getText() catch( e : Dynamic ) { haxe.Timer.delay(onChange.bind(ntry),100); data.rules = oldRules; return; }
  71. var parser = new domkit.CssParser();
  72. try {
  73. data.add(parser.parseSheet(txt));
  74. } catch( e : domkit.Error ) {
  75. parser.warnings.push({ msg : e.message, pmin : e.pmin, pmax : e.pmax });
  76. }
  77. for( w in parser.warnings ) {
  78. var line = txt.substr(0,w.pmin).split("\n").length;
  79. errors.push(r.entry.path+":"+line+": " + w.msg);
  80. }
  81. }
  82. for( o in currentObjects )
  83. o.dom.applyStyle(this);
  84. if( errors.length == 0 ) {
  85. if( errorsText != null ) {
  86. errorsText.parent.remove();
  87. errorsText = null;
  88. }
  89. } else {
  90. if( errorsText == null ) {
  91. if( currentObjects.length == 0 ) return;
  92. var scene = currentObjects[0].getScene();
  93. if( scene == null ) {
  94. for( o in currentObjects ) {
  95. scene = o.getScene();
  96. if( scene != null ) break;
  97. }
  98. if( scene == null ) return;
  99. }
  100. var fl = new h2d.Flow();
  101. scene.add(fl,100);
  102. fl.backgroundTile = h2d.Tile.fromColor(0x400000,0.9);
  103. fl.padding = 10;
  104. errorsText = new h2d.Text(hxd.res.DefaultFont.get(), fl);
  105. }
  106. var fl = hxd.impl.Api.downcast(errorsText.parent, h2d.Flow);
  107. var sc = fl.getScene();
  108. fl.maxWidth = sc.width;
  109. errorsText.text = errors.join("\n");
  110. var b = fl.getBounds();
  111. fl.y = sc.height - Std.int(b.height);
  112. }
  113. }
  114. // ------ inspector -----
  115. var inspectModeActive = false;
  116. var inspectModeDetails = false;
  117. var inspectModeDetailsRight = -1;
  118. var inspectPreview : h2d.Object;
  119. var inspectPreviewObjects : Array<h2d.Object>;
  120. function set_allowInspect(b) {
  121. if( allowInspect == b )
  122. return b;
  123. @:privateAccess domkit.Properties.KEEP_VALUES = b;
  124. if( b ) {
  125. hxd.Window.getInstance().addEventTarget(onWindowEvent);
  126. onChange();
  127. } else {
  128. hxd.Window.getInstance().removeEventTarget(onWindowEvent);
  129. var scenes = [];
  130. for( o in currentObjects ) {
  131. var s = o.getScene();
  132. if( s == null || scenes.indexOf(s) >= 0 ) continue;
  133. scenes.push(s);
  134. function scanRec(o:h2d.Object) {
  135. if( o.dom != null ) @:privateAccess o.dom.currentValues = null;
  136. for( o in o ) scanRec(o);
  137. }
  138. scanRec(s);
  139. }
  140. if( inspectModeActive ) {
  141. inspectModeActive = false;
  142. hxd.System.setNativeCursor(Default);
  143. }
  144. }
  145. return allowInspect = b;
  146. }
  147. function onWindowEvent( e : hxd.Event ) {
  148. switch( e.kind ) {
  149. case EPush if( inspectKeyCode == 0 || hxd.Key.isDown(inspectKeyCode) ):
  150. if( e.button == hxd.Key.MOUSE_MIDDLE ) {
  151. if(hxd.Key.isDown(inspectDetailsKeyCode)) {
  152. inspectModeActive = true;
  153. inspectModeDetails = true;
  154. inspectModeDetailsRight = -1;
  155. }
  156. else {
  157. inspectModeActive = !inspectModeActive;
  158. inspectModeDetails = false;
  159. inspectModeDetailsRight = -1;
  160. }
  161. if( inspectModeActive && s3d != null && @:privateAccess s3d.renderer.debugging )
  162. inspectModeActive = false;
  163. if( inspectModeActive )
  164. updatePreview(e);
  165. else {
  166. clearPreview();
  167. hxd.System.setNativeCursor(Default);
  168. }
  169. }
  170. case EMove:
  171. if( inspectModeActive && !inspectModeDetails) updatePreview(e);
  172. case EWheel if( inspectKeyCode == 0 || hxd.Key.isDown(inspectKeyCode) ):
  173. if( inspectPreviewObjects != null ) {
  174. if( e.wheelDelta > 0 ) {
  175. var p = inspectPreviewObjects[0].parent;
  176. while( p != null && p.dom == null ) p = p.parent;
  177. if( p != null ) {
  178. var prev = inspectPreviewObjects;
  179. clearPreview();
  180. setPreview(p);
  181. inspectPreviewObjects = prev;
  182. inspectPreviewObjects.unshift(p);
  183. }
  184. } else if( inspectPreviewObjects.length > 1 ) {
  185. var prev = inspectPreviewObjects;
  186. clearPreview();
  187. prev.shift();
  188. setPreview(prev[0]);
  189. inspectPreviewObjects = prev;
  190. }
  191. }
  192. default:
  193. }
  194. if( inspectModeActive ) hxd.System.setNativeCursor(Move);
  195. }
  196. function clearPreview() {
  197. if( inspectPreview == null ) return;
  198. var obj = inspectPreviewObjects[0];
  199. var flow = Std.downcast(obj, h2d.Flow);
  200. if( flow != null ) flow.debug = false;
  201. inspectPreview.remove();
  202. inspectPreview = null;
  203. inspectPreviewObjects = null;
  204. }
  205. function updatePreview( e : hxd.Event ) {
  206. clearPreview();
  207. var checkedScenes = [];
  208. var ox = e.relX, oy = e.relY;
  209. for( o in currentObjects ) {
  210. var scene = o.getScene();
  211. if( scene == null || checkedScenes.indexOf(scene) >= 0 ) continue;
  212. checkedScenes.push(scene);
  213. e.relX = scene.mouseX;
  214. e.relY = scene.mouseY;
  215. if( lookupRec(scene, e) )
  216. break;
  217. }
  218. e.relX = ox;
  219. e.relY = oy;
  220. }
  221. function lookupRec( obj : h2d.Object, e : hxd.Event ) {
  222. if( !obj.visible || obj.alpha <= 0 )
  223. return false;
  224. var ch = @:privateAccess obj.children;
  225. for( i in 0...ch.length ) {
  226. if( lookupRec(ch[ch.length-1-i], e) )
  227. return true;
  228. }
  229. if( obj.dom == null )
  230. return false;
  231. var b = obj.getBounds();
  232. if( !b.contains(new h2d.col.Point(e.relX,e.relY)) )
  233. return false;
  234. if( Type.getClass(obj) == h2d.Object ) // objects containing transparent flow?
  235. return false;
  236. var fl = Std.downcast(obj, h2d.Flow);
  237. if( fl != null && fl.backgroundTile == null && fl.interactive == null )
  238. return false;
  239. setPreview(obj);
  240. return true;
  241. }
  242. @:access(domkit.Properties)
  243. static function getDisplayInfo(obj: h2d.Object) : String {
  244. var dom = obj.dom;
  245. var nameParts = [];
  246. if(dom != null) {
  247. nameParts.push(dom.component.name);
  248. if( dom.classes != null ) {
  249. for( c in dom.classes )
  250. nameParts.push("."+c);
  251. }
  252. if( dom.id.isDefined() )
  253. nameParts.push("#"+dom.id);
  254. }
  255. else
  256. nameParts.push('<font color="#aaaaaa">' + Type.getClassName(Type.getClass(obj)).split(".").pop() + '</font>');
  257. var sz = obj.getSize();
  258. nameParts.push(' <font color="#808080">${Math.ceil(sz.width)}x${Math.ceil(sz.height)}</font>');
  259. return nameParts.join("");
  260. }
  261. @:access(domkit.Properties)
  262. function setPreview( obj : h2d.Object, ?details=false) {
  263. var b = obj.getBounds();
  264. if( b.xMin < 0 ) b.xMin = 0;
  265. if( b.yMin < 0 ) b.yMin = 0;
  266. var scene = obj.getScene();
  267. if( scene == null ) return;
  268. inspectPreview = new h2d.Object();
  269. scene.add(inspectPreview, @:privateAccess scene.layerCount - 1);
  270. inspectPreviewObjects = [obj];
  271. if(inspectModeDetails) {
  272. var treeRoot = new h2d.Flow(inspectPreview);
  273. treeRoot.layout = Vertical;
  274. treeRoot.backgroundTile = h2d.Tile.fromColor(0x101010,1.0);
  275. treeRoot.padding = 6;
  276. var parents : Array<h2d.Object> = [];
  277. {
  278. var o = obj;
  279. for(i in 0...20) {
  280. o = o.parent;
  281. if(o == null) break;
  282. parents.push(o);
  283. }
  284. }
  285. function addBtn(o : h2d.Object, indent: Int, open: Bool) {
  286. var btn = new Flow(treeRoot);
  287. var txt = new HtmlText(hxd.res.DefaultFont.get(), btn);
  288. var text = getDisplayInfo(o);
  289. if(o.numChildren > 0 && !open)
  290. text = '<font color="#808080">[+] </font>' + text;
  291. txt.text = text;
  292. btn.paddingLeft = 10 * indent;
  293. btn.paddingTop = btn.paddingBottom = 2;
  294. btn.paddingRight = 10;
  295. btn.enableInteractive = true;
  296. if(o == obj)
  297. btn.backgroundTile = Tile.fromColor(0x50aaff, 1, 1, 0.5);
  298. else {
  299. btn.interactive.onOver = function(_) btn.backgroundTile = Tile.fromColor(0x202020, 1, 1, 1.0);
  300. btn.interactive.onOut = function(_) btn.backgroundTile = null;
  301. }
  302. btn.interactive.onPush = function(e) {
  303. clearPreview();
  304. setPreview(o, true);
  305. }
  306. }
  307. for(i in 0...parents.length)
  308. addBtn(parents[parents.length-1 - i], i, true);
  309. var thisIndent = parents.length;
  310. var childCount = 0;
  311. final maxDepth = 3;
  312. function addChildRec(o : h2d.Object, depth : Int) {
  313. if(childCount > 50) return;
  314. addBtn(o, thisIndent + depth, depth < maxDepth);
  315. ++childCount;
  316. if(depth < maxDepth) {
  317. for(i in 0...o.numChildren)
  318. addChildRec(o.getChildAt(i), depth+1);
  319. }
  320. }
  321. addChildRec(obj, 0);
  322. if(inspectModeDetailsRight < 0)
  323. inspectModeDetailsRight = (scene.mouseX > scene.width / 2) ? 0 : 1;
  324. if(inspectModeDetailsRight > 0)
  325. treeRoot.x = scene.width - treeRoot.getBounds().width;
  326. }
  327. var p = new h2d.Bitmap(h2d.Tile.fromColor(0xFF0000, Math.round(b.width), Math.round(b.height), 0.1), inspectPreview);
  328. p.x = Math.round(b.xMin);
  329. p.y = Math.round(b.yMin);
  330. var flow = Std.downcast(obj, h2d.Flow);
  331. if( flow != null )
  332. flow.debug = true;
  333. else {
  334. var w = p.tile.iwidth;
  335. var h = p.tile.iheight;
  336. var horiz = h2d.Tile.fromColor(0xFF0000, w, 1);
  337. var vert = h2d.Tile.fromColor(0xFF0000, 1, h);
  338. new h2d.Bitmap(horiz, p);
  339. new h2d.Bitmap(vert, p);
  340. new h2d.Bitmap(horiz, p).y = h - 1;
  341. new h2d.Bitmap(vert, p).x = w - 1;
  342. }
  343. var prevFlow = new h2d.Flow(p);
  344. prevFlow.backgroundTile = h2d.Tile.fromColor(0,0.8);
  345. prevFlow.padding = 7;
  346. prevFlow.paddingTop = 4;
  347. var previewText = new h2d.HtmlText(hxd.res.DefaultFont.get(), prevFlow);
  348. var lines = [];
  349. lines.push(getDisplayInfo(obj));
  350. lines.push("");
  351. var dom = obj.dom;
  352. if(dom != null) {
  353. for( s in dom.style ) {
  354. if( s.p.name == "text" || hxd.impl.Api.isOfType(s.value,h2d.Tile) ) continue;
  355. lines.push(' <font color="#D0D0D0"> ${s.p.name}</font> <font color="#808080">${s.value}</font><font color="#606060"> (style)</font>');
  356. }
  357. for( i in 0...dom.currentSet.length ) {
  358. var p = dom.currentSet[i];
  359. if( p.name == "text" ) continue;
  360. var v = dom.currentValues == null ? null : dom.currentValues[i];
  361. var vstr = v == null ? "???" : StringTools.htmlEscape(domkit.CssParser.valueStr(v));
  362. lines.push(' <font color="#D0D0D0"> ${p.name}</font> <font color="#808080">$vstr</font>');
  363. }
  364. previewText.text = lines.join("<br/>");
  365. }
  366. var size = prevFlow.getBounds();
  367. if( b.xMax + size.width < scene.width )
  368. prevFlow.x = Std.int(b.width+2);
  369. else if( b.xMin - size.width > 0 )
  370. prevFlow.x = -Std.int(size.width+2);
  371. if( b.yMin + size.height + 10 > scene.height )
  372. prevFlow.y = Std.int(scene.height - (b.yMin + size.height + 10));
  373. }
  374. }