• Home
  • Products
    • Game Engine
      • Base
      • Mathematics
      • Geometry
      • Physics
      • Particles
      • Animation
      • Graphics
      • Game
      • Game UI
    • Windows Forms
      • Docking Windows
      • Text Editor Control
  • Downloads
  • Buy
    • Overview
    • Professional
    • Indie
    • Non-Commercial
  • Support
    • Overview
    • Blog
    • Forum
    • License FAQ
    • Documentation
  • About
    • About Us
      • Services
    • Contact Us
    • Press
    • Legal Terms
      • Imprint (English)
      • Imprint (German)
Select the search type
 
  • Site
  • Web
Search
DigitalRune.com
Login |Register
NEWS News RSS Feed BLOG Blog RSS Feed FORUM News RSS Feed DOCUMENTATION DigitalRune Software on YouTube DigitalRune Software on Twitter
You are here: SupportForum

If you want to contribute to the forum discussions, please Register or Login.

SearchHome
  • 1
  • 2
  • 3
  • 4
  • 5
HomeHomeDigitalRune Sof...DigitalRune Sof...Game EngineGame EngineGame.UI: MessageBoxGame.UI: MessageBox
Previous
 
Next
New Post
10/26/2011 1:45 AM
 
jkachhad
No Ranking

Joined: 8/14/2011
Posts: 14
Game.UI: MessageBox  Modified By jkachhad  on 10/27/2011 10:14:58 PM

Needed a MessageBox type control, so implemented one. Thought I would post it here to see if anyone can make improvements.

using System;
 
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
 
using DigitalRune;
using DigitalRune.Game.UI;
using DigitalRune.Mathematics.Algebra;
 
namespace DigitalRune.Game.UI.Controls
{
    public enum MessageBoxButton
    {
        OK = 0,
        OKCancel,
        YesNo,
        YesNoCancel,
    }
 
    public enum MessageBoxResult
    {
        OK,
        Cancel,
        Yes,
        No
    }
 
    public class ResponseEventArgs : EventArgs
    {
        private MessageBoxResult _result;
 
        public MessageBoxResult Result
        {
            get { return _result; }
        }
 
        public ResponseEventArgs(MessageBoxResult type)
        {
            _result = type;
        }
    }
 
    public class MessageBox : Window
    {
        private static int MaximumWidth = 300;
 
        #region Button entries
        private class MessageBoxButtonEntry
        {
            private string _text;
            private MessageBoxResult _result;
 
            public string Text
            {
                get { return _text; }
            }
 
            public MessageBoxResult Result
            {
                get { return _result; }
            }
 
            public MessageBoxButtonEntry(string text, MessageBoxResult result)
            {
                _text = text;
                _result = result;
            }
        }
 
        private static MessageBoxButtonEntry[][] _entries = new MessageBoxButtonEntry[][]
        {
            new MessageBoxButtonEntry[] {   new MessageBoxButtonEntry("Okay", MessageBoxResult.OK), },
            new MessageBoxButtonEntry[] {   new MessageBoxButtonEntry("Okay", MessageBoxResult.OK),
                                            new MessageBoxButtonEntry("Cancel", MessageBoxResult.Cancel), },
            new MessageBoxButtonEntry[] {   new MessageBoxButtonEntry("Yes", MessageBoxResult.Yes),
                                            new MessageBoxButtonEntry("No", MessageBoxResult.No), },
            new MessageBoxButtonEntry[] {   new MessageBoxButtonEntry("Yes", MessageBoxResult.Yes),
                                            new MessageBoxButtonEntry("No", MessageBoxResult.No),
                                            new MessageBoxButtonEntry("Cancel", MessageBoxResult.Cancel), },
        };
        #endregion
 
        public event EventHandler Response;
 
        public MessageBox(string message, string title, MessageBoxButton button) : this(message, title, button, null)
        {
        }
 
        public MessageBox(string message, string title, MessageBoxButton button, Texture2D icon)
        {
            Title = title;
 
            CanDrag = false;
 
            IsModal = true;
 
            CloseButtonStyle = null;
 
            HorizontalAlignment = HorizontalAlignment.Center;
            VerticalAlignment = VerticalAlignment.Center;
 
            var mainPanel = new StackPanel
            {
                Margin = new Vector4F(12, 12, 12, 12),
            };
 
            var messagePanel = new StackPanel
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Center,
            };
 
