Skip to content

Commit

Permalink
auto hover event
Browse files Browse the repository at this point in the history
  • Loading branch information
xland committed Mar 3, 2023
1 parent 6fa27e1 commit a27b586
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 31 deletions.
Binary file modified Doc/img/helloWorld.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions RedRedStar/include/RRS/CommonType.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace RRS {
Resize,
Focs,
Blur,
Click,
/// <summary>
///
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions RedRedStar/include/RRS/Label.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#pragma once
#include "Element.h"
#include <string>
class SkFont;
namespace RRS {
class Label:public Element
{
public:
Label(const char* text);
Label(std::wstring&& text);
void Paint(SkCanvas* canvas) override;
const char* Text;
void SetFontColor(Color fontColor);
std::wstring Text;
private:
Color fontColor;
SkFont* font;
float advanceX;
float advanceY;
Expand Down
5 changes: 5 additions & 0 deletions RedRedStar/include/RRS/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ namespace RRS {
void AddChildElement(std::shared_ptr<Element> element);
void SetBackgroundColor(Color color);
void SetBackgroundColorHover(Color color);
void SetBorderRadius(float borderRadius);
private:
void regMouseHoverEvent();
int hoverId=-1;
int hoverOffId = -1;
Color backgroundColor;
Color backgroundColorHover;
float borderRadius = 0.f;
/// <summary>
///
/// </summary>
Expand Down
13 changes: 11 additions & 2 deletions RedRedStar/src/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "../include/RRS/Panel.h"
#include "../include/RRS/Label.h"
#include "../include/RRS/Color.h"
#include "../include/RRS/Window.h"
#include "include/core/SkCanvas.h"
#include <Windows.h>

namespace RRS {
Button::Button()
Expand All @@ -11,10 +13,17 @@ namespace RRS {
SetBackgroundColorHover(GetColor(28, 88, 156));
SetFlexDirection(FlexDirection::Column);
SetJustifyContent(JustifyContent::Center);
auto label = std::make_shared<Label>("Hello World");
this->SetBorderRadius(4.f);
auto label = std::make_shared<Label>(L"Hello ÊÀ½ç");
label->SetFontColor(GetColor(255, 255, 255));
label->SetAlignSelf(LayoutAlign::Center);
AddChildElement(label);
SetSize(380, 120);
SetSize(label->GetWidth() + 50, 60);


AddEventListener(EventType::Click, [this](EventListener* arg) {
InvalidateRect(this->GetOwnerWindow()->Hwnd, nullptr, false);
});
}
Button::~Button()
{
Expand Down
2 changes: 1 addition & 1 deletion RedRedStar/src/DisplayParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace RRS {
DisplayParams()
: colorType(kN32_SkColorType)
, fColorSpace(nullptr)
, fMSAASampleCount(8)
, fMSAASampleCount(8) //重要
, fSurfaceProps(0, kRGB_H_SkPixelGeometry)
, fDisableVsync(false)
, fDelayDrawableAcquisition(false)
Expand Down
43 changes: 25 additions & 18 deletions RedRedStar/src/Label.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
#include "include/core/SkFont.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFontMgr.h"
#include "../include/RRS/Label.h"
#include "../include/RRS/Layout.h"
#include "modules/skparagraph/include/TextStyle.h"
namespace RRS {
Label::Label(const char* text):Text {text},font{new SkFont(nullptr,40)}
{
SkRect rect;
SkString str{ Text };
font->measureText(Text, strlen(Text), SkTextEncoding::kUTF8, &rect);
font->setSubpixel(true);
advanceX = rect.x();
advanceY = -rect.y();
SetSize(rect.width(), rect.height());
}
void Label::Paint(SkCanvas* canvas)
{
calculatePosition();
SkPaint paint;
paint.setColor(SK_ColorBLACK);
//paint.setAntiAlias(true);
canvas->drawString(Text, xAbsolute+advanceX, yAbsolute+advanceY, *font, paint);
}
Label::Label(std::wstring&& text)
:Text {text}
,font{new SkFont(SkTypeface::MakeFromName("STSong", SkFontStyle::Normal()),20)} //Microsoft Yahei
,fontColor { GetColor(0, 0, 0) }
{
SkRect rect;
font->measureText(Text.data(), wcslen(Text.data()) * 2, SkTextEncoding::kUTF16, &rect); //strlen(Text)
advanceX = rect.x();
advanceY = -rect.y();
SetSize(rect.width(), rect.height());
}
void Label::SetFontColor(Color fontColor)
{
this->fontColor = fontColor;
}
void Label::Paint(SkCanvas* canvas)
{
calculatePosition();
SkPaint paint;
paint.setColor(fontColor);
paint.setAntiAlias(true);
//canvas->drawString(SkString{ Text }, xAbsolute + advanceX, yAbsolute + advanceY, *font, paint);
canvas->drawSimpleText(Text.data(), wcslen(Text.data()) * 2, SkTextEncoding::kUTF16, xAbsolute + advanceX, yAbsolute + advanceY, *font, paint);
}
}
48 changes: 40 additions & 8 deletions RedRedStar/src/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,70 @@

namespace RRS {
Panel::Panel()
:backgroundColor { GetColor(255, 255, 255) }
,backgroundColorHover { GetColor(255, 255, 255) }
{
AddEventListener(EventType::MouseOver, [this](EventListener* arg) {
//InvalidateRect(OwnerWindow->Hwnd, nullptr, false);
});
AddEventListener(EventType::MouseOut, [this](EventListener* arg) {
//InvalidateRect(OwnerWindow->Hwnd, nullptr, false);
});

}
Panel::~Panel()
{
}
void Panel::regMouseHoverEvent()
{
if (backgroundColor != backgroundColorHover && hoverId == -1 && hoverOffId == -1) {
hoverId = AddEventListener(EventType::MouseOver, [this](EventListener* arg) {
InvalidateRect(this->GetOwnerWindow()->Hwnd, nullptr, false);
});
hoverOffId = AddEventListener(EventType::MouseOut, [this](EventListener* arg) {
InvalidateRect(this->GetOwnerWindow()->Hwnd, nullptr, false);
});
}
if (backgroundColor == backgroundColorHover && hoverId != -1 && hoverOffId != -1)
{
RemoveEventListener(EventType::MouseOver, hoverId);
RemoveEventListener(EventType::MouseOver, hoverOffId);
hoverId = -1;
hoverOffId = -1;
}
}
void Panel::SetBackgroundColor(Color color)
{
if (backgroundColor != color) {
if (backgroundColor != color)
{
backgroundColor = color;
regMouseHoverEvent();
}

}
void Panel::SetBackgroundColorHover(Color color)
{
if (backgroundColorHover != color)
{
backgroundColorHover = color;
regMouseHoverEvent();
}
}

void Panel::SetBorderRadius(float borderRadius)
{
this->borderRadius = borderRadius;
}
void Panel::Paint(SkCanvas* canvas)
{
calculatePosition();
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(GetIsMouseEnter()?backgroundColor:backgroundColorHover);
paint.setStrokeJoin(SkPaint::Join::kRound_Join);
SkRect rect = SkRect::MakeXYWH(xAbsolute, yAbsolute, GetWidth(), GetHeight());
canvas->drawRoundRect(rect, 12.0, 12.0, paint);
if (borderRadius != 0.f) {
canvas->drawRoundRect(rect, borderRadius, borderRadius, paint);
}
else
{
canvas->drawRect(rect, paint);
}

for (auto element : children)
{
element->Paint(canvas);
Expand Down

0 comments on commit a27b586

Please sign in to comment.