Jump to content
  • 0

These awful buttons


Rales99

Question

 

I am trying to add a registration button to the login window


1. I finished 2 private readonly

 

Spoiler

 

            private readonly Button mRegisterButton;

            private readonly RegisterWindow mRegisterWindow;

 



2. After i added

 

Spoiler

            mRegisterButton = new Button(mLoginWindow, "RegisterButton");
            mRegisterButton.SetText(Strings.MainMenu.register);
            mRegisterButton.Clicked += RegisterButton_Clicked;

 

3. And after I added void

 

Spoiler

        void RegisterButton_Clicked(Base sender, ClickedEventArgs arguments)
        {
            Hide();
            mRegisterWindow.Show();
        }




 

When you click on the "register" button, the client closes. error logs:

 

Spoiler

 

2020-04-10 13:41:46.550 [Error] 
  Message: Ссылка на объект не указывает на экземпляр объекта.
    Stack:    в Intersect.Client.Interface.Menu.LoginWindow.RegisterButton_Clicked(Base sender, ClickedEventArgs arguments) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client\Interface\Menu\LoginWindow.cs:строка 172
   в Intersect.Client.Framework.Gwen.Control.Base.OnMouseClickedLeft(Int32 x, Int32 y, Boolean down, Boolean automated) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Control\Base.cs:строка 1999
   в Intersect.Client.Framework.Gwen.Control.Button.OnClicked(Int32 x, Int32 y) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Control\Button.cs:строка 374
   в Intersect.Client.Framework.Gwen.Control.Button.OnMouseClickedLeft(Int32 x, Int32 y, Boolean down, Boolean automated) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Control\Button.cs:строка 343
   в Intersect.Client.Framework.Gwen.Control.Base.InputMouseClickedLeft(Int32 x, Int32 y, Boolean down, Boolean automated) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Control\Base.cs:строка 2008
   в Intersect.Client.Framework.Gwen.Input.InputHandler.OnMouseClicked(Base canvas, Int32 mouseButton, Boolean down) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Input\InputHandler.cs:строка 373
   в Intersect.Client.Framework.Gwen.Control.Canvas.Input_MouseButton(Int32 button, Boolean down) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Control\Canvas.cs:строка 292
   в Intersect.Client.Framework.Gwen.Input.IntersectInput.ProcessMessage(Object message) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client.Framework\Gwen\Input\IntersectInput.cs:строка 115
   в Intersect.Client.MonoGame.Input.MonoInput.CheckMouseButton(ButtonState bs, MouseButtons mb) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client\MonoGame\Input\MonoInput.cs:строка 154
   в Intersect.Client.MonoGame.Input.MonoInput.Update() в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client\MonoGame\Input\MonoInput.cs:строка 181
   в Intersect.Client.Core.Main.Update() в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client\Core\Main.cs:строка 109
   в Intersect.Client.MonoGame.IntersectGame.Update(GameTime gameTime) в C:\Users\olegr\OneDrive\Документы\GitHub\Intersect-Engine\Intersect.Client\MonoGame\IntersectGame.cs:строка 119
   в Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime)
   в Microsoft.Xna.Framework.Game.Tick()
   в Microsoft.Xna.Framework.SdlGamePlatform.RunLoop()
   в Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)


--------------------------------------------------------------------------------

 


Полный код LoginWindow


 

Spoiler

 

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

using Intersect.Client.Core;
using Intersect.Client.Framework.File_Management;
using Intersect.Client.Framework.Gwen.Control;
using Intersect.Client.Framework.Gwen.Control.EventArguments;
using Intersect.Client.Framework.Input;
using Intersect.Client.General;
using Intersect.Client.Interface.Game.Chat;
using Intersect.Client.Localization;
using Intersect.Client.Networking;
using Intersect.Utilities;

namespace Intersect.Client.Interface.Menu
{

    public class LoginWindow
    {

        private Button mBackBtn;

        private Button mForgotPassswordButton;

