Patch #1092994 (Selfscaling GUI)

svn-id: r16455
This commit is contained in:
Max Horn 2005-01-06 19:09:34 +00:00
parent 5d88c39549
commit 4fae197c67
7 changed files with 158 additions and 81 deletions

View file

@ -20,6 +20,7 @@
#include "common/stdafx.h"
#include "graphics/font.h"
#include "gui/newgui.h"
namespace Graphics {
@ -36,8 +37,11 @@ int NewFont::getCharWidth(byte chr) const {
return desc.width[chr - desc.firstchar];
}
void NewFont::drawChar(const Surface *dst, byte chr, int tx, int ty, uint32 color) const {
void NewFont::drawChar(const Surface *dst, byte chr, int tx, int ty, uint32 color, bool scale) const {
assert(dst != 0);
const int scaleFactor = scale ? g_gui.getScaleFactor() : 1;
tx *= scaleFactor; ty *= scaleFactor;
byte *ptr = (byte *)dst->getBasePtr(tx, ty);
assert(desc.bits != 0 && desc.maxwidth <= 16);
@ -54,16 +58,30 @@ void NewFont::drawChar(const Surface *dst, byte chr, int tx, int ty, uint32 colo
chr -= desc.firstchar;
const bitmap_t *tmp = desc.bits + (desc.offset ? desc.offset[chr] : (chr * desc.height));
for (int y = 0; y < desc.height; y++, ptr += dst->pitch) {
const bitmap_t buffer = *tmp++;
for (int y = 0; y < desc.height * scaleFactor; y++, ptr += dst->pitch) {
const bitmap_t *buffer = 0;
if(scaleFactor != 1) {
if(!(y % 2))
buffer = tmp++;
else
buffer = tmp;
} else
buffer = tmp++;
bitmap_t mask = 0x8000;
if (ty + y < 0 || ty + y >= dst->h)
continue;
for (int x = 0; x < w; x++, mask >>= 1) {
for (int x = 0; x < w * scaleFactor; x++) {
if(scaleFactor != 1) {
if(!(x % 2) && x != 0)
mask >>= 1;
} else if(x != 0) {
mask >>= 1;
}
if (tx + x < 0 || tx + x >= dst->w)
continue;
if ((buffer & mask) != 0) {
if ((*buffer & mask) != 0) {
if (dst->bytesPerPixel == 1)
ptr[x] = color;
else if (dst->bytesPerPixel == 2)
@ -85,7 +103,7 @@ int Font::getStringWidth(const Common::String &str) const {
return space;
}
void Font::drawString(const Surface *dst, const Common::String &s, int x, int y, int w, uint32 color, TextAlignment align, int deltax, bool useEllipsis) const {
void Font::drawString(const Surface *dst, const Common::String &s, int x, int y, int w, uint32 color, TextAlignment align, int deltax, bool useEllipsis, bool scale) const {
assert(dst != 0);
const int leftX = x, rightX = x + w;
uint i;
@ -150,7 +168,7 @@ void Font::drawString(const Surface *dst, const Common::String &s, int x, int y,
if (x+w > rightX)
break;
if (x >= leftX)
drawChar(dst, str[i], x, y, color);
drawChar(dst, str[i], x, y, color, scale);
x += w;
}
}

View file

@ -52,9 +52,9 @@ public:
virtual int getMaxCharWidth() const = 0;
virtual int getCharWidth(byte chr) const = 0;
virtual void drawChar(const Surface *dst, byte chr, int x, int y, uint32 color) const = 0;
virtual void drawChar(const Surface *dst, byte chr, int x, int y, uint32 color, bool scale = false) const = 0;
void drawString(const Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlignment align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true) const;
void drawString(const Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlignment align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true, bool scale = false) const;
int getStringWidth(const Common::String &str) const;
};
@ -65,7 +65,7 @@ public:
virtual int getMaxCharWidth() const { return 8; };
virtual int getCharWidth(byte chr) const;
virtual void drawChar(const Surface *dst, byte chr, int x, int y, uint32 color) const;
virtual void drawChar(const Surface *dst, byte chr, int x, int y, uint32 color, bool scale) const;
};
extern const ScummFont g_scummfont;
@ -101,7 +101,7 @@ public:
virtual int getMaxCharWidth() const { return desc.maxwidth; };
virtual int getCharWidth(byte chr) const;
virtual void drawChar(const Surface *dst, byte chr, int x, int y, uint32 color) const;
virtual void drawChar(const Surface *dst, byte chr, int x, int y, uint32 color, bool scale) const;
};
extern const NewFont g_sysfont;

View file

@ -20,6 +20,7 @@
#include "stdafx.h"
#include "graphics/font.h"
#include "gui/newgui.h"
namespace Graphics {
@ -62,24 +63,36 @@ int ScummFont::getCharWidth(byte chr) const {
}
//void ScummFont::drawChar(byte chr, int xx, int yy, OverlayColor color) {
void ScummFont::drawChar(const Surface *dst, byte chr, int tx, int ty, uint32 color) const {
void ScummFont::drawChar(const Surface *dst, byte chr, int tx, int ty, uint32 color, bool scale) const {
assert(dst != 0);
const int scaleFactor = scale ? g_gui.getScaleFactor() : 1;
tx *= scaleFactor; ty *= scaleFactor;
byte *ptr = (byte *)dst->getBasePtr(tx, ty);
const byte *tmp = guifont + 6 + guifont[4] + chr * 8;
uint buffer = 0;
uint mask = 0;
for (int y = 0; y < 8; y++) {
for (int y = 0; y < 8 * scaleFactor; y++) {
if (ty + y < 0 || ty + y >= dst->h)
continue;
for (int x = 0; x < 8; x++) {
for (int x = 0; x < 8 * scaleFactor; x++) {
if(scaleFactor != 1 && !(x % 2))
mask >>= 1;
else if(scaleFactor == 1)
mask >>= 1;
if (tx + x < 0 || tx + x >= dst->w)
continue;
unsigned char c;
mask >>= 1;
if (mask == 0) {
if(scaleFactor != 1 && !(y % 2))
buffer = *tmp++;
else if(scaleFactor == 1)
buffer = *tmp++;
mask = 0x80;
}
c = ((buffer & mask) != 0);

View file

@ -111,6 +111,9 @@ void ConsoleDialog::slideUpAndClose() {
}
void ConsoleDialog::open() {
// disable scaling because the console is using non fixed positions
g_gui.enableScaling(false);
// Initiate sliding the console down. We do a very simple trick to achieve
// this effect: we simply move the console dialog just above (outside) the
// visible screen area, then shift it down in handleTickle() over a

View file

@ -117,7 +117,10 @@ void Dialog::drawDialog() {
}
void Dialog::handleMouseDown(int x, int y, int button, int clickCount) {
x /= g_gui.getScaleFactor(); y /= g_gui.getScaleFactor();
Widget *w;
w = findWidget(x, y);
_dragWidget = w;
@ -141,6 +144,8 @@ void Dialog::handleMouseDown(int x, int y, int button, int clickCount) {
}
void Dialog::handleMouseUp(int x, int y, int button, int clickCount) {
x /= g_gui.getScaleFactor(); y /= g_gui.getScaleFactor();
Widget *w;
if (_focusedWidget) {
@ -161,6 +166,8 @@ void Dialog::handleMouseUp(int x, int y, int button, int clickCount) {
}
void Dialog::handleMouseWheel(int x, int y, int direction) {
x /= g_gui.getScaleFactor(); y /= g_gui.getScaleFactor();
Widget *w;
// This may look a bit backwards, but I think it makes more sense for
@ -212,6 +219,8 @@ void Dialog::handleKeyUp(uint16 ascii, int keycode, int modifiers) {
}
void Dialog::handleMouseMoved(int x, int y, int button) {
x /= g_gui.getScaleFactor(); y /= g_gui.getScaleFactor();
Widget *w;
//if (!button)

View file

@ -54,7 +54,7 @@ enum {
// Constructor
NewGui::NewGui() : _needRedraw(false),
NewGui::NewGui() : _scaleEnable(true), _needRedraw(false),
_stateIsSaved(false), _cursorAnimateCounter(0), _cursorAnimateTimer(0) {
_system = &OSystem::instance();
@ -64,6 +64,9 @@ NewGui::NewGui() : _needRedraw(false),
// Reset key repeat
_currentKeyDown.keycode = 0;
// updates the scaling factor
updateScaleFactor();
}
void NewGui::updateColors() {
@ -75,6 +78,20 @@ void NewGui::updateColors() {
_textcolorhi = _system->RGBToColor(0, 255, 0);
}
void NewGui::updateScaleFactor() {
if(!_scaleEnable) {
_scaleFactor = 1;
return;
}
enum {
kDefaultGUIWidth = 320,
kDefaultGUIHeight = 200
};
_scaleFactor = MIN(_system->getWidth() / kDefaultGUIWidth, _system->getHeight() / kDefaultGUIHeight);
}
void NewGui::runLoop() {
Dialog *activeDialog = _dialogStack.top();
bool didSaveState = false;
@ -86,6 +103,7 @@ void NewGui::runLoop() {
// EVENT_SCREEN_CHANGED is received. However, not yet all backends support
// that event, so we also do it "manually" whenever a run loop is entered.
updateColors();
updateScaleFactor();
if (!_stateIsSaved) {
saveState();
@ -131,7 +149,7 @@ void NewGui::runLoop() {
_currentKeyDown.keycode = 0;
break;
case OSystem::EVENT_MOUSEMOVE:
activeDialog->handleMouseMoved(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y, 0);
activeDialog->handleMouseMoved(event.mouse.x - (activeDialog->_x * _scaleFactor), event.mouse.y - (activeDialog->_y * _scaleFactor), 0);
break;
// We don't distinguish between mousebuttons (for now at least)
case OSystem::EVENT_LBUTTONDOWN:
@ -146,23 +164,24 @@ void NewGui::runLoop() {
_lastClick.count = 1;
}
_lastClick.time = time;
activeDialog->handleMouseDown(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y, 1, _lastClick.count);
activeDialog->handleMouseDown(event.mouse.x - (activeDialog->_x * _scaleFactor), event.mouse.y - (activeDialog->_y * _scaleFactor), 1, _lastClick.count);
break;
case OSystem::EVENT_LBUTTONUP:
case OSystem::EVENT_RBUTTONUP:
activeDialog->handleMouseUp(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y, 1, _lastClick.count);
activeDialog->handleMouseUp(event.mouse.x - (activeDialog->_x * _scaleFactor), event.mouse.y - (activeDialog->_y * _scaleFactor), 1, _lastClick.count);
break;
case OSystem::EVENT_WHEELUP:
activeDialog->handleMouseWheel(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y, -1);
activeDialog->handleMouseWheel(event.mouse.x - (activeDialog->_x * _scaleFactor), event.mouse.y - (activeDialog->_y * _scaleFactor), -1);
break;
case OSystem::EVENT_WHEELDOWN:
activeDialog->handleMouseWheel(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y, 1);
activeDialog->handleMouseWheel(event.mouse.x - (activeDialog->_x * _scaleFactor), event.mouse.y - (activeDialog->_y * _scaleFactor), 1);
break;
case OSystem::EVENT_QUIT:
_system->quit();
return;
case OSystem::EVENT_SCREEN_CHANGED:
updateColors();
updateScaleFactor();
activeDialog->handleScreenChanged();
break;
}
@ -212,6 +231,7 @@ void NewGui::saveState() {
_lastClick.count = 0;
_stateIsSaved = true;
_scaleEnable = true;
}
void NewGui::restoreState() {
@ -270,18 +290,18 @@ void NewGui::box(int x, int y, int width, int height, OverlayColor colorA, Overl
}
void NewGui::hLine(int x, int y, int x2, OverlayColor color) {
_screen.hLine(x, y, x2, color);
_screen.hLine(x * _scaleFactor, y * _scaleFactor, x2 * _scaleFactor, color);
}
void NewGui::vLine(int x, int y, int y2, OverlayColor color) {
_screen.vLine(x, y, y2, color);
_screen.vLine(x * _scaleFactor, y * _scaleFactor, y2 * _scaleFactor, color);
}
void NewGui::blendRect(int x, int y, int w, int h, OverlayColor color, int level) {
#ifdef NEWGUI_256
fillRect(x, y, w, h, color);
#else
Common::Rect rect(x, y, x + w, y + h);
Common::Rect rect(x * _scaleFactor, y * _scaleFactor, (x + w) * _scaleFactor, (y + h) * _scaleFactor);
rect.clip(_screen.w, _screen.h);
if (!rect.isValidRect())
@ -311,15 +331,15 @@ void NewGui::blendRect(int x, int y, int w, int h, OverlayColor color, int level
}
void NewGui::fillRect(int x, int y, int w, int h, OverlayColor color) {
_screen.fillRect(Common::Rect(x, y, x+w, y+h), color);
_screen.fillRect(Common::Rect(x * _scaleFactor, y * _scaleFactor, (x+w) * _scaleFactor, (y+h) * _scaleFactor), color);
}
void NewGui::frameRect(int x, int y, int w, int h, OverlayColor color) {
_screen.frameRect(Common::Rect(x, y, x+w, y+h), color);
_screen.frameRect(Common::Rect(x * _scaleFactor, y * _scaleFactor, (x+w) * _scaleFactor, (y+h) * _scaleFactor), color);
}
void NewGui::addDirtyRect(int x, int y, int w, int h) {
Common::Rect rect(x, y, x + w, y + h);
Common::Rect rect(x * _scaleFactor, y * _scaleFactor, (x + w) * _scaleFactor, (y + h) * _scaleFactor);
rect.clip(_screen.w, _screen.h);
if (!rect.isValidRect())
@ -335,7 +355,7 @@ void NewGui::addDirtyRect(int x, int y, int w, int h) {
void NewGui::drawChar(byte chr, int xx, int yy, OverlayColor color, const Graphics::Font *font) {
if (font == 0)
font = &getFont();
font->drawChar(&_screen, chr, xx, yy, color);
font->drawChar(&_screen, chr, xx, yy, color, _scaleEnable);
}
int NewGui::getStringWidth(const String &str) {
@ -347,23 +367,30 @@ int NewGui::getCharWidth(byte c) {
}
void NewGui::drawString(const String &s, int x, int y, int w, OverlayColor color, TextAlignment align, int deltax, bool useEllipsis) {
getFont().drawString(&_screen, s, x, y, w, color, align, deltax, useEllipsis);
getFont().drawString(&_screen, s, x, y, w, color, align, deltax, useEllipsis, _scaleEnable);
}
//
// Draw an 8x8 bitmap at location (x,y)
//
void NewGui::drawBitmap(uint32 *bitmap, int tx, int ty, OverlayColor color, int h) {
tx *= _scaleFactor; ty *= _scaleFactor;
h *= _scaleFactor;
OverlayColor *ptr = getBasePtr(tx, ty);
for (int y = 0; y < h; y++, ptr += _screenPitch) {
uint32 mask = 0xF0000000;
if (ty + y < 0 || ty + y >= _screen.h)
continue;
for (int x = 0; x < 8; x++, mask >>= 4) {
for (int x = 0; x < 8 * _scaleFactor; x++) {
if(!(x % 2) && _scaleFactor != 1 && x != 0)
mask >>= 4;
else if(_scaleFactor == 1)
mask >>= 4;
if (tx + x < 0 || tx + x >= _screen.w)
continue;
if (bitmap[y] & mask)
if (bitmap[y / _scaleFactor] & mask)
ptr[x] = color;
}
}

View file

@ -66,11 +66,17 @@ public:
bool isActive() { return ! _dialogStack.empty(); }
int getScaleFactor() { return _scaleFactor; }
void enableScaling(bool enable) { _scaleEnable = enable; updateScaleFactor(); }
protected:
OSystem *_system;
Graphics::Surface _screen;
int _screenPitch;
int _scaleFactor;
bool _scaleEnable;
bool _needRedraw;
DialogStack _dialogStack;
@ -107,6 +113,7 @@ protected:
void animateCursor();
void updateColors();
void updateScaleFactor();
OverlayColor *getBasePtr(int x, int y);