Skip to content

Commit

Permalink
ADDED better customizable look for gui
Browse files Browse the repository at this point in the history
STARTED to refactor and document gui code
  • Loading branch information
ksterker committed Aug 11, 2010
1 parent 9ccffac commit 07b0b11
Show file tree
Hide file tree
Showing 36 changed files with 1,428 additions and 515 deletions.
4 changes: 2 additions & 2 deletions src/gfx/drawable.h
Expand Up @@ -115,8 +115,8 @@ namespace gfx
* @param target pointer to the surface where to draw the drawable. If NULL,
* draw on the screen.
*/
virtual void draw (const s_int16 & x, const s_int16 & y, const drawing_area * da_opt = NULL,
surface * target = NULL) const = 0;
virtual void draw (const s_int16 & x, const s_int16 & y, const gfx::drawing_area * da_opt = NULL,
gfx::surface * target = NULL) const = 0;

protected:

Expand Down
15 changes: 13 additions & 2 deletions src/gfx/surface.cc
Expand Up @@ -56,7 +56,6 @@ namespace gfx
int Dx,Dy;
int Err;
int inc1,inc2;
int offset;

drawing_area da;

Expand Down Expand Up @@ -111,7 +110,6 @@ namespace gfx
Err+=inc2;

x+=IncX;
offset+=IncX;
}
}
else
Expand Down Expand Up @@ -141,6 +139,19 @@ namespace gfx
unlock();
}

// tile the given surface onto this one
void surface::tile (const surface& src, const drawing_area *da_opt)
{
drawing_area da;

if (da_opt) da = da_opt->setup_rects();
else da.resize (length(), height());

for (u_int16 posy = 0; posy < height(); posy += src.height ())
for (u_int16 posx = 0; posx < length(); posx += src.length ())
src.draw (da.x() + posx, da.y() + posy, &da, this);
}

