GameObjectListWidget.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Gtk;
  18. public class GameObjectListWidget : IconView
  19. {
  20. const int COL_NAME = 0;
  21. const int COL_OBJECT = 1;
  22. private IGameObject currentObject;
  23. private Application application;
  24. private Sector sector;
  25. private Gtk.Frame myFrame;
  26. public GameObjectListWidget(Application application)
  27. {
  28. this.application = application;
  29. ButtonPressEvent += OnButtonPressed;
  30. TextColumn = COL_NAME;
  31. application.SectorChanged += OnSectorChanged;
  32. }
  33. public void SetGtkFrame(Gtk.Frame myFrame)
  34. {
  35. this.myFrame = myFrame;
  36. }
  37. private void OnSectorChanged(Level level, Sector sector)
  38. {
  39. sector.ObjectAdded -= ObjectsChanged;
  40. sector.ObjectRemoved -= ObjectsChanged;
  41. this.sector = sector;
  42. sector.ObjectAdded += ObjectsChanged;
  43. sector.ObjectRemoved += ObjectsChanged;
  44. UpdateList();
  45. }
  46. private void ObjectsChanged(Sector sector, IGameObject Object)
  47. {
  48. if((Object is IObject) || (Object is ILayer))
  49. return;
  50. UpdateList();
  51. }
  52. private void UpdateList()
  53. {
  54. bool found = false;
  55. ListStore store = new ListStore(typeof(string), typeof(System.Object));
  56. foreach(IGameObject Object in sector.GetObjects()) {
  57. if (Object is ILayer) //skip items moved into LayerListWidget
  58. continue;
  59. if (Object is Camera) //skip Camera, it has Toolbar button and item in sectorSwitchNotebook context menu
  60. continue;
  61. if(! (Object is IObject)) {
  62. store.AppendValues(Object.GetType().Name, Object);
  63. found = true;
  64. }
  65. }
  66. Model = store;
  67. if (myFrame != null) myFrame.Visible = found;
  68. }
  69. [GLib.ConnectBefore]
  70. private void OnButtonPressed(object o, ButtonPressEventArgs args)
  71. {
  72. TreePath path = GetPathAtPos((int) args.Event.X, (int) args.Event.Y);
  73. if (path == null) return;
  74. TreeIter iter;
  75. if(!Model.GetIter(out iter, path))
  76. return;
  77. currentObject = (IGameObject) Model.GetValue(iter, COL_OBJECT);
  78. application.EditProperties(currentObject, currentObject.GetType().Name);
  79. if(args.Event.Button == 3) {
  80. ShowPopupMenu();
  81. }
  82. }
  83. private void ShowPopupMenu()
  84. {
  85. Menu popupMenu = new Menu();
  86. MenuItem editPathItem = new MenuItem("Edit Path");
  87. editPathItem.Activated += OnEditPath;
  88. editPathItem.Sensitive = currentObject is IPathObject;
  89. popupMenu.Append(editPathItem);
  90. MenuItem deletePathItem = new MenuItem("Delete Path");
  91. deletePathItem.Activated += OnDeletePath;
  92. deletePathItem.Sensitive = currentObject is IPathObject;
  93. popupMenu.Append(deletePathItem);
  94. MenuItem deleteItem = new ImageMenuItem(Stock.Delete, null);
  95. deleteItem.Activated += OnDelete;
  96. popupMenu.Append(deleteItem);
  97. popupMenu.ShowAll();
  98. popupMenu.Popup();
  99. }
  100. private void OnDelete(object o, EventArgs args)
  101. {
  102. if(currentObject == null)
  103. return;
  104. sector.Remove(currentObject);
  105. UpdateList();
  106. }
  107. private void OnEditPath(object o, EventArgs args)
  108. {
  109. if(! (currentObject is IPathObject))
  110. return;
  111. IPathObject pathObject = (IPathObject)currentObject;
  112. if (pathObject.Path == null) {
  113. pathObject.Path = new Path();
  114. pathObject.Path.Nodes.Add(new Path.Node());
  115. }
  116. application.SetToolPath();
  117. }
  118. private void OnDeletePath(object o, EventArgs args) {
  119. if (!(currentObject is IPathObject))
  120. return;
  121. IPathObject pathObject = (IPathObject)currentObject;
  122. if (pathObject.Path != null) {
  123. pathObject.Path = null;
  124. //application.SetEditor(null);
  125. }
  126. }
  127. }
  128. /* EOF */