Blame view

ios/Runner/Wowgame/Classes/game_food/AlertView.cpp 4.76 KB
cb213901   xiaoyu   添加一个游戏的源码和编译选项
1
2
3
4
5
6
7
8
9
10
11
12
13
  //
  //  AlertView.cpp
  //  Steve and Maggie Halloween
  //
  //  Created by Katarzyna Kalinowska-Górska on 09/10/2019.
  //
  
  #include <stdio.h>
  #include "AlertView.h"
  #include "ScalingUtils.h"
  #include "../../cocos2d/cocos/2d/CCActionInterval.h"
  #include "../../cocos2d/cocos/2d/CCActionInstant.h"
  #include "../../cocos2d/cocos/2d/CCNode.h"
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
14
  #include "MiscUtils.h"
cb213901   xiaoyu   添加一个游戏的源码和编译选项
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  
  //AlertView* AlertView::create(std::string message, std::string okText, cocos2d::Color3B okColor, std::function<void()> onConfirmCallback,){
  //
  //    AlertView * view = new (std::nothrow) AlertView();
  //    if(view && view->init(message, okText, okColor, onConfirmCallback))
  //    {
  //        view->autorelease();
  //        return view;
  //    }
  //    CC_SAFE_DELETE(view);
  //    return nullptr;
  //}
  
  AlertView* AlertView::create(std::string message, std::string okText, std::string cancelText, cocos2d::Color3B okColor, cocos2d::Color3B cancelColor, std::function<void()> onConfirmCallback,std::function<void()> onCancelCallback){
      
      AlertView * view = new (std::nothrow) AlertView();
      if(view && view->init(message, okText, cancelText, okColor, cancelColor, onConfirmCallback, onCancelCallback))
      {
          view->autorelease();
          return view;
      }
      CC_SAFE_DELETE(view);
      return nullptr;
  }
  
  bool AlertView::init(std::string message, std::string okText, std::string cancelText, cocos2d::Color3B okColor, cocos2d::Color3B cancelColor, std::function<void()> onConfirmCallback,std::function<void()> onCancelCallback){
      
      auto winSize = cocos2d::Director::getInstance()->getWinSize();
      if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0,0,0,220), winSize.width, winSize.height)){
          return false;
      }
      
      _onConfirmCallback = onConfirmCallback;
      _onCancelCallback = onCancelCallback;
      setupAppearance(message, okText, cancelText, okColor, cancelColor);
  
      return true;
  }
  
  void AlertView::setupAppearance(std::string message, std::string okText, std::string cancelText, cocos2d::Color3B okColor, cocos2d::Color3B cancelColor){
  
      setCascadeOpacityEnabled(true);
  ;
      auto wholeContainer = cocos2d::Node::create();
      addChild(wholeContainer);
      
      //    // add the instruction label
      auto messageLabel = cocos2d::Label::createWithTTF(message, "fonts/ComicSansMSBold.ttf", 90*ScalingUtils::getScaleForFont()); //TODO magic number, hard-coded text
      messageLabel->setColor(cocos2d::Color3B(200, 200, 200));
      wholeContainer->addChild(messageLabel);
      wholeContainer->setCascadeOpacityEnabled(true);
  
      cocos2d::MenuItemFont::setFontSize(80*ScalingUtils::getScaleForFont()); //TODO MAGIC NUMBER
  #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
      cocos2d::MenuItemFont::setFontName("ComicSansMS-Bold");
  #else
      cocos2d::MenuItemFont::setFontName("fonts/ComicSansMSBold.ttf");
  #endif
      auto menuPadding = 140*ScalingUtils::scaleAspectFillToDesignIpadProSize();//TODO magic number
      auto menuItemCancel = cocos2d::MenuItemFont::create(cancelText, CC_CALLBACK_1(AlertView::onCancelClicked, this));
      menuItemCancel->setColor(cancelColor);
      auto menuItemOK = cocos2d::MenuItemFont::create(okText, CC_CALLBACK_1(AlertView::onOKClicked, this));
  //    menuItemOK->setColor(cocos2d::Color3B(200, 100, 100));
      menuItemOK->setColor(okColor);
      auto menuCenter = cocos2d::Menu::create(menuItemCancel,menuItemOK, nullptr);
      menuCenter->setContentSize(cocos2d::Size(menuItemOK->getBoundingBox().size.width + menuItemCancel->getBoundingBox().size.width + menuPadding, MAX(menuItemOK->getBoundingBox().size.height, menuItemCancel->getBoundingBox().size.height)));
      menuCenter->alignItemsHorizontallyWithPadding(menuPadding);
      menuCenter->setCascadeOpacityEnabled(true);
  
      wholeContainer->addChild(menuCenter);
      wholeContainer->setContentSize(cocos2d::Size(MAX(menuCenter->getBoundingBox().size.width, messageLabel->getBoundingBox().size.width), menuCenter->getBoundingBox().size.height + menuPadding + messageLabel->getBoundingBox().size.height));
      
      menuCenter->setPositionX(wholeContainer->getContentSize().width/2);
      menuCenter->setPositionY(messageLabel->getBoundingBox().getMinY() + menuPadding);
      messageLabel->setPositionY(wholeContainer->getContentSize().height - messageLabel->getBoundingBox().size.height/2);
      messageLabel->setPositionX(wholeContainer->getContentSize().width/2);
      
      wholeContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
      wholeContainer->setPosition(getContentSize().width/2, getContentSize().height/2);
  }
  
  void AlertView::onCancelClicked(cocos2d::Ref* pSender){
      _onCancelCallback();
          runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(MiscUtils::StandardAnimationTime), cocos2d::CallFunc::create([&](){
              MiscUtils::hideAndRemoveView(this,true);
          }), nullptr));
  }
  
  void AlertView::onOKClicked(cocos2d::Ref* pSender){
      _onConfirmCallback();
  }