Refactoring - Moved some code into input class

This commit is contained in:
Booklordofthedings 2024-06-29 17:41:49 +02:00
parent 6d06a13d64
commit 5661fffed1
2 changed files with 117 additions and 109 deletions

View file

@ -178,33 +178,15 @@ class App
}
Input.HandleScrolling(Raylib.GetMouseWheelMove());
if (Raylib.IsMouseButtonPressed(0))
HandleLeftClick();
Input.HandleLeftClick();
else if (Raylib.IsMouseButtonDown(0))
HandleButtonDown();
Input.HandleLeftDown();
else if (Raylib.IsMouseButtonPressed(1))
{
if (_Popups.Count > 0)
{
var top = _Popups[_Popups.Count - 1];
if (top is RightClickPopup)
{
_Popups.Remove(top);
delete top;
}
}
Toolbar.ToolbarCategory popupCat = new .("Items");
CurrentScreen.OnRightClick((.)Mouse.x, (.)Mouse.y, popupCat);
if (popupCat.Items.Count > 0)
_Popups.Add(new RightClickPopup((.)Mouse.x, (.)Mouse.y, popupCat));
else
delete popupCat;
}
Input.HandleRightClick();
else
HandleHover();
Input.HandleHover();
}
@ -266,94 +248,11 @@ class App
_Popups.Add(popup);
}
///Handles the leftclick action
private static void HandleLeftClick()
public static Result<Popup> GetTopmostPopup()
{
if (_Popups.Count > 0)
{ //Only checking the topmost popup
var top = _Popups[_Popups.Count - 1];
if (Rectangle(top.X, top.Y, top.Width, top.Height).Overlaps(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - Wheel)))
{
if (!top.OnClick(Raylib.GetMouseX(), Raylib.GetMouseY()))
{
_Popups.Remove(top);
top.OnClose(); //Any last words ?
delete top;
}
}
else
{
bool a = top is DropdownPopup; //TODO: Make this less horrible
_Popups.Remove(top);
top.OnClose(); //Any last words ?
delete top;
if (!a)
CurrentScreen.OnClick(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - App.Wheel)); //So that we dont eat any inputs
}
}
else
CurrentScreen.OnClick(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - App.Wheel));
}
private static bool HandleButtonDown()
{
//Topmost first
for (var i in _Popups.Reversed)
{
var hoverResult = i.OnDown(Raylib.GetMouseX(), Raylib.GetMouseY() - (.)Wheel);
if (hoverResult == false)
continue;
return hoverResult;
}
return CurrentScreen.OnDown(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - App.Wheel));
}
///Handles the hovering effect
private static void HandleHover()
{
Component hoverRes = HandleHoverPopups();
if (hoverRes == null)
hoverRes = HandleHoverScreen();
if (hoverRes == null)
Raylib.SetMouseCursor((.)MouseCursor.MOUSE_CURSOR_DEFAULT);
else
{
if (hoverRes.Enabled)
Raylib.SetMouseCursor((.)MouseCursor.MOUSE_CURSOR_POINTING_HAND);
if (_OldPos != Mouse)
_MovTime = Raylib.GetTime();
if (Raylib.GetTime() - _MovTime >= 0.5)
_HoverText.Append(hoverRes.Description);
_OldPos = Mouse;
}
}
///Handle the hover effect for all screen objects
private static Component HandleHoverScreen()
{
return CurrentScreen.OnHover(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - App.Wheel));
}
///Checks if any popup catches the hover
private static Component HandleHoverPopups()
{
//Topmost first
for (var i in _Popups.Reversed)
{
var hoverResult = i.OnHover(Raylib.GetMouseX(), Raylib.GetMouseY() - (.)Wheel);
if (hoverResult == null)
continue;
return hoverResult;
}
return null;
if(_Popups.Count < 1)
return .Err;
return .Ok(_Popups[^1]);
}
}

View file

@ -2,6 +2,10 @@ namespace TheaterGui;
using System;
using RaylibBeef;
using TheaterGui.Components;
class Input
{
internal static void HandleScrolling(float pWheelMove)
@ -14,4 +18,109 @@ class Input
App.Wheel = Math.Clamp(App.Wheel, -1 * (App.CurrentScreen.Height - App.Height), 0);
}
}
internal static void HandleLeftClick()
{
var mouse = App.Mouse;
if (App.GetTopmostPopup() case .Ok(let top))
{
if (Rectangle(top.X, top.Y, top.Width, top.Height).Overlaps((.)mouse.x, (.)(mouse.y - App.Wheel)))
{
if (top.OnClick((.)mouse.x, (.)mouse.y))
return;
App.[Friend]_Popups.Remove(top);
top.OnClose(); //Any last words ?
delete top;
}
else
{
bool a = top is DropdownPopup; //TODO: Make this less horrible
App.[Friend]_Popups.Remove(top);
top.OnClose(); //Any last words ?
delete top;
if(!a)
App.CurrentScreen.OnClick((.)mouse.x, (.)(mouse.y - App.Wheel)); //So that we dont eat any inputs
}
}
else
App.CurrentScreen.OnClick((.)mouse.x, (.)(mouse.y - App.Wheel));
}
internal static void HandleRightClick()
{
if (App.[Friend]_Popups.Count > 0)
{
var top = App.[Friend]_Popups[App.[Friend]_Popups.Count - 1];
if (top is RightClickPopup)
{
App.[Friend]_Popups.Remove(top);
delete top;
}
}
Toolbar.ToolbarCategory popupCat = new .("Items");
App.CurrentScreen.OnRightClick((.)App.Mouse.x, (.)App.Mouse.y, popupCat);
if (popupCat.Items.Count > 0)
App.[Friend]_Popups.Add(new RightClickPopup((.)App.Mouse.x, (.)App.Mouse.y, popupCat));
else
delete popupCat;
}
internal static bool HandleLeftDown()
{
//Topmost first
for (var i in App.[Friend]_Popups.Reversed)
{
var hoverResult = i.OnDown(Raylib.GetMouseX(), Raylib.GetMouseY() - (.)App.Wheel);
if (hoverResult == false)
continue;
return hoverResult;
}
return App.CurrentScreen.OnDown(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - App.Wheel));
}
internal static void HandleHover()
{
Component hoverRes = HandleHoverPopups();
if (hoverRes == null)
hoverRes = HandleHoverScreen();
if (hoverRes == null)
Raylib.SetMouseCursor((.)MouseCursor.MOUSE_CURSOR_DEFAULT);
else
{
if (hoverRes.Enabled)
Raylib.SetMouseCursor((.)MouseCursor.MOUSE_CURSOR_POINTING_HAND);
if (App.[Friend]_OldPos != App.Mouse)
App.[Friend]_MovTime = Raylib.GetTime();
if (Raylib.GetTime() - App.[Friend]_MovTime >= 0.5)
App.[Friend]_HoverText.Append(hoverRes.Description);
App.[Friend]_OldPos = App.Mouse;
}
}
///Handle the hover effect for all screen objects
private static Component HandleHoverScreen()
{
return App.CurrentScreen.OnHover(Raylib.GetMouseX(), (.)(Raylib.GetMouseY() - App.Wheel));
}
///Checks if any popup catches the hover
private static Component HandleHoverPopups()
{
//Topmost first
for (var i in App.[Friend]_Popups.Reversed)
{
var hoverResult = i.OnHover(Raylib.GetMouseX(), Raylib.GetMouseY() - (.)App.Wheel);
if (hoverResult == null)
continue;
return hoverResult;
}
return null;
}
}