Skip to content

Commit

Permalink
REMOVED unnecessary includes
Browse files Browse the repository at this point in the history
UPDATED API docs of gui module
ADDED gamepad support to gui module (untested)
  • Loading branch information
Kai Sterker committed Oct 16, 2010
1 parent 16bcc7d commit e8beca1
Show file tree
Hide file tree
Showing 27 changed files with 2,296 additions and 265 deletions.
1,429 changes: 1,409 additions & 20 deletions doc/reference.cfg

Large diffs are not rendered by default.

57 changes: 46 additions & 11 deletions src/gui/button.cpp
@@ -1,14 +1,42 @@
/*
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/button.cpp
* @author Rian Shelley
* @brief Implements the button class.
*/

#include "button.h"
#include "base/logging.h"

/// offset of text in Clicked button
#define CLICK_OFFSET 4

namespace gui
{
// render button
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
if (Clicked) //have it drawn with the alternate image
{
Cache->set_offset (clickpush, clickpush);
Cache->set_offset (CLICK_OFFSET, CLICK_OFFSET);
Look->set_state("Activated");
label::draw(x, y, da, target);
}
Expand All @@ -19,45 +47,52 @@ namespace gui
label::draw(x, y, da, target);
}
}

// key pressed
bool button::keydown(input::keyboard_event& k)
{
if (!clicked && (k.key() == input::keyboard_event::SPACE_KEY || k.key() == input::keyboard_event::RETURN_KEY))
if (!Clicked && (k.key() == input::keyboard_event::SPACE_KEY || k.key() == input::keyboard_event::RETURN_KEY))
{
clicked = true;
downkey = k.key();
DownKey = k.key();
Clicked = true;
return true;
}

return false;
}

// key released
bool button::keyup(input::keyboard_event&k)
{
if (clicked && k.key() == downkey)
if (Clicked && k.key() == DownKey)
{
clicked = false;
activate();
Clicked = false;
return true;
}

return false;
}

//for if we have mouse support later
/*
bool button::mousedown(SDL_MouseButtonEvent & m)
{
LOG(INFO) << logging::indent() << "mouse down";
if (m.button == SDL_BUTTON_LEFT)
{
clicked = true;
downkey = SDLK_0;
Clicked = true;
DownKey = SDLK_0;
return true;
}
return false;
}
bool button::mouseup(SDL_MouseButtonEvent & m)
{
LOG(INFO) << logging::indent() << "mouse up";
if (m.button == SDL_BUTTON_LEFT && clicked)
if (m.button == SDL_BUTTON_LEFT && Clicked)
{
clicked = false;
Clicked = false;
if (m.x < w && m.y < h) //if within bounds, activate
activate();
return true;
Expand Down
182 changes: 157 additions & 25 deletions src/gui/button.h
@@ -1,27 +1,113 @@
/*
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/button.h
* @author Rian Shelley
* @brief Defines the button class.
*/

#ifndef GUI_BUTTON_H
#define GUI_BUTTON_H
#include "input/input.h"
#include "base/callback.h"
#include "label.h"

#include "gui/label.h"

namespace gui
{
//typedef void (*clickevent)(bool down, void* arg);
/// callback for being notified of button presses
typedef ::base::functor_2<bool, void * > * clickevent;
class button:public label

/**
* A widget that can receive focus and react to
* user input. Otherwise it has the same functionality
* as a label. Button text is centered in x and y
* direction by default.
*
* With the proper style it becomes a button, but
* it can also be used to represent a selectable
* list item or a slot in an inventory.
*/
class button : public label
{
protected:
bool clicked;
clickevent clickit;
input::keyboard_event::key_type downkey;
int clickpush;
public:
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); }

/**
* Create a button of the given size.
* @param width the width of the button.
* @param height the height of the button.
*/
button(const u_int16 & width, const u_int16 & height)
: label (width, height), Clicked(false), OnClick(NULL), UserData(NULL)
{
set_center (true, true);
}

/**
* Create button from the given data file.
* Size will be set to the background size.
* @param style filename of widget decoration.
*/
button(const std::string & style)
: label (style), Clicked(false), OnClick(NULL), UserData(NULL)
{
set_center (true, true);
}

/**
* Destroy the button and its associated resources.
*/
~button()
{
if(clickit) delete clickit;
delete OnClick;
}

/**
* Set a callback to notify the user that the
* button has been pressed. The callback will
* be freed when the widget is destroyed or
* a new callback is set. The argument is not.
*
* @param c the callback to execute
* @param arg the user data to pass to the callback.
*/
void set_callback (clickevent c, void* arg = NULL)
{
// cleanup first
delete OnClick;

OnClick = c;
UserData = arg;
}

