aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-05-23 23:50:00 +0800
committercrupest <crupest@outlook.com>2020-05-23 23:50:00 +0800
commitf3a8fd608a9776ef0a5f547da918a32cf6074060 (patch)
tree85b320479296ae12339ee1e28bab66ab001cb44b /include
parent75ff8a6a05afd02aaadf7e3049b0a0e305241182 (diff)
downloadcru-f3a8fd608a9776ef0a5f547da918a32cf6074060.tar.gz
cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.tar.bz2
cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.zip
...
Diffstat (limited to 'include')
-rw-r--r--include/cru/common/format.hpp97
-rw-r--r--include/cru/common/logger.hpp12
-rw-r--r--include/cru/platform/check.hpp6
-rw-r--r--include/cru/platform/graph/painter.hpp4
-rw-r--r--include/cru/ui/control.hpp6
-rw-r--r--include/cru/ui/controls/text_box.hpp6
-rw-r--r--include/cru/ui/render/scroll_render_object.hpp30
-rw-r--r--include/cru/win/graph/direct/painter.hpp8
8 files changed, 57 insertions, 112 deletions
diff --git a/include/cru/common/format.hpp b/include/cru/common/format.hpp
deleted file mode 100644
index 4276880e..00000000
--- a/include/cru/common/format.hpp
+++ /dev/null
@@ -1,97 +0,0 @@
-#pragma once
-#include "pre_config.hpp"
-
-#include <sstream>
-#include <string>
-#include <string_view>
-
-namespace cru::util {
-namespace details {
-template <typename TChar>
-struct FormatTrait {};
-
-template <>
-struct FormatTrait<char> {
- using StringType = std::string;
- using ViewType = std::string_view;
- using StreamType = std::stringstream;
- static constexpr ViewType placeholder = "{}";
-};
-
-template <>
-struct FormatTrait<wchar_t> {
- using StringType = std::wstring;
- using ViewType = std::wstring_view;
- using StreamType = std::wstringstream;
- static constexpr ViewType placeholder = L"{}";
-};
-} // namespace details
-
-template <typename TFormatTrait, typename T>
-struct Formatter {
- static typename TFormatTrait::StringType Format(const T& value) {
- typename TFormatTrait::StreamType stream;
- stream << value;
- return stream.str();
- }
-};
-
-namespace details {
-template <typename TString>
-void FormatInternal(TString& string) {
- using Trait = FormatTrait<TString::value_type>;
- constexpr const auto& placeholder = Trait::placeholder;
-
- const auto find_result = string.find(placeholder);
- if (find_result != TString::npos)
- throw std::invalid_argument("There is more placeholders than args.");
-}
-
-template <typename TString, typename T, typename... TRest>
-void FormatInternal(TString& string, const T& arg, const TRest&... args) {
- using Trait = FormatTrait<TString::value_type>;
- constexpr const auto& placeholder = Trait::placeholder;
-
- const auto find_result = string.find(placeholder);
- if (find_result == TString::npos)
- throw std::invalid_argument("There is less placeholders than args.");
-
- string.replace(find_result, 2, Formatter<Trait, T>::Format(arg));
- FormatInternal<TString>(string, args...);
-}
-} // namespace details
-
-template <typename... T>
-std::wstring Format(std::wstring format, const T&... args) {
- details::FormatInternal(format, args...);
- return format;
-}
-
-template <typename... T>
-std::string Format(std::string format, const T&... args) {
- details::FormatInternal(format, args...);
- return format;
-}
-
-// Why is two overloads below exist?
-// Because people should be able to pass string_view instance as format.
-// However, the two overloads above do not accept string_view as format due to
-// conversion from string_view to string is explicit.
-//
-// Why not just overload but SFINAE?
-// Because I want to make two overloads below worse than the two ones above.
-// Otherwise it will be ambiguous when pass const char* as format.
-//
-
-template <typename T, typename... TArgs>
-auto Format(T format, const TArgs&... args)
- -> std::enable_if_t<std::is_same_v<T, std::wstring_view>, std::wstring> {
- return Format(std::wstring{format}, args...);
-}
-
-template <typename T, typename... TArgs>
-auto Format(T format, const TArgs&... args)
- -> std::enable_if_t<std::is_same_v<T, std::string_view>, std::string> {
- return Format(std::string{format}, args...);
-}
-} // namespace cru::util
diff --git a/include/cru/common/logger.hpp b/include/cru/common/logger.hpp
index 38ddb6eb..ab3f2250 100644
--- a/include/cru/common/logger.hpp
+++ b/include/cru/common/logger.hpp
@@ -1,8 +1,8 @@
#pragma once
-
#include "cru/common/base.hpp"
-#include "cru/common/format.hpp"
+#include <fmt/format.h>
+#include <fmt/ostream.h>
#include <iostream>
#include <list>
#include <memory>
@@ -68,25 +68,25 @@ template <typename... TArgs>
void Debug([[maybe_unused]] TArgs&&... args) {
#ifdef CRU_DEBUG
Logger::GetInstance()->Log(LogLevel::Debug,
- util::Format(std::forward<TArgs>(args)...));
+ fmt::format(std::forward<TArgs>(args)...));
#endif
}
template <typename... TArgs>
void Info(TArgs&&... args) {
Logger::GetInstance()->Log(LogLevel::Info,
- util::Format(std::forward<TArgs>(args)...));
+ fmt::format(std::forward<TArgs>(args)...));
}
template <typename... TArgs>
void Warn(TArgs&&... args) {
Logger::GetInstance()->Log(LogLevel::Warn,
- util::Format(std::forward<TArgs>(args)...));
+ fmt::format(std::forward<TArgs>(args)...));
}
template <typename... TArgs>
void Error(TArgs&&... args) {
Logger::GetInstance()->Log(LogLevel::Error,
- util::Format(std::forward<TArgs>(args)...));
+ fmt::format(std::forward<TArgs>(args)...));
}
} // namespace cru::log
diff --git a/include/cru/platform/check.hpp b/include/cru/platform/check.hpp
index 6128fe5a..6e353afb 100644
--- a/include/cru/platform/check.hpp
+++ b/include/cru/platform/check.hpp
@@ -1,8 +1,8 @@
#pragma once
-#include "cru/common/format.hpp"
#include "exception.hpp"
#include "resource.hpp"
+#include <fmt/format.h>
#include <memory>
#include <type_traits>
@@ -13,7 +13,7 @@ TTarget* CheckPlatform(INativeResource* resource,
Expects(resource);
const auto result = dynamic_cast<TTarget*>(resource);
if (result == nullptr) {
- throw UnsupportPlatformException(util::Format(
+ throw UnsupportPlatformException(fmt::format(
"Try to convert resource to target platform failed. Platform id of "
"resource to convert: {} . Target platform id: {} .",
resource->GetPlatformId(), target_platform));
@@ -30,7 +30,7 @@ std::shared_ptr<TTarget> CheckPlatform(
Expects(resource);
const auto result = std::dynamic_pointer_cast<TTarget>(resource);
if (result == nullptr) {
- throw UnsupportPlatformException(util::Format(
+ throw UnsupportPlatformException(fmt::format(
"Try to convert resource to target platform failed. Platform id of "
"resource to convert: {} . Target platform id: {} .",
resource->GetPlatformId(), target_platform));
diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp
index 1f4ab7cb..b6eb5452 100644
--- a/include/cru/platform/graph/painter.hpp
+++ b/include/cru/platform/graph/painter.hpp
@@ -20,6 +20,10 @@ struct IPainter : virtual INativeResource {
virtual void DrawText(const Point& offset, ITextLayout* text_layout,
IBrush* brush) = 0;
+ virtual void PushLayer(const Rect& bounds) = 0;
+
+ virtual void PopLayer() = 0;
+
virtual void EndDraw() = 0;
};
} // namespace cru::platform::graph
diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp
index 30dc589a..d66405e6 100644
--- a/include/cru/ui/control.hpp
+++ b/include/cru/ui/control.hpp
@@ -147,12 +147,6 @@ class Control : public Object {
private:
bool is_mouse_over_ = false;
- struct {
- bool left;
- bool middle;
- bool right;
- } click_map_;
-
std::shared_ptr<platform::native::ICursor> cursor_ = nullptr;
};
} // namespace cru::ui
diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp
index c0160658..15fcb734 100644
--- a/include/cru/ui/controls/text_box.hpp
+++ b/include/cru/ui/controls/text_box.hpp
@@ -2,6 +2,8 @@
#include "../no_child_control.hpp"
#include "base.hpp"
+#include <memory>
+
namespace cru::ui::controls {
template <typename TControl>
class TextControlService;
@@ -10,6 +12,8 @@ class TextBox : public NoChildControl {
public:
static constexpr std::string_view control_type = "TextBox";
+ static TextBox* Create() { return new TextBox(); }
+
protected:
TextBox();
@@ -21,6 +25,8 @@ class TextBox : public NoChildControl {
std::string_view GetControlType() const final { return control_type; }
+ render::RenderObject* GetRenderObject() const override;
+
render::TextRenderObject* GetTextRenderObject();
const TextBoxBorderStyle& GetBorderStyle();
diff --git a/include/cru/ui/render/scroll_render_object.hpp b/include/cru/ui/render/scroll_render_object.hpp
new file mode 100644
index 00000000..1527db6c
--- /dev/null
+++ b/include/cru/ui/render/scroll_render_object.hpp
@@ -0,0 +1,30 @@
+#pragma once
+#include "render_object.hpp"
+
+#include "cru/platform/graph/util/painter.hpp"
+
+namespace cru::ui::render {
+class ScrollRenderObject : public RenderObject {
+ public:
+ ScrollRenderObject() : RenderObject(ChildMode::Single) {}
+
+ CRU_DELETE_COPY(ScrollRenderObject)
+ CRU_DELETE_MOVE(ScrollRenderObject)
+
+ ~ScrollRenderObject() override = default;
+
+ void Draw(platform::graph::IPainter* painter) override;
+
+ RenderObject* HitTest(const Point& point) override;
+
+ Point GetScrollOffset() { return scroll_offset_; }
+ void SetScrollOffset(const Point& offset);
+
+ protected:
+ void OnAddChild(RenderObject* new_child, Index position) override;
+ void OnRemoveChild(RenderObject* removed_child, Index position) override;
+
+ private:
+ Point scroll_offset_;
+};
+} // namespace cru::ui::render
diff --git a/include/cru/win/graph/direct/painter.hpp b/include/cru/win/graph/direct/painter.hpp
index 5a1fe03f..4f2164c9 100644
--- a/include/cru/win/graph/direct/painter.hpp
+++ b/include/cru/win/graph/direct/painter.hpp
@@ -4,6 +4,8 @@
#include "cru/platform/graph/painter.hpp"
+#include <vector>
+
namespace cru::platform::graph::win::direct {
class D2DPainter : public DirectResource,
public virtual IPainter,
@@ -35,6 +37,10 @@ class D2DPainter : public DirectResource,
void DrawText(const Point& offset, ITextLayout* text_layout,
IBrush* brush) override;
+ void PushLayer(const Rect& bounds) override;
+
+ void PopLayer() override;
+
void EndDraw() override final;
protected:
@@ -47,6 +53,8 @@ class D2DPainter : public DirectResource,
private:
ID2D1RenderTarget* render_target_;
+ std::vector<Microsoft::WRL::ComPtr<ID2D1Layer>> layers_;
+
bool is_drawing_ = true;
};
} // namespace cru::platform::graph::win::direct