2 CLASS(XonoticResolutionSlider) EXTENDS(XonoticTextSlider)
3 METHOD(XonoticResolutionSlider, configureXonoticResolutionSlider, void(entity))
4 METHOD(XonoticResolutionSlider, loadResolutions, void(entity, float))
5 METHOD(XonoticResolutionSlider, addResolution, void(entity, float, float, float))
6 METHOD(XonoticResolutionSlider, loadCvars, void(entity))
7 METHOD(XonoticResolutionSlider, saveCvars, void(entity))
8 METHOD(XonoticResolutionSlider, draw, void(entity))
9 ATTRIB(XonoticResolutionSlider, vid_fullscreen, float, -1)
10 ATTRIB(XonoticResolutionSlider, maxAllowedWidth, float, 0)
11 ATTRIB(XonoticResolutionSlider, maxAllowedHeight, float, 0)
12 ENDCLASS(XonoticResolutionSlider)
13 entity makeXonoticResolutionSlider();
14 float updateConwidths(float width, float height, float pixelheight);
19 /* private static */ float XonoticResolutionSlider_DataHasChanged;
21 // Updates cvars (to be called by menu.qc at startup or on detected res change)
22 float updateConwidths(float width, float height, float pixelheight)
25 float minfactor, maxfactor;
28 sz = cvar("menu_vid_scale");
30 return 0; // No recalculation.
32 // Save off current settings.
33 cvar_set("_menu_vid_width", ftos(width));
34 cvar_set("_menu_vid_height", ftos(height));
35 cvar_set("_menu_vid_pixelheight", ftos(pixelheight));
36 cvar_set("_menu_vid_desktopfullscreen", cvar_string("vid_desktopfullscreen"));
42 // calculate the base resolution
45 c_y = c.x * r.y * r.z / r.x;
49 c_x = c.y * r.x / (r.y * r.z);
52 f = min(r.x / c.x, r.y / c.y);
54 c = c * f; // ensures that c_x <= r_x and c_y <= r_y
56 minfactor = min(1, 640 / c.x); // can be > 1 only if c_x is <640
57 maxfactor = max(1, r.x / c.x, r.y / c.y); // can be < 1 only if r_x < c_x and r_y < c_y
58 dprint("min factor: ", ftos(minfactor), "\n");
59 dprint("max factor: ", ftos(maxfactor), "\n");
62 f = 1 - (maxfactor - 1) * sz;
64 f = 1 + (minfactor - 1) * sz;
67 c = c * f; // fteqcc fail
72 // Please reload resolutions list and such stuff.
73 XonoticResolutionSlider_DataHasChanged = true;
75 if (c.x != cvar("vid_conwidth") || c.y != cvar("vid_conheight"))
77 cvar_set("vid_conwidth", ftos(c.x));
78 cvar_set("vid_conheight", ftos(c.y));
83 entity makeXonoticResolutionSlider()
86 me = spawnXonoticResolutionSlider();
87 me.configureXonoticResolutionSlider(me);
90 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight)
92 if (me.maxAllowedWidth && w > me.maxAllowedWidth)
94 if (me.maxAllowedHeight && h > me.maxAllowedHeight)
97 for (i = 0; i < me.nValues; ++i)
99 tokenize_console(me.valueToIdentifier(me, i));
100 if (w > stof(argv(0))) continue;
101 if (w < stof(argv(0))) break;
102 if (h > stof(argv(1))) continue;
103 if (h < stof(argv(1))) break;
104 if (pixelheight > stof(argv(2)) + 0.01) continue;
105 if (pixelheight < stof(argv(2)) - 0.01) break;
106 return; // already there
108 if (pixelheight != 1)
110 float aspect = w / (h * pixelheight);
111 float bestdenom = rint(aspect);
114 for (denom = 2; denom < 10; ++denom) {
115 float num = rint(aspect * denom);
116 if (fabs(num / denom - aspect) < fabs(bestnum / bestdenom - aspect))
122 me.insertValue(me, i, strzone(sprintf(_("%dx%d (%d:%d)"), w, h, bestnum, bestdenom)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
125 me.insertValue(me, i, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
127 float autocvar_menu_vid_allowdualscreenresolution;
128 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
130 me.configureXonoticTextSlider(me, "_menu_vid_width");
131 me.loadResolutions(me, cvar("vid_fullscreen"));
133 void XonoticResolutionSlider_loadResolutions(entity me, float fullscreen)
138 // HACK: text slider assumes the strings are constants, so clearValues
139 // will not unzone them
140 for(i = 0; i < me.nValues; ++i)
142 strunzone(me.valueToIdentifier(me, i));
143 strunzone(me.valueToText(me, i));
145 // NOW we can safely clear.
147 me.maxAllowedWidth = 0;
148 me.maxAllowedHeight = 0;
154 r = getresolution(i);
155 if(r_x == 0 && r_y == 0)
157 if(r.x < 640 || r.y < 480)
159 if(r.x > 2 * r.y) // likely dualscreen resolution, skip this one
160 if(autocvar_menu_vid_allowdualscreenresolution <= 0)
162 me.addResolution(me, r.x, r.y, r.z);
164 r = getresolution(-1);
165 if(r.x != 0 || r.y != 0)
166 me.addResolution(me, r.x, r.y, r.z);
167 dprint("Added system resolutions.\n");
173 r = getresolution(-2);
174 // If workarea is not supported, get desktop size.
175 if(r.x == 0 && r.y == 0)
176 r = getresolution(-1);
178 // Add it, and limit all other resolutions to the workarea/desktop size.
179 if(r.x != 0 || r.y != 0)
181 me.maxAllowedWidth = r.x;
182 me.maxAllowedHeight = r.y;
183 me.addResolution(me, r.x, r.y, r.z);
186 // Add nice hardcoded defaults.
187 me.addResolution(me, 640, 480, 1); // pc res
189 me.addResolution(me, 720, 480, 1.125); // DVD NTSC 4:3
190 me.addResolution(me, 720, 576, 0.9375); // DVD PAL 4:3
191 me.addResolution(me, 720, 480, 0.84375); // DVD NTSC 16:9
192 me.addResolution(me, 720, 576, 0.703125); // DVD PAL 16:9
194 me.addResolution(me, 800, 480, 1); // 480p at 1:1 pixel aspect
195 me.addResolution(me, 800, 600, 1); // pc res
196 me.addResolution(me, 1024, 600, 1); // notebook res
197 me.addResolution(me, 1024, 768, 1); // pc res
198 me.addResolution(me, 1280, 720, 1); // 720p
199 me.addResolution(me, 1280, 960, 1); // pc res
200 me.addResolution(me, 1280, 1024, 1); // pc res
201 me.addResolution(me, 1920, 1080, 1); // 1080p
202 dprint("Added default resolutions.\n");
204 dprint("Total number of resolutions detected: ", ftos(me.nValues), "\n");
206 me.vid_fullscreen = fullscreen;
208 me.configureXonoticTextSliderValues(me);
210 void XonoticResolutionSlider_loadCvars(entity me)
212 me.setValueFromIdentifier(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
214 void XonoticResolutionSlider_saveCvars(entity me)
216 if(me.value >= 0 || me.value < me.nValues)
218 tokenize_console(me.getIdentifier(me));
219 cvar_set("_menu_vid_width", argv(0));
220 cvar_set("_menu_vid_height", argv(1));
221 cvar_set("_menu_vid_pixelheight", argv(2));
222 vector r = getresolution(-1);
223 if (stof(argv(0)) == r.x && stof(argv(1)) == r.y && fabs(stof(argv(2)) - r.z) < 0.01)
224 cvar_set("_menu_vid_desktopfullscreen", "1");
226 cvar_set("_menu_vid_desktopfullscreen", "0");
229 void XonoticResolutionSlider_draw(entity me)
231 if (cvar("vid_fullscreen") != me.vid_fullscreen)
233 me.loadResolutions(me, cvar("vid_fullscreen"));
234 XonoticResolutionSlider_DataHasChanged = true;
236 if (XonoticResolutionSlider_DataHasChanged)
238 XonoticResolutionSlider_DataHasChanged = false;
241 SUPER(XonoticResolutionSlider).draw(me);