Merge pull request #4847 from Hinsbart/fix_icon_scale_new

Proper alignment and fixed scaling for TileMapEditor tile icons.
This commit is contained in:
Rémi Verschelde 2016-05-29 18:39:05 +02:00
commit c2b1315689
3 changed files with 70 additions and 31 deletions

View File

@ -325,6 +325,18 @@ int ItemList::get_fixed_column_width() const{
return fixed_column_width;
}
void ItemList::set_same_column_width(bool p_enable){
same_column_width=p_enable;
update();
shape_changed=true;
}
int ItemList::is_same_column_width() const{
return same_column_width;
}
void ItemList::set_max_text_lines(int p_lines){
ERR_FAIL_COND(p_lines<1);
@ -720,7 +732,7 @@ void ItemList::ensure_current_is_visible() {
update();
}
static Size2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size, bool p_stretch) {
static Size2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) {
if (p_max_size.x<=0)
p_max_size.x=1e20;
@ -730,10 +742,6 @@ static Size2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size, bool p_stretch)
Size2 new_size;
if (p_stretch && (p_size.x * p_size.y < p_max_size.x * p_max_size.y)) {
return p_max_size;
}
if (p_size.x > p_max_size.x) {
new_size.width=p_max_size.x;
@ -829,13 +837,15 @@ void ItemList::_notification(int p_what) {
if (shape_changed) {
float max_column_width = 0;
//1- compute item minimum sizes
for(int i=0;i<items.size();i++) {
Size2 minsize;
if (items[i].icon.is_valid()) {
minsize=_adjust_to_max_size(items[i].get_icon_size(),max_icon_size, icon_stretch);
minsize=_adjust_to_max_size(items[i].get_icon_size(),max_icon_size) * icon_scale;
if (items[i].text!="") {
if (icon_mode==ICON_MODE_TOP) {
@ -868,10 +878,11 @@ void ItemList::_notification(int p_what) {
}
items[i].rect_cache.size=minsize;
if (fixed_column_width>0)
items[i].rect_cache.size.x=fixed_column_width;
minsize.x=fixed_column_width;
max_column_width=MAX(max_column_width,minsize.x);
items[i].rect_cache.size=minsize;
items[i].min_rect_cache.size=minsize;
}
@ -900,17 +911,23 @@ void ItemList::_notification(int p_what) {
break;
}
items[i].rect_cache=items[i].min_rect_cache;
if(same_column_width)
items[i].rect_cache.size.x=max_column_width;
items[i].rect_cache.pos=ofs;
max_h=MAX(max_h,items[i].rect_cache.size.y);
ofs.x+=items[i].rect_cache.size.x;
ofs.x+=items[i].rect_cache.size.x + hseparation;
//print_line("item "+itos(i)+" ofs "+rtos(items[i].rect_cache.size.x));
if (col>0)
ofs.x+=hseparation;
col++;
if (col==current_columns) {
if (i<items.size()-1)
separators.push_back(ofs.y+max_h+vseparation/2);
for(int j=i;j>=0 && col>0;j--, col--) {
items[j].rect_cache.size.y = max_h;
}
ofs.x=0;
ofs.y+=max_h+vseparation;
col=0;
@ -918,6 +935,10 @@ void ItemList::_notification(int p_what) {
}
}
for(int j=items.size()-1;j>=0 && col>0;j--, col--) {
items[j].rect_cache.size.y = max_h;
}
if (all_fit) {
float max = MAX(page,ofs.y+max_h);
scroll_bar->set_max(max);
@ -980,7 +1001,7 @@ void ItemList::_notification(int p_what) {
Vector2 text_ofs;
if (items[i].icon.is_valid()) {
Size2 icon_size = _adjust_to_max_size(items[i].get_icon_size(),max_icon_size, icon_stretch);
Size2 icon_size = _adjust_to_max_size(items[i].get_icon_size(),max_icon_size) * icon_scale;
Vector2 icon_ofs;
if (min_icon_size!=Vector2()) {
@ -992,7 +1013,12 @@ void ItemList::_notification(int p_what) {
if (icon_mode==ICON_MODE_TOP) {
pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width)/2);
pos.y += MIN(
Math::floor((items[i].rect_cache.size.height - icon_size.height)/2),
items[i].rect_cache.size.height - items[i].min_rect_cache.size.height
);
text_ofs.y = MAX(icon_size.height, min_icon_size.y) + icon_margin;
text_ofs.y += items[i].rect_cache.size.height - items[i].min_rect_cache.size.height;
} else {
pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height)/2);
@ -1018,6 +1044,8 @@ void ItemList::_notification(int p_what) {
Vector2 size = font->get_string_size(items[i].text);
if (fixed_column_width)
max_len=fixed_column_width;
else if(same_column_width)
max_len=items[i].rect_cache.size.x;
else
max_len=size.x;
@ -1205,14 +1233,12 @@ bool ItemList::get_allow_rmb_select() const {
return allow_rmb_select;
}
void ItemList::set_icon_stretch_to_max_size(bool p_stretch) {
icon_stretch = p_stretch;
void ItemList::set_icon_scale(real_t p_scale) {
icon_scale = p_scale;
}
bool ItemList::get_icon_stretch_to_max_size() const {
return icon_stretch;
real_t ItemList::get_icon_scale() const {
return icon_scale;
}
void ItemList::_bind_methods(){
@ -1257,6 +1283,9 @@ void ItemList::_bind_methods(){
ObjectTypeDB::bind_method(_MD("set_fixed_column_width","width"),&ItemList::set_fixed_column_width);
ObjectTypeDB::bind_method(_MD("get_fixed_column_width"),&ItemList::get_fixed_column_width);
ObjectTypeDB::bind_method(_MD("set_same_column_width","enable"),&ItemList::set_same_column_width);
ObjectTypeDB::bind_method(_MD("is_same_column_width"),&ItemList::is_same_column_width);
ObjectTypeDB::bind_method(_MD("set_max_text_lines","lines"),&ItemList::set_max_text_lines);
ObjectTypeDB::bind_method(_MD("get_max_text_lines"),&ItemList::get_max_text_lines);
@ -1275,8 +1304,8 @@ void ItemList::_bind_methods(){
ObjectTypeDB::bind_method(_MD("set_max_icon_size","size"),&ItemList::set_max_icon_size);
ObjectTypeDB::bind_method(_MD("get_max_icon_size"),&ItemList::get_max_icon_size);
ObjectTypeDB::bind_method(_MD("set_icon_stretch_to_max_size","stretch"),&ItemList::set_icon_stretch_to_max_size);
ObjectTypeDB::bind_method(_MD("get_icon_stretch_to_max_size"),&ItemList::get_icon_stretch_to_max_size);
ObjectTypeDB::bind_method(_MD("set_icon_scale","scale"),&ItemList::set_icon_scale);
ObjectTypeDB::bind_method(_MD("get_icon_scale"),&ItemList::get_icon_scale);
ObjectTypeDB::bind_method(_MD("set_allow_rmb_select","allow"),&ItemList::set_allow_rmb_select);
ObjectTypeDB::bind_method(_MD("get_allow_rmb_select"),&ItemList::get_allow_rmb_select);
@ -1309,6 +1338,7 @@ ItemList::ItemList() {
icon_mode=ICON_MODE_LEFT;
fixed_column_width=0;
same_column_width = false;
max_text_lines=1;
max_columns=1;
@ -1325,8 +1355,7 @@ ItemList::ItemList() {
defer_select_single=-1;
allow_rmb_select=false;
icon_stretch = false;
icon_scale = 1.0f;
}
ItemList::~ItemList() {

View File

@ -33,6 +33,7 @@ private:
Color custom_bg;
Rect2 rect_cache;
Rect2 min_rect_cache;
Size2 get_icon_size() const;
@ -44,8 +45,7 @@ private:
bool shape_changed;
bool ensure_selected_visible;
bool icon_stretch;
bool same_column_width;
Vector<Item> items;
Vector<int> separators;
@ -61,12 +61,18 @@ private:
int fixed_column_width;
int max_text_lines;
int max_columns;
Size2 min_icon_size;
Size2 max_icon_size;
Size2 max_item_size_cache;
int defer_select_single;
bool allow_rmb_select;
real_t icon_scale;
void _scroll_changed(double);
void _input_event(const InputEvent& p_event);
protected:
@ -123,6 +129,9 @@ public:
void set_fixed_column_width(int p_size);
int get_fixed_column_width() const;
void set_same_column_width(bool p_enable);
int is_same_column_width() const;
void set_max_text_lines(int p_amount);
int get_max_text_lines() const;
@ -152,8 +161,8 @@ public:
virtual String get_tooltip(const Point2& p_pos) const;
int get_item_at_pos(const Point2& p_pos,bool p_exact=false) const;
void set_icon_stretch_to_max_size(bool p_stretch);
bool get_icon_stretch_to_max_size() const;
void set_icon_scale(real_t p_scale);
real_t get_icon_scale() const;
ItemList();
~ItemList();

View File

@ -205,7 +205,9 @@ void TileMapEditor::_update_palette() {
if (tiles.empty())
return;
palette->set_max_columns(0);
palette->add_constant_override("hseparation", 6);
palette->set_icon_mode(ItemList::ICON_MODE_TOP);
palette->set_max_text_lines(2);
@ -240,6 +242,8 @@ void TileMapEditor::_update_palette() {
palette->set_item_metadata(palette->get_item_count()-1, E->get());
}
palette->set_same_column_width(true);
if (selected != -1)
set_selected_tile(selected);
else
@ -1210,10 +1214,7 @@ void TileMapEditor::_tileset_settings_changed() {
void TileMapEditor::_icon_size_changed(float p_value) {
if (node) {
Size2 size = node->get_cell_size() * p_value;
palette->set_min_icon_size(size + Size2(4, 0)); //4px gap between tiles
palette->set_icon_stretch_to_max_size(true);
palette->set_max_icon_size(size);
palette->set_icon_scale(p_value);
_update_palette();
}
}