aboutsummaryrefslogtreecommitdiff
path: root/include/cru
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-04 17:52:15 +0800
committercrupest <crupest@outlook.com>2019-04-04 17:52:15 +0800
commit4d650160a388b0192e04c454ba1a3de21b0d4cc3 (patch)
treef9c416767e039eeb7c5a37f93b70443f37c49f1d /include/cru
parenta410e2048db6f5ef6fb50e401a59b4b98b979050 (diff)
downloadcru-4d650160a388b0192e04c454ba1a3de21b0d4cc3.tar.gz
cru-4d650160a388b0192e04c454ba1a3de21b0d4cc3.tar.bz2
cru-4d650160a388b0192e04c454ba1a3de21b0d4cc3.zip
...
Diffstat (limited to 'include/cru')
-rw-r--r--include/cru/common/event.hpp56
-rw-r--r--include/cru/platform/native_window.hpp8
-rw-r--r--include/cru/platform/painter.hpp1
-rw-r--r--include/cru/platform/win/win_native_window.hpp16
-rw-r--r--include/cru/platform/win/win_painter.hpp1
-rw-r--r--include/cru/ui/window.hpp23
6 files changed, 70 insertions, 35 deletions
diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp
index ce014fb8..52d75a7b 100644
--- a/include/cru/common/event.hpp
+++ b/include/cru/common/event.hpp
@@ -3,33 +3,65 @@
#include <functional>
#include <map>
+#include <memory>
#include <utility>
namespace cru {
+using EventHandlerRevoker = std::function<void()>;
+
// A non-copyable non-movable Event class.
// It stores a list of event handlers.
template <typename... TArgs>
class Event {
+ private:
+ using EventResolver = std::function<Event*()>;
+ class EventHandlerRevokerImpl {
+ public:
+ EventHandlerRevokerImpl(const std::shared_ptr<EventResolver>& resolver,
+ long token)
+ : resolver_(resolver), token_(token) {}
+ EventHandlerRevokerImpl(const EventHandlerRevokerImpl& other) = default;
+ EventHandlerRevokerImpl(EventHandlerRevokerImpl&& other) = default;
+ EventHandlerRevokerImpl& operator=(EventHandlerRevokerImpl&& other) =
+ default;
+ EventHandlerRevokerImpl& operator=(EventHandlerRevokerImpl&& other) =
+ default;
+ ~EventHandlerRevokerImpl() = default;
+
+ void operator()() {
+ const auto true_resolver = resolver_.lock();
+ if (true_resolver) {
+ true_resolver()->RemoveHandler(token_);
+ }
+ }
+
+ private:
+ std::weak_ptr<EventResolver> resolver_;
+ long token_;
+ };
+
public:
using EventHandler = std::function<void(TArgs...)>;
- using EventHandlerToken = long;
- Event() = default;
+ Event()
+ : event_resolver_(new std::function<Event*()>([this] { return this; })) {}
Event(const Event&) = delete;
Event& operator=(const Event&) = delete;
Event(Event&&) = delete;
Event& operator=(Event&&) = delete;
~Event() = default;
- EventHandlerToken AddHandler(const EventHandler& handler) {
+ EventHandlerRevoker AddHandler(const EventHandler& handler) {
const auto token = current_token_++;
handlers_.emplace(token, handler);
- return token;
+ return EventHandlerRevoker(EventHandlerRevokerImpl(event_resolver_, token));
}
- void RemoveHandler(const EventHandlerToken token) {
- auto find_result = handlers_.find(token);
- if (find_result != handlers_.cend()) handlers_.erase(find_result);
+ template <typename... Args>
+ EventHandlerRevoker AddHandler(Args&& args...) {
+ const auto token = current_token_++;
+ handlers_.emplace(token, EventHandler(std::forward<Args>(args)...));
+ return EventHandlerRevoker(EventHandlerRevokerImpl(event_resolver_, token));
}
template <typename... Args>
@@ -39,8 +71,14 @@ class Event {
}
private:
- std::map<EventHandlerToken, EventHandler> handlers_;
+ void RemoveHandler(const long token) {
+ auto find_result = handlers_.find(token);
+ if (find_result != handlers_.cend()) handlers_.erase(find_result);
+ }
- EventHandlerToken current_token_ = 0;
+ private:
+ std::map<long, EventHandler> handlers_{};
+ long current_token_ = 0;
+ std::shared_ptr<EventResolver> event_resolver_;
};
} // namespace cru
diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native_window.hpp
index 2daf4870..f68fd3a4 100644
--- a/include/cru/platform/native_window.hpp
+++ b/include/cru/platform/native_window.hpp
@@ -35,13 +35,13 @@ struct NativeWindow : public virtual Interface {
virtual Painter* BeginPaint() = 0;
virtual Event<>* DestroyEvent() = 0;
- virtual Event<ui::Size>* ResizeEvent() = 0;
+ virtual Event<const ui::Size&>* ResizeEvent() = 0;
virtual Event<>* PaintEvent() = 0;
virtual Event<bool>* FocusEvent() = 0;
virtual Event<bool>* MouseEnterLeaveEvent() = 0;
- virtual Event<ui::Point>* MouseMoveEvent() = 0;
- virtual Event<MouseButton, ui::Point>* MouseDownEvent() = 0;
- virtual Event<MouseButton, ui::Point>* MouseUpEvent() = 0;
+ virtual Event<const ui::Point&>* MouseMoveEvent() = 0;
+ virtual Event<MouseButton, const ui::Point&>* MouseDownEvent() = 0;
+ virtual Event<MouseButton, const ui::Point&>* MouseUpEvent() = 0;
virtual Event<int>* KeyDownEvent() = 0;
virtual Event<int>* KeyUpEvent() = 0;
};
diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/painter.hpp
index 7310bc5c..eaaf61f9 100644
--- a/include/cru/platform/painter.hpp
+++ b/include/cru/platform/painter.hpp
@@ -12,6 +12,7 @@ struct TextLayout;
struct Painter : virtual Interface {
virtual Matrix GetTransform() = 0;
virtual void SetTransform(const Matrix& matrix) = 0;
+ virtual void Clear(const ui::Color& color) = 0;
virtual void StrokeRectangle(const ui::Rect& rectangle, Brush* brush,
float width) = 0;
virtual void FillRectangle(const ui::Rect& rectangle, Brush* brush) = 0;
diff --git a/include/cru/platform/win/win_native_window.hpp b/include/cru/platform/win/win_native_window.hpp
index ae19c9f3..7b93fd5c 100644
--- a/include/cru/platform/win/win_native_window.hpp
+++ b/include/cru/platform/win/win_native_window.hpp
@@ -47,17 +47,17 @@ class WinNativeWindow : public Object, public virtual NativeWindow {
Painter* BeginPaint() override;
Event<>* DestroyEvent() override { return &destroy_event_; }
- Event<ui::Size>* ResizeEvent() override { return &resize_event_; }
+ Event<const ui::Size&>* ResizeEvent() override { return &resize_event_; }
Event<>* PaintEvent() override { return &paint_event_; }
Event<bool>* FocusEvent() override { return &focus_event_; }
Event<bool>* MouseEnterLeaveEvent() override {
return &mouse_enter_leave_event_;
}
- Event<ui::Point>* MouseMoveEvent() override { return &mouse_move_event_; }
- Event<MouseButton, ui::Point>* MouseDownEvent() override {
+ Event<const ui::Point&>* MouseMoveEvent() override { return &mouse_move_event_; }
+ Event<MouseButton, const ui::Point&>* MouseDownEvent() override {
return &mouse_down_event_;
}
- Event<MouseButton, ui::Point>* MouseUpEvent() override {
+ Event<MouseButton, const ui::Point&>* MouseUpEvent() override {
return &mouse_up_event_;
}
Event<int>* KeyDownEvent() override { return &key_down_event_; }
@@ -117,13 +117,13 @@ class WinNativeWindow : public Object, public virtual NativeWindow {
std::shared_ptr<WindowRenderTarget> window_render_target_;
Event<> destroy_event_;
- Event<ui::Size> resize_event_;
+ Event<const ui::Size&> resize_event_;
Event<> paint_event_;
Event<bool> focus_event_;
Event<bool> mouse_enter_leave_event_;
- Event<ui::Point> mouse_move_event_;
- Event<MouseButton, ui::Point> mouse_down_event_;
- Event<MouseButton, ui::Point> mouse_up_event_;
+ Event<const ui::Point&> mouse_move_event_;
+ Event<MouseButton, const ui::Point&> mouse_down_event_;
+ Event<MouseButton, const ui::Point&> mouse_up_event_;
Event<int> key_down_event_;
Event<int> key_up_event_;
diff --git a/include/cru/platform/win/win_painter.hpp b/include/cru/platform/win/win_painter.hpp
index dfb981d5..3c37ccb2 100644
--- a/include/cru/platform/win/win_painter.hpp
+++ b/include/cru/platform/win/win_painter.hpp
@@ -17,6 +17,7 @@ class WinPainter : public Object, public virtual Painter {
Matrix GetTransform() override;
void SetTransform(const Matrix& matrix) override;
+ void Clear(const ui::Color& color) override;
void StrokeRectangle(const ui::Rect& rectangle, Brush* brush,
float width) override;
void FillRectangle(const ui::Rect& rectangle, Brush* brush) override;
diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp
index 043aae35..f0c790be 100644
--- a/include/cru/ui/window.hpp
+++ b/include/cru/ui/window.hpp
@@ -60,23 +60,18 @@ class Window final : public ContentControl {
void OnNativeDestroy();
void OnNativePaint();
- void OnNativeResize(float new_width, float new_height);
+ void OnNativeResize(const Size& size);
- void OnSetFocusInternal();
- void OnKillFocusInternal();
+ void OnNativeFocus(bool focus);
- void OnMouseMoveInternal(POINT point);
- void OnMouseLeaveInternal();
- void OnMouseDownInternal(MouseButton button, POINT point);
- void OnMouseUpInternal(MouseButton button, POINT point);
+ void OnNativeMouseMove(const Point& point);
+ void OnNativeMouseLeave();
+ void OnNativeMouseDown(platform::MouseButton button, const Point& point);
+ void OnNativeMouseUp(platform::MouseButton button, const Point& point);
- void OnMouseWheelInternal(short delta, POINT point);
- void OnKeyDownInternal(int virtual_code);
- void OnKeyUpInternal(int virtual_code);
- void OnCharInternal(wchar_t c);
+ void OnNativeKeyDown(int virtual_code);
+ void OnNativeKeyUp(int virtual_code);
- void OnActivatedInternal();
- void OnDeactivatedInternal();
//*************** region: event dispatcher helper ***************
@@ -86,7 +81,7 @@ class Window final : public ContentControl {
private:
platform::NativeWindow* native_window_;
- std::vector<EventHandlerToken> revoke_tokens_;
+ std::vector<EventHandlerRevoker> event_revokers_;
std::shared_ptr<render::WindowRenderObject> render_object_;