Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // menuitem
- using System;
- using System.Drawing;
- using SdlDotNet.Graphics;
- namespace SmallGameLibrary.Widgets.Menu
- {
- public class SimpleMenuItem : MenuItem
- {
- public string item;
- Color textColour {
- get {
- if (isSelected) {
- return Color.Gold;
- } else {
- return Color.White;
- }
- }
- }
- SdlDotNet.Graphics.Font fnt;
- public void setup (string _item, int _x, int _y, Size _size)
- {
- item = _item;
- x = _x-_size.Width/2;
- y = _y;
- this.size = _size;
- surface = new Surface (this.size);
- int size = 200;
- fnt = new SdlDotNet.Graphics.Font ("media/freesans.ttf", size);
- while (fnt.SizeText ("a").Width*item.Length >= this.size.Width ||
- fnt.SizeText ("a").Height >= this.size.Height + 10) {
- size -= 1;
- fnt.Close ();
- fnt = new SdlDotNet.Graphics.Font ("media/freesans.ttf", size);
- }
- }
- public SimpleMenuItem (string _item, int _x, int _y, Size _size, ref object _point)
- {
- point = _point;
- setup (_item, _x, _y, _size);
- }
- public SimpleMenuItem (string _item, int _x, int _y, Size _size, object _point)
- {
- point = _point;
- setup (_item, _x, _y, _size);
- }
- public override void move (double elapsed)
- {
- x += xSpeed * elapsed;
- y += ySpeed * elapsed;
- if ((ySpeed < 0 && y <= yDest) || (ySpeed > 0 && y >= yDest))
- {
- ySpeed = 0;
- yDest = (int)y;
- }
- }
- public override void render (Surface screen)
- {
- surface.Fill (Color.Black);
- Surface text = fnt.Render (item, textColour);
- surface.Blit (text, new Point (this.size.Width / 2 - text.Width / 2,
- this.size.Height / 2 - text.Height / 2));
- screen.Blit (surface, new Point ((int)x, (int)y));
- }
- }
- }
- // menu
- using System;
- using System.Drawing;
- using System.Collections.Generic;
- using SdlDotNet.Graphics;
- using SdlDotNet.Input;
- using SmallGameLibrary;
- namespace SmallGameLibrary.Widgets.Menu
- {
- public class SimpleMenu : Menu
- {
- public event menuEntryChosen OnMenuEntryChosen;
- int slowSpeed = 200;
- int fastSpeed = 850;
- int gameHeight;
- int gameWidth;
- int backi, upi, downi;
- public SimpleMenu (Dictionary<string, Widget> items, ref object _parent, int _gameWidth,
- int _gameHeight, bool isTopLevel)
- {
- widgets = new List<MenuItem> ();
- int x = _gameWidth/2;
- int y = 10;
- parent = _parent;
- space = 50;
- gameHeight = _gameHeight;
- gameWidth = _gameWidth;
- Size s = new Size(700, 100);
- foreach (string item in items.Keys)
- {
- SimpleMenuItem m = new SimpleMenuItem(item, x, y, s, items[item]);
- widgets.Add (m);
- y += m.surface.Height + space;
- }
- backi = -1;
- SimpleMenuItem up = new SimpleMenuItem ("up", gameWidth-60, 0, new Size (100, 50), null);
- widgets.Add (up);
- upi = widgets.Count -1;
- SimpleMenuItem down = new SimpleMenuItem ("down", gameWidth-60, gameHeight-50,
- new Size(100, 50), null);
- widgets.Add (down);
- downi = widgets.Count - 1;
- if (!isTopLevel) {
- Console.WriteLine ("SimpleMenu 45: {0}", parent);
- SimpleMenuItem back = new SimpleMenuItem ("back", 0, gameHeight / 2 - s.Height / 2, new Size (100, 50), ref parent);
- back.x = 0;
- widgets.Add (back);
- backi = widgets.Count - 1;
- }
- widgets[0].isSelected = true;
- }
- public override void keyInput (Key key)
- {
- if (key == Key.UpArrow)
- {
- incSelection (-1);
- } else if (key == Key.DownArrow)
- {
- incSelection (1);
- } else if (key == Key.LeftArrow && (backi != -1))
- {
- setSelection (backi);
- } else if (key == Key.Return)
- {
- if (selected < upi || (backi != -1 && selected == backi))
- {
- if (OnMenuEntryChosen != null)
- {
- OnMenuEntryChosen (this, new MenuEntryChosenArgs (ref widgets[selected].point));
- }
- }
- }
- }
- public override void mouseMotion (int x, int y)
- {
- for (int i = 0; i < widgets.Count; i++)
- {
- MenuItem w = widgets[i];
- if (UsefulFunctions.pointOverRect (x, y, (float)w.x, (float)w.y, w.surface.Width, w.surface.Height))
- {
- setSelection (i);
- }
- }
- }
- public override void mouseButtonDown (MouseButtonEventArgs args)
- {
- for (int i = 0; i < widgets.Count; i++) {
- MenuItem w = widgets[i];
- if (UsefulFunctions.pointOverRect (args.X, args.Y, (float)w.x, (float)w.y, w.surface.Width, w.surface.Height))
- {
- if (i < upi || (backi != -1 && i == backi))
- {
- OnMenuEntryChosen (this, new MenuEntryChosenArgs (ref w.point));
- } else if (i == downi)
- {
- setWidgetsYMove (-slowSpeed, 1000000);
- } else if (i == upi)
- {
- setWidgetsYMove (slowSpeed, 1000000);
- }
- }
- }
- }
- public override void mouseButtonUp (MouseButtonEventArgs args)
- {
- setWidgetsYMove (0, 0);
- }
- void incSelection (int inc)
- {
- int n = selected + inc;
- setWidgetsYMove (-slowSpeed * inc);
- if (n == -1)
- {
- n = upi - 1;
- setWidgetsYMove (-fastSpeed, widgets.Count-2);
- } else if (n == downi-1)
- {
- n = 0;
- setWidgetsYMove (fastSpeed, widgets.Count-2);
- }
- setSelection (n);
- }
- void setSelection (int n)
- {
- widgets[selected].isSelected = false;
- selected = n;
- widgets[selected].isSelected = true;
- }
- void setWidgetsYMove (int speed)
- {
- foreach (SimpleMenuItem w in widgets)
- {
- w.ySpeed = speed;
- w.yDest = (speed < 0) ? (int)w.y - space - w.surface.Height :
- (int)w.y + space + w.surface.Height;
- }
- }
- void setWidgetsYMove (int speed, double mult)
- {
- foreach (MenuItem w in widgets)
- {
- w.ySpeed = speed;
- w.yDest = (speed < 0) ? (int)w.y - space - (int)(w.surface.Height * mult) :
- (int)w.y + space + (int)(w.surface.Height * mult);
- }
- }
- public override void move (double elapsed)
- {
- for (int i = 0; i < upi; i++)
- {
- widgets[i].move (elapsed);
- }
- if (widgets[0].y > space)
- {
- setWidgetsYMove (0, 0);
- } if (widgets[upi - 1].y < gameHeight - widgets[0].surface.Height - space)
- {
- setWidgetsYMove (0, 0);
- }
- }
- public override void render (Surface screen)
- {
- base.render (screen);
- }
- }
- }
- // test
- using System;
- using System.Drawing;
- using System.Collections.Generic;
- using SdlDotNet.Graphics;
- using SdlDotNet.Input;
- using SmallGameLibrary.Widgets;
- using SmallGameLibrary.Widgets.Menu;
- using SmallGameLibrary.StateMachine;
- namespace Test
- {
- public class StateMenu : State
- {
- object currentMenu;
- object menu;
- public StateMenu (Surface _screen)
- {
- screen = _screen;
- Dictionary<string, Widget> menus = new Dictionary<string, Widget> ();
- Dictionary<string, Widget> menusA = new Dictionary<string, Widget> ();
- menusA["Hello, world"] = null;
- menus["A"] = new SimpleMenu (menusA, ref menu, Constants.gameWidth, Constants.gameHeight, false);
- menus["B"] = null;
- menus["C"] = null;
- menus["D"] = null;
- menus["E"] = null;
- menus["F"] = null;
- menus["G"] = null;
- menus["H"] = null;
- menus["I"] = null;
- object n = null;
- menu = new SimpleMenu (menus, ref n, Constants.gameWidth, Constants.gameHeight, true);
- Console.WriteLine ("StateMenu 34, menu: {0}", menu);
- Console.WriteLine ("StateMenu 35, menus[a].parent: {0}", ((Menu)menus["A"]).parent);
- currentMenu = menu;
- ((SimpleMenu)currentMenu).OnMenuEntryChosen += delegate(object sender, MenuEntryChosenArgs args) {
- currentMenu = args.pointer;
- };
- }
- public override void keyPressed (Key key)
- {
- ((SimpleMenu)currentMenu).keyInput (key);
- }
- public override void logic (float elapsed)
- {
- ((SimpleMenu)currentMenu).move (elapsed);
- }
- public override void mouseMotion (int x, int y)
- {
- ((SimpleMenu)currentMenu).mouseMotion (x, y);
- }
- public override void mouseButtonDown (MouseButtonEventArgs args)
- {
- ((SimpleMenu)currentMenu).mouseButtonDown (args);
- }
- public override void mouseButtonUp (MouseButtonEventArgs args)
- {
- ((SimpleMenu)menu).mouseButtonUp (args);
- }
- public override void render ()
- {
- screen.Fill (Color.White);
- ((SimpleMenu)currentMenu).render (screen);
- screen.Update ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement