Added texture mipmaps and filtering to DynamicFont
This commit is contained in:
parent
3eb3752a59
commit
7b165e8ac2
2 changed files with 88 additions and 10 deletions
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) {
|
Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size, uint32_t p_texture_flags) {
|
||||||
|
|
||||||
|
|
||||||
if (size_cache.has(p_size)) {
|
if (size_cache.has(p_size)) {
|
||||||
|
@ -48,6 +48,7 @@ Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) {
|
||||||
|
|
||||||
size_cache[p_size]=dfas.ptr();
|
size_cache[p_size]=dfas.ptr();
|
||||||
|
|
||||||
|
dfas->texture_flags=p_texture_flags;
|
||||||
dfas->size=p_size;
|
dfas->size=p_size;
|
||||||
dfas->_load();
|
dfas->_load();
|
||||||
|
|
||||||
|
@ -270,6 +271,15 @@ Size2 DynamicFontAtSize::get_char_size(CharType p_char,CharType p_next,const Vec
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DynamicFontAtSize::set_texture_flags(uint32_t p_flags){
|
||||||
|
|
||||||
|
texture_flags=p_flags;
|
||||||
|
for(int i=0;i<textures.size();i++) {
|
||||||
|
Ref<ImageTexture> &tex = textures[i].texture;
|
||||||
|
if (!tex.is_null())
|
||||||
|
tex->set_flags(p_flags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2& p_pos, CharType p_char,CharType p_next,const Color& p_modulate,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const {
|
float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2& p_pos, CharType p_char,CharType p_next,const Color& p_modulate,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const {
|
||||||
|
|
||||||
|
@ -555,7 +565,7 @@ void DynamicFontAtSize::_update_char(CharType p_char) {
|
||||||
|
|
||||||
if (tex.texture.is_null()) {
|
if (tex.texture.is_null()) {
|
||||||
tex.texture.instance();
|
tex.texture.instance();
|
||||||
tex.texture->create_from_image(img,Texture::FLAG_VIDEO_SURFACE);
|
tex.texture->create_from_image(img,Texture::FLAG_VIDEO_SURFACE|texture_flags);
|
||||||
} else {
|
} else {
|
||||||
tex.texture->set_data(img); //update
|
tex.texture->set_data(img); //update
|
||||||
}
|
}
|
||||||
|
@ -595,6 +605,7 @@ DynamicFontAtSize::DynamicFontAtSize() {
|
||||||
ascent=1;
|
ascent=1;
|
||||||
descent=1;
|
descent=1;
|
||||||
linegap=1;
|
linegap=1;
|
||||||
|
texture_flags=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicFontAtSize::~DynamicFontAtSize(){
|
DynamicFontAtSize::~DynamicFontAtSize(){
|
||||||
|
@ -613,7 +624,7 @@ void DynamicFont::set_font_data(const Ref<DynamicFontData>& p_data) {
|
||||||
|
|
||||||
data=p_data;
|
data=p_data;
|
||||||
if (data.is_valid())
|
if (data.is_valid())
|
||||||
data_at_size=data->_get_dynamic_font_at_size(size);
|
data_at_size=data->_get_dynamic_font_at_size(size,texture_flags);
|
||||||
else
|
else
|
||||||
data_at_size=Ref<DynamicFontAtSize>();
|
data_at_size=Ref<DynamicFontAtSize>();
|
||||||
|
|
||||||
|
@ -634,19 +645,64 @@ void DynamicFont::set_size(int p_size){
|
||||||
ERR_FAIL_COND(p_size<1);
|
ERR_FAIL_COND(p_size<1);
|
||||||
if (!data.is_valid())
|
if (!data.is_valid())
|
||||||
return;
|
return;
|
||||||
data_at_size=data->_get_dynamic_font_at_size(size);
|
data_at_size=data->_get_dynamic_font_at_size(size,texture_flags);
|
||||||
for(int i=0;i<fallbacks.size();i++) {
|
for(int i=0;i<fallbacks.size();i++) {
|
||||||
fallback_data_at_size[i]=fallbacks[i]->_get_dynamic_font_at_size(size);
|
fallback_data_at_size[i]=fallbacks[i]->_get_dynamic_font_at_size(size,texture_flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
emit_changed();
|
emit_changed();
|
||||||
_change_notify();
|
_change_notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicFont::get_size() const{
|
int DynamicFont::get_size() const{
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DynamicFont::_update_texture_flags(){
|
||||||
|
|
||||||
|
texture_flags = 0;
|
||||||
|
if (use_mipmaps)
|
||||||
|
texture_flags|=Texture::FLAG_MIPMAPS;
|
||||||
|
if (use_filter)
|
||||||
|
texture_flags|=Texture::FLAG_FILTER;
|
||||||
|
if (!data.is_valid())
|
||||||
|
return;
|
||||||
|
data_at_size->set_texture_flags(texture_flags);
|
||||||
|
for(int i=0;i<fallbacks.size();i++) {
|
||||||
|
fallback_data_at_size[i]->set_texture_flags(texture_flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit_changed();
|
||||||
|
_change_notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DynamicFont::get_use_mipmaps() const{
|
||||||
|
|
||||||
|
return use_mipmaps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicFont::set_use_mipmaps(bool p_enable){
|
||||||
|
|
||||||
|
if (use_mipmaps==p_enable)
|
||||||
|
return;
|
||||||
|
use_mipmaps=p_enable;
|
||||||
|
_update_texture_flags();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DynamicFont::get_use_filter() const{
|
||||||
|
|
||||||
|
return use_filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicFont::set_use_filter(bool p_enable){
|
||||||
|
|
||||||
|
if (use_filter==p_enable)
|
||||||
|
return;
|
||||||
|
use_filter=p_enable;
|
||||||
|
_update_texture_flags();
|
||||||
|
}
|
||||||
|
|
||||||
float DynamicFont::get_height() const{
|
float DynamicFont::get_height() const{
|
||||||
|
|
||||||
if (!data_at_size.is_valid())
|
if (!data_at_size.is_valid())
|
||||||
|
@ -699,7 +755,7 @@ void DynamicFont::set_fallback(int p_idx,const Ref<DynamicFontData>& p_data) {
|
||||||
ERR_FAIL_COND(p_data.is_null());
|
ERR_FAIL_COND(p_data.is_null());
|
||||||
ERR_FAIL_INDEX(p_idx,fallbacks.size());
|
ERR_FAIL_INDEX(p_idx,fallbacks.size());
|
||||||
fallbacks[p_idx]=p_data;
|
fallbacks[p_idx]=p_data;
|
||||||
fallback_data_at_size[p_idx]=fallbacks[p_idx]->_get_dynamic_font_at_size(size);
|
fallback_data_at_size[p_idx]=fallbacks[p_idx]->_get_dynamic_font_at_size(size,texture_flags);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -707,7 +763,7 @@ void DynamicFont::add_fallback(const Ref<DynamicFontData>& p_data) {
|
||||||
|
|
||||||
ERR_FAIL_COND(p_data.is_null());
|
ERR_FAIL_COND(p_data.is_null());
|
||||||
fallbacks.push_back(p_data);
|
fallbacks.push_back(p_data);
|
||||||
fallback_data_at_size.push_back(fallbacks[fallbacks.size()-1]->_get_dynamic_font_at_size(size)); //const..
|
fallback_data_at_size.push_back(fallbacks[fallbacks.size()-1]->_get_dynamic_font_at_size(size,texture_flags)); //const..
|
||||||
|
|
||||||
_change_notify();
|
_change_notify();
|
||||||
emit_changed();
|
emit_changed();
|
||||||
|
@ -794,6 +850,11 @@ void DynamicFont::_bind_methods() {
|
||||||
ObjectTypeDB::bind_method(_MD("set_size","data"),&DynamicFont::set_size);
|
ObjectTypeDB::bind_method(_MD("set_size","data"),&DynamicFont::set_size);
|
||||||
ObjectTypeDB::bind_method(_MD("get_size"),&DynamicFont::get_size);
|
ObjectTypeDB::bind_method(_MD("get_size"),&DynamicFont::get_size);
|
||||||
|
|
||||||
|
ObjectTypeDB::bind_method(_MD("set_use_mipmaps","enable"),&DynamicFont::set_use_mipmaps);
|
||||||
|
ObjectTypeDB::bind_method(_MD("get_use_mipmaps"),&DynamicFont::get_use_mipmaps);
|
||||||
|
ObjectTypeDB::bind_method(_MD("set_use_filter","enable"),&DynamicFont::set_use_filter);
|
||||||
|
ObjectTypeDB::bind_method(_MD("get_use_filter"),&DynamicFont::get_use_filter);
|
||||||
|
|
||||||
ObjectTypeDB::bind_method(_MD("add_fallback","data:DynamicFontData"),&DynamicFont::add_fallback);
|
ObjectTypeDB::bind_method(_MD("add_fallback","data:DynamicFontData"),&DynamicFont::add_fallback);
|
||||||
ObjectTypeDB::bind_method(_MD("set_fallback","idx","data:DynamicFontData"),&DynamicFont::set_fallback);
|
ObjectTypeDB::bind_method(_MD("set_fallback","idx","data:DynamicFontData"),&DynamicFont::set_fallback);
|
||||||
ObjectTypeDB::bind_method(_MD("get_fallback:DynamicFontData","idx"),&DynamicFont::get_fallback);
|
ObjectTypeDB::bind_method(_MD("get_fallback:DynamicFontData","idx"),&DynamicFont::get_fallback);
|
||||||
|
@ -802,12 +863,17 @@ void DynamicFont::_bind_methods() {
|
||||||
|
|
||||||
|
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::INT,"font/size"),_SCS("set_size"),_SCS("get_size"));
|
ADD_PROPERTY(PropertyInfo(Variant::INT,"font/size"),_SCS("set_size"),_SCS("get_size"));
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL,"font/use_mipmaps"),_SCS("set_use_mipmaps"),_SCS("get_use_mipmaps"));
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL,"font/use_filter"),_SCS("set_use_filter"),_SCS("get_use_filter"));
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font/font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data"));
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font/font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data"));
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicFont::DynamicFont() {
|
DynamicFont::DynamicFont() {
|
||||||
|
|
||||||
size=16;
|
size=16;
|
||||||
|
use_mipmaps=false;
|
||||||
|
use_filter=false;
|
||||||
|
texture_flags=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicFont::~DynamicFont() {
|
DynamicFont::~DynamicFont() {
|
||||||
|
|
|
@ -59,7 +59,7 @@ class DynamicFontData : public Resource {
|
||||||
friend class DynamicFont;
|
friend class DynamicFont;
|
||||||
|
|
||||||
|
|
||||||
Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size);
|
Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size, uint32_t p_texture_flags=0);
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
@ -90,6 +90,8 @@ class DynamicFontAtSize : public Reference {
|
||||||
int linegap;
|
int linegap;
|
||||||
int rect_margin;
|
int rect_margin;
|
||||||
|
|
||||||
|
uint32_t texture_flags;
|
||||||
|
|
||||||
bool valid;
|
bool valid;
|
||||||
|
|
||||||
struct CharTexture {
|
struct CharTexture {
|
||||||
|
@ -145,7 +147,7 @@ public:
|
||||||
|
|
||||||
float draw_char(RID p_canvas_item, const Point2& p_pos, CharType p_char,CharType p_next,const Color& p_modulate,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const;
|
float draw_char(RID p_canvas_item, const Point2& p_pos, CharType p_char,CharType p_next,const Color& p_modulate,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const;
|
||||||
|
|
||||||
|
void set_texture_flags(uint32_t p_flags);
|
||||||
|
|
||||||
DynamicFontAtSize();
|
DynamicFontAtSize();
|
||||||
~DynamicFontAtSize();
|
~DynamicFontAtSize();
|
||||||
|
@ -166,9 +168,14 @@ class DynamicFont : public Font {
|
||||||
|
|
||||||
int size;
|
int size;
|
||||||
bool valid;
|
bool valid;
|
||||||
|
bool use_mipmaps;
|
||||||
|
bool use_filter;
|
||||||
|
uint32_t texture_flags;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
void _update_texture_flags();
|
||||||
|
|
||||||
bool _set(const StringName& p_name, const Variant& p_value);
|
bool _set(const StringName& p_name, const Variant& p_value);
|
||||||
bool _get(const StringName& p_name,Variant &r_ret) const;
|
bool _get(const StringName& p_name,Variant &r_ret) const;
|
||||||
void _get_property_list( List<PropertyInfo> *p_list) const;
|
void _get_property_list( List<PropertyInfo> *p_list) const;
|
||||||
|
@ -183,6 +190,11 @@ public:
|
||||||
void set_size(int p_size);
|
void set_size(int p_size);
|
||||||
int get_size() const;
|
int get_size() const;
|
||||||
|
|
||||||
|
bool get_use_mipmaps() const;
|
||||||
|
void set_use_mipmaps(bool p_enable);
|
||||||
|
|
||||||
|
bool get_use_filter() const;
|
||||||
|
void set_use_filter(bool p_enable);
|
||||||
|
|
||||||
void add_fallback(const Ref<DynamicFontData>& p_data);
|
void add_fallback(const Ref<DynamicFontData>& p_data);
|
||||||
void set_fallback(int p_idx,const Ref<DynamicFontData>& p_data);
|
void set_fallback(int p_idx,const Ref<DynamicFontData>& p_data);
|
||||||
|
|
Loading…
Reference in a new issue