Преглед на файлове

added alignment to BoxContainer

Allows aligning contents of VBoxContainer and HBoxcontainer without
having to insert expanding spacers.
Zher Huei Lee преди 10 години
родител
ревизия
c38808b5b1
променени са 2 файла, в които са добавени 53 реда и са изтрити 0 реда
  1. 36 0
      scene/gui/box_container.cpp
  2. 17 0
      scene/gui/box_container.h

+ 36 - 0
scene/gui/box_container.cpp

@@ -99,8 +99,10 @@ void BoxContainer::_resort() {
 		elements exist */
 
 
+	bool has_stretched = false;
 	while(stretch_ratio_total>0) { // first of all, dont even be here if no stretchable objects exist
 
+		has_stretched = true;
 		bool refit_successful=true; //assume refit-test will go well
 
 		for(int i=0;i<get_child_count();i++) {
@@ -143,6 +145,18 @@ void BoxContainer::_resort() {
 
 
 	int ofs=0;
+	if (!has_stretched) {
+		switch (align) {
+			case ALIGN_BEGIN:
+				break;
+			case ALIGN_CENTER:
+				ofs = stretch_diff / 2;
+				break;
+			case ALIGN_END:
+				ofs = stretch_diff;
+				break;
+		}
+	}
 
 	first=true;
 	int idx=0;
@@ -254,6 +268,15 @@ void BoxContainer::_notification(int p_what) {
 	}
 }
 
+void BoxContainer::set_alignment(AlignMode p_align) {
+	align = p_align;
+	_resort();
+}
+
+BoxContainer::AlignMode BoxContainer::get_alignment() const {
+	return align;
+}
+
 void BoxContainer::add_spacer(bool p_begin) {
 
 	Control *c = memnew( Control );
@@ -270,10 +293,23 @@ void BoxContainer::add_spacer(bool p_begin) {
 BoxContainer::BoxContainer(bool p_vertical) {
 
 	vertical=p_vertical;
+	align = ALIGN_BEGIN;
 //	set_ignore_mouse(true);
 	set_stop_mouse(false);
 }
 
+void BoxContainer::_bind_methods() {
+
+	ObjectTypeDB::bind_method(_MD("get_alignment"),&BoxContainer::get_alignment);
+	ObjectTypeDB::bind_method(_MD("set_alignment","alignment"),&BoxContainer::set_alignment);
+
+	BIND_CONSTANT( ALIGN_BEGIN );
+	BIND_CONSTANT( ALIGN_CENTER );
+	BIND_CONSTANT( ALIGN_END );
+
+	ADD_PROPERTY( PropertyInfo(Variant::INT,"alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), _SCS("set_alignment"),_SCS("get_alignment") );
+
+}
 
 MarginContainer* VBoxContainer::add_margin_child(const String& p_label,Control *p_control,bool p_expand) {
 

+ 17 - 0
scene/gui/box_container.h

@@ -35,16 +35,31 @@ class BoxContainer : public Container {
 
 	OBJ_TYPE(BoxContainer,Container);
 
+public:
+
+	enum AlignMode {
+		ALIGN_BEGIN,
+		ALIGN_CENTER,
+		ALIGN_END
+	};
+
+private:
 	bool vertical;
+	AlignMode align;
 
 	void _resort();
 protected:
 
 	void _notification(int p_what);
+
+	static void _bind_methods();
 public:
 
 	void add_spacer(bool p_begin=false);
 
+	void set_alignment(AlignMode p_align);
+	AlignMode get_alignment() const;
+
 	virtual Size2 get_minimum_size() const;
 
 	BoxContainer(bool p_vertical=false);
@@ -73,4 +88,6 @@ public:
 	VBoxContainer() : BoxContainer(true) {}
 };
 
+VARIANT_ENUM_CAST(BoxContainer::AlignMode);
+
 #endif // BOX_CONTAINER_H