Skip to content

Commit

Permalink
raylib spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Oct 7, 2024
1 parent cf50d4a commit 1bad8b2
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 161 deletions.
13 changes: 9 additions & 4 deletions selfdrive/ui/SConscript
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import json
Import('qt_env', 'arch', 'common', 'messaging', 'visionipc', 'transformations')
Import('env', 'qt_env', 'arch', 'common', 'messaging', 'visionipc', 'transformations')

base_libs = [common, messaging, visionipc, transformations,
'm', 'OpenCL', 'ssl', 'crypto', 'pthread'] + qt_env["LIBS"]
Expand Down Expand Up @@ -70,10 +70,8 @@ if GetOption('extras'):
if GetOption('extras') and arch != "Darwin":
qt_env.SharedLibrary("qt/python_helpers", ["qt/qt_window.cc"], LIBS=qt_libs)

# spinner and text window
# text window
qt_env.Program("_text", ["qt/text.cc"], LIBS=qt_libs)
qt_env.Program("_spinner", ["qt/spinner.cc"], LIBS=qt_libs)


# setup and factory resetter
qt_env.Program("qt/setup/reset", ["qt/setup/reset.cc"], LIBS=qt_libs)
Expand Down Expand Up @@ -110,3 +108,10 @@ if GetOption('extras') and arch != "Darwin":
# build watch3
if arch in ['x86_64', 'aarch64', 'Darwin'] or GetOption('extras'):
qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'msgq', 'visionipc'])

# raylib env
raylib_env = env.Clone()
raylib_lib = env.Library("raylib_lib", ['raylib/util.cc'], LIBS='raylib')
raylib_libs = ['raylib', raylib_lib]
raylib_env['LIBPATH'] += [f'#third_party/raylib/{arch}/']
raylib_env.Program("_spinner", ["raylib/spinner.cc"], LIBS=raylib_libs)
120 changes: 0 additions & 120 deletions selfdrive/ui/qt/spinner.cc

This file was deleted.

37 changes: 0 additions & 37 deletions selfdrive/ui/qt/spinner.h

This file was deleted.

71 changes: 71 additions & 0 deletions selfdrive/ui/raylib/spinner.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <algorithm>
#include <cmath>
#include <iostream>

#include "selfdrive/ui/raylib/util.h"
#include "third_party/raylib/include/raylib.h"

constexpr int kProgressBarWidth = 1000;
constexpr int kProgressBarHeight = 20;
constexpr float kRotationRate = 12.0f;
constexpr int kMargin = 200;
constexpr int kTextureSize = 360;
constexpr int kFontSize = 80;

