Libgdx中TextButton的一些思考

版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/caihongshijie6/article/details/37566183

        由于有要实现下面TextButton的这个需求。然后就去看了一下Libgdx中文档。

游戏中的button,非常多人都比較习惯使用换图片的方式来实现。

非常少有人会直接使用libgdx中的TextButton。假设实在不行也是自己去写一个TextButton的类。

抱着“它真的有那么渣的态度吗”,我去看了一下libgdx自带的TextButton。下面是我的思考的轨迹。整理例如以下:

        在如今,libgdx的资料那么少,有的那些资料也是比較基础的。抱着“看别人的,还不如自己去官方文档。”的态度,自己就開始了下面的历程。。。

        1、首先是看了他官方提供的Gdx-test的样例中有下面的这个使用方法:

new TextButton("Flick Scroll", skin.get("toggle", TextButtonStyle.class));


2、这里面用到了Skin这个类,Skin这个类一直被同事诟病。可是我还是抱着学习的态度去看了一下Skin这个类的官方文档。

下面是自己对Skin类学习以后的一些思考与笔记:

http://blog.csdn.net/hjd_love_zzt/article/details/37566435


 3、 对TextButton的学习与分析。

对一个类的学习还是依照下面思路:“假设有官方demo。就先去看官方的demo。掌握基本使用以后。然后去看那个类的源代码”。


1)下面是自己整理出来的基本使用:

//使用Skin来存储组件的style
		TextButtonStyle textButtonStyle = new TextButtonStyle();
		textButtonStyle.fontColor = Color.RED;//不起作用
		textButtonStyle.font = new BitmapFont(Gdx.files.internal("hjd.fnt"), Gdx.files.internal("hjd.png"),false);
		
//		textButtonStyle.font.setColor(Color.RED);//不起作用
		
//		textButtonStyle.downFontColor = Color.BLUE;
		
		skin.add("style", textButtonStyle);
		
		
		textButton = new TextButton("hello textButton", skin, "style");
//		textButton.getLabel().getStyle().fontColor = Color.YELLOW;//没起作用
//		textButton.getLabel().setColor(Color.RED);//没起作用
		
		textButton.setPosition(50, 50);
//		textButton.setColor(Color.RED);//不起作用
		
		stage.addActor(image);
		stage.addActor(textButton);


2)源代码分析

先贴出TextButton的源代码相关源代码:

这里仅仅看3个函数:。调用TextButton(String,Skin,String)后,它内部会调TextButton(String,TextButtonStyle)这个构造函数。

而这个构造函数中掉了Label的构造函数,所以Style.font、fontColor对象一定要初始化,否则会报对应的异常。

。。

public TextButton (String text, Skin skin, String styleName) {
		this(text, skin.get(styleName, TextButtonStyle.class));
		setSkin(skin);
	}

	public TextButton (String text, TextButtonStyle style) {
		super(style);
		this.style = style;
		label = new Label(text, new LabelStyle(style.font, style.fontColor));
		label.setAlignment(Align.center);
		add(label).expand().fill();
		setWidth(getPrefWidth());
		setHeight(getPrefHeight());
	}


至于draw()函数,我想这就是为什么这个TextButton为什么写的失败的原因了吧。。。。实在是太渣了。。

。。。恩恩。是的。

事实证明官方的TextButton确实是太渣了。

。想要设置个字体的颜色都做不到。。

。使用提供的API没有效果。点进去看,有的函数竟然还没有实现。

。。

草。。

。。

public void draw (SpriteBatch batch, float parentAlpha) {
		Color fontColor;
		if (isDisabled && style.disabledFontColor != null)
			fontColor = style.disabledFontColor;
		else if (isPressed() && style.downFontColor != null)
			fontColor = style.downFontColor;
		else if (isChecked && style.checkedFontColor != null)
			fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;
		else if (isOver() && style.overFontColor != null)
			fontColor = style.overFontColor;
		else
			fontColor = style.fontColor;
		if (fontColor != null) label.getStyle().fontColor = fontColor;
		super.draw(batch, parentAlpha);
	}


 




     


     


【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10889059.html