Tabs: cleanup code, no functional change

Some dead code removed, a few cosmetic changes.
This commit is contained in:
Rémi Verschelde 2016-05-01 16:27:33 +02:00
parent 8574489dd2
commit 8b32c80529
1 changed files with 36 additions and 86 deletions

View File

@ -32,25 +32,23 @@
Size2 Tabs::get_minimum_size() const { Size2 Tabs::get_minimum_size() const {
Ref<StyleBox> tab_bg = get_stylebox("tab_bg"); Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
Ref<StyleBox> tab_fg = get_stylebox("tab_fg"); Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
Ref<Font> font = get_font("font"); Ref<Font> font = get_font("font");
Size2 ms(0, MAX( tab_bg->get_minimum_size().height,tab_fg->get_minimum_size().height)+font->get_height() ); Size2 ms(0, MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height)+font->get_height());
// h+=MIN( get_constant("label_valign_fg"), get_constant("label_valign_bg") );
for(int i=0;i<tabs.size();i++) { for(int i=0;i<tabs.size();i++) {
Ref<Texture> tex = tabs[i].icon; Ref<Texture> tex = tabs[i].icon;
if (tex.is_valid()) { if (tex.is_valid()) {
ms.height = MAX( ms.height, tex->get_size().height ); ms.height = MAX(ms.height, tex->get_size().height);
if (tabs[i].text!="") if (tabs[i].text!="")
ms.width+=get_constant("hseparation"); ms.width+=get_constant("hseparation");
} }
ms.width+=font->get_string_size(tabs[i].text).width; ms.width+=font->get_string_size(tabs[i].text).width;
if (current==i) if (current==i)
ms.width+=tab_fg->get_minimum_size().width; ms.width+=tab_fg->get_minimum_size().width;
else else
@ -58,28 +56,26 @@ Size2 Tabs::get_minimum_size() const {
if (tabs[i].right_button.is_valid()) { if (tabs[i].right_button.is_valid()) {
Ref<Texture> rb=tabs[i].right_button; Ref<Texture> rb=tabs[i].right_button;
Size2 bms = rb->get_size();//+get_stylebox("button")->get_minimum_size(); Size2 bms = rb->get_size();
bms.width+=get_constant("hseparation"); bms.width+=get_constant("hseparation");
ms.width+=bms.width; ms.width+=bms.width;
ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height); ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height);
} }
if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) { if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) {
Ref<Texture> cb=get_icon("close"); Ref<Texture> cb=get_icon("close");
Size2 bms = cb->get_size();//+get_stylebox("button")->get_minimum_size(); Size2 bms = cb->get_size();
bms.width+=get_constant("hseparation"); bms.width+=get_constant("hseparation");
ms.width+=bms.width; ms.width+=bms.width;
ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height); ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height);
} }
} }
ms.width=0; //should make this optional ms.width=0; //TODO: should make this optional
return ms; return ms;
} }
void Tabs::_input_event(const InputEvent& p_event) { void Tabs::_input_event(const InputEvent& p_event) {
if (p_event.type==InputEvent::MOUSE_MOTION) { if (p_event.type==InputEvent::MOUSE_MOTION) {
@ -101,15 +97,14 @@ void Tabs::_input_event(const InputEvent& p_event) {
} }
} }
// test hovering to display right or close button
int hover_buttons=-1; int hover_buttons=-1;
hover=-1; hover=-1;
for(int i=0;i<tabs.size();i++) { for(int i=0;i<tabs.size();i++) {
// test hovering tab to display close button if policy says so if (i<offset)
continue;
// test hovering right button and close button
if (tabs[i].rb_rect.has_point(pos)) { if (tabs[i].rb_rect.has_point(pos)) {
rb_hover=i; rb_hover=i;
cb_hover=-1; cb_hover=-1;
@ -123,11 +118,9 @@ void Tabs::_input_event(const InputEvent& p_event) {
break; break;
} }
} }
if (hover_buttons == -1) { // no hover if (hover_buttons == -1) { // no hover
rb_hover= hover_buttons; rb_hover= hover_buttons;
cb_hover= hover_buttons; cb_hover= hover_buttons;
} }
@ -137,8 +130,6 @@ void Tabs::_input_event(const InputEvent& p_event) {
} }
if (rb_pressing && p_event.type==InputEvent::MOUSE_BUTTON && if (rb_pressing && p_event.type==InputEvent::MOUSE_BUTTON &&
!p_event.mouse_button.pressed && !p_event.mouse_button.pressed &&
p_event.mouse_button.button_index==BUTTON_LEFT) { p_event.mouse_button.button_index==BUTTON_LEFT) {
@ -152,9 +143,10 @@ void Tabs::_input_event(const InputEvent& p_event) {
update(); update();
} }
if (cb_pressing && p_event.type==InputEvent::MOUSE_BUTTON && if (cb_pressing && p_event.type==InputEvent::MOUSE_BUTTON &&
!p_event.mouse_button.pressed && !p_event.mouse_button.pressed &&
p_event.mouse_button.button_index==BUTTON_LEFT) { p_event.mouse_button.button_index==BUTTON_LEFT) {
if (cb_hover!=-1) { if (cb_hover!=-1) {
//pressed //pressed
@ -195,12 +187,12 @@ void Tabs::_input_event(const InputEvent& p_event) {
} }
} }
int found=-1; int found=-1;
for(int i=0;i<tabs.size();i++) { for(int i=0;i<tabs.size();i++) {
if (i<offset) if (i<offset)
continue; continue;
if (tabs[i].rb_rect.has_point(pos)) { if (tabs[i].rb_rect.has_point(pos)) {
rb_pressing=true; rb_pressing=true;
update(); update();
@ -213,10 +205,7 @@ void Tabs::_input_event(const InputEvent& p_event) {
return; return;
} }
int ofs=tabs[i].ofs_cache;
int size = tabs[i].ofs_cache;
if (pos.x >=tabs[i].ofs_cache && pos.x<tabs[i].ofs_cache+tabs[i].size_cache) { if (pos.x >=tabs[i].ofs_cache && pos.x<tabs[i].ofs_cache+tabs[i].size_cache) {
found=i; found=i;
break; break;
} }
@ -232,8 +221,8 @@ void Tabs::_input_event(const InputEvent& p_event) {
} }
void Tabs::_notification(int p_what) {
void Tabs::_notification(int p_what) {
switch(p_what) { switch(p_what) {
@ -259,39 +248,20 @@ void Tabs::_notification(int p_what) {
Ref<Texture> close=get_icon("close"); Ref<Texture> close=get_icon("close");
int h = get_size().height; int h = get_size().height;
int w = 0;
int label_valign_fg = get_constant("label_valign_fg");
int label_valign_bg = get_constant("label_valign_bg");
int w=0;
int mw = 0; int mw = 0;
{ for(int i=0;i<tabs.size();i++) {
// h+=MIN( get_constant("label_valign_fg"), get_constant("label_valign_bg") );
for(int i=0;i<tabs.size();i++) {
int sz = get_tab_width(i);
tabs[i].ofs_cache=mw;
mw+=sz;
}
tabs[i].ofs_cache = mw;
mw += get_tab_width(i);
} }
if (tab_align==ALIGN_CENTER) { if (tab_align==ALIGN_CENTER) {
w=(get_size().width-mw)/2; w=(get_size().width-mw)/2;
} else if (tab_align==ALIGN_RIGHT) { } else if (tab_align==ALIGN_RIGHT) {
w=get_size().width-mw; w=get_size().width-mw;
} }
if (w<0) { if (w<0) {
@ -311,6 +281,7 @@ void Tabs::_notification(int p_what) {
if (i<offset) if (i<offset)
continue; continue;
tabs[i].ofs_cache=w; tabs[i].ofs_cache=w;
int lsize = get_tab_width(i); int lsize = get_tab_width(i);
@ -327,18 +298,14 @@ void Tabs::_notification(int p_what) {
} }
Ref<StyleBox> sb; Ref<StyleBox> sb;
int va;
Color col; Color col;
if (i==current) { if (i==current) {
sb=tab_fg; sb=tab_fg;
va=label_valign_fg;
col=color_fg; col=color_fg;
} else { } else {
sb=tab_bg; sb=tab_bg;
va=label_valign_bg;
col=color_bg; col=color_bg;
} }
@ -363,6 +330,7 @@ void Tabs::_notification(int p_what) {
w+=slen; w+=slen;
if (tabs[i].right_button.is_valid()) { if (tabs[i].right_button.is_valid()) {
Ref<StyleBox> style = get_stylebox("button"); Ref<StyleBox> style = get_stylebox("button");
Ref<Texture> rb=tabs[i].right_button; Ref<Texture> rb=tabs[i].right_button;
@ -387,10 +355,8 @@ void Tabs::_notification(int p_what) {
w+=style->get_margin(MARGIN_RIGHT); w+=style->get_margin(MARGIN_RIGHT);
tabs[i].rb_rect=rb_rect; tabs[i].rb_rect=rb_rect;
} }
if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) { if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) {
Ref<StyleBox> style = get_stylebox("button"); Ref<StyleBox> style = get_stylebox("button");
@ -410,11 +376,8 @@ void Tabs::_notification(int p_what) {
style->draw(ci,cb_rect); style->draw(ci,cb_rect);
} }
//w+=style->get_margin(MARGIN_LEFT);
cb->draw(ci,Point2i( w,cb_rect.pos.y+style->get_margin(MARGIN_TOP) )); cb->draw(ci,Point2i( w,cb_rect.pos.y+style->get_margin(MARGIN_TOP) ));
w+=cb->get_width(); w+=cb->get_width();
//w+=style->get_margin(MARGIN_RIGHT);
tabs[i].cb_rect=cb_rect; tabs[i].cb_rect=cb_rect;
} }
@ -429,28 +392,26 @@ void Tabs::_notification(int p_what) {
int vofs = (get_size().height-incr->get_size().height)/2; int vofs = (get_size().height-incr->get_size().height)/2;
if (offset>0) if (offset>0)
draw_texture(hilite_arrow==0?decr_hl:decr,Point2(limit,vofs)); draw_texture(hilite_arrow==0 ? decr_hl : decr, Point2(limit,vofs));
else else
draw_texture(decr,Point2(limit,vofs),Color(1,1,1,0.5)); draw_texture(decr,Point2(limit,vofs), Color(1,1,1,0.5));
if (missing_right) if (missing_right)
draw_texture(hilite_arrow==1?incr_hl:incr,Point2(limit+decr->get_size().width,vofs)); draw_texture(hilite_arrow==1 ? incr_hl : incr, Point2(limit+decr->get_size().width,vofs));
else else
draw_texture(incr,Point2(limit+decr->get_size().width,vofs),Color(1,1,1,0.5)); draw_texture(incr,Point2(limit+decr->get_size().width,vofs), Color(1,1,1,0.5));
buttons_visible=true; buttons_visible=true;
} else { } else {
buttons_visible=false; buttons_visible=false;
} }
} break; } break;
} }
} }
int Tabs::get_tab_count() const { int Tabs::get_tab_count() const {
return tabs.size(); return tabs.size();
} }
@ -459,11 +420,9 @@ void Tabs::set_current_tab(int p_current) {
ERR_FAIL_INDEX( p_current, get_tab_count() ); ERR_FAIL_INDEX( p_current, get_tab_count() );
//printf("DEBUG %p: set_current_tab to %i\n", this, p_current);
current=p_current; current=p_current;
_change_notify("current_tab"); _change_notify("current_tab");
//emit_signal("tab_changed",current);
update(); update();
} }
@ -487,9 +446,9 @@ String Tabs::get_tab_title(int p_tab) const{
ERR_FAIL_INDEX_V(p_tab,tabs.size(),""); ERR_FAIL_INDEX_V(p_tab,tabs.size(),"");
return tabs[p_tab].text; return tabs[p_tab].text;
} }
void Tabs::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){ void Tabs::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){
ERR_FAIL_INDEX(p_tab,tabs.size()); ERR_FAIL_INDEX(p_tab,tabs.size());
@ -498,6 +457,7 @@ void Tabs::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){
minimum_size_changed(); minimum_size_changed();
} }
Ref<Texture> Tabs::get_tab_icon(int p_tab) const{ Ref<Texture> Tabs::get_tab_icon(int p_tab) const{
ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>()); ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>());
@ -506,7 +466,6 @@ Ref<Texture> Tabs::get_tab_icon(int p_tab) const{
} }
void Tabs::set_tab_right_button(int p_tab,const Ref<Texture>& p_right_button){ void Tabs::set_tab_right_button(int p_tab,const Ref<Texture>& p_right_button){
ERR_FAIL_INDEX(p_tab,tabs.size()); ERR_FAIL_INDEX(p_tab,tabs.size());
@ -556,8 +515,6 @@ void Tabs::remove_tab(int p_idx) {
if (current>=tabs.size()) if (current>=tabs.size())
current=tabs.size()-1; current=tabs.size()-1;
//emit_signal("tab_changed",current);
_ensure_no_over_offset(); _ensure_no_over_offset();
} }
@ -581,7 +538,7 @@ int Tabs::get_tab_width(int p_idx) const {
Ref<StyleBox> tab_bg = get_stylebox("tab_bg"); Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
Ref<StyleBox> tab_fg = get_stylebox("tab_fg"); Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
Ref<Font> font = get_font("font"); Ref<Font> font = get_font("font");
Ref<Texture> close=get_icon("close");
int x=0; int x=0;
@ -593,8 +550,8 @@ int Tabs::get_tab_width(int p_idx) const {
} }
x+=font->get_string_size(tabs[p_idx].text).width; x+=font->get_string_size(tabs[p_idx].text).width;
if (current==p_idx) if (current==p_idx)
x+=tab_fg->get_minimum_size().width; x+=tab_fg->get_minimum_size().width;
else else
@ -602,17 +559,14 @@ int Tabs::get_tab_width(int p_idx) const {
if (tabs[p_idx].right_button.is_valid()) { if (tabs[p_idx].right_button.is_valid()) {
Ref<Texture> rb=tabs[p_idx].right_button; Ref<Texture> rb=tabs[p_idx].right_button;
Size2 bms = rb->get_size();//+get_stylebox("button")->get_minimum_size(); x+=rb->get_width();
bms.width+=get_constant("hseparation"); x+=get_constant("hseparation");
x+=bms.width;
} }
if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx==current)) { if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx==current)) {
Ref<Texture> cb=get_icon("close");
Size2 bms = close->get_size();//+get_stylebox("button")->get_minimum_size(); x+=cb->get_width();
bms.width+=get_constant("hseparation"); x+=get_constant("hseparation");
x+=bms.width;
} }
return x; return x;
@ -668,11 +622,9 @@ void Tabs::ensure_tab_visible(int p_idx) {
Ref<Texture> incr = get_icon("increment"); Ref<Texture> incr = get_icon("increment");
Ref<Texture> decr = get_icon("decrement"); Ref<Texture> decr = get_icon("decrement");
int limit=get_size().width-incr->get_width()-decr->get_width(); int limit=get_size().width-incr->get_width()-decr->get_width();
int x=0; int x=0;
for(int i=0;i<tabs.size();i++) { for(int i=0;i<tabs.size();i++) {
@ -718,7 +670,6 @@ void Tabs::_bind_methods() {
ADD_SIGNAL(MethodInfo("right_button_pressed",PropertyInfo(Variant::INT,"tab"))); ADD_SIGNAL(MethodInfo("right_button_pressed",PropertyInfo(Variant::INT,"tab")));
ADD_SIGNAL(MethodInfo("tab_close",PropertyInfo(Variant::INT,"tab"))); ADD_SIGNAL(MethodInfo("tab_close",PropertyInfo(Variant::INT,"tab")));
ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab") ); ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab") );
BIND_CONSTANT( ALIGN_LEFT ); BIND_CONSTANT( ALIGN_LEFT );
@ -742,9 +693,8 @@ Tabs::Tabs() {
cb_hover=-1; cb_hover=-1;
cb_pressing=false; cb_pressing=false;
cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER; // Default : no close button cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
offset=0; offset=0;
max_drawn_tab=0; max_drawn_tab=0;
} }