tweaks to the widget class, fixes to the textbox widget
This commit is contained in:
parent
15f61f8981
commit
323e7f6791
5 changed files with 57 additions and 22 deletions
|
@ -187,6 +187,8 @@ int play_multiplayer(display& disp, game_data& units_data, config cfg,
|
|||
const int mouse_flags = SDL_GetMouseState(&mousex,&mousey);
|
||||
const bool left_button = mouse_flags&SDL_BUTTON_LMASK;
|
||||
|
||||
name_entry.process();
|
||||
|
||||
maps_menu.process(mousex,mousey,left_button,
|
||||
key[SDLK_UP],key[SDLK_DOWN],
|
||||
key[SDLK_PAGEUP],key[SDLK_PAGEDOWN]);
|
||||
|
|
|
@ -25,7 +25,7 @@ const int font_size = 16;
|
|||
|
||||
textbox::textbox(display& d, int width, const std::string& text)
|
||||
: widget(d), text_(text), firstOnScreen_(0),
|
||||
cursor_(text.size())
|
||||
cursor_(text.size()), show_cursor_(true)
|
||||
{
|
||||
static const SDL_Rect area = d.screen_area();
|
||||
const int height = font::draw_text(NULL,area,font_size,font::NORMAL_COLOUR,"ABCD",0,0).h;
|
||||
|
@ -42,6 +42,7 @@ void textbox::set_text(std::string text)
|
|||
{
|
||||
text_ = text;
|
||||
cursor_ = text_.size();
|
||||
set_dirty(true);
|
||||
}
|
||||
|
||||
void textbox::clear()
|
||||
|
@ -49,22 +50,21 @@ void textbox::clear()
|
|||
text_ = "";
|
||||
cursor_ = 0;
|
||||
firstOnScreen_ = 0;
|
||||
set_dirty(true);
|
||||
}
|
||||
|
||||
void textbox::draw_cursor(int pos, display &disp) const
|
||||
{
|
||||
const bool show_cursor = (SDL_GetTicks()%1000) > 500;
|
||||
|
||||
if(show_cursor) {
|
||||
if(show_cursor_) {
|
||||
SDL_Rect rect = {location().x + pos, location().y, 1, location().h };
|
||||
SDL_Surface* const frame_buffer = disp.video().getSurface();
|
||||
SDL_FillRect(frame_buffer,&rect,SDL_MapRGB(frame_buffer->format,255,255,255));
|
||||
}
|
||||
}
|
||||
|
||||
void textbox::draw() const
|
||||
void textbox::draw()
|
||||
{
|
||||
if(location().x == 0)
|
||||
if(location().x == 0 || !dirty())
|
||||
return;
|
||||
|
||||
bg_restore();
|
||||
|
@ -72,7 +72,7 @@ void textbox::draw() const
|
|||
gui::draw_solid_tinted_rectangle(location().x,location().y,location().w,location().h,0,0,0,
|
||||
focus() ? 0.2 : 0.4, disp().video().getSurface());
|
||||
|
||||
if(cursor_ == 0)
|
||||
if (cursor_ == 0)
|
||||
draw_cursor(0, disp());
|
||||
|
||||
int pos = 1;
|
||||
|
@ -98,13 +98,25 @@ void textbox::draw() const
|
|||
|
||||
pos += area.w;
|
||||
|
||||
if(cursor_ == i+1)
|
||||
draw_cursor(pos-1, disp());
|
||||
}
|
||||
|
||||
draw_cursor(pos-1, disp());
|
||||
|
||||
set_dirty(false);
|
||||
update_rect(location());
|
||||
}
|
||||
|
||||
void textbox::process()
|
||||
{
|
||||
//Blink the cursor
|
||||
bool old_cursor = show_cursor_;
|
||||
show_cursor_ = (SDL_GetTicks()%1000) > 500;
|
||||
if (old_cursor != show_cursor_)
|
||||
set_dirty(true);
|
||||
|
||||
draw();
|
||||
}
|
||||
|
||||
void textbox::handle_event(const SDL_Event& event)
|
||||
{
|
||||
if(location().x == 0)
|
||||
|
@ -113,8 +125,11 @@ void textbox::handle_event(const SDL_Event& event)
|
|||
int mousex, mousey;
|
||||
SDL_GetMouseState(&mousex,&mousey);
|
||||
|
||||
if(event.type != SDL_KEYDOWN || !focus())
|
||||
if(event.type != SDL_KEYDOWN || focus() != true)
|
||||
{
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
|
||||
const SDL_keysym& key = reinterpret_cast<const SDL_KeyboardEvent&>(event).keysym;
|
||||
|
||||
|
@ -153,7 +168,7 @@ void textbox::handle_event(const SDL_Event& event)
|
|||
++cursor_;
|
||||
}
|
||||
|
||||
update();
|
||||
set_dirty(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,19 +33,22 @@ public:
|
|||
const std::string& text() const;
|
||||
void set_text(std::string text);
|
||||
void clear();
|
||||
void process();
|
||||
|
||||
protected:
|
||||
using widget::bg_restore;
|
||||
using widget::update;
|
||||
using widget::set_dirty;
|
||||
using widget::dirty;
|
||||
|
||||
private:
|
||||
std::string text_;
|
||||
mutable unsigned int firstOnScreen_;
|
||||
unsigned int cursor_;
|
||||
bool show_cursor_;
|
||||
|
||||
void handle_event(const SDL_Event& event);
|
||||
|
||||
void draw() const;
|
||||
void draw();
|
||||
void draw_cursor(int pos, display &disp) const;
|
||||
};
|
||||
|
||||
|
|
|
@ -7,12 +7,14 @@ namespace {
|
|||
|
||||
namespace gui {
|
||||
|
||||
widget::widget(display& disp) : disp_(disp), rect_(EmptyRect), focus_(true)
|
||||
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)
|
||||
widget::widget(display& disp, const SDL_Rect& rect) :
|
||||
disp_(disp), rect_(rect), focus_(true), dirty_(true)
|
||||
{
|
||||
bg_backup();
|
||||
}
|
||||
|
@ -21,6 +23,7 @@ void widget::set_location(const SDL_Rect& rect)
|
|||
{
|
||||
bg_restore();
|
||||
rect_ = rect;
|
||||
dirty_ = true;
|
||||
bg_backup();
|
||||
draw();
|
||||
}
|
||||
|
@ -30,6 +33,7 @@ void widget::set_position(int x, int y)
|
|||
bg_restore();
|
||||
SDL_Rect rect = {x,y,location().w,location().h};
|
||||
rect_ = rect;
|
||||
dirty_ = true;
|
||||
bg_backup();
|
||||
draw();
|
||||
}
|
||||
|
@ -39,6 +43,7 @@ void widget::set_width(int w)
|
|||
bg_restore();
|
||||
SDL_Rect rect = {location().x,location().y,w,location().h};
|
||||
rect_ = rect;
|
||||
dirty_ = true;
|
||||
bg_backup();
|
||||
draw();
|
||||
}
|
||||
|
@ -48,6 +53,7 @@ void widget::set_height(int h)
|
|||
bg_restore();
|
||||
SDL_Rect rect = {location().x,location().y,location().w,h};
|
||||
rect_ = rect;
|
||||
dirty_ = true;
|
||||
bg_backup();
|
||||
draw();
|
||||
}
|
||||
|
@ -60,6 +66,8 @@ const SDL_Rect& widget::location() const
|
|||
void widget::set_focus(bool focus)
|
||||
{
|
||||
focus_ = focus;
|
||||
dirty_ = true;
|
||||
draw();
|
||||
}
|
||||
|
||||
const bool widget::focus() const
|
||||
|
@ -67,6 +75,16 @@ const bool widget::focus() const
|
|||
return focus_;
|
||||
}
|
||||
|
||||
void widget::set_dirty(bool dirty)
|
||||
{
|
||||
dirty_ = dirty;
|
||||
}
|
||||
|
||||
const bool widget::dirty() const
|
||||
{
|
||||
return dirty_;
|
||||
}
|
||||
|
||||
void widget::bg_backup()
|
||||
{
|
||||
restorer_ = surface_restorer(&disp_.video(), rect_);
|
||||
|
@ -77,11 +95,6 @@ void widget::bg_restore() const
|
|||
restorer_.restore();
|
||||
}
|
||||
|
||||
void widget::update() const
|
||||
{
|
||||
draw();
|
||||
}
|
||||
|
||||
void widget::handle_event(const SDL_Event& event)
|
||||
{
|
||||
if (!focus_)
|
||||
|
|
|
@ -26,7 +26,8 @@ protected:
|
|||
virtual ~widget() {}
|
||||
|
||||
void bg_restore() const;
|
||||
void update() const;
|
||||
void set_dirty(bool dirty);
|
||||
const bool dirty() const;
|
||||
|
||||
display& disp() const { return disp_; }
|
||||
|
||||
|
@ -34,7 +35,8 @@ private:
|
|||
mutable display& disp_;
|
||||
mutable surface_restorer restorer_;
|
||||
SDL_Rect rect_;
|
||||
bool focus_;
|
||||
bool focus_; // Should user input be ignored?
|
||||
bool dirty_; // Does the widget need drawn?
|
||||
|
||||
void bg_backup();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue