2002-08-16 21:54:21 +00:00
|
|
|
/*
|
|
|
|
options_util.qc
|
|
|
|
|
|
|
|
Utilities for the options menu
|
|
|
|
|
|
|
|
Copyright (C) 2002 Robin Redeker <elmex@x-paste.de>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public
|
|
|
|
License along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
2004-02-07 00:09:50 +00:00
|
|
|
#include "cvar.h"
|
2002-08-16 21:54:21 +00:00
|
|
|
#include "draw.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
2004-02-04 02:35:57 +00:00
|
|
|
#include "gui/Text.h"
|
|
|
|
#include "gui/Rect.h"
|
|
|
|
#include "gui/Slider.h"
|
|
|
|
#include "options_util.h"
|
|
|
|
|
2004-02-07 00:09:50 +00:00
|
|
|
@implementation CvarToggle
|
|
|
|
|
|
|
|
-(id)initWithCvar:(string)cvname
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
name = str_new ();
|
|
|
|
str_copy (name, cvname);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)toggle
|
|
|
|
{
|
|
|
|
Cvar_Toggle (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)value
|
|
|
|
{
|
|
|
|
return cvar (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CvarToggleView
|
|
|
|
|
|
|
|
-(void)update
|
|
|
|
{
|
|
|
|
[value setText:[toggle value] ? "On" : "Off"];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id)initWithBounds:(Rect)aRect title:(string)_title :(CvarToggle)_toggle
|
|
|
|
{
|
|
|
|
local Rect rect;
|
|
|
|
|
|
|
|
self = [super initWithBounds:aRect];
|
|
|
|
|
|
|
|
toggle = _toggle;
|
|
|
|
|
|
|
|
rect = [[Rect alloc] initWithComponents:0 :0 :strlen (_title) * 8 :8];
|
|
|
|
title = [[Text alloc] initWithBounds:rect text:_title];
|
|
|
|
|
|
|
|
rect.size.width = 3 * 8;
|
|
|
|
rect.origin.x = xlen - rect.size.width;
|
|
|
|
value = [[Text alloc] initWithBounds:rect];
|
|
|
|
|
|
|
|
[self addView:title];
|
|
|
|
[self addView:value];
|
|
|
|
|
|
|
|
[self update];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)toggle
|
|
|
|
{
|
|
|
|
[toggle toggle];
|
|
|
|
[self update];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2004-02-04 02:35:57 +00:00
|
|
|
@implementation CvarRange
|
|
|
|
|
2004-02-04 06:56:53 +00:00
|
|
|
-(id)initWithCvar:(string)cvname min:(float)_min max:(float)_max step:(float)_step
|
|
|
|
{
|
2004-02-07 00:09:50 +00:00
|
|
|
self = [super init];
|
|
|
|
|
2004-02-04 06:56:53 +00:00
|
|
|
name = str_new ();
|
|
|
|
str_copy (name, cvname);
|
|
|
|
min = _min;
|
|
|
|
max = _max;
|
|
|
|
step = _step;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)inc
|
2004-02-04 02:35:57 +00:00
|
|
|
{
|
|
|
|
local float val = cvar (name);
|
2004-02-04 06:56:53 +00:00
|
|
|
val = min_max_cnt (min, max, step, val, 1);
|
|
|
|
cvar_set (name, ftos (val));
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)dec
|
|
|
|
{
|
|
|
|
local float val = cvar (name);
|
|
|
|
val = min_max_cnt (min, max, step, val, 0);
|
|
|
|
cvar_set (name, ftos (val));
|
|
|
|
}
|
|
|
|
|
|
|
|
-(float)value
|
|
|
|
{
|
|
|
|
return cvar (name);
|
2004-02-04 02:35:57 +00:00
|
|
|
}
|
|
|
|
|
2004-02-04 06:56:53 +00:00
|
|
|
-(integer)percentage
|
|
|
|
{
|
|
|
|
return to_percentage(min, max, cvar (name));
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2004-02-07 00:09:50 +00:00
|
|
|
@implementation CvarRangeView
|
2004-02-04 06:56:53 +00:00
|
|
|
|
|
|
|
-(void)update
|
|
|
|
{
|
|
|
|
[slider setIndex:[range percentage]];
|
|
|
|
[value setText:ftos ([range value])];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id)initWithBounds:(Rect)aRect title:(string)_title sliderWidth:(integer)width :(CvarRange)_range
|
2004-02-04 02:35:57 +00:00
|
|
|
{
|
|
|
|
local Rect rect;
|
|
|
|
|
|
|
|
self = [super initWithBounds:aRect];
|
2004-02-04 06:56:53 +00:00
|
|
|
|
|
|
|
range = _range;
|
2004-02-04 02:35:57 +00:00
|
|
|
|
2004-02-04 05:49:54 +00:00
|
|
|
rect = [[Rect alloc] initWithComponents:0 :0 :strlen (_title) * 8 :8];
|
2004-02-04 02:35:57 +00:00
|
|
|
title = [[Text alloc] initWithBounds:rect text:_title];
|
|
|
|
|
|
|
|
rect.origin.x += rect.size.width + 8;
|
2004-02-04 06:56:53 +00:00
|
|
|
rect.size.width = width;
|
|
|
|
if (rect.origin.x + rect.size.width > xlen)
|
|
|
|
rect.size.width = xlen - rect.origin.x;
|
2004-02-04 02:35:57 +00:00
|
|
|
slider = [[Slider alloc] initWithBounds:rect size:100];
|
|
|
|
|
|
|
|
rect.origin.x += rect.size.width + 8;
|
|
|
|
rect.size.width = xlen - rect.origin.x;
|
|
|
|
value = [[Text alloc] initWithBounds:rect];
|
|
|
|
|
|
|
|
[self addView:title];
|
|
|
|
[self addView:slider];
|
|
|
|
[self addView:value];
|
|
|
|
|
|
|
|
[self update];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)inc
|
|
|
|
{
|
2004-02-04 06:56:53 +00:00
|
|
|
[range inc];
|
2004-02-04 02:35:57 +00:00
|
|
|
[self update];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)dec
|
|
|
|
{
|
2004-02-04 06:56:53 +00:00
|
|
|
[range dec];
|
2004-02-04 02:35:57 +00:00
|
|
|
[self update];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2002-08-16 21:54:21 +00:00
|
|
|
/*
|
|
|
|
opt_cursor
|
|
|
|
|
|
|
|
function for drawing the cursor
|
|
|
|
*/
|
2003-03-03 05:24:45 +00:00
|
|
|
void (integer x, integer y)
|
2002-08-16 21:54:21 +00:00
|
|
|
opt_cursor =
|
|
|
|
{
|
|
|
|
// use time becaus we want a nice rotaing cursor
|
2002-11-01 01:39:36 +00:00
|
|
|
Draw_Character (x, y, 12 + (integer) (time * 4) & 1);
|
2002-08-16 21:54:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
draw_item
|
|
|
|
|
2003-03-03 05:24:45 +00:00
|
|
|
Draws a item with a specific spacing between label and value to
|
|
|
|
position x, y.
|
2002-08-16 21:54:21 +00:00
|
|
|
Used as helper function for draw_val_item.
|
|
|
|
*/
|
2003-03-03 05:24:45 +00:00
|
|
|
void (integer x, integer y, integer spacing, string spacechar,
|
|
|
|
string label, string valstr)
|
2002-08-16 21:54:21 +00:00
|
|
|
draw_item =
|
|
|
|
{
|
2003-03-03 05:24:45 +00:00
|
|
|
local integer i;
|
|
|
|
|
2002-08-16 21:54:21 +00:00
|
|
|
Draw_String (x, y, label);
|
2003-04-08 02:35:09 +00:00
|
|
|
for (i = x + (integer) strlen (label) * 8; i < (x+spacing); i += 8) {
|
2002-08-16 21:54:21 +00:00
|
|
|
Draw_String (i, y, spacechar);
|
|
|
|
}
|
|
|
|
Draw_String (x + spacing, y, valstr);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
draw_val_item
|
|
|
|
|
|
|
|
Draws a nice menu item.
|
|
|
|
Use this function for a consistent look of menu items!
|
|
|
|
Example:
|
|
|
|
<Label>.....:<valstr>
|
|
|
|
spacing are the number of the points to put
|
|
|
|
*/
|
|
|
|
void (integer x, integer y, integer spacing, string label, string valstr)
|
|
|
|
draw_val_item =
|
|
|
|
{
|
|
|
|
draw_item (x, y, spacing, ".", label, ":" + valstr);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
to_percentage
|
|
|
|
|
|
|
|
Calculates the percentage of a value relative
|
|
|
|
to a min and max value.
|
|
|
|
*/
|
|
|
|
integer (float min, float max, float val)
|
|
|
|
to_percentage =
|
|
|
|
{
|
2003-03-03 05:24:45 +00:00
|
|
|
local float max_v = (max - min);
|
|
|
|
local integer perc;
|
|
|
|
|
2002-08-16 21:54:21 +00:00
|
|
|
val -= min;
|
|
|
|
|
2003-03-03 05:24:45 +00:00
|
|
|
if (val > max_v) {
|
2002-08-16 21:54:21 +00:00
|
|
|
val = max_v;
|
|
|
|
}
|
2003-03-03 05:24:45 +00:00
|
|
|
if (val < 0) {
|
2002-08-16 21:54:21 +00:00
|
|
|
val = 0;
|
|
|
|
}
|
2004-02-03 05:59:51 +00:00
|
|
|
perc = (integer) ((val / max_v) * 100);
|
2002-08-16 21:54:21 +00:00
|
|
|
|
|
|
|
return perc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
min_max_cnt
|
|
|
|
|
2003-03-03 05:24:45 +00:00
|
|
|
Increases or decreases a value by take care of the bordervalues.
|
2002-08-16 21:54:21 +00:00
|
|
|
min, max are the borders.
|
|
|
|
step is the step by in-/de-creasing.
|
2003-03-03 05:24:45 +00:00
|
|
|
cntflag should be true for increasing and false for decreasing
|
2002-08-16 21:54:21 +00:00
|
|
|
*/
|
2003-03-03 05:24:45 +00:00
|
|
|
float (float min, float max, float step, float val, integer cntflag)
|
2002-08-16 21:54:21 +00:00
|
|
|
min_max_cnt =
|
|
|
|
{
|
2003-03-03 05:24:45 +00:00
|
|
|
if (cntflag) {
|
2002-08-16 21:54:21 +00:00
|
|
|
val += step;
|
|
|
|
} else {
|
|
|
|
val -= step;
|
|
|
|
}
|
|
|
|
|
2003-03-03 05:24:45 +00:00
|
|
|
if (val > max) {
|
2002-08-16 21:54:21 +00:00
|
|
|
val = max;
|
2003-03-03 05:24:45 +00:00
|
|
|
} else if (val < min) {
|
2002-08-16 21:54:21 +00:00
|
|
|
val = min;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
};
|