Advertisement
Guest User

DFSDK Menu Example

a guest
Oct 3rd, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. /* Copyright 2012 Juan José Sánchez Ramírez
  2.  
  3.    Licensed under the Apache License, Version 2.0 (the "License");
  4.    you may not use this file except in compliance with the License.
  5.    You may obtain a copy of the License at
  6.  
  7.        http://www.apache.org/licenses/LICENSE-2.0
  8.  
  9.    Unless required by applicable law or agreed to in writing, software
  10.    distributed under the License is distributed on an "AS IS" BASIS,
  11.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.    See the License for the specific language governing permissions and
  13.    limitations under the License.
  14.  
  15.    Author Juan José Sánchez Ramírez
  16.    Version 1.0
  17. */
  18.  
  19. #include "DragonFireSDK.h"
  20. #include "Scroller.h"
  21. #include "ScrollButton.h"
  22.  
  23. enum Screen
  24. {
  25.     SCREEN_1,
  26.     SCREEN_2,
  27.     SCREEN_3
  28. };
  29.  
  30. Screen nCurrentScreen = SCREEN_1;
  31.  
  32. Scroller scroller1;
  33. Scroller scroller2;
  34. Scroller scroller3;
  35.  
  36. ScrollButton aScrollerButton[20];
  37.  
  38. int nContainer1;
  39. int nContainer2;
  40. int nContainer3;
  41. int nGUIContainer;
  42.  
  43. int nFont;
  44. int nHeaderFont;
  45. int nHeaderFontH;
  46. int nHeaderFontS;
  47.  
  48. // This function adds text with a 1px highlight and a 1px shadow
  49. void HeaderTextAdd(int container, int x, int y, char* text)
  50. {
  51.     TextAdd(container, x, y-1, text, nHeaderFontS);
  52.     TextAdd(container, x, y+1, text, nHeaderFontH);
  53.     TextAdd(container, x, y,   text, nHeaderFont);
  54. }
  55.  
  56. // This function moves a page left
  57. void MoveLeft(Scroller &scroller)
  58. {
  59.     scroller.SetMinX(-320);
  60.     scroller.SetMaxX(-320);
  61.     scroller.SetActive(false);
  62. }
  63.  
  64. // This function moves a page right
  65. void MoveRight(Scroller &scroller)
  66. {
  67.     scroller.SetMinX(320);
  68.     scroller.SetMaxX(320);
  69.     scroller.SetActive(false);
  70. }
  71.  
  72. // This function centers a page
  73. void CenterPage(Scroller &scroller)
  74. {
  75.     scroller.SetMinX(0);
  76.     scroller.SetMaxX(0);
  77.     scroller.SetActive(true);
  78. }
  79.  
  80. // This function refreshes the screen
  81. void UpdateScreen()
  82. {
  83.     switch(nCurrentScreen)
  84.     {
  85.     case 0:
  86.         CenterPage(scroller1);
  87.         break;
  88.     case 1:
  89.         CenterPage(scroller2);
  90.         break;
  91.     case 2:
  92.         CenterPage(scroller3);
  93.         break;
  94.     };
  95. }
  96.  
  97. // This function will be called when a scroller button is pressed
  98. int OnButton(int id)
  99. {
  100.     if (id == 201) // Button on page 3
  101.     {
  102.         ContainerSetx(nContainer2, 0);
  103.         MoveRight(scroller2);
  104.         MoveRight(scroller3);
  105.         nCurrentScreen = SCREEN_1;
  106.     }
  107.     else if (id == 101) // Button on page 2
  108.     {
  109.         MoveLeft(scroller2);
  110.         nCurrentScreen = SCREEN_3;
  111.     }
  112.     else // Buttons on page 1
  113.     {
  114.         MoveLeft(scroller1);
  115.         nCurrentScreen = SCREEN_2;
  116.     }
  117.    
  118.     UpdateScreen();
  119.  
  120.     return id;
  121. }
  122.  
  123. // This function marks the beginning of program execution
  124. void AppMain()
  125. {
  126.     nContainer1 = ContainerAdd(0, 0, 0);
  127.     nContainer2 = ContainerAdd(0, 320, 0); // Container to the right
  128.     nContainer3 = ContainerAdd(0, 320, 0); // Container to the right
  129.    
  130.     nGUIContainer = ContainerAdd(0, 0, 0);
  131.  
  132.     nFont = FontAdd("Helvetica", "Regular", 18, 0x333333);
  133.    
  134.     nHeaderFont = FontAdd("Helvetica", "Bold", 30, 0xFFFFFF);
  135.     nHeaderFontS = FontAdd("Helvetica", "Bold", 30, 0x333333);
  136.     nHeaderFontH = FontAdd("Helvetica", "Bold", 30, 0x999999);
  137.    
  138.     // Initiate scroller 320x1096, ID 0, horizontal scroll false
  139.     scroller1.Init(nContainer1, 320, 1096, 0);
  140.     scroller1.SetHorizontalScroll(false);
  141.    
  142.     // Populate container 1
  143.     ViewAdd(nContainer1, "Images/Wood.png", 0, 0);
  144.     HeaderTextAdd(nContainer1, 8, 76, "First Page");
  145.  
  146.     for (int i=0; i<20; i++)
  147.     {
  148.         char filename[100];
  149.         StrCopy(filename, "Images/RoundedList");
  150.  
  151.         if (i == 0)
  152.             StrAppend(filename, "Top");
  153.         else if (i == 19)
  154.             StrAppend(filename, "Bottom");
  155.  
  156.         aScrollerButton[i].Init(nContainer1, filename, 8, i*44+122, OnButton, i+1);
  157.  
  158.         char text[100];
  159.         sprintf(text, "Button %d", i+1);
  160.        
  161.         TextAdd(nContainer1, 28, i*44+134, text, nFont);
  162.     }
  163.  
  164.     // Initiate scroller 320x480, ID 100, horizontal scroll false, active false, min X 320, max X 320
  165.     scroller2.Init(nContainer2, 320, 480, 100);
  166.     scroller2.SetHorizontalScroll(false);
  167.     scroller2.SetActive(false);
  168.     scroller2.SetMinX(320);
  169.     scroller2.SetMaxX(320);
  170.    
  171.     // Populate container 2
  172.     ViewAdd(nContainer2, "Images/Wood.png", 0, 0);
  173.     HeaderTextAdd(nContainer2, 8, 76, "Another Page");
  174.     ButtonAdd(nContainer2, "Images/RoundedButton", 8, 122, OnButton, 101);
  175.     TextAdd(nContainer2, 28, 134, "Next page", nFont);
  176.  
  177.     // Initiate scroller 320x480, ID 200, horizontal scroll false, active false, min X 320, max X 320
  178.     scroller3.Init(nContainer3, 320, 480, 200);
  179.     scroller3.SetHorizontalScroll(false);
  180.     scroller3.SetActive(false);
  181.     scroller3.SetMinX(320);
  182.     scroller3.SetMaxX(320);
  183.    
  184.     // Populate container 3
  185.     ViewAdd(nContainer3, "Images/Wood.png", 0, 0);
  186.     HeaderTextAdd(nContainer3, 8, 76, "Last Page");
  187.     ButtonAdd(nContainer3, "Images/RoundedButton", 8, 122, OnButton, 201);
  188.     TextAdd(nContainer3, 28, 134, "Go back", nFont);
  189.  
  190.     // GUI elements
  191.     ViewAdd(nGUIContainer, "Images/TopBar.png", 0, 0);
  192.     ViewAdd(nGUIContainer, "Images/Navigator.png", 0, 20);
  193.     ViewAdd(nGUIContainer, "Images/Toolbar.png", 0, 436);
  194. }
  195.  
  196. // This function is called whenever the app terminates
  197. void AppExit()
  198. {
  199.    
  200. }
  201.  
  202. // This function is called 30 times per second
  203. void OnTimer()
  204. {
  205.     scroller1.Update();
  206.     scroller2.Update();
  207.     scroller3.Update();
  208.  
  209.     for (int i=0; i<20; i++)
  210.         aScrollerButton[i].Update();
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement