Skip to content

Commit

Permalink
dynamic button label
Browse files Browse the repository at this point in the history
  • Loading branch information
xland committed Mar 3, 2023
1 parent a27b586 commit 97ad235
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 44 deletions.
6 changes: 4 additions & 2 deletions RedRedStar/include/RRS/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#include "Panel.h"
#include "Color.h"
#include <memory>
#include <string>
namespace RRS {
class Panel;
class Label;
class Button : public Panel
{
public:
Button();
Button(std::wstring label);
~Button();

private:
std::wstring labelStr;
};
}

Expand Down
13 changes: 10 additions & 3 deletions RedRedStar/include/RRS/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ namespace RRS {
/// <summary>
/// show the element
/// </summary>
void Hide();
virtual void calculatePosition();
void Hide();
virtual void SetIsMouseEnter(int x, int y);
virtual void CalculatePosition();
bool GetIsMouseEnter();
/// <summary>
/// 当子元素被添加到父元素内,再把父元素添加到窗口内,此时子元素的ownerWindow为空
/// 我们通过子元素的GetOwnerWindow方法获取ownerWindow时,会遍历它的父元素,直到找到ownerWindow为止
/// 获取到子元素的ownerWindow后,这个指针会被缓存下来,下次就不用再执行遍历操作了
/// </summary>
/// <returns></returns>
Window* GetOwnerWindow();
Element* GetParentElement();
virtual void EmitClickEvent();
void SetParentElement(Element* element);
protected:
private:
friend Window;
bool isMouseEnter = false;
Expand Down
2 changes: 1 addition & 1 deletion RedRedStar/include/RRS/EventCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace RRS {
class EventCallback
{
public:
EventCallback(std::function<void(EventListener*)> callBack);
EventCallback(std::function<void(EventListener*)>&& callBack);
~EventCallback();
void Execute(EventListener* target);
int Id;
Expand Down
2 changes: 1 addition & 1 deletion RedRedStar/include/RRS/EventListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace RRS {
public:
EventListener();
~EventListener();
virtual int AddEventListener(EventType eventType, std::function<void(EventListener*)> callBack);
virtual int AddEventListener(EventType eventType, std::function<void(EventListener*)>&& callBack);
virtual void RemoveEventListener(EventType eventType, int callBackId);
void EmitEvent(EventType eventType);
protected:
Expand Down
2 changes: 1 addition & 1 deletion RedRedStar/include/RRS/Label.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions RedRedStar/include/RRS/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace RRS {
~Panel();
void Paint(SkCanvas* canvas) override;
void SetIsMouseEnter(int x, int y) override;
void EmitClickEvent() override;
/// <summary>
/// Add an element to the window
/// </summary>
Expand Down
11 changes: 3 additions & 8 deletions RedRedStar/include/RRS/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace RRS {
/// Add an element to the window
/// </summary>
void AddChildElement(std::shared_ptr<Element> element);

void SetTitle(std::wstring& title);
void SetSize(float w, float h) override;
float GetWidth() override;
float GetHeight() override;
Expand Down Expand Up @@ -92,13 +92,7 @@ namespace RRS {
/// is window shown in the center of the screen
/// </summary>
bool ShowInCenterScreen = true;
/// <summary>
/// window title
/// </summary>
std::wstring Title = L"Window";
/// <summary>
///
/// </summary>

std::vector<std::shared_ptr<Element>> Children;
Color BackgroundColor;
protected:
Expand Down Expand Up @@ -150,5 +144,6 @@ namespace RRS {
int heightMinimum = 600;
int widthMaximum = 2000;
int heightMaximum = 1400;
std::wstring title = L"Window";
};
}
12 changes: 4 additions & 8 deletions RedRedStar/src/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
#include <Windows.h>

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<Label>(L"Hello ÊÀ½ç");
auto label = std::make_shared<Label>(labelStr);
label->SetFontColor(GetColor(255, 255, 255));
label->SetAlignSelf(LayoutAlign::Center);
AddChildElement(label);
SetSize(label->GetWidth() + 50, 60);


AddEventListener(EventType::Click, [this](EventListener* arg) {
InvalidateRect(this->GetOwnerWindow()->Hwnd, nullptr, false);
});
SetSize(label->GetWidth() + 50, 60);
}
Button::~Button()
{
Expand Down
8 changes: 7 additions & 1 deletion RedRedStar/src/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ namespace RRS {
void Element::Hide() {
//todo
}
void Element::calculatePosition()
void Element::EmitClickEvent()
{
if (isMouseEnter) {
EmitEvent(RRS::EventType::Click);
}
}
void Element::CalculatePosition()
{
if (parentElement) {
xAbsolute = parentElement->xAbsolute + GetXOffset();
Expand Down
2 changes: 1 addition & 1 deletion RedRedStar/src/EventCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <random>
namespace RRS {
static int index = 0;
EventCallback::EventCallback(std::function<void(EventListener*)> callBack)
EventCallback::EventCallback(std::function<void(EventListener*)>&& callBack)
:callBack{ callBack },Id{index++}
{

Expand Down
4 changes: 2 additions & 2 deletions RedRedStar/src/EventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace RRS {
{
}
//todo 这些都是同步事件,要搞一套异步事件出来
int EventListener::AddEventListener(EventType eventType, std::function<void(EventListener*)> callBack)
int EventListener::AddEventListener(EventType eventType, std::function<void(EventListener*)>&& callBack)
{
std::shared_ptr<EventCallback> functor = std::make_shared<EventCallback>(callBack);
std::shared_ptr<EventCallback> functor = std::make_shared<EventCallback>(std::move(callBack));
dispatcher.insert({ eventType ,functor });
return functor->Id;
}
Expand Down
4 changes: 2 additions & 2 deletions RedRedStar/src/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "../include/RRS/Layout.h"
#include "modules/skparagraph/include/TextStyle.h"
namespace RRS {
Label::Label(std::wstring&& text)
Label::Label(std::wstring text)
:Text {text}
,font{new SkFont(SkTypeface::MakeFromName("STSong", SkFontStyle::Normal()),20)} //Microsoft Yahei
,fontColor { GetColor(0, 0, 0) }
Expand All @@ -22,7 +22,7 @@ void Label::SetFontColor(Color fontColor)
}
void Label::Paint(SkCanvas* canvas)
{
calculatePosition();
CalculatePosition();
SkPaint paint;
paint.setColor(fontColor);
paint.setAntiAlias(true);
Expand Down
12 changes: 10 additions & 2 deletions RedRedStar/src/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace RRS {
}
Panel::~Panel()
{

}
void Panel::regMouseHoverEvent()
{
Expand All @@ -38,8 +39,15 @@ namespace RRS {
{
backgroundColor = color;
regMouseHoverEvent();
}
}
void Panel::EmitClickEvent()
{
Element::EmitClickEvent();
for (auto& item : children)
{
item->EmitClickEvent();
}

}
void Panel::SetBackgroundColorHover(Color color)
{
Expand All @@ -56,7 +64,7 @@ namespace RRS {
}
void Panel::Paint(SkCanvas* canvas)
{
calculatePosition();
CalculatePosition();
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(GetIsMouseEnter()?backgroundColor:backgroundColorHover);
Expand Down
4 changes: 4 additions & 0 deletions RedRedStar/src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace RRS
{
App::Get()->Windows.push_back(this);
}
void Window::SetTitle(std::wstring& title)
{
this->title = title;
}
void Window::SetSize(float w, float h)
{
if (w < widthMinimum) {
Expand Down
9 changes: 7 additions & 2 deletions RedRedStar/src/WindowNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace RRS {
XWindow = (screenRect.right - width) / 2;
YWindow = (screenRect.bottom - height) / 2;
}
Hwnd = CreateWindow(windowClassName.c_str(), Title.c_str(), WS_OVERLAPPEDWINDOW, XWindow, YWindow, width, height,
Hwnd = CreateWindow(windowClassName.c_str(), title.c_str(), WS_OVERLAPPEDWINDOW, XWindow, YWindow, width, height,
nullptr, nullptr, App::Get()->HInstance, nullptr);
if (!Hwnd)
{
Expand Down Expand Up @@ -93,6 +93,12 @@ namespace RRS {
Close();
return 0;
}
case WM_LBUTTONUP: {
for (auto element : Children)
{
element->EmitClickEvent();
}
}
case WM_NCHITTEST: {
return hitTest(hwnd, lParam);
}
Expand Down Expand Up @@ -139,7 +145,6 @@ namespace RRS {
}
void Window::mouseMove(int x, int y)
{
bool flag = false;
for (auto element : Children)
{
element->SetIsMouseEnter(x, y);
Expand Down
17 changes: 7 additions & 10 deletions Sample/HelloWorld/main.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
#include <Windows.h>
#include <memory>
#include <RRS/App.h>
#include <RRS/CommonType.h>
#include <RRS/Window.h>
#include <RRS/Button.h>
#include <functional>

using namespace RRS;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow)
{
App::Init(hInstance);
std::wstring hello = L"Hello ÊÀ½ç£¡";
auto win = std::make_unique<Window>();
win->Title = L"Hello World";
win->SetTitle(hello);
win->SetFlexDirection(FlexDirection::Column);
win->SetJustifyContent(JustifyContent::Center);
win->AddEventListener(EventType::Loaded, [&win](EventListener* arg) {
auto btn = std::make_shared<Button>();
win->AddEventListener(EventType::Loaded, [&win,&hello](EventListener* arg) {
auto btn = std::make_shared<Button>(hello);
btn->SetAlignSelf(LayoutAlign::Center);
btn->AddEventListener(EventType::Click, [&win,&hello](EventListener* arg) {
MessageBox(win->Hwnd, hello.c_str(), L"ϵͳÌáʾ", MB_ICONWARNING | MB_OK | MB_DEFBUTTON1);
});
win->AddChildElement(btn);
win->Show();
});
win->AddEventListener(EventType::WindowClosed, [](EventListener* arg) {
App::Quit();
});
win->Load();

//If you want to implement your own window class to gain more control, read the following code.
//auto win = std::make_unique<WindowHelloWorld>();
//win->Load();

return App::Exec();
}

0 comments on commit 97ad235

Please sign in to comment.