aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-15 23:28:06 +0800
committercrupest <crupest@outlook.com>2018-09-15 23:28:06 +0800
commit683419f2856d348436ca64cfd4b3abbfc73cda89 (patch)
treeeed6f7b324ca2a4a6b7cdf7879233eb2eacedf2c
parent8248001b2506a866b6be0e22af36f8399a595da2 (diff)
downloadcru-683419f2856d348436ca64cfd4b3abbfc73cda89.tar.gz
cru-683419f2856d348436ca64cfd4b3abbfc73cda89.tar.bz2
cru-683419f2856d348436ca64cfd4b3abbfc73cda89.zip
...
-rw-r--r--CruUI/base.h15
-rw-r--r--CruUI/main.cpp17
-rw-r--r--CruUI/ui/control.cpp34
-rw-r--r--CruUI/ui/control.h11
-rw-r--r--CruUI/ui/controls/linear_layout.cpp6
5 files changed, 76 insertions, 7 deletions
diff --git a/CruUI/base.h b/CruUI/base.h
index 861bf677..99b493b1 100644
--- a/CruUI/base.h
+++ b/CruUI/base.h
@@ -4,8 +4,14 @@
#include "global_macros.h"
+#ifdef CRU_DEBUG
+#include <string>
+#include <vector>
+#else
#include <folly/String.h>
#include <folly/FBVector.h>
+#endif
+
#include <folly/Function.h>
#include <stdexcept>
@@ -19,7 +25,11 @@ namespace cru
Break
};
+#ifdef CRU_DEBUG
+ using String = std::wstring;
+#else
using String = folly::basic_fbstring<wchar_t>;
+#endif
template<typename FunctionType>
using Function = folly::Function<FunctionType>;
@@ -30,8 +40,13 @@ namespace cru
template<typename... Args>
using FlowControlAction = Function<FlowControl(Args...)>;
+#ifdef CRU_DEBUG
+ template<typename T>
+ using Vector = std::vector<T>;
+#else
template<typename T>
using Vector = folly::fbvector<T>;
+#endif
class Object
{
diff --git a/CruUI/main.cpp b/CruUI/main.cpp
index 99f9ded6..0d1b4032 100644
--- a/CruUI/main.cpp
+++ b/CruUI/main.cpp
@@ -68,10 +68,25 @@ int APIENTRY wWinMain(
const auto layout = LinearLayout::Create();
- layout->AddChild(TextBlock::Create(L"Hello World!!!"));
+ layout->GetLayoutParams()->width.mode = cru::ui::MeasureMode::Stretch;
+
+ layout->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args)
+ {
+ layout->AddChild(TextBlock::Create(L"Layout is clicked!"));
+ });
+
+ const auto text_block = TextBlock::Create(L"Hello World!!!");
+
+ text_block->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args)
+ {
+ layout->AddChild(TextBlock::Create(L"Hello world is clicked!"));
+ });
+
+ layout->AddChild(text_block);
layout->AddChild(TextBlock::Create(L"This is a very very very very very long sentence!!!"));
layout->AddChild(TextBlock::Create(L"By crupest!!!"));
+
window.AddChild(layout);
window.Show();
diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp
index 552eb096..7a705b4b 100644
--- a/CruUI/ui/control.cpp
+++ b/CruUI/ui/control.cpp
@@ -12,7 +12,7 @@ namespace cru {
namespace ui {
using namespace events;
- Control::Control(bool container) :
+ Control::Control(const bool container) :
is_container_(container),
window_(nullptr),
parent_(nullptr),
@@ -22,6 +22,11 @@ namespace cru {
size_(Size::zero),
position_cache_(),
is_mouse_inside_(false),
+ is_mouse_leave_{
+ { MouseButton::Left, true },
+ { MouseButton::Middle, true },
+ { MouseButton::Right, true }
+ },
layout_params_(new BasicLayoutParams()),
desired_size_(Size::zero)
{
@@ -148,6 +153,8 @@ namespace cru {
{
if (is_container_)
TraverseDescendantsInternal(this, predicate);
+ else
+ predicate(this);
}
Point Control::GetPositionRelative()
@@ -286,8 +293,8 @@ namespace cru {
control->OnAttachToWindow(window);
});
window->RefreshControlList();
-
}
+ Relayout();
}
void Control::OnRemoveChild(Control* child)
@@ -299,6 +306,7 @@ namespace cru {
});
window->RefreshControlList();
}
+ Relayout();
}
void Control::OnAttachToWindow(Window* window)
@@ -369,6 +377,11 @@ namespace cru {
{
}
+ void Control::OnMouseClick(MouseButtonEventArgs& args)
+ {
+
+ }
+
void Control::OnMouseEnterCore(MouseEventArgs & args)
{
is_mouse_inside_ = true;
@@ -377,6 +390,8 @@ namespace cru {
void Control::OnMouseLeaveCore(MouseEventArgs & args)
{
is_mouse_inside_ = false;
+ for (auto& is_mouse_leave : is_mouse_leave_)
+ is_mouse_leave.second = true;
}
void Control::OnMouseMoveCore(MouseEventArgs & args)
@@ -386,11 +401,17 @@ namespace cru {
void Control::OnMouseDownCore(MouseButtonEventArgs & args)
{
-
+ is_mouse_leave_[args.GetMouseButton()] = false;
}
void Control::OnMouseUpCore(MouseButtonEventArgs & args)
{
+ if (!is_mouse_leave_[args.GetMouseButton()])
+ OnMouseClickInternal(args);
+ }
+
+ void Control::OnMouseClickCore(MouseButtonEventArgs& args)
+ {
}
@@ -429,6 +450,13 @@ namespace cru {
mouse_up_event.Raise(args);
}
+ void Control::OnMouseClickInternal(MouseButtonEventArgs& args)
+ {
+ OnMouseClickCore(args);
+ OnMouseClick(args);
+ mouse_click_event.Raise(args);
+ }
+
void Control::OnGetFocus(FocusChangeEventArgs& args)
{
diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h
index 126c92d9..b889fcd5 100644
--- a/CruUI/ui/control.h
+++ b/CruUI/ui/control.h
@@ -2,6 +2,7 @@
#include "system_headers.h"
#include <optional>
+#include <unordered_map>
#include "base.h"
#include "ui_base.h"
@@ -91,7 +92,7 @@ namespace cru
return window_;
}
- //Traverse the tree rooted the control.
+ //Traverse the tree rooted the control including itself.
void TraverseDescendants(Action<Control*>&& predicate);
//*************** region: position and size ***************
@@ -177,6 +178,8 @@ namespace cru
events::MouseButtonEvent mouse_down_event;
//Raised when a mouse button is released in the control.
events::MouseButtonEvent mouse_up_event;
+ //Raised when a mouse button is pressed in the control and released in the control with mouse not leaving it between two operations.
+ events::MouseButtonEvent mouse_click_event;
events::FocusChangeEvent get_focus_event;
events::FocusChangeEvent lose_focus_event;
@@ -223,19 +226,21 @@ namespace cru
virtual void OnMouseMove(events::MouseEventArgs& args);
virtual void OnMouseDown(events::MouseButtonEventArgs& args);
virtual void OnMouseUp(events::MouseButtonEventArgs& args);
+ virtual void OnMouseClick(events::MouseButtonEventArgs& args);
virtual void OnMouseEnterCore(events::MouseEventArgs& args);
virtual void OnMouseLeaveCore(events::MouseEventArgs& args);
virtual void OnMouseMoveCore(events::MouseEventArgs& args);
virtual void OnMouseDownCore(events::MouseButtonEventArgs& args);
virtual void OnMouseUpCore(events::MouseButtonEventArgs& args);
+ virtual void OnMouseClickCore(events::MouseButtonEventArgs& args);
void OnMouseEnterInternal(events::MouseEventArgs& args);
void OnMouseLeaveInternal(events::MouseEventArgs& args);
void OnMouseMoveInternal(events::MouseEventArgs& args);
void OnMouseDownInternal(events::MouseButtonEventArgs& args);
void OnMouseUpInternal(events::MouseButtonEventArgs& args);
-
+ void OnMouseClickInternal(events::MouseButtonEventArgs& args);
//*************** region: focus event ***************
virtual void OnGetFocus(events::FocusChangeEventArgs& args);
@@ -287,6 +292,8 @@ namespace cru
bool is_mouse_inside_;
+ std::unordered_map<MouseButton, bool> is_mouse_leave_; // used for clicking determination
+
std::shared_ptr<BasicLayoutParams> layout_params_;
Size desired_size_;
};
diff --git a/CruUI/ui/controls/linear_layout.cpp b/CruUI/ui/controls/linear_layout.cpp
index 1d88a4e1..33a7855f 100644
--- a/CruUI/ui/controls/linear_layout.cpp
+++ b/CruUI/ui/controls/linear_layout.cpp
@@ -57,7 +57,11 @@ namespace cru::ui::controls
}
});
- auto actual_size_for_children = total_available_size_for_children - rest_available_size_for_children;
+ auto actual_size_for_children = total_available_size_for_children;
+ if (orientation_ == Orientation::Horizontal)
+ actual_size_for_children.width -= rest_available_size_for_children.width;
+ else
+ actual_size_for_children.height -= rest_available_size_for_children.height;
auto&& calculate_final_length = [](const MeasureLength& layout_length, const float length_for_children, const float max_child_length) -> float
{