/**
* Called when the widget has been activated.
* If present, executes the callback with the
* user defined data.
*/
virtual void activate()
{
if (OnClick)
{
(*OnClick)(Clicked, UserData);
}
}

/**
* Draw the object on the %screen.
*
Expand All @@ -32,20 +118,66 @@ namespace gui
* 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;


#ifndef SWIG
GET_TYPE_NAME_VIRTUAL(gui::button);
#endif

protected:
/**
* @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);
virtual bool keydown(input::keyboard_event&k);
//not till we get a mouse_event class
// virtual bool mouseup(SDL_MouseButtonEvent & m);
// virtual bool mousedown(SDL_MouseButtonEvent & m);
virtual bool focus() {return true;}
virtual void unfocus() {clicked = false;}

virtual void activate() {(*clickit)(clicked, arg);}
//so we can change the argument later
void* arg;

/**
* 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);
//@}

/**
* @name Focus Handling
*/
//@{
/**
* 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 true; }

/**
* Notification that the object will no longer receive keyboard
* events
*/
virtual void unfocus() { Clicked = false; }
//@}

//not till we get a mouse_event class
// virtual bool mouseup(SDL_MouseButtonEvent & m);
// virtual bool mousedown(SDL_MouseButtonEvent & m);

/// callback to notify when user pressed the button
clickevent OnClick;
/// user data to pass to the callback
void* UserData;
/// whether the button is currently being pressed
bool Clicked;
/// the key that caused the button to be pressed
input::keyboard_event::key_type DownKey;
};
};


#endif//GUI_LABEL_H
#endif//GUI_BUTTON_H
16 changes: 8 additions & 8 deletions src/gui/conversation.cc
Expand Up @@ -19,11 +19,11 @@ namespace gui
ct.set_multiline(true);
addchild(speaker, 15, 10);
addchild(ct, 15, LINEHEIGHT+10);
opty = ct.height() + 40;
opty = 40;
optcount = 0;
update();
color = c.color();
ct.setColor(color);
ct.set_color(color);
}

void conversation::selectopt(bool down, void* arg)
Expand Down Expand Up @@ -57,7 +57,7 @@ namespace gui
ct.set_string(line->text());

int i;
int y = opty;
int y = opty + ct.height();
optcount = line->num_answers() > MAX_OPTS ? MAX_OPTS : line->num_answers();

font *f = ct.get_font();
Expand All @@ -71,13 +71,13 @@ namespace gui
{
answers[i].which = i;
answers[i].obj = this;
options[i] = new button(length()-40, LINEHEIGHT, ::base::make_functor(*this, &conversation::selectopt), (void*)&answers[i], 2);
options[i] = new button(length()-40, LINEHEIGHT);
options[i]->set_callback(::base::make_functor(*this, &conversation::selectopt), (void*)&answers[i]);
options[i]->get_font()->setSize(LINEHEIGHT-1);
options[i]->set_multiline(true);
char tmp[16];
snprintf(tmp, 16, "%i)", i+1);
options[i]->set_string(string(tmp)+line->answer(i));
options[i]->reheight();
options[i]->set_center(false, false);
addchild(*options[i], 20, y);
y += options[i]->height() +5;
Expand All @@ -87,13 +87,13 @@ namespace gui
optcount = 1;
answers[0].which = -1;
answers[0].obj = this;
options[0] = new button(length()-40, LINEHEIGHT, ::base::make_functor(*this, &conversation::selectopt), (void*)&answers[i],2);
options[0] = new button(length()-40, LINEHEIGHT);
options[0]->set_callback(::base::make_functor(*this, &conversation::selectopt), (void*)&answers[i]);
options[0]->get_font()->setSize(LINEHEIGHT-1);
options[0]->set_multiline(true);
options[0]->set_string("1) (continue)");
options[0]->reheight();
options[0]->set_center(false, false);
options[0]->setColor(0xffffffff);
options[0]->set_color(0xffffffff);
addchild(*options[0], 20, y);
y += options[0]->height() +5;
}
Expand Down
3 changes: 1 addition & 2 deletions src/gui/font.h
Expand Up @@ -6,11 +6,10 @@
#include FT_FREETYPE_H

#include "gfx/gfx.h"
#include <string>
using std::string;
#include <vector>
#ifndef SWIG
using std::vector;
using std::string;
#endif

namespace gui
Expand Down
1 change: 1 addition & 0 deletions src/gui/gui.h
Expand Up @@ -8,5 +8,6 @@
#include "gui/layout.h"
#include "gui/option.h"
#include "gui/textbox.h"
#include "gui/window_manager.h"

#endif

0 comments on commit e8beca1

Please sign in to comment.