Listbox with Clickable Text
Posted: Wed Nov 02, 2005 14:36
This would be useful in a chat window where links to items could be displayed. When clicking on a link a window would pop up detailing the item. Here's how I think it could be implemented and before I start coding, I'd like comments on whether I'm on the right track or completely off.
First the tools available:
ListboxItem * CEGUI::Listbox::getItemAtPoint ( const Point & pt ) const
Return the ListboxItem under the given window local pixel co-ordinate.
const String& CEGUI::ListboxItem::getText ( void )
return the text string set for this list box item.
const Font * ListboxTextItem::getFont (void) const
Return a pointer to the font being used by this ListboxTextItem.
size_t CEGUI::Font::getCharAtPixel ( const String & text, float pixel, float x_scale = 1.0f )
Return the index of the closest text character in String text that corresponds to pixel location pixel if the text were rendered.
void * d_itemData
Pointer to some client code data. This has no meaning within the GUI system.
Text links require two features. The first is to change the mouse cursor when hovering a link and the second feature is to trigger an event when clicking the mouse button over a link.
getItemAtPoint() would return the listbox item under the cursor. From that listbox item are obtained the text with getText() and the font with getFont(). Finally getCharAtPixel() would use the x coordinate of the mouse to find the character position under the mouse. From that position I can find the word and determine if it is part of a link. This link information would be stored in d_itemData. As an optimization step this process would only be started if d_itemData contained a flag that a link does indeed exist.
If getFont() does not contain the coordinates of the text then I would need to transform the mouse world coordinate into local coordinates (local to the region where the text of the listbox item is rendered).
This functionality would be packaged into a function that would return the ID of the link. But a better approach would be to return a class. Only returning an ID requires that every ID is unique. It also means that should we want to open different dialogs (when clicking on the link) we'd have to test the link (lots of IFs or a big switch) and open the appropriate dialog (ie IDs 1-1000 are for armor and 1001-2000 are for weapons). If instead a class was returned then that class would call up the appropriate dialog through a function like OnLinkClicked().
This first implementation would only work for listbox items. But hopefully it could be implemented in a more general manner such that any text can be clickable.
So, is this plausible or completely wacky?
P.S. I know that I have not talked about displaying the link as underlined text.
First the tools available:
ListboxItem * CEGUI::Listbox::getItemAtPoint ( const Point & pt ) const
Return the ListboxItem under the given window local pixel co-ordinate.
const String& CEGUI::ListboxItem::getText ( void )
return the text string set for this list box item.
const Font * ListboxTextItem::getFont (void) const
Return a pointer to the font being used by this ListboxTextItem.
size_t CEGUI::Font::getCharAtPixel ( const String & text, float pixel, float x_scale = 1.0f )
Return the index of the closest text character in String text that corresponds to pixel location pixel if the text were rendered.
void * d_itemData
Pointer to some client code data. This has no meaning within the GUI system.
Text links require two features. The first is to change the mouse cursor when hovering a link and the second feature is to trigger an event when clicking the mouse button over a link.
getItemAtPoint() would return the listbox item under the cursor. From that listbox item are obtained the text with getText() and the font with getFont(). Finally getCharAtPixel() would use the x coordinate of the mouse to find the character position under the mouse. From that position I can find the word and determine if it is part of a link. This link information would be stored in d_itemData. As an optimization step this process would only be started if d_itemData contained a flag that a link does indeed exist.
If getFont() does not contain the coordinates of the text then I would need to transform the mouse world coordinate into local coordinates (local to the region where the text of the listbox item is rendered).
This functionality would be packaged into a function that would return the ID of the link. But a better approach would be to return a class. Only returning an ID requires that every ID is unique. It also means that should we want to open different dialogs (when clicking on the link) we'd have to test the link (lots of IFs or a big switch) and open the appropriate dialog (ie IDs 1-1000 are for armor and 1001-2000 are for weapons). If instead a class was returned then that class would call up the appropriate dialog through a function like OnLinkClicked().
This first implementation would only work for listbox items. But hopefully it could be implemented in a more general manner such that any text can be clickable.
So, is this plausible or completely wacky?
P.S. I know that I have not talked about displaying the link as underlined text.