Repos

MELHARFI-2D-Game-Engine.

Got to repo.

11. Controls and ControlLayer

Controls

To use a controls like button, panel or whatever control you already experianced, there is no built-in.

The reason wht is that most of the time i have seen some ugly control in some game engine, and event controls are really limited with a bounch of errors.

This is the main reasion why i made that engine is that because you can simply use the Windows controls itself, i mean all of them just like if you use it in your desktop application.

But the best way is add control programatically not by drag and drop, yet nothing stop you to do so.

                        
using System;
using System.Drawing;
using System.Windows.Forms;
using MELHARFI.Manager;
using MELHARFI.Manager.Gfx;

namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            TextBox name = new TextBox();
            name.Name = "name";
            name.Size = new Size(100, 20);
            name.Location = new Point(10, 10);
            name.TabIndex = 1;
            name.Focus();
            name.BackColor = Color.DarkSlateGray;
            name.ForeColor = Color.White;
            this.Controls.Add(name);

            Manager manager = new Manager(this, "manager");
            manager.Background = Color.Red;
        }
    }
}
                          
                        
                      

There is one issue using controls in the context of 2d gaming behavior that we should take care.

Supposing in one moment you want to clean the screen using Clear() "that we will see in the next chapter", one of the behavior when you want to clean a screen is when you want to change a map, or change the scenario of the game or whatever

In that moment the Clear methode will not remove the controls, because they are not part of your game, they are just a control added to your form, and then not managed by the engine itself.

So we need to tell to the engine that those controls are a part of the enine and here it cam the ControlLayer.

  • ControlLayer
  • It's a layer just like Background, Object or Top layers but that one it support only controls.

                            
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using MELHARFI.Manager;
    using MELHARFI.Manager.Gfx;
    
    namespace _2dProject
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                TextBox name = new TextBox();
                name.Name = "name";
                name.Size = new Size(100, 20);
                name.Location = new Point(10, 10);
                name.TabIndex = 1;
                name.Focus();
                name.BackColor = Color.DarkSlateGray;
                name.ForeColor = Color.White;
                this.Controls.Add(name);
    
                Manager manager = new Manager(this, "manager");
                manager.Background = Color.Red;
    
                manager.ControlLayer.Add(name);
                manager.Clear();
            }
        }
    }
                            
                          

    The code behavior will not show the control after clear() method because it was referenced to our engine.

    If somehow you want to keep showing the controls event when you can Clear method and when controls are referenced, you can makr them as Fixed object using FixedObjectLayer that we gonna see next chapter

    Services

    App Developpement

    Want me to dev an app for you ? dont hesitate to contact me.

    Programming

    Are you looking for a coder/teammate for your project ? Let's give it a try.

    Job Invitation

    Have a proposal for me ? we can discuss about it.

    Donation maybe ?!

    You want to buy me a coffe ? m.elharfi@gmail.com