Skip to content

Commit

Permalink
Merge pull request #396 from gfgtdf/lua_tree_view
Browse files Browse the repository at this point in the history
Enable tree_view gui2 widget for lua
  • Loading branch information
gfgtdf committed Apr 8, 2015
2 parents b406029 + dce3e24 commit e4c6c8b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/gui/widgets/tree_view.hpp
Expand Up @@ -76,7 +76,7 @@ class ttree_view : public tscrollbar_container
return selected_item_;
}

void set_selection_change_callback(boost::function<void()> callback)
void set_selection_change_callback(boost::function<void(twidget&)> callback)
{
selection_change_callback_ = callback;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ class ttree_view : public tscrollbar_container

ttree_view_node* selected_item_;

boost::function<void()> selection_change_callback_;
boost::function<void(twidget&)> selection_change_callback_;

/**
* Resizes the content.
Expand Down
33 changes: 29 additions & 4 deletions src/gui/widgets/tree_view_node.cpp
Expand Up @@ -336,9 +336,9 @@ bool ttree_view_node::disable_click_dismiss() const
return true;
}

tpoint ttree_view_node::get_current_size() const
tpoint ttree_view_node::get_current_size(bool assume_visible) const
{
if(parent_node_ && parent_node_->is_folded()) {
if(parent_node_ && parent_node_->is_folded() && !assume_visible) {
return tpoint(0, 0);
}

Expand Down Expand Up @@ -396,7 +396,7 @@ tpoint ttree_view_node::get_unfolded_size() const
continue;
}

tpoint node_size = node.get_unfolded_size();
tpoint node_size = node.get_current_size(true);

size.y += node_size.y;
size.x = std::max(size.x, node_size.x);
Expand Down Expand Up @@ -478,6 +478,7 @@ unsigned ttree_view_node::place(const unsigned indention_step_size,

if(!is_root_node()) {
origin.x += indention_step_size;
assert(width >= indention_step_size);
width -= indention_step_size;
}
origin.y += best_size.y;
Expand Down Expand Up @@ -615,7 +616,7 @@ void ttree_view_node::signal_handler_label_left_button_click(
tree_view().selected_item_ = this;

if(tree_view().selection_change_callback_) {
tree_view().selection_change_callback_();
tree_view().selection_change_callback_(tree_view());
}
}
}
Expand Down Expand Up @@ -666,4 +667,28 @@ const std::string& ttree_view_node::get_control_type() const
return type;
}

ttree_view_node& ttree_view_node::get_child_at(int index)
{
assert(static_cast<size_t>(index) < children_.size());
return children_[index];
}

std::vector<int> ttree_view_node::describe_path()
{
if(is_root_node()) {
return std::vector<int>();
}
else {
std::vector<int> res = parent_node_->describe_path();
const boost::ptr_vector<ttree_view_node>& parents_childs = parent_node_->children_;
for(int i = 0, size = parents_childs.size(); i < size; ++i) {
if(&parents_childs[i] == this) {
res.push_back(i);
return res;
}
}
assert(!"tree_view_node was not found in parent nodes children");
throw "assertion ignored"; //To silence 'no return value in this codepath' warning.
}
}
} // namespace gui2
9 changes: 7 additions & 2 deletions src/gui/widgets/tree_view_node.hpp
Expand Up @@ -184,6 +184,11 @@ class ttree_view_node : public twidget

const ttree_view& tree_view() const;

ttree_view_node& get_child_at(int index);
/**
calculates the node indicies that we need to get from the root node to this node.
*/
std::vector<int> describe_path();
private:
/** See @ref twidget::request_reduce_width. */
virtual void request_reduce_width(const unsigned maximum_width) OVERRIDE;
Expand Down Expand Up @@ -240,8 +245,8 @@ class ttree_view_node : public twidget

tpoint calculate_best_size(const int indention_level,
const unsigned indention_step_size) const;

tpoint get_current_size() const;
/** @param assume_visible: if false (default) it will return 0 if the parent node is folded*/
tpoint get_current_size(bool assume_visible = false) const;
tpoint get_folded_size() const;
tpoint get_unfolded_size() const;

Expand Down
79 changes: 79 additions & 0 deletions src/scripting/lua_gui2.cpp
Expand Up @@ -25,6 +25,8 @@
#include "gui/widgets/selectable.hpp" // for tselectable_
#include "gui/widgets/slider.hpp" // for tslider
#include "gui/widgets/text_box.hpp" // for ttext_box
#include "gui/widgets/tree_view.hpp"
#include "gui/widgets/tree_view_node.hpp"
#include "gui/widgets/widget.hpp" // for twidget
#include "gui/widgets/window.hpp" // for twindow

Expand Down Expand Up @@ -152,6 +154,47 @@ static gui2::twidget *find_widget(lua_State *L, int i, bool readonly)
}
w = &l->page_grid(v - 1);
}
else if (gui2::ttree_view *tv = dynamic_cast<gui2::ttree_view *>(w))
{
gui2::ttree_view_node& tvn = tv->get_root_node();
if(lua_isnumber(L, i))
{
int v = lua_tointeger(L, i);
if (v < 1)
goto error_call_destructors_1;
int n = tvn.size();
if (v > n) {
goto error_call_destructors_1;
}
w = &tvn.get_child_at(v - 1);

}
else
{
std::string m = luaL_checkstring(L, i);
w = tvn.find(m, false);
}
}
else if (gui2::ttree_view_node *tvn = dynamic_cast<gui2::ttree_view_node *>(w))
{
if(lua_isnumber(L, i))
{
int v = lua_tointeger(L, i);
if (v < 1)
goto error_call_destructors_1;
int n = tvn->size();
if (v > n) {
goto error_call_destructors_1;
}
w = &tvn->get_child_at(v - 1);

}
else
{
std::string m = luaL_checkstring(L, i);
w = tvn->find(m, false);
}
}
else
{
char const *m = lua_tostring(L, i);
Expand Down Expand Up @@ -291,6 +334,13 @@ int intf_get_dialog_value(lua_State *L)
lua_pushinteger(L, s->get_value());
} else if (gui2::tprogress_bar *p = dynamic_cast<gui2::tprogress_bar *>(w)) {
lua_pushinteger(L, p->get_percentage());
} else if (gui2::ttree_view *tv = dynamic_cast<gui2::ttree_view *>(w)) {
std::vector<int> path = tv->selected_item()->describe_path();
lua_createtable(L, path.size(), 0);
for(size_t i =0; i < path.size(); ++i) {
lua_pushinteger(L, path[i] + 1);
lua_rawseti(L, -2, i + 1);
}
} else
return luaL_argerror(L, lua_gettop(L), "unsupported widget");

