* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MYAPPLICATION_ARKUICUSTOMNODE_H
#define MYAPPLICATION_ARKUICUSTOMNODE_H
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_canvas.h>
#include <native_drawing/drawing_path.h>
#include "ArkUINode.h"
namespace NativeModule {
class ArkUICustomNode : public ArkUINode {
public:
ArkUICustomNode()
: ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_CUSTOM))
{
nativeModule_->addNodeCustomEventReceiver(handle_, OnStaticCustomEvent);
nativeModule_->registerNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW_FRONT, 0, this);
nativeModule_->registerNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW, 0, this);
nativeModule_->registerNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW_BEHIND, 0, this);
OH_ArkUI_RegisterDrawCallbackOnNodeHandle(handle_, nullptr, [](void* userData) {});
}
~ArkUICustomNode() override
{
nativeModule_->removeNodeCustomEventReceiver(handle_, OnStaticCustomEvent);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW_FRONT);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW_BEHIND);
OH_ArkUI_UnregisterDrawCallbackOnNodeHandle(handle_);
}
private:
int32_t NUM_2 = 2;
int32_t NUM_3 = 3;
int32_t NUM_4 = 4;
int32_t NUM_5 = 5;
static void OnStaticCustomEvent(ArkUI_NodeCustomEvent *event)
{
auto targetId = OH_ArkUI_NodeCustomEvent_GetEventTargetId(event);
auto handle = OH_ArkUI_NodeCustomEvent_GetNodeHandle(event);
auto customNode = reinterpret_cast<ArkUICustomNode *>(OH_ArkUI_NodeCustomEvent_GetUserData(event));
auto type = OH_ArkUI_NodeCustomEvent_GetEventType(event);
switch (type) {
case ARKUI_NODE_CUSTOM_EVENT_ON_DRAW_BEHIND:
customNode->OnDrawBehind(event);
break;
case ARKUI_NODE_CUSTOM_EVENT_ON_DRAW:
customNode->OnDraw(event);
break;
case ARKUI_NODE_CUSTOM_EVENT_ON_DRAW_FRONT:
customNode->OnDrawFront(event);
break;
case ARKUI_NODE_CUSTOM_EVENT_ON_FOREGROUND_DRAW:
customNode->OnDrawForeGround(event);
break;
case ARKUI_NODE_CUSTOM_EVENT_ON_OVERLAY_DRAW:
customNode->OnDrawOverLay(event);
break;
default:
break;
}
}
void OnDrawBehind(ArkUI_NodeCustomEvent *event)
{
auto drawContext = OH_ArkUI_NodeCustomEvent_GetDrawContextInDraw(event);
auto drawCanvas = reinterpret_cast<OH_Drawing_Canvas *>(OH_ArkUI_DrawContext_GetCanvas(drawContext));
auto size = OH_ArkUI_DrawContext_GetSize(drawContext);
auto path = OH_Drawing_PathCreate();
OH_Drawing_PathMoveTo(path, size.width / NUM_5, size.height / NUM_5);
OH_Drawing_PathLineTo(path, size.width * NUM_4 / NUM_5, size.height / NUM_5);
OH_Drawing_PathLineTo(path, size.width * NUM_4 / NUM_5, size.height * NUM_4 / NUM_5);
OH_Drawing_PathLineTo(path, size.width / NUM_5, size.height * NUM_4 / NUM_5);
OH_Drawing_PathLineTo(path, size.width / NUM_5, size.height / NUM_5);
OH_Drawing_PathClose(path);
auto brush = OH_Drawing_BrushCreate();
OH_Drawing_BrushSetColor(brush, 0xFFF0FAFF);
OH_Drawing_CanvasAttachBrush(drawCanvas, brush);
OH_Drawing_CanvasDrawPath(drawCanvas, path);
OH_Drawing_BrushDestroy(brush);
OH_Drawing_PathDestroy(path);
}
void OnDraw(ArkUI_NodeCustomEvent *event)
{
auto drawContext = OH_ArkUI_NodeCustomEvent_GetDrawContextInDraw(event);
auto drawCanvas = reinterpret_cast<OH_Drawing_Canvas *>(OH_ArkUI_DrawContext_GetCanvas(drawContext));
auto size = OH_ArkUI_DrawContext_GetSize(drawContext);
auto path = OH_Drawing_PathCreate();
OH_Drawing_PathMoveTo(path, size.width / NUM_4, size.height / NUM_4);
OH_Drawing_PathLineTo(path, size.width * NUM_3 / NUM_4, size.height / NUM_4);
OH_Drawing_PathLineTo(path, size.width * NUM_3 / NUM_4, size.height * NUM_3 / NUM_4);
OH_Drawing_PathLineTo(path, size.width / NUM_4, size.height * NUM_3 / NUM_4);
OH_Drawing_PathLineTo(path, size.width / NUM_4, size.height / NUM_4);
OH_Drawing_PathClose(path);
auto brush = OH_Drawing_BrushCreate();
OH_Drawing_BrushSetColor(brush, 0xff2787D9);
OH_Drawing_CanvasAttachBrush(drawCanvas, brush);
OH_Drawing_CanvasDrawPath(drawCanvas, path);
OH_Drawing_BrushDestroy(brush);
OH_Drawing_PathDestroy(path);
}
void OnDrawFront(ArkUI_NodeCustomEvent *event)
{
auto drawContext = OH_ArkUI_NodeCustomEvent_GetDrawContextInDraw(event);
auto drawCanvas = reinterpret_cast<OH_Drawing_Canvas *>(OH_ArkUI_DrawContext_GetCanvas(drawContext));
auto size = OH_ArkUI_DrawContext_GetSize(drawContext);
auto path = OH_Drawing_PathCreate();
OH_Drawing_PathMoveTo(path, size.width / NUM_3, size.height / NUM_3);
OH_Drawing_PathLineTo(path, size.width * NUM_2 / NUM_3, size.height / NUM_3);
OH_Drawing_PathLineTo(path, size.width * NUM_2 / NUM_3, size.height * NUM_2 / NUM_3);
OH_Drawing_PathLineTo(path, size.width / NUM_3, size.height * NUM_2 / NUM_3);
OH_Drawing_PathLineTo(path, size.width / NUM_3, size.height / NUM_3);
OH_Drawing_PathClose(path);
auto brush = OH_Drawing_BrushCreate();
OH_Drawing_BrushSetColor(brush, 0xFF004AAF);
OH_Drawing_CanvasAttachBrush(drawCanvas, brush);
OH_Drawing_CanvasDrawPath(drawCanvas, path);
OH_Drawing_BrushDestroy(brush);
OH_Drawing_PathDestroy(path);
}
void OnDrawForeGround(ArkUI_NodeCustomEvent *event)
{
auto drawContext = OH_ArkUI_NodeCustomEvent_GetDrawContextInDraw(event);
auto drawCanvas = reinterpret_cast<OH_Drawing_Canvas *>(OH_ArkUI_DrawContext_GetCanvas(drawContext));
auto size = OH_ArkUI_DrawContext_GetSize(drawContext);
auto path = OH_Drawing_PathCreate();
OH_Drawing_PathMoveTo(path, size.width / NUM_2, size.height / NUM_2);
OH_Drawing_PathLineTo(path, size.width * NUM_2 / NUM_2, size.height / NUM_2);
OH_Drawing_PathLineTo(path, size.width * NUM_2 / NUM_2, size.height * NUM_2 / NUM_2);
OH_Drawing_PathLineTo(path, size.width / NUM_2, size.height * NUM_2 / NUM_2);
OH_Drawing_PathLineTo(path, size.width / NUM_2, size.height / NUM_2);
OH_Drawing_PathClose(path);
auto brush = OH_Drawing_BrushCreate();
OH_Drawing_BrushSetColor(brush, 0xFFFF0000);
OH_Drawing_CanvasAttachBrush(drawCanvas, brush);
OH_Drawing_CanvasDrawPath(drawCanvas, path);
OH_Drawing_BrushDestroy(brush);
OH_Drawing_PathDestroy(path);
}
void OnDrawOverLay(ArkUI_NodeCustomEvent *event)
{
auto drawContext = OH_ArkUI_NodeCustomEvent_GetDrawContextInDraw(event);
auto drawCanvas = reinterpret_cast<OH_Drawing_Canvas *>(OH_ArkUI_DrawContext_GetCanvas(drawContext));
auto size = OH_ArkUI_DrawContext_GetSize(drawContext);
auto path = OH_Drawing_PathCreate();
OH_Drawing_PathMoveTo(path, size.width, size.height);
OH_Drawing_PathLineTo(path, size.width, size.height);
OH_Drawing_PathLineTo(path, size.width * NUM_2, size.height * NUM_2);
OH_Drawing_PathLineTo(path, size.width, size.height * NUM_2);
OH_Drawing_PathLineTo(path, size.width, size.height / NUM_2);
OH_Drawing_PathClose(path);
auto brush = OH_Drawing_BrushCreate();
OH_Drawing_BrushSetColor(brush, 0xFF00FF00);
OH_Drawing_CanvasAttachBrush(drawCanvas, brush);
OH_Drawing_CanvasDrawPath(drawCanvas, path);
OH_Drawing_BrushDestroy(brush);
OH_Drawing_PathDestroy(path);
}
};
}
#endif