int main(int argc, char *argv[]) {
initApp("spinner", 30);

// Turn off input buffering for std::cin
std::cin.sync_with_stdio(false);
std::cin.tie(nullptr);

const int screenWidth = GetScreenWidth();
const int screenHeight = GetScreenHeight();
Vector2 screenCenter = {screenWidth / 2.0f, screenHeight / 2.0f};

Texture2D commaTexture = LoadTextureResized("../assets/img_spinner_comma.png", kTextureSize);
Texture2D spinnerTexture = LoadTextureResized("../assets/img_spinner_track.png", kTextureSize);

const Vector2 spinnerOrigin{spinnerTexture.width / 2.0f, spinnerTexture.height / 2.0f};
const Vector2 commaPosition{screenCenter.x - commaTexture.width / 2.0f, screenCenter.y - commaTexture.height / 2.0f};

float rotationAngle = 0.0f;
std::string userInput;

while (!WindowShouldClose()) {
rotationAngle = fmod(rotationAngle + kRotationRate, 360.0f);

BeginDrawing();
ClearBackground(BLACK);

// Draw rotating spinner and static comma logo
DrawTexturePro(spinnerTexture, {0, 0, (float)spinnerTexture.width, (float)spinnerTexture.height},
{screenCenter.x, screenCenter.y, (float)spinnerTexture.width, (float)spinnerTexture.height},
spinnerOrigin, rotationAngle, WHITE);
DrawTextureV(commaTexture, commaPosition, WHITE);

// Check for user input
if (std::cin.rdbuf()->in_avail() > 0) {
std::getline(std::cin, userInput);
}

// Display either a progress bar or user input text based on input
if (!userInput.empty()) {
float yPos = screenHeight - kMargin - kProgressBarHeight;
if (std::all_of(userInput.begin(), userInput.end(), ::isdigit)) {
int progressValue = std::clamp(std::stoi(userInput), 0, 100);
Rectangle progressBar = {screenCenter.x - kProgressBarWidth / 2.0f, yPos, kProgressBarWidth, kProgressBarHeight};
DrawRectangleRounded(progressBar, 0.5f, 10, GRAY);
DrawRectangleRounded({progressBar.x, progressBar.y, progressBar.width * (progressValue / 100.0f), progressBar.height}, 0.5f, 10, RAYWHITE);
} else {
Vector2 textSize = MeasureTextEx(getFont(), userInput.c_str(), kFontSize, 1.0);
DrawTextEx(getFont(), userInput.c_str(), {screenCenter.x - textSize.x / 2, yPos}, kFontSize, 1.0, WHITE);
}
}

EndDrawing();
}

CloseWindow();
return 0;
}
56 changes: 56 additions & 0 deletions selfdrive/ui/raylib/util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "selfdrive/ui/raylib/util.h"

#include <array>

#undef GREEN
#undef RED
#undef YELLOW
#include "common/swaglog.h"
#include "system/hardware/hw.h"

constexpr std::array<const char *, static_cast<int>(FontWeight::Count)> FONT_FILE_PATHS = {
"../assets/fonts/Inter-Black.ttf",
"../assets/fonts/Inter-Bold.ttf",
"../assets/fonts/Inter-ExtraBold.ttf",
"../assets/fonts/Inter-ExtraLight.ttf",
"../assets/fonts/Inter-Medium.ttf",
"../assets/fonts/Inter-Regular.ttf",
"../assets/fonts/Inter-SemiBold.ttf",
"../assets/fonts/Inter-Thin.ttf",
};

struct FontManager {
FontManager() {
for (int i = 0; i < fonts.size(); ++i) {
fonts[i] = LoadFontEx(FONT_FILE_PATHS[i], 120, nullptr, 250);
SetTextureFilter(fonts[i].texture, TEXTURE_FILTER_TRILINEAR);
}
}

~FontManager() {
for (auto &f : fonts) UnloadFont(f);
}

std::array<Font, static_cast<int>(FontWeight::Count)> fonts;
};

const Font& getFont(FontWeight weight) {
static FontManager font_manager;
return font_manager.fonts[(int)weight];
}

Texture2D LoadTextureResized(const char *fileName, int size) {
Image img = LoadImage(fileName);
ImageResize(&img, size, size);
Texture2D texture = LoadTextureFromImage(img);
SetTextureFilter(texture, TEXTURE_FILTER_TRILINEAR);
return texture;
}

void initApp(const char *title, int fps) {
Hardware::set_display_power(true);
Hardware::set_brightness(65);
SetTraceLogLevel(LOG_NONE);
InitWindow(0, 0, title);
SetTargetFPS(fps);
}
21 changes: 21 additions & 0 deletions selfdrive/ui/raylib/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>

#include "third_party/raylib/include/raylib.h"

enum class FontWeight {
Normal,
Bold,
ExtraBold,
ExtraLight,
Medium,
Regular,
SemiBold,
Thin,
Count // To represent the total number of fonts
};

void initApp(const char *title, int fps);
const Font& getFont(FontWeight weight = FontWeight::Normal);
Texture2D LoadTextureResized(const char *fileName, int size);

0 comments on commit 1bad8b2

Please sign in to comment.