From 75351eb8b58bbbb281d4877831b126144c424cd8 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Thu, 25 Feb 2021 22:53:02 +0100 Subject: [PATCH] src/conf.zig can now be used to configure color theme. Related things were improved. --- README.md | 2 +- src/conf.zig | 39 +++++++++++++++++++++++++++++++++++---- src/draw.zig | 15 +++++++-------- src/level.zig | 11 +++-------- src/main.zig | 10 ++++------ src/mouse.zig | 8 ++++---- src/raylib.zig | 4 ++++ 7 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 src/raylib.zig diff --git a/README.md b/README.md index 5a7b76b..d681c3c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Subject to change. * Fully keyboard driven modal editing. Mouse support is secondary. * GUI software made in [Zig](https://ziglang.org/) with [raylib](https://www.raylib.com/). -* Configurable (format unknown). +* Configurable (editing `src/conf.zig`). # Installation Runtime requirements: [raylib](https://www.raylib.com). diff --git a/src/conf.zig b/src/conf.zig index 7bf82ec..96a1652 100644 --- a/src/conf.zig +++ b/src/conf.zig @@ -3,11 +3,42 @@ // This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. -//! Values and types used by different files and that don't fit in any. -//! Read /etc/kble.conf and execute content. -/// Number passed to commands. -const std = @import("std"); +//! User defined settings. +const ray = @import("raylib.zig"); +const cell_type = @import("level.zig").cell_type; +/// raylib.Color initialisation wrapper. +fn col(r: u8, g: u8, b: u8) ray.Color { + return ray.Color{ + .r = r, + .g = g, + .b = b, + .a = 255, + }; +} + +// BEGIN USER CONFIG pub const mouse_enabled: bool = true; pub const mouse_left_btn: c_int = 0; pub const mouse_right_btn: c_int = 1; + +pub const theme = .{ + .background = ray.BLACK, + .mode = .{ + .normal = ray.GRAY, + .select = ray.BLUE, + .select_rect = ray.SKYBLUE, + .camera = ray.PURPLE, + }, +}; + +/// Return user defined color for corresponding cell ID. +pub fn cell_color(cell: cell_type) ray.Color { + return switch (cell) { + 0 => comptime col(026, 026, 026), // air + 1 => comptime col(144, 144, 144), // solid + 2 => comptime col(240, 010, 050), // red thing + else => ray.PURPLE, // undefined + }; +} +// END USER CONFIG diff --git a/src/draw.zig b/src/draw.zig index 4c1378a..7e0c7f9 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -3,10 +3,9 @@ // This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. -const ray = @cImport({ - @cInclude("raylib.h"); -}); +const ray = @import("raylib.zig"); +const conf = @import("conf.zig"); const Vec2 = @import("vec2.zig"); const Rect = @import("rect.zig"); const scaling = @import("scaling.zig"); @@ -17,10 +16,10 @@ pub fn cursor(scale: scaling.scale_type, offset: Vec2, pos: Vec2, mode: Mode) vo const x = (pos.x - offset.y) * scale; const y = (pos.y - offset.y) * scale; const color: ray.Color = switch (mode) { - .normal => ray.GRAY, - .select => ray.BLUE, - .rectangle => ray.SKYBLUE, - .camera => ray.PURPLE, + .normal => conf.theme.mode.normal, + .select => conf.theme.mode.select, + .rectangle => conf.theme.mode.select_rect, + .camera => conf.theme.mode.camera, }; ray.DrawRectangleLines(x, y, scale, scale, color); } @@ -31,5 +30,5 @@ pub fn rectangle_selection(scale: scaling.scale_type, offset: Vec2, rect: Rect) const w = (rect.right_x - rect.left_x + 1) * scale; const y = (rect.top_y - offset.y) * scale; const h = (rect.bottom_y - rect.top_y + 1) * scale; - ray.DrawRectangleLines(x, y, w, h, ray.SKYBLUE); + ray.DrawRectangleLines(x, y, w, h, conf.theme.mode.select_rect); } diff --git a/src/level.zig b/src/level.zig index 70961a5..53ce378 100644 --- a/src/level.zig +++ b/src/level.zig @@ -4,12 +4,11 @@ // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. //! Level structure, grid containing tile data. -const ray = @cImport({ - @cInclude("raylib.h"); -}); +const ray = @import("raylib.zig"); const std = @import("std"); const expect = std.testing.expect; +const conf = @import("conf.zig"); const Vec2 = @import("vec2.zig"); const Rect = @import("rect.zig"); const SelectionUpdate = @import("movement.zig").SelectionUpdate; @@ -190,11 +189,7 @@ pub fn draw(self: Self, scale: u16, offset: Vec2) void { var cy: Vec2.int_type = offset.y; while (cy < self.height) { const cell_content: cell_type = self.content[cy * self.width + cx]; - const color = switch (cell_content) { - 0 => ray.Color{ .r = 26, .g = 26, .b = 26, .a = 255 }, - 1 => ray.Color{ .r = 144, .g = 144, .b = 144, .a = 255 }, - else => ray.PURPLE, //unknown - }; + const color: ray.Color = conf.cell_color(cell_content); ray.DrawRectangle(x + 1, y + 1, scale - 2, scale - 2, color); y += scale; cy += 1; diff --git a/src/main.zig b/src/main.zig index 2e4b377..1a8d39b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,9 +3,7 @@ // This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. -const ray = @cImport({ - @cInclude("raylib.h"); -}); +const ray = @import("raylib.zig"); const std = @import("std"); const maxInt = std.math.maxInt; const log = std.log.info; @@ -257,13 +255,13 @@ pub fn main() void { ray.BeginDrawing(); defer ray.EndDrawing(); - ray.ClearBackground(ray.BLACK); + ray.ClearBackground(conf.theme.background); level.draw(scale, camera); level.draw_selection(scale, camera); draw.cursor(scale, camera, cursor, mode); if (mode == Mode.rectangle) draw.rectangle_selection(scale, camera, Rect.init_from_vec2(cursor, cursor_before)); - mouse.draw(scale, camera); - //ray.DrawFPS(0, 0); + if (conf.mouse_enabled) + mouse.draw(scale, camera); } } diff --git a/src/mouse.zig b/src/mouse.zig index e0a76f8..d45513e 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -4,11 +4,10 @@ // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. //! Mouse (eurk) logic and operations. -const ray = @cImport({ - @cInclude("raylib.h"); -}); +const ray = @import("raylib.zig"); const std = @import("std"); +const conf = @import("conf.zig"); const Vec2 = @import("vec2.zig"); const Rect = @import("rect.zig"); const scaling = @import("scaling.zig"); @@ -42,7 +41,7 @@ pub fn draw(self: *Self, scale: scaling.scale_type, offset: Vec2) void { MouseMode.sel => { const x = (self.pos.x - offset.y) * scale; const y = (self.pos.y - offset.y) * scale; - ray.DrawRectangleLines(x, y, scale, scale, ray.SKYBLUE); + ray.DrawRectangleLines(x, y, scale, scale, conf.theme.mode.select); }, MouseMode.rect_sel => { const rect = Rect.init_from_vec2(self.pos, self.start_pos); @@ -51,3 +50,4 @@ pub fn draw(self: *Self, scale: scaling.scale_type, offset: Vec2) void { else => {}, } } + diff --git a/src/raylib.zig b/src/raylib.zig new file mode 100644 index 0000000..b0afd61 --- /dev/null +++ b/src/raylib.zig @@ -0,0 +1,4 @@ +/// Simplest imaginable raylib wrapper +pub usingnamespace @cImport({ + @cInclude("raylib.h"); +});