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