(Spout) Getting the center of the screen

Discussion in 'Plugin Development' started by stelar7, Oct 11, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    stelar7

    @Afforess
    How would i get the center of the screen?
    I've tried many combinations, but i just cant find the right one ...

    Code:java
    1. PopupScreen popup = new GenericPopup();
    2. Button button = (Button) new GenericButton("Button").setWidth(200).setHeight(20);
    3. popup.attachWidget(plugin, button);
    4. if (sender instanceof SpoutPlayer) {
    5. SpoutPlayer sp = (SpoutPlayer) sender;
    6. //this is the part in question!
    7. button.setX(sp.getMainScreen().getWidth() / 2).setY(sp.getMainScreen().getHeight() / 2);
    8. sp.getMainScreen().attachPopupScreen(popup);
     
  2. Offline

    Afforess

    the getMainScreen().getWidth()/2 will return the center...BUT remember your button is right justified, so the center of the button is not at the center of the screen. You need to correct for that.

    Try this:

    button.setX(sp.getMainScreen().getWidth() / 2 - button.getWidth() / 2).setY(sp.getMainScreen().getHeight() / 2 - button.getHeight() / 2);
     
  3. Offline

    stelar7

    @Afforess
    is there a way to get the button involved in a ButtonClickEvent?

    Code:
    public void onButtonClick(ButtonClickEvent event) {
    	if (event.getButton().equals(sometingh?)) {
    
            }
    }
     
  4. Offline

    Afforess

  5. Offline

    stelar7

  6. Offline

    CodeFreakC

    I always found the little piece of code Afforess works for me. Also, you need to have some if statements in your onButtonClick functions. A really simple way of doing this is to ensure that no button has the same name as any other button and check the button text. Obviously this is a really primitive and inefficient way, but it works :)
     
Thread Status:
Not open for further replies.

Share This Page