ControlPoint.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 OpenGl;
  18. using DataStructures;
  19. using SceneGraph;
  20. public sealed class ControlPoint : IObject, Node
  21. {
  22. public enum AttachPoint {
  23. TOP = 1,
  24. BOTTOM = 2,
  25. LEFT = 4,
  26. RIGHT = 8
  27. };
  28. private RectangleF area;
  29. private AttachPoint attachPoint;
  30. public IObject Object;
  31. private static float DISTANCE = 12;
  32. private static float SIZE = 16;
  33. public ControlPoint(IObject Object, AttachPoint attachPoint)
  34. {
  35. this.Object = Object;
  36. this.attachPoint = attachPoint;
  37. }
  38. public void Draw(Gdk.Rectangle cliprect)
  39. {
  40. UpdatePosition();
  41. gl.Color4f(0, 0, 1, 0.7f);
  42. gl.Disable(gl.TEXTURE_2D);
  43. gl.Begin(gl.QUADS);
  44. gl.Vertex2f(Area.Left, Area.Top);
  45. gl.Vertex2f(Area.Right, Area.Top);
  46. gl.Vertex2f(Area.Right, Area.Bottom);
  47. gl.Vertex2f(Area.Left, Area.Bottom);
  48. gl.End();
  49. gl.Enable(gl.TEXTURE_2D);
  50. gl.Color4f(1, 1, 1, 1);
  51. }
  52. public void UpdatePosition()
  53. {
  54. Vector pos;
  55. if((attachPoint & AttachPoint.TOP) != 0) {
  56. pos.Y = Object.Area.Top - DISTANCE;
  57. } else if((attachPoint & AttachPoint.BOTTOM) != 0) {
  58. pos.Y = Object.Area.Bottom + DISTANCE;
  59. } else {
  60. pos.Y = (Object.Area.Top + Object.Area.Bottom) / 2f;
  61. }
  62. if((attachPoint & AttachPoint.LEFT) != 0) {
  63. pos.X = Object.Area.Left - DISTANCE;
  64. } else if((attachPoint & AttachPoint.RIGHT) != 0) {
  65. pos.X = Object.Area.Right + DISTANCE;
  66. } else {
  67. pos.X = (Object.Area.Left + Object.Area.Right) / 2f;
  68. }
  69. area = new RectangleF(pos.X - SIZE/2f, pos.Y - SIZE/2f, SIZE, SIZE);
  70. }
  71. public void ChangeArea(RectangleF Area)
  72. {
  73. float adjust = SIZE/2f + DISTANCE;
  74. RectangleF newArea = Object.Area;
  75. if((attachPoint & AttachPoint.TOP) != 0) {
  76. newArea.Top = Area.Top + adjust;
  77. } else if((attachPoint & AttachPoint.BOTTOM) != 0) {
  78. newArea.Bottom = Area.Bottom - adjust;
  79. }
  80. if((attachPoint & AttachPoint.LEFT) != 0) {
  81. newArea.Left = Area.Left + adjust;
  82. } else if((attachPoint & AttachPoint.RIGHT) != 0) {
  83. newArea.Right = Area.Right - adjust;
  84. }
  85. Object.ChangeArea(newArea);
  86. area = Area;
  87. }
  88. public bool Resizable {
  89. get {
  90. return false;
  91. }
  92. }
  93. public RectangleF Area {
  94. get {
  95. return area;
  96. }
  97. }
  98. public Node GetSceneGraphNode() {
  99. return this;
  100. }
  101. }
  102. /* EOF */