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