set up the treeview reflections
[hkl.git] / gui / hkl3d-gui-view.vala
blobaadca59e2d2201743f6026af99af92aa4cca6942
1 /*
2 * This file is part of the hkl3d library.
3 * inspired from logo-model.c of the GtkGLExt logo models.
4 * written by Naofumi Yasufuku <naofumi@users.sourceforge.net>
6 * The hkl library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * The hkl library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
19 * Copyright (C) 2010-2011 Synchrotron SOLEIL
20 * L'Orme des Merisiers Saint-Aubin
21 * BP 48 91192 GIF-sur-YVETTE CEDEX
23 * Authors: Oussama Sboui <oussama.sboui@synchrotron-soleil.fr>
24 * Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
27 using GL;
29 public class Hkl3D.Gui.View : Gtk.Object
31 public void set_pos(float x, float y, float z)
33 this.m_Pos[0] = x;
34 this.m_Pos[1] = y;
35 this.m_Pos[2] = z;
38 public void set_quat(float q0, float q1, float q2, float q3)
40 this.m_Quat[0] = q0;
41 this.m_Quat[1] = q1;
42 this.m_Quat[2] = q2;
43 this.m_Quat[3] = q3;
46 public void set_scale(float scale)
48 this.m_Scale = scale;
51 /* members */
52 float m_Pos[3];
53 float m_Quat[4];
54 float m_Scale;
56 float m_BeginX;
57 float m_BeginY;
59 const float NEAR_CLIP = 2.0f;
60 const float FAR_CLIP = 60.0f;
62 const float INIT_POS_X = 0.0f;
63 const float INIT_POS_Y = 0.0f;
64 const float INIT_POS_Z = -30.0f;
66 const float INIT_AXIS_X = 0.0f;
67 const float INIT_AXIS_Y = 0.0f;
68 const float INIT_AXIS_Z = 0.0f;
69 const float INIT_ANGLE = 0.0f;
71 const float INIT_SCALE = 7.0f;
73 const float SCALE_MAX = 50.0f;
74 const float SCALE_MIN = 1.0f;
76 public View()
78 this.m_Scale = INIT_SCALE;
79 this.m_BeginX = 0.0f;
80 this.m_BeginY = 0.0f;
82 this.reset();
85 public void frustum(GLsizei w, GLsizei h)
87 glViewport(0, 0, w, h);
89 glMatrixMode(GL_PROJECTION);
90 glLoadIdentity();
92 if (w > h) {
93 float aspect = (float)(w) / (float)(h);
94 glFrustum(-aspect, aspect, -1.0, 1.0, NEAR_CLIP, FAR_CLIP);
95 }else{
96 float aspect = (float)(h) / (float)(w);
97 glFrustum(-1.0, 1.0, -aspect, aspect, NEAR_CLIP, FAR_CLIP);
100 glMatrixMode(GL_MODELVIEW);
103 public void ortho(int w, int h)
105 glMatrixMode(GL_PROJECTION);
106 glLoadIdentity();
108 glOrtho(-w/40, w/40 , -h/40 , h/40 , -1, 100);
110 glMatrixMode(GL_MODELVIEW);
113 public void xform()
115 /* OpenGL begin */
116 GLfloat m[16];
118 glTranslatef(m_Pos[0], m_Pos[1], m_Pos[2]);
119 glScalef(m_Scale, m_Scale, m_Scale);
120 Trackball.build_rotmatrix(m, m_Quat);
121 glMultMatrixf(m);
122 /* OpenGL end */
126 public void reset()
128 this.m_Pos[0] = this.INIT_POS_X;
129 this.m_Pos[1] = this.INIT_POS_Y;
130 this.m_Pos[2] = this.INIT_POS_Z;
132 float sine = (float)Math.sin(0.5 * INIT_ANGLE * Hkl.DEGTORAD);
133 this.m_Quat[0] = this.INIT_AXIS_X * sine;
134 this.m_Quat[1] = this.INIT_AXIS_Y * sine;
135 this.m_Quat[2] = this.INIT_AXIS_Z * sine;
136 this.m_Quat[3] = (float)Math.cos(0.5 * INIT_ANGLE * Hkl.DEGTORAD);
138 this.m_Scale = this.INIT_SCALE;
141 public bool on_button_press_event(Gdk.EventButton event, Hkl3D.Gui.Scene scene)
143 this.m_BeginX = (float)event.x;
144 this.m_BeginY = (float)event.y;
146 // don't block
147 return false;
150 public bool on_scroll_event(Gdk.EventScroll event, Hkl3D.Gui.Scene scene)
152 bool redraw = false;
154 if(event.direction == Gdk.ScrollDirection.DOWN)
155 m_Scale +=1;
156 else
157 m_Scale -=1;
159 if (m_Scale > this.SCALE_MAX)
160 m_Scale = this.SCALE_MAX;
161 else if (m_Scale < this.SCALE_MIN)
162 m_Scale = this.SCALE_MIN;
163 redraw = true;
165 if (redraw && !scene.BulletDraw_is_enabled())
166 scene.invalidate();
168 return false;
171 public bool on_motion_notify_event(Gdk.EventMotion event,
172 Hkl3D.Gui.Scene scene)
174 float h = scene.allocation.width;
175 float w = scene.allocation.height;
176 float x = (float)event.x;
177 float y = (float)event.y;
178 float d_quat[4];
179 bool redraw = false;
181 // Translation && Rotation.
182 if ((event.state & Gdk.ModifierType.BUTTON1_MASK) == Gdk.ModifierType.BUTTON1_MASK) {
183 if((event.state & Gdk.ModifierType.SHIFT_MASK) == Gdk.ModifierType.SHIFT_MASK){
184 /* shift pressed, translate view */
185 m_Pos[0] += (x - m_BeginX) / (m_Scale * 10);
186 m_Pos[1] -= (y - m_BeginY) / (m_Scale * 10);
187 redraw = true;
188 }else{
189 Trackball.trackball(d_quat,
190 (2.0f * m_BeginX - w) / w,
191 (h - 2.0f * m_BeginY) / h,
192 (2.0f * x - w) / w,
193 (h - 2.0f * y) / h);
194 Trackball.add_quats(d_quat, m_Quat, m_Quat);
195 redraw = true;
199 // Scaling.
200 if ((event.state & Gdk.ModifierType.BUTTON2_MASK) == Gdk.ModifierType.BUTTON2_MASK) {
201 this.m_Scale = this.m_Scale * (1.0f + (y - this.m_BeginY) / h);
202 if (this.m_Scale > this.SCALE_MAX)
203 this.m_Scale = this.SCALE_MAX;
204 else if (this.m_Scale < this.SCALE_MIN)
205 this.m_Scale = this.SCALE_MIN;
206 redraw = true;
209 this.m_BeginX = x;
210 this.m_BeginY = y;
212 if (redraw && !scene.BulletDraw_is_enabled())
213 scene.invalidate();
215 // don't block
216 return false;
218 } //namespace Hkl3dGui