Updated the widget class a bit, the slider is now useing it as well

changed the source code to reflect the changes.
This commit is contained in:
uid68698 2004-02-22 07:13:49 +00:00
parent 323e7f6791
commit 41b9ae5795
12 changed files with 249 additions and 282 deletions

View file

@ -43,4 +43,4 @@ std::string default_generate_map(size_t width, size_t height,
size_t max_lakes, size_t nvillages, size_t nplayers,
const config& cfg);
#endif
#endif

View file

@ -114,7 +114,10 @@ void default_map_generator::user_config(display& disp)
const int slider_left = text_right + 10;
const int slider_right = xpos + width - horz_margin - right_space;
SDL_Rect slider_rect = { slider_left,players_rect.y,slider_right-slider_left,players_rect.h};
gui::slider players_slider(disp,slider_rect,gui::slider::normalize(nplayers_,2,max_players));
gui::slider players_slider(disp,slider_rect);
players_slider.set_min(2);
players_slider.set_max(max_players);
players_slider.set_value(2);
const int min_width = 20;
const int max_width = 200;
@ -122,28 +125,43 @@ void default_map_generator::user_config(display& disp)
const int max_height = 200;
slider_rect.y = width_rect.y;
gui::slider width_slider(disp,slider_rect,gui::slider::normalize(width_,min_height,max_height));
gui::slider width_slider(disp,slider_rect);
width_slider.set_min(min_width);
width_slider.set_max(max_width);
width_slider.set_value(width_);
slider_rect.y = height_rect.y;
gui::slider height_slider(disp,slider_rect,gui::slider::normalize(height_,min_width,max_width));
gui::slider height_slider(disp,slider_rect);
height_slider.set_min(min_height);
height_slider.set_max(max_height);
height_slider.set_value(height_);
const int min_iterations = 10;
const int max_iterations = 3000;
slider_rect.y = iterations_rect.y;
gui::slider iterations_slider(disp,slider_rect,gui::slider::normalize(iterations_,min_iterations,max_iterations));
gui::slider iterations_slider(disp,slider_rect);
iterations_slider.set_min(min_iterations);
iterations_slider.set_max(max_iterations);
iterations_slider.set_value(iterations_);
const int min_hillsize = 1;
const int max_hillsize = 50;
slider_rect.y = hillsize_rect.y;
gui::slider hillsize_slider(disp,slider_rect,gui::slider::normalize(hill_size_,min_hillsize,max_hillsize));
gui::slider hillsize_slider(disp,slider_rect);
hillsize_slider.set_min(min_hillsize);
hillsize_slider.set_max(max_hillsize);
hillsize_slider.set_value(hill_size_);
const int min_villages = 10;
const int max_villages = 10000;
slider_rect.y = villages_rect.y;
gui::slider villages_slider(disp,slider_rect,gui::slider::normalize(nvillages_,min_villages,max_villages));
gui::slider villages_slider(disp,slider_rect);
villages_slider.set_min(min_villages);
villages_slider.set_max(max_villages);
villages_slider.set_value(nvillages_);
for(bool draw = true;; draw = false) {
int mousex, mousey;
@ -155,44 +173,19 @@ void default_map_generator::user_config(display& disp)
break;
}
const double new_players = players_slider.process(mousex,mousey,left_button);
if(new_players >= 0.0) {
nplayers_ = gui::slider::denormalize(new_players,2,max_players);
std::cerr << "set players to " << nplayers_ << "," << new_players << "\n";
draw = true;
}
players_slider.process();
width_slider.process();
height_slider.process();
iterations_slider.process();
hillsize_slider.process();
villages_slider.process();
const double new_width = width_slider.process(mousex,mousey,left_button);
if(new_width >= 0.0) {
width_ = gui::slider::denormalize(new_width,min_width,max_width);
draw = true;
}
const double new_height = height_slider.process(mousex,mousey,left_button);
if(new_height >= 0.0) {
height_ = gui::slider::denormalize(new_height,min_height,max_height);
draw = true;
}
const double new_iterations = iterations_slider.process(mousex,mousey,left_button);
if(new_iterations >= 0.0) {
iterations_ = gui::slider::denormalize(new_iterations,min_iterations,max_iterations);
draw = true;
}
const double new_hillsize = hillsize_slider.process(mousex,mousey,left_button);
if(new_hillsize >= 0.0) {
hill_size_ = gui::slider::denormalize(new_hillsize,min_hillsize,max_hillsize);
draw = true;
}
const double new_villages = villages_slider.process(mousex,mousey,left_button);
if(new_villages >= 0.0) {
nvillages_ = gui::slider::denormalize(new_villages,min_villages,max_villages);
draw = true;
}
if(draw) {
nplayers_ = players_slider.value();
width_ = width_slider.value();
height_ = height_slider.value();
iterations_ = iterations_slider.value();
hill_size_ = hillsize_slider.value();
nvillages_ = villages_slider.value();
gui::draw_dialog_frame(xpos,ypos,width,height,disp);
@ -221,16 +214,9 @@ void default_map_generator::user_config(display& disp)
font::draw_text(&disp,disp.screen_area(),14,font::NORMAL_COLOUR,height_str.str(),
slider_right+horz_margin,height_rect.y);
players_slider.draw();
width_slider.draw();
height_slider.draw();
iterations_slider.draw();
hillsize_slider.draw();
villages_slider.draw();
close_button.draw();
update_rect(xpos,ypos,width,height);
}
disp.update_display();
SDL_Delay(10);
@ -246,4 +232,4 @@ std::string default_map_generator::create_map(const std::vector<std::string>& ar
return default_generate_map(width_,height_,iterations_,hill_size_,max_lakes_,nvillages_,nplayers_,*cfg_);
else
return "";
}
}

