Beispiel #1
0
void StateMap::setupMapLevels() {
    const size_t levelAmount = 2;

    // Position of all level buttons
    const Vec2f positions[ levelAmount ] = {
        Vec2f( 320, 240 ),
        Vec2f( 450, 300 ),
    };

    // Level info of all level buttons
    const LevelInfo infos[ levelAmount ] = {
        DATA_MANAGER.getLevelInfo( "level_1" ),
        DATA_MANAGER.getLevelInfo( "level_2" ),
    };

    // Create container element for all level buttons
    m_lvButtons = UIElement::create( "MapButtons" );

    // Create level buttons
    for( size_t i = 0; i < levelAmount; i++ ) {
        UIButton* button = UIButton::create();
        // Add button to the container
        m_lvButtons->addChild( button );

        // Basic setup, same for all buttons
        button->setSprite( "button_level_dev" );
        button->setSize( 50, 50 );
        button->enSelectOnClick( true );

        // Button-specific data
        button->setPos( positions[ i ] );

        // Set button callback
        // Manages selection of level buttons
        // Note: There can always be only one level selected
        button->setButtonCallback( UIButton::Selected, [this,info = infos[ i ]](UIButton* button){
            // If the same button was selected again
            if( button == m_selectLvButton ) {
                return;
            }

            if( m_selectLvButton ) {
                m_selectLvButton->setSelected( false );
            }

            m_selectLvButton    = button;
            m_selectLvButton->resetNested();
            m_selectedLevelID   = info.levelID;
            setupDisplayInfo( info );
        } );
    }

    // Add the container to UI
    UI_MANAGER.addElement( m_lvButtons );
}
Beispiel #2
0
UIButton* UI::addButton(int id, int x, int y, int width, int height, 
            uint32_t textureID_idle, uint32_t textureID_hover, uint32_t textureID_press,  
            const char* text, void (*callback)(UINode* Sender), UINode* parent)
{
    UIButton* button;
    if(parent==NULL) button = new UIButton(mRootNode);
    else button = new UIButton(parent);
    button->nodeID = id;
    button->textureID_idle = textureID_idle;
    button->textureID_hover = textureID_hover;
    button->textureID_press = textureID_press;
    button->setPos(x, y);
    button->setText(text);
    button->setSize(width, height);
    button->setCallback(callback);
    nodeList.push_back(button);
    return button;
}
bool UIButtonTest_Scale9::init()
{
    if (UIScene::init())
    {
        CCSize widgetSize = m_pWidget->getSize();
        
        // Add a label in which the button events will be displayed
        m_pDisplayValueLabel = UILabel::create();
        m_pDisplayValueLabel->setText("No Event");
        m_pDisplayValueLabel->setFontName("Marker Felt");
        m_pDisplayValueLabel->setFontSize(32);
        m_pDisplayValueLabel->setAnchorPoint(ccp(0.5f, -1));
        m_pDisplayValueLabel->setPosition(ccp(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
        m_pUiLayer->addWidget(m_pDisplayValueLabel);
        
        // Add the alert
        UILabel* alert = UILabel::create();
        alert->setText("Button scale9 render");
        alert->setFontName("Marker Felt");
        alert->setFontSize(30);
        alert->setColor(ccc3(159, 168, 176));
        alert->setPosition(ccp(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75));
        m_pUiLayer->addWidget(alert);
        
        // Create the button
        UIButton* button = UIButton::create();
        button->setTouchEnabled(true);
        // open scale9 render
        button->setScale9Enabled(true);
        button->loadTextures("cocosui/button.png", "cocosui/buttonHighlighted.png", "");
        button->setPosition(ccp(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
        button->setSize(CCSizeMake(150, button->getContentSize().height * 1.5f));
        button->addTouchEventListener(this, toucheventselector(UIButtonTest_Scale9::touchEvent));
        m_pUiLayer->addWidget(button);
        
        return true;
    }
    return false;
}
Beispiel #4
0
void StateMap::setupLvDisplay() {
    // Create display element
    m_lvInfo = UIElement::create( "MapLevelInfo" );
    // Register the display in UI manager
    UI_MANAGER.addElement( m_lvInfo );

    const Vec2f displaySize  = Vec2f( 600, 380 );
    const Vec2i windowCenter = Game::getWindowCenter();

    // Set size and position
    m_lvInfo->setSize( displaySize );
    m_lvInfo->setCenter( windowCenter );

    // Hide the display by default
    m_lvInfo->setActive( false );
    m_lvInfo->setVisible( false );

    /** Sprites */
    {
        // Create sprite for display background
        Sprite* bgSprite = new Sprite();
        bgSprite->setName( "MapLevelInfoSprite" );
        bgSprite->setSprite( "window_map_levelinfo" );

        // Create sprite for level display
        Sprite* levelSprite = new Sprite();
        levelSprite->setName( "MapLevelInfoLevelSprite" );
        levelSprite->setScale( 0.25, 0.25 );
        levelSprite->setPos( 45, 92 );

        // Add sprites to the display
        m_lvInfo->addChild( bgSprite );
        m_lvInfo->addChild( levelSprite, 1 );
    }

    /** Level name */
    {
        Text* name = new Text();
        name->setName( "MapLevelName" );
        name->setFont( "" );
        name->setCharSize( 18 );
        name->setColor( sf::Color::Yellow );
        name->setAlign( Text::Align_Center );
        name->setPos( displaySize.x / 2, 20 );

        m_lvInfo->addChild( name, 1 );
    }

    /** Buttons */
    {
        const Vec2f buttonSize  = Vec2f( 130, 25 );
        const int   offset      = 20;

        UIButton* play = UIButton::create( "MapButtonPlay", "button_dev" );
        play->setSize( buttonSize );
        play->setPos( offset, displaySize.y - buttonSize.y - offset );
        play->setButtonCallback( UIButton::Clicked, DOS_FUNC_BIND_1( &StateMap::cbLevelPlay, this ) );

        UIButton* cancel = UIButton::create( "MapButtonCancel", "button_dev" );
        cancel->setSize( buttonSize );
        cancel->setPos(
                      displaySize.x - buttonSize.x - offset,
                      displaySize.y - buttonSize.y - offset
                    );
        cancel->setButtonCallback( UIButton::Clicked, DOS_FUNC_BIND_1( &StateMap::cbLevelCancel, this ) );

        m_lvInfo->addChild( play, 1 );
        m_lvInfo->addChild( cancel, 1 );
    }
}