mcu_hi3321_watch/tjd/ui/app/wf_select/TjdUiWatchFaceView.cpp
2025-05-26 20:15:20 +08:00

237 lines
7.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description:
*
* Author: huangshuyi
*
* Create: 2024-7
*--------------------------------------------------------------------------*/
#include "View.h"
#include <string>
#include "ChangeSliceListener.h"
#include "TjdUiImageIds.h"
#include "TjdUiMemManage.h"
#include "TjdUiWatchFaceCtrl.h"
#include "TjdUiWatchFaceView.h"
#include "common/image_cache_manager.h"
#include "components/root_view.h"
#include "sql_setting.h"
#include "sys_config.h"
#include <algorithm>
using namespace OHOS;
namespace TJD {
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) //一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) //警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) //错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__)
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
static constexpr uint16_t ITEM_SIZE = 246;
static constexpr int16_t OFFSET_Y = 30;
static constexpr uint32_t ROLLER_ITEM = 8;
#define MAIN_WF_SELECT_BG_BIN_PATH TJD_IMAGE_PATH "img_wf_select.bin"
#define MAIN_WF_0_BIN_PATH TJD_IMAGE_PATH "img_wf_preview.bin"
class TjdUiWatchFaceView::WfRollerViewListener : public OHOS::UIRollerView::OnScrollListener
{
public:
struct RollerItemInfo
{
float angle;
std::string name;
};
virtual ~WfRollerViewListener() {}
void OnScroll(OHOS::UIRollerView *roller, std::string currentItemName, float currentItemAngle) override
{
TjdUiWatchFaceView *watchFaceView = TjdUiWatchFaceView::GetInstance();
if (watchFaceView == nullptr) {
return;
}
watchFaceView->presenter_->ResetTimer();
uint8_t itemCnt = watchFaceView->GetWfImageList().size();
UIViewGroup *child = (UIViewGroup *)roller->GetChildrenHead();
UIViewGroup *child270 = nullptr;
while (child != nullptr) {
RollerItemInfo *info = (RollerItemInfo *)child->GetExtraMsg()->elementPtr;
int16_t opa = 0;
float angle = fmod(info->angle, 360.0f); // 确保角度在0到360度之间
if (info->angle >= 0 && info->angle <= 180) {
// 角度从0到180透明度从255到50
opa = EasingEquation::LinearEaseNone(255, 60, angle, 180);
} else if (info->angle > 180 && info->angle <= 360) {
// 角度从180到360透明度从50到255
opa = EasingEquation::LinearEaseNone(60, 255, angle - 180, 180);
} else {
// 处理角度超出范围的情况
opa = 255;
}
child->SetOpaScale(opa);
if (itemCnt > ROLLER_ITEM) {
if ((int16_t)angle == 270) {
child270 = child;
}
}
child = (UIViewGroup *)child->GetNextSibling();
}
}
};
static TjdUiWatchFaceView *g_watchFaceView = nullptr;
TjdUiWatchFaceView::TjdUiWatchFaceView() { g_watchFaceView = this; }
TjdUiWatchFaceView::~TjdUiWatchFaceView() { g_watchFaceView = nullptr; }
TjdUiWatchFaceView *TjdUiWatchFaceView::GetInstance(void) { return g_watchFaceView; }
void TjdUiWatchFaceView::OnStart() { LoadDialInfo(); }
void TjdUiWatchFaceView::OnStop()
{
if (coverflow2_ != nullptr) {
coverflow2_->RemoveAll();
coverflow2_->ClearFocus();
delete coverflow2_;
coverflow2_ = nullptr;
}
for (auto *it : coverflowItemList_) {
delete it;
}
uint32_t itemCnt = wfImageList.size();
for (uint8_t i = 0; i < itemCnt; i++) {
if (wfImageList[i].fileOffset < 0) {
OHOS::ImageCacheManager::GetInstance().UnloadSingleRes(wfImageList[i].path);
} else {
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(wfImageList[i].path);
}
}
}
uint32_t TjdUiWatchFaceView::GetCurCoverflowItemWfId(void)
{
uint16_t childNum = coverflow2_->GetChildrenNumber();
int16_t contMidX = static_cast<int16_t>(OHOS::Screen::GetInstance().GetWidth() / 2);
int16_t minOffset = contMidX;
uint8_t centerIndex = 0;
for (uint8_t i = 0; i < childNum; i++) {
auto rect = coverflow2_->GetViewByIndex(i)->GetOrigRect();
int16_t x = rect.GetLeft();
int16_t x1 = rect.GetRight();
int16_t xMid = static_cast<int16_t>(x + (x1 - x + 1) / 2);
int16_t distance = MATH_ABS(xMid - contMidX);
if (distance < minOffset) {
centerIndex = i;
minOffset = distance;
}
}
if (centerIndex < 0 || centerIndex >= childNum) {
static_print_error("GetCoverflowItemWfId: centerIndex out of range");
return 0;
}
auto it = coverflowItemList_.begin();
std::advance(it, centerIndex);
return (*it)->GetWfId();
}
void TjdUiWatchFaceView::LoadDialInfo(void)
{
wfImageList.clear();
uint32_t itemCnt = 0;
WfSelectInfo wfInfo = {};
WfImageInfo wfImageInfo = {};
uint32_t *wfInfoList = TjdUiWfGetIDList(&itemCnt);
for (uint8_t i = 0; i < itemCnt; i++) {
if (TjdUiWfGetSelectInfobyID(wfInfoList[i], &wfInfo)) {
wfImageInfo.wfId = wfInfo.wfId;
wfImageInfo.path = wfInfo.PreviewImgFullPath;
wfImageInfo.name = wfInfo.name;
wfImageInfo.fileOffset = wfInfo.PreviewImgFileOffset;
wfImageInfo.fileImgId = wfInfo.PreviewImgId;
wfImageInfo.wfType = 0;
wfImageList.push_back(wfImageInfo);
}
}
}
void TjdUiWatchFaceView::SetUpCoverflowView(void)
{
if (coverflow2_ == nullptr) {
coverflow2_ = new OHOS::UICoverFlowView2();
}
coverflow2_->SetPosition(0, 0, GetRootContainer()->GetWidth(), GetRootContainer()->GetHeight());
coverflow2_->SetIntercept(true);
coverflow2_->SetPagePadding(153);
coverflow2_->SetThrowDrag(true);
coverflow2_->SetOnCoverflowListener(presenter_);
uint32_t cur_id = sql_setting_get_watch_face_id();
for (std::size_t i = 0; i < wfImageList.size(); i++) {
WatchFaceCoverflowItemView *group = nullptr;
if (coverflowItemList_.size() > i) {
if (coverflowItemList_[i] == nullptr) {
group = new WatchFaceCoverflowItemView();
} else {
group = coverflowItemList_[i];
}
}
group->SetOnClickListener(presenter_);
coverflowItemList_.push_back(group);
coverflow2_->Add(group);
group->Resize(ITEM_SIZE, ITEM_SIZE);
group->LayoutCenterOfParent();
group->SetSrc(GetImageInfo(i));
group->SetWfId(wfImageList[i].wfId);
group->SetName(wfImageList[i].name);
if (wfImageList[i].wfId == cur_id) {
coverflow2_->SetCurrentPage(i);
}
}
coverflow2_->RequestFocus();
AddViewToRootContainer(coverflow2_);
}
OHOS::ImageInfo *TjdUiWatchFaceView::GetImageInfo(uint8_t index)
{
if (index >= wfImageList.size()) {
return nullptr;
}
OHOS::ImageInfo *imgInfo = nullptr;
auto &info = wfImageList[index];
if (info.fileOffset == 0) {
imgInfo = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(info.fileImgId, info.path);
} else if (info.fileOffset < 0) {
imgInfo = OHOS::ImageCacheManager::GetInstance().LoadSingleRes(info.path);
} else {
FILE *fp = fopen(info.path.c_str(), "rb");
if (fp) {
imgInfo = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(info.fileImgId, info.path, fp, false,
info.fileOffset);
fclose(fp);
}
}
if (imgInfo) {
info.imgData = const_cast<uint8_t *>(imgInfo->phyAddr);
}
return imgInfo;
}
} // namespace TJD