nifty-gui / nifty-gui

Your Open Source Java OpenGL GUI. Interactive user interfaces for games or similar applications. LWJGL, JOGL, JME or Java2d rendering.
http://nifty-gui.github.io/nifty-gui/
Other
245 stars 78 forks source link

Cannot have an onclick on listbox #338

Open harlock76 opened 9 years ago

harlock76 commented 9 years ago

Hello,

I spent hours trying to have this f... listbox do a simple item click

can someone look a this code ?

<!-- MARKET ITEM LIST ITEM - - - - - - - - - - - - - - - - - - - - - - - - - - -->  
    <controlDefinition name="marketItemListItem" >
      <interact onClick="listBoxItemClicked()" />
      <effect>
        <onCustom customKey="select" name="colorBar" post="false" color="#F00F" neverStopRendering="true" timeType="infinite" />
        <onCustom customKey="select" name="textColor" post="false" color="#0F0F" neverStopRendering="true" timeType="infinite" />
      </effect>  
      <panel id="#marketItemListItem" childLayout="horizontal" width="*" height="66px" backgroundColor="#0000" >
          <control name="marketItem" itemType="$itemType" amount="$amount" buyer="$buyer" credits="$credits" imageFileName="$imageFileName" />
      </panel>
    </controlDefinition>     

    <screen id="Hud" controller="mygame.appstates.HudAppState" >
...   
         <layer id="marketLayer" childLayout="center" backgroundColor="#0000">
            <panel id="marketPanel" width="500" height="400" childLayout="vertical">
                <control id="marketList" name="mylistbox" style="my-listbox" width="500" height="400" vertical="on" horizontal="off" displayItems="4" selection="Single" forceSelection="true" viewConverterClass="mygame.gui.converters.MarketItemListConverter" >
                   <control name="marketItemListItem" /> 
                </control>
            </panel>
        </layer>
   ...

   @NiftyEventSubscriber(id="marketList")
    public void onListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<MarketItemListItem> event) 
    { 
        List<MarketItemListItem> selection = event.getSelection();
// only come here at startup
    }

    public void listBoxItemClicked()
    {
// never comes here
    }
harlock76 commented 9 years ago

I cant update the code to display correctly I'll try on jmonkey forum jesus fucking christ

void256 commented 9 years ago

I don't think you can directly add onClick to ListBox items this way. Internally the ListBox will use the controlDefinition for the item only as a template for the actual items it displays. The reason for that is, that the ListBox only allocates (internal) elements for the number of visible items in the ListBox - and not the actual entries.

Why do you need an onClick? The ListBox "should" handle that internally and report the change of the selection back to you without you having to worry about the actual onClick event.

I suppose this is not working in your example, right? Can you confirm that?

PS: I've fixed your original post to display the code correctly ...