[Scummvm-cvs-logs] CVS: scummvm/gui dialog.h,NONE,1.1 widget.h,NONE,1.1 dialog.cpp,NONE,1.1 widget.cpp,NONE,1.1

Max Horn fingolfin at users.sourceforge.net
Fri Jul 5 09:57:17 CEST 2002


Update of /cvsroot/scummvm/scummvm/gui
In directory usw-pr-cvs1:/tmp/cvs-serv25508/gui

Added Files:
	dialog.h widget.h dialog.cpp widget.cpp 
Log Message:
new experimental GUI code

--- NEW FILE: dialog.h ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 The ScummVM project
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/gui/dialog.h,v 1.1 2002/07/05 16:56:53 fingolfin Exp $
 */

#ifndef DIALOG_H
#define DIALOG_H

#include "scummsys.h"


class Widget;
class NewGui;

class Dialog {
	friend class Widget;
	friend class StaticTextWidget;
protected:
	NewGui	*_gui;
	Widget	*_firstWidget;
	int16	_x, _y;
	uint16	_w, _h;
public:
	Dialog(NewGui *gui, int x, int y, int w, int h)
		: _gui(gui), _firstWidget(0), _x(x), _y(y), _w(w), _h(h)
		{}

	virtual void draw();

	//virtual void handleIdle(); // Called periodically
	virtual void handleClick(int x, int y, int button);
	virtual void handleKey(char key, int modifiers) // modifiers = alt/shift/ctrl etc.
		{ if (key == 27) close(); }
	virtual void handleCommand(uint32 cmd)
		{}

protected:
	Widget* findWidget(int x, int y); // Find the widget at pos x,y if any
	void close();

	void addResText(int x, int y, int w, int h, int resID);
	void addButton(int x, int y, int w, int h, char hotkey, const char *label, uint32 cmd);
	void addButton(int x, int y, int w, int h, char hotkey, int resID, uint32 cmd);
};

class SaveLoadDialog : public Dialog {
public:
	SaveLoadDialog(NewGui *gui);

	virtual void handleCommand(uint32 cmd);
};

class PauseDialog : public Dialog {
public:
	PauseDialog(NewGui *gui);

	virtual void handleClick(int x, int y, int button)
		{ close(); }
	virtual void handleKey(char key, int modifiers)
		{
			if (key == 32)
				close();
			else
				Dialog::handleKey(key, modifiers);
		}
};

#endif

--- NEW FILE: widget.h ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 The ScummVM project
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/gui/widget.h,v 1.1 2002/07/05 16:56:53 fingolfin Exp $
 */

#ifndef WIDGET_H
#define WIDGET_H

#include "scummsys.h"


class Dialog;

enum {
	WIDGET_ENABLED		= 1 << 0,
	WIDGET_INVISIBLE	= 1 << 1,
	WIDGET_BORDER		= 1 << 2,
	WIDGET_CLEARBG		= 1 << 3,
	WIDGET_WANT_TICKLE	= 1 << 4,
};

/* Widget */
class Widget {
friend class Dialog;
protected:
	Dialog		*_boss;
	Widget		*_next;
	int16		_x, _y;
	uint16		_w, _h;
	uint16		_id;
	int			_flags;
public:
	Widget(Dialog *boss, int x, int y, int w, int h);

	virtual void handleClick(int button) {}
	void draw();

	void setFlags(int flags)	{ _flags |= flags; }
	void clearFlags(int flags)	{ _flags &= ~flags; }
	int getFlags() const		{ return _flags; }

protected:
	virtual void drawWidget(bool hilite) {}
};


/* StaticTextWidget */
class StaticTextWidget : public Widget {
protected:
	const char	*_text;
public:
	StaticTextWidget(Dialog *boss, int x, int y, int w, int h, const char *text);
	void setText(const char *text);
	const char *getText();

protected:
	void drawWidget(bool hilite);
};


/* ButtonWidget */
class ButtonWidget : public StaticTextWidget {
protected:
	uint8	_hotkey;
	uint32	_cmd;
public:
	ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd);
	void setCmd(uint32 cmd);
	uint32 getCmd();
	void handleClick(int button);
};


#endif

--- NEW FILE: dialog.cpp ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 The ScummVM project
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/gui/dialog.cpp,v 1.1 2002/07/05 16:56:53 fingolfin Exp $
 */

#include "stdafx.h"
#include "dialog.h"
#include "widget.h"
#include "newgui.h"

void Dialog::draw()
{
	Widget *w = _firstWidget;
	
	_gui->clearArea(_x, _y, _w, _h);
	_gui->box(_x, _y, _w, _h);
	
	while (w) {
		w->draw();
		w = w->_next;
	}
}

void Dialog::handleClick(int x, int y, int button)
{
	Widget *w = findWidget(x - _x, y - _y);
	if (w)
		w->handleClick(button);
}

/*
 * Determine the widget at location (x,y) if any. Assumes the coordinates are
 * in the local coordinate system, i.e. relative to the top left of the dialog.
 */