        private Button mLoginBtn;

        private Label mLoginHeader;

        //Controls
        private ImagePanel mLoginWindow;

        //Parent
        private MainMenu mMainMenu;

        private ImagePanel mPasswordBackground;

        private Label mPasswordLabel;

        private TextBoxPassword mPasswordTextbox;

        private string mSavedPass = "";

        private LabeledCheckBox mSavePassChk;

        //Кнопка регистрации 
        private readonly Button mRegisterButton;

        private readonly RegisterWindow mRegisterWindow;

        //Controls
        private ImagePanel mUsernameBackground;

        private Label mUsernameLabel;

        private TextBox mUsernameTextbox;

        private bool mUseSavedPass;

        //Init
        public LoginWindow(Canvas parent, MainMenu mainMenu, ImagePanel parentPanel)
        {
            //Assign References
            mMainMenu = mainMenu;

            //Main Menu Window
            mLoginWindow = new ImagePanel(parent, "LoginWindow");

            //Menu Header
            mLoginHeader = new Label(mLoginWindow, "LoginHeader");
            mLoginHeader.SetText(Strings.Login.title);

            mUsernameBackground = new ImagePanel(mLoginWindow, "UsernamePanel");

            //Login Username Label
            mUsernameLabel = new Label(mUsernameBackground, "UsernameLabel");
            mUsernameLabel.SetText(Strings.Login.username);

            //Login Username Textbox
            mUsernameTextbox = new TextBox(mUsernameBackground, "UsernameField");
            mUsernameTextbox.SubmitPressed += UsernameTextbox_SubmitPressed;
            mUsernameTextbox.Clicked += _usernameTextbox_Clicked;

            mPasswordBackground = new ImagePanel(mLoginWindow, "PasswordPanel");

            //Login Password Label
            mPasswordLabel = new Label(mPasswordBackground, "PasswordLabel");
            mPasswordLabel.SetText(Strings.Login.password);

            //Login Password Textbox
            mPasswordTextbox = new TextBoxPassword(mPasswordBackground, "PasswordField");
            mPasswordTextbox.SubmitPressed += PasswordTextbox_SubmitPressed;
            mPasswordTextbox.TextChanged += _passwordTextbox_TextChanged;

            //Login Save Pass Checkbox
            mSavePassChk = new LabeledCheckBox(mLoginWindow, "SavePassCheckbox") {Text = Strings.Login.savepass};

            //Forgot Password Button
            mForgotPassswordButton = new Button(mLoginWindow, "ForgotPasswordButton");
            mForgotPassswordButton.IsHidden = true;
            mForgotPassswordButton.SetText(Strings.Login.forgot);
            mForgotPassswordButton.Clicked += mForgotPassswordButton_Clicked;

            //Login - Send Login Button
            mLoginBtn = new Button(mLoginWindow, "LoginButton");
            mLoginBtn.SetText(Strings.Login.login);
            mLoginBtn.Clicked += LoginBtn_Clicked;

            //Register Button
            mRegisterButton = new Button(mLoginWindow, "RegisterButton");
            mRegisterButton.SetText(Strings.MainMenu.register);
            mRegisterButton.Clicked += RegisterButton_Clicked;
            
            //Login - Back Button
            mBackBtn = new Button(mLoginWindow, "BackButton");
            mBackBtn.SetText(Strings.Login.back);
            mBackBtn.Clicked += BackBtn_Clicked;

            LoadCredentials();

            mLoginWindow.LoadJsonUi(GameContentManager.UI.Menu, Graphics.Renderer.GetResolutionString());

            //Hide Forgot Password Button if not supported by server
        }

        public bool IsHidden => mLoginWindow.IsHidden;

        private void mForgotPassswordButton_Clicked(Base sender, ClickedEventArgs arguments)
        {
            Interface.MenuUi.MainMenu.NotifyOpenForgotPassword();
        }

