2DView.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Copyright (C) 1999-2006 Id Software, Inc. and contributors.
  3. For a list of contributors, see the accompanying CONTRIBUTORS file.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant 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. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. //-----------------------------------------------------------------------------
  18. //
  19. // DESCRIPTION:
  20. // a class to provide basic services for 2D view of a world
  21. // window <-> local 2D space transforms
  22. // snap to grid
  23. // TODO: this one can be placed under an interface, and provided to the editor as a service
  24. #include "StdAfx.h"
  25. static void view_ZoomIn (GtkWidget* widget, gpointer data)
  26. {
  27. ((C2DView*)data)->ZoomIn ();
  28. }
  29. static void view_ZoomOut (GtkWidget* widget, gpointer data)
  30. {
  31. ((C2DView*)data)->ZoomOut ();
  32. }
  33. void C2DView::PreparePaint()
  34. {
  35. g_QglTable.m_pfn_qglClearColor( 0, 0, 0, 0 );
  36. g_QglTable.m_pfn_qglViewport( 0, 0, m_rect.right, m_rect.bottom );
  37. g_QglTable.m_pfn_qglMatrixMode( GL_PROJECTION );
  38. g_QglTable.m_pfn_qglLoadIdentity();
  39. g_QglTable.m_pfn_qglOrtho( m_Mins[0], m_Maxs[0], m_Maxs[1], m_Mins[1], -1, 1 );
  40. }
  41. void C2DView::SpaceForWindow( float c[2], int x, int y)
  42. {
  43. c[0] = ((float)(x))/((float)(m_rect.right-m_rect.left))*(m_Maxs[0]-m_Mins[0])+m_Mins[0];
  44. c[1] = ((float)(y))/((float)(m_rect.bottom-m_rect.top))*(m_Maxs[1]-m_Mins[1])+m_Mins[1];
  45. }
  46. void C2DView::GridForWindow( float c[2], int x, int y)
  47. {
  48. SpaceForWindow( c, x, y );
  49. if ( !m_bDoGrid )
  50. return;
  51. c[0] /= m_GridStep[0];
  52. c[1] /= m_GridStep[1];
  53. c[0] = (float)floor( c[0] + 0.5f );
  54. c[1] = (float)floor( c[1] + 0.5f );
  55. c[0] *= m_GridStep[0];
  56. c[1] *= m_GridStep[1];
  57. }
  58. void C2DView::WindowForSpace( int &x, int &y, const float c[2] )
  59. {
  60. x = m_rect.left + (int)( ((float)(m_rect.right-m_rect.left))*(c[0]-m_Mins[0])/(m_Maxs[0]-m_Mins[0]) );
  61. y = m_rect.top + (int)( ((float)(m_rect.bottom-m_rect.top))*(c[1]-m_Mins[1])/(m_Maxs[1]-m_Mins[1]) );
  62. }
  63. qboolean C2DView::DoesSelect( int x, int y, float c[2] )
  64. {
  65. int xc,yc;
  66. WindowForSpace( xc, yc, c );
  67. if ( abs(xc-x)<=3 && abs(yc-y)<=3 )
  68. return true;
  69. return false;
  70. }
  71. void C2DView::ZoomIn()
  72. {
  73. m_Mins[0] = 0.5f * ( m_Mins[0] - m_Center[0] ) + m_Center[0];
  74. m_Mins[1] = 0.5f * ( m_Mins[1] - m_Center[1] ) + m_Center[1];
  75. m_Maxs[0] = 0.5f * ( m_Maxs[0] - m_Center[0] ) + m_Center[0];
  76. m_Maxs[1] = 0.5f * ( m_Maxs[1] - m_Center[1] ) + m_Center[1];
  77. g_pToolWnd->Redraw ();
  78. }
  79. void C2DView::ZoomOut()
  80. {
  81. m_Mins[0] = 2.0f * ( m_Mins[0] - m_Center[0] ) + m_Center[0];
  82. m_Mins[1] = 2.0f * ( m_Mins[1] - m_Center[1] ) + m_Center[1];
  83. m_Maxs[0] = 2.0f * ( m_Maxs[0] - m_Center[0] ) + m_Center[0];
  84. m_Maxs[1] = 2.0f * ( m_Maxs[1] - m_Center[1] ) + m_Center[1];
  85. g_pToolWnd->Redraw ();
  86. }
  87. bool C2DView::OnRButtonDown (int x, int y)
  88. {
  89. if (ViewState == View_Idle)
  90. {
  91. m_xPosMove = x; // horizontal position of cursor
  92. m_yPosMove = y; // vertical position of cursor
  93. // store
  94. m_MinsMove[0] = m_Mins[0]; m_MinsMove[1] = m_Mins[1];
  95. m_MaxsMove[0] = m_Maxs[0]; m_MaxsMove[1] = m_Maxs[1];
  96. ViewState = View_Move;
  97. // set popup to true
  98. m_bPopup = true;
  99. return true;
  100. }
  101. return false;
  102. }
  103. bool C2DView::OnRButtonUp (int x, int y)
  104. {
  105. if (ViewState == View_Move)
  106. {
  107. // maybe it's time for popup menu
  108. if (m_bPopup)
  109. {
  110. GtkWidget *menu, *item;
  111. menu = gtk_menu_new ();
  112. item = gtk_menu_item_new_with_label ("Validate (RETURN)");
  113. gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (Textool_Validate), NULL);
  114. gtk_widget_show (item);
  115. gtk_menu_append (GTK_MENU (menu), item);
  116. item = gtk_menu_item_new_with_label ("Zoom in (INSERT)");
  117. gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (view_ZoomIn), this);
  118. gtk_widget_show (item);
  119. gtk_menu_append (GTK_MENU (menu), item);
  120. item = gtk_menu_item_new_with_label ("Zoom out (DELETE)");
  121. gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (view_ZoomOut), this);
  122. gtk_widget_show (item);
  123. gtk_menu_append (GTK_MENU (menu), item);
  124. item = gtk_menu_item_new_with_label ("Cancel (ESC)");
  125. gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (Textool_Cancel), NULL);
  126. gtk_widget_show (item);
  127. gtk_menu_append (GTK_MENU (menu), item);
  128. gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 1, GDK_CURRENT_TIME);
  129. }
  130. // back to Idle mode
  131. ViewState = View_Idle;
  132. return true;
  133. }
  134. return false;
  135. }
  136. bool C2DView::OnMouseMove (int xPos, int yPos)
  137. {
  138. if (ViewState == View_Move)
  139. {
  140. float V[2];
  141. // V is the offset
  142. V[0] = ((float)( xPos - m_xPosMove )) * ( m_MaxsMove[0] - m_MinsMove[0] ) / ((float)( m_rect.left - m_rect.right ));
  143. V[1] = ((float)( yPos - m_yPosMove )) * ( m_MaxsMove[1] - m_MinsMove[1] ) / ((float)( m_rect.top - m_rect.bottom ));
  144. // update m_Mins m_Maxs and m_Center
  145. m_Mins[0] = m_MinsMove[0] + V[0];
  146. m_Mins[1] = m_MinsMove[1] + V[1];
  147. m_Maxs[0] = m_MaxsMove[0] + V[0];
  148. m_Maxs[1] = m_MaxsMove[1] + V[1];
  149. m_Center[0] = 0.5f * ( m_Mins[0] + m_Maxs[0] );
  150. m_Center[1] = 0.5f * ( m_Mins[1] + m_Maxs[1] );
  151. // no popup menu if we moved
  152. m_bPopup = false;
  153. // send a repaint message
  154. g_pToolWnd->Redraw ();
  155. return true;
  156. }
  157. return false;
  158. }
  159. bool C2DView::OnKeyDown (char *s)
  160. {
  161. if (ViewState == View_Idle)
  162. {
  163. if (!strcmp(s,"Insert"))
  164. {
  165. ZoomOut();
  166. return true;
  167. }
  168. if (!strcmp(s,"Delete"))
  169. {
  170. ZoomIn();
  171. return true;
  172. }
  173. }
  174. return false;
  175. }