From cbbcea532885948c6563ac72369e1dc3f9680165 Mon Sep 17 00:00:00 2001 From: TimePath Date: Sun, 16 Aug 2015 20:05:39 +1000 Subject: [PATCH] Support switching between data sources --- qcsrc/menu/xonotic/datasource.qc | 3 +- qcsrc/menu/xonotic/dialog_media_guide.qc | 61 +++++++++++++++---- .../xonotic/dialog_media_guide_entries.qc | 15 +++-- .../menu/xonotic/dialog_media_guide_topics.qc | 6 +- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/qcsrc/menu/xonotic/datasource.qc b/qcsrc/menu/xonotic/datasource.qc index 4abea359c4..46e5a287c4 100644 --- a/qcsrc/menu/xonotic/datasource.qc +++ b/qcsrc/menu/xonotic/datasource.qc @@ -2,8 +2,9 @@ #define DATASOURCE_H .string stringfield_null; CLASS(DataSource, Object) - ATTRIB(DataSource, entryIcon, .string, stringfield_null) ATTRIB(DataSource, entryName, .string, stringfield_null) + ATTRIB(DataSource, entryIcon, .string, stringfield_null) + ATTRIB(DataSource, entryIconPrefix, string, "") METHOD(DataSource, getEntry, entity(int)) METHOD(DataSource, indexOf, int(string)) METHOD(DataSource, reload, int(string)) diff --git a/qcsrc/menu/xonotic/dialog_media_guide.qc b/qcsrc/menu/xonotic/dialog_media_guide.qc index f28ad61f8f..929cc20151 100644 --- a/qcsrc/menu/xonotic/dialog_media_guide.qc +++ b/qcsrc/menu/xonotic/dialog_media_guide.qc @@ -1,15 +1,49 @@ #ifndef DIALOG_MEDIA_GUIDE_H #define DIALOG_MEDIA_GUIDE_H #include "datasource.qc" + +#define TOPICS(X) \ + X(NEW(GametypeSource), _("Gametypes"), "gametype_dm") \ + X(NEW(MapSource), _("Maps"), "gametype_ctf") \ + /**/ CLASS(TopicSource, DataSource) - .string mdl, message; - ATTRIB(TopicSource, entryIcon, .string, mdl) - ATTRIB(TopicSource, entryName, .string, message) + .string icon, name; + ATTRIB(TopicSource, entryIcon, .string, icon) + ATTRIB(TopicSource, entryName, .string, name) METHOD(TopicSource, getEntry, entity(int)) + entity TopicSource_getEntry(int i) { + static entity e; + if (!e) e = new(entry); + int idx = 0; + .string en = name, ei = icon; + #define TOPIC(src, name, icon) if (idx++ == i) { e.(en) = name; e.(ei) = icon; return e; } + TOPICS(TOPIC) + #undef TOPIC + e.en = "undefined"; e.ei = "undefined"; + return e; + } METHOD(TopicSource, reload, int(string)) - METHOD(TopicSource, destroy, void(entity)) + int TopicSource_reload(string filter) { + int i = 0; + #define TOPIC(src, name, icon) i++; + TOPICS(TOPIC) + #undef TOPIC + return i; + } ENDCLASS(TopicSource) +#include "../../common/mapinfo.qh" +CLASS(GametypeSource, DataSource) + .string mdl, message; + ATTRIB(GametypeSource, entryName, .string, message) + ATTRIB(GametypeSource, entryIcon, .string, mdl) + ATTRIB(GametypeSource, entryIconPrefix, string, "gametype_") + METHOD(GametypeSource, getEntry, entity(int)) + entity GametypeSource_getEntry(int i) { return MAPINFO_TYPES[i]; } + METHOD(GametypeSource, reload, int(string)) + int GametypeSource_reload(string filter) { return MAPINFO_TYPE_COUNT; } +ENDCLASS(GametypeSource) + CLASS(MapSource, DataSource) .string icon, name; ATTRIB(MapSource, entryIcon, .string, icon) @@ -29,11 +63,8 @@ CLASS(XonoticGuideTab, XonoticTab) METHOD(XonoticGuideTab, topicChangeNotify, void(entity)) METHOD(XonoticGuideTab, topicSelectNotify, void(entity)) - ATTRIB(XonoticGuideTab, topicSource, entity, NEW(TopicSource)) - ATTRIB(XonoticGuideTab, mapSource, entity, NEW(MapSource)) - - ATTRIB(XonoticGuideTab, topicList, entity, NEW(XonoticTopicList, this.topicSource, "gametype_", this)) - ATTRIB(XonoticGuideTab, entryList, entity, NEW(XonoticEntryList, this.mapSource)) + ATTRIB(XonoticGuideTab, topicList, entity, NEW(XonoticTopicList, NEW(TopicSource), this)) + ATTRIB(XonoticGuideTab, entryList, entity, NEW(XonoticEntryList, NULL)) INIT(XonoticGuideTab) { this.configureDialog(this); @@ -73,16 +104,20 @@ void XonoticGuideTab_fill(entity this) void XonoticGuideTab_topicChangeNotify(entity this) { + entity topics = this.topicList; entity entries = this.entryList; + int i = topics.selectedItem; + int idx = 0; + entity found = NULL; + #define TOPIC(src, name, icon) if (idx++ == i) { static entity e; if (!e) e = src; found = e; break; } + do { TOPICS(TOPIC); } while (0); + #undef TOPIC + entries.source = found; entries.refilter(entries); } void XonoticGuideTab_topicSelectNotify(entity this) { this.setFocus(this, this.entryList); } -entity TopicSource_getEntry(int i) { return MAPINFO_TYPES[i]; } - -int TopicSource_reload(string filter) { return MAPINFO_TYPE_COUNT; } - entity MapSource_getEntry(int i) { if (!MapInfo_Get_ByID(i)) return NULL; diff --git a/qcsrc/menu/xonotic/dialog_media_guide_entries.qc b/qcsrc/menu/xonotic/dialog_media_guide_entries.qc index fed9d5d91e..eede28ba3f 100644 --- a/qcsrc/menu/xonotic/dialog_media_guide_entries.qc +++ b/qcsrc/menu/xonotic/dialog_media_guide_entries.qc @@ -40,8 +40,9 @@ ENDCLASS(XonoticEntryList) void XonoticEntryList_drawListBoxItem(entity this, int i, vector absSize, bool isSelected, bool isFocused) { - entity e = this.source.getEntry(i); - if (!e) return; + if (!this.source) return; + entity entry = this.source.getEntry(i); + if (!entry) return; if (isSelected) { draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED); @@ -49,8 +50,10 @@ 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.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); + string icon = strcat(this.source.entryIconPrefix, entry.(this.source.entryIcon)); + string name = entry.(this.source.entryName); + string s = draw_TextShortenToWidth(strdecolorize(name), this.columnNameSize, 0, this.realFontSize); + draw_Picture(this.columnPreviewOrigin * eX, icon, 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); } @@ -90,6 +93,10 @@ float XonoticEntryList_keyDown(entity this, float scan, float ascii, float shift void XonoticEntryList_refilter(entity this) { + if (!this.source) { + this.nItems = 0; + return; + } this.nItems = this.source.reload(this.stringFilter); for (int i = 0, n = this.nItems; i < n; ++i) { 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 8866b6736b..b3845c67bf 100644 --- a/qcsrc/menu/xonotic/dialog_media_guide_topics.qc +++ b/qcsrc/menu/xonotic/dialog_media_guide_topics.qc @@ -18,13 +18,11 @@ CLASS(XonoticTopicList, XonoticListBox) METHOD(XonoticTopicList, setSelected, void(entity, int)) ATTRIB(XonoticTopicList, source, DataSource, NULL) - ATTRIB(XonoticTopicList, entryIconPrefix, string, "") ATTRIB(XonoticTopicList, listener, entity, NULL) - CONSTRUCTOR(XonoticTopicList, DataSource _source, string _entryIconPrefix, entity _listener) { + CONSTRUCTOR(XonoticTopicList, DataSource _source, entity _listener) { CONSTRUCT(XonoticTopicList); this.source = _source; - this.entryIconPrefix = _entryIconPrefix; this.listener = _listener; this.nItems = _source.reload(""); this.configureXonoticListBox(this); @@ -48,7 +46,7 @@ void XonoticTopicList_drawListBoxItem(entity this, int i, vector absSize, bool i draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, this.focusedItemAlpha); } entity entry = this.source.getEntry(i); - string icon = strcat(this.entryIconPrefix, entry.(this.source.entryIcon)); + string icon = strcat(this.source.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; -- 2.39.2