From d0770596ea1e05f5a5d051cc461a2305d0f458fc Mon Sep 17 00:00:00 2001
From: StapleButter <thetotalworm@gmail.com>
Date: Mon, 2 Oct 2017 03:34:17 +0200
Subject: re-add basic scaling

---
 src/libui_sdl/DlgInputConfig.cpp     |  5 +++
 src/libui_sdl/libui/ui.h             |  3 ++
 src/libui_sdl/libui/windows/area.cpp |  5 +++
 src/libui_sdl/main.cpp               | 77 +++++++++++++++++++++++++++++++++---
 4 files changed, 84 insertions(+), 6 deletions(-)

(limited to 'src')

diff --git a/src/libui_sdl/DlgInputConfig.cpp b/src/libui_sdl/DlgInputConfig.cpp
index ccacb64..2d26001 100644
--- a/src/libui_sdl/DlgInputConfig.cpp
+++ b/src/libui_sdl/DlgInputConfig.cpp
@@ -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)
 {
     if (pollid < 0)
@@ -290,6 +294,7 @@ void Open()
     areahandler.MouseCrossed = OnAreaMouseCrossed;
     areahandler.DragBroken = OnAreaDragBroken;
     areahandler.KeyEvent = OnAreaKeyEvent;
+    areahandler.Resize = OnAreaResize;
 
     uiBox* top = uiNewVerticalBox();
     uiWindowSetChild(win, uiControl(top));
diff --git a/src/libui_sdl/libui/ui.h b/src/libui_sdl/libui/ui.h
index f1937e2..84a71c3 100644
--- a/src/libui_sdl/libui/ui.h
+++ b/src/libui_sdl/libui/ui.h
@@ -288,6 +288,8 @@ typedef struct uiAreaKeyEvent uiAreaKeyEvent;
 
 typedef struct uiDrawContext uiDrawContext;
 
+// TO CONSIDER: the uiAreaHandler param there seems useless
+// (might use individual callbacks instead of handler struct?)
 struct uiAreaHandler {
 	void (*Draw)(uiAreaHandler *, uiArea *, uiAreaDrawParams *);
 	// 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 (*DragBroken)(uiAreaHandler *, uiArea *);
 	int (*KeyEvent)(uiAreaHandler *, uiArea *, uiAreaKeyEvent *);
+	void (*Resize)(uiAreaHandler *, uiArea *, int, int);
 };
 
 // TODO RTL layouts?
diff --git a/src/libui_sdl/libui/windows/area.cpp b/src/libui_sdl/libui/windows/area.cpp
index 74fe768..6bb8fe7 100644
--- a/src/libui_sdl/libui/windows/area.cpp
+++ b/src/libui_sdl/libui/windows/area.cpp
@@ -37,6 +37,11 @@ static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
 		uiWindowsEnsureGetClientRect(a->hwnd, &client);
 		areaDrawOnResize(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;
 	}
 
diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp
index 1d5fcae..4f2285a 100644
--- a/src/libui_sdl/main.cpp
+++ b/src/libui_sdl/main.cpp
@@ -37,6 +37,9 @@
 #include "../Platform.h"
 
 
+const int kScreenGap[] = {0, 1, 8, 64, 90, 128};
+
+
 uiWindow* MainWindow;
 uiArea* MainDrawArea;
 
@@ -55,12 +58,16 @@ bool ScreenDrawInited = false;
 uiDrawBitmap* ScreenBitmap = NULL;
 u32 ScreenBuffer[256*384];
 
+uiRect TopScreenRect;
+uiRect BottomScreenRect;
+
 bool Touching = false;
 
 u32 KeyInputMask;
 SDL_Joystick* Joystick;
 
 
+
 void AudioCallback(void* data, Uint8* stream, int len)
 {
     SPU::ReadOutput((s16*)stream, len>>2);
@@ -236,11 +243,13 @@ void OnAreaDraw(uiAreaHandler* handler, uiArea* area, uiAreaDrawParams* params)
 
     if (!ScreenBitmap) return;
 
-    uiRect dorp = {0, 0, 256, 384};
+    uiRect top = {0, 0, 256, 192};
+    uiRect bot = {0, 192, 256, 192};
 
     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)
@@ -254,17 +263,23 @@ void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* ev
         NDS::ReleaseKey(16+6);
         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;
         NDS::PressKey(16+6);
-        // TODO: scaling/offset as needed
     }
 
     if (Touching)
     {
-        // TODO: scaling, here too
-        y -= 192;
+        x -= BottomScreenRect.X;
+        y -= BottomScreenRect.Y;
+
+        if (BottomScreenRect.Width != 256)
+            x = (x * 256) / BottomScreenRect.Width;
+        if (BottomScreenRect.Height != 192)
+            y = (y * 192) / BottomScreenRect.Height;
 
         // clamp
         if (x < 0) x = 0;
@@ -272,6 +287,7 @@ void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* ev
         if (y < 0) y = 0;
         else if (y > 191) y = 191;
 
+        // TODO: take advantage of possible extra precision when possible? (scaled window for example)
         NDS::TouchScreen(x, y);
     }
 }
@@ -308,6 +324,47 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
     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()
 {
@@ -569,6 +626,13 @@ int main(int argc, char** argv)
     uiMenuItemOnClicked(menuitem, OnOpenEmuSettings, NULL);
     menuitem = uiMenuAppendItem(menu, "Input config");
     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);
     uiWindowOnClosing(MainWindow, OnCloseWindow, NULL);
@@ -589,6 +653,7 @@ int main(int argc, char** argv)
     areahandler.MouseCrossed = OnAreaMouseCrossed;
     areahandler.DragBroken = OnAreaDragBroken;
     areahandler.KeyEvent = OnAreaKeyEvent;
+    areahandler.Resize = OnAreaResize;
 
     MainDrawArea = uiNewArea(&areahandler);
     uiWindowSetChild(MainWindow, uiControl(MainDrawArea));
-- 
cgit v1.2.3