Crossfire Client, Trunk  R20996
skills.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, see the
9  * 'LICENSE' and 'COPYING' files.
10  *
11  * The authors can be reached via e-mail to crossfire-devel@real-time.com
12  */
13 
19 #include "client.h"
20 
21 #include <gtk/gtk.h>
22 
23 #include "image.h"
24 #include "metaserver.h"
25 #include "main.h"
26 #include "gtk2proto.h"
27 
29 static GtkListStore *skill_store;
30 static GtkTreeSelection *skill_selection;
31 
33 
34 static int has_init = 0;
35 
46 static gboolean skill_selection_func(GtkTreeSelection *selection,
47  GtkTreeModel *model,
48  GtkTreePath *path,
49  gboolean path_currently_selected,
50  gpointer userdata) {
51  gtk_widget_set_sensitive(skill_ready, TRUE);
52  gtk_widget_set_sensitive(skill_use, TRUE);
53  return TRUE;
54 }
55 
63  GtkTreeIter iter;
64  char buf[MAX_BUF];
65  int i, sk, level;
66  guint64 exp_to_next_level;
67 
68  /* If the window/spellstore hasn't been created, or isn't currently being
69  * shown, return.
70  */
71  if (!has_init ||
72  !gtk_widget_get_visible(
73  GTK_WIDGET(gtk_builder_get_object(dialog_xml, "skill_window")))) {
74  return;
75  }
76 
77  gtk_list_store_clear(skill_store);
78  for (i = 0; i < MAX_SKILL; i++) {
79  sk = skill_mapping[i].value;
80  level = cpl.stats.skill_level[sk];
81  if (level > 0) {
82  gtk_list_store_append(skill_store, &iter);
83  buf[0] = 0;
84  if (level >= exp_table_max) {
85  /* we can't advance any more, so display 0*/
86  exp_to_next_level = 0;
87  } else {
88  exp_to_next_level =
89  exp_table[level + 1] - cpl.stats.skill_exp[sk];
90  }
91  gtk_list_store_set(skill_store, &iter,
92  LIST_NAME, skill_mapping[i].name,
93  LIST_LEVEL, level,
95  LIST_NEXTLEVEL, exp_to_next_level,
96  -1);
97  }
98  }
99 }
100 
106 void on_skills_activate(GtkMenuItem *menuitem, gpointer user_data) {
107  GtkWidget *widget;
108 
109  if (! has_init) {
110  GtkCellRenderer *renderer;
111  GtkTreeViewColumn *column;
112 
113  skill_window = GTK_WIDGET(gtk_builder_get_object(dialog_xml, "skill_window"));
114 
115  skill_use = GTK_WIDGET(gtk_builder_get_object(dialog_xml, "skill_use"));
116  skill_ready = GTK_WIDGET(gtk_builder_get_object(dialog_xml, "skill_ready"));
117  skill_treeview = GTK_WIDGET(gtk_builder_get_object(dialog_xml,
118  "skill_treeview"));
119 
120  g_signal_connect((gpointer) skill_window, "delete_event",
121  G_CALLBACK(gtk_widget_hide_on_delete), NULL);
122  g_signal_connect((gpointer) skill_treeview, "row_activated",
123  G_CALLBACK(on_skill_treeview_row_activated), NULL);
124  g_signal_connect((gpointer) skill_ready, "clicked",
125  G_CALLBACK(on_skill_ready_clicked), NULL);
126  g_signal_connect((gpointer) skill_use, "clicked",
127  G_CALLBACK(on_skill_use_clicked), NULL);
128 
129  widget = GTK_WIDGET(gtk_builder_get_object(dialog_xml, "skill_close"));
130  g_signal_connect((gpointer) widget, "clicked",
131  G_CALLBACK(on_skill_close_clicked), NULL);
132 
133  skill_store = gtk_list_store_new(4,
134  G_TYPE_STRING, /* Name */
135  G_TYPE_INT, /* Level */
136  G_TYPE_INT64, /* EXP */
137  G_TYPE_INT64 /* Exp to Next Level */
138  );
139 
140  gtk_tree_view_set_model(
141  GTK_TREE_VIEW(skill_treeview), GTK_TREE_MODEL(skill_store));
142  gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(skill_treeview), TRUE);
143 
144  renderer = gtk_cell_renderer_text_new();
145  column = gtk_tree_view_column_new_with_attributes(
146  "Skill", renderer, "text", LIST_NAME, NULL);
147  gtk_tree_view_append_column(GTK_TREE_VIEW(skill_treeview), column);
148  gtk_tree_view_column_set_sort_column_id(column, LIST_NAME);
149 
150  renderer = gtk_cell_renderer_text_new();
151  column = gtk_tree_view_column_new_with_attributes(
152  "Level", renderer, "text", LIST_LEVEL, NULL);
153  gtk_tree_view_append_column(GTK_TREE_VIEW(skill_treeview), column);
154  gtk_tree_view_column_set_sort_column_id(column, LIST_LEVEL);
155 
156 
157  renderer = gtk_cell_renderer_text_new();
158  column = gtk_tree_view_column_new_with_attributes(
159  "Exp", renderer, "text", LIST_EXP, NULL);
160  gtk_tree_view_append_column(GTK_TREE_VIEW(skill_treeview), column);
161  gtk_tree_view_column_set_sort_column_id(column, LIST_EXP);
162 
163  renderer = gtk_cell_renderer_text_new();
164  column = gtk_tree_view_column_new_with_attributes(
165  "Needed for next level", renderer, "text",
166  LIST_NEXTLEVEL, NULL);
167  gtk_tree_view_append_column(GTK_TREE_VIEW(skill_treeview), column);
168  gtk_tree_view_column_set_sort_column_id(column, LIST_NEXTLEVEL);
169 
170  skill_selection = gtk_tree_view_get_selection(
171  GTK_TREE_VIEW(skill_treeview));
172  gtk_tree_selection_set_mode(skill_selection, GTK_SELECTION_BROWSE);
173  gtk_tree_selection_set_select_function(
175 
176  gtk_tree_sortable_set_sort_column_id(
177  GTK_TREE_SORTABLE(skill_store), LIST_NAME, GTK_SORT_ASCENDING);
178 
179  }
180  gtk_widget_set_sensitive(skill_ready, FALSE);
181  gtk_widget_set_sensitive(skill_use, FALSE);
182  gtk_widget_show(skill_window);
183 
184  has_init = 1;
185  /* has to be called after has_init is set to 1 */
187 }
188 
197 void trigger_skill(GtkTreeIter iter, GtkTreeModel *model, int use_skill) {
198  gchar *skname;
199  char command[MAX_BUF];
200  char *commandname;
201 
202  gtk_tree_model_get(model, &iter, LIST_NAME, &skname, -1);
203  if (! skname) {
204  LOG(LOG_ERROR, "skills.c::trigger_skill",
205  "Unable to get skill name\n");
206  return;
207  }
208  commandname = use_skill ? "use_skill" : "ready_skill";
209  snprintf(command, MAX_BUF - 1, "%s %s", commandname, skname);
210  send_command(command, -1, 1);
211  g_free(skname);
212 }
213 
221 void on_skill_treeview_row_activated(GtkTreeView *treeview, GtkTreePath *path,
222  GtkTreeViewColumn *column, gpointer user_data) {
223  GtkTreeIter iter;
224  GtkTreeModel *model;
225 
226  model = gtk_tree_view_get_model(treeview);
227  if (gtk_tree_model_get_iter(model, &iter, path)) {
228  trigger_skill(iter, model, 0);
229  }
230  gtk_widget_hide(skill_window);
231 }
232 
238 void on_skill_ready_clicked(GtkButton *button, gpointer user_data) {
239  GtkTreeIter iter;
240  GtkTreeModel *model;
241 
242  if (gtk_tree_selection_get_selected(skill_selection, &model, &iter)) {
243  trigger_skill(iter, model, 0);
244  }
245  gtk_widget_hide(skill_window);
246 }
247 
253 void on_skill_use_clicked(GtkButton *button, gpointer user_data) {
254  GtkTreeIter iter;
255  GtkTreeModel *model;
256 
257  if (gtk_tree_selection_get_selected(skill_selection, &model, &iter)) {
258  trigger_skill(iter, model, 1);
259  }
260 }
261 
267 void on_skill_close_clicked(GtkButton *button, gpointer user_data) {
268  gtk_widget_hide(skill_window);
269 }
270 
void trigger_skill(GtkTreeIter iter, GtkTreeModel *model, int use_skill)
Definition: skills.c:197
int value
Definition: client.h:462
void on_skill_use_clicked(GtkButton *button, gpointer user_data)
Definition: skills.c:253
gint64 skill_exp[MAX_SKILL]
Definition: client.h:290
static GtkWidget * skill_ready
Definition: skills.c:28
#define MAX_SKILL
Definition: client.h:84
static GtkWidget * skill_window
Definition: skills.c:28
static GtkWidget * skill_use
Definition: skills.c:28
Stats stats
Definition: client.h:349
guint64 * exp_table
Definition: client.c:62
static int has_init
Definition: skills.c:34
void LOG(LogLevel level, const char *origin, const char *format,...)
Definition: misc.c:111
static GtkWidget * skill_treeview
Definition: skills.c:28
static gboolean skill_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer userdata)
Definition: skills.c:46
int send_command(const char *command, int repeat, int must_send)
Definition: player.c:156
NameMapping skill_mapping[MAX_SKILL]
Definition: client.c:64
void use_skill(int skill_id)
Definition: commands.c:1329
void on_skill_ready_clicked(GtkButton *button, gpointer user_data)
Definition: skills.c:238
Client_Player cpl
Definition: client.c:66
static GtkListStore * skill_store
Definition: skills.c:29
#define MAX_BUF
Definition: client.h:40
void update_skill_information(void)
Definition: skills.c:62
gint16 skill_level[MAX_SKILL]
Definition: client.h:289
Warning that something definitely didn&#39;t work.
Definition: client.h:444
void on_skill_treeview_row_activated(GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
Definition: skills.c:221
void on_skill_close_clicked(GtkButton *button, gpointer user_data)
Definition: skills.c:267
GtkBuilder * dialog_xml
Definition: main.c:96
guint16 exp_table_max
Definition: client.c:61
static GtkTreeSelection * skill_selection
Definition: skills.c:30
void on_skills_activate(GtkMenuItem *menuitem, gpointer user_data)
Definition: skills.c:106