]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/xorrectangle.h
Radiant:
[xonotic/netradiant.git] / libs / gtkutil / xorrectangle.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
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 #if !defined ( INCLUDED_XORRECTANGLE_H )
23 #define INCLUDED_XORRECTANGLE_H
24
25 #include <gtk/gtkwidget.h>
26 #include "math/vector.h"
27
28
29 #include "gtkutil/glwidget.h"
30 #include "igl.h"
31
32 #include <gtk/gtkglwidget.h>
33
34 //#include "stream/stringstream.h"
35
36
37 class rectangle_t
38 {
39 public:
40 rectangle_t()
41         : x( 0 ), y( 0 ), w( 0 ), h( 0 )
42 {}
43 rectangle_t( float _x, float _y, float _w, float _h )
44         : x( _x ), y( _y ), w( _w ), h( _h )
45 {}
46 float x;
47 float y;
48 float w;
49 float h;
50 };
51
52 struct Coord2D
53 {
54         float x, y;
55         Coord2D( float _x, float _y )
56                 : x( _x ), y( _y ){
57         }
58 };
59
60 inline Coord2D coord2d_device2screen( const Coord2D& coord, unsigned int width, unsigned int height ){
61         return Coord2D( ( ( coord.x + 1.0f ) * 0.5f ) * width, ( ( coord.y + 1.0f ) * 0.5f ) * height );
62 }
63
64 inline rectangle_t rectangle_from_area( const float min[2], const float max[2], unsigned int width, unsigned int height ){
65         Coord2D botleft( coord2d_device2screen( Coord2D( min[0], min[1] ), width, height ) );
66         Coord2D topright( coord2d_device2screen( Coord2D( max[0], max[1] ), width, height ) );
67         return rectangle_t( botleft.x, botleft.y, topright.x - botleft.x, topright.y - botleft.y );
68 }
69
70 class XORRectangle
71 {
72 rectangle_t m_rectangle;
73 GtkWidget* m_widget;
74
75 public:
76 XORRectangle( GtkWidget* widget ) : m_widget( widget ){
77 }
78 ~XORRectangle(){
79 }
80 void set( rectangle_t rectangle ){
81         if ( GTK_WIDGET_REALIZED( m_widget ) ) {
82                 if( m_rectangle.w != rectangle.w || m_rectangle.h != rectangle.h ){
83                 //if( !(m_rectangle.w == 0 && m_rectangle.h == 0 && rectangle.w == 0 && rectangle.h == 0) ){
84                 //globalOutputStream() << "m_x" << m_rectangle.x << " m_y" << m_rectangle.y << " m_w" << m_rectangle.w << " m_h" << m_rectangle.h << "\n";
85                 //globalOutputStream() << "__x" << rectangle.x << " __y" << rectangle.y << " __w" << rectangle.w << " __h" << rectangle.h << "\n";
86                         if ( glwidget_make_current( m_widget ) != FALSE ) {
87                                 GlobalOpenGL_debugAssertNoErrors();
88
89                                 gint width, height;
90                                 gdk_gl_drawable_get_size( gtk_widget_get_gl_drawable( m_widget ), &width, &height );
91
92                                 glViewport( 0, 0, width, height );
93                                 glMatrixMode( GL_PROJECTION );
94                                 glLoadIdentity();
95                                 glOrtho( 0, width, 0, height, -100, 100 );
96                                 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
97                                 glDisable( GL_DEPTH_TEST );
98
99                                 glDrawBuffer( GL_FRONT );
100
101                                 glEnable( GL_BLEND );
102                                 glBlendFunc( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
103
104                                 glLineWidth( 2 );
105                                 glColor3f( 1, 1, 1 );
106                                 glDisable( GL_TEXTURE_2D );
107                                 glBegin( GL_LINE_LOOP );
108                                 glVertex2f( m_rectangle.x, m_rectangle.y + m_rectangle.h );
109                                 glVertex2f( m_rectangle.x + m_rectangle.w, m_rectangle.y + m_rectangle.h );
110                                 glVertex2f( m_rectangle.x + m_rectangle.w, m_rectangle.y );
111                                 glVertex2f( m_rectangle.x, m_rectangle.y );
112                                 glEnd();
113
114                                 glBegin( GL_LINE_LOOP );
115                                 glVertex2f( rectangle.x, rectangle.y + rectangle.h );
116                                 glVertex2f( rectangle.x + rectangle.w, rectangle.y + rectangle.h );
117                                 glVertex2f( rectangle.x + rectangle.w, rectangle.y );
118                                 glVertex2f( rectangle.x, rectangle.y );
119                                 glEnd();
120
121                                 glDrawBuffer( GL_BACK );
122                                 GlobalOpenGL_debugAssertNoErrors();
123                                 //glwidget_swap_buffers( m_widget );
124                                 glwidget_make_current( m_widget );
125                         }
126                 }
127                 m_rectangle = rectangle;
128         }
129 }
130 };
131
132
133 #endif