diff options
author | crupest <crupest@outlook.com> | 2021-12-05 22:17:34 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-12-05 22:17:34 +0800 |
commit | 6117141e1ff1e464854949c8a792961dbad8ea73 (patch) | |
tree | 579f216c677f537712c9d7b08c42ca017714cdae | |
parent | e66fa1364d993b2a9f55781850d4b48f1723e309 (diff) | |
download | cru-6117141e1ff1e464854949c8a792961dbad8ea73.tar.gz cru-6117141e1ff1e464854949c8a792961dbad8ea73.tar.bz2 cru-6117141e1ff1e464854949c8a792961dbad8ea73.zip |
...
-rw-r--r-- | demos/graphics_experiments/4.cpp | 161 | ||||
-rw-r--r-- | demos/graphics_experiments/CMakeLists.txt | 5 | ||||
-rw-r--r-- | demos/main/main.cpp | 4 | ||||
-rw-r--r-- | include/cru/ui/controls/TextHostControlService.hpp | 2 | ||||
-rw-r--r-- | src/ui/controls/TextHostControlService.cpp | 2 | ||||
-rw-r--r-- | vcpkg.json | 3 |
6 files changed, 173 insertions, 4 deletions
diff --git a/demos/graphics_experiments/4.cpp b/demos/graphics_experiments/4.cpp new file mode 100644 index 00000000..04f7edb7 --- /dev/null +++ b/demos/graphics_experiments/4.cpp @@ -0,0 +1,161 @@ +#include "cru/platform/GraphicsBase.hpp" +#include "cru/platform/bootstrap/Bootstrap.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/graphics/Painter.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" + +#include <dlib/matrix.h> +#include <dlib/numeric_constants.h> +#include <cmath> + +using cru::platform::Point; + +using matrix14 = dlib::matrix<float, 1, 4>; +using matrix13 = dlib::matrix<float, 1, 3>; +using matrix44 = dlib::matrix<float, 4, 4>; + +matrix44 Identity() { + matrix44 m; + m = dlib::identity_matrix<float, 4>(); + return m; +} + +matrix44 T1(float a, float b, float c) { + auto m = Identity(); + + m(0, 3) = -a; + m(1, 3) = -b; + m(2, 3) = -c; + + return m; +} + +matrix44 T2(float theta) { + auto m = Identity(); + + m(0, 0) = -std::cos(theta); + m(0, 2) = -std::sin(theta); + m(2, 0) = std::sin(theta); + m(2, 2) = -std::cos(theta); + + return m; +} + +matrix44 T3(float phi) { + auto m = Identity(); + + m(1, 1) = std::sin(phi); + m(1, 2) = -std::cos(phi); + m(2, 1) = std::cos(phi); + m(2, 2) = std::sin(phi); + + return m; +} + +matrix44 T4(float alpha) { + auto m = Identity(); + + m(0, 0) = std::cos(alpha); + m(1, 0) = std::sin(alpha); + m(0, 1) = -std::sin(alpha); + m(1, 1) = std::cos(alpha); + + return m; +} + +matrix44 T5() { + auto m = Identity(); + + m(0, 0) = -1; + + return m; +} + +struct Args { + float a; + float b; + float c; + float theta; + float phi; + float alpha; +}; + +matrix44 Tv(Args args) { + return T1(args.a, args.b, args.c) * T2(args.theta) * T3(args.phi) * + T4(args.alpha) * T5(); +} + +matrix14 Transform(matrix14 point, Args args) { return point * Tv(args); } + +matrix14 Transform(matrix13 point, Args args) { + return matrix14{point(0), point(1), point(2), 1} * Tv(args); +} + +Point TransformTo2D(matrix14 point, float d) { + return Point{point(1), point(2)}; +} + +const float length = 100; + +matrix13 points[] = { + {0, 0, 0}, + {length, 0, 0}, + {length, 0, 0}, + {length, length, 0}, + {length, length, 0}, + {0, length, 0}, + {0, length, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, length}, + {length, 0, 0}, + {length, 0, length}, + {length, length, 0}, + {length, length, length}, + {0, length, 0}, + {0, length, length}, + {0, 0, length}, + {length, 0, length}, + {length, 0, length}, + {length, length, length}, + {length, length, length}, + {0, length, length}, + {0, length, length}, + {0, 0, length}, +}; + +const float pi = static_cast<float>(dlib::pi); + +Args args{30, 40, 50, pi / 3.f, pi / 4.f, pi / 5.f}; + +int main() { + std::vector<Point> points2d; + + for (auto p : points) { + auto point2d = TransformTo2D(Transform(std::move(p), args), length); + points2d.push_back(point2d); + } + + auto application = cru::platform::bootstrap::CreateUiApplication(); + auto window = application->CreateWindow(); + + auto brush = application->GetGraphicsFactory()->CreateSolidColorBrush( + cru::platform::colors::black); + + window->SetClientSize(cru::platform::Size(400, 400)); + + window->PaintEvent()->AddHandler([window, &brush, points2d](nullptr_t) { + auto painter = window->BeginPaint(); + painter->PushState(); + painter->ConcatTransform(cru::platform::Matrix::Translation(200, 200)); + for (int i = 0; i < points2d.size(); i += 2) { + painter->DrawLine(points2d[i], points2d[i + 1], brush.get(), 1.f); + } + painter->PopState(); + }); + + window->SetVisibility(cru::platform::gui::WindowVisibilityType::Show); + + return application->Run(); +} diff --git a/demos/graphics_experiments/CMakeLists.txt b/demos/graphics_experiments/CMakeLists.txt index 2e0237aa..103d1cc4 100644 --- a/demos/graphics_experiments/CMakeLists.txt +++ b/demos/graphics_experiments/CMakeLists.txt @@ -3,3 +3,8 @@ target_link_libraries(cru_graphics_experiment_1 PRIVATE cru_demo_base) add_executable(cru_graphics_experiment_2 2.cpp) target_link_libraries(cru_graphics_experiment_2 PRIVATE cru_demo_base) + +find_package(dlib CONFIG REQUIRED) + +add_executable(cru_graphics_experiment_4 4.cpp) +target_link_libraries(cru_graphics_experiment_4 PRIVATE cru_demo_base dlib::dlib) diff --git a/demos/main/main.cpp b/demos/main/main.cpp index d45c1618..cb07d25e 100644 --- a/demos/main/main.cpp +++ b/demos/main/main.cpp @@ -46,8 +46,8 @@ int main() { flex_layout->AddChild(text_box, 2); auto popup_menu = std::make_unique<cru::ui::components::PopupMenu>(window); - popup_menu->GetMenu()->AddTextItem(u"Item 1"); - popup_menu->GetMenu()->AddTextItem(u"Item 2000"); + popup_menu->GetMenu()->AddTextItem(u"Item 1", [] {}); + popup_menu->GetMenu()->AddTextItem(u"Item 2000", [] {}); window->MouseDownEvent()->Bubble()->AddHandler( [window, &popup_menu](cru::ui::event::MouseButtonEventArgs& e) { diff --git a/include/cru/ui/controls/TextHostControlService.hpp b/include/cru/ui/controls/TextHostControlService.hpp index 5216827b..93b74d09 100644 --- a/include/cru/ui/controls/TextHostControlService.hpp +++ b/include/cru/ui/controls/TextHostControlService.hpp @@ -81,7 +81,7 @@ class TextHostControlService : public Object { CRU_DELETE_COPY(TextHostControlService) CRU_DELETE_MOVE(TextHostControlService) - ~TextHostControlService() = default; + ~TextHostControlService(); public: bool IsEnabled() { return enable_; } diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 90896632..b0759058 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -140,6 +140,8 @@ TextHostControlService::TextHostControlService(gsl::not_null<Control*> control) shortcut_hub_.Install(control_); } +TextHostControlService::~TextHostControlService() {} + void TextHostControlService::SetEnabled(bool enable) { if (enable == this->enable_) return; this->enable_ = enable; @@ -4,6 +4,7 @@ "version": "0.0.1", "dependencies": [ "ms-gsl", - "gtest" + "gtest", + "dlib" ] } |