re-add basic scaling

This commit is contained in:
StapleButter 2017-10-02 03:34:17 +02:00
parent 09920126dd
commit d0770596ea
4 changed files with 84 additions and 6 deletions

View File

@ -91,6 +91,10 @@ void OnAreaDragBroken(uiAreaHandler* handler, uiArea* area)
{ {
} }
void OnAreaResize(uiAreaHandler* handler, uiArea* area, int width, int height)
{
}
int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt) int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
{ {
if (pollid < 0) if (pollid < 0)
@ -290,6 +294,7 @@ void Open()
areahandler.MouseCrossed = OnAreaMouseCrossed; areahandler.MouseCrossed = OnAreaMouseCrossed;
areahandler.DragBroken = OnAreaDragBroken; areahandler.DragBroken = OnAreaDragBroken;
areahandler.KeyEvent = OnAreaKeyEvent; areahandler.KeyEvent = OnAreaKeyEvent;
areahandler.Resize = OnAreaResize;
uiBox* top = uiNewVerticalBox(); uiBox* top = uiNewVerticalBox();
uiWindowSetChild(win, uiControl(top)); uiWindowSetChild(win, uiControl(top));

View File

@ -288,6 +288,8 @@ typedef struct uiAreaKeyEvent uiAreaKeyEvent;
typedef struct uiDrawContext uiDrawContext; typedef struct uiDrawContext uiDrawContext;
// TO CONSIDER: the uiAreaHandler param there seems useless
// (might use individual callbacks instead of handler struct?)
struct uiAreaHandler { struct uiAreaHandler {
void (*Draw)(uiAreaHandler *, uiArea *, uiAreaDrawParams *); void (*Draw)(uiAreaHandler *, uiArea *, uiAreaDrawParams *);
// TODO document that resizes cause a full redraw for non-scrolling areas; implementation-defined for scrolling areas // TODO document that resizes cause a full redraw for non-scrolling areas; implementation-defined for scrolling areas
@ -297,6 +299,7 @@ struct uiAreaHandler {
void (*MouseCrossed)(uiAreaHandler *, uiArea *, int left); void (*MouseCrossed)(uiAreaHandler *, uiArea *, int left);
void (*DragBroken)(uiAreaHandler *, uiArea *); void (*DragBroken)(uiAreaHandler *, uiArea *);
int (*KeyEvent)(uiAreaHandler *, uiArea *, uiAreaKeyEvent *); int (*KeyEvent)(uiAreaHandler *, uiArea *, uiAreaKeyEvent *);
void (*Resize)(uiAreaHandler *, uiArea *, int, int);
}; };
// TODO RTL layouts? // TODO RTL layouts?

View File

@ -37,6 +37,11 @@ static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
uiWindowsEnsureGetClientRect(a->hwnd, &client); uiWindowsEnsureGetClientRect(a->hwnd, &client);
areaDrawOnResize(a, &client); areaDrawOnResize(a, &client);
areaScrollOnResize(a, &client); areaScrollOnResize(a, &client);
{
double w, h;
loadAreaSize(a, a->rt, &w, &h);
a->ah->Resize(a->ah, a, (int)w, (int)h);
}
return 0; return 0;
} }

View File

@ -37,6 +37,9 @@
#include "../Platform.h" #include "../Platform.h"
const int kScreenGap[] = {0, 1, 8, 64, 90, 128};
uiWindow* MainWindow; uiWindow* MainWindow;
uiArea* MainDrawArea; uiArea* MainDrawArea;
@ -55,12 +58,16 @@ bool ScreenDrawInited = false;
uiDrawBitmap* ScreenBitmap = NULL; uiDrawBitmap* ScreenBitmap = NULL;
u32 ScreenBuffer[256*384]; u32 ScreenBuffer[256*384];
uiRect TopScreenRect;
uiRect BottomScreenRect;
bool Touching = false; bool Touching = false;
u32 KeyInputMask; u32 KeyInputMask;
SDL_Joystick* Joystick; SDL_Joystick* Joystick;
void AudioCallback(void* data, Uint8* stream, int len) void AudioCallback(void* data, Uint8* stream, int len)
{ {
SPU::ReadOutput((s16*)stream, len>>2); SPU::ReadOutput((s16*)stream, len>>2);
@ -236,11 +243,13 @@ void OnAreaDraw(uiAreaHandler* handler, uiArea* area, uiAreaDrawParams* params)
if (!ScreenBitmap) return; if (!ScreenBitmap) return;
uiRect dorp = {0, 0, 256, 384}; uiRect top = {0, 0, 256, 192};
uiRect bot = {0, 192, 256, 192};
uiDrawBitmapUpdate(ScreenBitmap, ScreenBuffer); uiDrawBitmapUpdate(ScreenBitmap, ScreenBuffer);
uiDrawBitmapDraw(params->Context, ScreenBitmap, &dorp, &dorp); uiDrawBitmapDraw(params->Context, ScreenBitmap, &top, &TopScreenRect);
uiDrawBitmapDraw(params->Context, ScreenBitmap, &bot, &BottomScreenRect);
} }
void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* evt) void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* evt)
@ -254,17 +263,23 @@ void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* ev
NDS::ReleaseKey(16+6); NDS::ReleaseKey(16+6);
NDS::ReleaseScreen(); NDS::ReleaseScreen();
} }
else if (!Touching && (evt->Down == 1) && (y >= 192)) else if (!Touching && (evt->Down == 1) &&
(x >= BottomScreenRect.X) && (y >= BottomScreenRect.Y) &&
(x < (BottomScreenRect.X+BottomScreenRect.Width)) && (y < (BottomScreenRect.Y+BottomScreenRect.Height)))
{ {
Touching = true; Touching = true;
NDS::PressKey(16+6); NDS::PressKey(16+6);
// TODO: scaling/offset as needed
} }
if (Touching) if (Touching)
{ {
// TODO: scaling, here too x -= BottomScreenRect.X;
y -= 192; y -= BottomScreenRect.Y;
if (BottomScreenRect.Width != 256)
x = (x * 256) / BottomScreenRect.Width;
if (BottomScreenRect.Height != 192)
y = (y * 192) / BottomScreenRect.Height;
// clamp // clamp
if (x < 0) x = 0; if (x < 0) x = 0;
@ -272,6 +287,7 @@ void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* ev
if (y < 0) y = 0; if (y < 0) y = 0;
else if (y > 191) y = 191; else if (y > 191) y = 191;
// TODO: take advantage of possible extra precision when possible? (scaled window for example)
NDS::TouchScreen(x, y); NDS::TouchScreen(x, y);
} }
} }
@ -308,6 +324,47 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
return 1; return 1;
} }
void OnAreaResize(uiAreaHandler* handler, uiArea* area, int width, int height)
{
float ratio = (height/2) / (float)width;
if (ratio <= 0.75)
{
// bars on the sides
int targetW = (height * 256) / 384;
int barW = (width - targetW) / 2;
TopScreenRect.X = barW;
TopScreenRect.Width = targetW;
TopScreenRect.Y = 0;
TopScreenRect.Height = height / 2;
BottomScreenRect.X = barW;
BottomScreenRect.Width = targetW;
BottomScreenRect.Y = height / 2;
BottomScreenRect.Height = height / 2;
}
else
{
// TODO: this should do bars on the top, and fixed screen gap
// for now we'll adjust the screen gap in consequence
int targetH = (width * 384) / 256;
int gap = height - targetH;
TopScreenRect.X = 0;
TopScreenRect.Width = width;
TopScreenRect.Y = 0;
TopScreenRect.Height = targetH / 2;
BottomScreenRect.X = 0;
BottomScreenRect.Width = width;
BottomScreenRect.Y = (targetH / 2) + gap;
BottomScreenRect.Height = targetH / 2;
}
}
void Run() void Run()
{ {
@ -569,6 +626,13 @@ int main(int argc, char** argv)
uiMenuItemOnClicked(menuitem, OnOpenEmuSettings, NULL); uiMenuItemOnClicked(menuitem, OnOpenEmuSettings, NULL);
menuitem = uiMenuAppendItem(menu, "Input config"); menuitem = uiMenuAppendItem(menu, "Input config");
uiMenuItemOnClicked(menuitem, OnOpenInputConfig, NULL); uiMenuItemOnClicked(menuitem, OnOpenInputConfig, NULL);
/*uiMenuAppendSeparator();
menuitem = uiMenuAppendItem(menu, "Mid-screen gap");
{
uiMenuItem* parent = menuitem;
//menuitem = uiMenu
// TODO: need submenu support in libui.
}*/
MainWindow = uiNewWindow("melonDS " MELONDS_VERSION, 256, 384, 1); MainWindow = uiNewWindow("melonDS " MELONDS_VERSION, 256, 384, 1);
uiWindowOnClosing(MainWindow, OnCloseWindow, NULL); uiWindowOnClosing(MainWindow, OnCloseWindow, NULL);
@ -589,6 +653,7 @@ int main(int argc, char** argv)
areahandler.MouseCrossed = OnAreaMouseCrossed; areahandler.MouseCrossed = OnAreaMouseCrossed;
areahandler.DragBroken = OnAreaDragBroken; areahandler.DragBroken = OnAreaDragBroken;
areahandler.KeyEvent = OnAreaKeyEvent; areahandler.KeyEvent = OnAreaKeyEvent;
areahandler.Resize = OnAreaResize;
MainDrawArea = uiNewArea(&areahandler); MainDrawArea = uiNewArea(&areahandler);
uiWindowSetChild(MainWindow, uiControl(MainDrawArea)); uiWindowSetChild(MainWindow, uiControl(MainDrawArea));