1 | <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" > |
---|
2 | <head> |
---|
3 | <title>Inspect DojoX GFX JSON</title> |
---|
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
5 | <style type="text/css"> |
---|
6 | @import "../../../dojo/resources/dojo.css"; |
---|
7 | @import "../../../dijit/tests/css/dijitTests.css"; |
---|
8 | td.cell { padding: 1em 1em 0em 0em; } |
---|
9 | td.note { font-size: 80%; } |
---|
10 | </style> |
---|
11 | <script type="text/javascript" src="../../../dojo/dojo.js"></script> |
---|
12 | <script type="text/javascript"> |
---|
13 | dojo.require("dojox.gfx"); |
---|
14 | dojo.require("dojox.gfx.move"); |
---|
15 | dojo.require("dojox.gfx.utils"); |
---|
16 | |
---|
17 | surface = null; |
---|
18 | container_pos = null; |
---|
19 | mover = null; |
---|
20 | |
---|
21 | init = function(){ |
---|
22 | // initialize graphics |
---|
23 | var container = dojo.byId("gfx"); |
---|
24 | surface = dojox.gfx.createSurface(container, 500, 500); |
---|
25 | container_pos = dojo.coords(container, true); |
---|
26 | // wire UI |
---|
27 | dojo.connect(dojo.byId("load"), "onclick", onLoad); |
---|
28 | dojo.connect(dojo.byId("add"), "onclick", onAdd); |
---|
29 | // handle moves |
---|
30 | dojo.subscribe("/gfx/move/start", function(m){ mover = m; }); |
---|
31 | dojo.subscribe("/gfx/move/stop", function(){ mover = null; }); |
---|
32 | // handle shape operations |
---|
33 | dojo.connect(document, "onkeydown", onKeyDown); |
---|
34 | // cancel text selection and text dragging |
---|
35 | dojo.connect(container, "ondragstart", dojo, "stopEvent"); |
---|
36 | dojo.connect(container, "onselectstart", dojo, "stopEvent"); |
---|
37 | }; |
---|
38 | |
---|
39 | onLoad = function(){ |
---|
40 | var s = dojo.byId("source"); |
---|
41 | if(!s.value){ |
---|
42 | alert("Name of the file is required."); |
---|
43 | return; |
---|
44 | } |
---|
45 | dojo.xhrGet({ |
---|
46 | url: s.value, |
---|
47 | preventCache: true, |
---|
48 | handleAs: "json", |
---|
49 | load: loadObjects, |
---|
50 | error: function(r){ alert("Error: " + r); } |
---|
51 | }); |
---|
52 | }; |
---|
53 | |
---|
54 | mainObject = null; |
---|
55 | names = []; |
---|
56 | |
---|
57 | loadObjects = function(r){ |
---|
58 | if(!r){ |
---|
59 | alert("Wrong JSON object. Did you type the file name correctly?"); |
---|
60 | return; |
---|
61 | } |
---|
62 | mainObject = r; |
---|
63 | // clear old object names |
---|
64 | names = []; |
---|
65 | var s = dojo.byId("names"), ni = dojo.byId("names_info"); |
---|
66 | ni.innerHTML = ""; |
---|
67 | while(s.childNodes.length){ s.removeChild(s.lastChild); } |
---|
68 | // find new names |
---|
69 | findNames(s, dojo.byId("named").checked, "", mainObject); |
---|
70 | ni.innerHTML = " (" + names.length + ")"; |
---|
71 | }; |
---|
72 | |
---|
73 | findNames = function(selector, named_only, prefix, o){ |
---|
74 | if(o instanceof Array){ |
---|
75 | for(var i = 0; i < o.length; ++i){ |
---|
76 | findNames(selector, named_only, prefix, o[i]); |
---|
77 | } |
---|
78 | return; |
---|
79 | } |
---|
80 | if(named_only && !("name" in o)) return; |
---|
81 | var name = ("name" in o) ? o.name : "*", |
---|
82 | full = prefix ? prefix + "/" + name : name, |
---|
83 | opt = document.createElement("option"); |
---|
84 | opt.value = names.length; |
---|
85 | opt.innerHTML = full; |
---|
86 | names.push(o); |
---|
87 | selector.appendChild(opt); |
---|
88 | if("children" in o){ |
---|
89 | findNames(selector, named_only, full, o.children); |
---|
90 | } |
---|
91 | }; |
---|
92 | |
---|
93 | onAdd = function(){ |
---|
94 | var s = dojo.byId("names"); |
---|
95 | for(var i = 0; i < s.options.length; ++i){ |
---|
96 | var opt = s.options[i]; |
---|
97 | if(!opt.selected) continue; |
---|
98 | var object = names[Number(opt.value)]; |
---|
99 | var group = surface.createGroup(); |
---|
100 | dojox.gfx.utils.deserialize(group, object); |
---|
101 | new dojox.gfx.Moveable(group); // make it moveable as whole |
---|
102 | } |
---|
103 | }; |
---|
104 | |
---|
105 | // event handling |
---|
106 | |
---|
107 | onKeyDown = function(e){ |
---|
108 | if(!mover) return; |
---|
109 | switch(e.keyCode){ |
---|
110 | case "f".charCodeAt(0): case "F".charCodeAt(0): |
---|
111 | mover.shape.moveToFront(); |
---|
112 | break; |
---|
113 | case "b".charCodeAt(0): case "B".charCodeAt(0): |
---|
114 | mover.shape.moveToBack(); |
---|
115 | break; |
---|
116 | case "q".charCodeAt(0): case "Q".charCodeAt(0): |
---|
117 | mover.shape.applyLeftTransform(dojox.gfx.matrix.rotategAt(-15, mover.lastX - container_pos.x, mover.lastY - container_pos.y)); |
---|
118 | break; |
---|
119 | case "w".charCodeAt(0): case "W".charCodeAt(0): |
---|
120 | mover.shape.applyLeftTransform(dojox.gfx.matrix.rotategAt(15, mover.lastX - container_pos.x, mover.lastY - container_pos.y)); |
---|
121 | break; |
---|
122 | case "d".charCodeAt(0): case "D".charCodeAt(0): |
---|
123 | mover.shape.parent.remove(mover.shape); |
---|
124 | mover.shape.rawNode = null; |
---|
125 | mover.destroy(); |
---|
126 | break; |
---|
127 | } |
---|
128 | dojo.stopEvent(e); |
---|
129 | }; |
---|
130 | |
---|
131 | dojo.addOnLoad(init); |
---|
132 | </script> |
---|
133 | </head> |
---|
134 | <body> |
---|
135 | <h1>Inspect DojoX GFX JSON</h1> |
---|
136 | <p>Help: load a file, select an object, and add it, move it around, or apply operations to selected items:<br /> |
---|
137 | F — bring to front, B — bring to back, Q — rotate CCW, W — rotate CW, D — delete.<br /> |
---|
138 | (all operations work on currently dragged item).</p> |
---|
139 | <p><strong>VML note:</strong> VML doesn't process PNG images with opacity correctly.</p> |
---|
140 | <table><tr> |
---|
141 | <td align="left" valign="top" class="cell"><div id="gfx" style="width: 500px; height: 500px; border: solid 1px black;"></div></td> |
---|
142 | <td align="left" valign="top" class="cell"><table> |
---|
143 | <tr><td>Source:</td></tr> |
---|
144 | <tr><td><input type="text" id="source" value="data/Lars.json" size="30" /> <button id="load">Load</button><br /> |
---|
145 | <input type="checkbox" id="named" checked="checked" /> <label for="named">Load only named objects</label></td></tr> |
---|
146 | <tr><td class="note"><em>Available sources:</em></td></tr> |
---|
147 | <tr><td class="note"><em>data/Lars.json — vectors from SVG</em></td></tr> |
---|
148 | <tr><td class="note"><em>data/Nils.json — vectors from SVG</em></td></tr> |
---|
149 | <tr><td class="note"><em>data/LarsDreaming.json — vectors from SVG</em></td></tr> |
---|
150 | <tr><td class="note"><em>data/buratino.json — images</em></td></tr> |
---|
151 | <tr><td class="note"><em>data/transform.json — from dojox.gfx</em></td></tr> |
---|
152 | <tr><td> </td></tr> |
---|
153 | <tr><td>Objects<span id="names_info"></span>:</td></tr> |
---|
154 | <tr><td><select id="names" multiple="multiple" size="10" style="width: 300px;"></select></td></tr> |
---|
155 | <tr><td><button id="add">Add Selected</button></td></tr> |
---|
156 | <tr><td class="note"><div style="width: 300px;">Object names are hierarchical and separated by "/". Adding a selected object creates a group for this object. |
---|
157 | A higher-level object (a group) always includes lower-level objects as children.</div></td></tr> |
---|
158 | </table></td> |
---|
159 | </tr></table> |
---|
160 | </body> |
---|
161 | </html> |
---|