Skip to content

Commit

Permalink
Polish UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Jan 3, 2024
1 parent ef49d61 commit 6e2a777
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 38 deletions.
5 changes: 0 additions & 5 deletions src/Asteroids.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ pub inline fn main() void {

// Default View on startup is the Splash Screen
var current_view: Shared.View.Views = Shared.View.Views.RaylibSplashScreenView;

// If DebugView is configure use that instead
if (settings.Debug and settings.DebugView != null) {
current_view = @enumFromInt(settings.DebugView.?);
}
defer DeinitViews();

// Load locale
Expand Down
6 changes: 4 additions & 2 deletions src/Helpers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const Helpers = struct {
fontSize: f32,
screenWidth: f32,
positionY: f32,
) void {
) raylib.Vector2 {
const TitleTextSize = raylib.measureTextEx(
font,
text,
Expand All @@ -29,14 +29,15 @@ pub const Helpers = struct {
@floatFromInt(font.glyphPadding),
color,
);
return TitleTextSize;
}
pub inline fn DrawTextCentered(
text: [:0]const u8,
color: raylib.Color,
fontSize: f32,
screenWidth: f32,
positionY: f32,
) void {
) i32 {
const TitleTextSize = raylib.measureText(text, @intFromFloat(fontSize));
raylib.drawText(
text,
Expand All @@ -45,6 +46,7 @@ pub const Helpers = struct {
@intFromFloat(fontSize),
color,
);
return TitleTextSize;
}
pub inline fn DrawTextWithFontRightAligned(
text: [:0]const u8,
Expand Down
4 changes: 3 additions & 1 deletion src/Localelizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pub const Locale = struct {
Menu_Quit: [:0]const u8,
Paused: [:0]const u8,
Continue: [:0]const u8,
Time: [:0]const u8,
Score: [:0]const u8,
HighScore: [:0]const u8,
ScoreNotFound: [:0]const u8,
Quit: [:0]const u8,
Game_Over: [:0]const u8,
Missing_Text: [:0]const u8,
Expand Down
4 changes: 3 additions & 1 deletion src/Locales/english.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub const english = Locale{
.Paused = "Paused",
.Continue = "Continue",
.Quit = "Quit",
.Time = "Time: ",
.Score = "Score: ",
.HighScore = "HighScore: ",
.ScoreNotFound = "Score Unknown!",
.Game_Over = "Game Over",
.Missing_Text = "Text not found!",
};
4 changes: 3 additions & 1 deletion src/Locales/french.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub const french = Locale{
.Paused = "En pause",
.Continue = "Continuer",
.Quit = "Quitter",
.Time = "Temps : ",
.Score = "Score : ",
.HighScore = "Meilleur score : ",
.ScoreNotFound = "Score inconnu !",
.Game_Over = "Fin de partie",
.Missing_Text = "Texte non trouvé !",
};
4 changes: 3 additions & 1 deletion src/Locales/spanish.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub const spanish = Locale{
.Paused = "Pausado",
.Continue = "Continuar",
.Quit = "Salir",
.Time = "Tiempo: ",
.Score = "Puntuacion: ",
.HighScore = "Puntuacion mas alta: ",
.ScoreNotFound = "!Puntuacion desconocida!",
.Game_Over = "Fin del Juego",
.Missing_Text = "!Texto no encontrado!",
};
8 changes: 0 additions & 8 deletions src/Settings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub const Settings = struct {
CurrentResolution: Resolution,
TargetFPS: i32,
Debug: bool,
DebugView: ?i32,
NoDamage: ?bool,
UserLocale: Locales,
HighScore: u64,
Expand Down Expand Up @@ -71,7 +70,6 @@ pub const Settings = struct {
},
.TargetFPS = 60,
.Debug = if (settings.value.object.contains("Debug")) settings.value.object.get("Debug").?.bool else default_settings.Debug,
.DebugView = if (settings.value.object.contains("DebugView")) @intCast(settings.value.object.get("DebugView").?.integer) else default_settings.DebugView,
.NoDamage = if (settings.value.object.contains("NoDamage")) settings.value.object.get("NoDamage").?.bool else default_settings.NoDamage,
.UserLocale = if (settings.value.object.contains("UserLocale")) @enumFromInt(settings.value.object.get("UserLocale").?.integer) else default_settings.UserLocale,
.HighScore = highScore,
Expand Down Expand Up @@ -123,7 +121,6 @@ pub const Settings = struct {
},
.TargetFPS = if (settings.TargetFPS == 0) 0 else @max(settings.TargetFPS, 60),
.Debug = settings.Debug,
.DebugView = settings.DebugView,
.NoDamage = settings.NoDamage,
.UserLocale = settings.UserLocale,
.HighScore = settings.HighScore,
Expand All @@ -145,10 +142,6 @@ pub const Settings = struct {
}
try out.objectField("Debug");
try out.write(self.Debug);
if (builtin.target.os.tag != .wasi or self.DebugView != null) {
try out.objectField("DebugView");
try out.write(self.DebugView);
}
if (builtin.target.os.tag != .wasi or self.NoDamage != null) {
try out.objectField("NoDamage");
try out.write(self.NoDamage);
Expand Down Expand Up @@ -177,7 +170,6 @@ pub const Settings = struct {
.CurrentResolution = Resolution{ .Width = 1600, .Height = 900 },
.TargetFPS = 60,
.Debug = false,
.DebugView = null,
.NoDamage = null,
.UserLocale = Locales.unknown,
.HighScore = 0,
Expand Down
2 changes: 0 additions & 2 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(

// Initialize game variables
pub inline fn init() void {
//starScape = Starscape.init(screenSize);

Shared.Music.SetVolume(.BackgroundMusic, 0.35);

const settings = Shared.Settings.GetSettings();
Expand Down
4 changes: 4 additions & 0 deletions src/ViewModels/GameOverViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ pub const GameOverViewModel = Shared.View.ViewModel.Create(
pub var score: u64 = 0;
pub var highScore: u64 = 0;

pub var frameCount: f32 = 0;

pub inline fn GameOver(Score: u64, HighScore: u64) void {
frameCount = 0.75;

score = Score;
highScore = HighScore;

Expand Down
2 changes: 1 addition & 1 deletion src/ViewModels/MenuViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const AsteroidsVM = AsteroidsViewModel.GetVM();
pub const MenuViewModel = Shared.View.ViewModel.Create(
struct {
pub var selection = Selection.Start;
pub var Rectangles: [std.enums.directEnumArrayLen(Selection, 0) - 1]raylib.Rectangle = undefined;
pub var Rectangles: [std.enums.directEnumArrayLen(Selection, 0)]raylib.Rectangle = undefined;

pub var frameCount: f32 = 0;

Expand Down
4 changes: 4 additions & 0 deletions src/ViewModels/PausedViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub const PausedViewModel = Shared.View.ViewModel.Create(
pub var View: Shared.View.Views = undefined;
pub var BackgroundTexture: ?raylib.Texture = null;

pub var frameCount: f32 = 0;

pub inline fn PauseView(v: Shared.View.Views) void {
frameCount = 0;

raylib.endDrawing();
const img = raylib.loadImageFromScreen();
defer img.unload();
Expand Down
28 changes: 18 additions & 10 deletions src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ const vm: type = AsteroidsViewModel.GetVM();

fn DrawFunction() Shared.View.Views {
raylib.clearBackground(Shared.Color.Tone.Dark);
raylib.drawRectangleLinesEx(
raylib.Rectangle.init(0, 0, vm.screenSize.x, vm.screenSize.y),
5,
Shared.Color.Green.Light,
);

vm.starScape.Draw(
vm.screenSize.x,
vm.screenSize.y,
vm.player.position,
);

raylib.drawRectangleLinesEx(
raylib.Rectangle.init(0, 0, vm.screenSize.x, vm.screenSize.y),
3,
Shared.Color.Green.Light,
);

// Flash shield if player hurt
if (vm.player.status != .default) {
raylib.drawCircleV(vm.player.position, vm.player.collider.z * 1.75, Shared.Color.Yellow.Base.alpha((Shared.Random.Get().float(f32) * 0.2) + 0.1));
}

// Draw spaceship
vm.player.Draw(vm.shipHeight, vm.PLAYER_BASE_SIZE);

Expand Down Expand Up @@ -79,10 +85,10 @@ fn DrawWithCamera() Shared.View.Views {
);
const result = camera.Draw(Shared.View.Views, &DrawFunction);

// Flash screen if player hurt
if (vm.player.status != .default) {
raylib.drawRectangleV(raylib.Vector2.init(0, 0), screenSize, Shared.Color.Red.Base.alpha(0.1));
}
//Flash screen if player hurt
// if (vm.player.status != .default) {
// raylib.drawRectangleV(raylib.Vector2.init(0, 0), screenSize, Shared.Color.Red.Base.alpha(0.1));
// }

// Draw Health Bar
const onePixelScaled: f32 = 0.0025 * screenWidth;
Expand Down Expand Up @@ -130,9 +136,11 @@ fn DrawWithCamera() Shared.View.Views {
Shared.Color.Gray.Dark,
);

const locale = Shared.Locale.GetLocale();

var scoreBuffer: [64]u8 = undefined;
Shared.Helpers.DrawTextRightAligned(
std.fmt.bufPrintZ(&scoreBuffer, "Score: {}", .{vm.score}) catch "Score Unknown!",
std.fmt.bufPrintZ(&scoreBuffer, "{s}{}", .{ locale.?.Score, vm.score }) catch locale.?.ScoreNotFound,
Shared.Color.Blue.Light,
onePixelScaled * 10,
screenWidth - (5 * onePixelScaled),
Expand Down
46 changes: 45 additions & 1 deletion src/Views/GameOverView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub fn DrawFunction() Shared.View.Views {
const startY = @divFloor(screenHeight, 4);
const startX = @divFloor(screenWidth, 4);

vm.frameCount += raylib.getFrameTime();
const selectionHidden = if (vm.frameCount < 0.75) false else true;

if (vm.frameCount > 1.25) {
vm.frameCount = 0;
}

// Draw the background texture
if (vm.BackgroundTexture != null) {
const bg: raylib.Texture = vm.BackgroundTexture.?;
Expand All @@ -42,7 +49,6 @@ pub fn DrawFunction() Shared.View.Views {
const foregroundColor = Shared.Color.Blue.Base;
const backgroundColor = Shared.Color.Blue.Light.alpha(0.75);
const accentColor = Shared.Color.Blue.Dark;
_ = accentColor;

const background_rec = raylib.Rectangle.init(
startX,
Expand Down Expand Up @@ -87,6 +93,44 @@ pub fn DrawFunction() Shared.View.Views {
foregroundColor,
);

_ = Shared.Helpers.DrawTextWithFontCentered(
locale.Continue,
if (selectionHidden) Shared.Color.Transparent else accentColor,
font,
fontSize,
screenWidth,
((screenHeight - fontSize) / 2) + (fontSize * 2),
);

var scoreBuffer: [64]u8 = undefined;
if (vm.score > vm.highScore) {
_ = Shared.Helpers.DrawTextWithFontCentered(
std.fmt.bufPrintZ(&scoreBuffer, "{s}{}", .{ locale.HighScore, vm.score }) catch locale.ScoreNotFound,
if (selectionHidden) Shared.Color.Transparent else accentColor,
font,
fontSize,
screenWidth,
((screenHeight - fontSize) / 2) - (fontSize * 2),
);
} else {
_ = Shared.Helpers.DrawTextWithFontCentered(
std.fmt.bufPrintZ(&scoreBuffer, "{s}{}", .{ locale.Score, vm.score }) catch locale.ScoreNotFound,
foregroundColor,
font,
fontSize,
screenWidth,
((screenHeight - fontSize) / 2) - (fontSize * 2),
);
_ = Shared.Helpers.DrawTextWithFontCentered(
std.fmt.bufPrintZ(&scoreBuffer, "{s}{}", .{ locale.HighScore, vm.highScore }) catch locale.ScoreNotFound,
foregroundColor,
font,
fontSize,
screenWidth,
((screenHeight - fontSize) / 2) - (fontSize * 3.1),
);
}

if (vm.startTime <= 0 and Shared.Input.A_Pressed()) {
if (vm.BackgroundTexture != null) {
vm.BackgroundTexture.?.unload();
Expand Down
4 changes: 2 additions & 2 deletions src/Views/MenuView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn DrawFunction() Shared.View.Views {
const title = locale.Title;
const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth());
const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight());
const fontSize = screenWidth / 20;
const fontSize = screenWidth / 25;
const startY = screenHeight / 4;

const playerPosition = raylib.Vector2.init(
Expand Down Expand Up @@ -145,7 +145,7 @@ pub fn DrawFunction() Shared.View.Views {
// Disable settings and quit options in WASM
if (index == 1) break;
}
if (index == 3) break;
if (index == 2) break;
}

if (Shared.Input.A_Pressed()) {
Expand Down
13 changes: 10 additions & 3 deletions src/Views/PausedView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn DrawFunction() Shared.View.Views {
const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth());
const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight());
const font = Shared.Font.Get(.Unknown);
const fontSize = @divFloor(screenWidth, 25);
const fontSize = @divFloor(screenWidth, 30);
const startY = @divFloor(screenHeight, 4);
const startX = @divFloor(screenWidth, 4);

Expand Down Expand Up @@ -109,6 +109,13 @@ pub fn DrawFunction() Shared.View.Views {
const combinedYesNoWidth: f32 = yesSize.x + noSize.x;
const YesNoPadding = (background_rec.width - combinedYesNoWidth) / 3;

vm.frameCount += raylib.getFrameTime();
const selectionHidden = if (vm.frameCount < 0.75) false else true;

if (vm.frameCount > 1.25) {
vm.frameCount = 0;
}

raylib.drawTextEx(
font,
yes,
Expand All @@ -118,7 +125,7 @@ pub fn DrawFunction() Shared.View.Views {
),
yesSizeF,
@floatFromInt(font.glyphPadding),
if (vm.selection == PauseOptions.Continue) accentColor else foregroundColor,
if (vm.selection == PauseOptions.Continue) (if (selectionHidden) Shared.Color.Transparent else accentColor) else foregroundColor,
);

raylib.drawTextEx(
Expand All @@ -130,7 +137,7 @@ pub fn DrawFunction() Shared.View.Views {
),
noSizeF,
@floatFromInt(font.glyphPadding),
if (vm.selection == PauseOptions.Quit) accentColor else foregroundColor,
if (vm.selection == PauseOptions.Quit) (if (selectionHidden) Shared.Color.Transparent else accentColor) else foregroundColor,
);

if (Shared.Input.Left_Pressed() and vm.selection != PauseOptions.Continue) {
Expand Down

0 comments on commit 6e2a777

Please sign in to comment.