        private void _usernameTextbox_Clicked(Base sender, ClickedEventArgs arguments)
        {
            Globals.InputManager.OpenKeyboard(
                GameInput.KeyboardType.Normal, mUsernameTextbox.Text, false, false, false
            );
        }

        //Methods
        public void Update()
        {
            if (Networking.Network.Connected)
            {
                return;
            }

            Hide();
            mMainMenu.Show();
            Interface.MsgboxErrors.Add(new KeyValuePair<string, string>("", Strings.Errors.lostconnection));
        }

        public void Hide()
        {
            mLoginWindow.IsHidden = true;
        }

        public void Show()
        {
            mLoginWindow.IsHidden = false;
            if (!mForgotPassswordButton.IsHidden)
            {
                mForgotPassswordButton.IsHidden = !Options.Instance.SmtpValid;
            }
        }

        //Input Handlers
        void _passwordTextbox_TextChanged(Base sender, EventArgs arguments)
        {
            mUseSavedPass = false;
        }

        void BackBtn_Clicked(Base sender, ClickedEventArgs arguments)
        {
            Hide();
            mMainMenu.Show();
        }

        void UsernameTextbox_SubmitPressed(Base sender, EventArgs arguments)
        {
            TryLogin();
        }

        void PasswordTextbox_SubmitPressed(Base sender, EventArgs arguments)
        {
            TryLogin();
        }

        void RegisterButton_Clicked(Base sender, ClickedEventArgs arguments)
        {
            Hide();
            mRegisterWindow.Show();
        }

        void LoginBtn_Clicked(Base sender, ClickedEventArgs arguments)
        {
            TryLogin();
        }

        public void TryLogin()
        {
            if (Globals.WaitingOnServer)
            {
                return;
            }

            if (!Networking.Network.Connected)
            {
                Interface.MsgboxErrors.Add(new KeyValuePair<string, string>("", Strings.Errors.notconnected));

                return;
            }

            if (!FieldChecking.IsValidUsername(mUsernameTextbox?.Text, Strings.Regex.username))
            {
                Interface.MsgboxErrors.Add(new KeyValuePair<string, string>("", Strings.Errors.usernameinvalid));

                return;
            }

            if (!FieldChecking.IsValidPassword(mPasswordTextbox?.Text, Strings.Regex.password))
            {
                if (!mUseSavedPass)
                {
                    Interface.MsgboxErrors.Add(new KeyValuePair<string, string>("", Strings.Errors.passwordinvalid));

                    return;
                }
            }

            var password = mSavedPass;
            if (!mUseSavedPass)
            {
                password = ComputePasswordHash(mPasswordTextbox?.Text?.Trim());
            }

            PacketSender.SendLogin(mUsernameTextbox?.Text, password);
            SaveCredentials();
            Globals.WaitingOnServer = true;
            ChatboxMsg.ClearMessages();
        }

        private void LoadCredentials()
        {
            var name = Globals.Database.LoadPreference("Username");
            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            mUsernameTextbox.Text = name;
            var pass = Globals.Database.LoadPreference("Password");
            if (string.IsNullOrEmpty(pass))
            {
                return;
            }

            mPasswordTextbox.Text = "****************";
            mSavedPass = pass;
            mUseSavedPass = true;
            mSavePassChk.IsChecked = true;
        }

        private static string ComputePasswordHash(string password)
        {
            using (var sha = new SHA256Managed())
            {
                return BitConverter.ToString(sha.ComputeHash(Encoding.UTF8.GetBytes(password ?? ""))).Replace("-", "");
            }
        }

        private void SaveCredentials()
        {
            var username = "";
            var password = "";

            if (mSavePassChk.IsChecked)
            {
                username = mUsernameTextbox?.Text?.Trim();
                password = mUseSavedPass ? mSavedPass : ComputePasswordHash(mPasswordTextbox?.Text?.Trim());
            }

            Globals.Database.SavePreference("Username", username);
            Globals.Database.SavePreference("Password", password);
        }

    }

}

 

 

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

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
×
×
  • Create New...