| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "XYMouseToolEvent.h" |
| 4 |
#include "i18n.h" |
| 5 |
#include "imousetool.h" |
| 6 |
#include "math/Vector3.h" |
| 7 |
#include "iorthoview.h" |
| 8 |
#include "icameraview.h" |
| 9 |
|
| 10 |
namespace ui |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* The CameraAngleTool re-orients the camera such that |
| 15 |
* the clicked location is in its view. |
| 16 |
*/ |
| 17 |
class CameraAngleTool : |
| 18 |
public MouseTool |
| 19 |
{ |
| 20 |
public: |
| 21 |
const std::string& getName() override |
| 22 |
{ |
| 23 |
static std::string name("CameraAngleTool"); |
| 24 |
return name; |
| 25 |
} |
| 26 |
|
| 27 |
const std::string& getDisplayName() override |
| 28 |
{ |
| 29 |
static std::string displayName(_("Point Camera")); |
| 30 |
return displayName; |
| 31 |
} |
| 32 |
|
| 33 |
Result onMouseDown(Event& ev) override |
| 34 |
{ |
| 35 |
try |
| 36 |
{ |
| 37 |
// Set the camera angle |
| 38 |
orientCamera(dynamic_cast<XYMouseToolEvent&>(ev)); |
| 39 |
|
| 40 |
return Result::Activated; |
| 41 |
} |
| 42 |
catch (std::bad_cast&) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
return Result::Ignored; // not handled |
| 47 |
} |
| 48 |
|
| 49 |
Result onMouseMove(Event& ev) override |
| 50 |
{ |
| 51 |
try |
| 52 |
{ |
| 53 |
orientCamera(dynamic_cast<XYMouseToolEvent&>(ev)); |
| 54 |
|
| 55 |
return Result::Continued; |
| 56 |
} |
| 57 |
catch (std::bad_cast&) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
return Result::Ignored; |
| 62 |
} |
| 63 |
|
| 64 |
Result onMouseUp(Event& ev) override |
| 65 |
{ |
| 66 |
try |
| 67 |
{ |
| 68 |
// We only operate on XY view events, so attempt to cast |
| 69 |
dynamic_cast<XYMouseToolEvent&>(ev).getScale(); |
| 70 |
return Result::Finished; |
| 71 |
} |
| 72 |
catch (std::bad_cast&) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
return Result::Ignored; |
| 77 |
} |
| 78 |
|
| 79 |
unsigned int getRefreshMode() override |
| 80 |
{ |
| 81 |
return RefreshMode::Force | RefreshMode::AllViews; // update cam view too |
| 82 |
} |
| 83 |
|
| 84 |
private: |
| 85 |
void orientCamera(XYMouseToolEvent& xyEvent) |
| 86 |
{ |
| 87 |
try |
| 88 |
{ |
| 89 |
auto& camView = GlobalCameraManager().getActiveView(); |
| 90 |
|
| 91 |
Vector3 point = xyEvent.getWorldPos(); |
| 92 |
xyEvent.getView().snapToGrid(point); |
| 93 |
|
| 94 |
point -= camView.getCameraOrigin(); |
| 95 |
|
| 96 |
int n1 = (xyEvent.getViewType() == OrthoOrientation::XY) ? 1 : 2; |
| 97 |
int n2 = (xyEvent.getViewType() == OrthoOrientation::YZ) ? 1 : 0; |
| 98 |
|
| 99 |
int nAngle = (xyEvent.getViewType() == OrthoOrientation::XY) ? camera::CAMERA_YAW : camera::CAMERA_PITCH; |
| 100 |
|
| 101 |
if (point[n1] || point[n2]) |
| 102 |
{ |
| 103 |
Vector3 angles(camView.getCameraAngles()); |
| 104 |
|
| 105 |
angles[nAngle] = radians_to_degrees(atan2(point[n1], point[n2])); |
| 106 |
|
| 107 |
camView.setCameraAngles(angles); |
| 108 |
} |
| 109 |
|
| 110 |
xyEvent.getView().queueDraw(); |
| 111 |
} |
| 112 |
catch (const std::runtime_error&) |
| 113 |
{ |
| 114 |
// no camera present |
| 115 |
} |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
} // namespace |