SPH
OrthoPane.cpp
Go to the documentation of this file.
2 #include "gui/Controller.h"
3 #include "gui/Settings.h"
4 #include "gui/Utils.h"
5 #include "gui/objects/Bitmap.h"
6 #include "gui/objects/Camera.h"
9 #include "system/Profiler.h"
10 #include "thread/CheckFunction.h"
11 #include <wx/dcclient.h>
12 #include <wx/timer.h>
13 
15 
16 OrthoPane::OrthoPane(wxWindow* parent, Controller* controller, const GuiSettings& UNUSED(gui))
17  : IGraphicsPane(parent)
18  , controller(controller) {
19  this->SetMinSize(wxSize(300, 300));
20  this->Connect(wxEVT_PAINT, wxPaintEventHandler(OrthoPane::onPaint));
21  this->Connect(wxEVT_MOTION, wxMouseEventHandler(OrthoPane::onMouseMotion));
22  this->Connect(wxEVT_MOUSEWHEEL, wxMouseEventHandler(OrthoPane::onMouseWheel));
23  this->Connect(wxEVT_RIGHT_DOWN, wxMouseEventHandler(OrthoPane::onRightDown));
24  this->Connect(wxEVT_RIGHT_UP, wxMouseEventHandler(OrthoPane::onRightUp));
25  this->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(OrthoPane::onLeftUp));
26  this->Connect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(OrthoPane::onDoubleClick));
27  this->Connect(wxEVT_SIZE, wxSizeEventHandler(OrthoPane::onResize));
28 
29  // get the camera from controller; note that since then, we provided the updated camera to controller from
30  // here, the camera is never modified in controller!
31  camera = controller->getCurrentCamera();
32  particle.lastIdx = -1;
33 
34  const wxSize size = this->GetSize();
35  arcBall.resize(Pixel(size.x, size.y));
36 }
37 
38 OrthoPane::~OrthoPane() = default;
39 
41  dragging.initialMatrix = AffineMatrix::identity();
43 }
44 
45 void OrthoPane::onTimeStep(const Storage& storage, const Statistics& UNUSED(stats)) {
46  if (controller->getParams().get<bool>(GuiSettingsId::CAMERA_AUTOSETUP)) {
47  camera->autoSetup(storage);
48  }
49 }
50 
51 void OrthoPane::onPaint(wxPaintEvent& UNUSED(evt)) {
53 
54  wxPaintDC dc(this);
55  const wxBitmap& bitmap = controller->getRenderedBitmap();
56  if (!bitmap.IsOk()) {
57  return;
58  }
59 
60  dc.DrawBitmap(bitmap, wxPoint(0, 0));
61 }
62 
63 void OrthoPane::onMouseMotion(wxMouseEvent& evt) {
65  Pixel position(evt.GetPosition());
66  if (evt.Dragging()) {
67  Pixel offset = Pixel(position.x - dragging.position.x, -(position.y - dragging.position.y));
68  if (evt.RightIsDown()) {
69  // right button, rotate view
70  AffineMatrix matrix = arcBall.drag(position, Vector(0._f));
71  camera->transform(dragging.initialMatrix * matrix);
72  } else {
73  // left button (or middle), pan
74  camera->pan(offset);
75  }
76  controller->refresh(camera->clone());
77  }
78  dragging.position = position;
79 }
80 
81 void OrthoPane::onRightDown(wxMouseEvent& evt) {
83  arcBall.click(Pixel(evt.GetPosition()));
84 }
85 
86 void OrthoPane::onRightUp(wxMouseEvent& evt) {
88  AffineMatrix matrix = arcBall.drag(Pixel(evt.GetPosition()), Vector(0._f));
89  dragging.initialMatrix = dragging.initialMatrix * matrix;
90 }
91 
92 void OrthoPane::onDoubleClick(wxMouseEvent& evt) {
94  const wxPoint position = evt.GetPosition();
96  const Pixel origin(this->GetSize().x - 50, 231);
97  const Pixel size(30, 201);
98  if (!Interval(origin.x, origin.x + size.x).contains(position.x) ||
99  !Interval(origin.y - size.y, origin.y).contains(position.y)) {
100  // did not click on the palette
101  return;
102  }
103 
104  Optional<Palette> palette = controller->getCurrentColorizer()->getPalette();
105  if (palette) {
106  PaletteDialog* paletteDialog = new PaletteDialog(
107  this->GetParent(), wxSize(300, 240), palette.value(), [this](const Palette& selected) {
108  controller->setPaletteOverride(selected);
109  });
110  paletteDialog->Show();
111  }
112 }
113 
114 void OrthoPane::onLeftUp(wxMouseEvent& evt) {
116  Pixel position(evt.GetPosition());
117  Optional<Size> selectedIdx = controller->getIntersectedParticle(position, 1.f);
118  if (selectedIdx.valueOr(-1) != particle.lastIdx.valueOr(-1)) {
119  particle.lastIdx = selectedIdx;
120  controller->setSelectedParticle(selectedIdx);
121  controller->refresh(camera->clone());
122  }
123 }
124 
125 void OrthoPane::onMouseWheel(wxMouseEvent& evt) {
127  const float spin = evt.GetWheelRotation();
128  const float amount = (spin > 0.f) ? 1.2f : 1.f / 1.2f;
129  Pixel fixedPoint(evt.GetPosition());
130  camera->zoom(fixedPoint, amount);
131  controller->refresh(camera->clone());
132  controller->setAutoZoom(false);
133 }
134 
135 void OrthoPane::onResize(wxSizeEvent& evt) {
136  const Pixel newSize(max(10, evt.GetSize().x), max(10, evt.GetSize().y));
137  arcBall.resize(newSize);
138  camera->resize(newSize);
139  controller->tryRedraw();
140 }
141 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Wrapper of wxBitmap, will be possibly replaced by custom implementation.
Defines projection transforming 3D particles onto 2D screen.
Helper functions to check the internal consistency of the code.
@ MAIN_THREAD
Function can only be executed from main thread.
#define CHECK_FUNCTION(flags)
Definition: CheckFunction.h:40
Object converting quantity values of particles into colors.
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Tool to measure time spent in functions and profile the code.
Random utility functions for drawing stuff to DC.
BasicVector< Float > Vector
Definition: Vector.h:539
static AffineMatrix identity()
Definition: AffineMatrix.h:132
void resize(const Pixel newSize)
Definition: ArcBall.h:37
void click(const Pixel point)
Called on mouse click, starting the rotation.
Definition: ArcBall.h:42
AffineMatrix drag(const Pixel point, const Vector &pivot)
Called when mouse moves, rotating the object.
Definition: ArcBall.h:51
Main GUI class connection the simulation with UI controls.
Definition: Controller.h:42
void setAutoZoom(const bool enable)
Enables or disables auto-zoom during the simulation.
Definition: Controller.cpp:213
AutoPtr< ICamera > getCurrentCamera() const
Returns the camera currently used for the rendering.
Definition: Controller.cpp:437
const wxBitmap & getRenderedBitmap() const
Renders a bitmap of current view.
Definition: Controller.cpp:426
SharedPtr< IColorizer > getCurrentColorizer() const
Returns the colorizer currently used for rendering into the window.
Definition: Controller.cpp:432
Optional< Size > getIntersectedParticle(const Pixel position, const float toleranceEps)
Returns the particle under given image position, or NOTHING if such particle exists.
Definition: Controller.cpp:442
void setSelectedParticle(const Optional< Size > &particleIdx)
Sets a selected particle or changes the current selection.
Definition: Controller.cpp:543
void refresh(AutoPtr< ICamera > &&camera)
Re-renders the particles with given camera.
Definition: Controller.cpp:638
bool tryRedraw()
If possible, redraws the particles with data from storage.
Definition: Controller.cpp:611
GuiSettings & getParams()
Returns the settings object.
Definition: Controller.cpp:316
INLINE TValue get(const GuiSettingsId id) const
Definition: Settings.h:245
virtual void autoSetup(const Storage &storage)=0
Initializes the camera, using the provided particle storage.
virtual void pan(const Pixel offset)=0
Moves the camera by relative offset in image space.
virtual void zoom(const Pixel fixedPoint, const float magnitude)=0
virtual AutoPtr< ICamera > clone() const =0
virtual void transform(const AffineMatrix &matrix)=0
Transforms the current view by given matrix.
virtual void resize(const Pixel newSize)=0
Changes the image size.
virtual Optional< Palette > getPalette() const =0
Returns recommended palette for drawing this colorizer.
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE bool contains(const Float &value) const
Checks whether value is inside the interval.
Definition: Interval.h:55
INLINE Type & value()
Returns the reference to the stored value.
Definition: Optional.h:172
INLINE Type valueOr(const TOther &other) const
Returns the stored value if the object has been initialized, otherwise returns provided parameter.
Definition: Optional.h:188
virtual void resetView() override
Definition: OrthoPane.cpp:40
OrthoPane(wxWindow *parent, Controller *controller, const GuiSettings &gui)
Definition: OrthoPane.cpp:16
Pixel position
Cached last mouse position when dragging the window.
Definition: OrthoPane.h:25
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Definition: OrthoPane.cpp:45
Represents a color palette, used for mapping arbitrary number to a color.
Definition: Palette.h:25
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Definition: Point.h:101