Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for drawable in SelectBox #7340

Open
3 of 6 tasks
wangdong20 opened this issue Feb 12, 2024 · 4 comments
Open
3 of 6 tasks

Support for drawable in SelectBox #7340

wangdong20 opened this issue Feb 12, 2024 · 4 comments

Comments

@wangdong20
Copy link

wangdong20 commented Feb 12, 2024

Issue details

Whe I use SelectBox, I see SelectBox support generic type which make me surprise that I can use some drawables or images in SelectBox. However when I dig it deeply, I found SelectBox only support textual elements, any other class from generic type will be converted to String in List class. I want to implement a SelectBox which can select different color NinePatchDrawable, but SelectBox can't make it. Maybe there is no need to support generic type in SelectBox, because SelectBox can only show textual elements. Any thoughts about add more support for SelectBox since it already support Generic type.

Reproduction steps/code

Version of libGDX and/or relevant dependencies

1.12.0

Stacktrace

//Please provide the stacktrace if applicable 

Please select the affected platforms

  • Android
  • iOS
  • HTML/GWT
  • Windows
  • Linux
  • macOS
@tommyettinger
Copy link
Member

This is requested fairly often, though I'm not sure if there's been an issue already. It does seem odd to me that SelectBox is so limited, since I see dropdowns with icons wherever I look; for instance, in IDEA:
idea64_PPIe0Xnxu1

It's a poor workaround, but you can have icons in your font today, if you jump through some hoops to add them as specific characters. They can even have full color and alpha. I think AngelCode BMFont (the first program to support the .fnt format libGDX uses) has a menu item to add images to a font. If the font has icons in it, you can use those icons in a Label, and it's still technically all textual.

I'm not at all familiar enough with scene2d.ui to know why this limitation exists, but there should be some way (added?) to avoid being limited to text-only. I don't know how hard it would be.

@wangdong20
Copy link
Author

This is requested fairly often, though I'm not sure if there's been an issue already. It does seem odd to me that SelectBox is so limited, since I see dropdowns with icons wherever I look; for instance, in IDEA: idea64_PPIe0Xnxu1

It's a poor workaround, but you can have icons in your font today, if you jump through some hoops to add them as specific characters. They can even have full color and alpha. I think AngelCode BMFont (the first program to support the .fnt format libGDX uses) has a menu item to add images to a font. If the font has icons in it, you can use those icons in a Label, and it's still technically all textual.

I'm not at all familiar enough with scene2d.ui to know why this limitation exists, but there should be some way (added?) to avoid being limited to text-only. I don't know how hard it would be.

I just checked the List source code file, it is not very hard to implement draw items as drawable rather than font. You can check the drawItem method in List.java file. I haven't try it, maybe it is hard. At least the List class should provide some interfaces to help developer scale its feature.

@wangdong20
Copy link
Author

I just tried implement a poor workaround and it works well in my project😄. I just create new class extended from SelectBox and SelectBoxScrollPane classes and override some functions to hack it to make it support drawable. I hope my workaround can help someone else who has same issue or give someone a hint to add a new PR either by me or you to support this feature. Here are my code in Kotlin.

import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.GlyphLayout
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.utils.Drawable

class DrawableSelectBox<T: Drawable>(style: SelectBoxStyle?) : SelectBox<T>(style) {

    override fun drawItem(
        batch: Batch?,
        font: BitmapFont?,
        item: T,
        x: Float,
        y: Float,
        width: Float
    ): GlyphLayout? {
        item.draw(batch, x, y + style.listStyle.selection.topHeight - style.listStyle.font.descent - scrollPane.list.itemHeight + 4f, width, scrollPane.list.itemHeight - 8f)
        return null
    }

    override fun newScrollPane(): SelectBoxScrollPane<T> {
        return DrawableSelectBoxScrollPane(this)
    }

}
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.GlyphLayout
import com.badlogic.gdx.scenes.scene2d.ui.List
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.utils.Drawable

class DrawableSelectBoxScrollPane<T: Drawable>(selectBox: SelectBox<T>?) :
    SelectBox.SelectBoxScrollPane<T>(selectBox) {
    override fun newList(): List<T> {
        return object: List<T>(selectBox.style.listStyle) {
            override fun drawItem(
                batch: Batch?,
                font: BitmapFont?,
                index: Int,
                item: T,
                x: Float,
                y: Float,
                width: Float
            ): GlyphLayout? {
                item.draw(batch, x, y + style.selection.topHeight - style.font.descent - itemHeight + 4f, width, itemHeight - 8f)
                return null
            }

            override fun toString(t: T): String {
                // Rewrite this function to assign new width to item
                return "1234"
            }
        }
    }
}

Here is the code how to use it.

val colorSelectBox = DrawableSelectBox<NinePatchDrawable>(mapTypeBoxStyle)
colorSelectBox.items = colorRectDrawables

@SomeTroglodyte
Copy link
Contributor

That sounds familiar. One of the required overridables was broken not too long ago, so I remember trying that with nice long reflection orgies. But yes, that goes some way. The toString limitation is sidestepped not solved, but that's how it is. But you won't get a dropdown that supports entries of varying height - better rewrite from scratch in that case.

Unciv has an entirely different kludge to allow somewhat appealing SelectBoxes (though mostly unused) without any subclassing - render actors as glyphs into the bitmap font...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants