]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/datasource.qc
Merge branch 'master' into terencehill/tooltips_cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / datasource.qc
1 #ifndef DATASOURCE_H
2 #define DATASOURCE_H
3 CLASS(DataSource, Object)
4     entity DataSource_true;
5     entity DataSource_false;
6     INIT_STATIC(DataSource) {
7         DataSource_true = NEW(Object);
8         DataSource_false = NULL;
9     }
10     /**
11      * get entry `i` passing `name` and `icon` through `returns` if it is not null
12      * returns `DataSource_false` if out of bounds
13      * otherwise returns an entity or `DataSource_true`
14      */
15     METHOD(DataSource, getEntry, entity(entity this, int i, void(string name, string icon) returns)) { return DataSource_false; }
16     /** return the index of the first match for `find`. optional */
17     METHOD(DataSource, indexOf, int(entity this, string find)) { return -1; }
18     /** reload all entries matching `filter` returning how many matches were found */
19     METHOD(DataSource, reload, int(entity this, string filter)) { return 0; }
20     /** cleanup on shutdown. optional */
21     METHOD(DataSource, destroy, void(entity this)) { }
22 ENDCLASS(DataSource)
23
24
25 CLASS(StringSource, DataSource)
26     ATTRIB(StringSource, StringSource_str, string, string_null)
27     ATTRIB(StringSource, StringSource_sep, string, string_null)
28     CONSTRUCTOR(StringSource, string str, string sep)
29     {
30         CONSTRUCT(StringSource);
31         this.StringSource_str = str;
32         this.StringSource_sep = sep;
33     }
34     METHOD(StringSource, getEntry, entity(entity this, int i, void(string name, string icon) returns))
35     {
36         int n = tokenizebyseparator(this.StringSource_str, this.StringSource_sep);
37         if (i < 0 || i >= n) return DataSource_false;
38         string s = argv(i);
39         if (returns) returns(s, string_null);
40         return DataSource_true;
41     }
42     METHOD(StringSource, reload, int(entity this, string filter))
43     {
44         return tokenizebyseparator(this.StringSource_str, this.StringSource_sep);
45     }
46 ENDCLASS(StringSource)
47
48 CLASS(CvarStringSource, StringSource)
49     ATTRIB(CvarStringSource, CvarStringSource_cvar, string, string_null)
50     CONSTRUCTOR(CvarStringSource, string cv, string sep)
51     {
52         CONSTRUCT(CvarStringSource);
53         this.CvarStringSource_cvar = cv;
54         this.StringSource_sep = sep;
55     }
56     METHOD(CvarStringSource, getEntry, entity(entity this, int i, void(string name, string icon) returns))
57     {
58         string s = this.CvarStringSource_cvar;
59         this.StringSource_str = s ? cvar_string(s) : string_null;
60         return super.getEntry(this, i, returns);
61     }
62     METHOD(CvarStringSource, reload, int(entity this, string filter))
63     {
64         string s = this.CvarStringSource_cvar;
65         this.StringSource_str = s ? cvar_string(s) : string_null;
66         return super.reload(this, filter);
67     }
68 ENDCLASS(CvarStringSource)
69 #endif