Page 1 of 2

Menu example code needed

Posted: 04 Feb 2012, 13:46
by Jojo_44
Hi,

I´m trying to use the manialib build in menu, which is also used by ManiaHome f.e. But I´m not sure how to use it correctly. Do I have to create the menu on every view or is there a way to do it one time and just reload the content. I also wanna know how I can make this nice "flying in" effect in the main menu of the game if I click a button (It seems that the content flying in).

Hopefully someone can give me some example code or a tip.

best regards, Jojo

Re: Menu example code needed

Posted: 04 Feb 2012, 13:55
by w1lla
I use this to make a menu kind of way:

Code: Select all

<?php
require_once 'manialib/libraries/autoload.php';
use ManiaLib\Gui\Manialink;
use ManiaLib\Gui\Cards\Navigation\Menu;
use ManiaLib\Gui\Cards\Navigation\Button;
use ManiaLib\Gui\Elements\Label;
use ManiaLib\Gui\Elements\Quad;
use ManiaLib\Gui\Elements\Bgs1;
use ManiaLib\Gui\Elements\FileEntry;
use ManiaLib\Gui\Elements\Icons128x128_1;
use ManiaLib\Gui\Elements\Icons64x64_1;
use ManiaLib\Gui\Layouts\Column;
use ManiaLib\Gui\Cards\Panel;

Manialink::load();

Manialink::beginFrame(0, 0, 0);

$ui = new Bgs1(110,180);
$ui->setSubStyle(Bgs1::BgWindow1);
$ui->setPosition(-160, 90, 0.1);
$ui->save();

$ui = new FileEntry(39, 10);
$ui->setPosY(16, -50, 0);
$ui->setFolder("");
$ui->setName("Maps");
$ui->setDefault("Select Map...");
$ui->save();

$ui = new Button();
$ui->setPosition(-110, -30, 0.1);
$ui->text->setText('$09fAdvanced');
$ui->icon->setPosition(-2, -1, 0.1);
$ui->icon->setSubStyle(Icons128x128_1::ProfileAdvanced);
$ui->setManialink('blabla');
$ui->save();

Manialink::endFrame();

Manialink::render();

?>
Using the require_once outside the controllers of manialib is possible and you can make every page look the same.

However "flying in" is not something i have seen before. However with this you get a almost same way kind of menu as maniahome has.

hope this sets up your requirements.

Re: Menu example code needed

Posted: 04 Feb 2012, 14:18
by Jojo_44
Hi,

first of all thanks for your answer. But I´m talking about the pre-defined menu from manialib (new Menu()). I guess gouxim or farfa have done the ManiaHome Menu with this. And I wanna know how to use this exactly, because there´s no example available. I´ve taken a look into maniapress and maniahost but there´s no global menu (every view has it´s own).

To the "flying in" thing: Go to the main menu and click on some buttons, f.e. All, Favourites or Create, and you will see that the content is flying from the right side into the end position. The effect is not very strong but you can see it. If you finally compare it with a normal ml you will see it.

best regards, Jojo

Re: Menu example code needed

Posted: 04 Feb 2012, 15:02
by w1lla
ah k for the first part:

edit manialib\libraries\ManiaLib\Gui\Cards\Navigation\menu.php

Code: Select all

<?php
/**
 * ManiaLib - Lightweight PHP framework for Manialinks
 * 
 * @see         http://code.google.com/p/manialib/
 * @copyright   Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL License 3
 * @version     $Revision: 590 $:
 * @author      $Author: maximeraoust $:
 * @date        $Date: 2011-12-27 19:40:38 +0100 (Tue, 27 Dec 2011) $:
 */

namespace ManiaLib\Gui\Cards\Navigation;

use ManiaLib\Gui\Manialink;
use ManiaLib\Gui\Elements\Quad;
use ManiaLib\Gui\Elements\Bgs1;
use ManiaLib\Gui\Elements\Icons128x128_1;
use ManiaLib\Gui\Elements\Icons64x64_1;
use ManiaLib\Gui\Elements\Label;
use ManiaLib\Gui\Layouts\Column;

/**
 * Navigation menu
 * Looks like the navigation menu on the left in the game menus
 */
class Menu extends Bgs1
{
	const BUTTONS_TOP = true;
	const BUTTONS_BOTTOM = false;

	/**
	 * @var Label
	 */
	public $title;

	/**
	 * @var Label
	 */
	public $subTitle;

	/**
	 * @var \ManiaLib\Gui\Elements\Quad
	 */
	public $titleBg;

	/**
	 * @var \ManiaLib\Gui\Elements\Quad
	 */
	public $logo;