            var buttonPanel = new StackPanel
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Center,
 
                Margin = new Vector4F(0, 5, 0, 0),
            };
 
            #region Icon & Message
            var messageBlock = new TextBlock
            {
                Text = message,
                Margin = new Vector4F(4),
                HorizontalAlignment = HorizontalAlignment.Center,
 
                MaxWidth = MessageBox.MaximumWidth,
                WrapText = true,
 
                Foreground = Color.White,
            };
 
            if (icon != null)
            {
                var messageIcon = new Image
                {
                    Width = 32,
                    Height = 32,
 
                    Margin = new Vector4F(0, 0, 4, 0),
 
                    VerticalAlignment = VerticalAlignment.Top,
                    HorizontalAlignment = HorizontalAlignment.Center,
 
                    Texture = icon,
                };
 
                messagePanel.Children.Add(messageIcon);
            }
 
            messagePanel.Children.Add(messageBlock);
            #endregion
 
            #region Buttons
            foreach (MessageBoxButtonEntry entry in _entries[(int)button])
            {
                var entryButton = new Button()
                {
                    Content = new TextBlock { Text = entry.Text },
                    Tag = entry.Result,
 
                    Margin = new Vector4F(4, 0, 4, 0),
                    Padding = new Vector4F(8, 5, 8, 5),
 
                    HorizontalAlignment = HorizontalAlignment.Center,
                };
                entryButton.Click += InvokeResponse;
 
                buttonPanel.Children.Add(entryButton);
            }
            #endregion
 
            mainPanel.Children.Add(messagePanel);
            mainPanel.Children.Add(buttonPanel);
 
            Content = mainPanel;
        }
 
        public void InvokeResponse(object sender, EventArgs e)
        {
            var button = sender as Button;
 
            if (button == null)
                return;
 
            Close();
 
            var args = new ResponseEventArgs((MessageBoxResult)button.Tag);
 
            if (Response != null)
                Response(this, args);
        }
    }
}

To create & show:

var messageBox = new MessageBox("Disconnected", "Error", MessageBoxButton.OKCancel);
 
messageBox.Response += (s, e) =>
{
    if (e.Result == MessageBoxResult.OK)
        _loginWindow.IsVisible = true;
};
 
messageBox.Show(_screen);

Feel free to modify as you wish.

 
New Post
10/27/2011 9:27 AM
 
HelmutG
6th Level Poster

www.digitalrune.com
Joined: 10/15/2006
Posts: 565
Re: Game.UI: MessageBox 

Nice.

One comment about the code: Measure() is called several times. Was this only for debugging the sizes? Normally, it should not be necessary to call Measure() manually.

Thanks for sharing.

 
New Post
11/1/2011 11:58 PM
 
jkachhad
No Ranking

Joined: 8/14/2011
Posts: 14
Re: Game.UI: MessageBox 

I've removed the lines you mentioned.

I have a issue regarding position, how can I get it show up on center of Screen? I'm doing Horizontal and Vertical alignment to Center, but when dragging the object gets moved as if it was set to Left, Top. I've tried playing around with various options, but I can't seem to get any sort of Size before the object is rendered.

 
New Post
11/2/2011 7:53 AM
 
HelmutG
6th Level Poster

www.digitalrune.com
Joined: 10/15/2006
Posts: 565
Re: Game.UI: MessageBox 
The window dragging code works only for left/top alignment. If you need the window size for manual positioning, then calling Measure() is the right thing to do.
 
 Page 1 of 1
Previous
 
Next
HomeHomeDigitalRune Sof...DigitalRune Sof...Game EngineGame EngineGame.UI: MessageBoxGame.UI: MessageBox


DigitalRune is a trademark of Garstenauer Information Technology OG.

Garstenauer Information Technology OG
Weingartenstrasse 35, 4452 Ternberg
Austria (EUROPE)
office@digitalrune.com

Home Products Downloads Buy Support About Us
Game Engine Particles Windows Forms Professional Blog Services
Base Animation Docking Windows Indie Forum Contact Us
Mathematics Graphics Text Editor Control Non-Commercial License FAQ Press (News)
Geometry Game Documentation Legal Terms
Physics Game UI Imprint
Impressum
Copyright © 2006-2012 Garstenauer Information Technology OG Terms Of UsePrivacy Statement