Skip to content

Commit

Permalink
Virtual Keyboard Added
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAndreiM committed Jul 27, 2024
1 parent a0999c1 commit b68553e
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 56 deletions.
60 changes: 54 additions & 6 deletions src/Juka/GUI/Clicked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,50 @@ public class Clicked

public static void itemclicked()
{
if (keyboardOn == true)
{
for (int i = 0; i < menuOptions[Menus.VirtualKeyboard].Count; i++)
{
SDL.SDL_Rect keyRect = new SDL.SDL_Rect { x = menuDescript[Menus.VirtualKeyboard][i].X, y = menuDescript[Menus.VirtualKeyboard][i].Y, w = menuDescript[Menus.VirtualKeyboard][i].Width, h = menuDescript[Menus.VirtualKeyboard][i].Height };

if (Helper.HandleSelection(mouseX, mouseY, keyRect))
{
if (menuOptions[Menus.VirtualKeyboard][i] == "<-")
{
if (keyboardbuffer.Length > 0)
{
keyboardbuffer = keyboardbuffer.Substring(0, keyboardbuffer.Length - 1);
}
} else if(menuOptions[Menus.VirtualKeyboard][i] == "Ext")
{
keyboardOn = false;
}
else if (menuOptions[Menus.VirtualKeyboard][i] == "Ent")
{
if (Menus.MediaPlayer == currentscreen)
{
var apikey = "AIzaSyBzpZzE4nQVxr_EQLgWqTfREpvWON - gWu8";
var youtube = new YouTubeApiService(apikey);
var videos = youtube.GetTopVideosSync();

videoInfos = videos.Select(v => new VideoInfo
{
VideoId = v.VideoId,
Title = v.Title,
Description = v.Description
}).ToList();
}
keyboardOn = false;
}
else
{
keyboardbuffer += menuOptions[Menus.VirtualKeyboard][i];
}
}
}
}

if (currentscreen == Menus.MenuMain)
else if (currentscreen == Menus.MenuMain)
{
for (int i = 0; i < menuOptions[Menus.MenuMain].Count; i++)
{
Expand Down Expand Up @@ -67,19 +109,25 @@ public static void itemclicked()
}
else if (currentscreen == Menus.MediaPlayer)
{
for (int i = 0; i < videoInfos.Count + 1; i++)
for (int i = 0; i < videoInfos.Count + 2; i++)
{
SDL.SDL_Rect optionRect = new SDL.SDL_Rect { x = 160, y = 190 + fontSizeC * (i + 1) + 5, w = 1280 - 160, h = fontSizeC };
SDL.SDL_Rect optionRect = new SDL.SDL_Rect { x = menuDescript[Menus.MediaPlayer][i].X, y = menuDescript[Menus.MediaPlayer][i].Y, w = menuDescript[Menus.MediaPlayer][i].Width, h = menuDescript[Menus.MediaPlayer][i].Height };
if (Helper.HandleSelection(mouseX, mouseY, optionRect))
{
if (i == videoInfos.Count)
if (menuOptions[Menus.MediaPlayer][i] == "Back")
{
currentscreen = 0;
YouTubeApiService.DeleteThumbnails(videoInfos);
currentscreen = Menus.MenuMain;
}
else if(menuOptions[Menus.MediaPlayer][i] == "Search")
{
keyboardy = 159;
keyboardOn = true;
}
else
{

var VedioUrl = "https://www.youtube.com/embed/" + videoInfos[i].VideoId + ".mp4";
var VedioUrl = "https://www.youtube.com/embed/" + menuOptions[Menus.MediaPlayer][i] + ".mp4";
var youTube = YouTube.Default;
var video = youTube.GetVideo(VedioUrl);
File.WriteAllBytes(videoInfos[i].VideoId + ".mp4", video.GetBytes());
Expand Down
15 changes: 14 additions & 1 deletion src/Juka/GUI/Globals.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SDL2;
using VideoLibrary;

namespace Juka.GUI
{
Expand Down Expand Up @@ -33,10 +34,21 @@ public static class Globals
public static nint controller; //GET THE CONTROLLER
public static bool quit = false; //Render UNTIL QUIT


//keyboard
public static string keyboardbuffer = "";

// Screen Selection
public static int mouseX = SCREEN_WIDTH / 2;
public static int mouseY = SCREEN_HEIGHT / 2;

public static nint fontBig;
public static nint fontFooter;
public static nint menufont;
public static nint descriptionfont;
public static bool keyboardOn = false;
public static int keyboardy = SCREEN_HEIGHT / 2;

public enum Menus
{
MenuMain,
Expand All @@ -45,7 +57,8 @@ public enum Menus
MediaPlayer,
MediaDownloaded,
MenuInstall,
MenuInstallJuka
MenuInstallJuka,
VirtualKeyboard
}


Expand Down
36 changes: 22 additions & 14 deletions src/Juka/GUI/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using JukaCompiler;
using SDL2;
using System.Net.Sockets;
using System.Net;
using static Juka.GUI.Globals;

namespace Juka.GUI
Expand Down Expand Up @@ -45,24 +47,27 @@ public static void generateProgram()

public static void RenderText(string text, int x, int y, nint font, SDL.SDL_Color color)
{
nint surface = SDL_ttf.TTF_RenderText_Solid(font, text, color);
if (surface == nint.Zero)
if (text.Length > 0)
{
Console.WriteLine("Failed to render in rendetext! " + text);
return;
}
nint surface = SDL_ttf.TTF_RenderText_Solid(font, text, color);
if (surface == nint.Zero)
{
Console.WriteLine("Failed to render in rendetext! " + text);
return;
}

nint message = SDL.SDL_CreateTextureFromSurface(renderer, surface);
nint message = SDL.SDL_CreateTextureFromSurface(renderer, surface);

// Get the text surface dimensions
int w, h;
SDL.SDL_QueryTexture(message, out _, out _, out w, out h);
SDL.SDL_Rect dstrect = new SDL.SDL_Rect { x = x, y = y, w = w, h = h }; // Adjust position if needed
// Get the text surface dimensions
int w, h;
SDL.SDL_QueryTexture(message, out _, out _, out w, out h);
SDL.SDL_Rect dstrect = new SDL.SDL_Rect { x = x, y = y, w = w, h = h }; // Adjust position if needed

SDL.SDL_RenderCopy(renderer, message, nint.Zero, ref dstrect);
SDL.SDL_RenderCopy(renderer, message, nint.Zero, ref dstrect);

SDL.SDL_DestroyTexture(message);
SDL.SDL_FreeSurface(surface);
SDL.SDL_DestroyTexture(message);
SDL.SDL_FreeSurface(surface);
}
}

public static async Task GeneratePackages()
Expand All @@ -80,9 +85,12 @@ public static void GenerateMedia()
videoInfos = videos.Select(v => new VideoInfo
{
VideoId = v.VideoId,
Title = v.Title
Title = v.Title,
Description = v.Description
}).ToList();

}


}
}
26 changes: 16 additions & 10 deletions src/Juka/GUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using static Juka.GUI.Helper;
using static Juka.GUI.Renderer;
using static SDL2.SDL;

using static Juka.GUI.VirtualKeyboard;
namespace Juka.GUI;


Expand Down Expand Up @@ -32,7 +32,7 @@ public static Task GUI(string[] args)

try
{

string mapping = "030000005e0400008e02000014010000,Xbox 360 Controller,a:b0,b:b1,x:b2,y:b3,back:b6,guide:b8,start:b7,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,";
SDL.SDL_GameControllerAddMapping(mapping);
for (int i = 0; i < SDL.SDL_NumJoysticks(); i++)
Expand All @@ -42,7 +42,7 @@ public static Task GUI(string[] args)
controller = SDL.SDL_GameControllerOpen(i);
if (controller != nint.Zero)
{

SDL_HapticEffect effect = new SDL_HapticEffect()
{
type = SDL.SDL_HAPTIC_SINE,
Expand All @@ -58,7 +58,7 @@ public static Task GUI(string[] args)
SDL.SDL_HapticRunEffect(JoyHaptic, effectID, 1);

Console.WriteLine("Controller opened successfully!");
if(SDL.SDL_GameControllerHasRumbleTriggers(controller) == SDL.SDL_bool.SDL_TRUE)
if (SDL.SDL_GameControllerHasRumbleTriggers(controller) == SDL.SDL_bool.SDL_TRUE)
{
rumble2 = true;
}
Expand Down Expand Up @@ -92,9 +92,10 @@ public static Task GUI(string[] args)
}
SDL_ttf.TTF_Init();

nint fontBig = SDL_ttf.TTF_OpenFont(openFont, fontSize);
nint fontFooter = SDL_ttf.TTF_OpenFont(openFont, fontSizeC);
nint menufont = SDL_ttf.TTF_OpenFont(openFont, 20);
fontBig = SDL_ttf.TTF_OpenFont(openFont, fontSize);
fontFooter = SDL_ttf.TTF_OpenFont(openFont, fontSizeC);
menufont = SDL_ttf.TTF_OpenFont(openFont, 20);
descriptionfont = SDL_ttf.TTF_OpenFont(openFont, 10);

menuOptions[Menus.MenuMain] = new List<string>() { "Run File", "Update/Install", "Media Downloader", "Exit" };
menuTextures[Menus.MenuMain] = Helper.GenerateMenu(menuOptions[Menus.MenuMain], menufont, Colors.colorWhite);
Expand All @@ -121,6 +122,7 @@ public static Task GUI(string[] args)
menuTextures[Menus.MenuJuka] = Helper.GenerateMenu(menuOptions[Menus.MenuJuka], menufont, Colors.colorWhite);
//


// Main loop for handling input and rendering
while (!quit)
{
Expand All @@ -138,8 +140,8 @@ public static Task GUI(string[] args)
if (currentscreen == Menus.MenuMain)
{

var infoOffsetX = 800;
var infoOffsetY = 300;
var infoOffsetX = 820;
var infoOffsetY = 250;
var infoOffsetYr = 30;
RenderText("System: ", infoOffsetX, infoOffsetY + infoOffsetYr * 0, menufont, Colors.colorWhite);
RenderText("OS: " + systemInfo["platform"], infoOffsetX, infoOffsetY + infoOffsetYr * 1, menufont, Colors.colorWhite);
Expand Down Expand Up @@ -173,13 +175,17 @@ public static Task GUI(string[] args)
}
else if (currentscreen == Menus.MenuInstall)
{
RenderMenuInstall();
RenderMenuInstall();
}
else if (currentscreen == Menus.MenuInstallJuka)
{
RenderMenuJukaInstall();
}

if (keyboardOn)
{
RenderKeyboard();
}
Controller.MouseHandler();
SDL.SDL_Rect textRect2 = new SDL.SDL_Rect { x = mouseX, y = mouseY, w = 12, h = 12 };
SDL.SDL_SetRenderDrawColor(renderer, 255, 0, 0, 15); // Mouse Simulation if Controller is used
Expand Down
102 changes: 97 additions & 5 deletions src/Juka/GUI/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,120 @@ public static void MediaStreamer(nint fontFooter, SDL.SDL_Color colorWhite, SDL.
RenderText("Dowloaded to Juka/" + myfile + ". Press any button to continue", 160, 190, fontFooter, colorWhite);
}



public static void RenderMedia(nint fontFooter, SDL.SDL_Color colorWhite, SDL.SDL_Color colorRed)
{
RenderText("Videos: ", 160, 190, fontFooter, colorWhite);
menuDescript[Menus.MediaPlayer] = new List<Descript<int>>();
menuOptions[Menus.MediaPlayer] = new List<string>();


for (int i = 0; i < videoInfos.Count; i++)
{
RenderYThumb(videoInfos[i].VideoId,i);
RenderText(videoInfos[i].Title, 160, 200 + (100 * i), menufont, colorWhite);
var mydesc = videoInfos[i].Description;
string[] lines = mydesc.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

RenderText(videoInfos[i].Title, 160, 190 + fontSizeC * (i + 1), fontFooter, colorWhite);
for (int j = 0; j < Math.Min(lines.Length,4); j++)
{
if (lines[j] != "")
{
RenderText(lines[j], 160, 200 + (100 * i) + 25 + (15 * j), descriptionfont, colorWhite);
}
}
}
RenderText("Back", 160, 190 + fontSizeC * (videoInfos.Count + 1), fontFooter, colorWhite);

RenderText("Back", 160, 200 + (100 * videoInfos.Count), menufont, colorWhite);



// Iterate through menu options and render text
for (int i = 0; i < videoInfos.Count + 1; ++i)
for (int i = 0; i < videoInfos.Count; ++i)
{
SDL.SDL_Rect textRect2 = new SDL.SDL_Rect { x = 160, y = 190 + fontSizeC * (i + 1) + 5, w = 1280 - 160, h = fontSizeC };
var menuOptionDescript = new Descript<int>
{
X = 10,
Y = 200 + (100 * i),
Width = 1260,
Height = 72
};
menuDescript[Menus.MediaPlayer].Add(menuOptionDescript);
menuOptions[Menus.MediaPlayer].Add(videoInfos[i].VideoId);

SDL.SDL_Rect textRect2 = new SDL.SDL_Rect { x = 10, y = 200 + (100 * i), w = 1260, h = 72 };
if (HandleSelection(mouseX, mouseY, textRect2))
{

SDL.SDL_SetRenderDrawColor(renderer, 255, 0, 0, 15); // Red highlight for selection
SDL.SDL_RenderDrawRect(renderer, ref textRect2);
}
}





SDL.SDL_Rect textRect3 = new SDL.SDL_Rect { x = 160, y = 204 + (100 * videoInfos.Count), w = 55, h = 24 };


if (HandleSelection(mouseX, mouseY, textRect3))
{
SDL.SDL_SetRenderDrawColor(renderer, 255, 0, 0, 15); // Red highlight for selection
SDL.SDL_RenderDrawRect(renderer, ref textRect3);
}

var menuOptionDescript2 = new Descript<int>
{
X = 160,
Y = 204 + (100 * videoInfos.Count),
Width = 55,
Height = 24
};
menuDescript[Menus.MediaPlayer].Add(menuOptionDescript2);
menuOptions[Menus.MediaPlayer].Add("Back");


var menuOptionDescript3 = new Descript<int>
{
X = 430,
Y = 122,
Width = 400,
Height = 32
};
menuDescript[Menus.MediaPlayer].Add(menuOptionDescript3);
menuOptions[Menus.MediaPlayer].Add("Search");
RenderText("Search: ", 300, 120, fontFooter, colorWhite);

SDL.SDL_Rect searchRect = new SDL.SDL_Rect { x = 430, y = 122, w = 400, h = 32 };
SDL.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Juka background
SDL.SDL_RenderFillRect(renderer, ref searchRect);
RenderText(keyboardbuffer,430,122,fontFooter, colorRed);
}

public static void RenderYThumb(string videoId, int offset)
{

// Load the logo image
nint ylogo = SDL_image.IMG_Load(videoId+".jpg");
if (ylogo == nint.Zero)
{
Console.WriteLine("Failed to load image!" + SDL_image.IMG_GetError());
return;
}

// Define the destination rectangle for the logo on the screen
SDL.SDL_Rect imageRect = new SDL.SDL_Rect { x = 10, y = 200+(100*offset), w = 128, h = 72 }; // Adjust position and size as needed

// Create a texture from the image surface
nint texture = SDL.SDL_CreateTextureFromSurface(renderer, ylogo);

// Render the logo onto the renderer
SDL.SDL_RenderCopy(renderer, texture, nint.Zero, ref imageRect);

// Free the surface and destroy the texture after rendering
SDL.SDL_FreeSurface(ylogo);
SDL.SDL_DestroyTexture(texture);
}


Expand Down
Loading

0 comments on commit b68553e

Please sign in to comment.