RenderView.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // $Id$
  2. using System;
  3. using Gtk;
  4. using Gdk;
  5. using OpenGl;
  6. using Drawing;
  7. using DataStructures;
  8. public abstract class RenderView : GLWidgetBase
  9. {
  10. private bool dragging;
  11. private Vector dragStartMouse;
  12. private Vector dragStartTranslation;
  13. private Vector MousePos;
  14. private IEditor editor;
  15. public IEditor Editor {
  16. set {
  17. if(this.editor != null) {
  18. this.editor.Redraw -= QueueDraw;
  19. if(this.editor is IEditorCursorChange)
  20. ((IEditorCursorChange) this.editor).CursorChange -= CursorChange;
  21. if(this.editor is IDisposable)
  22. ((IDisposable) this.editor).Dispose();
  23. }
  24. this.editor = value;
  25. this.editor.Redraw += QueueDraw;
  26. if(this.editor is IEditorCursorChange)
  27. ((IEditorCursorChange) this.editor).CursorChange += CursorChange;
  28. }
  29. get {
  30. return editor;
  31. }
  32. }
  33. public RenderView()
  34. {
  35. ButtonPressEvent += OnButtonPress;
  36. ButtonReleaseEvent += OnButtonRelease;
  37. MotionNotifyEvent += OnMotionNotify;
  38. ScrollEvent += OnScroll;
  39. AddEvents((int) Gdk.EventMask.ButtonPressMask);
  40. AddEvents((int) Gdk.EventMask.ButtonReleaseMask);
  41. AddEvents((int) Gdk.EventMask.PointerMotionMask);
  42. AddEvents((int) Gdk.EventMask.ScrollMask);
  43. CanFocus = true;
  44. }
  45. protected override void DrawGl()
  46. {
  47. gl.ClearColor(0.4f, 0, 0.4f, 1);
  48. gl.Clear(gl.COLOR_BUFFER_BIT);
  49. DrawingContext context = new DrawingContext();
  50. /* TODO: setup transform */
  51. DrawContents(context);
  52. if(!dragging && Editor != null)
  53. Editor.Draw(context);
  54. context.DoDrawing();
  55. }
  56. public abstract void DrawContents(DrawingContext context);
  57. private void OnButtonPress(object o, ButtonPressEventArgs args)
  58. {
  59. try {
  60. MousePos = MouseToWorld(
  61. new Vector((float) args.Event.X, (float) args.Event.Y));
  62. if(args.Event.Button == 2) {
  63. dragStartMouse = new Vector((float) args.Event.X, (float) args.Event.Y);
  64. dragStartTranslation = Translation;
  65. dragging = true;
  66. QueueDraw();
  67. } else if(Editor != null) {
  68. Editor.OnMouseButtonPress(MousePos, (int) args.Event.Button, args.Event.State);
  69. }
  70. args.RetVal = true;
  71. } catch(Exception e) {
  72. ErrorDialog.Exception(e);
  73. }
  74. }
  75. private void OnButtonRelease(object o, ButtonReleaseEventArgs args)
  76. {
  77. try {
  78. MousePos = MouseToWorld(
  79. new Vector((float) args.Event.X, (float) args.Event.Y));
  80. if(args.Event.Button == 2) {
  81. dragging = false;
  82. QueueDraw();
  83. } else if(Editor != null) {
  84. Editor.OnMouseButtonRelease(MousePos, (int) args.Event.Button, args.Event.State);
  85. }
  86. args.RetVal = true;
  87. } catch(Exception e) {
  88. ErrorDialog.Exception(e);
  89. }
  90. }
  91. private void OnMotionNotify(object o, MotionNotifyEventArgs args)
  92. {
  93. try {
  94. Vector pos = new Vector((float) args.Event.X, (float) args.Event.Y);
  95. MousePos = MouseToWorld(pos);
  96. if(dragging) {
  97. SetTranslation(dragStartTranslation +
  98. (pos - dragStartMouse) / Zoom);
  99. } else if(Editor != null) {
  100. Editor.OnMouseMotion(MousePos, args.Event.State);
  101. }
  102. args.RetVal = true;
  103. } catch(Exception e) {
  104. ErrorDialog.Exception(e);
  105. }
  106. }
  107. private void OnScroll(object o, ScrollEventArgs args)
  108. {
  109. float oldZoom = Zoom;
  110. Vector realMousePos = (MousePos + Translation) * Zoom;
  111. if(args.Event.Direction == ScrollDirection.Down) {
  112. Zoom /= (float) Math.Sqrt(2);
  113. } else if(args.Event.Direction == ScrollDirection.Up) {
  114. Zoom *= (float) Math.Sqrt(2);
  115. }
  116. //Limit the Zoom to useful values;
  117. if( Zoom < 0.002 || Zoom > 500 ){
  118. Zoom = oldZoom;
  119. }
  120. SetTranslation(Translation
  121. + realMousePos / Zoom - realMousePos / oldZoom);
  122. MousePos = MouseToWorld(realMousePos);
  123. if(Editor != null) {
  124. Editor.OnMouseMotion(MousePos, args.Event.State);
  125. }
  126. args.RetVal = true;
  127. }
  128. private void UpdateAdjustments()
  129. {
  130. if(hadjustment != null) {
  131. hadjustment.SetBounds(minx, maxx, 32/Zoom, 256/Zoom, Allocation.Width/Zoom);
  132. hadjustment.ClampPage(-Translation.X, -Translation.X + (Allocation.Width/Zoom));
  133. }
  134. if(vadjustment != null) {
  135. vadjustment.SetBounds(miny, maxy, 32/Zoom, 256/Zoom, Allocation.Height/Zoom);
  136. vadjustment.ClampPage(-Translation.Y, -Translation.Y + (Allocation.Height/Zoom));
  137. }
  138. }
  139. public void SetZoom(float newZoom)
  140. {
  141. float oldZoom = Zoom;
  142. Zoom = newZoom;
  143. // Limit the Zoom to useful values;
  144. if( Zoom < 0.002 || Zoom > 500 ){
  145. Zoom = oldZoom;
  146. }
  147. UpdateAdjustments();
  148. QueueDraw();
  149. }
  150. public float GetZoom()
  151. {
  152. return Zoom;
  153. }
  154. public void ZoomIn()
  155. {
  156. SetZoom( Zoom * (float) Math.Sqrt(2));
  157. }
  158. public void ZoomOut()
  159. {
  160. SetZoom( Zoom / (float) Math.Sqrt(2));
  161. }
  162. public void SetTranslation(Vector tr)
  163. {
  164. if(Translation != tr) {
  165. Translation = tr;
  166. UpdateAdjustments();
  167. QueueDraw();
  168. }
  169. }
  170. public Vector GetTranslation()
  171. {
  172. return Translation;
  173. }
  174. /// <summary>
  175. /// Returns a <see cref="RectangleF"/> for the currently
  176. /// visible area in world coordinates.
  177. /// </summary>
  178. /// <returns>A <see cref="RectangleF"/>.</returns>
  179. public RectangleF GetClipRect() {
  180. return new RectangleF(-Translation.X, -Translation.Y,
  181. Allocation.Width / Zoom,
  182. Allocation.Height / Zoom);
  183. }
  184. private Vector MouseToWorld(Vector MousePos)
  185. {
  186. return MousePos / Zoom - Translation;
  187. }
  188. private void CursorChange(Cursor cursor)
  189. {
  190. GdkWindow.Cursor = cursor;
  191. }
  192. /**
  193. * Add gtk adjustments for vertical and horizontal scrolling
  194. * (This is used for scrollbars)
  195. */
  196. public void SetAdjustments(Adjustment hadjustment, Adjustment vadjustment)
  197. {
  198. this.vadjustment = vadjustment;
  199. this.hadjustment = hadjustment;
  200. if(vadjustment != null) {
  201. vadjustment.ValueChanged += OnVAdjustmentChanged;
  202. }
  203. if(hadjustment != null) {
  204. hadjustment.ValueChanged += OnHAdjustmentChanged;
  205. }
  206. UpdateAdjustments();
  207. }
  208. private void OnHAdjustmentChanged(object sender, EventArgs e)
  209. {
  210. SetTranslation(new Vector((float) -hadjustment.Value, Translation.Y));
  211. }
  212. private void OnVAdjustmentChanged(object sender, EventArgs e)
  213. {
  214. SetTranslation(new Vector(Translation.X, (float) -vadjustment.Value));
  215. }
  216. private Adjustment vadjustment;
  217. private Adjustment hadjustment;
  218. protected float minx = -1000, maxx = 1000;
  219. protected float miny = -1000, maxy = 1000;
  220. }