2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 // Leonardo Zide (leo@lokigames.com)
30 #include "debugging/debugging.h"
37 #include <gtk/gtklabel.h>
38 #include <gtk/gtkmenuitem.h>
40 #include "generic/callback.h"
41 #include "string/string.h"
42 #include "stream/stringstream.h"
45 #include "eclasslib.h"
47 #include "moduleobserver.h"
49 #include "gtkutil/menu.h"
50 #include "gtkutil/container.h"
51 #include "gtkutil/widget.h"
52 #include "gtkutil/glwidget.h"
56 #include "brushmanip.h"
57 #include "selection.h"
59 #include "camwindow.h"
60 #include "texwindow.h"
61 #include "mainframe.h"
62 #include "preferences.h"
66 #include "windowobservers.h"
75 Vector3 m_ptClip; // the 3d point
84 m_ptClip[0] = m_ptClip[1] = m_ptClip[2] = 0.0;
100 /*! Draw clip/path point with rasterized number label */
101 void Draw(int num, float scale);
102 /*! Draw clip/path point with rasterized string label */
103 void Draw(const char *label, float scale);
106 VIEWTYPE g_clip_viewtype;
107 bool g_bSwitch = true;
108 bool g_clip_useCaulk = false;
112 ClipPoint* g_pMovingClip = 0;
114 /* Drawing clip points */
115 void ClipPoint::Draw(int num, float scale)
117 StringOutputStream label(4);
119 Draw(label.c_str(), scale);
122 void ClipPoint::Draw(const char *label, float scale)
126 glColor3fv(vector3_to_array(g_xywindow_globals.color_clipper));
128 glVertex3fv(vector3_to_array(m_ptClip));
132 float offset = 2.0f / scale;
135 glRasterPos3f (m_ptClip[0] + offset, m_ptClip[1] + offset, m_ptClip[2] + offset);
136 glCallLists (GLsizei(strlen(label)), GL_UNSIGNED_BYTE, label);
139 float fDiff(float f1, float f2)
147 inline double ClipPoint_Intersect(const ClipPoint& clip, const Vector3& point, VIEWTYPE viewtype, float scale)
149 int nDim1 = (viewtype == YZ) ? 1 : 0;
150 int nDim2 = (viewtype == XY) ? 1 : 2;
151 double screenDistanceSquared(vector2_length_squared(Vector2(fDiff(clip.m_ptClip[nDim1], point[nDim1]) * scale, fDiff(clip.m_ptClip[nDim2], point[nDim2]) * scale)));
152 if(screenDistanceSquared < 8*8)
154 return screenDistanceSquared;
159 inline void ClipPoint_testSelect(ClipPoint& clip, const Vector3& point, VIEWTYPE viewtype, float scale, double& bestDistance, ClipPoint*& bestClip)
163 double distance = ClipPoint_Intersect(clip, point, viewtype, scale);
164 if(distance < bestDistance)
166 bestDistance = distance;
172 inline ClipPoint* GlobalClipPoints_Find(const Vector3& point, VIEWTYPE viewtype, float scale)
174 double bestDistance = FLT_MAX;
175 ClipPoint* bestClip = 0;
176 ClipPoint_testSelect(g_Clip1, point, viewtype, scale, bestDistance, bestClip);
177 ClipPoint_testSelect(g_Clip2, point, viewtype, scale, bestDistance, bestClip);
178 ClipPoint_testSelect(g_Clip3, point, viewtype, scale, bestDistance, bestClip);
182 inline void GlobalClipPoints_Draw(float scale)
186 g_Clip1.Draw(1, scale);
188 g_Clip2.Draw(2, scale);
190 g_Clip3.Draw(3, scale);
193 inline bool GlobalClipPoints_valid()
195 return g_Clip1.Set() && g_Clip2.Set();
198 void PlanePointsFromClipPoints(Vector3 planepts[3], const AABB& bounds, int viewtype)
200 ASSERT_MESSAGE(GlobalClipPoints_valid(), "clipper points not initialised");
201 planepts[0] = g_Clip1.m_ptClip;
202 planepts[1] = g_Clip2.m_ptClip;
203 planepts[2] = g_Clip3.m_ptClip;
204 Vector3 maxs(vector3_added(bounds.origin, bounds.extents));
205 Vector3 mins(vector3_subtracted(bounds.origin, bounds.extents));
208 int n = (viewtype == XY) ? 2 : (viewtype == YZ) ? 0 : 1;
209 int x = (n == 0) ? 1 : 0;
210 int y = (n == 2) ? 1 : 2;
212 if (n == 1) // on viewtype XZ, flip clip points
214 planepts[0][n] = maxs[n];
215 planepts[1][n] = maxs[n];
216 planepts[2][x] = g_Clip1.m_ptClip[x];
217 planepts[2][y] = g_Clip1.m_ptClip[y];
218 planepts[2][n] = mins[n];
222 planepts[0][n] = mins[n];
223 planepts[1][n] = mins[n];
224 planepts[2][x] = g_Clip1.m_ptClip[x];
225 planepts[2][y] = g_Clip1.m_ptClip[y];
226 planepts[2][n] = maxs[n];
234 if(!GlobalClipPoints_valid())
236 planepts[0] = Vector3(0, 0, 0);
237 planepts[1] = Vector3(0, 0, 0);
238 planepts[2] = Vector3(0, 0, 0);
239 Scene_BrushSetClipPlane(GlobalSceneGraph(), Plane3(0, 0, 0, 0));
243 AABB bounds(Vector3(0, 0, 0), Vector3(64, 64, 64));
244 PlanePointsFromClipPoints(planepts, bounds, g_clip_viewtype);
247 std::swap(planepts[0], planepts[1]);
249 Scene_BrushSetClipPlane(GlobalSceneGraph(), plane3_for_points(planepts[0], planepts[1], planepts[2]));
251 ClipperChangeNotify();
254 const char* Clip_getShader()
256 return g_clip_useCaulk ? "textures/common/caulk" : TextureBrowser_GetSelectedShader(GlobalTextureBrowser());
261 if (ClipMode() && GlobalClipPoints_valid())
264 AABB bounds(Vector3(0, 0, 0), Vector3(64, 64, 64));
265 PlanePointsFromClipPoints(planepts, bounds, g_clip_viewtype);
266 Scene_BrushSplitByPlane(GlobalSceneGraph(), planepts[0], planepts[1], planepts[2], Clip_getShader(), (!g_bSwitch) ? eFront : eBack);
271 ClipperChangeNotify();
277 if (ClipMode() && GlobalClipPoints_valid())
280 AABB bounds(Vector3(0, 0, 0), Vector3(64, 64, 64));
281 PlanePointsFromClipPoints(planepts, bounds, g_clip_viewtype);
282 Scene_BrushSplitByPlane(GlobalSceneGraph(), planepts[0], planepts[1], planepts[2], Clip_getShader(), eFrontAndBack);
287 ClipperChangeNotify();
293 g_bSwitch = !g_bSwitch;
295 ClipperChangeNotify();
298 void OnClipMode(bool enabled)
304 if(!enabled && g_pMovingClip)
310 ClipperChangeNotify();
315 return GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip;
318 void NewClipPoint(const Vector3& point)
320 if (g_Clip1.Set() == false)
322 g_Clip1.m_ptClip = point;
325 else if (g_Clip2.Set() == false)
327 g_Clip2.m_ptClip = point;
330 else if (g_Clip3.Set() == false)
332 g_Clip3.m_ptClip = point;
340 g_Clip1.m_ptClip = point;
345 ClipperChangeNotify();
350 struct xywindow_globals_private_t
354 // these are in the View > Show menu with Show coordinates
356 bool show_coordinates;
370 xywindow_globals_private_t() :
374 show_coordinates(true),
383 m_bCamXYUpdate(true),
391 xywindow_globals_t g_xywindow_globals;
392 xywindow_globals_private_t g_xywindow_globals_private;
394 const unsigned int RAD_NONE = 0x00;
395 const unsigned int RAD_SHIFT = 0x01;
396 const unsigned int RAD_ALT = 0x02;
397 const unsigned int RAD_CONTROL = 0x04;
398 const unsigned int RAD_PRESS = 0x08;
399 const unsigned int RAD_LBUTTON = 0x10;
400 const unsigned int RAD_MBUTTON = 0x20;
401 const unsigned int RAD_RBUTTON = 0x40;
403 inline ButtonIdentifier button_for_flags(unsigned int flags)
405 if(flags & RAD_LBUTTON)
407 if(flags & RAD_RBUTTON)
408 return c_buttonRight;
409 if(flags & RAD_MBUTTON)
410 return c_buttonMiddle;
411 return c_buttonInvalid;
414 inline ModifierFlags modifiers_for_flags(unsigned int flags)
416 ModifierFlags modifiers = c_modifierNone;
417 if(flags & RAD_SHIFT)
418 modifiers |= c_modifierShift;
419 if(flags & RAD_CONTROL)
420 modifiers |= c_modifierControl;
422 modifiers |= c_modifierAlt;
426 inline unsigned int buttons_for_button_and_modifiers(ButtonIdentifier button, ModifierFlags flags)
428 unsigned int buttons = 0;
430 switch (button.get())
432 case ButtonEnumeration::LEFT: buttons |= RAD_LBUTTON; break;
433 case ButtonEnumeration::MIDDLE: buttons |= RAD_MBUTTON; break;
434 case ButtonEnumeration::RIGHT: buttons |= RAD_RBUTTON; break;
437 if(bitfield_enabled(flags, c_modifierControl))
438 buttons |= RAD_CONTROL;
440 if(bitfield_enabled(flags, c_modifierShift))
441 buttons |= RAD_SHIFT;
443 if(bitfield_enabled(flags, c_modifierAlt))
449 inline unsigned int buttons_for_event_button(GdkEventButton* event)
451 unsigned int flags = 0;
453 switch (event->button)
455 case 1: flags |= RAD_LBUTTON; break;
456 case 2: flags |= RAD_MBUTTON; break;
457 case 3: flags |= RAD_RBUTTON; break;
460 if ((event->state & GDK_CONTROL_MASK) != 0)
461 flags |= RAD_CONTROL;
463 if ((event->state & GDK_SHIFT_MASK) != 0)
466 if((event->state & GDK_MOD1_MASK) != 0)
472 inline unsigned int buttons_for_state(guint state)
474 unsigned int flags = 0;
476 if ((state & GDK_BUTTON1_MASK) != 0)
477 flags |= RAD_LBUTTON;
479 if ((state & GDK_BUTTON2_MASK) != 0)
480 flags |= RAD_MBUTTON;
482 if ((state & GDK_BUTTON3_MASK) != 0)
483 flags |= RAD_RBUTTON;
485 if ((state & GDK_CONTROL_MASK) != 0)
486 flags |= RAD_CONTROL;
488 if ((state & GDK_SHIFT_MASK) != 0)
491 if ((state & GDK_MOD1_MASK) != 0)
498 void XYWnd::SetScale(float f)
506 void XYWnd_ZoomIn(XYWnd* xy)
508 float max_scale = 64;
509 float scale = xy->Scale() * 5.0f / 4.0f;
510 if(scale > max_scale)
512 if(xy->Scale() != max_scale)
514 xy->SetScale (max_scale);
524 // NOTE: the zoom out factor is 4/5, we could think about customizing it
525 // we don't go below a zoom factor corresponding to 10% of the max world size
526 // (this has to be computed against the window size)
527 void XYWnd_ZoomOut(XYWnd* xy)
529 float min_scale = MIN(xy->Width(),xy->Height()) / ( 1.1f * (g_MaxWorldCoord-g_MinWorldCoord));
530 float scale = xy->Scale() * 4.0f / 5.0f;
531 if(scale < min_scale)
533 if(xy->Scale() != min_scale)
535 xy->SetScale (min_scale);
544 VIEWTYPE GlobalXYWnd_getCurrentViewType()
546 ASSERT_NOTNULL(g_pParentWnd);
547 ASSERT_NOTNULL(g_pParentWnd->ActiveXY());
548 return g_pParentWnd->ActiveXY()->GetViewType();
551 // =============================================================================
554 bool g_bCrossHairs = false;
556 GtkMenu* XYWnd::m_mnuDrop = 0;
558 // this is disabled, and broken
559 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=394
564 width = g_pParentWnd->ActiveXY()->Width();
565 height = g_pParentWnd->ActiveXY()->Height();
567 const char* filename;
569 filename = file_dialog(GTK_WIDGET(MainFrame_getWindow()), FALSE, "Save Image", 0, FILTER_BMP);
573 g_pParentWnd->ActiveXY()->MakeCurrent();
574 img = (unsigned char*)malloc (width*height*3);
575 glReadPixels (0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,img);
578 fp = fopen(filename, "wb");
582 unsigned long cmap, bfSize;
586 bfSize = 54 + width*height*3;
588 long byteswritten = 0;
589 long pixoff = 54 + cmap*4;
591 char m1 ='B', m2 ='M';
592 fwrite(&m1, 1, 1, fp); byteswritten++; // B
593 fwrite(&m2, 1, 1, fp); byteswritten++; // M
594 fwrite(&bfSize, 4, 1, fp); byteswritten+=4;// bfSize
595 fwrite(&res, 2, 1, fp); byteswritten+=2;// bfReserved1
596 fwrite(&res, 2, 1, fp); byteswritten+=2;// bfReserved2
597 fwrite(&pixoff, 4, 1, fp); byteswritten+=4;// bfOffBits
599 unsigned long biSize = 40, compress = 0, size = 0;
601 unsigned short planes = 1;
602 fwrite(&biSize, 4, 1, fp); byteswritten+=4;// biSize
603 fwrite(&width, 4, 1, fp); byteswritten+=4;// biWidth
604 fwrite(&height, 4, 1, fp); byteswritten+=4;// biHeight
605 fwrite(&planes, 2, 1, fp); byteswritten+=2;// biPlanes
606 fwrite(&bits, 2, 1, fp); byteswritten+=2;// biBitCount
607 fwrite(&compress, 4, 1, fp);byteswritten+=4;// biCompression
608 fwrite(&size, 4, 1, fp); byteswritten+=4;// biSizeImage
609 fwrite(&pixels, 4, 1, fp); byteswritten+=4;// biXPelsPerMeter
610 fwrite(&pixels, 4, 1, fp); byteswritten+=4;// biYPelsPerMeter
611 fwrite(&cmap, 4, 1, fp); byteswritten+=4;// biClrUsed
612 fwrite(&cmap, 4, 1, fp); byteswritten+=4;// biClrImportant
614 unsigned long widthDW = (((width*24) + 31) / 32 * 4);
615 long row, row_size = width*3;
616 for (row = 0; row < height; row++)
618 unsigned char* buf = img+row*row_size;
622 for (col = 0; col < row_size; col += 3)
624 putc(buf[col+2], fp);
625 putc(buf[col+1], fp);
628 byteswritten += row_size;
631 for (count = row_size; count < widthDW; count++)
633 putc(0, fp); // dummy
648 Timer g_chasemouse_timer;
650 void XYWnd::ChaseMouse()
652 float multiplier = g_chasemouse_timer.elapsed_msec() / 10.0f;
653 Scroll(float_to_integer(multiplier * m_chasemouse_delta_x), float_to_integer(multiplier * -m_chasemouse_delta_y));
655 //globalOutputStream() << "chasemouse: multiplier=" << multiplier << " x=" << m_chasemouse_delta_x << " y=" << m_chasemouse_delta_y << '\n';
657 XY_MouseMoved(m_chasemouse_current_x, m_chasemouse_current_y , getButtonState());
658 g_chasemouse_timer.start();
661 gboolean xywnd_chasemouse(gpointer data)
663 reinterpret_cast<XYWnd*>(data)->ChaseMouse();
667 inline const int& min_int(const int& left, const int& right)
669 return std::min(left, right);
672 bool XYWnd::chaseMouseMotion(int pointx, int pointy)
674 m_chasemouse_delta_x = 0;
675 m_chasemouse_delta_y = 0;
677 if (g_xywindow_globals_private.m_bChaseMouse && getButtonState() == RAD_LBUTTON)
679 const int epsilon = 16;
681 if (pointx < epsilon)
683 m_chasemouse_delta_x = std::max(pointx, 0) - epsilon;
685 else if ((pointx - m_nWidth) > -epsilon)
687 m_chasemouse_delta_x = min_int((pointx - m_nWidth), 0) + epsilon;
690 if (pointy < epsilon)
692 m_chasemouse_delta_y = std::max(pointy, 0) - epsilon;
694 else if ((pointy - m_nHeight) > -epsilon)
696 m_chasemouse_delta_y = min_int((pointy - m_nHeight), 0) + epsilon;
699 if(m_chasemouse_delta_y != 0 || m_chasemouse_delta_x != 0)
701 //globalOutputStream() << "chasemouse motion: x=" << pointx << " y=" << pointy << "... ";
702 m_chasemouse_current_x = pointx;
703 m_chasemouse_current_y = pointy;
704 if(m_chasemouse_handler == 0)
706 //globalOutputStream() << "chasemouse timer start... ";
707 g_chasemouse_timer.start();
708 m_chasemouse_handler = g_idle_add(xywnd_chasemouse, this);
714 if(m_chasemouse_handler != 0)
716 //globalOutputStream() << "chasemouse cancel\n";
717 g_source_remove(m_chasemouse_handler);
718 m_chasemouse_handler = 0;
724 if(m_chasemouse_handler != 0)
726 //globalOutputStream() << "chasemouse cancel\n";
727 g_source_remove(m_chasemouse_handler);
728 m_chasemouse_handler = 0;
734 // =============================================================================
736 Shader* XYWnd::m_state_selected = 0;
738 void xy_update_xor_rectangle(XYWnd& self, rect_t area)
740 if(GTK_WIDGET_VISIBLE(self.GetWidget()))
742 self.m_XORRectangle.set(rectangle_from_area(area.min, area.max, self.Width(), self.Height()));
746 gboolean xywnd_button_press(GtkWidget* widget, GdkEventButton* event, XYWnd* xywnd)
748 if(event->type == GDK_BUTTON_PRESS)
750 g_pParentWnd->SetActiveXY(xywnd);
752 xywnd->ButtonState_onMouseDown(buttons_for_event_button(event));
754 xywnd->onMouseDown(WindowVector(event->x, event->y), button_for_button(event->button), modifiers_for_state(event->state));
759 gboolean xywnd_button_release(GtkWidget* widget, GdkEventButton* event, XYWnd* xywnd)
761 if(event->type == GDK_BUTTON_RELEASE)
763 xywnd->XY_MouseUp(static_cast<int>(event->x), static_cast<int>(event->y), buttons_for_event_button(event));
765 xywnd->ButtonState_onMouseUp(buttons_for_event_button(event));
770 void xywnd_motion(gdouble x, gdouble y, guint state, void* data)
772 if(reinterpret_cast<XYWnd*>(data)->chaseMouseMotion(static_cast<int>(x), static_cast<int>(y)))
776 reinterpret_cast<XYWnd*>(data)->XY_MouseMoved(static_cast<int>(x), static_cast<int>(y), buttons_for_state(state));
779 gboolean xywnd_wheel_scroll(GtkWidget* widget, GdkEventScroll* event, XYWnd* xywnd)
781 if(event->direction == GDK_SCROLL_UP)
785 else if(event->direction == GDK_SCROLL_DOWN)
787 XYWnd_ZoomOut(xywnd);
792 gboolean xywnd_size_allocate(GtkWidget* widget, GtkAllocation* allocation, XYWnd* xywnd)
794 xywnd->m_nWidth = allocation->width;
795 xywnd->m_nHeight = allocation->height;
796 xywnd->updateProjection();
797 xywnd->m_window_observer->onSizeChanged(xywnd->Width(), xywnd->Height());
801 gboolean xywnd_expose(GtkWidget* widget, GdkEventExpose* event, XYWnd* xywnd)
803 if(glwidget_make_current(xywnd->GetWidget()) != FALSE)
805 if(Map_Valid(g_map) && ScreenUpdates_Enabled())
807 GlobalOpenGL_debugAssertNoErrors();
809 GlobalOpenGL_debugAssertNoErrors();
811 xywnd->m_XORRectangle.set(rectangle_t());
813 glwidget_swap_buffers(xywnd->GetWidget());
819 void XYWnd_CameraMoved(XYWnd& xywnd)
821 if(g_xywindow_globals_private.m_bCamXYUpdate)
828 m_gl_widget(glwidget_new(FALSE)),
829 m_deferredDraw(WidgetQueueDrawCaller(*m_gl_widget)),
830 m_deferred_motion(xywnd_motion, this),
832 m_window_observer(NewWindowObserver()),
833 m_XORRectangle(m_gl_widget),
834 m_chasemouse_handler(0)
839 m_bNewBrushDrag = false;
840 m_move_started = false;
841 m_zoom_started = false;
852 m_entityCreate = false;
856 GlobalWindowObservers_add(m_window_observer);
857 GlobalWindowObservers_connectWidget(m_gl_widget);
859 m_window_observer->setRectangleDrawCallback(ReferenceCaller1<XYWnd, rect_t, xy_update_xor_rectangle>(*this));
860 m_window_observer->setView(m_view);
862 gtk_widget_ref(m_gl_widget);
864 gtk_widget_set_events(m_gl_widget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK);
865 GTK_WIDGET_SET_FLAGS(m_gl_widget, GTK_CAN_FOCUS);
867 m_sizeHandler = g_signal_connect(G_OBJECT(m_gl_widget), "size_allocate", G_CALLBACK(xywnd_size_allocate), this);
868 m_exposeHandler = g_signal_connect(G_OBJECT(m_gl_widget), "expose_event", G_CALLBACK(xywnd_expose), this);
870 g_signal_connect(G_OBJECT(m_gl_widget), "button_press_event", G_CALLBACK(xywnd_button_press), this);
871 g_signal_connect(G_OBJECT(m_gl_widget), "button_release_event", G_CALLBACK(xywnd_button_release), this);
872 g_signal_connect(G_OBJECT(m_gl_widget), "motion_notify_event", G_CALLBACK(DeferredMotion::gtk_motion), &m_deferred_motion);
874 g_signal_connect(G_OBJECT(m_gl_widget), "scroll_event", G_CALLBACK(xywnd_wheel_scroll), this);
876 Map_addValidCallback(g_map, DeferredDrawOnMapValidChangedCaller(m_deferredDraw));
881 AddSceneChangeCallback(ReferenceCaller<XYWnd, &XYWnd_Update>(*this));
882 AddCameraMovedCallback(ReferenceCaller<XYWnd, &XYWnd_CameraMoved>(*this));
884 PressedButtons_connect(g_pressedButtons, m_gl_widget);
886 onMouseDown.connectLast(makeSignalHandler3(MouseDownCaller(), *this));
893 gtk_widget_destroy(GTK_WIDGET(m_mnuDrop));
897 g_signal_handler_disconnect(G_OBJECT(m_gl_widget), m_sizeHandler);
898 g_signal_handler_disconnect(G_OBJECT(m_gl_widget), m_exposeHandler);
900 gtk_widget_unref(m_gl_widget);
902 m_window_observer->release();
905 void XYWnd::captureStates()
907 m_state_selected = GlobalShaderCache().capture("$XY_OVERLAY");
910 void XYWnd::releaseStates()
912 GlobalShaderCache().release("$XY_OVERLAY");
915 const Vector3& XYWnd::GetOrigin()
920 void XYWnd::SetOrigin(const Vector3& origin)
926 void XYWnd::Scroll(int x, int y)
928 int nDim1 = (m_viewType == YZ) ? 1 : 0;
929 int nDim2 = (m_viewType == XY) ? 1 : 2;
930 m_vOrigin[nDim1] += x / m_fScale;
931 m_vOrigin[nDim2] += y / m_fScale;
936 unsigned int Clipper_buttons()
941 void XYWnd::DropClipPoint(int pointx, int pointy)
945 XY_ToPoint(pointx, pointy, point);
949 g_clip_viewtype = static_cast<VIEWTYPE>(GetViewType());
950 int nDim = (g_clip_viewtype == YZ ) ? nDim = 0 : ( (g_clip_viewtype == XZ) ? nDim = 1 : nDim = 2 );
951 point[nDim] = mid[nDim];
952 vector3_snap(point, GetGridSize());
956 void XYWnd::Clipper_OnLButtonDown(int x, int y)
958 Vector3 mousePosition;
959 XY_ToPoint(x, y , mousePosition);
960 g_pMovingClip = GlobalClipPoints_Find(mousePosition, (VIEWTYPE)m_viewType, m_fScale);
967 void XYWnd::Clipper_OnLButtonUp(int x, int y)
975 void XYWnd::Clipper_OnMouseMoved(int x, int y)
979 XY_ToPoint(x, y , g_pMovingClip->m_ptClip);
980 XY_SnapToGrid(g_pMovingClip->m_ptClip);
982 ClipperChangeNotify();
986 void XYWnd::Clipper_Crosshair_OnMouseMoved(int x, int y)
988 Vector3 mousePosition;
989 XY_ToPoint(x, y , mousePosition);
990 if(ClipMode() && GlobalClipPoints_Find(mousePosition, (VIEWTYPE)m_viewType, m_fScale) != 0)
993 cursor = gdk_cursor_new (GDK_CROSSHAIR);
994 gdk_window_set_cursor (m_gl_widget->window, cursor);
995 gdk_cursor_unref (cursor);
999 gdk_window_set_cursor (m_gl_widget->window, 0);
1003 unsigned int MoveCamera_buttons()
1005 return RAD_CONTROL | (g_glwindow_globals.m_nMouseType == ETwoButton ? RAD_RBUTTON : RAD_MBUTTON);
1008 void XYWnd_PositionCamera(XYWnd* xywnd, int x, int y, CamWnd& camwnd)
1010 Vector3 origin(Camera_getOrigin(camwnd));
1011 xywnd->XY_ToPoint(x, y, origin);
1012 xywnd->XY_SnapToGrid(origin);
1013 Camera_setOrigin(camwnd, origin);
1016 unsigned int OrientCamera_buttons()
1018 if(g_glwindow_globals.m_nMouseType == ETwoButton)
1019 return RAD_RBUTTON | RAD_SHIFT | RAD_CONTROL;
1023 void XYWnd_OrientCamera(XYWnd* xywnd, int x, int y, CamWnd& camwnd)
1025 Vector3 point = g_vector3_identity;
1026 xywnd->XY_ToPoint(x, y, point);
1027 xywnd->XY_SnapToGrid(point);
1028 vector3_subtract(point, Camera_getOrigin(camwnd));
1030 int n1 = (xywnd->GetViewType() == XY) ? 1 : 2;
1031 int n2 = (xywnd->GetViewType() == YZ) ? 1 : 0;
1032 int nAngle = (xywnd->GetViewType() == XY) ? CAMERA_YAW : CAMERA_PITCH;
1033 if (point[n1] || point[n2])
1035 Vector3 angles(Camera_getAngles(camwnd));
1036 angles[nAngle] = static_cast<float>(radians_to_degrees(atan2 (point[n1], point[n2])));
1037 Camera_setAngles(camwnd, angles);
1046 unsigned int NewBrushDrag_buttons()
1051 void XYWnd::NewBrushDrag_Begin(int x, int y)
1054 m_nNewBrushPressx = x;
1055 m_nNewBrushPressy = y;
1057 m_bNewBrushDrag = true;
1058 GlobalUndoSystem().start();
1061 void XYWnd::NewBrushDrag_End(int x, int y)
1063 if(m_NewBrushDrag != 0)
1065 GlobalUndoSystem().finish("brushDragNew");
1069 void XYWnd::NewBrushDrag(int x, int y)
1072 XY_ToPoint(m_nNewBrushPressx, m_nNewBrushPressy, mins);
1073 XY_SnapToGrid(mins);
1074 XY_ToPoint(x, y, maxs);
1075 XY_SnapToGrid(maxs);
1077 int nDim = (m_viewType == XY) ? 2 : (m_viewType == YZ) ? 0 : 1;
1079 mins[nDim] = float_snapped(Select_getWorkZone().d_work_min[nDim], GetGridSize());
1080 maxs[nDim] = float_snapped(Select_getWorkZone().d_work_max[nDim], GetGridSize());
1082 if (maxs[nDim] <= mins[nDim])
1083 maxs[nDim] = mins[nDim] + GetGridSize();
1085 for(int i=0 ; i<3 ; i++)
1087 if (mins[i] == maxs[i])
1088 return; // don't create a degenerate brush
1089 if (mins[i] > maxs[i])
1091 float temp = mins[i];
1097 if(m_NewBrushDrag == 0)
1099 NodeSmartReference node(GlobalBrushCreator().createBrush());
1100 Node_getTraversable(Map_FindOrInsertWorldspawn(g_map))->insert(node);
1102 scene::Path brushpath(makeReference(GlobalSceneGraph().root()));
1103 brushpath.push(makeReference(*Map_GetWorldspawn(g_map)));
1104 brushpath.push(makeReference(node.get()));
1105 selectPath(brushpath, true);
1107 m_NewBrushDrag = node.get_pointer();
1110 Scene_BrushResize_Selected(GlobalSceneGraph(), aabb_for_minmax(mins, maxs), TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
1113 void entitycreate_activated(GtkWidget* item)
1115 g_pParentWnd->ActiveXY()->OnEntityCreate(gtk_label_get_text(GTK_LABEL(GTK_BIN(item)->child)));
1118 void EntityClassMenu_addItem(GtkMenu* menu, const char* name)
1120 GtkMenuItem* item = GTK_MENU_ITEM(gtk_menu_item_new_with_label(name));
1121 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(entitycreate_activated), item);
1122 gtk_widget_show(GTK_WIDGET(item));
1123 menu_add_item(menu, item);
1126 class EntityClassMenuInserter : public EntityClassVisitor
1128 typedef std::pair<GtkMenu*, CopiedString> MenuPair;
1129 typedef std::vector<MenuPair> MenuStack;
1131 CopiedString m_previous;
1133 EntityClassMenuInserter(GtkMenu* menu)
1136 m_stack.push_back(MenuPair(menu, ""));
1138 ~EntityClassMenuInserter()
1140 if(!string_empty(m_previous.c_str()))
1142 addItem(m_previous.c_str(), "");
1145 void visit(EntityClass* e)
1147 ASSERT_MESSAGE(!string_empty(e->name()), "entity-class has no name");
1148 if(!string_empty(m_previous.c_str()))
1150 addItem(m_previous.c_str(), e->name());
1152 m_previous = e->name();
1154 void pushMenu(const CopiedString& name)
1156 GtkMenuItem* item = GTK_MENU_ITEM(gtk_menu_item_new_with_label(name.c_str()));
1157 gtk_widget_show(GTK_WIDGET(item));
1158 container_add_widget(GTK_CONTAINER(m_stack.back().first), GTK_WIDGET(item));
1160 GtkMenu* submenu = GTK_MENU(gtk_menu_new());
1161 gtk_menu_item_set_submenu(item, GTK_WIDGET(submenu));
1163 m_stack.push_back(MenuPair(submenu, name));
1169 void addItem(const char* name, const char* next)
1171 const char* underscore = strchr(name, '_');
1173 if(underscore != 0 && underscore != name)
1175 bool nextEqual = string_equal_n(name, next, (underscore + 1) - name);
1176 const char* parent = m_stack.back().second.c_str();
1178 if(!string_empty(parent)
1179 && string_length(parent) == std::size_t(underscore - name)
1180 && string_equal_n(name, parent, underscore - name)) // this is a child
1185 if(m_stack.size() == 2)
1189 pushMenu(CopiedString(StringRange(name, underscore)));
1191 else if(m_stack.size() == 2)
1196 else if(m_stack.size() == 2)
1201 EntityClassMenu_addItem(m_stack.back().first, name);
1205 void XYWnd::OnContextMenu()
1207 if (g_xywindow_globals.m_bRightClick == false)
1210 if (m_mnuDrop == 0) // first time, load it up
1212 GtkMenu* menu = m_mnuDrop = GTK_MENU(gtk_menu_new());
1214 EntityClassMenuInserter inserter(menu);
1215 GlobalEntityClassManager().forEach(inserter);
1218 gtk_menu_popup(m_mnuDrop, 0, 0, 0, 0, 1, GDK_CURRENT_TIME);
1221 FreezePointer g_xywnd_freezePointer;
1223 unsigned int Move_buttons()
1228 void XYWnd_moveDelta(int x, int y, unsigned int state, void* data)
1230 reinterpret_cast<XYWnd*>(data)->EntityCreate_MouseMove(x, y);
1231 reinterpret_cast<XYWnd*>(data)->Scroll(-x, y);
1234 gboolean XYWnd_Move_focusOut(GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd)
1240 void XYWnd::Move_Begin()
1246 m_move_started = true;
1247 g_xywnd_freezePointer.freeze_pointer(m_parent != 0 ? m_parent : MainFrame_getWindow(), XYWnd_moveDelta, this);
1248 m_move_focusOut = g_signal_connect(G_OBJECT(m_gl_widget), "focus_out_event", G_CALLBACK(XYWnd_Move_focusOut), this);
1251 void XYWnd::Move_End()
1253 m_move_started = false;
1254 g_xywnd_freezePointer.unfreeze_pointer(m_parent != 0 ? m_parent : MainFrame_getWindow());
1255 g_signal_handler_disconnect(G_OBJECT(m_gl_widget), m_move_focusOut);
1258 unsigned int Zoom_buttons()
1260 return RAD_RBUTTON | RAD_SHIFT;
1265 void XYWnd_zoomDelta(int x, int y, unsigned int state, void* data)
1271 while(abs(g_dragZoom) > 8)
1275 XYWnd_ZoomOut(reinterpret_cast<XYWnd*>(data));
1280 XYWnd_ZoomIn(reinterpret_cast<XYWnd*>(data));
1287 gboolean XYWnd_Zoom_focusOut(GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd)
1293 void XYWnd::Zoom_Begin()
1299 m_zoom_started = true;
1301 g_xywnd_freezePointer.freeze_pointer(m_parent != 0 ? m_parent : MainFrame_getWindow(), XYWnd_zoomDelta, this);
1302 m_zoom_focusOut = g_signal_connect(G_OBJECT(m_gl_widget), "focus_out_event", G_CALLBACK(XYWnd_Zoom_focusOut), this);
1305 void XYWnd::Zoom_End()
1307 m_zoom_started = false;
1308 g_xywnd_freezePointer.unfreeze_pointer(m_parent != 0 ? m_parent : MainFrame_getWindow());
1309 g_signal_handler_disconnect(G_OBJECT(m_gl_widget), m_zoom_focusOut);
1312 // makes sure the selected brush or camera is in view
1313 void XYWnd::PositionView(const Vector3& position)
1315 int nDim1 = (m_viewType == YZ) ? 1 : 0;
1316 int nDim2 = (m_viewType == XY) ? 1 : 2;
1318 m_vOrigin[nDim1] = position[nDim1];
1319 m_vOrigin[nDim2] = position[nDim2];
1323 XYWnd_Update(*this);
1326 void XYWnd::SetViewType(VIEWTYPE viewType)
1328 m_viewType = viewType;
1333 gtk_window_set_title(m_parent, ViewType_getTitle(m_viewType));
1338 inline WindowVector WindowVector_forInteger(int x, int y)
1340 return WindowVector(static_cast<float>(x), static_cast<float>(y));
1343 void XYWnd::mouseDown(const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers)
1345 XY_MouseDown(static_cast<int>(position.x()), static_cast<int>(position.y()), buttons_for_button_and_modifiers(button, modifiers));
1347 void XYWnd::XY_MouseDown (int x, int y, unsigned int buttons)
1349 if(buttons == Move_buttons())
1352 EntityCreate_MouseDown(x, y);
1354 else if(buttons == Zoom_buttons())
1358 else if(ClipMode() && buttons == Clipper_buttons())
1360 Clipper_OnLButtonDown(x, y);
1362 else if(buttons == NewBrushDrag_buttons() && GlobalSelectionSystem().countSelected() == 0)
1364 NewBrushDrag_Begin(x, y);
1366 // control mbutton = move camera
1367 else if (buttons == MoveCamera_buttons())
1369 XYWnd_PositionCamera(this, x, y, *g_pParentWnd->GetCamWnd());
1371 // mbutton = angle camera
1372 else if(buttons == OrientCamera_buttons())
1374 XYWnd_OrientCamera(this, x, y, *g_pParentWnd->GetCamWnd());
1378 m_window_observer->onMouseDown(WindowVector_forInteger(x, y), button_for_flags(buttons), modifiers_for_flags(buttons));
1382 void XYWnd::XY_MouseUp(int x, int y, unsigned int buttons)
1387 EntityCreate_MouseUp(x, y);
1389 else if(m_zoom_started)
1393 else if (ClipMode() && buttons == Clipper_buttons())
1395 Clipper_OnLButtonUp(x, y);
1397 else if (m_bNewBrushDrag)
1399 m_bNewBrushDrag = false;
1400 NewBrushDrag_End(x, y);
1404 m_window_observer->onMouseUp(WindowVector_forInteger(x, y), button_for_flags(buttons), modifiers_for_flags(buttons));
1408 void XYWnd::XY_MouseMoved (int x, int y, unsigned int buttons)
1410 // rbutton = drag xy origin
1415 else if(m_zoom_started)
1419 else if (ClipMode() && g_pMovingClip != 0)
1421 Clipper_OnMouseMoved(x, y);
1423 // lbutton without selection = drag new brush
1424 else if (m_bNewBrushDrag)
1429 // control mbutton = move camera
1430 else if (getButtonState() == MoveCamera_buttons())
1432 XYWnd_PositionCamera(this, x, y, *g_pParentWnd->GetCamWnd());
1435 // mbutton = angle camera
1436 else if (getButtonState() == OrientCamera_buttons())
1438 XYWnd_OrientCamera(this, x, y, *g_pParentWnd->GetCamWnd());
1443 m_window_observer->onMouseMotion(WindowVector_forInteger(x, y), modifiers_for_flags(buttons));
1445 m_mousePosition[0] = m_mousePosition[1] = m_mousePosition[2] = 0.0;
1446 XY_ToPoint(x, y , m_mousePosition);
1447 XY_SnapToGrid(m_mousePosition);
1449 StringOutputStream status(64);
1450 status << "x:: " << FloatFormat(m_mousePosition[0], 6, 1)
1451 << " y:: " << FloatFormat(m_mousePosition[1], 6, 1)
1452 << " z:: " << FloatFormat(m_mousePosition[2], 6, 1);
1453 g_pParentWnd->SetStatusText(g_pParentWnd->m_position_status, status.c_str());
1457 XYWnd_Update(*this);
1460 Clipper_Crosshair_OnMouseMoved(x, y);
1464 void XYWnd::EntityCreate_MouseDown(int x, int y)
1466 m_entityCreate = true;
1467 m_entityCreate_x = x;
1468 m_entityCreate_y = y;
1471 void XYWnd::EntityCreate_MouseMove(int x, int y)
1473 if(m_entityCreate && (m_entityCreate_x != x || m_entityCreate_y != y))
1475 m_entityCreate = false;
1479 void XYWnd::EntityCreate_MouseUp(int x, int y)
1483 m_entityCreate = false;
1488 inline float screen_normalised(int pos, unsigned int size)
1490 return ((2.0f * pos) / size) - 1.0f;
1493 inline float normalised_to_world(float normalised, float world_origin, float normalised2world_scale)
1495 return world_origin + normalised * normalised2world_scale;
1499 // TTimo: watch it, this doesn't init one of the 3 coords
1500 void XYWnd::XY_ToPoint (int x, int y, Vector3& point)
1502 float normalised2world_scale_x = m_nWidth / 2 / m_fScale;
1503 float normalised2world_scale_y = m_nHeight / 2 / m_fScale;
1504 if (m_viewType == XY)
1506 point[0] = normalised_to_world(screen_normalised(x, m_nWidth), m_vOrigin[0], normalised2world_scale_x);
1507 point[1] = normalised_to_world(-screen_normalised(y, m_nHeight), m_vOrigin[1], normalised2world_scale_y);
1509 else if (m_viewType == YZ)
1511 point[1] = normalised_to_world(screen_normalised(x, m_nWidth), m_vOrigin[1], normalised2world_scale_x);
1512 point[2] = normalised_to_world(-screen_normalised(y, m_nHeight), m_vOrigin[2], normalised2world_scale_y);
1516 point[0] = normalised_to_world(screen_normalised(x, m_nWidth), m_vOrigin[0], normalised2world_scale_x);
1517 point[2] = normalised_to_world(-screen_normalised(y, m_nHeight), m_vOrigin[2], normalised2world_scale_y);
1521 void XYWnd::XY_SnapToGrid(Vector3& point)
1523 if (m_viewType == XY)
1525 point[0] = float_snapped(point[0], GetGridSize());
1526 point[1] = float_snapped(point[1], GetGridSize());
1528 else if (m_viewType == YZ)
1530 point[1] = float_snapped(point[1], GetGridSize());
1531 point[2] = float_snapped(point[2], GetGridSize());
1535 point[0] = float_snapped(point[0], GetGridSize());
1536 point[2] = float_snapped(point[2], GetGridSize());
1541 ============================================================================
1545 ============================================================================
1554 double two_to_the_power(int power)
1556 return pow(2.0f, power);
1559 void XYWnd::XY_DrawGrid()
1561 float x, y, xb, xe, yb, ye;
1564 float step, minor_step, stepx, stepy;
1565 step = minor_step = stepx = stepy = GetGridSize();
1567 int minor_power = Grid_getPower();
1570 while((minor_step * m_fScale) <= 4.0f) // make sure minor grid spacing is at least 4 pixels on the screen
1575 int power = minor_power;
1576 while((power % 3) != 0 || (step * m_fScale) <= 32.0f) // make sure major grid spacing is at least 32 pixels on the screen
1579 step = float(two_to_the_power(power));
1581 mask = (1 << (power - minor_power)) - 1;
1582 while ((stepx * m_fScale) <= 32.0f) // text step x must be at least 32
1584 while ((stepy * m_fScale) <= 32.0f) // text step y must be at least 32
1588 glDisable(GL_TEXTURE_2D);
1589 glDisable(GL_TEXTURE_1D);
1590 glDisable(GL_DEPTH_TEST);
1591 glDisable(GL_BLEND);
1594 w = (m_nWidth / 2 / m_fScale);
1595 h = (m_nHeight / 2 / m_fScale);
1597 int nDim1 = (m_viewType == YZ) ? 1 : 0;
1598 int nDim2 = (m_viewType == XY) ? 1 : 2;
1600 xb = m_vOrigin[nDim1] - w;
1601 if (xb < region_mins[nDim1])
1602 xb = region_mins[nDim1];
1603 xb = step * floor (xb/step);
1605 xe = m_vOrigin[nDim1] + w;
1606 if (xe > region_maxs[nDim1])
1607 xe = region_maxs[nDim1];
1608 xe = step * ceil (xe/step);
1610 yb = m_vOrigin[nDim2] - h;
1611 if (yb < region_mins[nDim2])
1612 yb = region_mins[nDim2];
1613 yb = step * floor (yb/step);
1615 ye = m_vOrigin[nDim2] + h;
1616 if (ye > region_maxs[nDim2])
1617 ye = region_maxs[nDim2];
1618 ye = step * ceil (ye/step);
1620 #define COLORS_DIFFER(a,b) \
1621 ((a)[0] != (b)[0] || \
1622 (a)[1] != (b)[1] || \
1626 // draw minor blocks
1627 if (g_xywindow_globals_private.d_showgrid)
1629 if (COLORS_DIFFER(g_xywindow_globals.color_gridminor, g_xywindow_globals.color_gridback))
1631 glColor3fv(vector3_to_array(g_xywindow_globals.color_gridminor));
1635 for (x = xb ; x < xe ; x += minor_step, ++i)
1644 for (y = yb ; y < ye ; y += minor_step, ++i)
1655 // draw major blocks
1656 if (COLORS_DIFFER(g_xywindow_globals.color_gridmajor, g_xywindow_globals.color_gridback))
1658 glColor3fv(vector3_to_array(g_xywindow_globals.color_gridmajor));
1661 for (x=xb ; x<=xe ; x+=step)
1666 for (y=yb ; y<=ye ; y+=step)
1675 // draw coordinate text if needed
1676 if ( g_xywindow_globals_private.show_coordinates)
1678 glColor3fv(vector3_to_array(g_xywindow_globals.color_gridtext));
1679 float offx = m_vOrigin[nDim2] + h - 6 / m_fScale, offy = m_vOrigin[nDim1] - w + 1 / m_fScale;
1680 for (x = xb - fmod(xb, stepx); x <= xe ; x += stepx)
1682 glRasterPos2f (x, offx);
1683 sprintf (text, "%g", x);
1684 GlobalOpenGL().drawString(text);
1686 for (y = yb - fmod(yb, stepy); y <= ye ; y += stepy)
1688 glRasterPos2f (offy, y);
1689 sprintf (text, "%g", y);
1690 GlobalOpenGL().drawString(text);
1694 glColor3fv(vector3_to_array(g_xywindow_globals.color_viewname));
1696 // we do this part (the old way) only if show_axis is disabled
1697 if (!g_xywindow_globals_private.show_axis)
1699 glRasterPos2f ( m_vOrigin[nDim1] - w + 35 / m_fScale, m_vOrigin[nDim2] + h - 20 / m_fScale );
1701 GlobalOpenGL().drawString(ViewType_getTitle(m_viewType));
1705 if ( g_xywindow_globals_private.show_axis)
1707 const char g_AxisName[3] = { 'X', 'Y', 'Z' };
1709 const Vector3& colourX = (m_viewType == YZ) ? g_xywindow_globals.AxisColorY : g_xywindow_globals.AxisColorX;
1710 const Vector3& colourY = (m_viewType == XY) ? g_xywindow_globals.AxisColorY : g_xywindow_globals.AxisColorZ;
1712 // draw two lines with corresponding axis colors to highlight current view
1713 // horizontal line: nDim1 color
1715 glBegin( GL_LINES );
1716 glColor3fv (vector3_to_array(colourX));
1717 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1718 glVertex2f( m_vOrigin[nDim1] - w + 65 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1720 glVertex2f( 32 / m_fScale, 0 );
1721 glColor3fv (vector3_to_array(colourY));
1722 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1723 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 20 / m_fScale );
1725 glVertex2f( 0, 32 / m_fScale );
1728 // now print axis symbols
1729 glColor3fv (vector3_to_array(colourX));
1730 glRasterPos2f ( m_vOrigin[nDim1] - w + 55 / m_fScale, m_vOrigin[nDim2] + h - 55 / m_fScale );
1731 GlobalOpenGL().drawChar(g_AxisName[nDim1]);
1732 glRasterPos2f (28 / m_fScale, -10 / m_fScale );
1733 GlobalOpenGL().drawChar(g_AxisName[nDim1]);
1734 glColor3fv (vector3_to_array(colourY));
1735 glRasterPos2f ( m_vOrigin[nDim1] - w + 25 / m_fScale, m_vOrigin[nDim2] + h - 30 / m_fScale );
1736 GlobalOpenGL().drawChar(g_AxisName[nDim2]);
1737 glRasterPos2f ( -10 / m_fScale, 28 / m_fScale );
1738 GlobalOpenGL().drawChar(g_AxisName[nDim2]);
1742 // show current work zone?
1743 // the work zone is used to place dropped points and brushes
1744 if (g_xywindow_globals_private.d_show_work)
1746 glColor3f( 1.0f, 0.0f, 0.0f );
1747 glBegin( GL_LINES );
1748 glVertex2f( xb, Select_getWorkZone().d_work_min[nDim2] );
1749 glVertex2f( xe, Select_getWorkZone().d_work_min[nDim2] );
1750 glVertex2f( xb, Select_getWorkZone().d_work_max[nDim2] );
1751 glVertex2f( xe, Select_getWorkZone().d_work_max[nDim2] );
1752 glVertex2f( Select_getWorkZone().d_work_min[nDim1], yb );
1753 glVertex2f( Select_getWorkZone().d_work_min[nDim1], ye );
1754 glVertex2f( Select_getWorkZone().d_work_max[nDim1], yb );
1755 glVertex2f( Select_getWorkZone().d_work_max[nDim1], ye );
1765 void XYWnd::XY_DrawBlockGrid()
1767 if(Map_FindWorldspawn(g_map) == 0)
1771 const char *value = Node_getEntity(*Map_GetWorldspawn(g_map))->getKeyValue("_blocksize" );
1773 sscanf( value, "%i", &g_xywindow_globals_private.blockSize );
1775 if (!g_xywindow_globals_private.blockSize || g_xywindow_globals_private.blockSize > 65536 || g_xywindow_globals_private.blockSize < 1024)
1776 // don't use custom blocksize if it is less than the default, or greater than the maximum world coordinate
1777 g_xywindow_globals_private.blockSize = 1024;
1779 float x, y, xb, xe, yb, ye;
1783 glDisable(GL_TEXTURE_2D);
1784 glDisable(GL_TEXTURE_1D);
1785 glDisable(GL_DEPTH_TEST);
1786 glDisable(GL_BLEND);
1788 w = (m_nWidth / 2 / m_fScale);
1789 h = (m_nHeight / 2 / m_fScale);
1791 int nDim1 = (m_viewType == YZ) ? 1 : 0;
1792 int nDim2 = (m_viewType == XY) ? 1 : 2;
1794 xb = m_vOrigin[nDim1] - w;
1795 if (xb < region_mins[nDim1])
1796 xb = region_mins[nDim1];
1797 xb = static_cast<float>(g_xywindow_globals_private.blockSize * floor (xb/g_xywindow_globals_private.blockSize));
1799 xe = m_vOrigin[nDim1] + w;
1800 if (xe > region_maxs[nDim1])
1801 xe = region_maxs[nDim1];
1802 xe = static_cast<float>(g_xywindow_globals_private.blockSize * ceil (xe/g_xywindow_globals_private.blockSize));
1804 yb = m_vOrigin[nDim2] - h;
1805 if (yb < region_mins[nDim2])
1806 yb = region_mins[nDim2];
1807 yb = static_cast<float>(g_xywindow_globals_private.blockSize * floor (yb/g_xywindow_globals_private.blockSize));
1809 ye = m_vOrigin[nDim2] + h;
1810 if (ye > region_maxs[nDim2])
1811 ye = region_maxs[nDim2];
1812 ye = static_cast<float>(g_xywindow_globals_private.blockSize * ceil (ye/g_xywindow_globals_private.blockSize));
1814 // draw major blocks
1816 glColor3fv(vector3_to_array(g_xywindow_globals.color_gridblock));
1821 for (x=xb ; x<=xe ; x+=g_xywindow_globals_private.blockSize)
1827 if (m_viewType == XY)
1829 for (y=yb ; y<=ye ; y+=g_xywindow_globals_private.blockSize)
1839 // draw coordinate text if needed
1841 if (m_viewType == XY && m_fScale > .1)
1843 for (x=xb ; x<xe ; x+=g_xywindow_globals_private.blockSize)
1844 for (y=yb ; y<ye ; y+=g_xywindow_globals_private.blockSize)
1846 glRasterPos2f (x+(g_xywindow_globals_private.blockSize/2), y+(g_xywindow_globals_private.blockSize/2));
1847 sprintf (text, "%i,%i",(int)floor(x/g_xywindow_globals_private.blockSize), (int)floor(y/g_xywindow_globals_private.blockSize) );
1848 GlobalOpenGL().drawString(text);
1852 glColor4f(0, 0, 0, 0);
1855 void XYWnd::DrawCameraIcon(const Vector3& origin, const Vector3& angles)
1857 float x, y, fov, box;
1860 fov = 48 / m_fScale;
1861 box = 16 / m_fScale;
1863 if (m_viewType == XY)
1867 a = degrees_to_radians(angles[CAMERA_YAW]);
1869 else if (m_viewType == YZ)
1873 a = degrees_to_radians(angles[CAMERA_PITCH]);
1879 a = degrees_to_radians(angles[CAMERA_PITCH]);
1882 glColor3f (0.0, 0.0, 1.0);
1883 glBegin(GL_LINE_STRIP);
1884 glVertex3f (x-box,y,0);
1885 glVertex3f (x,y+(box/2),0);
1886 glVertex3f (x+box,y,0);
1887 glVertex3f (x,y-(box/2),0);
1888 glVertex3f (x-box,y,0);
1889 glVertex3f (x+box,y,0);
1892 glBegin(GL_LINE_STRIP);
1893 glVertex3f (x + static_cast<float>(fov*cos(a+c_pi/4)), y + static_cast<float>(fov*sin(a+c_pi/4)), 0);
1894 glVertex3f (x, y, 0);
1895 glVertex3f (x + static_cast<float>(fov*cos(a-c_pi/4)), y + static_cast<float>(fov*sin(a-c_pi/4)), 0);
1901 float Betwixt(float f1, float f2)
1904 return f2 + ((f1 - f2) / 2);
1906 return f1 + ((f2 - f1) / 2);
1910 // can be greatly simplified but per usual i am in a hurry
1911 // which is not an excuse, just a fact
1912 void XYWnd::PaintSizeInfo(int nDim1, int nDim2, Vector3& vMinBounds, Vector3& vMaxBounds)
1914 if(vector3_equal(vMinBounds, vMaxBounds))
1918 const char* g_pDimStrings[] = {"x:", "y:", "z:"};
1919 typedef const char* OrgStrings[2];
1920 const OrgStrings g_pOrgStrings[] = { { "x:", "y:", }, { "x:", "z:", }, { "y:", "z:", } };
1922 Vector3 vSize(vector3_subtracted(vMaxBounds, vMinBounds));
1924 glColor3f(g_xywindow_globals.color_selbrushes[0] * .65f,
1925 g_xywindow_globals.color_selbrushes[1] * .65f,
1926 g_xywindow_globals.color_selbrushes[2] * .65f);
1928 StringOutputStream dimensions(16);
1930 if (m_viewType == XY)
1934 glVertex3f(vMinBounds[nDim1], vMinBounds[nDim2] - 6.0f / m_fScale, 0.0f);
1935 glVertex3f(vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f);
1937 glVertex3f(vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f);
1938 glVertex3f(vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f);
1940 glVertex3f(vMaxBounds[nDim1], vMinBounds[nDim2] - 6.0f / m_fScale, 0.0f);
1941 glVertex3f(vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f);
1944 glVertex3f(vMaxBounds[nDim1] + 6.0f / m_fScale, vMinBounds[nDim2], 0.0f);
1945 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, vMinBounds[nDim2], 0.0f);
1947 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, vMinBounds[nDim2], 0.0f);
1948 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, vMaxBounds[nDim2], 0.0f);
1950 glVertex3f(vMaxBounds[nDim1] + 6.0f / m_fScale, vMaxBounds[nDim2], 0.0f);
1951 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, vMaxBounds[nDim2], 0.0f);
1955 glRasterPos3f (Betwixt(vMinBounds[nDim1], vMaxBounds[nDim1]), vMinBounds[nDim2] - 20.0f / m_fScale, 0.0f);
1956 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
1957 GlobalOpenGL().drawString(dimensions.c_str());
1960 glRasterPos3f (vMaxBounds[nDim1] + 16.0f / m_fScale, Betwixt(vMinBounds[nDim2], vMaxBounds[nDim2]), 0.0f);
1961 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
1962 GlobalOpenGL().drawString(dimensions.c_str());
1965 glRasterPos3f (vMinBounds[nDim1] + 4, vMaxBounds[nDim2] + 8 / m_fScale, 0.0f);
1966 dimensions << "(" << g_pOrgStrings[0][0] << vMinBounds[nDim1] << " " << g_pOrgStrings[0][1] << vMaxBounds[nDim2] << ")";
1967 GlobalOpenGL().drawString(dimensions.c_str());
1969 else if (m_viewType == XZ)
1973 glVertex3f(vMinBounds[nDim1], 0, vMinBounds[nDim2] - 6.0f / m_fScale);
1974 glVertex3f(vMinBounds[nDim1], 0, vMinBounds[nDim2] - 10.0f / m_fScale);
1976 glVertex3f(vMinBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f / m_fScale);
1977 glVertex3f(vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f / m_fScale);
1979 glVertex3f(vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 6.0f / m_fScale);
1980 glVertex3f(vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f / m_fScale);
1983 glVertex3f(vMaxBounds[nDim1] + 6.0f / m_fScale, 0,vMinBounds[nDim2]);
1984 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, 0,vMinBounds[nDim2]);
1986 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, 0,vMinBounds[nDim2]);
1987 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, 0,vMaxBounds[nDim2]);
1989 glVertex3f(vMaxBounds[nDim1] + 6.0f / m_fScale, 0,vMaxBounds[nDim2]);
1990 glVertex3f(vMaxBounds[nDim1] + 10.0f / m_fScale, 0,vMaxBounds[nDim2]);
1994 glRasterPos3f (Betwixt(vMinBounds[nDim1], vMaxBounds[nDim1]), 0, vMinBounds[nDim2] - 20.0f / m_fScale);
1995 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
1996 GlobalOpenGL().drawString(dimensions.c_str());
1999 glRasterPos3f (vMaxBounds[nDim1] + 16.0f / m_fScale, 0, Betwixt(vMinBounds[nDim2], vMaxBounds[nDim2]));
2000 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
2001 GlobalOpenGL().drawString(dimensions.c_str());
2004 glRasterPos3f (vMinBounds[nDim1] + 4, 0, vMaxBounds[nDim2] + 8 / m_fScale);
2005 dimensions << "(" << g_pOrgStrings[1][0] << vMinBounds[nDim1] << " " << g_pOrgStrings[1][1] << vMaxBounds[nDim2] << ")";
2006 GlobalOpenGL().drawString(dimensions.c_str());
2012 glVertex3f(0, vMinBounds[nDim1], vMinBounds[nDim2] - 6.0f / m_fScale);
2013 glVertex3f(0, vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale);
2015 glVertex3f(0, vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale);
2016 glVertex3f(0, vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale);
2018 glVertex3f(0, vMaxBounds[nDim1], vMinBounds[nDim2] - 6.0f / m_fScale);
2019 glVertex3f(0, vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale);
2022 glVertex3f(0, vMaxBounds[nDim1] + 6.0f / m_fScale, vMinBounds[nDim2]);
2023 glVertex3f(0, vMaxBounds[nDim1] + 10.0f / m_fScale, vMinBounds[nDim2]);
2025 glVertex3f(0, vMaxBounds[nDim1] + 10.0f / m_fScale, vMinBounds[nDim2]);
2026 glVertex3f(0, vMaxBounds[nDim1] + 10.0f / m_fScale, vMaxBounds[nDim2]);
2028 glVertex3f(0, vMaxBounds[nDim1] + 6.0f / m_fScale, vMaxBounds[nDim2]);
2029 glVertex3f(0, vMaxBounds[nDim1] + 10.0f / m_fScale, vMaxBounds[nDim2]);
2033 glRasterPos3f (0, Betwixt(vMinBounds[nDim1], vMaxBounds[nDim1]), vMinBounds[nDim2] - 20.0f / m_fScale);
2034 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
2035 GlobalOpenGL().drawString(dimensions.c_str());
2038 glRasterPos3f (0, vMaxBounds[nDim1] + 16.0f / m_fScale, Betwixt(vMinBounds[nDim2], vMaxBounds[nDim2]));
2039 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
2040 GlobalOpenGL().drawString(dimensions.c_str());
2043 glRasterPos3f (0, vMinBounds[nDim1] + 4.0f, vMaxBounds[nDim2] + 8 / m_fScale);
2044 dimensions << "(" << g_pOrgStrings[2][0] << vMinBounds[nDim1] << " " << g_pOrgStrings[2][1] << vMaxBounds[nDim2] << ")";
2045 GlobalOpenGL().drawString(dimensions.c_str());
2049 class XYRenderer: public Renderer
2058 unsigned int m_highlight;
2062 XYRenderer(RenderStateFlags globalstate, Shader* selected) :
2063 m_globalstate(globalstate),
2064 m_state_selected(selected)
2066 ASSERT_NOTNULL(selected);
2067 m_state_stack.push_back(state_type());
2070 void SetState(Shader* state, EStyle style)
2072 ASSERT_NOTNULL(state);
2073 if(style == eWireframeOnly)
2074 m_state_stack.back().m_state = state;
2076 const EStyle getStyle() const
2078 return eWireframeOnly;
2082 m_state_stack.push_back(m_state_stack.back());
2086 ASSERT_MESSAGE(!m_state_stack.empty(), "popping empty stack");
2087 m_state_stack.pop_back();
2089 void Highlight(EHighlightMode mode, bool bEnable = true)
2092 ? m_state_stack.back().m_highlight |= mode
2093 : m_state_stack.back().m_highlight &= ~mode;
2095 void addRenderable(const OpenGLRenderable& renderable, const Matrix4& localToWorld)
2097 if(m_state_stack.back().m_highlight & ePrimitive)
2099 m_state_selected->addRenderable(renderable, localToWorld);
2103 m_state_stack.back().m_state->addRenderable(renderable, localToWorld);
2107 void render(const Matrix4& modelview, const Matrix4& projection)
2109 GlobalShaderCache().render(m_globalstate, modelview, projection);
2112 std::vector<state_type> m_state_stack;
2113 RenderStateFlags m_globalstate;
2114 Shader* m_state_selected;
2117 void XYWnd::updateProjection()
2119 m_projection[0] = 1.0f / static_cast<float>(m_nWidth / 2);
2120 m_projection[5] = 1.0f / static_cast<float>(m_nHeight / 2);
2121 m_projection[10] = 1.0f / (g_MaxWorldCoord * m_fScale);
2123 m_projection[12] = 0.0f;
2124 m_projection[13] = 0.0f;
2125 m_projection[14] = -1.0f;
2137 m_projection[11] = 0.0f;
2139 m_projection[15] = 1.0f;
2141 m_view.Construct(m_projection, m_modelview, m_nWidth, m_nHeight);
2144 // note: modelview matrix must have a uniform scale, otherwise strange things happen when rendering the rotation manipulator.
2145 void XYWnd::updateModelview()
2147 int nDim1 = (m_viewType == YZ) ? 1 : 0;
2148 int nDim2 = (m_viewType == XY) ? 1 : 2;
2151 m_modelview[12] = -m_vOrigin[nDim1] * m_fScale;
2152 m_modelview[13] = -m_vOrigin[nDim2] * m_fScale;
2153 m_modelview[14] = g_MaxWorldCoord * m_fScale;
2159 m_modelview[0] = m_fScale;
2164 m_modelview[5] = m_fScale;
2169 m_modelview[10] = -m_fScale;
2172 m_modelview[0] = m_fScale;
2178 m_modelview[6] = m_fScale;
2181 m_modelview[9] = m_fScale;
2182 m_modelview[10] = 0;
2187 m_modelview[2] = -m_fScale;
2189 m_modelview[4] = m_fScale;
2194 m_modelview[9] = m_fScale;
2195 m_modelview[10] = 0;
2199 m_modelview[3] = m_modelview[7] = m_modelview[11] = 0;
2200 m_modelview[15] = 1;
2202 m_view.Construct(m_projection, m_modelview, m_nWidth, m_nHeight);
2211 //#define DBG_SCENEDUMP
2213 void XYWnd::XY_Draw()
2218 glViewport(0, 0, m_nWidth, m_nHeight);
2219 glClearColor (g_xywindow_globals.color_gridback[0],
2220 g_xywindow_globals.color_gridback[1],
2221 g_xywindow_globals.color_gridback[2],0);
2223 glClear(GL_COLOR_BUFFER_BIT);
2229 glMatrixMode(GL_PROJECTION);
2230 glLoadMatrixf(reinterpret_cast<const float*>(&m_projection));
2232 glMatrixMode(GL_MODELVIEW);
2234 glScalef(m_fScale, m_fScale, 1);
2235 int nDim1 = (m_viewType == YZ) ? 1 : 0;
2236 int nDim2 = (m_viewType == XY) ? 1 : 2;
2237 glTranslatef(-m_vOrigin[nDim1], -m_vOrigin[nDim2], 0);
2239 glDisable (GL_LINE_STIPPLE);
2241 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2242 glDisableClientState(GL_NORMAL_ARRAY);
2243 glDisableClientState(GL_COLOR_ARRAY);
2244 glDisable(GL_TEXTURE_2D);
2245 glDisable(GL_LIGHTING);
2246 glDisable(GL_COLOR_MATERIAL);
2247 glDisable(GL_DEPTH_TEST);
2250 if ( g_xywindow_globals_private.show_blocks)
2253 glLoadMatrixf(reinterpret_cast<const float*>(&m_modelview));
2255 unsigned int globalstate = RENDER_COLOURARRAY | RENDER_COLOURWRITE | RENDER_POLYGONSMOOTH | RENDER_LINESMOOTH;
2256 if(!g_xywindow_globals.m_bNoStipple)
2258 globalstate |= RENDER_LINESTIPPLE;
2262 XYRenderer renderer(globalstate, m_state_selected);
2264 Scene_Render(renderer, m_view);
2266 GlobalOpenGL_debugAssertNoErrors();
2267 renderer.render(m_modelview, m_projection);
2268 GlobalOpenGL_debugAssertNoErrors();
2271 glDepthMask(GL_FALSE);
2273 GlobalOpenGL_debugAssertNoErrors();
2275 glLoadMatrixf(reinterpret_cast<const float*>(&m_modelview));
2277 GlobalOpenGL_debugAssertNoErrors();
2278 glDisable(GL_LINE_STIPPLE);
2279 GlobalOpenGL_debugAssertNoErrors();
2281 GlobalOpenGL_debugAssertNoErrors();
2282 if(GlobalOpenGL().GL_1_3())
2284 glActiveTexture(GL_TEXTURE0);
2285 glClientActiveTexture(GL_TEXTURE0);
2287 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2288 GlobalOpenGL_debugAssertNoErrors();
2289 glDisableClientState(GL_NORMAL_ARRAY);
2290 GlobalOpenGL_debugAssertNoErrors();
2291 glDisableClientState(GL_COLOR_ARRAY);
2292 GlobalOpenGL_debugAssertNoErrors();
2293 glDisable(GL_TEXTURE_2D);
2294 GlobalOpenGL_debugAssertNoErrors();
2295 glDisable(GL_LIGHTING);
2296 GlobalOpenGL_debugAssertNoErrors();
2297 glDisable(GL_COLOR_MATERIAL);
2298 GlobalOpenGL_debugAssertNoErrors();
2300 GlobalOpenGL_debugAssertNoErrors();
2304 if(g_xywindow_globals_private.m_bSizePaint && GlobalSelectionSystem().countSelected() != 0)
2307 Select_GetBounds(min, max);
2308 PaintSizeInfo(nDim1, nDim2, min, max);
2313 glColor4f(0.2f, 0.9f, 0.2f, 0.8f);
2315 if (m_viewType == XY)
2317 glVertex2f(2.0f * g_MinWorldCoord, m_mousePosition[1]);
2318 glVertex2f(2.0f * g_MaxWorldCoord, m_mousePosition[1]);
2319 glVertex2f(m_mousePosition[0], 2.0f * g_MinWorldCoord);
2320 glVertex2f(m_mousePosition[0], 2.0f * g_MaxWorldCoord);
2322 else if (m_viewType == YZ)
2324 glVertex3f(m_mousePosition[0], 2.0f * g_MinWorldCoord, m_mousePosition[2]);
2325 glVertex3f(m_mousePosition[0], 2.0f * g_MaxWorldCoord, m_mousePosition[2]);
2326 glVertex3f(m_mousePosition[0], m_mousePosition[1], 2.0f * g_MinWorldCoord);
2327 glVertex3f(m_mousePosition[0], m_mousePosition[1], 2.0f * g_MaxWorldCoord);
2331 glVertex3f (2.0f * g_MinWorldCoord, m_mousePosition[1], m_mousePosition[2]);
2332 glVertex3f (2.0f * g_MaxWorldCoord, m_mousePosition[1], m_mousePosition[2]);
2333 glVertex3f(m_mousePosition[0], m_mousePosition[1], 2.0f * g_MinWorldCoord);
2334 glVertex3f(m_mousePosition[0], m_mousePosition[1], 2.0f * g_MaxWorldCoord);
2341 GlobalClipPoints_Draw(m_fScale);
2344 GlobalOpenGL_debugAssertNoErrors();
2348 glScalef(m_fScale, m_fScale, 1);
2349 glTranslatef(-m_vOrigin[nDim1], -m_vOrigin[nDim2], 0);
2351 DrawCameraIcon (Camera_getOrigin(*g_pParentWnd->GetCamWnd()), Camera_getAngles(*g_pParentWnd->GetCamWnd()));
2353 Feedback_draw2D( m_viewType );
2355 if (g_xywindow_globals_private.show_outline)
2359 glMatrixMode (GL_PROJECTION);
2361 glOrtho (0, m_nWidth, 0, m_nHeight, 0, 1);
2363 glMatrixMode (GL_MODELVIEW);
2366 // four view mode doesn't colorize
2367 if (g_pParentWnd->CurrentStyle() == MainFrame::eSplit)
2368 glColor3fv(vector3_to_array(g_xywindow_globals.color_viewname));
2374 glColor3fv(vector3_to_array(g_xywindow_globals.AxisColorX));
2377 glColor3fv(vector3_to_array(g_xywindow_globals.AxisColorY));
2380 glColor3fv(vector3_to_array(g_xywindow_globals.AxisColorZ));
2384 glBegin (GL_LINE_LOOP);
2386 glVertex2i (m_nWidth-1, 0);
2387 glVertex2i (m_nWidth-1, m_nHeight-1);
2388 glVertex2i (0, m_nHeight-1);
2393 GlobalOpenGL_debugAssertNoErrors();
2398 void XYWnd_MouseToPoint(XYWnd* xywnd, int x, int y, Vector3& point)
2400 xywnd->XY_ToPoint(x, y, point);
2401 xywnd->XY_SnapToGrid(point);
2403 int nDim = (xywnd->GetViewType() == XY) ? 2 : (xywnd->GetViewType() == YZ) ? 0 : 1;
2404 float fWorkMid = float_mid(Select_getWorkZone().d_work_min[nDim], Select_getWorkZone().d_work_max[nDim]);
2405 point[nDim] = float_snapped(fWorkMid, GetGridSize());
2408 void XYWnd::OnEntityCreate (const char* item)
2410 StringOutputStream command;
2411 command << "entityCreate -class " << item;
2412 UndoableCommand undo(command.c_str());
2414 XYWnd_MouseToPoint(this, m_entityCreate_x, m_entityCreate_y, point);
2415 Entity_createFromSelection(item, point);
2420 void GetFocusPosition(Vector3& position)
2422 if(GlobalSelectionSystem().countSelected() != 0)
2424 Select_GetMid(position);
2428 position = Camera_getOrigin(*g_pParentWnd->GetCamWnd());
2432 void XYWnd_Focus(XYWnd* xywnd)
2435 GetFocusPosition(position);
2436 xywnd->PositionView(position);
2439 void XY_Split_Focus()
2442 GetFocusPosition(position);
2443 g_pParentWnd->GetXYWnd()->PositionView(position);
2444 g_pParentWnd->GetXZWnd()->PositionView(position);
2445 g_pParentWnd->GetYZWnd()->PositionView(position);
2450 XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2456 XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2457 xywnd->SetViewType(XY);
2463 XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2464 xywnd->SetViewType(XZ);
2470 g_pParentWnd->GetXYWnd()->SetViewType(YZ);
2471 XYWnd_Focus(g_pParentWnd->GetXYWnd());
2476 XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2477 if (xywnd->GetViewType() == XY)
2478 xywnd->SetViewType(XZ);
2479 else if (xywnd->GetViewType() == XZ)
2480 xywnd->SetViewType(YZ);
2482 xywnd->SetViewType(XY);
2488 if (g_pParentWnd->GetXYWnd())
2489 g_pParentWnd->GetXYWnd()->SetScale(1);
2490 if (g_pParentWnd->GetXZWnd())
2491 g_pParentWnd->GetXZWnd()->SetScale(1);
2492 if (g_pParentWnd->GetYZWnd())
2493 g_pParentWnd->GetYZWnd()->SetScale(1);
2498 XYWnd_ZoomIn(g_pParentWnd->ActiveXY());
2501 // NOTE: the zoom out factor is 4/5, we could think about customizing it
2502 // we don't go below a zoom factor corresponding to 10% of the max world size
2503 // (this has to be computed against the window size)
2506 XYWnd_ZoomOut(g_pParentWnd->ActiveXY());
2511 void ToggleShowCrosshair()
2514 XY_UpdateAllWindows();
2517 void ToggleShowSizeInfo()
2519 g_xywindow_globals_private.m_bSizePaint = !g_xywindow_globals_private.m_bSizePaint;
2520 XY_UpdateAllWindows();
2523 void ToggleShowGrid()
2525 g_xywindow_globals_private.d_showgrid = !g_xywindow_globals_private.d_showgrid;
2526 XY_UpdateAllWindows();
2529 ToggleShown g_xy_top_shown(true);
2531 void XY_Top_Shown_Construct(GtkWindow* parent)
2533 g_xy_top_shown.connect(GTK_WIDGET(parent));
2536 ToggleShown g_yz_side_shown(false);
2538 void YZ_Side_Shown_Construct(GtkWindow* parent)
2540 g_yz_side_shown.connect(GTK_WIDGET(parent));
2543 ToggleShown g_xz_front_shown(false);
2545 void XZ_Front_Shown_Construct(GtkWindow* parent)
2547 g_xz_front_shown.connect(GTK_WIDGET(parent));
2551 class EntityClassMenu : public ModuleObserver
2553 std::size_t m_unrealised;
2555 EntityClassMenu() : m_unrealised(1)
2560 if(--m_unrealised == 0)
2566 if(++m_unrealised == 1)
2568 if(XYWnd::m_mnuDrop != 0)
2570 gtk_widget_destroy(GTK_WIDGET(XYWnd::m_mnuDrop));
2571 XYWnd::m_mnuDrop = 0;
2577 EntityClassMenu g_EntityClassMenu;
2582 void ShowNamesToggle()
2584 GlobalEntityCreator().setShowNames(!GlobalEntityCreator().getShowNames());
2585 XY_UpdateAllWindows();
2587 typedef FreeCaller<ShowNamesToggle> ShowNamesToggleCaller;
2588 void ShowNamesExport(const BoolImportCallback& importer)
2590 importer(GlobalEntityCreator().getShowNames());
2592 typedef FreeCaller1<const BoolImportCallback&, ShowNamesExport> ShowNamesExportCaller;
2594 void ShowAnglesToggle()
2596 GlobalEntityCreator().setShowAngles(!GlobalEntityCreator().getShowAngles());
2597 XY_UpdateAllWindows();
2599 typedef FreeCaller<ShowAnglesToggle> ShowAnglesToggleCaller;
2600 void ShowAnglesExport(const BoolImportCallback& importer)
2602 importer(GlobalEntityCreator().getShowAngles());
2604 typedef FreeCaller1<const BoolImportCallback&, ShowAnglesExport> ShowAnglesExportCaller;
2606 void ShowBlocksToggle()
2608 g_xywindow_globals_private.show_blocks ^= 1;
2609 XY_UpdateAllWindows();
2611 typedef FreeCaller<ShowBlocksToggle> ShowBlocksToggleCaller;
2612 void ShowBlocksExport(const BoolImportCallback& importer)
2614 importer(g_xywindow_globals_private.show_blocks);
2616 typedef FreeCaller1<const BoolImportCallback&, ShowBlocksExport> ShowBlocksExportCaller;
2618 void ShowCoordinatesToggle()
2620 g_xywindow_globals_private.show_coordinates ^= 1;
2621 XY_UpdateAllWindows();
2623 typedef FreeCaller<ShowCoordinatesToggle> ShowCoordinatesToggleCaller;
2624 void ShowCoordinatesExport(const BoolImportCallback& importer)
2626 importer(g_xywindow_globals_private.show_coordinates);
2628 typedef FreeCaller1<const BoolImportCallback&, ShowCoordinatesExport> ShowCoordinatesExportCaller;
2630 void ShowOutlineToggle()
2632 g_xywindow_globals_private.show_outline ^= 1;
2633 XY_UpdateAllWindows();
2635 typedef FreeCaller<ShowOutlineToggle> ShowOutlineToggleCaller;
2636 void ShowOutlineExport(const BoolImportCallback& importer)
2638 importer(g_xywindow_globals_private.show_outline);
2640 typedef FreeCaller1<const BoolImportCallback&, ShowOutlineExport> ShowOutlineExportCaller;
2642 void ShowAxesToggle()
2644 g_xywindow_globals_private.show_axis ^= 1;
2645 XY_UpdateAllWindows();
2647 typedef FreeCaller<ShowAxesToggle> ShowAxesToggleCaller;
2648 void ShowAxesExport(const BoolImportCallback& importer)
2650 importer(g_xywindow_globals_private.show_axis);
2652 typedef FreeCaller1<const BoolImportCallback&, ShowAxesExport> ShowAxesExportCaller;
2654 void ShowWorkzoneToggle()
2656 g_xywindow_globals_private.d_show_work ^= 1;
2657 XY_UpdateAllWindows();
2659 typedef FreeCaller<ShowWorkzoneToggle> ShowWorkzoneToggleCaller;
2660 void ShowWorkzoneExport(const BoolImportCallback& importer)
2662 importer(g_xywindow_globals_private.d_show_work);
2664 typedef FreeCaller1<const BoolImportCallback&, ShowWorkzoneExport> ShowWorkzoneExportCaller;
2666 ShowNamesExportCaller g_show_names_caller;
2667 BoolExportCallback g_show_names_callback(g_show_names_caller);
2668 ToggleItem g_show_names(g_show_names_callback);
2670 ShowAnglesExportCaller g_show_angles_caller;
2671 BoolExportCallback g_show_angles_callback(g_show_angles_caller);
2672 ToggleItem g_show_angles(g_show_angles_callback);
2674 ShowBlocksExportCaller g_show_blocks_caller;
2675 BoolExportCallback g_show_blocks_callback(g_show_blocks_caller);
2676 ToggleItem g_show_blocks(g_show_blocks_callback);
2678 ShowCoordinatesExportCaller g_show_coordinates_caller;
2679 BoolExportCallback g_show_coordinates_callback(g_show_coordinates_caller);
2680 ToggleItem g_show_coordinates(g_show_coordinates_callback);
2682 ShowOutlineExportCaller g_show_outline_caller;
2683 BoolExportCallback g_show_outline_callback(g_show_outline_caller);
2684 ToggleItem g_show_outline(g_show_outline_callback);
2686 ShowAxesExportCaller g_show_axes_caller;
2687 BoolExportCallback g_show_axes_callback(g_show_axes_caller);
2688 ToggleItem g_show_axes(g_show_axes_callback);
2690 ShowWorkzoneExportCaller g_show_workzone_caller;
2691 BoolExportCallback g_show_workzone_callback(g_show_workzone_caller);
2692 ToggleItem g_show_workzone(g_show_workzone_callback);
2694 void XYShow_registerCommands()
2696 GlobalToggles_insert("ShowAngles", ShowAnglesToggleCaller(), ToggleItem::AddCallbackCaller(g_show_angles));
2697 GlobalToggles_insert("ShowNames", ShowNamesToggleCaller(), ToggleItem::AddCallbackCaller(g_show_names));
2698 GlobalToggles_insert("ShowBlocks", ShowBlocksToggleCaller(), ToggleItem::AddCallbackCaller(g_show_blocks));
2699 GlobalToggles_insert("ShowCoordinates", ShowCoordinatesToggleCaller(), ToggleItem::AddCallbackCaller(g_show_coordinates));
2700 GlobalToggles_insert("ShowWindowOutline", ShowOutlineToggleCaller(), ToggleItem::AddCallbackCaller(g_show_outline));
2701 GlobalToggles_insert("ShowAxes", ShowAxesToggleCaller(), ToggleItem::AddCallbackCaller(g_show_axes));
2702 GlobalToggles_insert("ShowWorkzone", ShowWorkzoneToggleCaller(), ToggleItem::AddCallbackCaller(g_show_workzone));
2705 void XYWnd_registerShortcuts()
2707 command_connect_accelerator("ToggleCrosshairs");
2708 command_connect_accelerator("ToggleSizePaint");
2713 void Orthographic_constructPreferences(PreferencesPage& page)
2715 page.appendCheckBox("", "Solid selection boxes", g_xywindow_globals.m_bNoStipple);
2716 page.appendCheckBox("", "Display size info", g_xywindow_globals_private.m_bSizePaint);
2717 page.appendCheckBox("", "Chase mouse during drags", g_xywindow_globals_private.m_bChaseMouse);
2718 page.appendCheckBox("", "Update views on camera move", g_xywindow_globals_private.m_bCamXYUpdate);
2720 void Orthographic_constructPage(PreferenceGroup& group)
2722 PreferencesPage page(group.createPage("Orthographic", "Orthographic View Preferences"));
2723 Orthographic_constructPreferences(page);
2725 void Orthographic_registerPreferencesPage()
2727 PreferencesDialog_addSettingsPage(FreeCaller1<PreferenceGroup&, Orthographic_constructPage>());
2730 void Clipper_constructPreferences(PreferencesPage& page)
2732 page.appendCheckBox("", "Clipper tool uses caulk", g_clip_useCaulk);
2734 void Clipper_constructPage(PreferenceGroup& group)
2736 PreferencesPage page(group.createPage("Clipper", "Clipper Tool Settings"));
2737 Clipper_constructPreferences(page);
2739 void Clipper_registerPreferencesPage()
2741 PreferencesDialog_addSettingsPage(FreeCaller1<PreferenceGroup&, Clipper_constructPage>());
2745 #include "preferencesystem.h"
2746 #include "stringio.h"
2751 void ToggleShown_importBool(ToggleShown& self, bool value)
2755 typedef ReferenceCaller1<ToggleShown, bool, ToggleShown_importBool> ToggleShownImportBoolCaller;
2756 void ToggleShown_exportBool(const ToggleShown& self, const BoolImportCallback& importer)
2758 importer(self.active());
2760 typedef ConstReferenceCaller1<ToggleShown, const BoolImportCallback&, ToggleShown_exportBool> ToggleShownExportBoolCaller;
2763 void XYWindow_Construct()
2765 GlobalCommands_insert("ToggleCrosshairs", FreeCaller<ToggleShowCrosshair>(), Accelerator('X', (GdkModifierType)GDK_SHIFT_MASK));
2766 GlobalCommands_insert("ToggleSizePaint", FreeCaller<ToggleShowSizeInfo>(), Accelerator('J'));
2767 GlobalCommands_insert("ToggleGrid", FreeCaller<ToggleShowGrid>(), Accelerator('0'));
2769 GlobalToggles_insert("ToggleView", ToggleShown::ToggleCaller(g_xy_top_shown), ToggleItem::AddCallbackCaller(g_xy_top_shown.m_item), Accelerator('V', (GdkModifierType)(GDK_SHIFT_MASK|GDK_CONTROL_MASK)));
2770 GlobalToggles_insert("ToggleSideView", ToggleShown::ToggleCaller(g_yz_side_shown), ToggleItem::AddCallbackCaller(g_yz_side_shown.m_item));
2771 GlobalToggles_insert("ToggleFrontView", ToggleShown::ToggleCaller(g_xz_front_shown), ToggleItem::AddCallbackCaller(g_xz_front_shown.m_item));
2772 GlobalCommands_insert("NextView", FreeCaller<XY_Next>(), Accelerator(GDK_Tab, (GdkModifierType)GDK_CONTROL_MASK));
2773 GlobalCommands_insert("ZoomIn", FreeCaller<XY_ZoomIn>(), Accelerator(GDK_Delete));
2774 GlobalCommands_insert("ZoomOut", FreeCaller<XY_ZoomOut>(), Accelerator(GDK_Insert));
2775 GlobalCommands_insert("ViewTop", FreeCaller<XY_Top>());
2776 GlobalCommands_insert("ViewSide", FreeCaller<XY_Side>());
2777 GlobalCommands_insert("ViewFront", FreeCaller<XY_Front>());
2778 GlobalCommands_insert("Zoom100", FreeCaller<XY_Zoom100>());
2779 GlobalCommands_insert("CenterXYViews", FreeCaller<XY_Split_Focus>(), Accelerator(GDK_Tab, (GdkModifierType)(GDK_SHIFT_MASK|GDK_CONTROL_MASK)));
2780 GlobalCommands_insert("CenterXYView", FreeCaller<XY_Focus>(), Accelerator(GDK_Tab, (GdkModifierType)(GDK_SHIFT_MASK|GDK_CONTROL_MASK)));
2782 GlobalPreferenceSystem().registerPreference("ClipCaulk", BoolImportStringCaller(g_clip_useCaulk), BoolExportStringCaller(g_clip_useCaulk));
2784 GlobalPreferenceSystem().registerPreference("NewRightClick", BoolImportStringCaller(g_xywindow_globals.m_bRightClick), BoolExportStringCaller(g_xywindow_globals.m_bRightClick));
2785 GlobalPreferenceSystem().registerPreference("ChaseMouse", BoolImportStringCaller(g_xywindow_globals_private.m_bChaseMouse), BoolExportStringCaller(g_xywindow_globals_private.m_bChaseMouse));
2786 GlobalPreferenceSystem().registerPreference("SizePainting", BoolImportStringCaller(g_xywindow_globals_private.m_bSizePaint), BoolExportStringCaller(g_xywindow_globals_private.m_bSizePaint));
2787 GlobalPreferenceSystem().registerPreference("NoStipple", BoolImportStringCaller(g_xywindow_globals.m_bNoStipple), BoolExportStringCaller(g_xywindow_globals.m_bNoStipple));
2788 GlobalPreferenceSystem().registerPreference("SI_ShowCoords", BoolImportStringCaller(g_xywindow_globals_private.show_coordinates), BoolExportStringCaller(g_xywindow_globals_private.show_coordinates));
2789 GlobalPreferenceSystem().registerPreference("SI_ShowOutlines", BoolImportStringCaller(g_xywindow_globals_private.show_outline), BoolExportStringCaller(g_xywindow_globals_private.show_outline));
2790 GlobalPreferenceSystem().registerPreference("SI_ShowAxis", BoolImportStringCaller(g_xywindow_globals_private.show_axis), BoolExportStringCaller(g_xywindow_globals_private.show_axis));
2791 GlobalPreferenceSystem().registerPreference("CamXYUpdate", BoolImportStringCaller(g_xywindow_globals_private.m_bCamXYUpdate), BoolExportStringCaller(g_xywindow_globals_private.m_bCamXYUpdate));
2793 GlobalPreferenceSystem().registerPreference("SI_AxisColors0", Vector3ImportStringCaller(g_xywindow_globals.AxisColorX), Vector3ExportStringCaller(g_xywindow_globals.AxisColorX));
2794 GlobalPreferenceSystem().registerPreference("SI_AxisColors1", Vector3ImportStringCaller(g_xywindow_globals.AxisColorY), Vector3ExportStringCaller(g_xywindow_globals.AxisColorY));
2795 GlobalPreferenceSystem().registerPreference("SI_AxisColors2", Vector3ImportStringCaller(g_xywindow_globals.AxisColorZ), Vector3ExportStringCaller(g_xywindow_globals.AxisColorZ));
2796 GlobalPreferenceSystem().registerPreference("SI_Colors1", Vector3ImportStringCaller(g_xywindow_globals.color_gridback), Vector3ExportStringCaller(g_xywindow_globals.color_gridback));
2797 GlobalPreferenceSystem().registerPreference("SI_Colors2", Vector3ImportStringCaller(g_xywindow_globals.color_gridminor), Vector3ExportStringCaller(g_xywindow_globals.color_gridminor));
2798 GlobalPreferenceSystem().registerPreference("SI_Colors3", Vector3ImportStringCaller(g_xywindow_globals.color_gridmajor), Vector3ExportStringCaller(g_xywindow_globals.color_gridmajor));
2799 GlobalPreferenceSystem().registerPreference("SI_Colors6", Vector3ImportStringCaller(g_xywindow_globals.color_gridblock), Vector3ExportStringCaller(g_xywindow_globals.color_gridblock));
2800 GlobalPreferenceSystem().registerPreference("SI_Colors7", Vector3ImportStringCaller(g_xywindow_globals.color_gridtext), Vector3ExportStringCaller(g_xywindow_globals.color_gridtext));
2801 GlobalPreferenceSystem().registerPreference("SI_Colors8", Vector3ImportStringCaller(g_xywindow_globals.color_brushes), Vector3ExportStringCaller(g_xywindow_globals.color_brushes));
2802 GlobalPreferenceSystem().registerPreference("SI_Colors9", Vector3ImportStringCaller(g_xywindow_globals.color_selbrushes), Vector3ExportStringCaller(g_xywindow_globals.color_selbrushes));
2803 GlobalPreferenceSystem().registerPreference("SI_Colors10", Vector3ImportStringCaller(g_xywindow_globals.color_clipper), Vector3ExportStringCaller(g_xywindow_globals.color_clipper));
2804 GlobalPreferenceSystem().registerPreference("SI_Colors11", Vector3ImportStringCaller(g_xywindow_globals.color_viewname), Vector3ExportStringCaller(g_xywindow_globals.color_viewname));
2805 GlobalPreferenceSystem().registerPreference("SI_Colors13", Vector3ImportStringCaller(g_xywindow_globals.color_gridminor_alt), Vector3ExportStringCaller(g_xywindow_globals.color_gridminor_alt));
2806 GlobalPreferenceSystem().registerPreference("SI_Colors14", Vector3ImportStringCaller(g_xywindow_globals.color_gridmajor_alt), Vector3ExportStringCaller(g_xywindow_globals.color_gridmajor_alt));
2809 GlobalPreferenceSystem().registerPreference("XZVIS", makeBoolStringImportCallback(ToggleShownImportBoolCaller(g_xz_front_shown)), makeBoolStringExportCallback(ToggleShownExportBoolCaller(g_xz_front_shown)));
2810 GlobalPreferenceSystem().registerPreference("YZVIS", makeBoolStringImportCallback(ToggleShownImportBoolCaller(g_yz_side_shown)), makeBoolStringExportCallback(ToggleShownExportBoolCaller(g_yz_side_shown)));
2812 Orthographic_registerPreferencesPage();
2813 Clipper_registerPreferencesPage();
2815 XYWnd::captureStates();
2816 GlobalEntityClassManager().attach(g_EntityClassMenu);
2819 void XYWindow_Destroy()
2821 GlobalEntityClassManager().detach(g_EntityClassMenu);
2822 XYWnd::releaseStates();