View file

@ -123,7 +123,10 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
rect.y = (disp.y()-height)/2+100;
rect.h = name_entry.location().w;
gui::slider turns_slider(disp,rect,0.38);
gui::slider turns_slider(disp,rect);
turns_slider.set_min(20);
turns_slider.set_max(100);
turns_slider.set_value(50);
//Village Gold
rect.x = (disp.x()-width)/2+(int)(width*0.4)+maps_menu.width()+19;
@ -134,7 +137,10 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
string_table["village_gold"] + ": 1",rect.x,rect.y);
rect.y = (disp.y()-height)/2+147;
rect.h = name_entry.location().w;
gui::slider villagegold_slider(disp,rect,0.0);
gui::slider villagegold_slider(disp,rect);
villagegold_slider.set_min(1);
villagegold_slider.set_max(10);
villagegold_slider.set_value(1);
//FOG of war
gui::button fog_game(disp,string_table["fog_of_war"],gui::button::TYPE_CHECK);
@ -188,6 +194,8 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
const bool left_button = mouse_flags&SDL_BUTTON_LMASK;
name_entry.process();
turns_slider.process();
villagegold_slider.process();
maps_menu.process(mousex,mousey,left_button,
key[SDLK_UP],key[SDLK_DOWN],
@ -199,7 +207,6 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
if(launch_game.process(mousex,mousey,left_button)) {
if(name_entry.text().empty() == false) {
//Connector
name_entry.set_focus(false);
mp_connect connector(disp, name_entry.text(), cfg, units_data, state);
const int res = connector.load_map(maps_menu.selection(), cur_turns, cur_villagegold, fog_game.checked(), shroud_game.checked());
@ -208,6 +215,7 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
const network::manager net_manager;
const network::server_manager server_man(15000,server);
name_entry.set_focus(false);
connector.gui_do();
return -1;
} else {
@ -226,50 +234,41 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
}
fog_game.process(mousex,mousey,left_button);
fog_game.draw();
shroud_game.process(mousex,mousey,left_button);
shroud_game.draw();
observers_game.process(mousex,mousey,left_button);
observers_game.draw();
events::raise_process_event();
events::raise_draw_event();
//Game turns are 20 to 99
//FIXME: Should never be a - number, but it is sometimes
int check_turns=20+int(79*turns_slider.process(mousex,mousey,left_button));
if(abs(check_turns) == check_turns)
new_turns=check_turns;
if(new_turns != cur_turns) {
cur_turns = new_turns;
rect.x = (disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19;
rect.y = (disp.y()-height)/2+83;
rect.w = ((disp.x()-width)/2+width)-((disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19)-10;
rect.h = 12;
SDL_BlitSurface(village_bg, NULL, disp.video().getSurface(), &rect);
sprintf(buf,"Turns: %d", cur_turns);
font::draw_text(&disp,disp.screen_area(),12,font::GOOD_COLOUR,
buf,rect.x,rect.y);
update_rect(rect);
}
//Turns per game
cur_turns = turns_slider.value();
rect.x = (disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19;
rect.y = (disp.y()-height)/2+83;
rect.w = ((disp.x()-width)/2+width)-((disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19)-10;
rect.h = 12;
SDL_BlitSurface(village_bg, NULL, disp.video().getSurface(), &rect);
sprintf(buf,"Turns: %d", cur_turns);
font::draw_text(&disp,disp.screen_area(),12,font::GOOD_COLOUR,
buf,rect.x,rect.y);
update_rect(rect);
//work out if we have to generate a new map
bool map_changed = false;
//Villages can produce between 1 and 10 gold a turn
//FIXME: Should never be a - number, but it is sometimes
int check_villagegold=1+int(9*villagegold_slider.process(mousex,mousey,left_button));
if(abs(check_villagegold) == check_villagegold)
new_villagegold=check_villagegold;
if(new_villagegold != cur_villagegold) {
cur_villagegold = new_villagegold;
rect.x = (disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19;
rect.y = (disp.y()-height)/2+130;
rect.w = ((disp.x()-width)/2+width)-((disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19)-10;
rect.h = 12;
SDL_BlitSurface(village_bg, NULL, disp.video().getSurface(), &rect);
sprintf(buf,": %d", cur_villagegold);
font::draw_text(&disp,disp.screen_area(),12,font::GOOD_COLOUR,
string_table["village_gold"] + buf,rect.x,rect.y);
update_rect(rect);
}
cur_villagegold = villagegold_slider.value();
rect.x = (disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19;
rect.y = (disp.y()-height)/2+130;
rect.w = ((disp.x()-width)/2+width)-((disp.x()-width)/2+int(width*0.4)+maps_menu.width()+19)-10;
rect.h = 12;
SDL_BlitSurface(village_bg, NULL, disp.video().getSurface(), &rect);
sprintf(buf,": %d", cur_villagegold);
font::draw_text(&disp,disp.screen_area(),12,font::GOOD_COLOUR,
string_table["village_gold"] + buf,rect.x,rect.y);
update_rect(rect);
if(maps_menu.selection() != cur_selection) {
map_changed = true;

View file

@ -401,7 +401,10 @@ void mp_connect::gui_init()
rect.y = (disp_->y()-height_)/2+55+(30*side_num);
rect.w = launch_.width()-5;
rect.h = launch_.height();
sliders_gold_.push_back(gui::slider(*disp_, rect, 0.0+((80.0)/979.0)));
sliders_gold_.push_back(gui::slider(*disp_, rect));
sliders_gold_.back().set_min(20);
sliders_gold_.back().set_max(1000);
sliders_gold_.back().set_value(100);
rect.w = 30;
rect.x = (disp_->x()-width_)/2+603;
gold_bg_.push_back(surface_restorer(&disp_->video(),rect));
@ -470,7 +473,7 @@ void mp_connect::gui_update()
//Player Gold
std::string str = side["gold"];
sliders_gold_[n].set_value((atoi(str.c_str()) - 20 + 0.0) / 979.0);
sliders_gold_[n].set_value(atoi(str.c_str()));
rect.x = (disp_->x() - width_) / 2 + 603;
rect.y = (disp_->y() - height_) / 2 + 55 + (30 * n);
rect.w = 30;
@ -563,15 +566,13 @@ int mp_connect::gui_do()
network::send_data(*level_);
}
sliders_gold_[n].process();
if(!save_){
int check_playergold = 20 + int(979 *
sliders_gold_[n].process(mousex, mousey, left_button));
if(abs(check_playergold) == check_playergold)
new_playergold = check_playergold;
if(new_playergold != cur_playergold) {
cur_playergold = new_playergold;
std::stringstream playergold;
playergold << cur_playergold;
cur_playergold = sliders_gold_[n].value();
std::stringstream playergold;
playergold << cur_playergold;
if (side["gold"] != playergold.str())
{
side["gold"] = playergold.str();
rect.x = (disp_->x() - width_) / 2 + 603;
rect.y = (disp_->y() - height_) / 2 + 55 + (30 * n);
@ -585,8 +586,6 @@ int mp_connect::gui_do()
update_rect(rect);
network::send_data(*level_);
}
}else{
sliders_gold_[n].draw();
}
}

View file

@ -171,42 +171,42 @@ void set_locale(const std::string& s)
prefs["locale"] = s;
}
double music_volume()
int music_volume()
{
static const double default_value = 1.0;
static const int default_value = 100;
const string_map::const_iterator volume = prefs.values.find("music_volume");
if(volume != prefs.values.end() && volume->second.empty() == false)
return atof(volume->second.c_str());
return atoi(volume->second.c_str());
else
return default_value;
}
void set_music_volume(double vol)
void set_music_volume(int vol)
{
std::stringstream stream;
stream << vol;
prefs["music_volume"] = stream.str();
sound::set_music_volume(vol);
sound::set_music_volume(vol / 100.0);
}
double sound_volume()
int sound_volume()
{
static const double default_value = 1.0;
static const int default_value = 100;
const string_map::const_iterator volume = prefs.values.find("sound_volume");
if(volume != prefs.values.end() && volume->second.empty() == false)
return atof(volume->second.c_str());
return atoi(volume->second.c_str());
else
return default_value;
}
void set_sound_volume(double vol)
void set_sound_volume(int vol)
{
std::stringstream stream;
stream << vol;
prefs["sound_volume"] = stream.str();
sound::set_sound_volume(vol);
sound::set_sound_volume(vol / 100.0);
}
void mute(bool muted)
@ -276,32 +276,26 @@ namespace {
double scroll = 0.2;
}
double scroll_speed()
int scroll_speed()
{
return get_scroll_speed()*100.0 + 10.0;
}
double get_scroll_speed()
{
static bool first_time = true;
if(first_time) {
first_time = false;
const string_map::const_iterator itor = prefs.values.find("scroll");
if(itor != prefs.values.end()) {
scroll = minimum<double>(1.0,maximum<double>(0.0,
atof(itor->second.c_str())));
}
static const int default_value = 100;
string_map::const_iterator i = prefs.values.find("scroll");
if(i != prefs.values.end() && i->second.empty() == false)
{
scroll = atoi(i->second.c_str()) / 100.0;
return atoi(i->second.c_str());
} else {
scroll = default_value / 100.0;
return default_value;
}
return scroll;
}
void set_scroll_speed(double new_speed)
void set_scroll_speed(int new_speed)
{
std::stringstream formatter;
formatter << new_speed;
prefs["scroll"] = formatter.str();
scroll = new_speed;
std::stringstream stream;
stream << new_speed;
prefs["scroll"] = stream.str();
scroll = new_speed / 100.0;
}
bool turn_bell()
@ -390,6 +384,7 @@ void show_preferences_dialog(display& disp)
SDL_Rect dialog_rect = {xpos-10,ypos-10,width+20,height+20};
surface_restorer restorer(&disp.video(),dialog_rect);
gui::draw_dialog_frame(xpos,ypos,width,height,disp);
SDL_Rect clip_rect = {0,0,disp.x(),disp.y()};
SDL_Rect title_rect = font::draw_text(NULL,clip_rect,16,font::NORMAL_COLOUR,
string_table["preferences"],0,0);
@ -437,13 +432,22 @@ void show_preferences_dialog(display& disp)
return;
SDL_Rect slider_rect = { slider_left,sound_pos,slider_right-slider_left,10};
gui::slider sound_slider(disp,slider_rect,sound_volume());
gui::slider sound_slider(disp,slider_rect);
sound_slider.set_min(1);
sound_slider.set_max(100);
sound_slider.set_value(sound_volume());
slider_rect.y = music_pos;
gui::slider music_slider(disp,slider_rect,music_volume());
gui::slider music_slider(disp,slider_rect);
music_slider.set_min(1);
music_slider.set_max(100);
music_slider.set_value(music_volume());
slider_rect.y = scroll_pos;
gui::slider scroll_slider(disp,slider_rect,get_scroll_speed());
gui::slider scroll_slider(disp,slider_rect);
scroll_slider.set_min(1);
scroll_slider.set_max(100);
scroll_slider.set_value(scroll_speed());
gui::button fullscreen_button(disp,string_table["full_screen"],
gui::button::TYPE_CHECK);
@ -505,21 +509,13 @@ void show_preferences_dialog(display& disp)
break;
}
const double new_music = music_slider.process(mousex,mousey,left_button);
const double new_sound = sound_slider.process(mousex,mousey,left_button);
const double new_scroll = scroll_slider.process(mousex,mousey,left_button);
music_slider.process();
sound_slider.process();
scroll_slider.process();
if(new_sound >= 0.0) {
set_sound_volume(new_sound);
}
if(new_music >= 0.0) {
set_music_volume(new_music);
}
if(new_scroll >= 0.0) {
set_scroll_speed(new_scroll);
}
set_sound_volume(sound_slider.value());
set_music_volume(music_slider.value());
set_scroll_speed(scroll_slider.value());
if(fullscreen_button.process(mousex,mousey,left_button)) {
//the underlying frame buffer is changing, so cancel
@ -531,12 +527,6 @@ void show_preferences_dialog(display& disp)
if(redraw_all) {
gui::draw_dialog_frame(xpos,ypos,width,height,disp);
sound_slider.background_changed();
music_slider.background_changed();
scroll_slider.background_changed();
sound_slider.draw();
music_slider.draw();
scroll_slider.draw();
fullscreen_button.draw();
turbo_button.draw();
grid_button.draw();

View file

@ -45,11 +45,11 @@ namespace preferences {
const std::string& locale();
void set_locale(const std::string& s);
double music_volume();
void set_music_volume(double vol);
int music_volume();
void set_music_volume(int vol);
double sound_volume();
void set_sound_volume(double vol);
int sound_volume();
void set_sound_volume(int vol);
void mute(bool muted);
bool is_muted();
@ -63,9 +63,8 @@ namespace preferences {
const std::string& login();
void set_login(const std::string& username);
double scroll_speed();
double get_scroll_speed();
void set_scroll_speed(double scroll);
int scroll_speed();
void set_scroll_speed(int scroll);
bool turn_bell();
void set_turn_bell(bool ison);

View file

@ -256,9 +256,7 @@ SDL_Surface* adjust_surface_colour(SDL_Surface* surface, int r, int g, int b)
if(r == 0 && g == 0 && b == 0 || surface == NULL)
return clone_surface(surface);
std::cerr << "~\n";
scoped_sdl_surface surf(make_neutral_surface(surface));
std::cerr << "~+\n";
if(surf == NULL) {
std::cerr << "failed to make neutral surface\n";
@ -693,4 +691,4 @@ void sdl_safe_blit(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Re
{
SDL_BlitSurface(src,srcrect,dst,dstrect);
invalidate_sdl_surface_cache(src);
}
}

View file

@ -25,94 +25,98 @@ namespace {
namespace gui {
slider::slider(display& disp, SDL_Rect& rect, double value)
: disp_(&disp),
buffer_(NULL), area_(rect), value_(value), drawn_(false),
highlight_(false), clicked_(true), dragging_(false)
slider::slider(display& d, SDL_Rect& rect)
: widget(d, rect), highlight_(false), clicked_(true),
dragging_(false)
{
background_changed();
set_dirty(true);
}
slider::slider(const slider& o) :
disp_(o.disp_),
buffer_(NULL),
area_(o.area_), value_(o.value_), drawn_(o.drawn_), highlight_(o.highlight_),
clicked_(o.clicked_), dragging_(o.dragging_)
void slider::set_min(int value)
{
background_changed();
min_ = value;
if (value_ < min_)
value_ = min_;
set_dirty(true);
}
slider& slider::operator=(const slider& o)
void slider::set_max(int value)
{
disp_ = o.disp_;
buffer_.assign(NULL);
area_ = o.area_;
value_ = o.value_;
drawn_ = o.drawn_;
highlight_ = o.highlight_;
clicked_ = o.clicked_;
dragging_ = o.dragging_;
background_changed();
return *this;
max_ = value;
if (value_ > max_)
value_ = max_;
set_dirty(true);
}
int slider::height(display& disp)
void slider::set_value(int value)
{
const scoped_sdl_surface image(image::get_image(slider_image,image::UNSCALED));
if(image != NULL)
return image->h;
else
return 0;
value_ = value;
if (value_ > max_)
value_ = max_;
if (value_ < min_)
value_ = min_;
set_dirty(true);
}
double slider::normalize(int value, int min_value, int max_value)
int slider::value()
{
return (double(value) - double(min_value))/double(max_value - min_value);
return value_;
}
int slider::denormalize(double value, int min_value, int max_value)
SDL_Rect slider::slider_area() const
{
return min_value + int(value*double(max_value - min_value));
static const SDL_Rect default_value = {0,0,0,0};
const scoped_sdl_surface img(image::get_image(slider_image,image::UNSCALED));
if(img == NULL)
return default_value;
if(img->w >= location().w)
return default_value;
double tmp = (value_ - min_ + 0.0) / (max_ - min_ + 0.0);
int tmp2 = (int)(tmp * (location().w - img->w));
const int xpos = location().x + tmp2;
SDL_Rect res = {xpos,location().y,img->w,img->h};
return res;
}
void slider::draw()
{
drawn_ = true;
const scoped_sdl_surface image(image::get_image(highlight_ ? selected_image : slider_image,image::UNSCALED));
if(image == NULL || buffer_ == NULL)
if(image == NULL || dirty() == false)
return;
const int hpadding = image->w/2;
if(hpadding*2 >= area_.w)
if(image->w >= location().w)
return;
SDL_Surface* const screen = disp_->video().getSurface();
SDL_Surface* const screen = disp().video().getSurface();
SDL_BlitSurface(buffer_,NULL,screen,&area_);
bg_restore();
SDL_Rect line_rect = {area_.x, area_.y + area_.h/3, area_.w-hpadding*2, 1};
SDL_Rect line_rect = {location().x, location().y + location().h/2,
location().w, 1};
SDL_FillRect(screen,&line_rect,SDL_MapRGB(screen->format,255,255,255));
SDL_Rect slider = slider_area();
disp_->blit_surface(slider.x,slider.y,image);
disp().blit_surface(slider.x,slider.y,image);
update_rect(area_);
set_dirty(false);
update_rect(location());
}
void slider::set_value(double value)
void slider::process()
{
value_ = value;
}
int mousex, mousey;
const int mouse_flags = SDL_GetMouseState(&mousex,&mousey);
const bool button = mouse_flags&SDL_BUTTON_LMASK;
double slider::process(int mousex, int mousey, bool button)
{
const scoped_sdl_surface img(image::get_image(slider_image,image::UNSCALED));
if(img == NULL)
return 0.0;
return;
bool should_draw = !drawn_;
SDL_Rect rect = {location().x, location().y, location().w, img->h};
set_location(rect);
const SDL_Rect& hit_area = slider_area();
const bool on = mousex > hit_area.x && mousex <= hit_area.x+hit_area.w &&
@ -120,7 +124,7 @@ double slider::process(int mousex, int mousey, bool button)
if(on != highlight_) {
highlight_ = on;
should_draw = true;
set_dirty(true);
}
const bool new_click = button && !clicked_;
@ -134,53 +138,25 @@ double slider::process(int mousex, int mousey, bool button)
clicked_ = button;
double new_value = value_;
int new_value = value_;
if(dragging_) {
new_value = double(mousex - (area_.x + img->w/2))/
double(area_.w - img->w);
if(new_value < 0.0)
new_value = 0.0;
int tmp = mousex - location().x;
if (tmp < 0)
tmp = 0;
if (tmp > location().w - img->w)
tmp = location().w - img->w;
if(new_value > 1.0)
new_value = 1.0;
double tmp2 = (tmp + 0.0) / (location().w - img->w + 0.0);
new_value = (int)(tmp2 * (max_ - min_ + 0.0)) + min_;
}
if(should_draw || new_value != value_)
draw();
if(new_value != value_) {
value_ = new_value;
return value_;
} else {
return -1.0;
set_dirty(true);
}
}
SDL_Rect slider::slider_area() const
{
static const SDL_Rect default_value = {0,0,0,0};
const scoped_sdl_surface img(image::get_image(slider_image,image::UNSCALED));
if(img == NULL)
return default_value;
const int hpadding = img->w/2;
if(hpadding*2 >= area_.w)
return default_value;
const int position = int(value_*double(area_.w - hpadding*2));
const int xpos = area_.x + position;
SDL_Rect res = {xpos,area_.y,img->w,img->h};
return res;
}
void slider::background_changed()
{
const scoped_sdl_surface img(image::get_image(slider_image,image::UNSCALED));
if(img != NULL) {
area_.h = img->h;
buffer_.assign(get_surface_portion(disp_->video().getSurface(),area_));
}
draw();
}
}

View file

@ -15,44 +15,42 @@
#include "SDL.h"
#include "../display.hpp"
#include "../sdl_utils.hpp"
#include "widget.hpp"
#include <vector>
namespace gui {
class slider
class slider : public widget
{
display* disp_;
scoped_sdl_surface buffer_;
SDL_Rect area_;
double value_;
bool drawn_;
bool highlight_, clicked_, dragging_;
SDL_Rect slider_area() const;
public:
slider(display& disp, SDL_Rect& rect, double value);
slider(const slider& o);
slider& operator=(const slider& o);
slider(display& d, SDL_Rect& rect);
static int height(display& disp);
static double normalize(int value, int min_value, int max_value);
static int denormalize(double value, int min_value, int max_value);
void set_min(int value);
void set_max(int value);
void set_value(int value);
int value();
void process();
protected:
using widget::bg_restore;
using widget::set_dirty;
using widget::dirty;
private:
SDL_Rect slider_area() const;
void draw();
double process(int mousex, int mousey, bool button);
int min_;
int max_;
int value_;
const SDL_Rect& area() const;
void set_value(double value);
void background_changed();
bool highlight_;
bool clicked_;
bool dragging_;
};
}

View file

@ -14,7 +14,6 @@
#ifndef TEXTBOX_HPP_INCLUDED
#define TEXTBOX_HPP_INCLUDED
#include "../display.hpp"
#include "../events.hpp"
#include "../key.hpp"
#include "../sdl_utils.hpp"

View file

@ -7,18 +7,38 @@ namespace {
namespace gui {
widget::widget(const widget &o) :
disp_(o.disp_), rect_(o.rect_), focus_(o.focus_), dirty_(o.dirty_)
{
bg_backup();
}
widget::widget(display& disp) :
disp_(disp), rect_(EmptyRect), focus_(true), dirty_(true)
{
bg_backup();
}
widget::widget(display& disp, const SDL_Rect& rect) :
disp_(disp), rect_(rect), focus_(true), dirty_(true)
widget::widget(display& disp, SDL_Rect& rect) :
disp_(disp), rect_(EmptyRect), focus_(true), dirty_(true)
{
set_location(rect);
bg_backup();
}
widget& widget::operator=(const widget& o)
{
// Things seem to wrok without reassinging the disp_
// but i'm not sure why...
// disp_ = o.disp();
rect_ = o.location();
focus_ = o.focus();
dirty_ = o.dirty();
bg_backup();
return *this;
}
void widget::set_location(const SDL_Rect& rect)
{
bg_restore();

View file

@ -21,10 +21,13 @@ public:
void set_focus(bool focus);
protected:
widget(const widget &o);
widget(display& disp);
widget(display& disp, const SDL_Rect& rect);
widget(display& disp, SDL_Rect& rect);
virtual ~widget() {}
widget& operator=(const widget& o);
void bg_restore() const;
void set_dirty(bool dirty);
const bool dirty() const;