	/**
	 * @var \ManiaLib\Gui\Cards\Navigation\Button
	 */
	public $quitButton;

	public $forumlink;
	
	public $multismurf;
	
	public $solosmurf;
	
	/**
	 * @var \ManiaLib\Gui\Cards\Navigation\Button
	 */
	public $lastItem;
	protected $showQuitButton = true;
	protected $items = array();
	protected $bottomItems = array();
	protected $marginHeight = 1;
	protected $yIndex = -10;

	function __construct()
	{
		parent::__construct(110, 180);

		$this->setSubStyle(Bgs1::BgWindow1);
		$this->setPosition(-150, 90, 0.1);

		$this->titleBg = new Quad(200, 20);
		$this->titleBg->setPosition(110, -15);
		$this->titleBg->setImage("http://www.smurfen.net/images/header.png", true);
		$this->addCardElement($this->titleBg);

		$this->logo = new Icons128x128_1(16);
		$this->logo->setPosition(4, -15, 0.1);
		$this->logo->setSubStyle(Icons128x128_1::Vehicles);
		$this->addCardElement($this->logo);

		$this->title = new Label(46);
		$this->title->setPosition(30, -20, 0.1);
		$this->title->setStyle(Label::TextTitle1);
		$this->title->setScriptEvents();
		$this->title->setText('Welcome to $06f$lSmurfen.net$l');
		$this->addCardElement($this->title);

		$this->subTitle = new Label(46);
		$this->subTitle->setPosition(30, -25.75, 0.1);
		$this->subTitle->setStyle(Label::TextSubTitle1);
		$this->subTitle->setText('Online Gaming Community');
		$this->addCardElement($this->subTitle);
		
		
		$this->forumlink = new Button();
		$this->forumlink->setPosition(0, -163.5, 0.1);
		$this->forumlink->text->setText('Homepage');
		$this->forumlink->text->setStyle(Label::TextButtonNavBack);
		$this->forumlink->icon->setPosition(-2, -0.5, 0.1);
		$this->forumlink->icon->setStyle(Quad::Icons128x128_1);
		$this->forumlink->icon->setSubStyle(Icons128x128_1::BackFocusable);
		$this->forumlink->icon->setSize(11, 11);
		$this->forumlink->setUrl('www.smurfen.net');
		
		$this->multismurf = new Button();
		$this->multismurf->setPosition(10, -150, 0.1);
		$this->multismurf->text->setText('Multiplayer Ranking');
		$this->multismurf->icon->setPosition(-8, -0.5, 0.1);
		$this->multismurf->icon->setSubStyle(Icons128x128_1::Multiplayer);
		$this->multismurf->icon->setSize(11, 11);
		
		$this->solosmurf = new Button();
		$this->solosmurf->setPosition(10, -137.5, 0.1);
		$this->solosmurf->text->setText('Solo Ranking');
		$this->solosmurf->icon->setPosition(-8, -0.5, 0.1);
		$this->solosmurf->icon->setSubStyle(Icons128x128_1::Solo);
		$this->solosmurf->icon->setSize(11, 11);
	}

	/**
	 * Adds a navigation button to the menu
	 */
	function addItem($topItem = self::BUTTONS_TOP)
	{
		$item = new Button();
		$item->setSubStyle(Bgs1::BgEmpty);
		if($topItem == self::BUTTONS_TOP)
		{
			$this->items[] = $item;
		}
		else
		{
			$this->bottomItems[] = $item;
		}
		$this->lastItem = $item;
	}

	/**
	 * Adds a vertical gap before the next item
	 * @param float
	 */
	function addGap($gap = 4)
	{
		$item = new \ManiaLib\Gui\Elements\Spacer(1, $gap);
		$this->items[] = $item;
	}

	/**
	 * Hides the quit/back button
	 */
	function hideQuitButton()
	{
		$this->showQuitButton = false;
	}

	protected function preFilter()
	{
		$this->subTitle->setText('$o$999'.$this->subTitle->getText());
		$this->forumlink->text->setText('$09f'.$this->forumlink->text->getText());
		$this->forumlink->text->setPosX($this->forumlink->text->getPosX() - 1);
		$this->addCardElement($this->forumlink);
		$this->multismurf->text->setText('$09f'.$this->multismurf->text->getText());
		$this->multismurf->text->setPosX($this->multismurf->text->getPosX() - 1);
		$this->addCardElement($this->multismurf);
		$this->solosmurf->text->setText('$09f'.$this->solosmurf->text->getText());
		$this->solosmurf->text->setPosX($this->solosmurf->text->getPosX() - 1);
		$this->addCardElement($this->solosmurf);
		//$this->playerlink->text->setText('$09f'.$this->playerlink->text->getText());
		//$this->playerlink->text->setPosX($this->playerlink->text->getPosX() - 1);
		//$this->addCardElement($this->playerlink);
		
	}