// adjust brightness
void surface::set_brightness (const u_int8 & level)
{
Expand Down
10 changes: 10 additions & 0 deletions src/gfx/surface.h
Expand Up @@ -262,6 +262,16 @@ namespace gfx
*/
virtual void mirror (bool x, bool y) = 0;

/**
* Tile the given %surface onto this %surface. The location
* tiled can be constrained by an optional drawing_area. If
* omitted, the complete surface will be filled.
*
* @param src the source image.
* @param da_opt clipping rectangle.
*/
void tile (const surface& src, const drawing_area *da_opt = NULL);

/**
* Adjust image brightness. Values < 127 will darken
* the image, values > 127 brighten the image.
Expand Down
5 changes: 4 additions & 1 deletion src/gfx/surface_cacher.cc
Expand Up @@ -125,7 +125,10 @@ namespace gfx
// release reference
void surface_cacher::free_surface(const surface* surf)
{
free_by_name(SurfToString[surf]);
if (surf != NULL)
{
free_by_name(SurfToString[surf]);
}
}

// get refcount for given surface
Expand Down
5 changes: 5 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -2,11 +2,13 @@
set(adonthell_gui_SRCS
button.cpp
conversation.cc
decoration.cc
font.cpp
label.cpp
layout.cc
option.cpp
textbox.cpp
textcache.cc
widget.cpp
window_manager.cc
)
Expand All @@ -15,12 +17,15 @@ set(adonthell_gui_HEADERS
base.h
button.h
conversation.h
decoration.h
draw.h
font.h
gui.h
label.h
layout.h
option.h
textbox.h
textcache.h
widget.h
window_manager.h
)
Expand Down
8 changes: 6 additions & 2 deletions src/gui/Makefile.am
Expand Up @@ -6,27 +6,31 @@ pkgincludeinputdir = $(pkgincludedir)/gui
pkgincludeinput_HEADERS = \
base.h \
button.h \
conversation.h \
conversation.h \
decoration.h \
draw.h \
font.h \
gui.h \
label.h \
layout.h \
option.h \
textbox.h \
textcache.h \
widget.h \
window_manager.h

lib_LTLIBRARIES = libadonthell_gui.la

libadonthell_gui_la_SOURCES = \
button.cpp \
conversation.cc \
conversation.cc \
decoration.cc \
font.cpp \
label.cpp \
layout.cc \
option.cpp \
textbox.cpp \
textcache.cc \
widget.cpp \
window_manager.cc

Expand Down
135 changes: 115 additions & 20 deletions src/gui/base.h
@@ -1,27 +1,90 @@
/*
Copyright (C) 2008 Rian Shelley
Part of the Adonthell Project http://adonthell.linuxgames.com
Adonthell 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.
Adonthell 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 Adonthell; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/**
* @file gui/widget.h
* @author Rian Shelley
* @brief Defines the surface global interface.
*/

#ifndef GUI_BASE_H
#define GUI_BASE_H

#include "gfx/gfx.h"
#include "gfx/drawable.h"
#include "gui/decoration.h"
#include "input/keyboard_event.h"

namespace gui
{
class base
/**
* The base class for all user interface elements, both widgets and
* layouts.
*/
class base : public gfx::drawable
{
protected:
int w, h;
bool visible;
bool selhilite;
public:
base():w(0),h(0),visible(true),selhilite(true) {}
/* called when the object needs to be drawn */
virtual void draw(int x, int y, gfx::surface* s) = 0;
/* called when a keystroke happens. returns whether or not the
/**
* Create a new GUI element. Initially it's visible
* and will indicate that it received the focus.
*/
base() : Visible(true), Selhilite(true)
{
Look = new decoration();
}

/**
* Cleanup.
*/
virtual ~base ()
{
delete Look;
}

/**
* @name Keyboard Callbacks.
*
* Called when a keystroke happens. returns whether or not the
* object used the keystroke
*/
//@{
/**
* Called when a key has been released by the user.
* @param k the keyboard event.
* @return true if the event was consumed, false otherwise.
*/
virtual bool keyup(input::keyboard_event & k) { return false; }
virtual bool keydown(input::keyboard_event & k) { return false; }
virtual bool input(input::keyboard_event & k) { return false; }

/**
* Called when a key has been pressed by the user.
* @param k the keyboard event.
* @return true if the event was consumed, false otherwise.
*/
virtual bool keydown(input::keyboard_event & k) { return false; }

/**
* Called when text has been input by the user.
* @param k the keyboard event.
* @return true if the event was consumed, false otherwise.
*/
virtual bool input(input::keyboard_event & k) { return false; }
//@}
#if 0
/*
* called when a mouseevent happens, returns whether it was used
Expand All @@ -30,19 +93,51 @@ namespace gui
virtual bool mousedown(SDL_MouseButtonEvent & m) {return false; }
virtual bool mousemove(SDL_MouseMotionEvent & m) {return false; }
#endif
/* calls when object is offered the keyboard events. returns true
* to accept them
/**
* Called when object is offered the keyboard focus. Returns true
* to accept it.
* @return true to accept focus, false to decline.
*/
virtual bool focus() {return false;}
/* notification that the object will no longer recieve keyboard events*/
virtual bool focus() { return false; }

/**
* Notification that the object will no longer recieve keyboard
* events
*/
virtual void unfocus() {}

/* change the size of the widget */
virtual void setSize(int width, int height) {w = width; h = height;}
/**
* Change the size of the widget
* @param width the new width
* @param height the new height
*/
virtual void set_size(const u_int16 & width, const u_int16 & height)
{
set_length (width);
set_height (height);
Look->set_size (width, height);
}

int getWidth() { return w;}
int getHeight() { return h;}
bool highlightEnabled() { return selhilite; }
/**
* Return whether the widget should indicate that is has focus.
* @return true if it indicates focus, false otherwise.
*/
bool highlightEnabled() const
{
return Selhilite;
}

#ifndef SWIG
GET_TYPE_NAME_ABSTRACT(gui::base);
#endif

protected:
/// whether widget is visible
bool Visible;
/// whether widget indicates focus
bool Selhilite;
/// graphical representation of the widget
decoration *Look;
};
};

Expand Down
15 changes: 7 additions & 8 deletions src/gui/button.cpp
Expand Up @@ -4,20 +4,19 @@

namespace gui
{
void button::draw(int x, int y, gfx::surface* s)
void button::draw(const s_int16 & x, const s_int16 & y, const gfx::drawing_area *da, gfx::surface *target) const
{
if (clicked) //have it drawn with the alternate image
{
const gfx::surface* old = bg;
bg = imgdown;
px = py = clickpush;
label::draw(x, y, s);
bg = old;
Cache->set_offset (clickpush, clickpush);
Look->set_state("Activated");
label::draw(x, y, da, target);
}
else
{
px = py = 0;
label::draw(x, y, s);
Cache->set_offset (0, 0);
Look->set_state("Default");
label::draw(x, y, da, target);
}
}
bool button::keydown(input::keyboard_event& k)
Expand Down
23 changes: 17 additions & 6 deletions src/gui/button.h
Expand Up @@ -10,18 +10,29 @@ namespace gui
class button:public label
{
protected:
const gfx::surface * imgdown;
bool clicked;
clickevent clickit;
input::keyboard_event::key_type downkey;
int clickpush;
public:
button(int width, int height, clickevent c, void* a=NULL, int o=5):label(width, height),imgdown(NULL),clicked(false),clickit(c),clickpush(o),arg(a) {centerH(true);centerV(true);}
button(const char* upimg, const char* downimg, clickevent c, void* a=NULL, int o=5):label(upimg),imgdown(gfx::surfaces->get_surface_only((downimg))),clicked(false),clickit(c),clickpush(o),arg(a) {centerH(true);centerV(true);}
~button() {if (imgdown) gfx::surfaces->free_surface(imgdown); if(clickit) delete clickit;}
button(const u_int16 & width, const u_int16 & height, clickevent c, void* a=NULL, int o=5):label(width, height),clicked(false),clickit(c),clickpush(o),arg(a) { set_center (true, true); }
button(const std::string & name, clickevent c, void* a=NULL, int o=5):label(name),clicked(false),clickit(c),clickpush(o),arg(a) { set_center (true, true); }
~button()
{
if(clickit) delete clickit;
}


virtual void draw(int x, int y, gfx::surface* s);
/**
* Draw the object on the %screen.
*
* @param x X position where to draw.
* @param y Y position where to draw.
* @param da optional drawing_area to use during the drawing operation.
* @param target pointer to the surface where to draw the drawable. If NULL,
* draw on the screen.
*/
virtual void draw(const s_int16 & x, const s_int16 & y, const gfx::drawing_area *da = NULL, gfx::surface *target = NULL) const;

virtual bool keyup(input::keyboard_event& k);
virtual bool keydown(input::keyboard_event&k);
//not till we get a mouse_event class
Expand Down

0 comments on commit 07b0b11

Please sign in to comment.