LayerListWidget.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using Gtk;
  20. using GLib;
  21. using Drawing;
  22. using LispReader;
  23. using Undo;
  24. public class LayerListWidget : TreeView {
  25. private Application application;
  26. private static object separatorObject = new System.Object();
  27. private static object badguysObject = new System.Object();
  28. private Sector sector;
  29. private Dictionary<object, float> visibility = new Dictionary<object, float>(new ReferenceComparer<object>());
  30. private class VisibilityRenderer : CellRendererPixbuf
  31. {
  32. public VisibilityRenderer()
  33. {
  34. Mode = CellRendererMode.Editable;
  35. }
  36. public override CellEditable StartEditing(Gdk.Event evnt,
  37. Widget widget,
  38. string path,
  39. Gdk.Rectangle background_area,
  40. Gdk.Rectangle cell_area,
  41. CellRendererState flags) {
  42. if(VisibilityChanged != null)
  43. VisibilityChanged(this, null);
  44. return null;
  45. }
  46. public delegate void VisibilityChangedHandler(object o, EventArgs args);
  47. public event VisibilityChangedHandler VisibilityChanged;
  48. }
  49. public LayerListWidget(Application application)
  50. {
  51. this.application = application;
  52. RowSeparatorFunc = OurRowSeparatorFunc;
  53. ButtonPressEvent += OnButtonPressed;
  54. VisibilityRenderer visibilityRenderer = new VisibilityRenderer();
  55. visibilityRenderer.VisibilityChanged += OnVisibilityChange;
  56. TreeViewColumn visibilityColumn = new TreeViewColumn("Visibility",
  57. visibilityRenderer);
  58. visibilityColumn.SetCellDataFunc(visibilityRenderer, (TreeCellDataFunc)VisibilityDataFunc);
  59. AppendColumn(visibilityColumn);
  60. CellRendererText TextRenderer = new CellRendererText();
  61. TreeViewColumn TypeColumn = new TreeViewColumn();
  62. TypeColumn.PackStart(TextRenderer, true);
  63. TypeColumn.SetCellDataFunc(TextRenderer, (TreeCellDataFunc)TextDataFunc);
  64. TypeColumn.Title = "Type";
  65. AppendColumn(TypeColumn);
  66. HeadersVisible = false;
  67. application.SectorChanged += OnSectorChanged;
  68. application.TilemapChanged += OnTilemapChanged;
  69. application.LevelChanged += OnLevelChanged;
  70. FieldOrProperty.AnyFieldChanged += OnFieldModified;
  71. }
  72. private void OnFieldModified(object Object, FieldOrProperty field, object oldValue)
  73. {
  74. if (! (Object is ILayer)) //ignore changes on other objects
  75. return;
  76. if (! (field.Name=="Layer" || field.Name=="Name")) //ignore changes on other fields
  77. return;
  78. //TODO: Is that sorting execute-once or what? (found no other working way)
  79. TreeStore store = (TreeStore) Model;
  80. if (store != null)
  81. store.SetSortFunc( 0, compareLayer );
  82. QueueDraw();
  83. }
  84. private void OnSectorChanged(Level level, Sector sector)
  85. {
  86. sector.ObjectAdded -= ObjectsChanged;
  87. sector.ObjectRemoved -= ObjectsChanged;
  88. this.sector = sector;
  89. //Find and select first solid tilemap (and discard tilemap from level opened before).
  90. application.CurrentTilemap = null;
  91. sector.ObjectAdded += ObjectsChanged;
  92. sector.ObjectRemoved += ObjectsChanged;
  93. UpdateList();
  94. }
  95. private void ObjectsChanged(Sector sector, IGameObject Object)
  96. {
  97. if(! (Object is ILayer))
  98. return;
  99. UpdateList();
  100. }
  101. /// <summary>Called when a new level is loaded</summary>
  102. private void OnLevelChanged(Level level)
  103. {
  104. // no-op; everything happens in OnSectorChanged
  105. }
  106. /// <summary> Get ZPos Value for object, non-tilemaps last </summary>
  107. private int getZPos (object o) {
  108. if(o is ILayer) {
  109. ILayer ILayer = (ILayer) o;
  110. return ILayer.Layer;
  111. }
  112. return int.MaxValue;
  113. }
  114. /// <summary> Compare ZPos Values and return which comes first </summary>
  115. public int compareLayer(TreeModel model, TreeIter tia, TreeIter tib){
  116. object objA = model.GetValue (tia, 0);
  117. object objB = model.GetValue (tib, 0);
  118. int a = getZPos(objA);
  119. int b = getZPos(objB);
  120. return a.CompareTo(b);
  121. }
  122. private void UpdateList()
  123. {
  124. visibility.Clear();
  125. TreeStore store = new TreeStore(typeof(System.Object));
  126. Model = store;
  127. store.SetSortFunc( 0, compareLayer );
  128. store.SetSortColumnId( 0, SortType.Ascending );
  129. foreach(ILayer ILayer in sector.GetObjects(typeof(ILayer))) {
  130. if (ILayer is IDrawableLayer) {
  131. visibility[ILayer] = application.CurrentRenderer.GetILayerColor(ILayer).Alpha;
  132. }
  133. store.AppendValues(ILayer);
  134. if (ILayer is Tilemap) {
  135. Tilemap Tilemap = (Tilemap) ILayer;
  136. // if no tilemap is yet selected, select the first solid one
  137. if ((application.CurrentTilemap == null) && (Tilemap.Solid)) {
  138. application.CurrentTilemap = Tilemap;
  139. string name = (String.IsNullOrEmpty(Tilemap.Name))?"":" \"" +Tilemap.Name + "\"";
  140. application.EditProperties(application.CurrentTilemap, "Tilemap" + name + " (" + application.CurrentTilemap.Layer + ")");
  141. }
  142. }
  143. }
  144. visibility[separatorObject] = 0;
  145. store.AppendValues(separatorObject);
  146. visibility[badguysObject] = application.CurrentRenderer.GetObjectsColor().Alpha;
  147. store.AppendValues(badguysObject);
  148. QueueDraw();
  149. }
  150. private void OnTilemapChanged(Tilemap Tilemap)
  151. {
  152. // Visibly select new Tilemap
  153. if (Tilemap != null) {
  154. TreePath path = TreePath.NewFirst();
  155. TreeIter iter;
  156. while (Model.GetIter(out iter, path)) {
  157. object obj = Model.GetValue(iter, 0);
  158. if(obj == Tilemap) {
  159. HasFocus = true;
  160. ActivateRow(path, GetColumn(0));
  161. SetCursor(path, GetColumn(0), false);
  162. }
  163. path.Next();
  164. }
  165. }
  166. }
  167. private bool OurRowSeparatorFunc (TreeModel model, TreeIter iter)
  168. {
  169. object o = model.GetValue(iter, 0);
  170. return (o == separatorObject);
  171. }
  172. private void VisibilityDataFunc(TreeViewColumn Column, CellRenderer Renderer,
  173. TreeModel Model, TreeIter Iter)
  174. {
  175. CellRendererPixbuf PixbufRenderer = (CellRendererPixbuf) Renderer;
  176. object o = Model.GetValue(Iter, 0);
  177. if (o is ILayer && !(o is IDrawableLayer)) { //no visibility for objects that we can't currently display
  178. PixbufRenderer.StockId = null;
  179. return;
  180. }
  181. float vis = visibility[o];
  182. if(vis <= 0) {
  183. PixbufRenderer.StockId = null;
  184. } else if(vis <= 0.5f) {
  185. PixbufRenderer.StockId = EditorStock.EyeHalf;
  186. } else {
  187. PixbufRenderer.StockId = EditorStock.Eye;
  188. }
  189. }
  190. private void TextDataFunc(TreeViewColumn Column, CellRenderer Renderer,
  191. TreeModel Model, TreeIter Iter)
  192. {
  193. object o = Model.GetValue(Iter, 0);
  194. CellRendererText TextRenderer = (CellRendererText) Renderer;
  195. if(o is ILayer) {
  196. ILayer ILayer = (ILayer) o;
  197. if (ILayer.Name.Length == 0) {
  198. TextRenderer.Text = ILayer.GetType().Name + " (" + ILayer.Layer + ")";
  199. } else {
  200. TextRenderer.Text = ILayer.Name + " (" + ILayer.Layer + ")";
  201. }
  202. } else {
  203. if (o == badguysObject)
  204. TextRenderer.Text = "Objects";
  205. else
  206. TextRenderer.Text = "UNKNOWN";
  207. }
  208. }
  209. [GLib.ConnectBefore]
  210. private void OnButtonPressed(object o, ButtonPressEventArgs args)
  211. {
  212. TreePath path;
  213. if(!GetPathAtPos((int) args.Event.X, (int) args.Event.Y, out path))
  214. return;
  215. TreeIter iter;
  216. if(!Model.GetIter(out iter, path))
  217. return;
  218. object obj = Model.GetValue(iter, 0);
  219. if(obj is Tilemap) {
  220. if (visibility[obj]>0) { //set null tilemap when selected one is invisible
  221. application.CurrentTilemap = (Tilemap) obj;
  222. } else {
  223. application.CurrentTilemap = null;
  224. }
  225. } else {
  226. if (obj == separatorObject)
  227. return;
  228. application.CurrentTilemap = null;
  229. }
  230. if (obj is ILayer) {
  231. ILayer ILayer = (ILayer) obj;
  232. if (ILayer != null) { //open it's properties if any
  233. string name = (String.IsNullOrEmpty(ILayer.Name))?"":" \"" +ILayer.Name + "\"";
  234. application.EditProperties(ILayer, ILayer.GetType().Name + name + " (" + ILayer.Layer.ToString() + ")");
  235. }
  236. }
  237. if ((args.Event.Button == 3) && (obj is ILayer)) {
  238. ShowPopupMenu(obj as ILayer);
  239. }
  240. }
  241. private void ShowPopupMenu(ILayer layer)
  242. {
  243. Menu popupMenu = new Menu();
  244. MenuItem addItem = new ImageMenuItem(Stock.Add, null);
  245. addItem.Activated += OnAdd;
  246. popupMenu.Append(addItem);
  247. if (layer is Tilemap) {
  248. MenuItem resizeItem = new MenuItem("Resize");
  249. resizeItem.Activated += OnResize;
  250. popupMenu.Append(resizeItem);
  251. MenuItem editPathItem = new MenuItem("Edit Path");
  252. editPathItem.Activated += OnEditPath;
  253. popupMenu.Append(editPathItem);
  254. MenuItem deletePathItem = new MenuItem("Delete Path");
  255. deletePathItem.Sensitive = application.CurrentTilemap != null && application.CurrentTilemap.Path != null;
  256. deletePathItem.Activated += OnDeletePath;
  257. popupMenu.Append(deletePathItem);
  258. MenuItem CheckIDsItem = new MenuItem("Check tile IDs");
  259. CheckIDsItem.Activated += OnCheckIDs;
  260. popupMenu.Append(CheckIDsItem);
  261. }
  262. MenuItem deleteItem = new ImageMenuItem(Stock.Delete, null);
  263. if (layer is Tilemap) {
  264. deleteItem.Sensitive = sector.GetObjects(typeof(Tilemap)).Count > 1;
  265. }
  266. deleteItem.Activated += OnDelete;
  267. popupMenu.Append(deleteItem);
  268. popupMenu.ShowAll();
  269. popupMenu.Popup();
  270. }
  271. private void OnAdd(object o, EventArgs args)
  272. {
  273. Tilemap tilemap = new Tilemap();
  274. tilemap.Resize( sector.Width, sector.Height, 0);
  275. sector.Add(tilemap, "Tilemap");
  276. }
  277. private void OnResize(object o, EventArgs args)
  278. {
  279. try {
  280. new ResizeDialog(sector, application.CurrentTilemap);
  281. } catch(Exception e) {
  282. ErrorDialog.Exception(e);
  283. }
  284. }
  285. private void OnEditPath(object o, EventArgs args)
  286. {
  287. application.SetToolPath(); //iPathToEdit is set when calling "EditProperties()" and already contains active Tilemap
  288. }
  289. private void OnDeletePath(object o, EventArgs args)
  290. {
  291. IPathObject pathObject = (IPathObject) application.CurrentTilemap;
  292. if (pathObject.Path != null) {
  293. Command command = new PropertyChangeCommand("Removed path of Tilemap " + application.CurrentTilemap.Name + " (" + application.CurrentTilemap.Layer + ")",
  294. FieldOrProperty.Lookup(typeof(Tilemap).GetProperty("Path")),
  295. application.CurrentTilemap,
  296. null);
  297. command.Do();
  298. UndoManager.AddCommand(command);
  299. }
  300. }
  301. private void OnDelete(object o, EventArgs args)
  302. {
  303. TreeIter treeIter;
  304. TreeModel treeModel;
  305. if (Selection.GetSelected(out treeModel, out treeIter))
  306. { //we have selected row
  307. IGameObject obj = (IGameObject) treeModel.GetValue(treeIter, 0);
  308. if (obj == null)
  309. return;
  310. // Don't remove last tilemap, that cause bugs.
  311. if (obj is Tilemap && sector.GetObjects(typeof(Tilemap)).Count == 1)
  312. return;
  313. sector.Remove(obj);
  314. if (obj is Tilemap) application.CurrentTilemap = null;
  315. UpdateList();
  316. }
  317. }
  318. private void OnCheckIDs(object o, EventArgs args) {
  319. if (application.CurrentTilemap == null)
  320. return;
  321. List<int> invalidtiles = QACheck.CheckIds(application.CurrentTilemap, application.CurrentLevel.Tileset);
  322. MessageType msgtype;
  323. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  324. if (invalidtiles.Count == 0) {
  325. msgtype = MessageType.Info;
  326. sb.Append("All tile ids are valid");
  327. } else {
  328. msgtype = MessageType.Warning;
  329. sb.Append("This tilemap contains tiles with these nonexistent IDs:");
  330. foreach (int id in invalidtiles) {
  331. sb.Append(" " + id.ToString());
  332. }
  333. }
  334. MessageDialog md = new MessageDialog(null, DialogFlags.DestroyWithParent,
  335. msgtype, ButtonsType.Close, sb.ToString());
  336. md.Run();
  337. md.Destroy();
  338. }
  339. private void OnVisibilityChange(object o, EventArgs args)
  340. {
  341. try {
  342. TreeIter treeIter;
  343. TreeModel treeModel;
  344. if (Selection.GetSelected(out treeModel, out treeIter))
  345. { //we have selected row
  346. object obj = treeModel.GetValue(treeIter, 0);
  347. if (obj is ILayer && !(obj is IDrawableLayer)) //skip it, if we can't currently display that object
  348. return;
  349. float vis = visibility[obj];
  350. float newvis = 1.0f;
  351. if(vis == 1.0f) {
  352. newvis = 0.5f;
  353. } else if(vis == 0.5f) {
  354. newvis = 0.0f;
  355. } else {
  356. newvis = 1.0f;
  357. }
  358. if (obj is ILayer)
  359. application.CurrentRenderer.SetILayerColor((ILayer)obj,
  360. new Color(1, 1, 1, newvis));
  361. if (obj == badguysObject)
  362. application.CurrentRenderer.SetObjectsColor(new Color(1, 1, 1, newvis));
  363. visibility[obj] = newvis;
  364. if (obj is Tilemap) { //Selecting and deselecting for invisible layers
  365. if ( (application.CurrentTilemap == obj) && newvis == 0) //deselect active tilemap that is made invisible
  366. application.CurrentTilemap = null;
  367. if ( (application.CurrentTilemap == null) && newvis != 0) //select un-invisibled tilemap if we have no active one
  368. application.CurrentTilemap = (Tilemap) obj;
  369. }
  370. QueueDraw();
  371. }
  372. } catch(Exception e) {
  373. LogManager.Log(LogLevel.Debug, "LayerListWidget.cs OnVisibilityChange: " + e);
  374. }
  375. }
  376. }
  377. /* EOF */