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

359 lines
12 KiB
C++

#include "TjdUiAppGameView.h"
#include "NativeAbility.h"
#include "TjdUiImageIds.h"
#include "TjdUiMultiLanguageExt.h"
#include "common/image_cache_manager.h"
#include "style.h"
#include "sys_config.h"
#include "TjdUiAppBirdFly.h"
#include "TjdUiAppAirscraft.h"
// #include "gfx_utils/mem_check.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
#define HID_X_Y_SPEED 300
namespace TJD {
#define IMAGE_BIN_PATH TJD_IMAGE_PATH "img_game.bin"
static TjdUiAppGameView *g_pv_AppGameView = nullptr;
struct GameInfo
{
GameItemIndex index;
int resId;
std::string description;
};
GameInfo g_pv_game[] = {
{GAME_AIRCRAFT_INDEX, IMG_GAME_AIRCRAFT_ICON, "飞机大战"},
{GAME_BIRDFLY_INDEX, IMG_GAME_BIRDFLY_ICON, "飞翔小鸟"},
};
GameItemIndex TjdUiAppGameView::currentGameItem_ = GAME_MAX_INDEX;
static int16_t HorizontalCenter(int16_t width) { return (OHOS::HORIZONTAL_RESOLUTION - width) / 2; }
class GameOnClickedListener : public UIView::OnClickListener
{
public:
GameOnClickedListener() {}
virtual ~GameOnClickedListener() {}
virtual bool OnClick(UIView &view, const ClickEvent &event) { return true; }
private:
};
static void InitLabel(OHOS::UILabel &label, uint8_t size, int16_t y, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(HorizontalCenter(label.GetWidth()), y);
}
static void InitImage(OHOS::UIImageView &imageView, int16_t x, int16_t y, uint32_t resId, const char *imageViewId)
{
imageView.SetPosition(x, y);
OHOS::ImageInfo *imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(resId, IMAGE_BIN_PATH);
if (imgInfo != nullptr) {
imageView.SetSrc(imgInfo);
}
imageView.SetTouchable(true);
imageView.SetViewId(imageViewId);
imageView.SetOnClickListener(new GameOnClickedListener());
}
static void InitButton(OHOS::UIButton &button, int16_t x, int16_t y, int16_t width, int16_t height,
uint32_t defaultResId, uint32_t traResId, const char *buttonViewId)
{
button.SetPosition(x, y, width, height);
button.SetViewId(buttonViewId);
button.SetStyle(STYLE_BACKGROUND_OPA, 0);
button.SetStyleForState(STYLE_BACKGROUND_OPA, 0, OHOS::UIButton::PRESSED);
OHOS::ImageInfo *defaultImgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(defaultResId, IMAGE_BIN_PATH);
OHOS::ImageInfo *traImgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(traResId, IMAGE_BIN_PATH);
if (defaultImgInfo != nullptr && traImgInfo != nullptr) {
button.SetImageSrc(defaultImgInfo, traImgInfo);
}
button.SetVisible(true);
}
bool GameItemViewOnClickedListener::OnClick(UIView &view, const ClickEvent &event)
{
TjdUiAppGameView::GetInstance()->ChooseGameItem(static_cast<GameItemIndex>(view.GetViewIndex()));
return true;
}
GameItemView::GameItemView(GameItemIndex index, const char *name, int iconResId) : UITransformGroup()
{
Resize(450, 110);
SetViewIndex(index);
SetStyle(STYLE_BACKGROUND_COLOR, 0xff262626);
SetStyle(STYLE_BORDER_RADIUS, 55);
InitLabel(lblName_, 34, 32, name);
SetTouchable(true);
SetOnClickListener(&onClickListener_);
imgIcon_.SetPosition(16, 13,84,84);
auto &image = OHOS::ImageCacheManager::GetInstance();
OHOS::ImageInfo *imgInfo = image.LoadOneInMultiRes(iconResId, IMAGE_BIN_PATH);
if (imgInfo != nullptr) {
imgIcon_.SetSrc(imgInfo);
}
Add(&lblName_);
Add(&imgIcon_);
}
GameItemView::~GameItemView()
{
RemoveAll();
}
TjdUiAppGameView::TjdUiAppGameView()
{
// OHOS::MemCheck::GetInstance()->EnableLeakCheck(true);
static_print_debug("TjdUiAppGameView()");
g_pv_AppGameView = this;
}
TjdUiAppGameView::~TjdUiAppGameView()
{
static_print_debug("~TjdUiAppGameView()");
g_pv_AppGameView = nullptr;
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
// OHOS::MemCheck::GetInstance()->EnableLeakCheck(false);
for (uint8_t i = 0; i < GAME_MAX_INDEX; i++) {
if (gameItemView_[i] != nullptr) {
this->Remove(gameItemView_[i]);
delete gameItemView_[i];
gameItemView_[i] = nullptr;
}
}
}
TjdUiAppGameView *TjdUiAppGameView::GetInstance(void) { return g_pv_AppGameView; }
void TjdUiAppGameView::InitGameList()
{
auto &image = OHOS::ImageCacheManager::GetInstance();
image.LoadAllInMultiRes(IMAGE_BIN_PATH);
if (gameItemList_ == nullptr) {
gameItemList_ = new OHOS::List<GameCaseInfo>();
}
gameItemList_->Clear();
for (const auto &game : g_pv_game) {
imgItemInfo[game.index] = image.LoadOneInMultiRes(game.resId, IMAGE_BIN_PATH);
gameItemList_->PushBack({game.index, imgItemInfo[game.index], game.description.c_str()});
}
}
void TjdUiAppGameView::ChooseGameItem(GameItemIndex index)
{
if (index < 0 || index >= GAME_MAX_INDEX) {
return;
}
mainContainer_->SetVisible(false);
InitGameItemView(index);
if (itemView_[index] != nullptr) {
itemView_[index]->Show();
}
currentGameItem_ = index;
if (itemView_[index] == nullptr) {
ReturnToMainPage();
}
}
void TjdUiAppGameView::OnStart()
{
static_print_debug("TjdUiAppGameView::OnStart()");
OHOS::FocusManager::GetInstance()->ClearFocus();
// InitGameList();
if (mainContainer_ == nullptr) {
mainContainer_ = new UIViewGroup();
}
if (onClickListener_ == nullptr) {
onClickListener_ = dynamic_cast<UIView::OnClickListener *>(TjdUiAppGamePresenter::GetInstance());
}
if (OnDragListener_ == nullptr) {
OnDragListener_ = dynamic_cast<UIView::OnDragListener *>(TjdUiAppGamePresenter::GetInstance());
}
InitGameView();
mainContainer_->SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
mainContainer_->SetStyle(STYLE_BACKGROUND_OPA, 0);
mainContainer_->SetOnDragListener(TjdUiAppGamePresenter::GetInstance());
mainContainer_->Add(&gameView_);
// RootView::GetInstance()->Add(mainContainer_);
AddViewToRootContainer(mainContainer_);
}
void TjdUiAppGameView::OnStop()
{
printf("TjdUiAppGameView::OnStop()\n");
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(IMAGE_BIN_PATH);
// RootView::GetInstance()->Remove(mainContainer_);
if (mainContainer_ != nullptr) {
mainContainer_->RemoveAll();
delete mainContainer_;
mainContainer_ = nullptr;
}
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
if (onClickListener_ != nullptr) {
delete onClickListener_;
onClickListener_ = nullptr;
}
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
if (OnDragListener_ != nullptr) {
delete OnDragListener_;
OnDragListener_ = nullptr;
}
titleContainer_.RemoveAll();
gameView_.RemoveAll();
if(gameItemList_ != nullptr){
gameItemList_->Clear();
delete gameItemList_;
gameItemList_ = nullptr;
}
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
for(int i = 0; i < GAME_MAX_INDEX; i++) {
if(itemView_[i] != nullptr){
itemView_[i]->RemoveAll();
delete itemView_[i];
itemView_[i] = nullptr;
}
}
OHOS::FocusManager::GetInstance()->ClearFocus();
}
void TjdUiAppGameView::InitGameView()
{
static_print_info("TjdUiAppGameView::InitGameView()");
gameView_.SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
gameView_.SetStyle(STYLE_BACKGROUND_OPA, 0);
gameView_.SetOnDragListener(onDragListener_);
gameView_.SetVisible(true);
titleContainer_.SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, 60);
titleContainer_.SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0);
titleContainer_.SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0xff);
InitLabel(lblTitle_gameView, 28, 17, "游戏\n");
// lblTitle_gameView->SetTextColor(Color::GetColorFromRGBA(0xE9, 0x42, 0x00, 0xff));
titleContainer_.Add(&lblTitle_gameView);
// if (gameListAdapter_ == nullptr) {
// static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
// gameListAdapter_ = new TjdUiAppGameAdapter();
// }
// gameListAdapter_ = TjdUiAppGameAdapter();
// gameList_.SetPosition(8, 60, 450, OHOS::VERTICAL_RESOLUTION);
// static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
// gameList_.SetAdapter(&gameListAdapter_);
// gameList_.SetScrollBlankSize(55);
// gameList_.EnableAutoAlign(true);
// gameList_.SetThrowDrag(true);
// gameList_.SetYScrollBarVisible(true);
// // gameList_.SetSelectPosition(55 + 164);
// gameList_.SetReboundSize(200);
// // gameList_.SetRotateFactor(-10);
// // gameList_.SetRotateThreshold(8);
// gameList_.RequestFocus();
// // gameList_.SetStartIndex(0);
// gameView_.Add(&gameList_);
gameItemView_[GAME_AIRCRAFT_INDEX] = new GameItemView(GAME_AIRCRAFT_INDEX, "飞机大战", IMG_GAME_AIRCRAFT_ICON);
gameItemView_[GAME_AIRCRAFT_INDEX]->SetPosition(8, 173);
gameItemView_[GAME_BIRDFLY_INDEX] = new GameItemView(GAME_BIRDFLY_INDEX, "飞翔小鸟", IMG_GAME_BIRDFLY_ICON);
gameItemView_[GAME_BIRDFLY_INDEX]->SetPosition(8, 261);
gameItemView_[GAME_BIRDFLY_INDEX]->Scale({0.8, 0.9}, {233,341});
gameView_.Add(&titleContainer_);
gameView_.Add(gameItemView_[GAME_AIRCRAFT_INDEX]);
gameView_.Add(gameItemView_[GAME_BIRDFLY_INDEX]);
}
void TjdUiAppGameView::InitGameItemView(GameItemIndex index)
{
if (index < 0 || index >= GAME_MAX_INDEX) {
return;
}
if (itemView_[index] != nullptr) {
return;
}
// clang-format off
switch (index) {
case GAME_AIRCRAFT_INDEX: itemView_[index] = new GameAirsfcraftView(); break;
case GAME_BIRDFLY_INDEX: itemView_[index] = new GameBirdFlyView(); break;
default: break;
}
if (itemView_[index] == nullptr) {
return;
}
// clang-format on
mainContainer_->Add(itemView_[index]);
}
void TjdUiAppGameView::ReturnToMainPage()
{
static_print_debug("%s , line is %d , currentGameItem_ = %d", __FUNCTION__, __LINE__, currentGameItem_);
if (itemView_[currentGameItem_] != nullptr) {
itemView_[currentGameItem_]->SetVisible(false);
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
mainContainer_->Remove(itemView_[currentGameItem_]);
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
delete itemView_[currentGameItem_];
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
itemView_[currentGameItem_] = nullptr;
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
}
mainContainer_->SetVisible(true);
TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(TjdUiAppGamePresenter::GetInstance(), KeyModelType::APP_KEY_TYPE);
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
// gameList_.RequestFocus();
static_print_debug("%s , line is %d", __FUNCTION__, __LINE__);
currentGameItem_ = GAME_MAX_INDEX;
}
} // namespace TJD
// void tjd_into_photo(void)
// {
// OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_GAME);
// }