1. #include "MainMenu.h"
  2. #include "Game.h"
  3.  
  4. MainMenu::MainMenu()
  5. {
  6.  
  7. }
  8. void MainMenu::init()
  9. {
  10.     this->view = IwGetUIView();
  11.     this->controller = IwGetUIController();
  12.     CIwResGroup* pResGroup = IwGetResManager()->LoadGroup("main_menu.group");
  13.  //   this->create_button_style();
  14.  
  15.     IwGetUIStyleManager()->SetStylesheet(this->CreateStyles());
  16.     //CIwUIElement* pElement = new CIwUIElement;
  17.     CIwUIElement* pDialog = CreateDialog();
  18.     this->view->AddElementToLayout(pDialog);
  19. }
  20.  
  21.  
  22. CIwUIElement* MainMenu::CreateDialog()
  23. {
  24.     // Create scrollable view with vertical layout
  25.    
  26.     CIwUILayoutGrid* pLayout = new CIwUILayoutGrid;
  27.     for(int i = 0; i < 4; i++)
  28.     {
  29.         pLayout->AddColumn();
  30.     }
  31.  
  32.     for(int i = 0; i < 15 ; i++)
  33.     {
  34.         pLayout->AddRow();
  35.     }
  36.    
  37.     CIwUIButton* pStartButton = new CIwUIButton;
  38.     pStartButton->SetStyle("<button>");
  39.     pStartButton->SetCaption("START");
  40.            
  41.  
  42.     CIwUIButton* pContinueButton = new CIwUIButton;
  43.     pContinueButton->SetStyle("<button>"); 
  44.     pContinueButton->SetCaption("CONTINUE");
  45.    
  46.  
  47.  
  48.     CIwUIButton* pOptionButton = new CIwUIButton;
  49.     pOptionButton->SetStyle("<button>");   
  50.     pOptionButton->SetCaption("OPTIONS");
  51.    
  52.  
  53.    
  54.     CIwUIButton* pExtraButton = new CIwUIButton;
  55.     pExtraButton->SetStyle("<button>");
  56.     pExtraButton->SetCaption("EXTRA");
  57.    
  58.  
  59.     CIwUIImage* pImage = new CIwUIImage;
  60.     pImage->SetTexture(GetTexture("start"));
  61.     pImage->SetSizeToContent(true);
  62.    
  63.  
  64. //  pButton->AttachClickSlot("RequestQuit");
  65.     //pLayout->AddElement(pImage,0,0);
  66.     pLayout->AddElement(pStartButton, 2,9);
  67.     pLayout->AddElement(pContinueButton,2, 10);
  68.     //pLayout->AddElement(pOptionButton,2, 11);
  69.     //pLayout->AddElement(pExtraButton,2, 12);
  70.  
  71.    
  72.  
  73.     CIwUIScrollableView* pContainer = new CIwUIScrollableView;
  74.     pContainer->SetSizeToContent(true);
  75.    
  76.     pContainer->AddChild(pImage);//SET THE BACKGROUND
  77.     pContainer->AddChild(pStartButton);
  78.     pContainer->AddChild(pContinueButton);
  79.     //pContainer->AddChild(pOptionButton);
  80.     //pContainer->AddChild(pExtraButton);
  81.  
  82.     pContainer->SetLayout(pLayout);
  83.    
  84.     return pContainer;
  85.    
  86. }
  87. CIwUIStylesheet* MainMenu::CreateStyles()
  88. {
  89.     // Create stylesheet and make resource manager own it.
  90.     CIwUIStylesheet* pStylesheet = new CIwUIStylesheet;
  91.     IwGetResManager()->AddRes(IW_UI_RESTYPE_STYLESHEET, pStylesheet);
  92.  
  93.     pStylesheet->AddPropertySet(this->create_button_style());
  94.  
  95.  
  96.     return pStylesheet;
  97. }
  98.  
  99. void MainMenu::update(int delta)
  100. {
  101.     this->controller->Update();
  102.     this->view->Update(delta); //IF I UNCOMMENT THIS IT WILL CENTERS THE BUTTON ELEMENT ON UPDATE
  103. }
  104. void MainMenu::render()
  105. {
  106.     this->view->Render();
  107.    
  108. }  
  109. CIwUIPropertySet* MainMenu::create_button_style()
  110. {
  111.     // Create button style
  112.     CIwUIPropertySet* pButton = new CIwUIPropertySet;
  113.     pButton->SetName("<button>");
  114.    
  115.     pButton->SetProperty("font", GetFont("font"));
  116.     pButton->SetProperty("sizeToContent", true);
  117.     pButton->SetProperty("alignH", IW_UI_ALIGN_CENTRE);
  118.     pButton->SetProperty("alignV", IW_UI_ALIGN_MIDDLE);
  119.     pButton->SetProperty("sizeMin", CIwVec2(128, 32));
  120.     pButton->SetProperty("margin", CIwSVec2(16, 8));
  121.    
  122.     // Create button up style
  123.     CIwUIStyle buttonUpStyle;
  124.     buttonUpStyle.InlinePropertySet();
  125.    
  126.     CIwUIPropertySet& buttonUp = *buttonUpStyle.GetInlinePropertySet();
  127.     buttonUp.SetProperty("drawableType", IwHashString("border"));
  128.     buttonUp.SetProperty("border", CIwSVec2(3, 3));
  129.     buttonUp.SetProperty("texture", GetTexture("buttonUp"));
  130.    
  131.     // Create button down style
  132.     CIwUIStyle buttonDownStyle;
  133.     buttonDownStyle.InlinePropertySet();
  134.    
  135.     CIwUIPropertySet& buttonDown = *buttonDownStyle.GetInlinePropertySet();
  136.     buttonDown.SetProperty("drawableType", IwHashString("border"));
  137.     buttonDown.SetProperty("border", CIwSVec2(13, 13));
  138.     buttonDown.SetProperty("texture", GetTexture("buttonDown"));
  139.    
  140.     // Attach inline styles to main style
  141.     pButton->SetProperty("buttonUp", buttonUpStyle);
  142.     pButton->SetProperty("buttonDown", buttonDownStyle);
  143.  
  144.     return pButton;
  145. }
  146. CIwTexture* MainMenu::GetTexture(const char* pTextureName)
  147. {
  148.     return IwSafeCast<CIwTexture*>(
  149.         IwGetResManager()->GetResNamed(pTextureName, IW_GX_RESTYPE_TEXTURE));
  150. }
  151.  
  152. CIwGxFont* MainMenu::GetFont(const char* pFontName)
  153. {
  154.     return IwSafeCast<CIwGxFont*>(
  155.         IwGetResManager()->GetResNamed(pFontName, IW_GX_RESTYPE_FONT));
  156. }