Advertisement
Guest User

Untitled

a guest
Jul 31st, 2010
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.15 KB | None | 0 0
  1. // menuitem
  2. using System;
  3. using System.Drawing;
  4. using SdlDotNet.Graphics;
  5.  
  6. namespace SmallGameLibrary.Widgets.Menu
  7. {
  8.     public class SimpleMenuItem : MenuItem
  9.     {
  10.         public string item;
  11.         Color textColour {
  12.             get {
  13.                 if (isSelected) {
  14.                     return Color.Gold;
  15.                 } else {
  16.                     return Color.White;
  17.                 }
  18.             }
  19.         }
  20.  
  21.         SdlDotNet.Graphics.Font fnt;
  22.        
  23.         public void setup (string _item, int _x, int _y, Size _size)
  24.         {
  25.             item = _item;
  26.             x = _x-_size.Width/2;
  27.             y = _y;        
  28.             this.size = _size;         
  29.             surface = new Surface (this.size);
  30.             int size = 200;
  31.             fnt = new SdlDotNet.Graphics.Font ("media/freesans.ttf", size);
  32.             while (fnt.SizeText ("a").Width*item.Length >= this.size.Width ||
  33.                 fnt.SizeText ("a").Height >= this.size.Height + 10) {
  34.                 size -= 1;
  35.                 fnt.Close ();
  36.                 fnt = new SdlDotNet.Graphics.Font ("media/freesans.ttf", size);
  37.             }      
  38.         }
  39.        
  40.         public SimpleMenuItem (string _item, int _x, int _y, Size _size, ref object _point)
  41.         {
  42.             point = _point;
  43.             setup (_item, _x, _y, _size);
  44.         }
  45.        
  46.         public SimpleMenuItem (string _item, int _x, int _y, Size _size, object _point)
  47.         {
  48.             point = _point;
  49.             setup (_item, _x, _y, _size);
  50.         }
  51.        
  52.         public override void move (double elapsed)
  53.         {
  54.             x += xSpeed * elapsed;
  55.             y += ySpeed * elapsed;
  56.             if ((ySpeed < 0 && y <= yDest) || (ySpeed > 0 && y >= yDest))
  57.             {
  58.                 ySpeed = 0;
  59.                 yDest = (int)y;
  60.             }
  61.         }
  62.        
  63.         public override void render (Surface screen)
  64.         {
  65.             surface.Fill (Color.Black);
  66.             Surface text = fnt.Render (item, textColour);
  67.             surface.Blit (text, new Point (this.size.Width / 2 - text.Width / 2,
  68.                     this.size.Height / 2 - text.Height / 2));          
  69.             screen.Blit (surface, new Point ((int)x, (int)y));
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. // menu
  76.  
  77. using System;
  78. using System.Drawing;
  79. using System.Collections.Generic;
  80. using SdlDotNet.Graphics;
  81. using SdlDotNet.Input;
  82. using SmallGameLibrary;
  83.  
  84. namespace SmallGameLibrary.Widgets.Menu
  85. {
  86.     public class SimpleMenu : Menu
  87.     {
  88.         public event menuEntryChosen OnMenuEntryChosen;
  89.         int slowSpeed = 200;
  90.         int fastSpeed = 850;
  91.         int gameHeight;
  92.         int gameWidth;
  93.         int backi, upi, downi;
  94.        
  95.         public SimpleMenu (Dictionary<string, Widget> items, ref object _parent, int _gameWidth,
  96.             int _gameHeight, bool isTopLevel)
  97.         {
  98.             widgets = new List<MenuItem> ();
  99.             int x = _gameWidth/2;
  100.             int y = 10;
  101.             parent = _parent;
  102.             space = 50;
  103.             gameHeight = _gameHeight;
  104.             gameWidth = _gameWidth;
  105.             Size s = new Size(700, 100);
  106.             foreach (string item in items.Keys)
  107.             {
  108.                 SimpleMenuItem m = new SimpleMenuItem(item, x, y, s, items[item]);
  109.                 widgets.Add (m);
  110.                 y += m.surface.Height + space;
  111.             }  
  112.             backi = -1;
  113.             SimpleMenuItem up = new SimpleMenuItem ("up", gameWidth-60, 0, new Size (100, 50), null);
  114.             widgets.Add (up);
  115.             upi = widgets.Count -1;
  116.             SimpleMenuItem down = new SimpleMenuItem ("down", gameWidth-60, gameHeight-50,
  117.                 new Size(100, 50), null);
  118.             widgets.Add (down);
  119.             downi = widgets.Count - 1;
  120.             if (!isTopLevel) {
  121.                 Console.WriteLine ("SimpleMenu 45: {0}", parent);
  122.                 SimpleMenuItem back = new SimpleMenuItem ("back", 0, gameHeight / 2 - s.Height / 2, new Size (100, 50), ref parent);
  123.                 back.x = 0;
  124.                 widgets.Add (back);
  125.                 backi = widgets.Count - 1;
  126.             }          
  127.             widgets[0].isSelected = true;
  128.         }
  129.        
  130.         public override void keyInput (Key key)
  131.         {
  132.             if (key == Key.UpArrow)
  133.             {
  134.                 incSelection (-1);
  135.             } else if (key == Key.DownArrow)
  136.             {
  137.                 incSelection (1);
  138.             } else if (key == Key.LeftArrow && (backi != -1))
  139.             {
  140.                 setSelection (backi);
  141.             } else if (key == Key.Return)
  142.             {
  143.                 if (selected < upi || (backi != -1 && selected == backi))
  144.                 {
  145.                     if (OnMenuEntryChosen != null)
  146.                     {
  147.                         OnMenuEntryChosen (this, new MenuEntryChosenArgs (ref widgets[selected].point));
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.        
  153.         public override void mouseMotion (int x, int y)
  154.         {
  155.             for (int i = 0; i < widgets.Count; i++)
  156.             {
  157.                 MenuItem w = widgets[i];
  158.                 if (UsefulFunctions.pointOverRect (x, y, (float)w.x, (float)w.y, w.surface.Width, w.surface.Height))
  159.                 {
  160.                     setSelection (i);
  161.                 }
  162.             }  
  163.         }
  164.  
  165.         public override void mouseButtonDown (MouseButtonEventArgs args)
  166.         {
  167.             for (int i = 0; i < widgets.Count; i++) {
  168.                 MenuItem w = widgets[i];
  169.                 if (UsefulFunctions.pointOverRect (args.X, args.Y, (float)w.x, (float)w.y, w.surface.Width, w.surface.Height))
  170.                 {
  171.                     if (i < upi || (backi != -1 && i == backi))
  172.                     {
  173.                         OnMenuEntryChosen (this, new MenuEntryChosenArgs (ref w.point));
  174.                     } else if (i == downi)
  175.                     {
  176.                         setWidgetsYMove (-slowSpeed, 1000000);
  177.                     } else if (i == upi)
  178.                     {
  179.                         setWidgetsYMove (slowSpeed, 1000000);                      
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.  
  185.         public override void mouseButtonUp (MouseButtonEventArgs args)
  186.         {
  187.             setWidgetsYMove (0, 0);
  188.         }
  189.  
  190.         void incSelection (int inc)
  191.         {
  192.             int n = selected + inc;
  193.             setWidgetsYMove (-slowSpeed * inc);
  194.             if (n == -1)
  195.             {
  196.                 n = upi - 1;
  197.                 setWidgetsYMove (-fastSpeed, widgets.Count-2);
  198.             } else if (n == downi-1)
  199.             {
  200.                 n = 0;
  201.                 setWidgetsYMove (fastSpeed, widgets.Count-2);
  202.             }
  203.             setSelection (n);
  204.         }
  205.        
  206.         void setSelection (int n)
  207.         {
  208.             widgets[selected].isSelected = false;
  209.             selected = n;
  210.             widgets[selected].isSelected = true;
  211.         }
  212.        
  213.         void setWidgetsYMove (int speed)
  214.         {
  215.             foreach (SimpleMenuItem w in widgets)
  216.             {
  217.                 w.ySpeed = speed;
  218.                 w.yDest = (speed < 0) ? (int)w.y - space - w.surface.Height :
  219.                     (int)w.y + space + w.surface.Height;
  220.             }
  221.         }
  222.        
  223.         void setWidgetsYMove (int speed, double mult)
  224.         {
  225.             foreach (MenuItem w in widgets)
  226.             {
  227.                 w.ySpeed = speed;
  228.                 w.yDest = (speed < 0) ? (int)w.y - space - (int)(w.surface.Height * mult) :
  229.                     (int)w.y + space + (int)(w.surface.Height * mult);
  230.             }
  231.         }
  232.        
  233.         public override void move (double elapsed)
  234.         {
  235.             for (int i = 0; i < upi; i++)
  236.             {
  237.                 widgets[i].move (elapsed);
  238.             }
  239.             if (widgets[0].y > space)
  240.             {
  241.                 setWidgetsYMove (0, 0);
  242.             } if (widgets[upi - 1].y < gameHeight - widgets[0].surface.Height - space)
  243.             {
  244.                 setWidgetsYMove (0, 0);
  245.             }
  246.         }
  247.  
  248.         public override void render (Surface screen)
  249.         {
  250.             base.render (screen);
  251.         }
  252.  
  253.     }
  254. }
  255.  
  256.  
  257. // test
  258.  
  259. using System;
  260. using System.Drawing;
  261. using System.Collections.Generic;
  262. using SdlDotNet.Graphics;
  263. using SdlDotNet.Input;
  264. using SmallGameLibrary.Widgets;
  265. using SmallGameLibrary.Widgets.Menu;
  266. using SmallGameLibrary.StateMachine;
  267.  
  268. namespace Test
  269. {
  270.     public class StateMenu : State
  271.     {
  272.         object currentMenu;
  273.         object menu;
  274.        
  275.         public StateMenu (Surface _screen)
  276.         {
  277.             screen = _screen;
  278.             Dictionary<string, Widget> menus = new Dictionary<string, Widget> ();
  279.             Dictionary<string, Widget> menusA = new Dictionary<string, Widget> ();
  280.             menusA["Hello, world"] = null;
  281.             menus["A"] = new SimpleMenu (menusA, ref menu, Constants.gameWidth, Constants.gameHeight, false);
  282.             menus["B"] = null;
  283.             menus["C"] = null;
  284.             menus["D"] = null;
  285.             menus["E"] = null;
  286.             menus["F"] = null;
  287.             menus["G"] = null;
  288.             menus["H"] = null;
  289.             menus["I"] = null;
  290.             object n = null;
  291.             menu = new SimpleMenu (menus, ref n, Constants.gameWidth, Constants.gameHeight, true);
  292.             Console.WriteLine ("StateMenu 34, menu: {0}", menu);
  293.             Console.WriteLine ("StateMenu 35, menus[a].parent: {0}", ((Menu)menus["A"]).parent);
  294.             currentMenu = menu;
  295.             ((SimpleMenu)currentMenu).OnMenuEntryChosen += delegate(object sender, MenuEntryChosenArgs args) {
  296.                 currentMenu = args.pointer;
  297.             };
  298.         }
  299.        
  300.         public override void keyPressed (Key key)
  301.         {
  302.             ((SimpleMenu)currentMenu).keyInput (key);
  303.         }
  304.        
  305.         public override void logic (float elapsed)
  306.         {
  307.             ((SimpleMenu)currentMenu).move (elapsed);
  308.         }
  309.        
  310.         public override void mouseMotion (int x, int y)
  311.         {
  312.             ((SimpleMenu)currentMenu).mouseMotion (x, y);
  313.         }
  314.  
  315.         public override void mouseButtonDown (MouseButtonEventArgs args)
  316.         {
  317.             ((SimpleMenu)currentMenu).mouseButtonDown (args);
  318.         }
  319.  
  320.         public override void mouseButtonUp (MouseButtonEventArgs args)
  321.         {
  322.             ((SimpleMenu)menu).mouseButtonUp (args);
  323.         }
  324.  
  325.         public override void render ()
  326.         {
  327.             screen.Fill (Color.White);
  328.             ((SimpleMenu)currentMenu).render (screen);
  329.             screen.Update ();
  330.         }
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement