DIYLabsED_ConsoleEmulator/compiled/linux-aarch64/data/ConsoleEmulator.pde
import java.awt.Desktop;
import java.net.URI;
import java.io.IOException;
import java.net.URISyntaxException;

Desktop desktop = Desktop.getDesktop();

final int SCENE_HOME = 0;
final int SCENE_THEME = 1;
final int SCENE_LANGUAGE = 2;
final int SCENE_PROJECT_SELECT = 3;
int scene = SCENE_HOME;

PImage consoleEmulatorLogo;

boolean isMouseReleased = false;
int mouseReleasedX;
int mouseReleasedY;


JSONObject coreConfig;
JSONObject saveFile;


UIButton themeMenuBack;
UIButton languageMenuBack;
UIButton homeMenuExit;
UIButton homeMenuTheme;
UIButton homeMenuLanguage;
UIButton homeMenuEditor;
UIButton homeMenuProject;
UIButton projectMenuBack;
UIButton homeMenuDocumentation;


void setup(){
  
  coreConfig = loadJSONObject("internal/core.json");
  saveFile = loadJSONObject("internal/save.json");
  
  loadDataFromSave();

  PImage icon = loadImage("internal/icon.png");
  surface.setIcon(icon);
  surface.setTitle("ConsoleEmulator " + coreConfig.getString("versionString") + "    |    DIY Labs 2024");
  
  
  if(coreConfig == null){
   super.exit(); 
  }
  
  consoleEmulatorLogo = loadImage(coreConfig.getString("logoFilepath"));
  consoleEmulatorLogo.resize(900, 0);
  
  if(!localisationInit()){
    exit();
  }

  if(!themeInit()){
    exit();
  }
    
  loadUIElements();
  
  size(900, 500);
    
}

void draw(){
 
  background(THEME_BACKGROUND_COL);
  
  switch(scene){
   
    case SCENE_HOME:
      homePage();
    break;
    
    case SCENE_THEME:
      themeSelector();
    break;
    
    case SCENE_LANGUAGE:
      languageSelector();
    break;
    
    case SCENE_PROJECT_SELECT:
      projectSelector();
    break;
    
    default:
      errorPageScene();
    break;
    
  }
  
  
}

void mouseReleased(){
  
  isMouseReleased = true; //<>//
  mouseReleasedX = mouseX;;
  mouseReleasedY = mouseY;
  
}


void keyReleased(){
   
  if(scene == SCENE_THEME){
      
    if(keyCode == UP){
      
      handleThemeDecrement();
      
    }
    
    if(keyCode == DOWN){
      
      handleThemeIncrement();

    }
    
  }
  
  if(scene == SCENE_LANGUAGE){
      
    if(keyCode == UP){
      
      handleLanguageDecrement();
      
    }
    
    if(keyCode == DOWN){
      
      handleLanguageIncrement();

    }
    
  }
  
  if(keyCode == TAB){
   
    frameCount = -1;
    
  }
  
  if(keyCode == ESC){
   
    if(scene == SCENE_THEME){
     
      scene = SCENE_HOME;
      
    }
    
  }
  
}

void loadUIElements(){
  
  int y = 100;
  int inc = 55;
  
  themeMenuBack          = new UIButton(140, 30, 5, 5, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_BACK"));
  languageMenuBack       = new UIButton(140, 30, 5, 5, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_BACK"));
  projectMenuBack        = new UIButton(140, 30, 5, 5, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_BACK"));

  homeMenuEditor         = new UIButton(350, 45, (width/2) - (350/2), y, 2, THEME_BACKGROUND_COL, THEME_BACKGROUND_COL, THEME_UNAVAILABLE_COL, THEME_UNAVAILABLE_COL, THEME_UNAVAILABLE_COL, THEME_UNAVAILABLE_COL, localisation.getString("UI_BUTTON_EDITOR"));  
  y += inc;
  homeMenuProject        = new UIButton(350, 45, (width/2) - (350/2), y, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_PROJECTS"));  
  y += inc;
  homeMenuDocumentation  = new UIButton(350, 45, (width/2) - (350/2), y, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_DOCUMENTATION"));  
  y += (inc*1.3);
  homeMenuTheme          = new UIButton(350, 45, (width/2) - (350/2), y, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_THEMES"));  
  y += inc;
  homeMenuLanguage       = new UIButton(350, 45, (width/2) - (350/2), y, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_LANGUAGE"));  
  y += (inc*1.3);
  homeMenuExit           = new UIButton(350, 45, (width/2) - (350/2), y, 2, THEME_BACKGROUND_COL, THEME_HIGHLIGHT_COL, THEME_HIGHLIGHT_COL, THEME_FOREGROUND_COL, THEME_FOREGROUND_COL, THEME_BACKGROUND_COL, localisation.getString("UI_BUTTON_EXIT"));
  
}

void errorPageScene(){
 
  fill(255, 0, 0);
  strokeWeight(0);
  background(0);
  
  textAlign(CENTER, CENTER);
  textSize(50);
  
  text(localisation.getString("UI_SCENE_ILLEGAL"), width/2, height/2);
  
}

void homePage(){
 
  if(homeMenuExit.handle()){
    super.exit();    
  }
  

  if(homeMenuTheme.handle()){
    scene = SCENE_THEME;  
  }
  
  if(homeMenuLanguage.handle()){
    scene = SCENE_LANGUAGE;  
  }
    
  if(homeMenuProject.handle()){
   scene = SCENE_PROJECT_SELECT; 
  }
  
  if(homeMenuDocumentation.handle()){
   openWebPage("https://github.com/DIYLabsED/ConsoleEmulator/wiki"); 
  }

  homeMenuEditor.handle();
 
  image(consoleEmulatorLogo, 0, 0);
  
  fill(THEME_FOREGROUND_COL);
  strokeWeight(0);
  textSize(14);
  textAlign(LEFT, BOTTOM);
  text("ConsoleEmulator " + coreConfig.getString("versionString"), 2, height - 2);
  textAlign(RIGHT, BOTTOM);
  text(coreConfig.getString("githubRepo"), width - 2, height - 2);
  
}


// prevent window from closing when escape key is pressed
// to close sketch, super.exit() is used
void exit(){
    
}

void openWebPage(String siteUrl) {
  
  try {
    URI site = new URI(siteUrl);
    if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
      desktop.browse(site);
    } else {
      println("Desktop does not support browse");
    }
  } catch(URISyntaxException e) {
    e.printStackTrace();
  } catch(IOException e) {
    e.printStackTrace();
  }
  
}

void loadDataFromSave(){
 
  selectedTheme = saveFile.getInt("theme");  
  selectedLanguage = saveFile.getInt("language");
  
}

void saveDataToSave(){

  JSONObject temp = new JSONObject();
  
  temp.setInt("theme", selectedTheme);
  temp.setInt("language", selectedLanguage);
  
  saveJSONObject(temp, "data/internal/save.json");
    
}
