/[escript]/trunk/cusplibrary/cusp/opengl/spy/matrix_data_cursor.h
ViewVC logotype

Annotation of /trunk/cusplibrary/cusp/opengl/spy/matrix_data_cursor.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4955 - (hide annotations)
Tue May 20 04:33:15 2014 UTC (6 years, 5 months ago) by caltinay
Original Path: branches/diaplayground/cusplibrary/cusp/opengl/spy/matrix_data_cursor.h
File MIME type: text/plain
File size: 4181 byte(s)
added pristine copy of cusplibrary (apache license) to be used by ripley.

1 caltinay 4955 /*
2     * Copyright 2008-2013 Steven Dalton
3     *
4     * Licensed under the Apache License, Version 2.0 (the "License");
5     * you may not use this file except in compliance with the License.
6     * You may obtain a copy of the License at
7     *
8     * http://www.apache.org/licenses/LICENSE-2.0
9     *
10     * Unless required by applicable law or agreed to in writing, software
11     * distributed under the License is distributed on an "AS IS" BASIS,
12     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     * See the License for the specific language governing permissions and
14     * limitations under the License.
15     */
16    
17     /**
18     * @file matrix_data_cursor.cc
19     * The definition file for the matrix_data_cursor class.
20     */
21    
22     /*
23     * David Gleich
24     * 22 November 2006
25     * Copyright, Stanford University
26     */
27     #pragma once
28    
29     #include <cusp/opengl/spy/gl_util.h>
30    
31     namespace cusp
32     {
33     namespace opengl
34     {
35     namespace spy
36     {
37    
38     /**
39     * The matrix_data_cursor class handles drawing a data_cursor on the
40     * screen. The cursor is a set of two lines identifying a row and
41     * a column of the matrix.
42     */
43     class matrix_data_cursor
44     {
45     public:
46     matrix_data_cursor(int in_nrows, int in_ncols)
47     : nrows(in_nrows), ncols(in_ncols),
48     state(STATE_NONE),
49     cursor_x((float)in_nrows/2),
50     cursor_y((float)in_nrows/2)
51     {
52     color[0]=1.0f; color[1]=1.0f; color[2]=0.0f;
53     }
54    
55     void set_matrix_size(int in_nrows, int in_ncols)
56     {
57     nrows = in_nrows;
58     ncols = in_ncols;
59     }
60    
61     void draw()
62     {
63     int m = nrows;
64     int n = ncols;
65     int vx = (int)cursor_x;
66     int vy = (int)cursor_y;
67    
68     glColor3f(color[0],color[1],color[2]);
69     glBegin(GL_LINES);
70     glVertex2f(-0.5f,(GLfloat)vy);
71     glVertex2f(n-0.5f,(GLfloat)vy);
72     glEnd();
73     glBegin(GL_LINES);
74     glVertex2f((GLfloat)vx,-0.5f);
75     glVertex2f((GLfloat)vx,(GLfloat)m-0.5f);
76     glEnd();
77     }
78    
79     bool scaled_motion(float x, float y)
80     {
81     if (state == STATE_MOVING)
82     {
83     cursor_x += x;
84     cursor_y += y;
85    
86     bound_cursor();
87    
88     glutPostRedisplay();
89    
90     return (true);
91     }
92    
93     return (false);
94     }
95    
96     bool mouse_click(int button, int button_state, int x, int y)
97     {
98     if (button_state == GLUT_UP)
99     {
100     state = STATE_NONE;
101    
102     return (false);
103     }
104    
105     if (button_state != GLUT_DOWN)
106     {
107     return (false);
108     }
109    
110     // henceforth, we can assume that button_state == GLUT_DOWN
111    
112     int mod_state = glutGetModifiers();
113    
114     if ((button == GLUT_LEFT_BUTTON && mod_state == GLUT_ACTIVE_CTRL) ||
115     (button == GLUT_MIDDLE_BUTTON))
116     {
117     state = STATE_MOVING;
118    
119     double wx,wy;
120     screen_to_world(x,y,&wx, &wy);
121    
122     cursor_x = (float)(wx+0.5);
123     cursor_y = (float)(wy+0.5);
124    
125     bound_cursor();
126     glutPostRedisplay();
127     return (true);
128     }
129    
130     return (false);
131     }
132    
133     int get_x()
134     {
135     return (int)cursor_x;
136     }
137     int get_y()
138     {
139     return (int)cursor_y;
140     }
141    
142     void set_position(int x, int y) {
143     cursor_x = (float)x; cursor_y = (float)y;
144     bound_cursor();
145     glutPostRedisplay();
146     }
147    
148     void set_color(float r, float g, float b) { color[0]=r; color[1]=g; color[2]=b; }
149    
150     private:
151     /*int matrix_data_cursor_nrows;
152     int matrix_data_cursor_ncols;
153    
154     int matrix_data_cursor_x;
155     int matrix_data_cursor_y;
156    
157     enum {
158     MOUSE_DATA_CURSOR_STATE_MOVING,
159     MOUSE_DATA_CURSOR_NONE
160     } mouse_data_cursor_state;*/
161    
162     int nrows;
163     int ncols;
164    
165     float cursor_x;
166     float cursor_y;
167    
168     enum {
169     STATE_MOVING,
170     STATE_NONE
171     } state;
172    
173     void bound_cursor()
174     {
175     if(cursor_x < 0) cursor_x = 0;
176     if(cursor_y < 0) cursor_y = 0;
177     if(cursor_x > ncols-1) cursor_x = (float)(ncols-1);
178     if(cursor_y > nrows-1) cursor_y = (float)(nrows-1);
179     }
180    
181     float color[3];
182     };
183    
184     } // end spy
185     } // end opengl
186     } // end cusp
187    
188     #include <cusp/opengl/spy/detail/matrix_data_cursor.inl>
189    

  ViewVC Help
Powered by ViewVC 1.1.26