Jump to content

Rage Native UI toggle TimerBar


khorio

Recommended Posts

Is there any way to toggle a timerbar on and off without it duplicating?

public static void NewStart()
        {
            stop = false;
            SpeedoMeterMain();
        }

        public static void SpeedoMeterMain()
        {

            if(!stop)
            { 
                    Game.FrameRender += Process;
                    timerBarPool = new TimerBarPool();
                    SpeedText = new TextTimerBar("Speed", "0 km/h");
                    timerBarPool.Add(SpeedText);
                    ismade = true;
                
            }


        }

        public static void KillSpeedoMeter()
        {
            stop = true;
            timerBarPool.Remove(SpeedText);
            timerBarPool = null;
            SpeedText = null;
            
        }


        public static void Process(object sender, GraphicsEventArgs e)
        {
            if(stop) { return; }
            timerBarPool.Draw();
            SpeedText.Text = MathHelper.ConvertMetersPerSecondToKilometersPerHourRounded(Game.LocalPlayer.Character.Speed) + " km/h";


        }

I've been trying all kinds of methods but it just won't give in :p Any help is appreciated :)

Link to comment
Share on other sites

Just swicthing the stop bool true/false should stop drawing the timerbar, no need to remove SpeedText from the pool and set the timerBarPool and SpeedText to null.

When you initialize the plugin create the timerbars and pool and if you want to have method to switch the stop bool, it could be something like this:
        public static void ToggleSpeedoMeter()
        {
            stop = !stop;
        }

The "!" symbol inverts a bool expression so if stop is true it will be false an viceversa.

That should work :smile:

Link to comment
Share on other sites

still no go sadly, Now it doesn't appear at all :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rage;
using Gwen;
using RagePluginHook;
using System.Drawing;
using RAGENativeUI.Elements;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace KTools
{
    class SpeedoMeter
    {
        private static TimerBarPool timerBarPool;
        private static TextTimerBar SpeedText;
        private static bool stop;

        public static void ToggleSpeedoMeter()
        {
            stop = !stop;
            Game.LogTrivial("Toggle: " + stop.ToString());

        }

        public static void SpeedoMeterMain()
        {
                    Game.FrameRender += Process;
                    timerBarPool = new TimerBarPool();
                    SpeedText = new TextTimerBar("Speed", "0 km/h");
                    timerBarPool.Add(SpeedText);                         
            
        }


        public static void Process(object sender, GraphicsEventArgs e)
        {
            if (!stop) { return; }
            timerBarPool.Draw();
            SpeedText.Text = MathHelper.ConvertMetersPerSecondToKilometersPerHourRounded(Game.LocalPlayer.Character.Speed) + " km/h";
        }
      }
    }
[21/05/2016 00:38:55.416] Plugin "KTools" was loaded from "KTools.dll".
[21/05/2016 00:39:00.515] KTools: Toggle: True
[21/05/2016 00:39:01.717] KTools: Toggle: False
[21/05/2016 00:39:02.524] KTools: Toggle: True
[21/05/2016 00:39:03.525] KTools: Toggle: False

 

Nevermind, it would help if i added a call to SpeedoMeterMain() :p

So its the same problem all over with this, here's after toggling it 4 times (apologies for the pbrush quality :p)

It seems like it keeps adding new ones on top of it, thats why i tried to setting it to null and all that.

toggle.png

Edited by khorio
Link to comment
Share on other sites

Just now, alexguirre said:

Are you calling SpeedoMeterMain somewhere? This is what creates everything.

        public static void ToggleSpeedoMeter()
        {
            stop = !stop;
            SpeedoMeterMain();
        }

 

fixed that now :p

but still the same duplication

Link to comment
Share on other sites

25 minutes ago, khorio said:

        public static void ToggleSpeedoMeter()
        {
            stop = !stop;
            SpeedoMeterMain();
        }

 

fixed that now :p

but still the same duplication

Because each time you toggle the speedometer you create a new timer bar, SpeedoMeterMain must be called only once when the plugin is loaded in your Main method or the first time you toggle the speedo meter.

Link to comment
Share on other sites

Every time you call SpeedoMeterMain(); you attach a new event handler to Game.FrameRender, without detaching the old one, thus drawing the TimerBar multiple times in one frame. You can detach the event handler in KillSpeedoMeter();

	Game.FrameRender -= Process;
	

Link to comment
Share on other sites

On 5/20/2016 at 1:26 AM, alexguirre said:

Because each time you toggle the speedometer you create a new timer bar, SpeedoMeterMain must be called only once when the plugin is loaded in your Main method or the first time you toggle the speedo meter.

already fixed it, but thanks :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...