From a735573327d26536bd29e14350f1e5811c030b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franco=20Eus=C3=A9bio=20Garcia?= Date: Sun, 1 May 2016 05:33:32 -0300 Subject: Add math/script expression evaluation in editor (#3620) Rebase of #453 --- scene/gui/tree.cpp | 26 ++++++++++++++++++++------ scene/gui/tree.h | 6 +++++- 2 files changed, 25 insertions(+), 7 deletions(-) (limited to 'scene/gui') diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 718206dee1..278e6584fa 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -154,7 +154,7 @@ void TreeItem::set_text(int p_column,String p_text) { ERR_FAIL_INDEX( p_column, cells.size() ); cells[p_column].text=p_text; - if (cells[p_column].mode==TreeItem::CELL_MODE_RANGE) { + if (cells[p_column].mode==TreeItem::CELL_MODE_RANGE || cells[p_column].mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) { cells[p_column].min=0; cells[p_column].max=p_text.get_slice_count(","); @@ -704,6 +704,7 @@ void TreeItem::_bind_methods() { BIND_CONSTANT( CELL_MODE_STRING ); BIND_CONSTANT( CELL_MODE_CHECK ); BIND_CONSTANT( CELL_MODE_RANGE ); + BIND_CONSTANT( CELL_MODE_RANGE_EXPRESSION ); BIND_CONSTANT( CELL_MODE_ICON ); BIND_CONSTANT( CELL_MODE_CUSTOM ); @@ -1127,7 +1128,8 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w ); } break; - case TreeItem::CELL_MODE_RANGE: { + case TreeItem::CELL_MODE_RANGE: + case TreeItem::CELL_MODE_RANGE_EXPRESSION: { if (p_item->cells[i].text!="") { @@ -1594,7 +1596,8 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ } } break; - case TreeItem::CELL_MODE_RANGE: { + case TreeItem::CELL_MODE_RANGE: + case TreeItem::CELL_MODE_RANGE_EXPRESSION: { if (c.text!="") { @@ -1794,6 +1797,13 @@ void Tree::text_editor_enter(String p_text) { //popup_edited_item->edited_signal.call( popup_edited_item_col ); } break; + case TreeItem::CELL_MODE_RANGE_EXPRESSION: { + + if(evaluator) + c.val=evaluator->eval(p_text); + else + c.val=p_text.to_double(); + } break; default: { ERR_FAIL(); } } @@ -2372,7 +2382,7 @@ bool Tree::edit_selected() { item_edited(col,s); return true; - } else if (c.mode==TreeItem::CELL_MODE_RANGE && c.text!="") { + } else if ((c.mode==TreeItem::CELL_MODE_RANGE||c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text!="") { popup_menu->clear(); for (int i=0;iset_pos( textedpos ); @@ -2398,7 +2408,7 @@ bool Tree::edit_selected() { text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:rtos(c.val) ); text_editor->select_all(); - if (c.mode==TreeItem::CELL_MODE_RANGE) { + if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) { value_editor->set_pos(textedpos + Point2i(0,text_editor->get_size().height) ); value_editor->set_size( Size2(rect.size.width,1)); @@ -3227,6 +3237,9 @@ bool Tree::is_folding_hidden() const { return hide_folding; } +void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) { + evaluator = p_evaluator; +} void Tree::_bind_methods() { @@ -3367,6 +3380,7 @@ Tree::Tree() { hide_folding=false; + evaluator=NULL; } diff --git a/scene/gui/tree.h b/scene/gui/tree.h index 1ba1c6a494..75bcfd8e33 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -34,6 +34,7 @@ #include "scene/gui/line_edit.h" #include "scene/gui/scroll_bar.h" #include "scene/gui/slider.h" +#include "core/helper/value_evaluator.h" /** @author Juan Linietsky @@ -52,6 +53,7 @@ public: CELL_MODE_STRING, ///< just a string CELL_MODE_CHECK, ///< string + check CELL_MODE_RANGE, ///< Contains a range + CELL_MODE_RANGE_EXPRESSION, ///< Contains a range CELL_MODE_ICON, ///< Contains a icon, not editable CELL_MODE_CUSTOM, ///< Contains a custom value, show a string, and an edit button }; @@ -422,6 +424,8 @@ friend class TreeItem; bool hide_folding; + ValueEvaluator *evaluator; + protected: static void _bind_methods(); @@ -482,7 +486,7 @@ public: void set_hide_folding(bool p_hide); bool is_folding_hidden() const; - + void set_value_evaluator(ValueEvaluator *p_evaluator); Tree(); ~Tree(); -- cgit v1.2.3