Fix improperly prompting about unused local in another modules, fix full recompile, Text editor: restructure
This commit is contained in:
37
examples/text_editor/core/basic.lc
Normal file
37
examples/text_editor/core/basic.lc
Normal file
@@ -0,0 +1,37 @@
|
||||
import "raylib";
|
||||
import "std_types";
|
||||
|
||||
ClampInt :: proc(val: int, min: int, max: int): int {
|
||||
result := val;
|
||||
if (val < min) result = min;
|
||||
if (val > max) result = max;
|
||||
return result;
|
||||
}
|
||||
|
||||
MinInt :: proc(a: int, b: int): int {
|
||||
result := a;
|
||||
if (a > b) result = b;
|
||||
return result;
|
||||
}
|
||||
|
||||
MaxInt :: proc(a: int, b: int): int {
|
||||
result := b;
|
||||
if (a > b) result = a;
|
||||
return result;
|
||||
}
|
||||
|
||||
MaxFloat :: proc(a: float, b: float): float {
|
||||
if a > b return a;
|
||||
return b;
|
||||
}
|
||||
|
||||
MinFloat :: proc(a: float, b: float): float {
|
||||
if a > b return b;
|
||||
return a;
|
||||
}
|
||||
|
||||
ClampFloat :: proc(val: float, min: float, max: float): float {
|
||||
if (val > max) return max;
|
||||
if (val < min) return min;
|
||||
return val;
|
||||
}
|
||||
73
examples/text_editor/core/rect2p.lc
Normal file
73
examples/text_editor/core/rect2p.lc
Normal file
@@ -0,0 +1,73 @@
|
||||
Rect2P :: struct {
|
||||
min: Vector2;
|
||||
max: Vector2;
|
||||
}
|
||||
|
||||
GetRectSize :: proc(rect: Rect2P): Vector2 {
|
||||
result: Vector2 = {rect.max.x - rect.min.x, rect.max.y - rect.min.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
GetRectY :: proc(rect: Rect2P): f32 {
|
||||
result := GetRectSize(rect);
|
||||
return result.y;
|
||||
}
|
||||
|
||||
GetRectX :: proc(rect: Rect2P): f32 {
|
||||
result := GetRectSize(rect);
|
||||
return result.x;
|
||||
}
|
||||
|
||||
CutLeft :: proc(r: *Rect2P, value: f32): Rect2P {
|
||||
minx := r.min.x;
|
||||
r.min.x = MinFloat(r.max.x, r.min.x + value);
|
||||
return :Rect2P{
|
||||
{ minx, r.min.y},
|
||||
{r.min.x, r.max.y},
|
||||
};
|
||||
}
|
||||
|
||||
CutRight :: proc(r: *Rect2P, value: f32): Rect2P {
|
||||
maxx := r.max.x;
|
||||
r.max.x = MaxFloat(r.max.x - value, r.min.x);
|
||||
return :Rect2P{
|
||||
{r.max.x, r.min.y},
|
||||
{ maxx, r.max.y},
|
||||
};
|
||||
}
|
||||
|
||||
CutBottom :: proc(r: *Rect2P, value: f32): Rect2P { // Y is down
|
||||
maxy := r.max.y;
|
||||
r.max.y = MaxFloat(r.min.y, r.max.y - value);
|
||||
return :Rect2P{
|
||||
{r.min.x, r.max.y},
|
||||
{r.max.x, maxy},
|
||||
};
|
||||
}
|
||||
|
||||
CutTop :: proc(r: *Rect2P, value: f32): Rect2P { // Y is down
|
||||
miny := r.min.y;
|
||||
r.min.y = MinFloat(r.min.y + value, r.max.y);
|
||||
return :Rect2P{
|
||||
{r.min.x, miny},
|
||||
{r.max.x, r.min.y},
|
||||
};
|
||||
}
|
||||
|
||||
Rect2PToRectangle :: proc(r: Rect2P): Rectangle {
|
||||
result: Rectangle = {r.min.x, r.min.y, r.max.x - r.min.x, r.max.y - r.min.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2PSize :: proc(x: f32, y: f32, w: f32, h: f32): Rect2P {
|
||||
result: Rect2P = {{x, y}, {x+w, y+h}};
|
||||
return result;
|
||||
}
|
||||
|
||||
Shrink :: proc(r: Rect2P, v: f32): Rect2P {
|
||||
r.min.x += v;
|
||||
r.min.y += v;
|
||||
r.max.x -= v;
|
||||
r.max.y -= v;
|
||||
return r;
|
||||
}
|
||||
94
examples/text_editor/core/unicode.lc
Normal file
94
examples/text_editor/core/unicode.lc
Normal file
@@ -0,0 +1,94 @@
|
||||
UTF32_Result :: struct {
|
||||
out_str: u32;
|
||||
advance: int;
|
||||
error: int;
|
||||
}
|
||||
|
||||
UTF8_Result :: struct {
|
||||
out_str: [4]u8;
|
||||
len: int;
|
||||
error: int;
|
||||
}
|
||||
|
||||
IsUTF8ContinuationByte :: proc(c: u8): bool {
|
||||
result := (c & 0b11000000) == 0b10000000;
|
||||
return result;
|
||||
}
|
||||
|
||||
UTF8ToUTF32 :: proc(c: *uchar, max_advance: int): UTF32_Result {
|
||||
result: UTF32_Result;
|
||||
|
||||
if (c[0] & 0x80) == 0 { // Check if leftmost zero of first byte is unset
|
||||
if max_advance >= 1 {
|
||||
result.out_str = :u32(c[0]);
|
||||
result.advance = 1;
|
||||
}
|
||||
else result.error = 1;
|
||||
}
|
||||
|
||||
else if (c[0] & 0xe0) == 0xc0 {
|
||||
if (c[1] & 0xc0) == 0x80 { // Continuation byte required
|
||||
if max_advance >= 2 {
|
||||
result.out_str = (:u32(c[0] & 0x1f) << 6) | :u32(c[1] & 0x3f);
|
||||
result.advance = 2;
|
||||
}
|
||||
else result.error = 2;
|
||||
}
|
||||
else result.error = 2;
|
||||
}
|
||||
|
||||
else if (c[0] & 0xf0) == 0xe0 {
|
||||
if ((c[1] & 0xc0) == 0x80) && ((c[2] & 0xc0) == 0x80) { // Two continuation bytes required
|
||||
if max_advance >= 3 {
|
||||
result.out_str = (:u32(c[0] & 0xf) << 12) | (:u32(c[1] & 0x3f) << 6) | :u32(c[2] & 0x3f);
|
||||
result.advance = 3;
|
||||
}
|
||||
else result.error = 3;
|
||||
}
|
||||
else result.error = 3;
|
||||
}
|
||||
|
||||
else if (c[0] & 0xf8) == 0xf0 {
|
||||
if (c[1] & 0xc0) == 0x80 && (c[2] & 0xc0) == 0x80 && (c[3] & 0xc0) == 0x80 { // Three continuation bytes required
|
||||
if max_advance >= 4 {
|
||||
result.out_str = :u32(c[0] & 0xf) << 18 | :u32(c[1] & 0x3f) << 12 | :u32(c[2] & 0x3f) << 6 | :u32(c[3] & 0x3f);
|
||||
result.advance = 4;
|
||||
}
|
||||
else result.error = 4;
|
||||
}
|
||||
else result.error = 4;
|
||||
}
|
||||
else result.error = 4;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
UTF32ToUTF8 :: proc(codepoint: u32): UTF8_Result {
|
||||
result: UTF8_Result;
|
||||
|
||||
if codepoint <= 0x7F {
|
||||
result.len = 1;
|
||||
result.out_str[0] = :u8(codepoint);
|
||||
}
|
||||
else if codepoint <= 0x7FF {
|
||||
result.len = 2;
|
||||
result.out_str[0] = :u8 (0xc0 | 0x1f & (codepoint >> 6));
|
||||
result.out_str[1] = :u8 (0x80 | 0x3f & codepoint);
|
||||
}
|
||||
else if codepoint <= 0xFFFF { // 16 bit word
|
||||
result.len = 3;
|
||||
result.out_str[0] = :u8 (0xe0 | 0xf & (codepoint >> 12)); // 4 bits
|
||||
result.out_str[1] = :u8 (0x80 | 0x3f & (codepoint >> 6)); // 6 bits
|
||||
result.out_str[2] = :u8 (0x80 | 0x3f & codepoint); // 6 bits
|
||||
}
|
||||
else if codepoint <= 0x10FFFF { // 21 bit word
|
||||
result.len = 4;
|
||||
result.out_str[0] = :u8 (0xf0 | 0x7 & (codepoint >> 18)); // 3 bits
|
||||
result.out_str[1] = :u8 (0x80 | 0x3f & (codepoint >> 12)); // 6 bits
|
||||
result.out_str[2] = :u8 (0x80 | 0x3f & (codepoint >> 6)); // 6 bits
|
||||
result.out_str[3] = :u8 (0x80 | 0x3f & codepoint); // 6 bits
|
||||
}
|
||||
else result.error = 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user