Expand Down Expand Up @@ -374,6 +424,9 @@ int intf_set_dialog_callback(lua_State *L)
l->set_callback_value_change(&dialog_callback);
}
#endif
else if (gui2::ttree_view *tv = dynamic_cast<gui2::ttree_view *>(w)) {
tv->set_selection_change_callback(&dialog_callback);
}
else
return luaL_argerror(L, lua_gettop(L), "unsupported widget");

Expand Down Expand Up @@ -456,4 +509,30 @@ int show_gamestate_inspector(CVideo & video, const vconfig & cfg)
return 0;
}

/**
* Sets a widget's state to active or inactive
* - Arg 1: string, the type (id of [node_definition]) of teh new node.
* - Arg 3: integer, where to insert the new node.
* - Args 3..n: path of strings and integers.
*/

int intf_add_dialog_tree_node(lua_State *L)
{
const std::string node_type = luaL_checkstring(L, 1);
const int insert_pos = luaL_checkinteger(L, 2);
static const std::map<std::string, string_map> data;
gui2::twidget *w = find_widget(L, 3, false);
gui2::ttree_view_node *twn = dynamic_cast<gui2::ttree_view_node *>(w);
if (!twn) {
if(gui2::ttree_view* tw = dynamic_cast<gui2::ttree_view *>(w)) {
twn = &tw->get_root_node();
}
else {
return luaL_argerror(L, lua_gettop(L), "unsupported widget");
}
}
twn->add_child(node_type, data, insert_pos);
return 0;
}

} // end namespace lua_gui2
1 change: 1 addition & 0 deletions src/scripting/lua_gui2.hpp
Expand Up @@ -28,6 +28,7 @@ int intf_set_dialog_callback(lua_State *L);
int intf_set_dialog_markup(lua_State *L);
int intf_set_dialog_canvas(lua_State *L);
int intf_set_dialog_active(lua_State *L);
int intf_add_dialog_tree_node(lua_State *L);
int show_dialog(lua_State *L, CVideo & video);
int show_lua_console(lua_State*L, CVideo & video, lua_kernel_base * lk);
int show_gamestate_inspector(CVideo & video, const vconfig & cfg);
Expand Down
1 change: 1 addition & 0 deletions src/scripting/lua_kernel_base.cpp
Expand Up @@ -234,6 +234,7 @@ lua_kernel_base::lua_kernel_base(CVideo * video)
{ "tovconfig", &lua_common::intf_tovconfig },
{ "get_dialog_value", &lua_gui2::intf_get_dialog_value },
{ "set_dialog_active", &lua_gui2::intf_set_dialog_active },
{ "add_dialog_tree_node", &lua_gui2::intf_add_dialog_tree_node },
{ "set_dialog_callback", &lua_gui2::intf_set_dialog_callback },
{ "set_dialog_canvas", &lua_gui2::intf_set_dialog_canvas },
{ "set_dialog_markup", &lua_gui2::intf_set_dialog_markup },
Expand Down

0 comments on commit e4c6c8b

Please sign in to comment.