Widget* Dialog::findWidget(int x, int y)
{
	Widget *w = _firstWidget;
	while (w) {
		// Stop as soon as we find a fidget that contains (x,y)
		if (x >= w->_x && x <= w->_x + w->_w && y >= w->_y && y <= w->_y + w->_h)
			break;
		w = w->_next;
	}
	return w;
}

void Dialog::close()
{
	// FIXME - this code should be inside the Gui class, and should be 
	// extended to support nested dialogs.
	_gui->restoreState();
	_gui->_active = false;
	_gui->_activeDialog = 0;
}

void Dialog::addResText(int x, int y, int w, int h, int resID)
{
	// Get the string
	const char *str = _gui->queryString(resID);
	if (!str)
		str = "Dummy!";
	new StaticTextWidget(this, x, y, w, h, str);
}

void Dialog::addButton(int x, int y, int w, int h, char hotkey, const char *label, uint32 cmd)
{
	new ButtonWidget(this, x, y, w, h, label, cmd);
	// TODO - handle hotkey
}

void Dialog::addButton(int x, int y, int w, int h, char hotkey, int resID, uint32 cmd)
{
	// Get the string
	const char *label = _gui->queryString(resID);
	if (!label)
		label = "Dummy!";
	addButton(x, y, w, h, hotkey, label, cmd);
}

#pragma mark -

enum {
	kSaveCmd	= 'SAVE',
	kLoadCmd	= 'LOAD',
	kPlayCmd	= 'PLAY',
	kOptionsCmd	= 'OPTN',
	kQuitCmd	= 'QUIT'
};

SaveLoadDialog::SaveLoadDialog(NewGui *gui)
	: Dialog(gui, 30, 20, 260, 124)
{
	addResText(10, 7, 240, 16, 1);
//	addResText(10, 7, 240, 16, 2);
//	addResText(10, 7, 240, 16, 3);
	
	addButton(200,  20, 54, 16, 'S', 4, kSaveCmd);		// Save
	addButton(200,  40, 54, 16, 'L', 5, kLoadCmd);		// Load
	addButton(200,  60, 54, 16, 'P', 6, kPlayCmd);		// Play
	addButton(200,  80, 54, 16, 'O', 17, kOptionsCmd);	// Options
	addButton(200, 100, 54, 16, 'Q', 8, kQuitCmd);		// Quit
}

void SaveLoadDialog::handleCommand(uint32 cmd)
{
	switch(cmd) {
	case kSaveCmd:
		break;
	case kLoadCmd:
		break;
	case kPlayCmd:
		close();
		break;
	case kOptionsCmd:
		break;
	case kQuitCmd:
		exit(1);
		break;
	}
}


#pragma mark -


PauseDialog::PauseDialog(NewGui *gui)
	: Dialog(gui, 50, 80, 220, 16)
{
	addResText(2, 2, 220, 16, 10);
}

--- NEW FILE: widget.cpp ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 The ScummVM project
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/gui/widget.cpp,v 1.1 2002/07/05 16:56:53 fingolfin Exp $
 */

#include "stdafx.h"
#include "widget.h"
#include "dialog.h"
#include "newgui.h"


Widget::Widget(Dialog *boss, int x, int y, int w, int h)
	: _boss(boss), _x(x), _y(y), _w(w), _h(h), _id(0), _flags(0)
{
	// Insert into the widget list of the boss
	_next = _boss->_firstWidget;
	_boss->_firstWidget = this;
}

void Widget::draw()
{
	if (_flags & WIDGET_INVISIBLE)
		return;

	// Account for our relative position in the dialog
	_x += _boss->_x;
	_y += _boss->_y;
	
	// Clear background
	if (_flags & WIDGET_CLEARBG)
		_boss->_gui->clearArea(_x, _y, _w, _h);
	
	// Draw border
	if (_flags & WIDGET_BORDER) {
		_boss->_gui->box(_x, _y, _w, _h);
		_x += 4;
		_y += 4;
	}
	
	// Now perform the actual widget draw
	drawWidget(false);
	
	if (_flags & WIDGET_BORDER) {
		_x -= 4;
		_y -= 4;
	}
	
	// Restore x/y
	_x -= _boss->_x;
	_y -= _boss->_y;
}


#pragma mark -


StaticTextWidget::StaticTextWidget(Dialog *boss, int x, int y, int w, int h, const char *text)
	: Widget(boss, x, y, w, h)
{
	// FIXME - maybe we should make a real copy of the string?
	_text = text;
}

void StaticTextWidget::drawWidget(bool hilite)
{
	NewGui	*gui = _boss->_gui;
	gui->drawString(_text, _x, _y, _w, hilite ? gui->_textcolorhi : gui->_textcolor);
}


#pragma mark -


ButtonWidget::ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd)
	: StaticTextWidget(boss, x, y, w, h, label), _cmd(cmd), _hotkey(0)
{
	_flags = WIDGET_ENABLED | WIDGET_BORDER /* | WIDGET_CLEARBG */;
}

void ButtonWidget::handleClick(int button)
{
	if (_flags & WIDGET_ENABLED)
		_boss->handleCommand(_cmd);
}





More information about the Scummvm-git-logs mailing list