Sign in to follow this  

Seperate GUi from GameLogic

This topic is 396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, 

 

im in trouble right now, because i stuck on a ( maybe ) a simple problem?!

 

 

So i have a GuiClass.cs with in this class i create my Button/Textbox, all the GUI stuff.

 

So i trigger the button/textbox with a event like in WinForms. But i need to exchange Data tgrough different Classes without a reference.

 

 

For case2 i create Singleton Class with a Dictionary<string, Action>, now in the OhterClass i can register a event like:
 

EventManager.Instance.AddAction("test", () => a = 3);

and in the GuiClass i can trigger this event like:
 

button.OnClick += () => { EventManager.Instance.TriggerEvent("test"); };

so i solved case2.

 

But now when i change  the variable in the OtherClass, how can i  transfer the new value back to the GUI-Class? I can create a second event in the EventManager, but is there a better way to do this? - case 1

 

And in general, is my approach a goad Idea? How do you solve to seperate GUI/Gamescreen,...

 

guii.png

 

I dont use WPF/Winform i wrote my own Textbox/Button rendered with Direct3D. 

 

hope you can help me :(

Edited by montify

Share this post


Link to post
Share on other sites

Usually there will be a single 'UpdateGUI' event - whenever you change a value, fire the UpdateGUI event, and the GUI receives that and knows it needs to read the latest values from the game and update them in the display. It's typically okay for the whole GUI to update in those circumstances, even though only 1 thing has changed, because the update is a very simple operation. (This ceases to be true with complex listviews and the like - you might want something a bit more targeted for that. You could add hints into the event regarding what needs to update, or have UI elements 'subscribe' to specific data sources. But worry about that later...)

Share this post


Link to post
Share on other sites

I think that is a good approach, if you're happy with your game data being somewhat coupled to the UI code. I don't like the data binding approach because it means you start needing to implement things like INotifyPropertyChanged or using ObservableCollection<T> containers, etc. But it can be more efficient that way.

Share this post


Link to post
Share on other sites

I don't know if this is the right way to do Binding, but:

 

i have 1 Interface IBinding.cs:

  public interface  IBinding
    {
        event Action OnChanged;
    }

Here is my DataBinding.cs

    public enum BindinType
    {
        OneWay,
        TwoWay
    }

    public class DataBinding
    {

        public DataBinding(TextBox source, IBinding desination, string property, BindinType bindinType)
        {

            var myType = desination.GetType();
            var p = myType.GetProperty(property);

            if (bindinType == BindinType.OneWay)
            {
                source.OnChanged += () =>
                {
                    string tmp = source.text;
                    p.SetValue(desination, float.Parse(tmp), null);
                };
            }


            if (bindinType == BindinType.TwoWay)
            {
                source.OnChanged += () =>
                {
                    var tmp = float.Parse(source.text);
                    p.SetValue(desination, tmp, null);
                };

                desination.OnChanged += () =>
                {
                    source.text = p.GetValue(desination, null).ToString();
                };
            }
        }

    }

now i can do like this:

new DataBinding(textBox, gameScreen, "a property value", BindinType.TwoWay);

As a said, i dont know if this is common or not, i tried it by myself to "recreate" Microsofts Binding in a easy way...

Share this post


Link to post
Share on other sites

This topic is 396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this