mcu_hi3321_watch/tjd/ui/watch_face/football_one/TjdUiWFPageFootballOne.cpp
2025-05-26 20:15:20 +08:00

370 lines
13 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description: TjdUiWFPageFootballOne.cpp
*
* Author: huangshuyi
*
* Create: 2024-9
*--------------------------------------------------------------------------*/
#include "TjdUiWFPageFootballOne.h"
#include "TjdUiImageIds.h"
#include "TjdUiMemManage.h"
#include "TjdUiMultiLanguageExt.h"
#include "common/image_cache_manager.h"
#include "graphic_service.h"
#include "rtc_api.h"
#include "sys_config.h"
#include <sys/time.h>
using namespace OHOS;
#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
namespace TJD {
static TjdUiWFPageFootballOne *g_pWFPage = nullptr;
static constexpr uint8_t PLANES_NUM = 20;
#define IMAGE_BIN_PATH TJD_IMAGE_PATH"img_football1.bin"
TjdUiWFPageFootballOne::TjdUiWFPageFootballOne()
{
SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
SetPeriod(500);
g_pWFPage = this;
}
TjdUiWFPageFootballOne::~TjdUiWFPageFootballOne()
{
static_print_debug("TjdUiWFPageFootballOne::~TjdUiWFPageFootballOne");
if (viewiInitStatus) {
if (callback_ != nullptr) {
delete callback_;
callback_ = nullptr;
}
if (secondHand) {
delete secondHand;
secondHand = nullptr;
}
if (minuteHand) {
delete minuteHand;
minuteHand = nullptr;
}
if (hourHand) {
delete hourHand;
hourHand = nullptr;
}
if (dotHand) {
delete dotHand;
dotHand = nullptr;
}
if (timesClock_) {
delete timesClock_;
timesClock_ = nullptr;
}
TjdUiMemManage::DeleteChildren(mainView_);
if (mainView_) {
delete mainView_;
mainView_ = nullptr;
}
ImageCacheManager::GetInstance().UnloadAllInMultiRes(IMAGE_BIN_PATH);
viewiInitStatus = false;
}
g_pWFPage = nullptr;
}
void TjdUiWFPageFootballOne::PreLoad(void)
{
static_print_debug("TjdUiWFPageFootballOne::PreLoad");
//MemCheck::GetInstance()->EnableLeakCheck(true);
if (!viewiInitStatus) {
ImageCacheManager::GetInstance().LoadAllInMultiRes(IMAGE_BIN_PATH);
InitView();
viewiInitStatus = true;
}
}
void TjdUiWFPageFootballOne::UnLoad(void)
{
static_print_debug("TjdUiWFPageFootballOne::UnLoad:");
}
void TjdUiWFPageFootballOne::OnActive()
{
if (enterAnimator_) {
enterAnimator_->Start();
}
}
void TjdUiWFPageFootballOne::OnInactive()
{
if (enterAnimator_) {
enterAnimator_->Stop();
}
}
void TjdUiWFPageFootballOne::EntryAnimatorCallback::Callback(UIView *view)
{
UIIcosahedronView* footView = dynamic_cast<UIIcosahedronView*>(view);
//int16_t el = EasingEquation::LinearEaseNone(startPos_, endPos_, animator_->GetRunTime(), animator_->GetTime());
//footView->SetSideLength(el);
//footView->RefreshIcosahedron();
const Vector3<float> rotateStart(Screen::GetInstance().GetWidth() / 2.0, // 2.0: divisor
Screen::GetInstance().GetHeight() / 2.0, 0); // 2.0: divisor
const Vector3<float> rotateEnd(0, 0, 0);
// 270 : stop angle
//int16_t angle = EasingEquation::LinearEaseNone(0, 270, animator_->GetRunTime(), animator_->GetTime());
static int16_t angle = 0;
angle += 1;
if (angle >= 360) {
angle = 0;
}
footView->RotatePlanes(angle, rotateStart, rotateEnd);
}
void TjdUiWFPageFootballOne::Update(void)
{
rtc_class_ops* rtc = tjd_driver_rtc_get_ops();
struct rtc_time localTime;
rtc->get_rtc_time(&localTime);
uint8_t hour = localTime.tm_hour;//获得当前的小时
uint8_t min = localTime.tm_min;
uint8_t sec = localTime.tm_sec;
static constexpr int16_t SEC_TO_MILLI = 1000;
static constexpr int16_t USEC_TO_MILLI = 1000;
static constexpr int16_t MIN_TO_HOUR = 60;
static constexpr int16_t HOURLY_SYSTEM = 24;
static constexpr int16_t INTERVAL = 30;
struct timeval tv;
gettimeofday(&tv, nullptr);
uint64_t second = static_cast<uint64_t>(tv.tv_sec);
uint64_t uSecond = static_cast<uint64_t>(tv.tv_usec);
uint64_t minute = second / MIN_TO_HOUR;
uint64_t hour1 = minute / MIN_TO_HOUR;
uint8_t realHour = static_cast<uint8_t>(hour1 % HOURLY_SYSTEM);
uint8_t realMinute = static_cast<uint8_t>(minute % MIN_TO_HOUR);
uint16_t realMilliSecond = static_cast<uint16_t>((second % MIN_TO_HOUR) *
SEC_TO_MILLI + uSecond / USEC_TO_MILLI);
//sweepClock->SetSweepTime12Hour(realHour, realMinute, realMilliSecond, 1);
mainSweepClock->SetSweepTime24Hour(hour, min, realMilliSecond);
}
static void ClockTimerCallback(void *param)
{
GraphicService::GetInstance()->PostGraphicEvent([] {
if (g_pWFPage) {
g_pWFPage->Update();
}});
}
void TjdUiWFPageFootballOne::InitClock(void)
{
mainSweepClock = new UISweepClock();
if (mainSweepClock == nullptr) {
GRAPHIC_LOGE("mainSweepClock new fail");
return;
}
mainSweepClock->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
mainSweepClock->SetWorkMode(UISweepClock::WorkMode::NORMAL);
mainSweepClock->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000);
mainSweepClock->SetStyle(STYLE_BACKGROUND_OPA, 0x00);
secondHand = new Image();
if (secondHand == nullptr) {
GRAPHIC_LOGE("secondHand new fail");
return;
}
ImageInfo* image = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_SECOND, IMAGE_BIN_PATH);
if (image == nullptr) {
return;
}
secondHand->SetSrc(image);
ImageHeader header;
secondHand->GetHeader(header);
int16_t centerx = header.width / 2;
int16_t centery = header.height - 15;
int16_t positionx = Screen::GetInstance().GetWidth() / 2 - centerx;
int16_t positiony = Screen::GetInstance().GetHeight() / 2 - centery;
mainSweepClock->SetHandImage(UISweepClock::HandType::SECOND_HAND, *secondHand,
{ positionx, positiony }, { centerx, centery});
minuteHand = new Image();
if (minuteHand == nullptr) {
GRAPHIC_LOGE("minuteHand new fail");
return;
}
image = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_MINUTE, IMAGE_BIN_PATH);
if (image == nullptr) {
return;
}
minuteHand->SetSrc(image);
minuteHand->GetHeader(header);
centerx = header.width / 2;
centery = header.height - 8;
positionx = Screen::GetInstance().GetWidth() / 2 - centerx;
positiony = Screen::GetInstance().GetHeight() / 2 - centery;
mainSweepClock->SetHandImage(UISweepClock::HandType::MINUTE_HAND, *minuteHand,
{ positionx, positiony }, { centerx, centery});
hourHand = new Image();
if (hourHand == nullptr) {
GRAPHIC_LOGE("hourHand new fail");
return;
}
image = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_HOUR, IMAGE_BIN_PATH);
if (image == nullptr) {
return;
}
hourHand->SetSrc(image);
hourHand->GetHeader(header);
centerx = header.width / 2;
centery = header.height - 8;
positionx = Screen::GetInstance().GetWidth() / 2 - centerx;
positiony = Screen::GetInstance().GetHeight() / 2 - centery;
mainSweepClock->SetHandImage(UISweepClock::HandType::HOUR_HAND, *hourHand,
{ positionx, positiony }, { centerx, centery});
dotHand = new Image();
if (dotHand == nullptr) {
GRAPHIC_LOGE("dotHand new fail");
return;
}
image = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_DOT, IMAGE_BIN_PATH);
if (image == nullptr) {
return;
}
dotHand->SetSrc(image);
dotHand->GetHeader(header);
centerx = header.width / 2;
centery = header.height / 2;
positionx = Screen::GetInstance().GetWidth() / 2 - centerx;
positiony = Screen::GetInstance().GetHeight() / 2 - centery;
mainSweepClock->SetHandCircleImage(UISweepClock::HandType::SECOND_HAND, *dotHand,
{ positionx, positiony });
mainSweepClock->SetHandCircleImage(UISweepClock::HandType::MINUTE_HAND, *dotHand,
{ positionx, positiony });
mainSweepClock->SetHandCircleImage(UISweepClock::HandType::HOUR_HAND, *dotHand,
{ positionx, positiony });
rtc_class_ops* rtc = tjd_driver_rtc_get_ops();
struct rtc_time localTime;
rtc->get_rtc_time(&localTime);
uint8_t hour = localTime.tm_hour;//获得当前的小时
uint8_t min = localTime.tm_min;
uint8_t sec = localTime.tm_sec;
static constexpr int16_t SEC_TO_MILLI = 1000;
static constexpr int16_t USEC_TO_MILLI = 1000;
static constexpr int16_t MIN_TO_HOUR = 60;
static constexpr int16_t HOURLY_SYSTEM = 24;
static constexpr int16_t INTERVAL = 30;
struct timeval tv;
gettimeofday(&tv, nullptr);
uint64_t second = static_cast<uint64_t>(tv.tv_sec);
uint64_t uSecond = static_cast<uint64_t>(tv.tv_usec);
uint64_t minute = second / MIN_TO_HOUR;
uint64_t hour1 = minute / MIN_TO_HOUR;
uint8_t realHour = static_cast<uint8_t>(hour1 % HOURLY_SYSTEM);
uint8_t realMinute = static_cast<uint8_t>(minute % MIN_TO_HOUR);
uint16_t realMilliSecond = static_cast<uint16_t>((second % MIN_TO_HOUR) *
SEC_TO_MILLI + uSecond / USEC_TO_MILLI);
mainSweepClock->SetSweepTime24Hour(hour, min, realMilliSecond);
mainView_->Add(mainSweepClock);
if (timesClock_ == nullptr) {
timesClock_ = new OHOS::GraphicTimer(50, ClockTimerCallback, nullptr, true);
timesClock_->Start();
}
}
void TjdUiWFPageFootballOne::InitView(void)
{
SetCoverable(true);
if (mainView_) {
static_print_error("TjdUiWFPageFootballOne::InitView mainView_[p%]", mainView_);
return;
}
mainView_ = new UIViewGroup();
if (mainView_ == nullptr) {
static_print_error("create main page failed");
return;
}
mainView_->SetViewId("wf_main_mainView_");
mainView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
Add(mainView_);
UIImageView *backGround = new UIImageView();
ImageInfo* imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_BG, IMAGE_BIN_PATH);
mainView_->Add(backGround);
backGround->SetPosition(0, 0);
backGround->SetSrc(imgInfo);
if (imgInfo_ == nullptr) {
imgInfo_ = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_FOOTBALL_SIDE, IMAGE_BIN_PATH);
if (imgInfo_ == nullptr) {
return;
}
}
if (imgInverseInfo_ == nullptr) {
imgInverseInfo_ = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FOOTBALL1_FOOTBALL_SIDE, IMAGE_BIN_PATH);
if (imgInverseInfo_ == nullptr) {
return;
}
}
if (container_ == nullptr) {
container_ = new FootballUIIcosahedronView();
container_->SetViewId("football1");
mainView_->Add(container_);
//container_->SetIntercept(true);
// container_->SetOpaScale(OPA_TRANSPARENT);
container_->SetStyle(STYLE_BACKGROUND_OPA, 0);
container_->SetDefaultImage(imgInfo_);
container_->SetSwipeACCLevel(200); // 0-255
container_->SetDragACCLevel(8); // 5: drag acceleration
//container_->SetLuminanceFactor(10000);
// container_->EnabelLightAndDrak(false);
container_->SetThrowDrag(true);
int cycle_r = Screen::GetInstance().GetWidth() / 2;
container_->SetPosition(0, 0, cycle_r * 2, cycle_r * 2);
if (callback_ == nullptr) {
callback_ = new EntryAnimatorCallback(container_, 20, 140); // 30: Initial Size 200: Final display size
}
enterAnimator_ = callback_->GetAnimator();
//uint16_t len = std::sqrt(Screen::GetInstance().GetWidth()*Screen::GetInstance().GetWidth()*2) / 4; // 2: divisor
container_->SetSideLength(160);
container_->RefreshIcosahedron();
int16_t cycle_recy_x = cycle_r - 145;
int16_t cycle_recy_y = cycle_r - 145;
int16_t cycle_recy_len = 145*2;
Rect footballRect(cycle_recy_x, cycle_recy_y, cycle_recy_x + cycle_recy_len, cycle_recy_y + cycle_recy_len);
container_->footballRect_ = footballRect;
// UIViewGroup*temp = new UIViewGroup();
// temp->SetPosition(cycle_recy_x, cycle_recy_y, cycle_recy_len, cycle_recy_len);
// temp->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
// temp->SetStyle(STYLE_BACKGROUND_OPA, 100);
//mainView_->Add(temp);
enterAnimator_->Start();
container_->SetAnimator(enterAnimator_);
}
InitClock();
}
} // namespace TJD