From 97ad23566edf1e276f090a74d8572c89e9ae0f99 Mon Sep 17 00:00:00 2001 From: liuxiaolun Date: Fri, 3 Mar 2023 17:09:25 +0800 Subject: [PATCH] dynamic button label --- RedRedStar/include/RRS/Button.h | 6 ++++-- RedRedStar/include/RRS/Element.h | 13 ++++++++++--- RedRedStar/include/RRS/EventCallback.h | 2 +- RedRedStar/include/RRS/EventListener.h | 2 +- RedRedStar/include/RRS/Label.h | 2 +- RedRedStar/include/RRS/Panel.h | 1 + RedRedStar/include/RRS/Window.h | 11 +++-------- RedRedStar/src/Button.cpp | 12 ++++-------- RedRedStar/src/Element.cpp | 8 +++++++- RedRedStar/src/EventCallback.cpp | 2 +- RedRedStar/src/EventListener.cpp | 4 ++-- RedRedStar/src/Label.cpp | 4 ++-- RedRedStar/src/Panel.cpp | 12 ++++++++++-- RedRedStar/src/Window.cpp | 4 ++++ RedRedStar/src/WindowNative.cpp | 9 +++++++-- Sample/HelloWorld/main.cpp | 17 +++++++---------- 16 files changed, 65 insertions(+), 44 deletions(-) diff --git a/RedRedStar/include/RRS/Button.h b/RedRedStar/include/RRS/Button.h index 7fc770a..2137b82 100644 --- a/RedRedStar/include/RRS/Button.h +++ b/RedRedStar/include/RRS/Button.h @@ -2,15 +2,17 @@ #include "Panel.h" #include "Color.h" #include +#include namespace RRS { class Panel; class Label; class Button : public Panel { public: - Button(); + Button(std::wstring label); ~Button(); - + private: + std::wstring labelStr; }; } diff --git a/RedRedStar/include/RRS/Element.h b/RedRedStar/include/RRS/Element.h index ecaa2f8..ee84968 100644 --- a/RedRedStar/include/RRS/Element.h +++ b/RedRedStar/include/RRS/Element.h @@ -19,14 +19,21 @@ namespace RRS { /// /// show the element /// - void Hide(); - virtual void calculatePosition(); + void Hide(); virtual void SetIsMouseEnter(int x, int y); + virtual void CalculatePosition(); bool GetIsMouseEnter(); + /// + /// 当子元素被添加到父元素内,再把父元素添加到窗口内,此时子元素的ownerWindow为空 + /// 我们通过子元素的GetOwnerWindow方法获取ownerWindow时,会遍历它的父元素,直到找到ownerWindow为止 + /// 获取到子元素的ownerWindow后,这个指针会被缓存下来,下次就不用再执行遍历操作了 + /// + /// Window* GetOwnerWindow(); Element* GetParentElement(); + virtual void EmitClickEvent(); void SetParentElement(Element* element); - + protected: private: friend Window; bool isMouseEnter = false; diff --git a/RedRedStar/include/RRS/EventCallback.h b/RedRedStar/include/RRS/EventCallback.h index 7700f1c..0c3a787 100644 --- a/RedRedStar/include/RRS/EventCallback.h +++ b/RedRedStar/include/RRS/EventCallback.h @@ -7,7 +7,7 @@ namespace RRS { class EventCallback { public: - EventCallback(std::function callBack); + EventCallback(std::function&& callBack); ~EventCallback(); void Execute(EventListener* target); int Id; diff --git a/RedRedStar/include/RRS/EventListener.h b/RedRedStar/include/RRS/EventListener.h index e388d3e..44d54eb 100644 --- a/RedRedStar/include/RRS/EventListener.h +++ b/RedRedStar/include/RRS/EventListener.h @@ -12,7 +12,7 @@ namespace RRS { public: EventListener(); ~EventListener(); - virtual int AddEventListener(EventType eventType, std::function callBack); + virtual int AddEventListener(EventType eventType, std::function&& callBack); virtual void RemoveEventListener(EventType eventType, int callBackId); void EmitEvent(EventType eventType); protected: diff --git a/RedRedStar/include/RRS/Label.h b/RedRedStar/include/RRS/Label.h index eb25683..87be94b 100644 --- a/RedRedStar/include/RRS/Label.h +++ b/RedRedStar/include/RRS/Label.h @@ -6,7 +6,7 @@ namespace RRS { class Label:public Element { public: - Label(std::wstring&& text); + Label(std::wstring text); void Paint(SkCanvas* canvas) override; void SetFontColor(Color fontColor); std::wstring Text; diff --git a/RedRedStar/include/RRS/Panel.h b/RedRedStar/include/RRS/Panel.h index 0eda2f1..17f9d09 100644 --- a/RedRedStar/include/RRS/Panel.h +++ b/RedRedStar/include/RRS/Panel.h @@ -10,6 +10,7 @@ namespace RRS { ~Panel(); void Paint(SkCanvas* canvas) override; void SetIsMouseEnter(int x, int y) override; + void EmitClickEvent() override; /// /// Add an element to the window /// diff --git a/RedRedStar/include/RRS/Window.h b/RedRedStar/include/RRS/Window.h index 0beb105..3f9f691 100644 --- a/RedRedStar/include/RRS/Window.h +++ b/RedRedStar/include/RRS/Window.h @@ -50,7 +50,7 @@ namespace RRS { /// Add an element to the window /// void AddChildElement(std::shared_ptr element); - + void SetTitle(std::wstring& title); void SetSize(float w, float h) override; float GetWidth() override; float GetHeight() override; @@ -92,13 +92,7 @@ namespace RRS { /// is window shown in the center of the screen /// bool ShowInCenterScreen = true; - /// - /// window title - /// - std::wstring Title = L"Window"; - /// - /// - /// + std::vector> Children; Color BackgroundColor; protected: @@ -150,5 +144,6 @@ namespace RRS { int heightMinimum = 600; int widthMaximum = 2000; int heightMaximum = 1400; + std::wstring title = L"Window"; }; } \ No newline at end of file diff --git a/RedRedStar/src/Button.cpp b/RedRedStar/src/Button.cpp index c4d6cdf..6ef966b 100644 --- a/RedRedStar/src/Button.cpp +++ b/RedRedStar/src/Button.cpp @@ -7,23 +7,19 @@ #include namespace RRS { - Button::Button() + Button::Button(std::wstring labelStr): labelStr {labelStr} { + SetBackgroundColor(GetColor(88, 28, 156)); SetBackgroundColorHover(GetColor(28, 88, 156)); SetFlexDirection(FlexDirection::Column); SetJustifyContent(JustifyContent::Center); this->SetBorderRadius(4.f); - auto label = std::make_shared