From: TimePath Date: Sun, 16 Aug 2015 08:37:52 +0000 (+1000) Subject: Create a DataSource class X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=4c523c865b2fd295d6160338260ebb692c12fe90;p=xonotic%2Fxonotic-data.pk3dir.git Create a DataSource class --- diff --git a/qcsrc/menu/classes.inc b/qcsrc/menu/classes.inc index 2e86017f9f..096ff0dd46 100644 --- a/qcsrc/menu/classes.inc +++ b/qcsrc/menu/classes.inc @@ -37,6 +37,7 @@ #include "xonotic/crosshairpicker.qc" #include "xonotic/crosshairpreview.qc" #include "xonotic/cvarlist.qc" +#include "xonotic/datasource.qc" #include "xonotic/demolist.qc" #include "xonotic/dialog.qc" #include "xonotic/dialog_credits.qc" diff --git a/qcsrc/menu/xonotic/datasource.qc b/qcsrc/menu/xonotic/datasource.qc new file mode 100644 index 0000000000..4abea359c4 --- /dev/null +++ b/qcsrc/menu/xonotic/datasource.qc @@ -0,0 +1,12 @@ +#ifndef DATASOURCE_H +#define DATASOURCE_H +.string stringfield_null; +CLASS(DataSource, Object) + ATTRIB(DataSource, entryIcon, .string, stringfield_null) + ATTRIB(DataSource, entryName, .string, stringfield_null) + METHOD(DataSource, getEntry, entity(int)) + METHOD(DataSource, indexOf, int(string)) + METHOD(DataSource, reload, int(string)) + METHOD(DataSource, destroy, void(entity)) +ENDCLASS(DataSource) +#endif diff --git a/qcsrc/menu/xonotic/dialog_media_guide.qc b/qcsrc/menu/xonotic/dialog_media_guide.qc index b5179dea00..f28ad61f8f 100644 --- a/qcsrc/menu/xonotic/dialog_media_guide.qc +++ b/qcsrc/menu/xonotic/dialog_media_guide.qc @@ -1,5 +1,24 @@ #ifndef DIALOG_MEDIA_GUIDE_H #define DIALOG_MEDIA_GUIDE_H +#include "datasource.qc" +CLASS(TopicSource, DataSource) + .string mdl, message; + ATTRIB(TopicSource, entryIcon, .string, mdl) + ATTRIB(TopicSource, entryName, .string, message) + METHOD(TopicSource, getEntry, entity(int)) + METHOD(TopicSource, reload, int(string)) + METHOD(TopicSource, destroy, void(entity)) +ENDCLASS(TopicSource) + +CLASS(MapSource, DataSource) + .string icon, name; + ATTRIB(MapSource, entryIcon, .string, icon) + ATTRIB(MapSource, entryName, .string, name) + METHOD(MapSource, getEntry, entity(int)) + METHOD(MapSource, indexOf, int(string)) + METHOD(MapSource, reload, int(string)) +ENDCLASS(MapSource) + #include "dialog_media_guide_topics.qc" #include "dialog_media_guide_entries.qc" #include "tab.qc" @@ -10,20 +29,13 @@ CLASS(XonoticGuideTab, XonoticTab) METHOD(XonoticGuideTab, topicChangeNotify, void(entity)) METHOD(XonoticGuideTab, topicSelectNotify, void(entity)) - entity Topics_get(int i); - int Topics_reload(string filter); - .string mdl, message; - ATTRIB(XonoticGuideTab, topicList, entity, NEW(XonoticTopicList, Topics_get, func_null, Topics_reload, mdl, message, func_null, this)) + ATTRIB(XonoticGuideTab, topicSource, entity, NEW(TopicSource)) + ATTRIB(XonoticGuideTab, mapSource, entity, NEW(MapSource)) - entity XonoticGuideTab_maps_get(int i); - int XonoticGuideTab_maps_indexOf(string s); - int XonoticGuideTab_maps_reload(string s); - void XonoticGuideTab_maps_destroy(entity this); - .string icon, name; - ATTRIB(XonoticGuideTab, entryList, entity, NEW(XonoticEntryList, XonoticGuideTab_maps_get, XonoticGuideTab_maps_indexOf, XonoticGuideTab_maps_reload, icon, name, XonoticGuideTab_maps_destroy)) + ATTRIB(XonoticGuideTab, topicList, entity, NEW(XonoticTopicList, this.topicSource, "gametype_", this)) + ATTRIB(XonoticGuideTab, entryList, entity, NEW(XonoticEntryList, this.mapSource)) INIT(XonoticGuideTab) { - this.topicList.entryIconPrefix = "gametype_"; this.configureDialog(this); } ENDCLASS(XonoticGuideTab) @@ -67,11 +79,11 @@ void XonoticGuideTab_topicChangeNotify(entity this) void XonoticGuideTab_topicSelectNotify(entity this) { this.setFocus(this, this.entryList); } -entity Topics_get(int i) { return MAPINFO_TYPES[i]; } +entity TopicSource_getEntry(int i) { return MAPINFO_TYPES[i]; } -int Topics_reload(string filter) { return MAPINFO_TYPE_COUNT; } +int TopicSource_reload(string filter) { return MAPINFO_TYPE_COUNT; } -entity XonoticGuideTab_maps_get(int i) +entity MapSource_getEntry(int i) { if (!MapInfo_Get_ByID(i)) return NULL; static entity e; @@ -84,19 +96,19 @@ entity XonoticGuideTab_maps_get(int i) return e; } -int XonoticGuideTab_maps_indexOf(string s) +int MapSource_indexOf(string s) { MapInfo_FindName(s); return MapInfo_FindName_firstResult; } -int XonoticGuideTab_maps_reload(string s) +int MapSource_reload(string s) { MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, 0, 0); if (s) MapInfo_FilterString(s); return MapInfo_count; } -void XonoticGuideTab_maps_destroy(entity this) { MapInfo_Shutdown(); } +void MapSource_destroy(entity this) { MapInfo_Shutdown(); } #endif diff --git a/qcsrc/menu/xonotic/dialog_media_guide_entries.qc b/qcsrc/menu/xonotic/dialog_media_guide_entries.qc index 1c75b8e28b..fed9d5d91e 100644 --- a/qcsrc/menu/xonotic/dialog_media_guide_entries.qc +++ b/qcsrc/menu/xonotic/dialog_media_guide_entries.qc @@ -1,5 +1,6 @@ #ifndef DIALOG_MEDIA_GUIDE_ENTRIES_H #define DIALOG_MEDIA_GUIDE_ENTRIES_H +#include "datasource.qc" #include "listbox.qc" CLASS(XonoticEntryList, XonoticListBox) ATTRIB(XonoticEntryList, alphaBG, float, 0) @@ -23,26 +24,12 @@ CLASS(XonoticEntryList, XonoticListBox) METHOD(XonoticEntryList, refilter, void(entity)) METHOD(XonoticEntryList, resizeNotify, void(entity, vector, vector, vector, vector)) - INIT(XonoticEntryList) { - this.configureXonoticListBox(this); - } + ATTRIB(XonoticEntryList, source, DataSource, NULL) - .string stringfield_null; - ATTRIB(XonoticEntryList, destroy, void(entity), func_null) - ATTRIB(XonoticEntryList, entries, void(int), func_null) - ATTRIB(XonoticEntryList, entryIcon, .string, stringfield_null) - ATTRIB(XonoticEntryList, entryName, .string, stringfield_null) - ATTRIB(XonoticEntryList, indexOf, int(string), func_null) - ATTRIB(XonoticEntryList, reload, int(string), func_null) - - CONSTRUCTOR(XonoticEntryList, entity _entries(int), int _indexOf(string), int _reload(string), .string _entryIcon, .string _entryName, void _destroy(entity)) { + CONSTRUCTOR(XonoticEntryList, DataSource _source) { CONSTRUCT(XonoticEntryList); - this.entries = _entries; - this.indexOf = _indexOf; - this.reload = _reload; - this.entryIcon = _entryIcon; - this.entryName = _entryName; - this.destroy = _destroy; + this.source = _source; + this.configureXonoticListBox(this); this.refilter(this); } @@ -53,7 +40,7 @@ ENDCLASS(XonoticEntryList) void XonoticEntryList_drawListBoxItem(entity this, int i, vector absSize, bool isSelected, bool isFocused) { - entity e = this.entries(i); + entity e = this.source.getEntry(i); if (!e) return; if (isSelected) { @@ -62,8 +49,8 @@ void XonoticEntryList_drawListBoxItem(entity this, int i, vector absSize, bool i this.focusedItemAlpha = getFadedAlpha(this.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED); draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, this.focusedItemAlpha); } - string s = draw_TextShortenToWidth(strdecolorize(e.(this.entryName)), this.columnNameSize, 0, this.realFontSize); - draw_Picture(this.columnPreviewOrigin * eX, e.(this.entryIcon), this.columnPreviewSize * eX + eY, '1 1 1', SKINALPHA_LISTBOX_SELECTED); + string s = draw_TextShortenToWidth(strdecolorize(e.(this.source.entryName)), this.columnNameSize, 0, this.realFontSize); + draw_Picture(this.columnPreviewOrigin * eX, e.(this.source.entryIcon), this.columnPreviewSize * eX + eY, '1 1 1', SKINALPHA_LISTBOX_SELECTED); draw_Text(this.realUpperMargin1 * eY + (this.columnNameOrigin + 0.00 * (this.columnNameSize - draw_TextWidth(s, 0, this.realFontSize))) * eX, s, this.realFontSize, '1 1 1', SKINALPHA_TEXT, 0); } @@ -78,7 +65,7 @@ float XonoticEntryList_keyDown(entity this, float scan, float ascii, float shift this.typeToSearchString = strzone(save); this.typeToSearchTime = time + 0.5; if (strlen(this.typeToSearchString)) { - int idx = this.indexOf(this.typeToSearchString); + int idx = this.source.indexOf(this.typeToSearchString); if (idx >= 0) this.setSelected(this, idx); } } @@ -88,7 +75,7 @@ float XonoticEntryList_keyDown(entity this, float scan, float ascii, float shift if (this.typeToSearchString) strunzone(this.typeToSearchString); this.typeToSearchString = strzone(save); this.typeToSearchTime = time + 0.5; - int idx = this.indexOf(this.typeToSearchString); + int idx = this.source.indexOf(this.typeToSearchString); if (idx >= 0) this.setSelected(this, idx); } else if (shift & S_CTRL && scan == 'f') { this.parent.setFocus(this.parent, this.stringFilterBox); @@ -103,9 +90,9 @@ float XonoticEntryList_keyDown(entity this, float scan, float ascii, float shift void XonoticEntryList_refilter(entity this) { - this.nItems = this.reload(this.stringFilter); + this.nItems = this.source.reload(this.stringFilter); for (int i = 0, n = this.nItems; i < n; ++i) { - draw_PreloadPicture(this.entries(i).(this.entryIcon)); + draw_PreloadPicture(this.source.getEntry(i).(this.source.entryIcon)); } } diff --git a/qcsrc/menu/xonotic/dialog_media_guide_topics.qc b/qcsrc/menu/xonotic/dialog_media_guide_topics.qc index 5624216ddb..8866b6736b 100644 --- a/qcsrc/menu/xonotic/dialog_media_guide_topics.qc +++ b/qcsrc/menu/xonotic/dialog_media_guide_topics.qc @@ -1,5 +1,6 @@ #ifndef DIALOG_MEDIA_GUIDE_TOPICS_H #define DIALOG_MEDIA_GUIDE_TOPICS_H +#include "datasource.qc" #include "listbox.qc" CLASS(XonoticTopicList, XonoticListBox) ATTRIB(XonoticTopicList, columnIconOrigin, float, 0) @@ -16,24 +17,17 @@ CLASS(XonoticTopicList, XonoticListBox) METHOD(XonoticTopicList, resizeNotify, void(entity, vector, vector, vector, vector)) METHOD(XonoticTopicList, setSelected, void(entity, int)) - INIT(XonoticTopicList) { - this.configureXonoticListBox(this); - } - - .string stringfield_null; - ATTRIB(XonoticTopicList, entries, entity(int), func_null) + ATTRIB(XonoticTopicList, source, DataSource, NULL) ATTRIB(XonoticTopicList, entryIconPrefix, string, "") - ATTRIB(XonoticTopicList, entryIcon, .string, stringfield_null) - ATTRIB(XonoticTopicList, entryName, .string, stringfield_null) ATTRIB(XonoticTopicList, listener, entity, NULL) - CONSTRUCTOR(XonoticTopicList, entity(int) _entries, int _indexOf(string), int _reload(string), .string _entryIcon, .string _entryName, void _destroy(entity), entity _listener) { + CONSTRUCTOR(XonoticTopicList, DataSource _source, string _entryIconPrefix, entity _listener) { CONSTRUCT(XonoticTopicList); - this.entries = _entries; - this.nItems = _reload(""); - this.entryIcon = _entryIcon; - this.entryName = _entryName; + this.source = _source; + this.entryIconPrefix = _entryIconPrefix; this.listener = _listener; + this.nItems = _source.reload(""); + this.configureXonoticListBox(this); } ENDCLASS(XonoticTopicList) #endif @@ -53,9 +47,9 @@ void XonoticTopicList_drawListBoxItem(entity this, int i, vector absSize, bool i this.focusedItemAlpha = getFadedAlpha(this.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED); draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, this.focusedItemAlpha); } - entity entry = this.entries(i); - string icon = strcat(this.entryIconPrefix, entry.(this.entryIcon)); - string name = entry.(this.entryName); + entity entry = this.source.getEntry(i); + string icon = strcat(this.entryIconPrefix, entry.(this.source.entryIcon)); + string name = entry.(this.source.entryName); draw_Picture(this.columnIconOrigin * eX, icon, this.columnIconSize * eX + eY, '1 1 1', SKINALPHA_LISTBOX_SELECTED); vector save_fontscale = draw_fontscale; float f = draw_CondensedFontFactor(name, false, this.realFontSize, 1);