summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gui/gui.cpp116
-rw-r--r--src/gui/gui.h27
2 files changed, 129 insertions, 14 deletions
diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp
index da8c3651..c227f05f 100644
--- a/src/gui/gui.cpp
+++ b/src/gui/gui.cpp
@@ -45,32 +45,26 @@
#define GUI_CALL_BUTTONCALLBACK(d)
static BITMAP *gui__repository[GUI_BMP_COUNT];
-/* The currently active skin */
+// The currently active skin
LexSkin gui_skin;
BITMAP *gui_bitmap;
DATAFILE *gui_gfx;
-// Guichan Allegro stuff
-gcn::AllegroGraphics* guiGraphics; // Graphics driver
-
// Guichan stuff
Gui* gui;
-gcn::Container* guiTop; // The top container
+gcn::AllegroGraphics* guiGraphics; // Graphics driver
+gcn::Container* guiTop; // The top container
Gui::Gui(BITMAP *screen)
{
- gui = new gcn::Gui();
-
// Set graphics
guiGraphics = new gcn::AllegroGraphics();
guiGraphics->setTarget(screen);
- gui->setGraphics(guiGraphics);
// Set input
guiInput = new AllegroInput();
- gui->setInput(guiInput);
// Set image loader
imageLoader = new gcn::AllegroImageLoader();
@@ -80,7 +74,10 @@ Gui::Gui(BITMAP *screen)
guiTop = new gcn::Container();
guiTop->setDimension(gcn::Rectangle(0, 0, SCREEN_W, SCREEN_H));
guiTop->setOpaque(false);
- gui->setTop(guiTop);
+
+ // Create focus handler
+ focusHandler = new gcn::FocusHandler();
+ guiTop->_setFocusHandler(focusHandler);
// Set global font
guiFont = new gcn::ImageFont("./data/graphic/fixedfont.bmp",
@@ -97,18 +94,111 @@ Gui::~Gui()
delete imageLoader;
delete guiInput;
delete guiGraphics;
- delete gui;
+ delete focusHandler;
}
void Gui::update()
{
- gui->logic();
- gui->draw();
+ logic();
+ draw();
// Draw the mouse
draw_sprite(gui_bitmap, mouse_sprite, mouse_x, mouse_y);
}
+void Gui::logic()
+{
+ guiInput->_pollInput();
+
+ while (!guiInput->isMouseQueueEmpty())
+ {
+ gcn::MouseInput mi = guiInput->dequeueMouseInput();
+ gcn::Widget* focused = focusHandler->getFocused();
+
+ if (mi.x > 0 && mi.y > 0 &&
+ guiTop->getDimension().isPointInRect(mi.x, mi.y))
+ {
+ if (!topHasMouse) {
+ guiTop->_mouseInMessage();
+ topHasMouse = true;
+ }
+
+ gcn::MouseInput mio = mi;
+ mio.x -= guiTop->getX();
+ mio.y -= guiTop->getY();
+
+ if (!guiTop->hasFocus()) {
+ guiTop->_mouseInputMessage(mio);
+ }
+ }
+ else {
+ if (topHasMouse) {
+ guiTop->_mouseOutMessage();
+ topHasMouse = false;
+ }
+ }
+
+ if (focusHandler->getFocused() && focused == focusHandler->getFocused())
+ {
+ int xOffset, yOffset;
+ focused->getAbsolutePosition(xOffset, yOffset);
+
+ gcn::MouseInput mio = mi;
+ mio.x -= xOffset;
+ mio.y -= yOffset;
+ focused->_mouseInputMessage(mio);
+ }
+ }
+
+ while (!guiInput->isKeyQueueEmpty())
+ {
+ gcn::KeyInput ki = guiInput->dequeueKeyInput();
+
+ // Handle tabbing
+ if (ki.getKey().getValue() == gcn::Key::TAB &&
+ ki.getType() == gcn::KeyInput::PRESS)
+ {
+ if (ki.getKey().isShiftPressed()) {
+ focusHandler->tabPrevious();
+ }
+ else {
+ focusHandler->tabNext();
+ }
+ }
+ else {
+ // Send key inputs to the focused widgets
+ gcn::Widget* focused = focusHandler->getFocused();
+ if (focused)
+ {
+ if (focused->isFocusable()) {
+ focused->_keyInputMessage(ki);
+ }
+ else {
+ focusHandler->focusNone();
+ }
+ }
+ }
+ }
+
+ guiTop->logic();
+}
+
+void Gui::draw()
+{
+ guiGraphics->_beginDraw();
+
+ guiGraphics->pushClipArea(guiTop->getDimension());
+ guiTop->draw(guiGraphics);
+ guiGraphics->popClipArea();
+
+ guiGraphics->_endDraw();
+}
+
+void Gui::focusNone()
+{
+ focusHandler->focusNone();
+}
+
void init_gui(BITMAP *bitmap, const char *skin) {
gui = new Gui(bitmap);
diff --git a/src/gui/gui.h b/src/gui/gui.h
index ada04a55..1055db0b 100644
--- a/src/gui/gui.h
+++ b/src/gui/gui.h
@@ -53,18 +53,43 @@ class Gui
/**
* Destructor.
*/
- virtual ~Gui();
+ ~Gui();
/**
* Performs GUI logic and drawing.
*/
void update();
+ /**
+ * Focus none of the Widgets in the Gui.
+ */
+ void focusNone();
+
private:
+ /**
+ * Performs the Gui:s logic by calling all logic functions
+ * down in the Gui heirarchy. Logic can be just about anything
+ * like adjusting a Widgets size or doing some calculations.
+ *
+ * NOTE: Logic also deals with user input (Mouse and Keyboard)
+ * for Widgets.
+ */
+ void logic();
+
+ /**
+ * Draws the whole Gui by calling draw functions down in the
+ * Gui hierarchy.
+ */
+ void draw();
+
gcn::Gui* gui; /**< The GUI system */
gcn::Input* guiInput; /**< Input driver */
gcn::ImageLoader* imageLoader; /**< For loading images */
gcn::ImageFont* guiFont; /**< The global GUI font */
+
+ bool topHasMouse;
+
+ gcn::FocusHandler* focusHandler;
};
typedef struct {