	protected function postFilter()
	{
		Manialink::beginFrame($this->posX, $this->posY, $this->posZ + 0.1);
		{
			if($this->items)
			{
				$layout = new Column();
				$layout->setMarginHeight(5);
				Manialink::beginFrame(0, -62, 0, 1, $layout);
				foreach($this->items as $item)
				{
					$item->save();
				}
				Manialink::endFrame();
			}
			if($this->bottomItems)
			{
				$this->bottomItems = array_reverse($this->bottomItems);

				$layout = new Column();
				$layout->setDirection(Column::DIRECTION_UP);
				$layout->setMarginHeight(5);
				Manialink::beginFrame(0, -160, 0, 1, $layout);
				foreach($this->bottomItems as $item)
				{
					$item->save();
				}
				Manialink::endFrame();
			}
		}
		Manialink::endFrame();
	}

}

?>
This will give you a menu basicly the same as menu of maniahome. but it misses images etc.

to use it just make

Code: Select all

 $ui = new Menu();
$ui->save();

The other one i did not get into yet so hold on for more information.

Re: Menu example code needed

Posted: 04 Feb 2012, 16:26
by Jojo_44
Hi,

big thanks for your effort. I know how to create the menu, addItem, etc. I want to know if I need to create the menu in evey view or is there another easier way.

best regards, Jojo

Re: Menu example code needed

Posted: 04 Feb 2012, 16:37
by farfa
If the menu is the same, I recommand you to create your menu in a separate view, and to use the "renderSubView" method in your view to render your Menu ;)

For example if your menu is coded in Navigation class. In your view you have to do this

Code: Select all

namespace MyProject\View\MyController;

class MyView extends \ManiaLib\Application\View
{
   function display()
   {
      $this->renderSubView('\MyProject\View\Navigation');
      //.....
   }
}

Re: Menu example code needed

Posted: 04 Feb 2012, 17:53
by Jojo_44
Hi,

ok thanks. Your code isn´t working out of the box because the path of renderSubView() always starts at the view you call the function. F.e. if you call renderSubView('\MyProject\Views\Navigation') from \MyProject\Views\Home it produces the path "\MyProject\Views\Home\\MyProject\Views\Navigation" ->ofc error. So your Navigation must be loacted in your view folder where you call renderSubView ?

I also have two question left.

1. How to set a link for a menu item ? I tried the code below but it didn´t work.

Code: Select all

$ui->addItem();
		$ui->lastItem->text->setText('MenuItem1');
		$manialink = $this->request->createLink('/home');
		$ui->lastItem->text->setManialink($manialink);
2. The link of the "back" button of the menu isn´t automatically set by manialib ?

Thanks, Jojo

Re: Menu example code needed

Posted: 04 Feb 2012, 19:57
by The_Big_Boo
Hi,
Jojo_44 wrote:ok thanks. Your code isn´t working out of the box because the path of renderSubView() always starts at the view you call the function. F.e. if you call renderSubView('\MyProject\Views\Navigation') from \MyProject\Views\Home it produces the path "\MyProject\Views\Home\\MyProject\Views\Navigation" ->ofc error. So your Navigation must be loacted in your view folder where you call renderSubView ?
Actually, the parameter passed to renderSubView() is a class name starting from the current namespace. So is you call renderSubView('Navigation'), Navigation need to be in the same folder than your view. The function always add the current namespace in front of the parameter. Maybe it's something to improve by the way so a absolute namespace can be use instead.

1. You need to call setManialink directly on lastItem, it will pass it to every element composing the button (and you can't do it another way):

Code: Select all

$ui->addItem();
$ui->lastItem->text->setText('MenuItem1');
$manialink = $this->request->createLink('/home');
$ui->lastItem->setManialink($manialink);
2. No, it isn't set automatically because it can be used whether to close the manialink explorer, or to go to a main page or wherever you want.

Re: Menu example code needed

Posted: 04 Feb 2012, 20:19
by Jojo_44
Hi,

thank you very much. Everything works fine now.

best regards, Jojo

Re: Menu example code needed

Posted: 05 Feb 2012, 19:37
by Jojo_44
While we're on it, can you give me an example of how I can use maniascript in manialib ? F.e. I have a frame with a quad and I wanna interact with the quad if someone clicks on it. I have some trouble with this. Also how to include the manialib.xml (I guess it´s the maniascript code) and where to include it.

It´s very hard to get this without any documentation ;)

Thanks, Jojo