[Tux4kids-commits] [SCM] tuxhistory - Educational history game branch, master, updated. 4b3105afd66d99f3321292175484ec850f0b05c2
julio (none)
julio at julio-desktop.
Fri Dec 3 16:32:16 UTC 2010
The following commit has been merged in the master branch:
commit 4b3105afd66d99f3321292175484ec850f0b05c2
Author: julio <julio at julio-desktop.(none)>
Date: Fri Dec 3 10:31:28 2010 -0600
Adding the new UI inferface (Made by Aviral Dasgupta in GCI)
diff --git a/src/t4kui/demo.c.txt b/src/t4kui/demo.c.txt
new file mode 100644
index 0000000..586af54
--- /dev/null
+++ b/src/t4kui/demo.c.txt
@@ -0,0 +1,116 @@
+/*
+ * demo.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+
+#include "ui_system.h"
+#include "ui_window.h"
+#include "ui_button.h"
+#include "ui_proxywidget.h"
+#include "ui_colors.h"
+#include "ui_events.h"
+#include "ui_layouts.h"
+#include "ui_callback.h"
+
+#define SCREEN_WIDTH 640
+#define SCREEN_HEIGHT 480
+#define SCREEN_BPP 32
+
+static SDL_Surface *screen;
+static UI_Window *window;
+
+void DEMO_CreateUI();
+void DEMO_DestroyUI();
+void DEMO_Update();
+void DEMO_Repaint();
+
+int main(int argc, char *argv[]) {
+ if(SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO)!=0) {
+ fprintf(stderr, "Fatal: %s\n(%s)\n", "SDL failed to initialize.", SDL_GetError());
+ return EXIT_FAILURE;
+ }
+
+ if((screen=SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_DOUBLEBUF))==NULL) {
+ fprintf(stderr, "Fatal: %s\n(%s)\n", "Failed to set video mode.", SDL_GetError());
+ return EXIT_FAILURE;
+ }
+ DEMO_CreateUI();
+
+ int running = 1;
+ while(running) {
+ //TODO add display functions here
+
+ DEMO_Update();
+ DEMO_Repaint();
+
+ SDL_Flip(screen);
+ SDL_Delay(1);
+ }
+
+ return EXIT_SUCCESS;
+}
+
+static UI_Button *button;
+static UI_Button *button2;
+
+int btn1_callback(void *target) {exit(EXIT_SUCCESS);}
+int btn2_callback(void *target) {SDL_WM_IconifyWindow();}
+
+void DEMO_CreateUI() {
+ SDL_WM_SetCaption("Tuxedo UI Library Demo", "TuxUIDemo");
+ ui_InitSystem();
+ uie_Register();
+ window = ui_CreateWindow(0, 0, 320, 240);
+ uie_RegisterWindow(window);
+ button = ui_CreateButton("Exit Application");
+ ui_SetButtonCallback(button, btn1_callback);
+ layout_FSetPos(0, 0);
+ ui_AddWidget(window, button, WT_BUTTON);
+ button2 = ui_CreateButton("Minimise Application");
+ ui_SetButtonCallback(button2, btn2_callback);
+ layout_FSetPos(100, 100);
+ ui_AddWidget(window, button2, WT_BUTTON);
+}
+
+void DEMO_DestroyUI() {
+}
+
+void DEMO_Update() {
+ SDL_Event event;
+
+ while(SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_QUIT:
+ exit(EXIT_SUCCESS);
+ }
+ }
+}
+
+void DEMO_Repaint() {
+ SDL_FillRect(screen, NULL, C_BLACK);
+ ui_DrawWindow(window, screen);
+}
+
diff --git a/src/t4kui/ui_button.c b/src/t4kui/ui_button.c
new file mode 100644
index 0000000..2f21d95
--- /dev/null
+++ b/src/t4kui/ui_button.c
@@ -0,0 +1,77 @@
+/*
+ * ui_button.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_button.h"
+#include "ui_colors.h"
+#include "ui_system.h"
+#include "ui_callback.h"
+#include <SDL/SDL.h>
+
+#include "../SDL_extras.h"
+
+struct _UI_Button {
+ char *label;
+ SDL_Surface *prerendered;
+ UI_Callback callback;
+};
+
+struct _UI_Button *ui_CreateButton(char *label) {
+ struct _UI_Button *btn = malloc(sizeof(struct _UI_Button));
+ ui_SetButtonLabel(btn, label);
+ return btn;
+}
+
+void ui_PaintButton(struct _UI_Button *button, SDL_Surface *canvas, int x, int y) {
+ SDL_Rect pos;
+ pos.x = x; pos.y = y;
+ SDL_BlitSurface(button->prerendered, NULL, canvas, &pos);
+}
+
+int ui_GetButtonWidth(struct _UI_Button *button) {
+ return button->prerendered->w;
+}
+
+int ui_GetButtonHeight(struct _UI_Button *button) {
+ return button->prerendered->h;
+}
+
+char *ui_GetButtonLabel(struct _UI_Button *button) {
+ return button->label;
+}
+
+void ui_SetButtonLabel(struct _UI_Button *button, char *label) {
+ SDL_Surface *tmpsurf;
+ button->label = label;
+ tmpsurf = SimpleText(label, FONT_SIZE, SDLC_WHITE);
+ SDL_BlitSurface(tmpsurf, NULL, button->prerendered, NULL);
+ //button->prerendered = TTF_RenderText_Shaded(ui_GetFont(), label, SDLC_WHITE, SDLC_BTN_BG);
+}
+
+void ui_SetButtonCallback(struct _UI_Button *button, UI_Callback callback) {
+ button->callback = callback;
+}
+
+void ui_onButtonEvent(struct _UI_Button *button) {
+ button->callback(button);
+}
+
+
diff --git a/src/t4kui/ui_button.h b/src/t4kui/ui_button.h
new file mode 100644
index 0000000..6aa0687
--- /dev/null
+++ b/src/t4kui/ui_button.h
@@ -0,0 +1,44 @@
+/*
+ * ui_button.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_BUTTON_H__
+#define __UI_BUTTON_H__
+
+#include <SDL/SDL.h>
+#include "ui_callback.h"
+
+typedef struct _UI_Button UI_Button;
+
+UI_Button *ui_CreateButton(char *label);
+
+void ui_SetButtonCallback(UI_Button *button, UI_Callback callback);
+void ui_onButtonEvent(UI_Button *button);
+
+void ui_PaintButton(UI_Button *button, SDL_Surface *canvas, int x, int y);
+
+int ui_GetButtonWidth(UI_Button *);
+int ui_GetButtonHeight(UI_Button *);
+char *ui_GetButtonLabel(UI_Button *);
+void ui_SetButtonLabel(UI_Button *, char *label);
+
+#endif /* __UI_BUTTON_H__ */
+
diff --git a/src/t4kui/ui_callback.h b/src/t4kui/ui_callback.h
new file mode 100644
index 0000000..50d9216
--- /dev/null
+++ b/src/t4kui/ui_callback.h
@@ -0,0 +1,29 @@
+/*
+ * ui_callback.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_CALLBACK_H__
+#define __UI_CALLBACK_H__
+
+typedef int (*UI_Callback)(void *);
+
+#endif /* __UI_CALLBACK_H__ */
+
diff --git a/src/t4kui/ui_colors.c b/src/t4kui/ui_colors.c
new file mode 100644
index 0000000..126fba4
--- /dev/null
+++ b/src/t4kui/ui_colors.c
@@ -0,0 +1,28 @@
+/*
+ * ui_colors.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_colors.h"
+#include <SDL/SDL.h>
+
+SDL_Color SDLC_WHITE = {0xff, 0xff, 0xff};
+SDL_Color SDLC_BLACK = {0x00, 0x00, 0x00};
+SDL_Color SDLC_BTN_BG = {0x55, 0x57, 0x53};
diff --git a/src/t4kui/ui_colors.h b/src/t4kui/ui_colors.h
new file mode 100644
index 0000000..7f5ddd8
--- /dev/null
+++ b/src/t4kui/ui_colors.h
@@ -0,0 +1,40 @@
+/*
+ * ui_colors.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_COLORS_H__
+#define __UI_COLORS_H__
+
+#include <SDL/SDL.h>
+
+typedef enum _UI_Color {
+ C_WHITE = 0xffffff,
+ C_BLACK = 0x000000,
+ C_WND = 0xd3d7cf,
+ C_BTN = 0x555753
+} UI_Color;
+
+SDL_Color SDLC_WHITE;
+SDL_Color SDLC_BLACK;
+SDL_Color SDLC_BTN_BG;
+
+#endif /* __UI_COLORS_H__ */
+
diff --git a/src/t4kui/ui_events.c b/src/t4kui/ui_events.c
new file mode 100644
index 0000000..a458687
--- /dev/null
+++ b/src/t4kui/ui_events.c
@@ -0,0 +1,60 @@
+/*
+ * ui_events.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_events.h"
+#include "ui_window.h"
+#include "util.h"
+#include <SDL/SDL.h>
+
+static LinkedList *reg_windows;
+
+void uie_Register() {
+ SDL_SetEventFilter(uie_EventManager);
+ reg_windows = list_Create();
+}
+
+void uie_RegisterWindow(UI_Window *win) {
+ list_AddNode(reg_windows, win);
+}
+
+static UI_Window *_uie_FindWindow(int x, int y) {
+ Node *node = list_GetFirst(reg_windows);
+ while(node!=NULL) {
+ if(ui_WindowContains((UI_Window *)list_GetNodeValue(node), x, y))
+ return (UI_Window *)list_GetNodeValue(node);
+ node = list_Traverse(node);
+ }
+ return NULL;
+}
+
+int uie_EventManager(const SDL_Event *event) {
+ switch(event->type) {
+ case SDL_MOUSEBUTTONDOWN:
+ if(event->button.button==SDL_BUTTON_LEFT) {
+ UI_Window *win=_uie_FindWindow(event->button.x, event->button.y);
+ if(win!=NULL)
+ ui_onWindowEvent(win, event->button.x, event->button.y);
+ }
+ break;
+ }
+ return 1;
+}
diff --git a/src/t4kui/ui_events.h b/src/t4kui/ui_events.h
new file mode 100644
index 0000000..824ead4
--- /dev/null
+++ b/src/t4kui/ui_events.h
@@ -0,0 +1,39 @@
+/*
+ * ui_events.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_EVENTS_H__
+#define __UI_EVENTS_H__
+
+#include <SDL/SDL.h>
+#include "ui_window.h"
+
+typedef enum _UI_Event {
+ UIE_CLICK
+} UI_Click;
+
+void uie_Register();
+void uie_RegisterWindow(UI_Window *win);
+int uie_EventManager(const SDL_Event *event);
+
+
+#endif /* __UI_EVENTS_H__ */
+
diff --git a/src/t4kui/ui_layouts.c b/src/t4kui/ui_layouts.c
new file mode 100644
index 0000000..e19c669
--- /dev/null
+++ b/src/t4kui/ui_layouts.c
@@ -0,0 +1,37 @@
+/*
+ * ui_layouts.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_layouts.h"
+#include <SDL/SDL.h>
+
+static int Fx=0, Fy=0;
+
+void layout_FSetPos(int x, int y) {
+ Fx=x; Fy=y;
+}
+
+SDL_Rect layout_FGetPos() {
+ SDL_Rect pos;
+ pos.x = Fx;
+ pos.y = Fy;
+ return pos;
+}
diff --git a/src/t4kui/ui_layouts.h b/src/t4kui/ui_layouts.h
new file mode 100644
index 0000000..bfd09a0
--- /dev/null
+++ b/src/t4kui/ui_layouts.h
@@ -0,0 +1,32 @@
+/*
+ * ui_layouts.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_LAYOUTS_H__
+#define __UI_LAYOUTS_H__
+
+#include <SDL/SDL.h>
+
+void layout_FSetPos(int x, int y);
+SDL_Rect layout_FGetPos();
+
+#endif /* __UI_LAYOUTS_H__ */
+
diff --git a/src/t4kui/ui_proxywidget.c b/src/t4kui/ui_proxywidget.c
new file mode 100644
index 0000000..00ac623
--- /dev/null
+++ b/src/t4kui/ui_proxywidget.c
@@ -0,0 +1,114 @@
+/*
+ * ui_proxywidget.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_proxywidget.h"
+#include "ui_button.h"
+#include <stdlib.h>
+
+struct _UI_ProxyWidget {
+ WidgetType type; /* contains type information about widget */
+ void *widget; /* pointer to actual widget */
+ int x, y;
+};
+
+int ui_WidgetContains(UI_ProxyWidget *widget, int x, int y) {
+ //fprintf(stdout, "X:%d Y:%d W:%d H:%d / px: %d py: %d\n", widget->x, widget->y, widget->x + ui_GetWidgetWidth(widget), widget->y + ui_GetWidgetHeight(widget), x, y);
+ return ((x > widget->x) &&
+ (y > widget->y) &&
+ (x < widget->x + ui_GetWidgetWidth(widget)) &&
+ (y < widget->y + ui_GetWidgetHeight(widget)))?1:0;
+}
+
+UI_ProxyWidget *ui_CreateProxyWidget(void *proxied, WidgetType type) {
+ UI_ProxyWidget *widget = malloc(sizeof(UI_ProxyWidget));
+ widget->widget = proxied;
+ widget->type = type;
+ widget->x=0; widget->y=0;
+ return widget;
+}
+
+void *UI_DestroyProxyWidget(UI_ProxyWidget *widget) {
+ void *proxied = widget->widget;
+ free(widget);
+ return proxied;
+}
+
+void ui_onWidgetEvent(UI_ProxyWidget *widget, int x, int y) {
+ switch(widget->type) {
+ case WT_BUTTON:
+ ui_onButtonEvent(widget->widget);
+ }
+}
+
+void ui_SetWidgetPosition(UI_ProxyWidget *widget, int x, int y) {
+ widget->x = x;
+ widget->y = y;
+}
+
+void ui_PaintWidget(UI_ProxyWidget *widget, SDL_Surface *canvas) {
+ switch(widget->type) {
+ case WT_BUTTON:
+ ui_PaintButton(widget->widget, canvas, widget->x, widget->y);
+ break;
+ }
+}
+
+WidgetType ui_GetWidgetType(UI_ProxyWidget *widget) {
+ return widget->type;
+}
+
+void *ui_GetWidget(UI_ProxyWidget *widget) {
+ return widget->widget;
+}
+
+int ui_GetWidgetHeight(UI_ProxyWidget *widget) {
+ switch(widget->type) {
+ case WT_BUTTON:
+ return ui_GetButtonHeight(widget->widget);
+ break;
+ }
+}
+
+int ui_GetWidgetWidth(UI_ProxyWidget *widget) {
+ switch(widget->type) {
+ case WT_BUTTON:
+ return ui_GetButtonWidth(widget->widget);
+ break;
+ }
+}
+
+char *ui_GetWidgetLabel(UI_ProxyWidget *widget) {
+ switch(widget->type) {
+ case WT_BUTTON:
+ return ui_GetButtonLabel(widget->widget);
+ break;
+ }
+}
+
+void ui_SetWidgetLabel(UI_ProxyWidget *widget, char *label) {
+ switch(widget->type) {
+ case WT_BUTTON:
+ ui_SetButtonLabel(widget->widget, label);
+ break;
+ }
+}
+
diff --git a/src/t4kui/ui_proxywidget.h b/src/t4kui/ui_proxywidget.h
new file mode 100644
index 0000000..9730431
--- /dev/null
+++ b/src/t4kui/ui_proxywidget.h
@@ -0,0 +1,54 @@
+/*
+ * ui_proxywidget.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_PROXYWIDGET_H__
+#define __UI_PROXYWIDGET_H__
+
+#include <SDL/SDL.h>
+
+typedef struct _UI_ProxyWidget UI_ProxyWidget;
+
+typedef enum _WidgetType {
+ WT_BUTTON
+} WidgetType;
+
+UI_ProxyWidget *ui_CreateProxyWidget(void *widget, WidgetType type);
+void *ui_DestroyProxyWidget(UI_ProxyWidget *widget);
+
+void ui_onWidgetEvent(UI_ProxyWidget *widget, int x, int y);
+int ui_WidgetContains(UI_ProxyWidget *widget, int x, int y);
+
+void ui_SetWidgetPosition(UI_ProxyWidget *widget, int x, int y);
+
+void ui_PaintWidget(UI_ProxyWidget *widget, SDL_Surface *canvas);
+
+int ui_GetWidgetHeight(UI_ProxyWidget *widget);
+int ui_GetWidgetWidth(UI_ProxyWidget *widget);
+
+WidgetType ui_GetWidgetType(UI_ProxyWidget *widget);
+void *ui_GetWidget(UI_ProxyWidget *widget);
+
+char *ui_GetWidgetLabel(UI_ProxyWidget *widget);
+void ui_SetWidgetLabel(UI_ProxyWidget *widget, char *label);
+
+#endif /* __UI_PROXYWIDGET_H__ */
+
diff --git a/src/t4kui/ui_system.c b/src/t4kui/ui_system.c
new file mode 100644
index 0000000..5cd49ca
--- /dev/null
+++ b/src/t4kui/ui_system.c
@@ -0,0 +1,45 @@
+/*
+ * ui_system.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_system.h"
+#include <SDL/SDL_ttf.h>
+
+#include "SDL_extras.h"
+
+/*static TTF_Font *std_font;
+
+int ui_InitSystem() {
+ if(TTF_Init()==-1)
+ return -1;
+
+ if((std_font = TTF_OpenFont(FONT_PATH, FONT_SIZE))==NULL) {
+ return -1;
+ }
+ return 1;
+}
+*/
+
+/*
+TTF_Font *ui_GetFont() {
+ return front
+ //return std_font;
+}*/
diff --git a/src/t4kui/ui_system.h b/src/t4kui/ui_system.h
new file mode 100644
index 0000000..de30c8f
--- /dev/null
+++ b/src/t4kui/ui_system.h
@@ -0,0 +1,36 @@
+/*
+ * ui_system.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_SYSTEM_H__
+#define __UI_SYSTEM_H__
+
+//#include <SDL/SDL_ttf.h>
+
+//#define FONT_PATH "dejavu.ttf"
+#define FONT_SIZE 12
+
+/*
+int ui_InitSystem();
+TTF_Font *ui_GetFont();
+*/
+#endif /* __UI_SYSTEM_H__ */
+
diff --git a/src/t4kui/ui_window.c b/src/t4kui/ui_window.c
new file mode 100644
index 0000000..c377fbb
--- /dev/null
+++ b/src/t4kui/ui_window.c
@@ -0,0 +1,96 @@
+/*
+ * ui_window.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "ui_window.h"
+#include "ui_colors.h"
+#include "ui_layouts.h"
+#include "ui_proxywidget.h"
+#include "util.h"
+#include <SDL/SDL.h>
+
+struct _UI_Window {
+ int x, y, width, height;
+ SDL_Surface *canvas;
+ LinkedList *widgets;
+};
+
+struct _UI_Window *ui_CreateWindow(int x, int y, int width, int height) {
+ struct _UI_Window *window = malloc(sizeof(struct _UI_Window));
+ window->x = x;
+ window->y = y;
+ window->width = width;
+ window->height = height;
+ window->canvas = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32, 0, 0, 0, 0);
+ window->widgets = list_Create();
+ SDL_FillRect(window->canvas, NULL, C_WND);
+ return window;
+}
+
+void ui_DestroyWindow(struct _UI_Window *window) {
+ SDL_FreeSurface(window->canvas);
+ free(window);
+}
+
+static UI_ProxyWidget *_ui_FindWidget(struct _UI_Window *window, int x, int y) {
+ Node *node = list_GetFirst(window->widgets);
+ while(node!=NULL) {
+ if(ui_WidgetContains((UI_ProxyWidget *)list_GetNodeValue(node), x, y))
+ return (UI_ProxyWidget *)list_GetNodeValue(node);
+ node = list_Traverse(node);
+ }
+ return NULL;
+}
+
+void ui_onWindowEvent(struct _UI_Window *window, int x, int y) {
+ //note: the subtraction stuff on the line below converts screen coords to window coords
+ UI_ProxyWidget *widget = _ui_FindWidget(window, x - window->x, y - window->y);
+ if(widget!=NULL) {
+ ui_onWidgetEvent(widget, x, y);
+ }
+}
+
+int ui_WindowContains(struct _UI_Window *window, int x, int y) {
+ //fprintf(stdout, "X:%d Y:%d W:%d H:%d / px: %d py: %d\n", window->x, window->y, window->width, window->height, x, y);
+ return ((x > window->x) && (y > window->y) && (x < (window->x + window->width)) && (y < (window->y + window->height)))?1:0;
+}
+
+void ui_DrawWindow(struct _UI_Window *window, SDL_Surface *screen) {
+ //redraw widgets
+ Node *node = list_GetFirst(window->widgets);
+ while(node!=NULL) {
+ ui_PaintWidget((UI_ProxyWidget *)list_GetNodeValue(node), window->canvas);
+ node = list_Traverse(node);
+ }
+
+ SDL_Rect position;
+ position.x = window->x;
+ position.y = window->y;
+
+ SDL_BlitSurface(window->canvas, NULL, screen, &position);
+}
+
+void ui_AddWidget(struct _UI_Window *window, void *widget, WidgetType type) {
+ UI_ProxyWidget *proxy = ui_CreateProxyWidget(widget, type);
+ SDL_Rect pos = layout_FGetPos();
+ ui_SetWidgetPosition(proxy, pos.x, pos.y);
+ list_AddNode(window->widgets, proxy);
+}
diff --git a/src/t4kui/ui_window.h b/src/t4kui/ui_window.h
new file mode 100644
index 0000000..3c30dbd
--- /dev/null
+++ b/src/t4kui/ui_window.h
@@ -0,0 +1,41 @@
+/*
+ * ui_window.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UI_WINDOW_H__
+#define __UI_WINDOW_H__
+
+#include <SDL/SDL.h>
+#include "ui_proxywidget.h"
+
+typedef struct _UI_Window UI_Window;
+
+void ui_onWindowEvent(UI_Window *win, int x, int y);
+
+UI_Window *ui_CreateWindow(int x, int y, int width, int height);
+void ui_DrawWindow(UI_Window *window, SDL_Surface *screen);
+int ui_WindowContains(UI_Window *window, int x, int y);
+void ui_DestroyWindow(UI_Window *window);
+
+void ui_AddWidget(UI_Window *window, void *widget, WidgetType type);
+
+#endif /* __UI_WINDOW_H__ */
+
diff --git a/src/t4kui/util.c b/src/t4kui/util.c
new file mode 100644
index 0000000..1382480
--- /dev/null
+++ b/src/t4kui/util.c
@@ -0,0 +1,68 @@
+/*
+ * util.c
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "util.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+struct _LinkedList {
+ Node *first;
+ Node *last;
+};
+
+struct _Node {
+ void *data;
+ Node *next;
+};
+
+struct _LinkedList *list_Create() {
+ struct _LinkedList *list = malloc(sizeof(struct _LinkedList));
+ list->first = NULL;
+ list->last = NULL;
+ return list;
+}
+
+void list_Destroy(LinkedList *list) {
+ while((list->first)) {
+ Node *tmp = list->first;
+ list->first = tmp->next;
+ free(tmp);
+ }
+ free(list);
+}
+Node *list_GetFirst(LinkedList *list) {return list->first;}
+Node *list_Traverse(Node *node) {return node->next;}
+void *list_GetNodeValue(Node *node) {return node->data;}
+
+void list_AddNode(struct _LinkedList *list, void *val) {
+ struct _Node *node = malloc(sizeof(struct _Node));
+ node->data = val;
+ node->next = NULL;
+ if(list->last==NULL) {
+ list->first = node;
+ list->last = node;
+ return;
+ }
+ list->last->next = node;
+ list->last = node;
+}
+
diff --git a/src/t4kui/util.h b/src/t4kui/util.h
new file mode 100644
index 0000000..6eda82c
--- /dev/null
+++ b/src/t4kui/util.h
@@ -0,0 +1,37 @@
+/*
+ * util.h
+ * This file is part of Tux4Kids
+ *
+ * Copyright (C) 2010 - Aviral Dasgupta
+ *
+ * Tux4Kids 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tux4Kids 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 Tux4Kids; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef __UTIL_H__
+#define __UTIL_H__
+
+typedef struct _LinkedList LinkedList;
+typedef struct _Node Node;
+
+LinkedList *list_Create();
+void list_AddNode(LinkedList *list, void *val);
+Node *list_GetFirst(LinkedList *list);
+Node *list_Traverse(Node *node);
+void *list_GetNodeValue(Node *node);
+void list_Destroy(LinkedList *list);
+
+#endif /* __UTIL_H__ */
+
--
tuxhistory - Educational history game
More information about the Tux4kids-commits
mailing list