aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/platform')
-rw-r--r--include/cru/platform/Base.hpp11
-rw-r--r--include/cru/platform/Check.hpp19
-rw-r--r--include/cru/platform/Color.hpp6
-rw-r--r--include/cru/platform/Exception.hpp31
-rw-r--r--include/cru/platform/Resource.hpp9
-rw-r--r--include/cru/platform/bootstrap/Bootstrap.hpp17
-rw-r--r--include/cru/platform/graphics/Base.hpp2
-rw-r--r--include/cru/platform/graphics/Brush.hpp2
-rw-r--r--include/cru/platform/graphics/Factory.hpp2
-rw-r--r--include/cru/platform/graphics/Font.hpp2
-rw-r--r--include/cru/platform/graphics/Geometry.hpp2
-rw-r--r--include/cru/platform/graphics/Painter.hpp2
-rw-r--r--include/cru/platform/graphics/Resource.hpp4
-rw-r--r--include/cru/platform/gui/Base.hpp33
-rw-r--r--include/cru/platform/gui/Cursor.hpp4
-rw-r--r--include/cru/platform/gui/InputMethod.hpp2
-rw-r--r--include/cru/platform/gui/Keyboard.hpp7
-rw-r--r--include/cru/platform/gui/TimerHelper.hpp69
-rw-r--r--include/cru/platform/gui/UiApplication.hpp68
-rw-r--r--include/cru/platform/gui/Window.hpp26
20 files changed, 191 insertions, 127 deletions
diff --git a/include/cru/platform/Base.hpp b/include/cru/platform/Base.hpp
new file mode 100644
index 00000000..8589e6cb
--- /dev/null
+++ b/include/cru/platform/Base.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#ifdef CRU_PLATFORM_WINDOWS
+#ifdef CRU_PLATFORM_EXPORT_API
+#define CRU_PLATFORM_API __declspec(dllexport)
+#else
+#define CRU_PLATFORM_API __declspec(dllimport)
+#endif
+#else
+#define CRU_PLATFORM_API
+#endif
diff --git a/include/cru/platform/Check.hpp b/include/cru/platform/Check.hpp
index d3180582..9ecd2d1d 100644
--- a/include/cru/platform/Check.hpp
+++ b/include/cru/platform/Check.hpp
@@ -10,32 +10,31 @@
namespace cru::platform {
template <typename TTarget>
-TTarget* CheckPlatform(INativeResource* resource,
- const std::u16string_view& target_platform) {
+TTarget* CheckPlatform(IPlatformResource* resource,
+ const String& target_platform) {
Expects(resource);
const auto result = dynamic_cast<TTarget*>(resource);
if (result == nullptr) {
- throw UnsupportPlatformException(fmt::format(
+ throw UnsupportPlatformException(String::FromUtf8(fmt::format(
"Try to convert resource to target platform failed. Platform id of "
"resource to convert: {} . Target platform id: {} .",
- ToUtf8(resource->GetPlatformId()), ToUtf8(target_platform)));
+ resource->GetPlatformId().ToUtf8(), target_platform.ToUtf8())));
}
return result;
}
template <typename TTarget, typename TSource>
-std::shared_ptr<TTarget> CheckPlatform(
- const std::shared_ptr<TSource>& resource,
- const std::u16string_view& target_platform) {
- static_assert(std::is_base_of_v<INativeResource, TSource>,
+std::shared_ptr<TTarget> CheckPlatform(const std::shared_ptr<TSource>& resource,
+ const String& target_platform) {
+ static_assert(std::is_base_of_v<IPlatformResource, TSource>,
"TSource must be a subclass of INativeResource.");
Expects(resource);
const auto result = std::dynamic_pointer_cast<TTarget>(resource);
if (result == nullptr) {
- throw UnsupportPlatformException(fmt::format(
+ throw UnsupportPlatformException(String::FromUtf8(fmt::format(
"Try to convert resource to target platform failed. Platform id of "
"resource to convert: {} . Target platform id: {} .",
- ToUtf8(resource->GetPlatformId()), ToUtf8(target_platform)));
+ resource->GetPlatformId().ToUtf8(), target_platform.ToUtf8())));
}
return result;
}
diff --git a/include/cru/platform/Color.hpp b/include/cru/platform/Color.hpp
index 530695b2..a6793269 100644
--- a/include/cru/platform/Color.hpp
+++ b/include/cru/platform/Color.hpp
@@ -1,5 +1,7 @@
#pragma once
-#include "boost/functional/hash.hpp"
+#include "cru/platform/Base.hpp"
+
+#include <boost/functional/hash.hpp>
#include <cstdint>
#include <optional>
@@ -7,7 +9,7 @@
#include <unordered_map>
namespace cru::platform {
-struct Color {
+struct CRU_PLATFORM_API Color {
constexpr Color() : Color(0, 0, 0, 255) {}
constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue,
std::uint8_t alpha = 255)
diff --git a/include/cru/platform/Exception.hpp b/include/cru/platform/Exception.hpp
index 8b958a1d..379753d4 100644
--- a/include/cru/platform/Exception.hpp
+++ b/include/cru/platform/Exception.hpp
@@ -1,28 +1,43 @@
#pragma once
+#include "Base.hpp"
#include "cru/common/Base.hpp"
-
-#include <stdexcept>
+#include "cru/common/Exception.hpp"
namespace cru::platform {
-class PlatformException : public std::runtime_error {
+class CRU_PLATFORM_API PlatformException : public Exception {
public:
- using runtime_error::runtime_error; // inherit constructors
+ using Exception::Exception; // inherit constructors
+
+ CRU_DEFAULT_COPY(PlatformException)
+ CRU_DEFAULT_MOVE(PlatformException)
+
+ CRU_DEFAULT_DESTRUCTOR(PlatformException)
};
// This exception is thrown when a resource is used on another platform.
// Of course, you can't mix resources of two different platform.
// For example, Win32 Brush (may add in the future) with Direct Painter.
-class UnsupportPlatformException : public std::runtime_error {
+class CRU_PLATFORM_API UnsupportPlatformException : public Exception {
public:
- using runtime_error::runtime_error; // inherit constructors
+ using Exception::Exception; // inherit constructors
+
+ CRU_DEFAULT_COPY(UnsupportPlatformException)
+ CRU_DEFAULT_MOVE(UnsupportPlatformException)
+
+ CRU_DEFAULT_DESTRUCTOR(UnsupportPlatformException)
};
// This exception is thrown when a resource has been disposed and not usable
// again.
// For example, calling Build twice on a GeometryBuilder::Build will lead to
// this exception.
-class ReuseException : public std::runtime_error {
+class CRU_PLATFORM_API ReuseException : public Exception {
public:
- using runtime_error::runtime_error; // inherit constructors
+ using Exception::Exception; // inherit constructors
+
+ CRU_DEFAULT_COPY(ReuseException)
+ CRU_DEFAULT_MOVE(ReuseException)
+
+ CRU_DEFAULT_DESTRUCTOR(ReuseException)
};
} // namespace cru::platform
diff --git a/include/cru/platform/Resource.hpp b/include/cru/platform/Resource.hpp
index 7a85d9c1..b55da43a 100644
--- a/include/cru/platform/Resource.hpp
+++ b/include/cru/platform/Resource.hpp
@@ -1,10 +1,15 @@
#pragma once
+#include "Base.hpp"
+
#include "cru/common/Base.hpp"
+#include "cru/common/String.hpp"
#include <string_view>
namespace cru::platform {
-struct INativeResource : virtual Interface {
- virtual std::u16string_view GetPlatformId() const = 0;
+struct CRU_PLATFORM_API IPlatformResource : virtual Interface {
+ CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(IPlatformResource)
+
+ virtual String GetPlatformId() const = 0;
};
} // namespace cru::platform
diff --git a/include/cru/platform/bootstrap/Bootstrap.hpp b/include/cru/platform/bootstrap/Bootstrap.hpp
new file mode 100644
index 00000000..730df28c
--- /dev/null
+++ b/include/cru/platform/bootstrap/Bootstrap.hpp
@@ -0,0 +1,17 @@
+#pragma once
+#include "cru/platform/gui/UiApplication.hpp"
+
+#ifdef CRU_PLATFORM_WINDOWS
+#ifdef CRU_PLATFORM_BOOTSTRAP_EXPORT_API
+#define CRU_PLATFORM_BOOTSTRAP_API __declspec(dllexport)
+#else
+#define CRU_PLATFORM_BOOTSTRAP_API __declspec(dllimport)
+#endif
+#else
+#define CRU_PLATFORM_BOOTSTRAP_API
+#endif
+
+namespace cru::platform::boostrap {
+CRU_PLATFORM_BOOTSTRAP_API cru::platform::gui::IUiApplication*
+CreateUiApplication();
+}
diff --git a/include/cru/platform/graphics/Base.hpp b/include/cru/platform/graphics/Base.hpp
index e751ebdb..25881772 100644
--- a/include/cru/platform/graphics/Base.hpp
+++ b/include/cru/platform/graphics/Base.hpp
@@ -21,4 +21,4 @@ struct TextHitTestResult {
bool trailing;
bool insideText;
};
-} // namespace cru::platform::graph
+} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/Brush.hpp b/include/cru/platform/graphics/Brush.hpp
index 10c666b5..18d9705e 100644
--- a/include/cru/platform/graphics/Brush.hpp
+++ b/include/cru/platform/graphics/Brush.hpp
@@ -8,4 +8,4 @@ struct ISolidColorBrush : virtual IBrush {
virtual Color GetColor() = 0;
virtual void SetColor(const Color& color) = 0;
};
-} // namespace cru::platform::graph
+} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/Factory.hpp b/include/cru/platform/graphics/Factory.hpp
index f9018e13..4ee10204 100644
--- a/include/cru/platform/graphics/Factory.hpp
+++ b/include/cru/platform/graphics/Factory.hpp
@@ -11,7 +11,7 @@
namespace cru::platform::graphics {
// Entry point of the graph module.
-struct IGraphFactory : virtual INativeResource {
+struct IGraphFactory : virtual IPlatformResource {
virtual std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush() = 0;
virtual std::unique_ptr<IGeometryBuilder> CreateGeometryBuilder() = 0;
diff --git a/include/cru/platform/graphics/Font.hpp b/include/cru/platform/graphics/Font.hpp
index 70392a69..c8a155fc 100644
--- a/include/cru/platform/graphics/Font.hpp
+++ b/include/cru/platform/graphics/Font.hpp
@@ -5,4 +5,4 @@ namespace cru::platform::graphics {
struct IFont : virtual IGraphResource {
virtual float GetFontSize() = 0;
};
-} // namespace cru::platform::graph
+} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/Geometry.hpp b/include/cru/platform/graphics/Geometry.hpp
index b0ce6ad9..cc8f8761 100644
--- a/include/cru/platform/graphics/Geometry.hpp
+++ b/include/cru/platform/graphics/Geometry.hpp
@@ -17,4 +17,4 @@ struct IGeometryBuilder : virtual IGraphResource {
virtual std::unique_ptr<IGeometry> Build() = 0;
};
-} // namespace cru::platform::graph
+} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/Painter.hpp b/include/cru/platform/graphics/Painter.hpp
index f75ea52b..24592f95 100644
--- a/include/cru/platform/graphics/Painter.hpp
+++ b/include/cru/platform/graphics/Painter.hpp
@@ -3,7 +3,7 @@
namespace cru::platform::graphics {
-struct IPainter : virtual INativeResource {
+struct IPainter : virtual IPlatformResource {
virtual Matrix GetTransform() = 0;
virtual void SetTransform(const Matrix& matrix) = 0;
diff --git a/include/cru/platform/graphics/Resource.hpp b/include/cru/platform/graphics/Resource.hpp
index a1625ce4..212e5b18 100644
--- a/include/cru/platform/graphics/Resource.hpp
+++ b/include/cru/platform/graphics/Resource.hpp
@@ -4,7 +4,7 @@
namespace cru::platform::graphics {
struct IGraphFactory;
-struct IGraphResource : virtual INativeResource {
+struct IGraphResource : virtual IPlatformResource {
virtual IGraphFactory* GetGraphFactory() = 0;
};
-} // namespace cru::platform::graph
+} // namespace cru::platform::graphics
diff --git a/include/cru/platform/gui/Base.hpp b/include/cru/platform/gui/Base.hpp
index fd9d265c..4c2a8034 100644
--- a/include/cru/platform/gui/Base.hpp
+++ b/include/cru/platform/gui/Base.hpp
@@ -1,11 +1,20 @@
#pragma once
-#include "Keyboard.hpp"
#include "cru/common/Base.hpp"
#include "cru/common/Bitmask.hpp"
#include "cru/platform/graphics/Base.hpp"
#include "../Resource.hpp"
+#ifdef CRU_PLATFORM_WINDOWS
+#ifdef CRU_PLATFORM_GUI_EXPORT_API
+#define CRU_PLATFORM_GUI_API __declspec(dllexport)
+#else
+#define CRU_PLATFORM_GUI_API __declspec(dllimport)
+#endif
+#else
+#define CRU_PLATFORM_GUI_API
+#endif
+
namespace cru::platform::gui {
struct ICursor;
struct ICursorManager;
@@ -24,26 +33,4 @@ constexpr MouseButton left{0b1};
constexpr MouseButton middle{0b10};
constexpr MouseButton right{0b100};
} // namespace mouse_buttons
-
-struct NativeMouseButtonEventArgs {
- MouseButton button;
- Point point;
- KeyModifier modifier;
-};
-
-struct NativeMouseWheelEventArgs {
- // Positive means down. Negative means up.
- float delta;
- Point point;
- KeyModifier modifier;
-};
-
-struct NativeKeyEventArgs {
- KeyCode key;
- KeyModifier modifier;
-};
-
-enum class FocusChangeType { Gain, Lost };
-
-enum class MouseEnterLeaveType { Enter, Leave };
} // namespace cru::platform::gui
diff --git a/include/cru/platform/gui/Cursor.hpp b/include/cru/platform/gui/Cursor.hpp
index 316496a0..abc8b064 100644
--- a/include/cru/platform/gui/Cursor.hpp
+++ b/include/cru/platform/gui/Cursor.hpp
@@ -6,9 +6,9 @@
namespace cru::platform::gui {
enum class SystemCursorType { Arrow, Hand, IBeam };
-struct ICursor : virtual INativeResource {};
+struct ICursor : virtual IPlatformResource {};
-struct ICursorManager : virtual INativeResource {
+struct ICursorManager : virtual IPlatformResource {
virtual std::shared_ptr<ICursor> GetSystemCursor(SystemCursorType type) = 0;
// TODO: Add method to create cursor.
diff --git a/include/cru/platform/gui/InputMethod.hpp b/include/cru/platform/gui/InputMethod.hpp
index 9d090eab..9b07959c 100644
--- a/include/cru/platform/gui/InputMethod.hpp
+++ b/include/cru/platform/gui/InputMethod.hpp
@@ -22,7 +22,7 @@ struct CompositionText {
TextRange selection;
};
-struct IInputMethodContext : virtual INativeResource {
+struct IInputMethodContext : virtual IPlatformResource {
// Return true if you should draw composition text manually. Return false if
// system will take care of that for you.
virtual bool ShouldManuallyDrawCompositionText() = 0;
diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp
index 6c29239b..b93b44fa 100644
--- a/include/cru/platform/gui/Keyboard.hpp
+++ b/include/cru/platform/gui/Keyboard.hpp
@@ -1,5 +1,6 @@
#pragma once
#include "cru/common/Bitmask.hpp"
+#include "cru/platform/gui/Base.hpp"
#include <string>
#include <string_view>
@@ -122,7 +123,7 @@ struct KeyModifiers {
static constexpr KeyModifier alt{0b100};
};
-std::u16string_view ToString(KeyCode key_code);
-std::u16string ToString(KeyModifier key_modifier,
- std::u16string_view separator = u"+");
+CRU_PLATFORM_GUI_API std::u16string_view ToString(KeyCode key_code);
+CRU_PLATFORM_GUI_API std::u16string ToString(
+ KeyModifier key_modifier, std::u16string_view separator = u"+");
} // namespace cru::platform::gui
diff --git a/include/cru/platform/gui/TimerHelper.hpp b/include/cru/platform/gui/TimerHelper.hpp
new file mode 100644
index 00000000..a61d2f04
--- /dev/null
+++ b/include/cru/platform/gui/TimerHelper.hpp
@@ -0,0 +1,69 @@
+#pragma once
+#include "UiApplication.hpp"
+
+namespace cru::platform::gui {
+
+class TimerAutoCanceler {
+ public:
+ TimerAutoCanceler() : id_(0) {}
+ explicit TimerAutoCanceler(long long id) : id_(id) {}
+
+ CRU_DELETE_COPY(TimerAutoCanceler)
+
+ TimerAutoCanceler(TimerAutoCanceler&& other) : id_(other.id_) {
+ other.id_ = 0;
+ }
+
+ TimerAutoCanceler& operator=(TimerAutoCanceler&& other) {
+ if (&other == this) {
+ return *this;
+ }
+ Reset(other.id_);
+ other.id_ = 0;
+ return *this;
+ }
+
+ TimerAutoCanceler& operator=(long long other) {
+ return this->operator=(TimerAutoCanceler(other));
+ }
+
+ ~TimerAutoCanceler() { Reset(); }
+
+ long long Release() {
+ auto temp = id_;
+ id_ = 0;
+ return temp;
+ }
+
+ void Reset(long long id = 0) {
+ if (id_ > 0) IUiApplication::GetInstance()->CancelTimer(id_);
+ id_ = id;
+ }
+
+ explicit operator bool() const { return id_; }
+
+ private:
+ long long id_;
+};
+
+class TimerListAutoCanceler {
+ public:
+ TimerListAutoCanceler() = default;
+ CRU_DELETE_COPY(TimerListAutoCanceler)
+ CRU_DEFAULT_MOVE(TimerListAutoCanceler)
+ ~TimerListAutoCanceler() = default;
+
+ TimerListAutoCanceler& operator+=(long long id) {
+ list_.push_back(TimerAutoCanceler(id));
+ return *this;
+ }
+
+ void Clear() { list_.clear(); }
+
+ bool IsEmpty() const { return list_.empty(); }
+
+ private:
+ std::vector<TimerAutoCanceler> list_;
+};
+
+} // namespace cru::platform::gui
diff --git a/include/cru/platform/gui/UiApplication.hpp b/include/cru/platform/gui/UiApplication.hpp
index 5a5b0b13..407e4fa1 100644
--- a/include/cru/platform/gui/UiApplication.hpp
+++ b/include/cru/platform/gui/UiApplication.hpp
@@ -20,7 +20,7 @@ struct CreateWindowFlags {
};
// The entry point of a ui application.
-struct IUiApplication : public virtual INativeResource {
+struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource {
public:
static IUiApplication* GetInstance() { return instance; }
@@ -66,70 +66,4 @@ struct IUiApplication : public virtual INativeResource {
virtual ICursorManager* GetCursorManager() = 0;
};
-
-class TimerAutoCanceler {
- public:
- TimerAutoCanceler() : id_(0) {}
- explicit TimerAutoCanceler(long long id) : id_(id) {}
-
- CRU_DELETE_COPY(TimerAutoCanceler)
-
- TimerAutoCanceler(TimerAutoCanceler&& other) : id_(other.id_) {
- other.id_ = 0;
- }
-
- TimerAutoCanceler& operator=(TimerAutoCanceler&& other) {
- if (&other == this) {
- return *this;
- }
- Reset(other.id_);
- other.id_ = 0;
- return *this;
- }
-
- TimerAutoCanceler& operator=(long long other) {
- return this->operator=(TimerAutoCanceler(other));
- }
-
- ~TimerAutoCanceler() { Reset(); }
-
- long long Release() {
- auto temp = id_;
- id_ = 0;
- return temp;
- }
-
- void Reset(long long id = 0) {
- if (id_ > 0) IUiApplication::GetInstance()->CancelTimer(id_);
- id_ = id;
- }
-
- explicit operator bool() const { return id_; }
-
- private:
- long long id_;
-};
-
-class TimerListAutoCanceler {
- public:
- TimerListAutoCanceler() = default;
- CRU_DELETE_COPY(TimerListAutoCanceler)
- CRU_DEFAULT_MOVE(TimerListAutoCanceler)
- ~TimerListAutoCanceler() = default;
-
- TimerListAutoCanceler& operator+=(long long id) {
- list_.push_back(TimerAutoCanceler(id));
- return *this;
- }
-
- void Clear() { list_.clear(); }
-
- bool IsEmpty() const { return list_.empty(); }
-
- private:
- std::vector<TimerAutoCanceler> list_;
-};
-
-// Bootstrap from this.
-std::unique_ptr<IUiApplication> CreateUiApplication();
} // namespace cru::platform::gui
diff --git a/include/cru/platform/gui/Window.hpp b/include/cru/platform/gui/Window.hpp
index b2129322..3502868a 100644
--- a/include/cru/platform/gui/Window.hpp
+++ b/include/cru/platform/gui/Window.hpp
@@ -1,14 +1,38 @@
#pragma once
#include "Base.hpp"
+#include "Keyboard.hpp"
+
#include "cru/common/Event.hpp"
#include <string_view>
namespace cru::platform::gui {
+enum class FocusChangeType { Gain, Lost };
+
+enum class MouseEnterLeaveType { Enter, Leave };
+
+struct NativeMouseButtonEventArgs {
+ MouseButton button;
+ Point point;
+ KeyModifier modifier;
+};
+
+struct NativeMouseWheelEventArgs {
+ // Positive means down. Negative means up.
+ float delta;
+ Point point;
+ KeyModifier modifier;
+};
+
+struct NativeKeyEventArgs {
+ KeyCode key;
+ KeyModifier modifier;
+};
+
// Represents a native window, which exposes some low-level events and
// operations.
-struct INativeWindow : virtual INativeResource {
+struct INativeWindow : virtual IPlatformResource {
virtual void Close() = 0;
virtual INativeWindow* GetParent() = 0;