Handling Button (widget) clicks

Started by Garwel, July 23, 2020, 03:30:05 AM

Previous topic - Next topic

Garwel

I have what looks like a basic question: how do you process a click on a button (the Widgets.Button or Listing_Standard.Button)? Most APIs have something like an onClick event that you provide when creating the button, but not RimWorld.

In other words, let's say I have this code:

class MyTabWindow : MainTabWindow
{
  void DoSomething()
  { ... }

  public override void DoWindowContents(Rect inRect)
  {
    Widgets.ButtonText(inRect, "Click me");
  }
}


How, do I make it so that when the user clicks the "Click me" button, DoSomething() is called?

LWM

Look at any of the Dialog_.... classes in RimWorld (you've decompiled the code,yes?)

It's all

if (Widgets.ButtonText()) {...do soemthing }


Go look at the Dialog_....windows anyway - it's a useful way to see what you can do easily.

PS - if you want to do fancy things or highlighting, you probably don't want listing_standards.

Garwel

I also figured it out. Here is the code:

class MyTabWindow : MainTabWindow
{
  void DoSomething()
  { ... }

  public override void DoWindowContents(Rect inRect)
  {
    if (Widgets.ButtonText(inRect, "Click me"))
      DoSomething();

  }
}

Garwel

Quote from: LWM on July 23, 2020, 11:52:01 AM
Look at any of the Dialog_.... classes in RimWorld (you've decompiled the code,yes?)

It's all

if (Widgets.ButtonText()) {...do soemthing }


Go look at the Dialog_....windows anyway - it's a useful way to see what you can do easily.

PS - if you want to do fancy things or highlighting, you probably don't want listing_standards.
Thanks, you ninja'd me.

LWM

Hehe :)  Just trying to be helpful!