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

428 lines
17 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: TjdUiAppPlayDialAdapter.cpp
*
* Author: liuguanwu
*
* Create: 2024-10
*--------------------------------------------------------------------------*/
#include "TjdUiImageIds.h"
#include "TjdUiAppPlayDialAdapter.h"
#include "TjdUiAppPlayDialPresenter.h"
#include "TjdUiAppPlayDialView.h"
#include "TjdUiAppPlayDialModel.h"
#include "common/image_cache_manager.h"
#include "sys_config.h"
#include "TjdUiMultiLanguage.h"
#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
#define TJD_VECTOR_FONT_FILENAME "PingFangMedium.TTF"
#define PLAY_DIAL_BIN_PATH TJD_IMAGE_PATH "img_play_dial.bin"
#define LIST_ITEM_WIDTH (Screen::GetInstance().GetWidth() - 16)
#define LIST_ITEM_HEIGHT (126)
using namespace OHOS;
namespace TJD {
// 待定使用
const AiStyleItem g_AiStyleItem[] = {
{0, AI_STYLE_INVALID, STR_ID_387},//风格选择
{IMG_PLAY_DIAL_STYLE_JAPANESE_ANIME, AI_STYLE_JAPANESE_ANIME, STR_ID_388},//日系动漫
{IMG_PLAY_DIAL_STYLE_OIL_PAINTING, AI_STYLE_OIL_PAINTING, STR_ID_389},//油画
{IMG_PLAY_DIAL_STYLE_ANTIQUITIES, AI_STYLE_ANTIQUITIES, STR_ID_390},//古风
{IMG_PLAY_DIAL_STYLE_ABSTRACT_ART, AI_STYLE_ABSTRACT_ART, STR_ID_391},//抽象派
{IMG_PLAY_DIAL_STYLE_DARK_ART, AI_STYLE_DARK_ART, STR_ID_392},//黑暗艺术
{IMG_PLAY_DIAL_STYLE_INK_AND_WASH, AI_STYLE_INK_AND_WASH, STR_ID_393},//水墨画
{IMG_PLAY_DIAL_STYLE_3D_RENDERING, AI_STYLE_3D_RENDERING, STR_ID_394},//3D渲染
{IMG_PLAY_DIAL_STYLE_VANGOGH_WIND, AI_STYLE_VANGOGH_WIND, STR_ID_395},//梵高风
{IMG_PLAY_DIAL_STYLE_ILLUSTRATION, AI_STYLE_ILLUSTRATION, STR_ID_396},//插画
{IMG_PLAY_DIAL_STYLE_HAYAO_MIYAZAKI, AI_STYLE_HAYAO_MIYAZAKI, STR_ID_397},//宫崎骏
{IMG_PLAY_DIAL_STYLE_MONET, AI_STYLE_MONET, STR_ID_398},//莫奈
{IMG_PLAY_DIAL_STYLE_MACHINERY, AI_STYLE_MACHINERY, STR_ID_399},//机械
{IMG_PLAY_DIAL_STYLE_CERAMIC_WORLD, AI_STYLE_CERAMIC_WORLD, STR_ID_400},//陶艺世界
{IMG_PLAY_DIAL_STYLE_PAPER_CUT, AI_STYLE_PAPER_CUT, STR_ID_401},//剪纸风格
{IMG_PLAY_DIAL_STYLE_BLACK_WHITE_SKETCH, AI_STYLE_BLACK_WHITE_SKETCH, STR_ID_402},//黑白素描
{IMG_PLAY_DIAL_STYLE_THICK_COATING_WIND, AI_STYLE_THICK_COATING_WIND, STR_ID_403},//厚涂风
{IMG_PLAY_DIAL_STYLE_5D_PORTRAIT, AI_STYLE_5D_PORTRAIT, STR_ID_404},//5D人像
{IMG_PLAY_DIAL_STYLE_SCIENCE_FICTION, AI_STYLE_SCIENCE_FICTION, STR_ID_405},//科幻
};
bool listContainerStyle::InitContainer()
{
if (imageBg_ == nullptr) {
imageBg_ = new UIImageView();
if (imageBg_ == nullptr) {
return false;
}
}
imageBg_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
if (image_ == nullptr) {
image_ = new UIImageView();
if (image_ == nullptr) {
return false;
}
}
image_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
if (label_ == nullptr) {
label_ = new UILabelExt();
if (label_ == nullptr) {
delete image_;
image_ = nullptr;
return false;
}
}
label_->SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
label_->SetFont(TJD_VECTOR_FONT_FILENAME, 40); // 40: item font size
Add(imageBg_);
Add(image_);
Add(label_);
return true;
}
uint16_t TjdPlayDialAdapter::GetCount(void)
{
return sizeof(g_AiStyleItem) / sizeof(g_AiStyleItem[0]);
}
OHOS::UIView *TjdPlayDialAdapter::GetView(UIView* inView, int16_t index)
{
if (index >= GetCount() || index < 0) {
static_print_error("inView=[%p], index=[%d]\n", inView, index);
return nullptr;
}
UIImageView* image = nullptr;
UILabelExt* label = nullptr;
ImageInfo* imgInfo = nullptr;
listContainerStyle* newView = nullptr;
if (inView == nullptr) {
newView = new listContainerStyle();
if (newView == nullptr) {
static_print_error("newView == nullptr:\n");
return nullptr;
}
if (!newView->InitContainer()) {
delete newView;
static_print_error("newView->InitContainer():false\n");
return nullptr;
}
} else {
newView = static_cast<listContainerStyle*>(inView);
}
static_print_debug("TjdPlayDialAdapter::GetView() list index:%d\n", index);
if (newView != nullptr) {
//newView->Resize(width_, height_);
newView->Resize(LIST_ITEM_WIDTH, LIST_ITEM_HEIGHT);
newView->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
//newView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
//newView->SetViewIndex(index);
newView->SetViewIndex(g_AiStyleItem[index].resId);
newView->SetOnClickListener(&TjdUiAppPlayDialPresenter::GetInstance());
if(g_AiStyleItem[index].styleId == AI_STYLE_INVALID) {
image = newView->GetImageBgView();
imgInfo = nullptr;
image->SetSrc(imgInfo);
image->LayoutCenterOfParent();
image = newView->GetImageView();
imgInfo = nullptr;
image->SetSrc(imgInfo);
image->LayoutCenterOfParent();
label = newView->GetLabelView();
label->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_34);
label->SetTextId(g_AiStyleItem[index].styleINamed);
//label->LayoutCenterOfParent(0, -100);
label->SetY(0);
label->SetWidth(LIST_ITEM_WIDTH);
label->SetHeight(LIST_ITEM_HEIGHT);
label->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
//label->SetStyle(STYLE_BORDER_WIDTH, 1);
label->LayoutCenterOfParent();
label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
label->SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
newView->SetTouchable(false);
}else{
image = newView->GetImageBgView();
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_PLAY_DIAL_SINGLE_LIST, PLAY_DIAL_BIN_PATH);
image->SetSrc(imgInfo);
image->LayoutCenterOfParent();
image = newView->GetImageView();
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(g_AiStyleItem[index].resId, PLAY_DIAL_BIN_PATH);
image->SetSrc(imgInfo);
//image->SetAutoEnable(false);//如果置为false则需要设置图片控件的宽度和高度否则将没有显示
image->LayoutCenterOfParent();
image->LayoutLeftOfParent(16);
//image->LayoutBottomOfParent();
image->SetResizeMode(UIImageView::ImageResizeMode::CENTER);//这里如果SetAutoEnable为false,则需要设置
image->SetViewId("img");
label = newView->GetLabelView();
label->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_34);
label->SetTextId(g_AiStyleItem[index].styleINamed);
//label->SetTextId(g_itemNode[index].txtId);
label->LayoutRightToSibling(image->GetViewId(), 0);
label->SetY(0);
label->SetWidth(LIST_ITEM_WIDTH - (image->GetWidth() + image->GetX() + 24));
label->SetHeight(LIST_ITEM_HEIGHT);
label->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
//label->SetStyle(STYLE_BORDER_WIDTH, 1);
//label->SetPosition(image->GetX() + 110, 0);
//label->SetPosition(image->GetX() + 110, 0, LIST_ITEM_WIDTH - 110 - image->GetX(), LIST_ITEM_HEIGHT);
//label->SetFont(fontName_, fontSize_);
label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
label->SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
newView->SetViewId(PLAYDIAL_AI_STYLE_VIEW_ITEM_ID);
newView->SetTouchable(true);
}
}
static_print_debug("TjdPlayDialAdapter::GetView:%p\n", newView);
return newView;
}
static int LinearEaseIn(int start, int end, int value, int duration)
{
float t = static_cast<float>(value) / duration;
return static_cast<int>(start + (end - start) * t);
}
void TjdPlayDialAdapter::StyleList::ReMeasure()
{
#if 0
//printf("UITestListFPS::TestList::ReMeasure\n");
UIViewGroup* view = static_cast<UIViewGroup*>(GetChildrenHead());
if (view == nullptr) {
return;
}
int16_t midY = GetY() + GetHeight() / 2;
int16_t y;
const int scaleFactor_ = 200;
int16_t total = midY;
do {
y = view->GetY();
if (direction_ == VERTICAL) {
int offset = (y + view->GetHeight() / 2 - midY);
//越靠经中点offset越小返回的temp值越接近0
//int temp = EasingEquation::SineEaseIn(0, scaleFactor_, abs(offset), scaleFactor_);
int temp = LinearEaseIn(0, total, abs(offset), total*4);
float scale = 1.0 - (static_cast<float>(temp) / total);
//scale = 0.3;
Rect reRect = view->GetRelativeRect();
UIView* child = view->GetChildrenHead();
while (child != nullptr) {
if (child->GetViewType() == UI_IMAGE_VIEW) {
break;
}
child = child->GetNextSibling();
}
UIImageView* imageView = static_cast<UIImageView*>(child);
//printf("view name:%s, scale:%f\n", imageView->GetViewId(), scale);
imageView->Scale(Vector2<float>(scale, scale), Vector2<float>(imageView->GetWidth() / 2, imageView->GetHeight() / 2));
//imageView->Scale(Vector2<float>(scale, scale), Vector2<float>(0, 0));
child = view->GetChildrenHead();
while (child != nullptr) {
if (child->GetViewType() == UI_LABEL_EXT) {
break;
}
child = child->GetNextSibling();
}
UIImageView* labelView = static_cast<UIImageView*>(child);
int16_t tar = total * 2;
if (abs(offset) > tar) {
offset = tar;
}
temp = LinearEaseIn(0, total, abs(offset), tar);
scale = 1.0 - (static_cast<float>(temp) / total);
//labelView->SetStyle(STYLE_TEXT_OPA, scale * 255);
labelView->UIView::SetStyle(STYLE_TEXT_OPA, scale * 255);
}
view = static_cast<UIViewGroup*>(view->GetNextSibling());
} while (view != nullptr);
#endif
}
#if 0
void TjdHistoryScrollView::ListAnimatorCallback::Callback(OHOS::UIView* view)
{
if (view == nullptr) {
return;
}
TjdHistoryScrollView* scrollView = static_cast<TjdHistoryScrollView*>(view);
bool needStopX = false;
if (startValueX_ != endValueX_) {
int16_t actX = OHOS::EasingEquation::CubicEaseOut(startValueX_, endValueX_, scrollView->scrollAnimator_.GetRunTime(), scrollView->scrollAnimator_.GetTime());
//scrollView->ScrollBy(actX - previousValueX_, 0);
scrollView->MoveOffset(actX - previousValueX_, 0);
previousValueX_ = actX;
} else {
needStopX = true;
}
//printf("TjdAnimatorViewback::StartAnimatorOnDragEnd(): x[%d-%d-%d],\n", startValueX_, endValueX_, previousValueX_);
}
#endif
bool listContainerHistory::InitContainer()
{
if (ItemBg_ == nullptr) {
ItemBg_ = new TjdHistoryScrollView();
if (ItemBg_ == nullptr) {
return false;
}
}
if (startView_ == nullptr) {
startView_ = new UIImageView();
if (startView_ == nullptr) {
return false;
}
}
if (imageBg_ == nullptr) {
imageBg_ = new UIImageView();
if (imageBg_ == nullptr) {
return false;
}
}
imageBg_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
if (image_ == nullptr) {
image_ = new UIImageView();
if (image_ == nullptr) {
return false;
}
}
image_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
if (deleteView_ == nullptr) {
deleteView_ = new UIImageView();
if (deleteView_ == nullptr) {
return false;
}
}
deleteView_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
ItemBg_->Add(startView_);
ItemBg_->Add(imageBg_);
ItemBg_->Add(image_);
ItemBg_->Add(deleteView_);
Add(ItemBg_);
return true;
}
uint16_t PlayDialHistoryAdapter::GetCount(void)
{
std::vector<std::string> *strList = TjdUiAppPlayDialModel::GetInstance().GetHistoryFileList();
static_print_debug("PlayDialHistoryAdapter::GetCount():%d\n", strList->size());
return strList->size();
}
OHOS::UIView *PlayDialHistoryAdapter::GetView(UIView* inView, int16_t index)
{
static_print_debug("PlayDialHistoryAdapter::GetView:start %p-%d\n", inView, index);
if (index >= GetCount() || index < 0) {
static_print_error("inView=[%p], index=[%d]\n", inView, index);
return nullptr;
}
std::vector<std::string> *strList = TjdUiAppPlayDialModel::GetInstance().GetHistoryFileList();
UIImageView* image = nullptr;
ImageInfo* imgInfo = nullptr;
listContainerHistory* newView = nullptr;
if (inView == nullptr) {
newView = new listContainerHistory();
if (newView == nullptr) {
static_print_error("newView == nullptr:\n");
return nullptr;
}
if (!newView->InitContainer()) {
delete newView;
static_print_error("newView->InitContainer():false\n");
return nullptr;
}
newView->SetPosition(0, 0,466, 220);
newView->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
//newView->GetItemBgView()->SetPositionPercent(1, 1);
newView->GetItemBgView()->SetPosition(0, 0,466, 220);
//newView->GetItemBgView()->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
//newView->GetItemBgView()->SetStyle(STYLE_BACKGROUND_COLOR, 0xff0000ff);
newView->GetItemBgView()->SetDirection(UIAbstractScroll::HORIZONTAL);
newView->GetItemBgView()->SetViewId(PLAYDIAL_LOG_LIST_VIEW_ITEM_ID);
newView->GetItemBgView()->SetOnDragListener(&TjdUiAppPlayDialPresenter::GetInstance());
newView->GetItemBgView()->SetTouchable(true);
image = newView->GetStartView();
image->SetAutoEnable(false);
image->SetPosition(0, 0, 10, 200);
//image->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
//image->SetStyle(STYLE_BACKGROUND_COLOR, 0xff00ff00);
std::string temp_str = (strList->data()[index]);
imgInfo = ImageCacheManager::GetInstance().LoadSingleRes(temp_str);
image = newView->GetImageBgView();
image->SetAutoEnable(false);
image->SetPosition(53, 10, 360, 200);
image->SetSrc(imgInfo);
image->SetResizeMode(UIImageView::COVER);
image->SetViewId(PLAYDIAL_LOG_LIST_VIEW_IMAGE_ID);
image->SetOnClickListener(&TjdUiAppPlayDialPresenter::GetInstance());
image->SetTouchable(true);
image = newView->GetImageView();
image->SetAutoEnable(false);
image->SetPosition(53-18, 10-18, 360, 200);
image->SetStyle(STYLE_BORDER_WIDTH, 18);
image->SetStyle(STYLE_BORDER_RADIUS, 48);
//image->SetStyle(STYLE_BORDER_OPA, 200);
image->SetStyle(STYLE_BORDER_COLOR, 0xff000000);
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_PLAY_DIAL_DELETE, PLAY_DIAL_BIN_PATH);
image = newView->GetDeleteView();
image->SetAutoEnable(false);
image->SetPosition(420, 10, 200, 200);
image->SetSrc(imgInfo);
image->SetViewId(PLAYDIAL_LOG_LIST_VIEW_DELETE_ID);
image->SetOnClickListener(&TjdUiAppPlayDialPresenter::GetInstance());
image->SetTouchable(true);
} else {
newView = static_cast<listContainerHistory*>(inView);
}
if (newView != nullptr) {
newView->GetItemBgView()->SetViewIndex(index);
int16_t xDistanceTemp = newView->GetItemBgView()->GetChildrenHead()->GetX();
if(xDistanceTemp < 0) {
newView->GetItemBgView()->ScrollBy(-xDistanceTemp, 0);
}
std::string temp_str = (strList->data()[index]);
imgInfo = ImageCacheManager::GetInstance().LoadSingleRes(temp_str);
image = newView->GetImageBgView();
image->SetSrc(imgInfo);
}
static_print_debug("PlayDialHistoryAdapter::GetView:%p-%d-[%s]\n", newView, index, strList->data()[index].c_str());
return newView;
}
} // namespace TJD