Git Product home page Git Product logo

Comments (4)

tommyettinger avatar tommyettinger commented on May 18, 2024

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.

from libgdx.

wangdong20 avatar wangdong20 commented on May 18, 2024

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.

from libgdx.

wangdong20 avatar wangdong20 commented on May 18, 2024

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

from libgdx.

SomeTroglodyte avatar SomeTroglodyte commented on May 18, 2024

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...

from libgdx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.