HLevelPickerLayer.cpp 2.96 KB
//
//  HLevelPickerLayer.cpp
//  HalloweenSpaceInvaders-mobile
//
//  Created by Katarzyna Kalinowska-Górska on 03/10/2019.
//

#include <stdio.h>
#include "HLevelPickerLayer.h"
#include "HScalingUtils.h"
#include "HalloweenSimpleButton.h"
#include "HMiscUtils.h"

HLevelPickerLayer* HLevelPickerLayer::create(float width, float height){
    HLevelPickerLayer * view = new (std::nothrow) HLevelPickerLayer();
    if(view && view->init(width, height))
    {
        view->autorelease();
        return view;
    }
    CC_SAFE_DELETE(view);
    return nullptr;
}

bool HLevelPickerLayer::init(float width, float height){
    if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0,0,0,220), width, height)){
        return false;
    }
    
    setCascadeOpacityEnabled(true);
    // add the level picker
    std::vector<std::string> levelImagePaths = { "graphics/levels/level_1.png", "graphics/levels/level_2.png", "graphics/levels/level_3.png" };
    std::vector<std::string> levelNames = { "EASY", "MEDIUM", "DIFFICULT" }; //TODO HARD CODED
    _HLevelPickerView = HLevelPickerView::create(levelImagePaths, levelNames);
    addChild(_HLevelPickerView);
    _HLevelPickerView->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    _HLevelPickerView->setPosition(width/2, height/2);
    _HLevelPickerView->addOnLevelChangedCallback([](int pickedLevel){
        HMiscUtils::saveLastLevel(pickedLevel);
    });
    _HLevelPickerView->selectLevelIndex((int)HMiscUtils::lastLevel(), false);
    
    // add the instruction label
    auto instructionLabel = cocos2d::Label::createWithTTF("CHOOSE YOUR LEVEL:", "fonts/ComicSansMSBold.ttf", 120*HScalingUtils::getScaleForFont()); //magic number
    addChild(instructionLabel);
    auto paddingTop = instructionLabel->getBoundingBox().size.height*1.5f;
    instructionLabel->setPosition(width/2, _HLevelPickerView->getBoundingBox().getMaxY() + paddingTop);
    
    // add the go button
    auto buttonGo = HalloweenSimpleButton::create();
    auto goButtonTexturePath = "buttons/graphics/dark_green.png";
    buttonGo->loadTextures(goButtonTexturePath, goButtonTexturePath, goButtonTexturePath);
    auto buttonBg = cocos2d::Sprite::create("buttons/graphics/button_go.png");
    buttonGo->addChild(buttonBg);
    buttonBg->setPosition(cocos2d::Vec2(buttonGo->getContentSize().width/2,buttonGo->getContentSize().height/2));
    addChild(buttonGo);
    auto paddingBottom = paddingTop;
    buttonGo->setAnchorPoint(cocos2d::Vec2(1, 0.5));
    buttonGo->setPosition(cocos2d::Vec2(_HLevelPickerView->getBoundingBox().getMaxX(), _HLevelPickerView->getBoundingBox().getMinY() - paddingBottom));
    buttonGo->setOnTouchEndedCallback([&](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
        if(_onGoPressedCallback != nullptr){
            _onGoPressedCallback();
        }
        HMiscUtils::hideAndRemoveView(this, true);
    });
    
    return true;
}

void HLevelPickerLayer::addOnGoPressedCallback(std::function<void()> onGoPressed){
    _onGoPressedCallback = onGoPressed;
}