mcu_hi3321_watch/tjd/ui/app/wf_select/TjdUiWatchFaceView.cpp
2025-05-31 10:45:15 +08:00

192 lines
6.4 KiB
C++

/*----------------------------------------------------------------------------
* 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 "TjdUiMultiLanguageExt.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"
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();
SetUpCoverflowView();
}
void TjdUiWatchFaceView::OnStop()
{
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)
{
if (lastIndex_ >= wfImageList.size()) {
static_print_error("GetCurCoverflowItemWfId: lastIndex_ out of range");
return 0;
}
return wfImageList[lastIndex_].wfId;
}
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)
{
coverflow2_.SetPosition(0, 0, GetRootContainer()->GetWidth(), GetRootContainer()->GetHeight());
coverflow2_.SetIntercept(true);
coverflow2_.SetOnCoverflowListener(presenter_);
coverflow2_.SetOnClickListener(presenter_);
coverflow2_.SetThrowDrag(true);
coverflow2_.SetImgSize(246, 246);
coverflow2_.RequestFocus();
uint32_t cur_id = sql_setting_get_watch_face_id();
for (std::size_t i = 0; i < wfImageList.size(); i++) {
coverflow2_.Add(wfImageList[i].name.c_str(), GetImageInfo(i));
if (wfImageList[i].wfId == cur_id) {
lastIndex_ = static_cast<uint8_t>(i);
coverflow2_.SetCurrentPage(i);
}
}
label_.SetPosition(127, 50, 200, 50);
label_.SetTextColor(Color::White());
label_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label_.SetText(wfImageList[lastIndex_].name.c_str());
label_.SetFont(TJD_VECTOR_FONT_FILENAME, 30);
AddViewToRootContainer(&coverflow2_);
AddViewToRootContainer(&label_);
}
void TjdUiWatchFaceView::CoverflowViewScroll(void)
{
int16_t contMidX = static_cast<int16_t>(GetRootContainer()->GetWidth() / 2);
int16_t minOffset = contMidX;
uint8_t newCenterIndex = lastIndex_;
for (uint8_t i = 0; i < coverflow2_.GetChildrenNumber(); i++) {
OHOS::Rect 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) {
newCenterIndex = i;
minOffset = distance;
}
}
if (newCenterIndex != lastIndex_) {
lastIndex_ = newCenterIndex;
label_.SetText(wfImageList[lastIndex_].name.c_str());
}
OHOS::Rect curViewRect = coverflow2_.GetViewByIndex(lastIndex_)->GetOrigRect();
int16_t centerMid = curViewRect.GetLeft() + curViewRect.GetWidth() / 2;
int16_t offset = MATH_ABS(contMidX - centerMid);
if (offset > curViewRect.GetWidth() / 2) {
label_.SetStyle(OHOS::STYLE_TEXT_OPA, 0);
} else {
uint8_t alpha = (uint8_t)((1.0f - ((float)offset / (curViewRect.GetWidth() / 2))) * OPA_OPAQUE);
label_.SetStyle(OHOS::STYLE_TEXT_OPA, alpha);
}
presenter_->ResetTimer();
}
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