• Advertisement
Sign in to follow this  

Seperate GUi from GameLogic

This topic is 527 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
Advertisement

You simply run in the spaghetti trap. GUI should never hold nor change data on its own rather than simply trigger stuff and fire events that is what MVC is on so you should change your general design

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

Hey guys, thx for answer!

 

I created my own Databinding class (similar to WPF) and now i can bind two variables with reflection, OneWay and TwoWay.

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
Sign in to follow this  

  • Advertisement